| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Mail_Mint\Ajax_Functions |
| 6 |
* @author AutomatorWP <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Return Mail Mint tags as a JSON list for Select2 fields |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
function automatorwp_mail_mint_ajax_get_tags() { |
| 19 |
// Security check |
| 20 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 21 |
|
| 22 |
// Permissions check |
| 23 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 24 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
global $wpdb; |
| 28 |
|
| 29 |
// Pull back the search string |
| 30 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 31 |
|
| 32 |
$tags = automatorwp_mail_mint_get_tags(); |
| 33 |
|
| 34 |
$results = array(); |
| 35 |
|
| 36 |
// Parse tags results to match select2 results |
| 37 |
foreach ( $tags as $tag ) { |
| 38 |
|
| 39 |
if( ! empty( $search ) ) { |
| 40 |
if( strpos( strtolower( $tag['name'] ), strtolower( $search ) ) === false ) { |
| 41 |
continue; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
$results[] = array( |
| 46 |
'id' => $tag['id'], |
| 47 |
'text' => $tag['name'] |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
// Prepend option none |
| 52 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 53 |
|
| 54 |
// Return our results |
| 55 |
wp_send_json_success( $results ); |
| 56 |
die; |
| 57 |
} |
| 58 |
add_action( 'wp_ajax_automatorwp_mail_mint_get_tags', 'automatorwp_mail_mint_ajax_get_tags' ); |
| 59 |
|