admin-ajax.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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( __DIR__ ) . '/wp-load.php';
  21. /** Allow for cross-domain requests (from the front end). */
  22. send_origin_headers();
  23. header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
  24. header( 'X-Robots-Tag: noindex' );
  25. // Require a valid action parameter.
  26. if ( empty( $_REQUEST['action'] ) || ! is_scalar( $_REQUEST['action'] ) ) {
  27. wp_die( '0', 400 );
  28. }
  29. /** Load WordPress Administration APIs */
  30. require_once ABSPATH . 'wp-admin/includes/admin.php';
  31. /** Load Ajax Handlers for WordPress Core */
  32. require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
  33. send_nosniff_header();
  34. nocache_headers();
  35. /** This action is documented in wp-admin/admin.php */
  36. do_action( 'admin_init' );
  37. $core_actions_get = array(
  38. 'fetch-list',
  39. 'ajax-tag-search',
  40. 'wp-compression-test',
  41. 'imgedit-preview',
  42. 'oembed-cache',
  43. 'autocomplete-user',
  44. 'dashboard-widgets',
  45. 'logged-in',
  46. 'rest-nonce',
  47. );
  48. $core_actions_post = array(
  49. 'oembed-cache',
  50. 'image-editor',
  51. 'delete-comment',
  52. 'delete-tag',
  53. 'delete-link',
  54. 'delete-meta',
  55. 'delete-post',
  56. 'trash-post',
  57. 'untrash-post',
  58. 'delete-page',
  59. 'dim-comment',
  60. 'add-link-category',
  61. 'add-tag',
  62. 'get-tagcloud',
  63. 'get-comments',
  64. 'replyto-comment',
  65. 'edit-comment',
  66. 'add-menu-item',
  67. 'add-meta',
  68. 'add-user',
  69. 'closed-postboxes',
  70. 'hidden-columns',
  71. 'update-welcome-panel',
  72. 'menu-get-metabox',
  73. 'wp-link-ajax',
  74. 'menu-locations-save',
  75. 'menu-quick-search',
  76. 'meta-box-order',
  77. 'get-permalink',
  78. 'sample-permalink',
  79. 'inline-save',
  80. 'inline-save-tax',
  81. 'find_posts',
  82. 'widgets-order',
  83. 'save-widget',
  84. 'delete-inactive-widgets',
  85. 'set-post-thumbnail',
  86. 'date_format',
  87. 'time_format',
  88. 'wp-remove-post-lock',
  89. 'dismiss-wp-pointer',
  90. 'upload-attachment',
  91. 'get-attachment',
  92. 'query-attachments',
  93. 'save-attachment',
  94. 'save-attachment-compat',
  95. 'send-link-to-editor',
  96. 'send-attachment-to-editor',
  97. 'save-attachment-order',
  98. 'media-create-image-subsizes',
  99. 'heartbeat',
  100. 'get-revision-diffs',
  101. 'save-user-color-scheme',
  102. 'update-widget',
  103. 'query-themes',
  104. 'parse-embed',
  105. 'set-attachment-thumbnail',
  106. 'parse-media-shortcode',
  107. 'destroy-sessions',
  108. 'install-plugin',
  109. 'update-plugin',
  110. 'crop-image',
  111. 'generate-password',
  112. 'save-wporg-username',
  113. 'delete-plugin',
  114. 'search-plugins',
  115. 'search-install-plugins',
  116. 'activate-plugin',
  117. 'update-theme',
  118. 'delete-theme',
  119. 'install-theme',
  120. 'get-post-thumbnail-html',
  121. 'get-community-events',
  122. 'edit-theme-plugin-file',
  123. 'wp-privacy-export-personal-data',
  124. 'wp-privacy-erase-personal-data',
  125. 'health-check-site-status-result',
  126. 'health-check-dotorg-communication',
  127. 'health-check-is-in-debug-mode',
  128. 'health-check-background-updates',
  129. 'health-check-loopback-requests',
  130. 'health-check-get-sizes',
  131. 'toggle-auto-updates',
  132. 'send-password-reset',
  133. );
  134. // Deprecated.
  135. $core_actions_post_deprecated = array(
  136. 'wp-fullscreen-save-post',
  137. 'press-this-save-post',
  138. 'press-this-add-category',
  139. 'health-check-dotorg-communication',
  140. 'health-check-is-in-debug-mode',
  141. 'health-check-background-updates',
  142. 'health-check-loopback-requests',
  143. );
  144. $core_actions_post = array_merge( $core_actions_post, $core_actions_post_deprecated );
  145. // Register core Ajax calls.
  146. if ( ! empty( $_GET['action'] ) && in_array( $_GET['action'], $core_actions_get, true ) ) {
  147. add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );
  148. }
  149. if ( ! empty( $_POST['action'] ) && in_array( $_POST['action'], $core_actions_post, true ) ) {
  150. add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );
  151. }
  152. add_action( 'wp_ajax_nopriv_generate-password', 'wp_ajax_nopriv_generate_password' );
  153. add_action( 'wp_ajax_nopriv_heartbeat', 'wp_ajax_nopriv_heartbeat', 1 );
  154. $action = $_REQUEST['action'];
  155. if ( is_user_logged_in() ) {
  156. // If no action is registered, return a Bad Request response.
  157. if ( ! has_action( "wp_ajax_{$action}" ) ) {
  158. wp_die( '0', 400 );
  159. }
  160. /**
  161. * Fires authenticated Ajax actions for logged-in users.
  162. *
  163. * The dynamic portion of the hook name, `$action`, refers
  164. * to the name of the Ajax action callback being fired.
  165. *
  166. * @since 2.1.0
  167. */
  168. do_action( "wp_ajax_{$action}" );
  169. } else {
  170. // If no action is registered, return a Bad Request response.
  171. if ( ! has_action( "wp_ajax_nopriv_{$action}" ) ) {
  172. wp_die( '0', 400 );
  173. }
  174. /**
  175. * Fires non-authenticated Ajax actions for logged-out users.
  176. *
  177. * The dynamic portion of the hook name, `$action`, refers
  178. * to the name of the Ajax action callback being fired.
  179. *
  180. * @since 2.8.0
  181. */
  182. do_action( "wp_ajax_nopriv_{$action}" );
  183. }
  184. // Default status.
  185. wp_die( '0' );