| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Elementor\Plugin as Elementor; |
| 6 |
use Sellkit\Funnel\Contacts\Base_Contacts; |
| 7 |
use Sellkit_Elementor_Optin_Module as Module; |
| 8 |
|
| 9 |
/** |
| 10 |
* Initializing the ajax handler class for handling form ajax requests. |
| 11 |
* |
| 12 |
* @since 1.5.0 |
| 13 |
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity) |
| 14 |
*/ |
| 15 |
class Sellkit_Elementor_Optin_Ajaxhandler { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds all the responses. |
| 19 |
* |
| 20 |
* @access public |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
public $response = [ |
| 24 |
'message' => [], |
| 25 |
'errors' => [], |
| 26 |
'admin_errors' => [], |
| 27 |
]; |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds the form settings. |
| 31 |
* |
| 32 |
* @access public |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
public $form; |
| 36 |
|
| 37 |
/** |
| 38 |
* Holds a record of the user-filled form. |
| 39 |
* |
| 40 |
* @access public |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
public $form_data; |
| 44 |
|
| 45 |
/** |
| 46 |
* Holds the reponse state. |
| 47 |
* |
| 48 |
* @access public |
| 49 |
* @var bool |
| 50 |
*/ |
| 51 |
public $is_success = true; |
| 52 |
|
| 53 |
/** |
| 54 |
* Initializes the AJAX handler class with registering AJAX hooks. |
| 55 |
* |
| 56 |
* @since 1.5.0 |
| 57 |
*/ |
| 58 |
public function __construct() { |
| 59 |
add_action( 'wp_ajax_sellkit_optin_frontend', [ $this, 'handle_frontend' ] ); |
| 60 |
add_action( 'wp_ajax_nopriv_sellkit_optin_frontend', [ $this, 'handle_frontend' ] ); |
| 61 |
add_action( 'wp_ajax_sellkit_optin_editor', [ $this, 'handle_editor' ] ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Handle the form submit in frontend. |
| 66 |
* |
| 67 |
* @since 1.5.0 |
| 68 |
* @access public |
| 69 |
*/ |
| 70 |
public function handle_frontend() { |
| 71 |
if ( false === check_ajax_referer( 'sellkit_elementor', 'nonce', false ) ) { |
| 72 |
$this |
| 73 |
->add_response( 'admin_errors', esc_html__( 'Error: Nonce mismatch or expired. Please reload the page and retry.', 'sellkit' ) ) |
| 74 |
->set_success( false ) |
| 75 |
->send_response(); |
| 76 |
|
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$post_id = filter_input( INPUT_POST, 'post_id' ); |
| 81 |
$form_id = filter_input( INPUT_POST, 'form_id' ); |
| 82 |
$funnel_page_id = filter_input( INPUT_POST, 'sellkit_current_page_id', FILTER_SANITIZE_NUMBER_INT ); |
| 83 |
|
| 84 |
$this->form_data = $_POST; // @codingStandardsIgnoreLine |
| 85 |
|
| 86 |
$post_elements = Elementor::$instance->documents->get( $post_id )->get_elements_data(); |
| 87 |
$this->form = self::find_form_recursive( $post_elements, $form_id ); |
| 88 |
$funnel = Sellkit_Funnel::get_instance( $funnel_page_id ); |
| 89 |
|
| 90 |
if ( 'sales-page' === $funnel->current_step_data['type']['key'] ) { |
| 91 |
$this |
| 92 |
->add_response( 'errors', esc_html__( 'Opt-in widget should not be used in the sales page step.', 'sellkit' ) ) |
| 93 |
->set_success( false ); |
| 94 |
|
| 95 |
wp_send_json_error( $this->response ); |
| 96 |
} |
| 97 |
|
| 98 |
$this |
| 99 |
->validate_form() |
| 100 |
->fill_form_settings() |
| 101 |
->set_custom_messages() |
| 102 |
->run_actions() |
| 103 |
->send_response(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Handle the form requests in editor. |
| 108 |
* |
| 109 |
* @since 1.5.0 |
| 110 |
* @access public |
| 111 |
*/ |
| 112 |
public function handle_editor() { |
| 113 |
$nonce_valid = check_ajax_referer( 'sellkit_optin_editor', 'nonce', false ); |
| 114 |
$action = filter_input( INPUT_POST, 'service' ); |
| 115 |
$request = filter_input( INPUT_POST, 'request' ); |
| 116 |
$params = filter_input( INPUT_POST, 'params', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 117 |
|
| 118 |
if ( false === $nonce_valid ) { |
| 119 |
$this->set_success( false )->send_response(); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
$class_name = 'Sellkit_Elementor_Optin_Action_' . ucfirst( $action ); |
| 124 |
$class_name::get_instance()->$request( $this, empty( $params ) ? [] : $params ); |
| 125 |
|
| 126 |
$this->send_response(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Loop recursively through post meta and find the form element. |
| 131 |
* |
| 132 |
* @param array $post_elements elements data of the post. |
| 133 |
* @param string $form_id id of the form. |
| 134 |
* @return array|false data of the form in question or false on failure. |
| 135 |
* |
| 136 |
* @since 1.5.0 |
| 137 |
* @access public |
| 138 |
* @static |
| 139 |
*/ |
| 140 |
public static function find_form_recursive( $post_elements, $form_id ) { |
| 141 |
foreach ( $post_elements as $element ) { |
| 142 |
if ( $form_id === $element['id'] ) { |
| 143 |
return $element; |
| 144 |
} |
| 145 |
|
| 146 |
if ( ! empty( $element['elements'] ) ) { |
| 147 |
$element = self::find_form_recursive( $element['elements'], $form_id ); |
| 148 |
|
| 149 |
if ( $element ) { |
| 150 |
return $element; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Set form state to success/error. |
| 160 |
* |
| 161 |
* @param boolean $bool True or false. |
| 162 |
* |
| 163 |
* @since 1.5.0 |
| 164 |
* @access public |
| 165 |
*/ |
| 166 |
public function set_success( $bool ) { |
| 167 |
$this->is_success = $bool; |
| 168 |
return $this; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Fills "settings" key of the form by creating an instance of it. |
| 173 |
* |
| 174 |
* @since 1.5.0 |
| 175 |
* @access private |
| 176 |
*/ |
| 177 |
private function fill_form_settings() { |
| 178 |
$this->form['settings'] = |
| 179 |
Elementor::$instance |
| 180 |
->elements_manager |
| 181 |
->create_element_instance( $this->form ) |
| 182 |
->get_settings_for_display(); |
| 183 |
|
| 184 |
return $this; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Validate the form based on form ID. |
| 189 |
* |
| 190 |
* @since 1.5.0 |
| 191 |
* @access public |
| 192 |
*/ |
| 193 |
public function set_custom_messages() { |
| 194 |
$form = $this->form; |
| 195 |
|
| 196 |
if ( ! $form ) { |
| 197 |
return $this; |
| 198 |
} |
| 199 |
|
| 200 |
if ( empty( $form['settings']['messages_custom'] ) ) { |
| 201 |
return $this; |
| 202 |
} |
| 203 |
|
| 204 |
Module::$messages = [ |
| 205 |
'success' => $form['settings']['messages_success'], |
| 206 |
'error' => $form['settings']['messages_error'], |
| 207 |
'required' => $form['settings']['messages_required'], |
| 208 |
]; |
| 209 |
|
| 210 |
return $this; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Validate the form based on form ID. |
| 215 |
* |
| 216 |
* @since 1.5.0 |
| 217 |
* @access private |
| 218 |
*/ |
| 219 |
private function validate_form() { |
| 220 |
if ( $this->form ) { |
| 221 |
return $this; |
| 222 |
} |
| 223 |
|
| 224 |
$this |
| 225 |
->add_response( 'message', esc_html__( 'There\'s something wrong. The form is not valid.', 'sellkit' ) ) |
| 226 |
->set_success( false ) |
| 227 |
->send_response(); |
| 228 |
|
| 229 |
return $this; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Run all the specified actions. |
| 234 |
* |
| 235 |
* @since 1.5.0 |
| 236 |
* @access private |
| 237 |
*/ |
| 238 |
private function run_actions() { |
| 239 |
$actions = $this->form['settings']['crm_actions']; |
| 240 |
|
| 241 |
if ( ! empty( $actions ) ) { |
| 242 |
$actions = is_array( $actions ) ? $actions : [ $actions ]; |
| 243 |
|
| 244 |
foreach ( $actions as $action ) { |
| 245 |
$class_name = Module::$action_types[ $action ]; |
| 246 |
$class_name::get_instance()->run( $this ); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
// Always run "After Submit Actions". |
| 251 |
Sellkit_Elementor_Optin_Action_Download_Redirect::get_instance()->run( $this ); |
| 252 |
|
| 253 |
return $this; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Add response to ajax response. |
| 258 |
* |
| 259 |
* @param string $type Response type. |
| 260 |
* @param string $text Response text. |
| 261 |
* @param string $text_key Response text key. |
| 262 |
* |
| 263 |
* @since 1.5.0 |
| 264 |
* @access public |
| 265 |
*/ |
| 266 |
public function add_response( $type, $text = '', $text_key = '' ) { |
| 267 |
if ( ! empty( $text_key ) ) { |
| 268 |
$this->response[ $type ][ $text_key ] = $text; |
| 269 |
return $this; |
| 270 |
} |
| 271 |
|
| 272 |
$this->response[ $type ][] = $text; |
| 273 |
return $this; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Send success/fail response. |
| 278 |
* |
| 279 |
* @since 1.5.0 |
| 280 |
* @access public |
| 281 |
*/ |
| 282 |
public function send_response() { |
| 283 |
if ( ! current_user_can( 'administrator' ) ) { |
| 284 |
unset( $this->response['admin_errors'] ); |
| 285 |
} else { |
| 286 |
// Flatten admin_errors. |
| 287 |
$this->response['admin_errors'] = array_values( $this->response['admin_errors'] ); |
| 288 |
} |
| 289 |
|
| 290 |
if ( $this->is_success ) { |
| 291 |
if ( empty( $this->response['message'] ) ) { |
| 292 |
$this->add_response( 'message', Module::$messages['success'] ); |
| 293 |
} |
| 294 |
|
| 295 |
Base_Contacts::step_is_passed(); |
| 296 |
|
| 297 |
wp_send_json_success( $this->response ); |
| 298 |
} |
| 299 |
|
| 300 |
if ( ! empty( $this->response['errors'] ) ) { |
| 301 |
$this->add_response( 'message', Module::$messages['error'] ); |
| 302 |
} |
| 303 |
|
| 304 |
wp_send_json_error( $this->response ); |
| 305 |
} |
| 306 |
} |
| 307 |
|