wp/wp-admin/admin-ajax.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * WordPress AJAX Process Execution.
     3  * WordPress Ajax Process Execution
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Administration
     6  * @subpackage Administration
     7  *
     7  *
     8  * @link https://codex.wordpress.org/AJAX_in_Plugins
     8  * @link https://codex.wordpress.org/AJAX_in_Plugins
     9  */
     9  */
    10 
    10 
    11 /**
    11 /**
    12  * Executing AJAX process.
    12  * Executing Ajax process.
    13  *
    13  *
    14  * @since 2.1.0
    14  * @since 2.1.0
    15  */
    15  */
    16 define( 'DOING_AJAX', true );
    16 define( 'DOING_AJAX', true );
    17 if ( ! defined( 'WP_ADMIN' ) ) {
    17 if ( ! defined( 'WP_ADMIN' ) ) {
    19 }
    19 }
    20 
    20 
    21 /** Load WordPress Bootstrap */
    21 /** Load WordPress Bootstrap */
    22 require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
    22 require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
    23 
    23 
    24 /** Allow for cross-domain requests (from the frontend). */
    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 	die( '0' );
    29 	wp_die( '0', 400 );
    30 
    30 
    31 /** Load WordPress Administration APIs */
    31 /** Load WordPress Administration APIs */
    32 require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    32 require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    33 
    33 
    34 /** Load Ajax Handlers for WordPress Core */
    34 /** Load Ajax Handlers for WordPress Core */
    54 	'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
    54 	'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
    55 	'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes',
    55 	'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes',
    56 	'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
    56 	'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
    57 	'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
    57 	'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
    58 	'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
    58 	'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
    59 	'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
    59 	'save-widget', 'delete-inactive-widgets', 'set-post-thumbnail', 'date_format', 'time_format',
    60 	'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
    60 	'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
    61 	'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
    61 	'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
    62 	'send-attachment-to-editor', 'save-attachment-order', 'heartbeat', 'get-revision-diffs',
    62 	'send-attachment-to-editor', 'save-attachment-order', 'heartbeat', 'get-revision-diffs',
    63 	'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',
    63 	'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',
    64 	'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
    64 	'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'crop-image',
    65 	'press-this-add-category',
    65 	'generate-password', 'save-wporg-username', 'delete-plugin', 'search-plugins',
       
    66 	'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme', 'install-theme',
       
    67 	'get-post-thumbnail-html', 'get-community-events', 'edit-theme-plugin-file',
       
    68 	'wp-privacy-export-personal-data',
       
    69 	'wp-privacy-erase-personal-data',
       
    70 	'update-try-gutenberg-panel',
    66 );
    71 );
       
    72 
       
    73 // Deprecated
       
    74 $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 );
    67 
    76 
    68 // Register core Ajax calls.
    77 // Register core Ajax calls.
    69 if ( ! empty( $_GET['action'] ) && in_array( $_GET['action'], $core_actions_get ) )
    78 if ( ! empty( $_GET['action'] ) && in_array( $_GET['action'], $core_actions_get ) )
    70 	add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );
    79 	add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );
    71 
    80 
    73 	add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );
    82 	add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );
    74 
    83 
    75 add_action( 'wp_ajax_nopriv_heartbeat', 'wp_ajax_nopriv_heartbeat', 1 );
    84 add_action( 'wp_ajax_nopriv_heartbeat', 'wp_ajax_nopriv_heartbeat', 1 );
    76 
    85 
    77 if ( is_user_logged_in() ) {
    86 if ( is_user_logged_in() ) {
       
    87 	// If no action is registered, return a Bad Request response.
       
    88 	if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
       
    89 		wp_die( '0', 400 );
       
    90 	}
       
    91 
    78 	/**
    92 	/**
    79 	 * Fires authenticated AJAX actions for logged-in users.
    93 	 * Fires authenticated Ajax actions for logged-in users.
    80 	 *
    94 	 *
    81 	 * The dynamic portion of the hook name, `$_REQUEST['action']`,
    95 	 * The dynamic portion of the hook name, `$_REQUEST['action']`,
    82 	 * refers to the name of the AJAX action callback being fired.
    96 	 * refers to the name of the Ajax action callback being fired.
    83 	 *
    97 	 *
    84 	 * @since 2.1.0
    98 	 * @since 2.1.0
    85 	 */
    99 	 */
    86 	do_action( 'wp_ajax_' . $_REQUEST['action'] );
   100 	do_action( 'wp_ajax_' . $_REQUEST['action'] );
    87 } else {
   101 } else {
       
   102 	// If no action is registered, return a Bad Request response.
       
   103 	if ( ! has_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] ) ) {
       
   104 		wp_die( '0', 400 );
       
   105 	}
       
   106 
    88 	/**
   107 	/**
    89 	 * Fires non-authenticated AJAX actions for logged-out users.
   108 	 * Fires non-authenticated Ajax actions for logged-out users.
    90 	 *
   109 	 *
    91 	 * The dynamic portion of the hook name, `$_REQUEST['action']`,
   110 	 * The dynamic portion of the hook name, `$_REQUEST['action']`,
    92 	 * refers to the name of the AJAX action callback being fired.
   111 	 * refers to the name of the Ajax action callback being fired.
    93 	 *
   112 	 *
    94 	 * @since 2.8.0
   113 	 * @since 2.8.0
    95 	 */
   114 	 */
    96 	do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
   115 	do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
    97 }
   116 }
    98 // Default status
   117 // Default status
    99 die( '0' );
   118 wp_die( '0' );