| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JP\CC\Admin; |
| 5 |
|
| 6 |
use JP\CC\Helpers; |
| 7 |
use JP\CC\Options; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
class Ajax { |
| 15 |
|
| 16 |
public static function init() { |
| 17 |
add_action( 'wp_ajax_jp_cc_object_search', array( __CLASS__, 'object_search' ) ); |
| 18 |
add_action( 'wp_ajax_jp_cc_options_autosave', array( __CLASS__, 'options_autosave' ) ); |
| 19 |
} |
| 20 |
|
| 21 |
public static function options_autosave() { |
| 22 |
// Make sure we have got the data we are expecting. |
| 23 |
$nonce = isset( $_POST["nonce"] ) ? $_POST["nonce"] : ""; |
| 24 |
|
| 25 |
$option_key = isset( $_POST["key"] ) ? $_POST["key"] : false; |
| 26 |
$option_value = isset( $_POST["value"] ) ? $_POST["value"] : null; |
| 27 |
|
| 28 |
if ( wp_verify_nonce( $nonce, 'jp-cc-admin-nonce' ) && isset( $option_value ) ) { |
| 29 |
|
| 30 |
if ( $option_key && ! empty( $option_key ) ) { |
| 31 |
Options::update( $option_key, $option_value ); |
| 32 |
} else { |
| 33 |
wp_send_json_error( "No option key was passed." ); |
| 34 |
} |
| 35 |
|
| 36 |
} else { |
| 37 |
// If the nonce was invalid or the comment was empty, send an error. |
| 38 |
wp_send_json_error( "This came from the wrong place" ); |
| 39 |
} |
| 40 |
|
| 41 |
wp_send_json_success( $option_value ); |
| 42 |
} |
| 43 |
|
| 44 |
public static function object_search() { |
| 45 |
$results = array( |
| 46 |
'items' => array(), |
| 47 |
'total_count' => 0, |
| 48 |
); |
| 49 |
|
| 50 |
$object_type = sanitize_text_field( $_REQUEST['object_type'] ); |
| 51 |
|
| 52 |
switch ( $object_type ) { |
| 53 |
case 'post_type': |
| 54 |
$post_type = ! empty( $_REQUEST['object_key'] ) ? sanitize_text_field( $_REQUEST['object_key'] ) : 'post'; |
| 55 |
|
| 56 |
$include = ! empty( $_REQUEST['include'] ) ? wp_parse_id_list( $_REQUEST['include'] ) : null; |
| 57 |
$exclude = ! empty( $_REQUEST['exclude'] ) ? wp_parse_id_list( $_REQUEST['exclude'] ) : null; |
| 58 |
|
| 59 |
if ( ! empty( $include ) && ! empty( $exclude ) ) { |
| 60 |
$exclude = array_merge( $include, $exclude ); |
| 61 |
} |
| 62 |
|
| 63 |
if ( $include ) { |
| 64 |
$include_query = Helpers::post_type_selectlist_query( $post_type, array( |
| 65 |
'post__in' => $include, |
| 66 |
), true ); |
| 67 |
|
| 68 |
foreach ( $include_query['items'] as $id => $name ) { |
| 69 |
$results['items'][ $id ] = array( |
| 70 |
'id' => $id, |
| 71 |
'text' => $name, |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
$results['total_count'] += $include_query['total_count']; |
| 76 |
} |
| 77 |
|
| 78 |
$query = Helpers::post_type_selectlist_query( $post_type, array( |
| 79 |
's' => ! empty( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : null, |
| 80 |
'paged' => ! empty( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : null, |
| 81 |
'post__not_in' => $exclude, |
| 82 |
'posts_per_page' => 10, |
| 83 |
), true ); |
| 84 |
|
| 85 |
foreach ( $query['items'] as $id => $name ) { |
| 86 |
$results['items'][ $id ] = array( |
| 87 |
'id' => $id, |
| 88 |
'text' => $name, |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
$results['total_count'] += $query['total_count']; |
| 93 |
|
| 94 |
break; |
| 95 |
case 'taxonomy': |
| 96 |
$taxonomy = ! empty( $_REQUEST['object_key'] ) ? sanitize_text_field( $_REQUEST['object_key'] ) : 'category'; |
| 97 |
|
| 98 |
$include = ! empty( $_REQUEST['include'] ) ? wp_parse_id_list( $_REQUEST['include'] ) : null; |
| 99 |
$exclude = ! empty( $_REQUEST['exclude'] ) ? wp_parse_id_list( $_REQUEST['exclude'] ) : null; |
| 100 |
|
| 101 |
if ( ! empty( $include ) && ! empty( $exclude ) ) { |
| 102 |
$exclude = array_merge( $include, $exclude ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( $include ) { |
| 106 |
$include_query = Helpers::taxonomy_selectlist_query( $taxonomy, array( |
| 107 |
'include' => $include, |
| 108 |
), true ); |
| 109 |
|
| 110 |
foreach ( $include_query['items'] as $id => $name ) { |
| 111 |
$results['items'][ $id ] = array( |
| 112 |
'id' => $id, |
| 113 |
'text' => $name, |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
$results['total_count'] += $include_query['total_count']; |
| 118 |
} |
| 119 |
|
| 120 |
$query = Helpers::taxonomy_selectlist_query( $taxonomy, array( |
| 121 |
'search' => ! empty( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : null, |
| 122 |
'paged' => ! empty( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : null, |
| 123 |
'exclude' => $exclude, |
| 124 |
'number' => 10, |
| 125 |
), true ); |
| 126 |
|
| 127 |
foreach ( $query['items'] as $id => $name ) { |
| 128 |
$results['items'][ $id ] = array( |
| 129 |
'id' => $id, |
| 130 |
'text' => $name, |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
$results['total_count'] += $query['total_count']; |
| 135 |
break; |
| 136 |
default: |
| 137 |
// Do nothing if object is not post_type or taxonomy. |
| 138 |
} |
| 139 |
|
| 140 |
// Take out keys which were only used to deduplicate. |
| 141 |
$results['items'] = array_values( $results['items'] ); |
| 142 |
|
| 143 |
echo json_encode( $results ); |
| 144 |
die(); |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|