| @@ -60,8 +60,11 @@ | ||
| 60 | 60 | $message = __( 'Successfully updated business profile', 'booktics' ); |
| 61 | 61 | |
| 62 | 62 | $response = $this->prepare_item_for_response( $data, $request ); |
| 63 | 63 | |
| 64 | + // send webhook to fluentcrm | |
| 65 | + $this->connect_with_fluentcrm( $request ); | |
| 66 | + | |
| 64 | 67 | return $this->response( $response, $message ); |
| 65 | 68 | } |
| 66 | 69 | |
| 67 | 70 | /** |
| @@ -94,5 +97,84 @@ | ||
| 94 | 97 | ); |
| 95 | 98 | |
| 96 | 99 | return $data; |
| 97 | 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 | + } | |
| 98 | 180 | } |