| 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 |
switch ( $_REQUEST['object_type'] ) { |
| 50 |
case 'post_type': |
| 51 |
$post_type = ! empty( $_REQUEST['object_key'] ) ? $_REQUEST['object_key'] : 'post'; |
| 52 |
$args = array( |
| 53 |
's' => ! empty( $_REQUEST['s'] ) ? $_REQUEST['s'] : null, |
| 54 |
'post__in' => ! empty( $_REQUEST['include'] ) ? array_map( 'intval', $_REQUEST['include'] ) : null, |
| 55 |
'page' => ! empty( $_REQUEST['page'] ) ? absint( $_REQUEST['page'] ) : null, |
| 56 |
'posts_per_page' => 10, |
| 57 |
); |
| 58 |
$query = Helpers::post_type_selectlist( $post_type, $args, true ); |
| 59 |
foreach ( $query['items'] as $name => $id ) { |
| 60 |
$results['items'][] = array( |
| 61 |
'id' => $id, |
| 62 |
'text' => $name, |
| 63 |
); |
| 64 |
} |
| 65 |
$results['total_count'] = $query['total_count']; |
| 66 |
break; |
| 67 |
case 'taxonomy': |
| 68 |
$taxonomy = ! empty( $_REQUEST['object_key'] ) ? $_REQUEST['object_key'] : 'category'; |
| 69 |
$args = array( |
| 70 |
'search' => ! empty( $_REQUEST['s'] ) ? $_REQUEST['s'] : '', |
| 71 |
'include' => ! empty( $_REQUEST['include'] ) ? $_REQUEST['include'] : null, |
| 72 |
'page' => ! empty( $_REQUEST['page'] ) ? absint( $_REQUEST['page'] ) : null, |
| 73 |
'number' => 10, |
| 74 |
); |
| 75 |
$query = Helpers::taxonomy_selectlist( $taxonomy, $args, true ); |
| 76 |
foreach ( $query['items'] as $name => $id ) { |
| 77 |
$results['items'][] = array( |
| 78 |
'id' => $id, |
| 79 |
'text' => $name, |
| 80 |
); |
| 81 |
} |
| 82 |
$results['total_count'] = $query['total_count']; |
| 83 |
break; |
| 84 |
} |
| 85 |
echo json_encode( $results ); |
| 86 |
die(); |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
|