| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Autonami\Ajax_Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Ajax function for selecting contents |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
function automatorwp_autonami_ajax_get_tags() { |
| 17 |
// Security check |
| 18 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 19 |
|
| 20 |
// Permissions check |
| 21 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 22 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
global $wpdb; |
| 26 |
|
| 27 |
// Pull back the search string |
| 28 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 29 |
|
| 30 |
$tags = automatorwp_autonami_get_tags(); |
| 31 |
|
| 32 |
// Parse tag results to match select2 results |
| 33 |
foreach ( $tags as $tag ) { |
| 34 |
|
| 35 |
if( ! empty( $search ) ) { |
| 36 |
if( strpos( strtolower( $tag['name'] ), strtolower( $search ) ) === false ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
$results[] = array( |
| 42 |
'id' => $tag['id'], |
| 43 |
'text' => $tag['name'] |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
// Prepend option none |
| 48 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 49 |
|
| 50 |
// Return our results |
| 51 |
wp_send_json_success( $results ); |
| 52 |
die; |
| 53 |
|
| 54 |
} |
| 55 |
add_action( 'wp_ajax_automatorwp_autonami_get_tags', 'automatorwp_autonami_ajax_get_tags' ); |
| 56 |
|
| 57 |
/** |
| 58 |
* Ajax function for selecting contents |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
*/ |
| 62 |
function automatorwp_autonami_ajax_get_lists() { |
| 63 |
// Security check |
| 64 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 65 |
|
| 66 |
// Permissions check |
| 67 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 68 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 69 |
} |
| 70 |
|
| 71 |
global $wpdb; |
| 72 |
|
| 73 |
// Pull back the search string |
| 74 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 75 |
|
| 76 |
$lists = automatorwp_autonami_get_lists(); |
| 77 |
|
| 78 |
// Parse list results to match select2 results |
| 79 |
foreach ( $lists as $list ) { |
| 80 |
|
| 81 |
if( ! empty( $search ) ) { |
| 82 |
if( strpos( strtolower( $list['name'] ), strtolower( $search ) ) === false ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
$results[] = array( |
| 88 |
'id' => $list['id'], |
| 89 |
'text' => $list['name'] |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
// Prepend option none |
| 94 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 95 |
|
| 96 |
// Return our results |
| 97 |
wp_send_json_success( $results ); |
| 98 |
die; |
| 99 |
|
| 100 |
} |
| 101 |
add_action( 'wp_ajax_automatorwp_autonami_get_lists', 'automatorwp_autonami_ajax_get_lists' ); |