/wp/wp-admin/admin-ajax.php
PHP | 1555 lines | 1256 code | 259 blank | 40 comment | 344 complexity | 1ca59095c4a7e365761c2289aa7841ad MD5 | raw file
- <?php
- /**
- * WordPress AJAX Process Execution.
- *
- * @package WordPress
- * @subpackage Administration
- */
- /**
- * Executing AJAX process.
- *
- * @since 2.1.0
- */
- define('DOING_AJAX', true);
- define('WP_ADMIN', true);
- if ( ! isset( $_REQUEST['action'] ) )
- die('-1');
- require_once('../wp-load.php');
- require_once('./includes/admin.php');
- @header('Content-Type: text/html; charset=' . get_option('blog_charset'));
- send_nosniff_header();
- do_action('admin_init');
- if ( ! is_user_logged_in() ) {
- if ( isset( $_POST['action'] ) && $_POST['action'] == 'autosave' ) {
- $id = isset($_POST['post_ID'])? (int) $_POST['post_ID'] : 0;
- if ( ! $id )
- 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();
- }
- if ( !empty( $_REQUEST['action'] ) )
- do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
- die('-1');
- }
- if ( isset( $_GET['action'] ) ) :
- switch ( $action = $_GET['action'] ) :
- case 'fetch-list' :
- $list_class = $_GET['list_args']['class'];
- check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' );
- $current_screen = (object) $_GET['list_args']['screen'];
- //TODO fix this in a better way see #15336
- $current_screen->is_network = 'false' === $current_screen->is_network ? false : true;
- $current_screen->is_user = 'false' === $current_screen->is_user ? false : true;
- define( 'WP_NETWORK_ADMIN', $current_screen->is_network );
- define( 'WP_USER_ADMIN', $current_screen->is_user );
- $wp_list_table = _get_list_table( $list_class );
- if ( ! $wp_list_table )
- die( '0' );
- if ( ! $wp_list_table->ajax_user_can() )
- die( '-1' );
- $wp_list_table->ajax_response();
- die( '0' );
- break;
- case 'ajax-tag-search' :
- if ( isset( $_GET['tax'] ) ) {
- $taxonomy = sanitize_key( $_GET['tax'] );
- $tax = get_taxonomy( $taxonomy );
- if ( ! $tax )
- die( '0' );
- if ( ! current_user_can( $tax->cap->assign_terms ) )
- die( '-1' );
- } else {
- die('0');
- }
- $s = stripslashes( $_GET['q'] );
- if ( false !== strpos( $s, ',' ) ) {
- $s = explode( ',', $s );
- $s = $s[count( $s ) - 1];
- }
- $s = trim( $s );
- if ( strlen( $s ) < 2 )
- 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" );
- die;
- break;
- case 'wp-compression-test' :
- if ( !current_user_can( 'manage_options' ) )
- die('-1');
- if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') ) {
- update_site_option('can_compress_scripts', 0);
- 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;
- die;
- } elseif ( 2 == $_GET['test'] ) {
- if ( !isset($_SERVER['HTTP_ACCEPT_ENCODING']) )
- 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 {
- die('-1');
- }
- echo $out;
- die;
- } elseif ( 'no' == $_GET['test'] ) {
- update_site_option('can_compress_scripts', 0);
- } elseif ( 'yes' == $_GET['test'] ) {
- update_site_option('can_compress_scripts', 1);
- }
- }
- die('0');
- break;
- case 'imgedit-preview' :
- $post_id = intval($_GET['postid']);
- if ( empty($post_id) || !current_user_can('edit_post', $post_id) )
- die('-1');
- check_ajax_referer( "image_editor-$post_id" );
- include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
- if ( ! stream_preview_image($post_id) )
- die('-1');
- die();
- break;
- case 'menu-quick-search':
- if ( ! current_user_can( 'edit_theme_options' ) )
- die('-1');
- require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
- _wp_ajax_menu_quick_search( $_REQUEST );
- exit;
- break;
- case 'oembed-cache' :
- $return = ( $wp_embed->cache_oembed( $_GET['post'] ) ) ? '1' : '0';
- die( $return );
- break;
- default :
- do_action( 'wp_ajax_' . $_GET['action'] );
- die('0');
- break;
- endswitch;
- endif;
- /**
- * 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 )
- die( (string) 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 );
- if ( !empty( $query_vars['comment_status'] ) )
- $status = $query_vars['comment_status'];
- if ( !empty( $query_vars['p'] ) )
- $post_id = (int) $query_vars['p'];
- }
- $comment_count = wp_count_comments($post_id);
- if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count
- $total = $comment_count->$status;
- // else use the decremented value from above
- }
- $time = time(); // The time since the last comment count
- $x = new WP_Ajax_Response( array(
- 'what' => 'comment',
- 'id' => $comment_id, // here for completeness - not used
- 'supplemental' => array(
- 'total_items_i18n' => sprintf( _n( '1 item', '%s items', $total ), number_format_i18n( $total ) ),
- 'total_pages' => ceil( $total / $per_page ),
- 'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ),
- 'total' => $total,
- 'time' => $time
- )
- ) );
- $x->send();
- }
- function _wp_ajax_add_hierarchical_term() {
- $action = $_POST['action'];
- $taxonomy = get_taxonomy(substr($action, 4));
- check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name );
- if ( !current_user_can( $taxonomy->cap->edit_terms ) )
- die('-1');
- $names = explode(',', $_POST['new'.$taxonomy->name]);
-