| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\ClickUp\Ajax_Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* AJAX handler for the authorize action |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
function automatorwp_clickup_ajax_authorize() { |
| 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 |
$prefix = 'automatorwp_clickup_'; |
| 26 |
|
| 27 |
$url = automatorwp_clickup_get_url(); |
| 28 |
$token = sanitize_text_field( $_POST['token'] ); |
| 29 |
|
| 30 |
// Check parameters given |
| 31 |
if( empty( $token ) ) { |
| 32 |
wp_send_json_error( array( 'message' => __( 'API Token is required to connect with ClickUp', 'automatorwp' ) ) ); |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// To get first answer and check the connection |
| 37 |
$response = wp_remote_get( $url . '/team', array( |
| 38 |
'headers' => array( |
| 39 |
'Authorization' => $token, |
| 40 |
'Accept' => 'application/json', |
| 41 |
'Content-Type' => 'application/json' |
| 42 |
) |
| 43 |
) ); |
| 44 |
|
| 45 |
// Incorrect API token |
| 46 |
if ( isset( $response['response']['code'] ) && $response['response']['code'] !== 200 ){ |
| 47 |
wp_send_json_error (array( 'message' => __( 'Please, check your credentials', 'automatorwp' ) ) ); |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$settings = get_option( 'automatorwp_settings' ); |
| 52 |
|
| 53 |
// Save client url and API key |
| 54 |
$settings[$prefix . 'token'] = $token; |
| 55 |
|
| 56 |
// Update settings |
| 57 |
update_option( 'automatorwp_settings', $settings ); |
| 58 |
$admin_url = str_replace( 'http://', 'http://', get_admin_url() ) . 'admin.php?page=automatorwp_settings&tab=opt-tab-clickup'; |
| 59 |
|
| 60 |
wp_send_json_success( array( |
| 61 |
'message' => __( 'Correct data to connect with ClickUp', 'automatorwp' ), |
| 62 |
'redirect_url' => $admin_url |
| 63 |
) ); |
| 64 |
|
| 65 |
} |
| 66 |
add_action( 'wp_ajax_automatorwp_clickup_authorize', 'automatorwp_clickup_ajax_authorize' ); |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Ajax function for selecting teams |
| 71 |
* |
| 72 |
* @since 1.0.0 |
| 73 |
*/ |
| 74 |
function automatorwp_clickup_ajax_get_teams() { |
| 75 |
// Security check, forces to die if not security passed |
| 76 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 77 |
|
| 78 |
// Permissions check |
| 79 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 80 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 81 |
} |
| 82 |
|
| 83 |
global $wpdb; |
| 84 |
|
| 85 |
// Pull back the search string |
| 86 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 87 |
|
| 88 |
$teams = automatorwp_clickup_get_teams(); |
| 89 |
|
| 90 |
$results = array(); |
| 91 |
|
| 92 |
// Parse teams results to match select2 results |
| 93 |
foreach ( $teams as $team ) { |
| 94 |
|
| 95 |
if( ! empty( $search ) ) { |
| 96 |
if( strpos( strtolower( $team['name'] ), strtolower( $search ) ) === false ) { |
| 97 |
continue; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
$results[] = array( |
| 102 |
'id' => $team['id'], |
| 103 |
'text' => $team['name'] |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
// Prepend option none |
| 108 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 109 |
|
| 110 |
// Return our results |
| 111 |
wp_send_json_success( $results ); |
| 112 |
die; |
| 113 |
|
| 114 |
} |
| 115 |
add_action( 'wp_ajax_automatorwp_clickup_get_teams', 'automatorwp_clickup_ajax_get_teams' ); |
| 116 |
|
| 117 |
/** |
| 118 |
* Ajax function for selecting spaces |
| 119 |
* |
| 120 |
* @since 1.0.0 |
| 121 |
*/ |
| 122 |
function automatorwp_clickup_ajax_get_spaces() { |
| 123 |
// Security check, forces to die if not security passed |
| 124 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 125 |
|
| 126 |
// Permissions check |
| 127 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 128 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 129 |
} |
| 130 |
|
| 131 |
global $wpdb; |
| 132 |
|
| 133 |
// Pull back the search string |
| 134 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 135 |
|
| 136 |
// Get Team ID |
| 137 |
$team_id = isset( $_REQUEST['table'] ) ? sanitize_text_field( $_REQUEST['table'] ) : ''; |
| 138 |
|
| 139 |
$spaces = automatorwp_clickup_get_spaces( $team_id ); |
| 140 |
|
| 141 |
$results = array(); |
| 142 |
|
| 143 |
// Parse spaces results to match select2 results |
| 144 |
foreach ( $spaces as $space ) { |
| 145 |
|
| 146 |
if( ! empty( $search ) ) { |
| 147 |
if( strpos( strtolower( $space['name'] ), strtolower( $search ) ) === false ) { |
| 148 |
continue; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
$results[] = array( |
| 153 |
'id' => strval($space['id']), |
| 154 |
'text' => $space['name'] |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
// Prepend option none |
| 159 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 160 |
|
| 161 |
// Return our results |
| 162 |
wp_send_json_success( $results ); |
| 163 |
die; |
| 164 |
|
| 165 |
} |
| 166 |
add_action( 'wp_ajax_automatorwp_clickup_get_spaces', 'automatorwp_clickup_ajax_get_spaces' ); |
| 167 |
|
| 168 |
/** |
| 169 |
* Ajax function for selecting folders |
| 170 |
* |
| 171 |
* @since 1.0.0 |
| 172 |
*/ |
| 173 |
function automatorwp_clickup_ajax_get_folders() { |
| 174 |
// Security check, forces to die if not security passed |
| 175 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 176 |
|
| 177 |
// Permissions check |
| 178 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 179 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 180 |
} |
| 181 |
|
| 182 |
global $wpdb; |
| 183 |
|
| 184 |
// Pull back the search string |
| 185 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 186 |
|
| 187 |
// Get Space ID |
| 188 |
$space_id = isset( $_REQUEST['table'] ) ? sanitize_text_field( $_REQUEST['table'] ) : ''; |
| 189 |
|
| 190 |
$folders = automatorwp_clickup_get_folders( $space_id ); |
| 191 |
|
| 192 |
$results = array(); |
| 193 |
|
| 194 |
// Parse folders results to match select2 results |
| 195 |
foreach ( $folders as $folder ) { |
| 196 |
|
| 197 |
if( ! empty( $search ) ) { |
| 198 |
if( strpos( strtolower( $folder['name'] ), strtolower( $search ) ) === false ) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
$results[] = array( |
| 204 |
'id' => strval($folder['id']), |
| 205 |
'text' => $folder['name'] |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
// Prepend option none |
| 210 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 211 |
|
| 212 |
// Return our results |
| 213 |
wp_send_json_success( $results ); |
| 214 |
die; |
| 215 |
|
| 216 |
} |
| 217 |
add_action( 'wp_ajax_automatorwp_clickup_get_folders', 'automatorwp_clickup_ajax_get_folders' ); |
| 218 |
|
| 219 |
/** |
| 220 |
* Ajax function for selecting lists |
| 221 |
* |
| 222 |
* @since 1.0.0 |
| 223 |
*/ |
| 224 |
function automatorwp_clickup_ajax_get_lists() { |
| 225 |
// Security check, forces to die if not security passed |
| 226 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 227 |
|
| 228 |
// Permissions check |
| 229 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 230 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 231 |
} |
| 232 |
|
| 233 |
global $wpdb; |
| 234 |
|
| 235 |
// Pull back the search string |
| 236 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 237 |
|
| 238 |
// Get Folder ID |
| 239 |
$folder_id = isset( $_REQUEST['table'] ) ? sanitize_text_field( $_REQUEST['table'] ) : ''; |
| 240 |
|
| 241 |
$lists = automatorwp_clickup_get_lists( $folder_id ); |
| 242 |
|
| 243 |
$results = array(); |
| 244 |
|
| 245 |
// Parse lists results to match select2 results |
| 246 |
foreach ( $lists as $list ) { |
| 247 |
|
| 248 |
if( ! empty( $search ) ) { |
| 249 |
if( strpos( strtolower( $list['name'] ), strtolower( $search ) ) === false ) { |
| 250 |
continue; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
$results[] = array( |
| 255 |
'id' => strval($list['id']), |
| 256 |
'text' => $list['name'] |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
// Prepend option none |
| 261 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 262 |
|
| 263 |
// Return our results |
| 264 |
wp_send_json_success( $results ); |
| 265 |
die; |
| 266 |
|
| 267 |
} |
| 268 |
add_action( 'wp_ajax_automatorwp_clickup_get_lists', 'automatorwp_clickup_ajax_get_lists' ); |
| 269 |
|
| 270 |
/** |
| 271 |
* Ajax function for selecting tasks |
| 272 |
* |
| 273 |
* @since 1.0.0 |
| 274 |
*/ |
| 275 |
function automatorwp_clickup_ajax_get_tasks() { |
| 276 |
// Security check, forces to die if not security passed |
| 277 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 278 |
|
| 279 |
// Permissions check |
| 280 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 281 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 282 |
} |
| 283 |
|
| 284 |
global $wpdb; |
| 285 |
|
| 286 |
// Pull back the search string |
| 287 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 288 |
|
| 289 |
// Get List ID |
| 290 |
$list_id = isset( $_REQUEST['table'] ) ? sanitize_text_field( $_REQUEST['table'] ) : ''; |
| 291 |
|
| 292 |
$tasks = automatorwp_clickup_get_tasks( $list_id ); |
| 293 |
|
| 294 |
$results = array(); |
| 295 |
|
| 296 |
// Parse tasks results to match select2 results |
| 297 |
foreach ( $tasks as $task ) { |
| 298 |
|
| 299 |
if( ! empty( $search ) ) { |
| 300 |
if( strpos( strtolower( $task['name'] ), strtolower( $search ) ) === false ) { |
| 301 |
continue; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
$results[] = array( |
| 306 |
'id' => strval($task['id']), |
| 307 |
'text' => $task['name'] |
| 308 |
); |
| 309 |
} |
| 310 |
|
| 311 |
// Prepend option none |
| 312 |
$results = automatorwp_ajax_parse_extra_options( $results ); |
| 313 |
|
| 314 |
// Return our results |
| 315 |
wp_send_json_success( $results ); |
| 316 |
die; |
| 317 |
|
| 318 |
} |
| 319 |
add_action( 'wp_ajax_automatorwp_clickup_get_tasks', 'automatorwp_clickup_ajax_get_tasks' ); |