/wp-admin/includes/ajax-actions.php
PHP | 1634 lines | 1271 code | 313 blank | 50 comment | 353 complexity | 0928de6c6c2d5b3f3d94e2f67bd60fbf MD5 | raw file
- <?php
- /**
- * WordPress Core Ajax Handlers.
- *
- * @package WordPress
- * @subpackage Administration
- */
- /*
- * No-privilege Ajax handlers.
- */
- function wp_ajax_nopriv_autosave() {
- $id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0;
- if ( ! $id )
- wp_die( -1 );
- $message = sprintf( __('<strong>ALERT: You are logged out!</strong> Could not save draft. <a href="%s" target="_blank">Please log in again.</a>'), wp_login_url() );
- $x = new WP_Ajax_Response( array(
- 'what' => 'autosave',
- 'id' => $id,
- 'data' => $message
- ) );
- $x->send();
- }
- /*
- * 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 = stripslashes( $_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 = $wpdb->get_col( $wpdb->prepare( "SELECT t.name FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.name LIKE (%s)", $taxonomy, '%' . like_escape( $s ) . '%' ) );
- 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 );
- 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';
- switch ( $_GET['widget'] ) {
- case 'dashboard_incoming_links' :
- wp_dashboard_incoming_links();
- break;
- case 'dashboard_primary' :
- wp_dashboard_primary();
- break;
- case 'dashboard_secondary' :
- wp_dashboard_secondary();
- break;
- case 'dashboard_plugins' :
- wp_dashboard_plugins();
- break;
- }
- wp_die();
- }
- function wp_ajax_logged_in() {
- wp_die( 1 );
- }
- /*
- * Ajax helper.
- */
- /**
- * Sends back current comment total and new page links if they need to be updated.
- *
- * Contrary to normal success AJAX response ("1"), die with time() on success.
- *
- * @since 2.7
- *
- * @param int $comment_id
- * @return die
- */
- function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) {
- $total = (int) @$_POST['_total'];
- $per_page = (int) @$_POST['_per_page'];
- $page = (int) @$_POST['_page'];
- $url = esc_url_raw( @$_POST['_url'] );
- // JS didn't send us everything we need to know. Just die with success message
- if ( !$total || !$per_page || !$page || !$url )
- wp_die( time() );
- $total += $delta;
- if ( $total < 0 )
- $total = 0;
- // Only do the expensive stuff on a page-break, and about 1 other time per page
- if ( 0 == $total % $per_page || 1 == mt_rand( 1, $per_page ) ) {
- $post_id = 0;
- $status = 'total_comments'; // What type of comment count are we looking for?
- $parsed = parse_url( $url );
- if ( isset( $parsed['query'] ) ) {
- parse_str( $parsed['query'], $query_vars );
-