| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Zoom\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_zoom_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_zoom_'; |
| 26 |
|
| 27 |
$client_id = sanitize_text_field( $_POST['client_id'] ); |
| 28 |
$client_secret = sanitize_text_field( $_POST['client_secret'] ); |
| 29 |
$platform = sanitize_text_field( $_POST['platform'] ); |
| 30 |
|
| 31 |
// Check parameters given |
| 32 |
if( empty( $client_id ) || empty( $client_secret ) ) { |
| 33 |
wp_send_json_error( array( 'message' => __( 'All fields are required to connect with Zoom', 'automatorwp' ) ) ); |
| 34 |
} |
| 35 |
|
| 36 |
$settings = get_option( 'automatorwp_settings' ); |
| 37 |
|
| 38 |
// Save client id and secret |
| 39 |
$settings[$prefix . $platform . '_client_id'] = $client_id; |
| 40 |
$settings[$prefix . $platform . '_client_secret'] = $client_secret; |
| 41 |
|
| 42 |
// Update settings |
| 43 |
update_option( 'automatorwp_settings', $settings ); |
| 44 |
|
| 45 |
$state = $prefix . $platform; |
| 46 |
$admin_url = str_replace( 'http://', 'https://', get_admin_url() ) . 'admin.php?page=automatorwp_settings&tab=opt-tab-zoom'; |
| 47 |
$redirect_url = 'https://zoom.us/oauth/authorize?response_type=code&client_id=' . $client_id . '&state=' . urlencode( $state ) . '&redirect_uri=' . urlencode( $admin_url ); |
| 48 |
|
| 49 |
// Return the redirect URL |
| 50 |
wp_send_json_success( array( |
| 51 |
'message' => __( 'Zoom settings saved successfully, redirecting to Zoom...', 'automatorwp' ), |
| 52 |
'redirect_url' => $redirect_url |
| 53 |
) ); |
| 54 |
|
| 55 |
} |
| 56 |
add_action( 'wp_ajax_automatorwp_zoom_authorize', 'automatorwp_zoom_ajax_authorize' ); |
| 57 |
|
| 58 |
/** |
| 59 |
* Ajax function for selecting meetings |
| 60 |
* |
| 61 |
* @since 1.0.0 |
| 62 |
*/ |
| 63 |
function automatorwp_zoom_ajax_get_meetings() { |
| 64 |
// Security check, forces to die if not security passed |
| 65 |
check_ajax_referer( 'automatorwp_admin', 'nonce' ); |
| 66 |
|
| 67 |
// Permissions check |
| 68 |
if( ! current_user_can( automatorwp_get_manager_capability() ) ) { |
| 69 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
global $wpdb; |
| 73 |
|
| 74 |
// Pull back the search string |
| 75 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; |
| 76 |
|
| 77 |
$meetings = automatorwp_zoom_get_meetings(); |
| 78 |
|
| 79 |
// Parse meetings results to match select2 results |
| 80 |
foreach ( $meetings as $meeting ) { |
| 81 |
|
| 82 |
if( ! empty( $search ) ) { |
| 83 |
if( strpos( strtolower( $meeting['name'] ), strtolower( $search ) ) === false ) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
$results[] = array( |
| 89 |
'id' => $meeting['id'], |
| 90 |
'text' => $meeting['name'] |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
// Prepend option none |
| 95 |
$results = automatorwp_ajax_get_ajax_results_option_none( $results ); |
| 96 |
|
| 97 |
// Return our results |
| 98 |
wp_send_json_success( $results ); |
| 99 |
die; |
| 100 |
|
| 101 |
} |
| 102 |
add_action( 'wp_ajax_automatorwp_zoom_get_meetings', 'automatorwp_zoom_ajax_get_meetings' ); |