← All changes
|
integrations/shortlinkspro/includes/ajax-functions.php
+216
-0
5.6.1
→
6.0.3
View file →
| @@ -1,0 +1,216 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Ajax Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\ShortLinksPro\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 links | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + */ | |
| 16 | +function automatorwp_shortlinkspro_ajax_get_links() { | |
| 17 | + | |
| 18 | + // Security check, forces to die if not security passed | |
| 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_shortlinkspro_get_links( isset( $_REQUEST['q'] ) ? $_REQUEST['q'] : '' ); | |
| 27 | + | |
| 28 | + // Prepend option none | |
| 29 | + $results = automatorwp_ajax_get_ajax_results_option_none( $results ); | |
| 30 | + | |
| 31 | + // Return our results | |
| 32 | + wp_send_json_success( $results ); | |
| 33 | + die; | |
| 34 | + | |
| 35 | +} | |
| 36 | +add_action( 'wp_ajax_automatorwp_shortlinkspro_get_links', 'automatorwp_shortlinkspro_ajax_get_links', 5 ); | |
| 37 | + | |
| 38 | +/** | |
| 39 | + * Function for selecting links | |
| 40 | + * | |
| 41 | + * @since 1.0.0 | |
| 42 | + * | |
| 43 | + * @param string $search | |
| 44 | + * | |
| 45 | + * @return array | |
| 46 | + */ | |
| 47 | +function automatorwp_shortlinkspro_get_links( $search = '' ) { | |
| 48 | + | |
| 49 | + global $wpdb; | |
| 50 | + | |
| 51 | + // Pull back the search string | |
| 52 | + $search = $wpdb->esc_like( $search ); | |
| 53 | + | |
| 54 | + $results = array(); | |
| 55 | + | |
| 56 | + // Setup table | |
| 57 | + $ct_table = ct_setup_table( 'shortlinkspro_links' ); | |
| 58 | + | |
| 59 | + $links = $wpdb->get_results( $wpdb->prepare( | |
| 60 | + "SELECT id, title | |
| 61 | + FROM {$ct_table->db->table_name} | |
| 62 | + WHERE title LIKE %s", | |
| 63 | + "%%{$search}%%" | |
| 64 | + ) ); | |
| 65 | + | |
| 66 | + ct_reset_setup_table(); | |
| 67 | + | |
| 68 | + foreach( $links as $link ) { | |
| 69 | + | |
| 70 | + if( $link->title === '' ) $link->title = '(no title)'; | |
| 71 | + | |
| 72 | + $results[] = array( | |
| 73 | + 'id' => $link->id, | |
| 74 | + 'text' => $link->title, | |
| 75 | + ); | |
| 76 | + } | |
| 77 | + | |
| 78 | + return $results; | |
| 79 | + | |
| 80 | +} | |
| 81 | + | |
| 82 | +/** | |
| 83 | + * Ajax function for selecting categories | |
| 84 | + * | |
| 85 | + * @since 1.0.0 | |
| 86 | + */ | |
| 87 | +function automatorwp_shortlinkspro_ajax_get_categories() { | |
| 88 | + | |
| 89 | + // Security check, forces to die if not security passed | |
| 90 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 91 | + | |
| 92 | + // Permissions check | |
| 93 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 94 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + $results = automatorwp_shortlinkspro_get_categories( isset( $_REQUEST['q'] ) ? $_REQUEST['q'] : '' ); | |
| 98 | + | |
| 99 | + // Prepend option none | |
| 100 | + $results = automatorwp_ajax_get_ajax_results_option_none( $results ); | |
| 101 | + | |
| 102 | + // Return our results | |
| 103 | + wp_send_json_success( $results ); | |
| 104 | + die; | |
| 105 | + | |
| 106 | +} | |
| 107 | +add_action( 'wp_ajax_automatorwp_shortlinkspro_get_categories', 'automatorwp_shortlinkspro_ajax_get_categories', 5 ); | |
| 108 | + | |
| 109 | +/** | |
| 110 | + * Function for selecting categories | |
| 111 | + * | |
| 112 | + * @since 1.0.0 | |
| 113 | + * | |
| 114 | + * @param string $search | |
| 115 | + * | |
| 116 | + * @return array | |
| 117 | + */ | |
| 118 | +function automatorwp_shortlinkspro_get_categories( $search = '' ) { | |
| 119 | + | |
| 120 | + global $wpdb; | |
| 121 | + | |
| 122 | + // Pull back the search string | |
| 123 | + $search = $wpdb->esc_like( $search ); | |
| 124 | + | |
| 125 | + $results = array(); | |
| 126 | + | |
| 127 | + // Setup table | |
| 128 | + $ct_table = ct_setup_table( 'shortlinkspro_link_categories' ); | |
| 129 | + | |
| 130 | + $categories = $wpdb->get_results( $wpdb->prepare( | |
| 131 | + "SELECT * | |
| 132 | + FROM {$ct_table->db->table_name} | |
| 133 | + WHERE name LIKE %s", | |
| 134 | + "%%{$search}%%" | |
| 135 | + ) ); | |
| 136 | + | |
| 137 | + ct_reset_setup_table(); | |
| 138 | + | |
| 139 | + foreach( $categories as $category ) { | |
| 140 | + $results[] = array( | |
| 141 | + 'id' => $category->id, | |
| 142 | + 'text' => $category->name, | |
| 143 | + ); | |
| 144 | + } | |
| 145 | + | |
| 146 | + return $results; | |
| 147 | + | |
| 148 | +} | |
| 149 | + | |
| 150 | +/** | |
| 151 | + * Ajax function for selecting tags | |
| 152 | + * | |
| 153 | + * @since 1.0.0 | |
| 154 | + */ | |
| 155 | +function automatorwp_shortlinkspro_ajax_get_tags() { | |
| 156 | + | |
| 157 | + // Security check | |
| 158 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 159 | + | |
| 160 | + // Permissions check | |
| 161 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 162 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 163 | + } | |
| 164 | + | |
| 165 | + $results = automatorwp_shortlinkspro_get_tags( isset( $_REQUEST['q'] ) ? $_REQUEST['q'] : '' ); | |
| 166 | + | |
| 167 | + // Prepend option none | |
| 168 | + $results = automatorwp_ajax_get_ajax_results_option_none( $results ); | |
| 169 | + | |
| 170 | + // Return our results | |
| 171 | + wp_send_json_success( $results ); | |
| 172 | + die; | |
| 173 | + | |
| 174 | +} | |
| 175 | +add_action( 'wp_ajax_automatorwp_shortlinkspro_get_tags', 'automatorwp_shortlinkspro_ajax_get_tags', 5 ); | |
| 176 | + | |
| 177 | +/** | |
| 178 | + * Function for selecting tags | |
| 179 | + * | |
| 180 | + * @since 1.0.0 | |
| 181 | + * | |
| 182 | + * @param string $search | |
| 183 | + * | |
| 184 | + * @return array | |
| 185 | + */ | |
| 186 | +function automatorwp_shortlinkspro_get_tags( $search = '' ) { | |
| 187 | + | |
| 188 | + global $wpdb; | |
| 189 | + | |
| 190 | + // Pull back the search string | |
| 191 | + $search = $wpdb->esc_like( $search ); | |
| 192 | + | |
| 193 | + $results = array(); | |
| 194 | + | |
| 195 | + // Setup table | |
| 196 | + $ct_table = ct_setup_table( 'shortlinkspro_link_tags' ); | |
| 197 | + | |
| 198 | + $tags = $wpdb->get_results( $wpdb->prepare( | |
| 199 | + "SELECT * | |
| 200 | + FROM {$ct_table->db->table_name} | |
| 201 | + WHERE name LIKE %s", | |
| 202 | + "%%{$search}%%" | |
| 203 | + ) ); | |
| 204 | + | |
| 205 | + ct_reset_setup_table(); | |
| 206 | + | |
| 207 | + foreach( $tags as $tag ) { | |
| 208 | + $results[] = array( | |
| 209 | + 'id' => $tag->id, | |
| 210 | + 'text' => $tag->name, | |
| 211 | + ); | |
| 212 | + } | |
| 213 | + | |
| 214 | + return $results; | |
| 215 | + | |
| 216 | +} | |