| @@ -1,0 +1,58 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Ajax Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\Bluesky\Ajax_Functions | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | + | |
| 10 | +// Exit if accessed directly | |
| 11 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * AJAX handler for the authorize action | |
| 15 | + * | |
| 16 | + * @since 1.0.0 | |
| 17 | + */ | |
| 18 | +function automatorwp_bluesky_ajax_authorize() { | |
| 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 | + $prefix = 'automatorwp_bluesky_'; | |
| 28 | + | |
| 29 | + $user_handle = automatorwp_bluesky_validate_name_account( sanitize_text_field( $_POST["user_handle"] ) ); | |
| 30 | + $user_password = sanitize_text_field( $_POST['user_password'] ); | |
| 31 | + | |
| 32 | + if( empty( $user_handle ) || empty( $user_password ) ) { | |
| 33 | + wp_send_json_error( array( 'message' => __( 'All fields are required to connect with Bluesky', 'automatorwp' ) ) ); | |
| 34 | + return; | |
| 35 | + } | |
| 36 | + | |
| 37 | + $status = automatorwp_bluesky_check_settings_status(['user_handle' => $user_handle, 'user_password' => $user_password]); | |
| 38 | + | |
| 39 | + if ( empty( $status ) ) { | |
| 40 | + return; | |
| 41 | + } | |
| 42 | + | |
| 43 | + $settings = get_option( 'automatorwp_settings' ); | |
| 44 | + | |
| 45 | + // Save API user_handle and API user_password | |
| 46 | + $settings[$prefix . 'user_handle'] = $user_handle; | |
| 47 | + $settings[$prefix . 'user_password'] = $user_password; | |
| 48 | + | |
| 49 | + // Update settings | |
| 50 | + update_option( 'automatorwp_settings', $settings ); | |
| 51 | + $admin_url = admin_url( 'admin.php?page=automatorwp_settings&tab=opt-tab-bluesky' ); | |
| 52 | + | |
| 53 | + wp_send_json_success( array( | |
| 54 | + 'message' => __( 'Correct data to connect with Bluesky', 'automatorwp' ), | |
| 55 | + 'redirect_url' => $admin_url | |
| 56 | + ) ); | |
| 57 | +} | |
| 58 | +add_action( 'wp_ajax_automatorwp_bluesky_authorize', 'automatorwp_bluesky_ajax_authorize' ); | |