| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\WordPress\Ajax_Functions |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Ajax function for selecting taxonomies |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
function automatorwp_wordpress_ajax_get_taxonomies() { |
| 18 |
// Security check |
| 19 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 20 |
|
| 21 |
// Permissions check |
| 22 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 23 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
$results = automatorwp_wordpress_get_taxonomies( isset( $_REQUEST['q'] ) ? $_REQUEST['q'] : '' ); |
| 27 |
|
| 28 |
// Prepend option none |
| 29 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 30 |
|
| 31 |
// Return our results |
| 32 |
wp_send_json_success( $results ); |
| 33 |
die; |
| 34 |
|
| 35 |
} |
| 36 |
add_action( 'wp_ajax_automatorwp_wordpress_get_taxonomies', 'automatorwp_wordpress_ajax_get_taxonomies' ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Function for selecting taxonomies |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* |
| 43 |
* @param string $search |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
function automatorwp_wordpress_get_taxonomies( $search = '' ) { |
| 48 |
|
| 49 |
global $wpdb; |
| 50 |
|
| 51 |
// Pull back the search string |
| 52 |
$search = $wpdb->esc_like( sanitize_text_field( $search ) ); |
| 53 |
|
| 54 |
$taxonomies = automatorwp_wordpress_get_public_taxonomies(); |
| 55 |
|
| 56 |
$results = array(); |
| 57 |
|
| 58 |
// Parse terms results to match select2 results |
| 59 |
foreach ( $taxonomies as $taxonomy ) { |
| 60 |
|
| 61 |
if( ! empty( $search ) ) { |
| 62 |
if( strpos( strtolower( $taxonomy['name'] ), strtolower( $search ) ) === false ) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
$results[] = array( |
| 68 |
'id' => $taxonomy['id'], |
| 69 |
'text' => $taxonomy['name'] |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
return $results; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Ajax function for selecting terms |
| 79 |
* |
| 80 |
* @since 1.0.0 |
| 81 |
*/ |
| 82 |
function automatorwp_wordpress_ajax_get_terms() { |
| 83 |
// Security check |
| 84 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 85 |
|
| 86 |
// Permissions check |
| 87 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 88 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 89 |
} |
| 90 |
|
| 91 |
$results = automatorwp_wordpress_get_terms( |
| 92 |
isset( $_REQUEST['q'] ) ? $_REQUEST['q'] : '', |
| 93 |
isset( $_REQUEST['table'] ) ? $_REQUEST['table'] : '' |
| 94 |
); |
| 95 |
|
| 96 |
// Prepend option none |
| 97 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 98 |
|
| 99 |
// Return our results |
| 100 |
wp_send_json_success( $results ); |
| 101 |
die; |
| 102 |
|
| 103 |
} |
| 104 |
add_action( 'wp_ajax_automatorwp_wordpress_get_terms', 'automatorwp_wordpress_ajax_get_terms' ); |
| 105 |
|
| 106 |
/** |
| 107 |
* Function for selecting terms |
| 108 |
* |
| 109 |
* @since 1.0.0 |
| 110 |
* |
| 111 |
* @param string $search |
| 112 |
* |
| 113 |
* @return array |
| 114 |
*/ |
| 115 |
function automatorwp_wordpress_get_terms( $search = '', $taxonomy_id = '' ) { |
| 116 |
|
| 117 |
global $wpdb; |
| 118 |
|
| 119 |
// Pull back the search string |
| 120 |
$search = $wpdb->esc_like( sanitize_text_field( $search ) ); |
| 121 |
|
| 122 |
// Get the taxonomy |
| 123 |
$taxonomy_id = sanitize_text_field( $taxonomy_id ); |
| 124 |
|
| 125 |
$terms = automatorwp_wordpress_get_taxonomy_terms( $taxonomy_id ); |
| 126 |
|
| 127 |
$results = array(); |
| 128 |
|
| 129 |
// Parse terms results to match select2 results |
| 130 |
foreach ( $terms as $term ) { |
| 131 |
|
| 132 |
if( ! empty( $search ) ) { |
| 133 |
if( strpos( strtolower( $term['name'] ), strtolower( $search ) ) === false ) { |
| 134 |
continue; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$results[] = array( |
| 139 |
'id' => $term['id'], |
| 140 |
'text' => $term['name'] |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
return $results; |
| 145 |
|
| 146 |
} |