| @@ -1,98 +1,180 @@ | ||
| 1 | -<?php | |
| 2 | -namespace Booktics\Onboard\Controllers; | |
| 3 | - | |
| 4 | -use Booktics\Abstracts\Base_Rest_Controller; | |
| 5 | -use Booktics\Onboard\Onboarding; | |
| 6 | -use WP_REST_Server; | |
| 7 | - | |
| 8 | -/** | |
| 9 | - * Onboard controller | |
| 10 | - * | |
| 11 | - * @package Booktics/Onboard | |
| 12 | - */ | |
| 13 | -class Onboard_Controller extends Base_Rest_Controller { | |
| 14 | - /** | |
| 15 | - * Endpoint namespace | |
| 16 | - * | |
| 17 | - * @var string | |
| 18 | - */ | |
| 19 | - protected $namespace = 'booktics/v1'; | |
| 20 | - | |
| 21 | - /** | |
| 22 | - * Route name | |
| 23 | - * | |
| 24 | - * @var string | |
| 25 | - */ | |
| 26 | - protected $base = 'onboard'; | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * Register routes | |
| 30 | - * | |
| 31 | - * @return void | |
| 32 | - */ | |
| 33 | - public function register_routes(): void { | |
| 34 | - register_rest_route( | |
| 35 | - $this->namespace, $this->base, array( | |
| 36 | - array( | |
| 37 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 38 | - 'callback' => array( $this, 'setup_profile' ), | |
| 39 | - 'permission_callback' => array( $this, 'setup_profile_permissions_check' ), | |
| 40 | - ), | |
| 41 | - ) | |
| 42 | - ); | |
| 43 | - } | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * Setup bussinsess profile | |
| 47 | - * | |
| 48 | - * @param WP_Rest_Request $request Request Object | |
| 49 | - * | |
| 50 | - * @return WP_Rest_Response | WP_Error | |
| 51 | - */ | |
| 52 | - public function setup_profile( $request ) { | |
| 53 | - $data = json_decode( $request->get_body(), true ); | |
| 54 | - | |
| 55 | - $result = Onboarding::update( $data ); | |
| 56 | - | |
| 57 | - if ( is_wp_error( $result ) ) { | |
| 58 | - return $this->error( $result->get_error_message() ); | |
| 59 | - } | |
| 60 | - $message = __( 'Successfully updated business profile', 'booktics' ); | |
| 61 | - | |
| 62 | - $response = $this->prepare_item_for_response( $data, $request ); | |
| 63 | - | |
| 64 | - return $this->response( $response, $message ); | |
| 65 | - } | |
| 66 | - | |
| 67 | - /** | |
| 68 | - * Check permissions for setup profile | |
| 69 | - * | |
| 70 | - * @param WP_Rest_Request $request | |
| 71 | - * | |
| 72 | - * @return WP_Rest_Response | WP_Error | |
| 73 | - */ | |
| 74 | - public function setup_profile_permissions_check( $request ): bool { | |
| 75 | - return current_user_can( 'manage_options' ); | |
| 76 | - } | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * Prepare item for response | |
| 80 | - * | |
| 81 | - * @param array $item Response item | |
| 82 | - * @param array $request Requested data | |
| 83 | - * | |
| 84 | - * @return array Collection of response data | |
| 85 | - */ | |
| 86 | - public function prepare_item_for_response( $item, $request ): array { | |
| 87 | - $data = array( | |
| 88 | - 'business_email' => Onboarding::get( 'business_email' ), | |
| 89 | - 'business_name' => Onboarding::get( 'business_name' ), | |
| 90 | - 'business_type' => Onboarding::get( 'business_type' ), | |
| 91 | - 'business_timezone' => Onboarding::get( 'business_timezone' ), | |
| 92 | - 'schedule' => Onboarding::get( 'schedule' ), | |
| 93 | - 'onboard_setup' => Onboarding::get( 'onboard_setup' ), | |
| 94 | - ); | |
| 95 | - | |
| 96 | - return $data; | |
| 97 | - } | |
| 98 | -} | |
| 1 | +<?php | |
| 2 | +namespace Booktics\Onboard\Controllers; | |
| 3 | + | |
| 4 | +use Booktics\Abstracts\Base_Rest_Controller; | |
| 5 | +use Booktics\Onboard\Onboarding; | |
| 6 | +use WP_REST_Server; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * Onboard controller | |
| 10 | + * | |
| 11 | + * @package Booktics/Onboard | |
| 12 | + */ | |
| 13 | +class Onboard_Controller extends Base_Rest_Controller { | |
| 14 | + /** | |
| 15 | + * Endpoint namespace | |
| 16 | + * | |
| 17 | + * @var string | |
| 18 | + */ | |
| 19 | + protected $namespace = 'booktics/v1'; | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Route name | |
| 23 | + * | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + protected $base = 'onboard'; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Register routes | |
| 30 | + * | |
| 31 | + * @return void | |
| 32 | + */ | |
| 33 | + public function register_routes(): void { | |
| 34 | + register_rest_route( | |
| 35 | + $this->namespace, $this->base, array( | |
| 36 | + array( | |
| 37 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 38 | + 'callback' => array( $this, 'setup_profile' ), | |
| 39 | + 'permission_callback' => array( $this, 'setup_profile_permissions_check' ), | |
| 40 | + ), | |
| 41 | + ) | |
| 42 | + ); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Setup bussinsess profile | |
| 47 | + * | |
| 48 | + * @param WP_Rest_Request $request Request Object | |
| 49 | + * | |
| 50 | + * @return WP_Rest_Response | WP_Error | |
| 51 | + */ | |
| 52 | + public function setup_profile( $request ) { | |
| 53 | + $data = json_decode( $request->get_body(), true ); | |
| 54 | + | |
| 55 | + $result = Onboarding::update( $data ); | |
| 56 | + | |
| 57 | + if ( is_wp_error( $result ) ) { | |
| 58 | + return $this->error( $result->get_error_message() ); | |
| 59 | + } | |
| 60 | + $message = __( 'Successfully updated business profile', 'booktics' ); | |
| 61 | + | |
| 62 | + $response = $this->prepare_item_for_response( $data, $request ); | |
| 63 | + | |
| 64 | + // send webhook to fluentcrm | |
| 65 | + $this->connect_with_fluentcrm( $request ); | |
| 66 | + | |
| 67 | + return $this->response( $response, $message ); | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Check permissions for setup profile | |
| 72 | + * | |
| 73 | + * @param WP_Rest_Request $request | |
| 74 | + * | |
| 75 | + * @return WP_Rest_Response | WP_Error | |
| 76 | + */ | |
| 77 | + public function setup_profile_permissions_check( $request ): bool { | |
| 78 | + return current_user_can( 'manage_options' ); | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * Prepare item for response | |
| 83 | + * | |
| 84 | + * @param array $item Response item | |
| 85 | + * @param array $request Requested data | |
| 86 | + * | |
| 87 | + * @return array Collection of response data | |
| 88 | + */ | |
| 89 | + public function prepare_item_for_response( $item, $request ): array { | |
| 90 | + $data = array( | |
| 91 | + 'business_email' => Onboarding::get( 'business_email' ), | |
| 92 | + 'business_name' => Onboarding::get( 'business_name' ), | |
| 93 | + 'business_type' => Onboarding::get( 'business_type' ), | |
| 94 | + 'business_timezone' => Onboarding::get( 'business_timezone' ), | |
| 95 | + 'schedule' => Onboarding::get( 'schedule' ), | |
| 96 | + 'onboard_setup' => Onboarding::get( 'onboard_setup' ), | |
| 97 | + ); | |
| 98 | + | |
| 99 | + return $data; | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * Connect with FluentCRM webhook | |
| 104 | + * | |
| 105 | + * @param WP_Rest_Request $request Request object | |
| 106 | + * @return bool|WP_Error True on success, WP_Error on failure | |
| 107 | + */ | |
| 108 | + public function connect_with_fluentcrm( $request ) { | |
| 109 | + $data = json_decode( $request->get_body(), true ); | |
| 110 | + | |
| 111 | + // Validate request data | |
| 112 | + if ( ! is_array( $data ) ) { | |
| 113 | + return $this->error( __( 'Invalid request data.', 'booktics' ) ); | |
| 114 | + } | |
| 115 | + | |
| 116 | + if ( empty( $data['business_email'] ) ) { | |
| 117 | + return $this->error( __( 'Business email is required.', 'booktics' ) ); | |
| 118 | + } | |
| 119 | + | |
| 120 | + $email = sanitize_email( $data['business_email'] ); | |
| 121 | + if ( ! is_email( $email ) ) { | |
| 122 | + return $this->error( __( 'Invalid email address.', 'booktics' ) ); | |
| 123 | + } | |
| 124 | + | |
| 125 | + // Permission check: only allow users with 'manage_options' | |
| 126 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 127 | + return $this->error( __( 'You do not have permission to update settings.', 'booktics' ) ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + // Verify email status using external API | |
| 131 | + $isValidEmail = $this->verify_email_status( $email ); | |
| 132 | + | |
| 133 | + | |
| 134 | + if ( true !== $isValidEmail ) { | |
| 135 | + return $this->error( __( 'Email address could not be verified as valid.', 'booktics' ) ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + // Prepare webhook call | |
| 139 | + $webhook_url = 'https://arraytics.com/?fluentcrm=1&route=contact&hash=785538fa-05ea-40ea-8762-02993eab4115'; | |
| 140 | + $body = [ 'email' => $email ]; | |
| 141 | + $webhook_response = wp_remote_post( $webhook_url, [ 'body' => $body ] ); | |
| 142 | + | |
| 143 | + if ( is_wp_error( $webhook_response ) ) { | |
| 144 | + if ( function_exists( 'error_log' ) && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 145 | + error_log( 'FluentCRM webhook error: ' . $webhook_response->get_error_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 146 | + } | |
| 147 | + return $this->error( $webhook_response->get_error_message() ); | |
| 148 | + } | |
| 149 | + | |
| 150 | + return true; | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * Verify email status | |
| 155 | + * | |
| 156 | + * @param string $email Email address | |
| 157 | + * @return bool True if email is valid, false otherwise | |
| 158 | + */ | |
| 159 | + public function verify_email_status(string $email = "") | |
| 160 | + { | |
| 161 | + $api_key = 'HGyNLSeQVNfcTpkMRfCJrECTjoEIfXPJ'; // Replace with your actual API key | |
| 162 | + $url = 'https://emailverifier.reoon.com/api/v1/verify'; | |
| 163 | + | |
| 164 | + $response = wp_remote_get(add_query_arg([ | |
| 165 | + 'email' => $email, | |
| 166 | + 'key' => $api_key, | |
| 167 | + 'mode' => 'quick', | |
| 168 | + ], $url)); | |
| 169 | + | |
| 170 | + if (is_wp_error($response)) { | |
| 171 | + return 'error'; | |
| 172 | + } | |
| 173 | + | |
| 174 | + $body = wp_remote_retrieve_body($response); | |
| 175 | + $data = json_decode($body, true); | |
| 176 | + | |
| 177 | + | |
| 178 | + return isset($data['status']) && $data['status'] === "valid"; | |
| 179 | + } | |
| 180 | +} | |