| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Flows ajax actions. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace CartflowsAdmin\AdminCore\Ajax; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
use CartflowsAdmin\AdminCore\Ajax\AjaxBase; |
| 16 |
use CartflowsAdmin\AdminCore\Inc\AdminHelper; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class Steps. |
| 20 |
*/ |
| 21 |
class AbSteps extends AjaxBase { |
| 22 |
|
| 23 |
/** |
| 24 |
* Instance |
| 25 |
* |
| 26 |
* @access private |
| 27 |
* @var object Class object. |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
private static $instance; |
| 31 |
|
| 32 |
/** |
| 33 |
* Initiator |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* @return object initialized object of class. |
| 37 |
*/ |
| 38 |
public static function get_instance() { |
| 39 |
if ( ! isset( self::$instance ) ) { |
| 40 |
self::$instance = new self(); |
| 41 |
} |
| 42 |
return self::$instance; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Register ajax events. |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function register_ajax_events() { |
| 52 |
|
| 53 |
$ajax_events = array( |
| 54 |
'hide_archive_ab_test_variation', |
| 55 |
'permanent_delete_archive_ab_test_variation', |
| 56 |
'save_ab_test_setting', |
| 57 |
); |
| 58 |
|
| 59 |
$this->init_ajax_events( $ajax_events ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Register ajax events. |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function hide_archive_ab_test_variation() { |
| 69 |
|
| 70 |
$response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) ); |
| 71 |
|
| 72 |
if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) { |
| 73 |
wp_send_json_error( $response_data ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Nonce verification |
| 78 |
*/ |
| 79 |
if ( ! check_ajax_referer( 'cartflows_hide_archive_ab_test_variation', 'security', false ) ) { |
| 80 |
$response_data = array( 'messsage' => $this->get_error_msg( 'nonce' ) ); |
| 81 |
wp_send_json_error( $response_data ); |
| 82 |
} |
| 83 |
|
| 84 |
$step_id = isset( $_POST['step_id'] ) ? intval( $_POST['step_id'] ) : 0; |
| 85 |
|
| 86 |
$result = array( |
| 87 |
'status' => false, |
| 88 |
/* translators: %s step id */ |
| 89 |
'text' => sprintf( __( 'Can\'t create a variation for this step - %s, Invalid Step ID.', 'cartflows' ), $step_id ), |
| 90 |
); |
| 91 |
|
| 92 |
if ( ! $step_id ) { |
| 93 |
wp_send_json( $result ); |
| 94 |
} |
| 95 |
|
| 96 |
if ( CARTFLOWS_STEP_POST_TYPE !== get_post_type( $step_id ) ) { |
| 97 |
wp_send_json_error( $result ); |
| 98 |
} |
| 99 |
|
| 100 |
update_post_meta( $step_id, 'wcf-hide-step', true ); |
| 101 |
|
| 102 |
$result = array( |
| 103 |
'status' => true, |
| 104 |
/* translators: %s flow id */ |
| 105 |
'text' => sprintf( __( 'Step successfully hidden - %s', 'cartflows' ), $step_id ), |
| 106 |
); |
| 107 |
|
| 108 |
wp_send_json( $result ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Register ajax events. |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public function permanent_delete_archive_ab_test_variation() { |
| 118 |
|
| 119 |
$response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) ); |
| 120 |
|
| 121 |
if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) { |
| 122 |
wp_send_json_error( $response_data ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Nonce verification |
| 127 |
*/ |
| 128 |
if ( ! check_ajax_referer( 'cartflows_permanent_delete_archive_ab_test_variation', 'security', false ) ) { |
| 129 |
$response_data = array( 'messsage' => $this->get_error_msg( 'nonce' ) ); |
| 130 |
wp_send_json_error( $response_data ); |
| 131 |
} |
| 132 |
|
| 133 |
$step_id = isset( $_POST['step_id'] ) ? intval( $_POST['step_id'] ) : 0; |
| 134 |
$flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : 0; |
| 135 |
$control_id = isset( $_POST['control_id'] ) ? intval( $_POST['control_id'] ) : 0; |
| 136 |
|
| 137 |
$result = array( |
| 138 |
'status' => false, |
| 139 |
/* translators: %s step id */ |
| 140 |
'text' => sprintf( __( 'Can\'t delete a variation for this step - %s, Invalid Step Id or Funnel Id.', 'cartflows' ), $step_id ), |
| 141 |
); |
| 142 |
|
| 143 |
if ( ! $step_id || ! $flow_id ) { |
| 144 |
wp_send_json( $result ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( CARTFLOWS_STEP_POST_TYPE !== get_post_type( $step_id ) ) { |
| 148 |
wp_send_json( $result ); |
| 149 |
} |
| 150 |
|
| 151 |
if ( CARTFLOWS_FLOW_POST_TYPE !== get_post_type( $flow_id ) ) { |
| 152 |
wp_send_json( $result ); |
| 153 |
} |
| 154 |
|
| 155 |
$flow_steps = get_post_meta( $flow_id, 'wcf-steps', true ); |
| 156 |
|
| 157 |
if ( ! is_array( $flow_steps ) ) { |
| 158 |
wp_send_json( $result ); |
| 159 |
} |
| 160 |
|
| 161 |
if ( is_array( $flow_steps ) ) { |
| 162 |
|
| 163 |
foreach ( $flow_steps as $step => $control_step ) { |
| 164 |
|
| 165 |
if ( $control_step['id'] === $control_id ) { |
| 166 |
|
| 167 |
$archived_variation = $control_step['ab-test-archived-variations']; |
| 168 |
|
| 169 |
foreach ( $archived_variation as $variations => $variation ) { |
| 170 |
|
| 171 |
if ( $variation['id'] === $step_id ) { |
| 172 |
unset( $archived_variation[ $variations ] ); |
| 173 |
|
| 174 |
$flow_steps[ $step ]['ab-test-archived-variations'] = array_values( $archived_variation ); |
| 175 |
break; |
| 176 |
} |
| 177 |
} |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
update_post_meta( $flow_id, 'wcf-steps', $flow_steps ); |
| 184 |
|
| 185 |
$result = array( |
| 186 |
'status' => true, |
| 187 |
/* translators: %s flow id */ |
| 188 |
'text' => sprintf( __( 'Step deleted - %s', 'cartflows' ), $step_id ), |
| 189 |
); |
| 190 |
|
| 191 |
wp_send_json( $result ); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* Save Ab test settings. |
| 197 |
* |
| 198 |
* @since 1.0.0 |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function save_ab_test_setting() { |
| 202 |
|
| 203 |
$response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) ); |
| 204 |
|
| 205 |
if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) { |
| 206 |
wp_send_json_error( $response_data ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Nonce verification |
| 211 |
*/ |
| 212 |
if ( ! check_ajax_referer( 'cartflows_save_ab_test_setting', 'security', false ) ) { |
| 213 |
$response_data = array( 'messsage' => $this->get_error_msg( 'nonce' ) ); |
| 214 |
wp_send_json_error( $response_data ); |
| 215 |
} |
| 216 |
|
| 217 |
$step_id = isset( $_POST['step_id'] ) ? intval( $_POST['step_id'] ) : 0; |
| 218 |
$flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : 0; |
| 219 |
|
| 220 |
$result = array( |
| 221 |
'status' => false, |
| 222 |
/* translators: %s step id */ |
| 223 |
'text' => sprintf( __( 'Can\'t create a variation for this step - %s', 'cartflows' ), $step_id ), |
| 224 |
); |
| 225 |
|
| 226 |
if ( ! $step_id ) { |
| 227 |
wp_send_json( $result ); |
| 228 |
} |
| 229 |
|
| 230 |
if ( CARTFLOWS_FLOW_POST_TYPE !== get_post_type( $flow_id ) ) { |
| 231 |
wp_send_json( $result ); |
| 232 |
} |
| 233 |
|
| 234 |
if ( isset( $_POST['wcf_ab_settings'] ) ) { |
| 235 |
// We are sanitizing form data using our function. Hence ignoring sanitization rule. |
| 236 |
$form_data = wcf_clean( wp_unslash( $_POST['wcf_ab_settings'] ) ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 237 |
|
| 238 |
$traffic = isset( $form_data['traffic'] ) ? $form_data['traffic'] : array(); |
| 239 |
|
| 240 |
$flow_steps = get_post_meta( $flow_id, 'wcf-steps', true ); |
| 241 |
|
| 242 |
if ( is_array( $flow_steps ) ) { |
| 243 |
|
| 244 |
foreach ( $flow_steps as $index => $step_data ) { |
| 245 |
|
| 246 |
if ( $step_data['id'] === $step_id ) { |
| 247 |
|
| 248 |
if ( isset( $step_data['ab-test-variations'] ) ) { |
| 249 |
|
| 250 |
$all_variations = $step_data['ab-test-variations']; |
| 251 |
|
| 252 |
foreach ( $all_variations as $var_key => $var_data ) { |
| 253 |
|
| 254 |
$var_id = intval( $var_data['id'] ); |
| 255 |
|
| 256 |
if ( isset( $traffic[ $var_id ] ) && isset( $traffic[ $var_id ]['value'] ) ) { |
| 257 |
$all_variations[ $var_key ]['traffic'] = intval( $traffic[ $var_id ]['value'] ); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
$flow_steps[ $index ]['ab-test-variations'] = $all_variations; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
update_post_meta( $flow_id, 'wcf-steps', $flow_steps ); |
| 267 |
|
| 268 |
do_action( |
| 269 |
'cartflows_save_ab_test_setting', |
| 270 |
array( |
| 271 |
'flow_id' => $flow_id, |
| 272 |
'step_id' => $step_id, |
| 273 |
) |
| 274 |
); |
| 275 |
|
| 276 |
$result = array( |
| 277 |
'status' => true, |
| 278 |
/* translators: %s step id */ |
| 279 |
'text' => sprintf( __( 'A/B test settings updated for this step - %s', 'cartflows' ), $step_id ), |
| 280 |
); |
| 281 |
|
| 282 |
} |
| 283 |
wp_send_json( $result ); |
| 284 |
} |
| 285 |
} |
| 286 |
|