admin-ajax.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * WordPress Ajax Process Execution
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. *
  8. * @link https://codex.wordpress.org/AJAX_in_Plugins
  9. */
  10. /**
  11. * Executing Ajax process.
  12. *
  13. * @since 2.1.0
  14. */
  15. define( 'DOING_AJAX', true );
  16. if ( ! defined( 'WP_ADMIN' ) ) {
  17. define( 'WP_ADMIN', true );
  18. }
  19. /** Load WordPress Bootstrap */
  20. require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
  21. /** Allow for cross-domain requests (from the front end). */
  22. send_origin_headers();
  23. // Require an action parameter
  24. if ( empty( $_REQUEST['action'] ) )
  25. wp_die( '0', 400 );
  26. /** Load WordPress Administration APIs */
  27. require_once( ABSPATH . 'wp-admin/includes/admin.php' );
  28. /** Load Ajax Handlers for WordPress Core */
  29. require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
  30. @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
  31. @header( 'X-Robots-Tag: noindex' );
  32. send_nosniff_header();
  33. nocache_headers();
  34. /** This action is documented in wp-admin/admin.php */
  35. do_action( 'admin_init' );
  36. $core_actions_get = array(
  37. 'fetch-list', 'ajax-tag-search', 'wp-compression-test', 'imgedit-preview', 'oembed-cache',
  38. 'autocomplete-user', 'dashboard-widgets', 'logged-in',
  39. );
  40. $core_actions_post = array(
  41. 'oembed-cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',
  42. 'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment',
  43. 'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
  44. 'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes',
  45. 'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
  46. 'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
  47. 'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
  48. 'save-widget', 'delete-inactive-widgets', 'set-post-thumbnail', 'date_format', 'time_format',
  49. 'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
  50. 'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
  51. 'send-attachment-to-editor', 'save-attachment-order', 'heartbeat', 'get-revision-diffs',
  52. 'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',
  53. 'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'crop-image',
  54. 'generate-password', 'save-wporg-username', 'delete-plugin', 'search-plugins',
  55. 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme', 'install-theme',
  56. 'get-post-thumbnail-html', 'get-community-events', 'edit-theme-plugin-file',
  57. 'wp-privacy-export-personal-data',
  58. 'wp-privacy-erase-personal-data',
  59. );
  60. // Deprecated
  61. $core_actions_post_deprecated = array( 'wp-fullscreen-save-post', 'press-this-save-post', 'press-this-add-category' );
  62. $core_actions_post = array_merge( $core_actions_post, $core_actions_post_deprecated );
  63. // Register core Ajax calls.
  64. if ( ! empty( $_GET['action'] ) && in_array( $_GET['action'], $core_actions_get ) )
  65. add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );
  66. if ( ! empty( $_POST['action'] ) && in_array( $_POST['action'], $core_actions_post ) )
  67. add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );
  68. add_action( 'wp_ajax_nopriv_heartbeat', 'wp_ajax_nopriv_heartbeat', 1 );
  69. if ( is_user_logged_in() ) {
  70. // If no action is registered, return a Bad Request response.
  71. if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
  72. wp_die( '0', 400 );
  73. }
  74. /**
  75. * Fires authenticated Ajax actions for logged-in users.
  76. *
  77. * The dynamic portion of the hook name, `$_REQUEST['action']`,
  78. * refers to the name of the Ajax action callback being fired.
  79. *
  80. * @since 2.1.0
  81. */
  82. do_action( 'wp_ajax_' . $_REQUEST['action'] );
  83. } else {
  84. // If no action is registered, return a Bad Request response.
  85. if ( ! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ) ) {
  86. wp_die( '0', 400 );
  87. }
  88. /**
  89. * Fires non-authenticated Ajax actions for logged-out users.
  90. *
  91. * The dynamic portion of the hook name, `$_REQUEST['action']`,
  92. * refers to the name of the Ajax action callback being fired.
  93. *
  94. * @since 2.8.0
  95. */
  96. do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
  97. }
  98. // Default status
  99. wp_die( '0' );