/wp-admin/includes/ajax-actions.php
PHP | 1650 lines | 1223 code | 310 blank | 117 comment | 353 complexity | d6c5c59e8355b1511a2a2cc9e6ba5719 MD5 | raw file
- <?php
- /**
- * WordPress Core Ajax Handlers.
- *
- * @package WordPress
- * @subpackage Administration
- */
- /*
- * No-privilege Ajax handlers.
- */
- /**
- * Heartbeat API (experimental)
- *
- * Runs when the user is not logged in.
- */
- function wp_ajax_nopriv_heartbeat() {
- $response = array();
- // screen_id is the same as $current_screen->id and the JS global 'pagenow'
- if ( ! empty($_POST['screen_id']) )
- $screen_id = sanitize_key($_POST['screen_id']);
- else
- $screen_id = 'front';
- if ( ! empty($_POST['data']) ) {
- $data = wp_unslash( (array) $_POST['data'] );
- /**
- * Filter Heartbeat AJAX response in no-privilege environments.
- *
- * @since 3.6.0
- *
- * @param array|object $response The no-priv Heartbeat response object or array.
- * @param array $data An array of data passed via $_POST.
- * @param string $screen_id The screen id.
- */
- $response = apply_filters( 'heartbeat_nopriv_received', $response, $data, $screen_id );
- }
- /**
- * Filter Heartbeat AJAX response when no data is passed.
- *
- * @since 3.6.0
- *
- * @param array|object $response The Heartbeat response object or array.
- * @param string $screen_id The screen id.
- */
- $response = apply_filters( 'heartbeat_nopriv_send', $response, $screen_id );
- /**
- * Fires when Heartbeat ticks in no-privilege environments.
- *
- * Allows the transport to be easily replaced with long-polling.
- *
- * @since 3.6.0
- *
- * @param array|object $response The no-priv Heartbeat response.
- * @param string $screen_id The screen id.
- */
- do_action( 'heartbeat_nopriv_tick', $response, $screen_id );
- // send the current time according to the server
- $response['server_time'] = time();
- wp_send_json($response);
- }
- /*
- * GET-based Ajax handlers.
- */
- function wp_ajax_fetch_list() {
- global $wp_list_table;
- $list_class = $_GET['list_args']['class'];
- check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' );
- $wp_list_table = _get_list_table( $list_class, array( 'screen' => $_GET['list_args']['screen']['id'] ) );
- if ( ! $wp_list_table )
- wp_die( 0 );
- if ( ! $wp_list_table->ajax_user_can() )
- wp_die( -1 );
- $wp_list_table->ajax_response();
- wp_die( 0 );
- }
- function wp_ajax_ajax_tag_search() {
- global $wpdb;
- if ( isset( $_GET['tax'] ) ) {
- $taxonomy = sanitize_key( $_GET['tax'] );
- $tax = get_taxonomy( $taxonomy );
- if ( ! $tax )
- wp_die( 0 );
- if ( ! current_user_can( $tax->cap->assign_terms ) )
- wp_die( -1 );
- } else {
- wp_die( 0 );
- }
- $s = wp_unslash( $_GET['q'] );
- $comma = _x( ',', 'tag delimiter' );
- if ( ',' !== $comma )
- $s = str_replace( $comma, ',', $s );
- if ( false !== strpos( $s, ',' ) ) {
- $s = explode( ',', $s );
- $s = $s[count( $s ) - 1];
- }
- $s = trim( $s );
- if ( strlen( $s ) < 2 )
- wp_die(); // require 2 chars for matching
- $results = get_terms( $taxonomy, array( 'name__like' => $s, 'fields' => 'names', 'hide_empty' => false ) );
- echo join( $results, "\n" );
- wp_die();
- }
- function wp_ajax_wp_compression_test() {
- if ( !current_user_can( 'manage_options' ) )
- wp_die( -1 );
- if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') ) {
- update_site_option('can_compress_scripts', 0);
- wp_die( 0 );
- }
- if ( isset($_GET['test']) ) {
- header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
- header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
- header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
- header( 'Pragma: no-cache' );
- header('Content-Type: application/x-javascript; charset=UTF-8');
- $force_gzip = ( defined('ENFORCE_GZIP') && ENFORCE_GZIP );
- $test_str = '"wpCompressionTest Lorem ipsum dolor sit amet consectetuer mollis sapien urna ut a. Eu nonummy condimentum fringilla tempor pretium platea vel nibh netus Maecenas. Hac molestie amet justo quis pellentesque est ultrices interdum nibh Morbi. Cras mattis pretium Phasellus ante ipsum ipsum ut sociis Suspendisse Lorem. Ante et non molestie. Porta urna Vestibulum egestas id congue nibh eu risus gravida sit. Ac augue auctor Ut et non a elit massa id sodales. Elit eu Nulla at nibh adipiscing mattis lacus mauris at tempus. Netus nibh quis suscipit nec feugiat eget sed lorem et urna. Pellentesque lacus at ut massa consectetuer ligula ut auctor semper Pellentesque. Ut metus massa nibh quam Curabitur molestie nec mauris congue. Volutpat molestie elit justo facilisis neque ac risus Ut nascetur tristique. Vitae sit lorem tellus et quis Phasellus lacus tincidunt nunc Fusce. Pharetra wisi Suspendisse mus sagittis libero lacinia Integer consequat ac Phasellus. Et urna ac cursus tortor aliquam Aliquam amet tellus volutpat Vestibulum. Justo interdum condimentum In augue congue tellus sollicitudin Quisque quis nibh."';
- if ( 1 == $_GET['test'] ) {
- echo $test_str;
- wp_die();
- } elseif ( 2 == $_GET['test'] ) {
- if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) )
- wp_die( -1 );
- if ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
- header('Content-Encoding: deflate');
- $out = gzdeflate( $test_str, 1 );
- } elseif ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && function_exists('gzencode') ) {
- header('Content-Encoding: gzip');
- $out = gzencode( $test_str, 1 );
- } else {
- wp_die( -1 );
- }
- echo $out;
- wp_die();
- } elseif ( 'no' == $_GET['test'] ) {
- update_site_option('can_compress_scripts', 0);
- } elseif ( 'yes' == $_GET['test'] ) {
- update_site_option('can_compress_scripts', 1);
- }
- }
- wp_die( 0 );
- }
- function wp_ajax_imgedit_preview() {
- $post_id = intval($_GET['postid']);
- if ( empty($post_id) || !current_user_can('edit_post', $post_id) )
- wp_die( -1 );
- check_ajax_referer( "image_editor-$post_id" );
- include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
- if ( ! stream_preview_image($post_id) )
- wp_die( -1 );
- wp_die();
- }
- function wp_ajax_oembed_cache() {
- global $wp_embed;
- $return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0';
- wp_die( $return );
- }
- function wp_ajax_autocomplete_user() {
- if ( ! is_multisite() || ! current_user_can( 'promote_users' ) || wp_is_large_network( 'users' ) )
- wp_die( -1 );
- /** This filter is documented in wp-admin/user-new.php */
- if ( ! is_super_admin() && ! apply_filters( 'autocomplete_users_for_site_admins', false ) )
- wp_die( -1 );
- $return = array();
- // Check the type of request
- if ( isset( $_REQUEST['autocomplete_type'] ) )
- $type = $_REQUEST['autocomplete_type'];
- else
- $type = 'add';
- // Exclude current users of this blog
- if ( isset( $_REQUEST['site_id'] ) )
- $id = absint( $_REQUEST['site_id'] );
- else
- $id = get_current_blog_id();
- $include_blog_users = ( $type == 'search' ? get_users( array( 'blog_id' => $id, 'fields' => 'ID' ) ) : array() );
- $exclude_blog_users = ( $type == 'add' ? get_users( array( 'blog_id' => $id, 'fields' => 'ID' ) ) : array() );
- $users = get_users( array(
- 'blog_id' => false,
- 'search' => '*' . $_REQUEST['term'] . '*',
- 'include' => $include_blog_users,
- 'exclude' => $exclude_blog_users,
- 'search_columns' => array( 'user_login', 'user_nicename', 'user_email' ),
- ) );
- foreach ( $users as $user ) {
- $return[] = array(
- /* translators: 1: user_login, 2: user_email */
- 'label' => sprintf( __( '%1$s (%2$s)' ), $user->user_login, $user->user_email ),
- 'value' => $user->user_login,
- );
- }
- wp_die( json_encode( $return ) );
- }
- function wp_ajax_dashboard_widgets() {
- require_once ABSPATH . 'wp-admin/includes/dashboard.php';
- $pagenow = $_GET['pagenow'];
- if ( $pagenow === 'dashboard-user' || $pagenow === 'dashboard-network' || $pagenow === 'dashboard' ) {
- set_current_screen( $pagenow );
- }
- switch ( $_GET['widget'] ) {
- case 'dashboard_primary' :
- wp_dashboard_primary();
- break;
- }
- wp_die();
- }