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