| 1 |
<?php |
| 2 |
/** |
| 3 |
* Step controller |
| 4 |
* |
| 5 |
* @package WPFunnels\Rest\Controllers |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Rest\Controllers; |
| 8 |
|
| 9 |
use WP_Error; |
| 10 |
use WP_REST_Request; |
| 11 |
use WP_REST_Response; |
| 12 |
use WPFunnels\Wpfnl; |
| 13 |
use WPFunnels\Wpfnl_functions; |
| 14 |
use WPFunnels\TemplateLibrary\Manager; |
| 15 |
|
| 16 |
class StepController extends Wpfnl_REST_Controller |
| 17 |
{ |
| 18 |
|
| 19 |
/** |
| 20 |
* Endpoint namespace. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $namespace = 'wpfunnels/v1'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Route base. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $rest_base = 'steps'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Check if user has valid permission |
| 35 |
* |
| 36 |
* @param $request |
| 37 |
* |
| 38 |
* @return bool|WP_Error |
| 39 |
* @since 1.0.0 |
| 40 |
*/ |
| 41 |
public function update_items_permissions_check($request) |
| 42 |
{ |
| 43 |
if (!Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'steps', 'edit' )) { |
| 44 |
return new WP_Error('wpfunnels_rest_cannot_edit', __('Sorry, you cannot edit this resource.', 'wpfnl'), ['status' => rest_authorization_required_code()]); |
| 45 |
} |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Makes sure the current user has access to READ the settings APIs. |
| 51 |
* |
| 52 |
* @param WP_REST_Request $request Full data about the request. |
| 53 |
* |
| 54 |
* @return WP_Error|boolean |
| 55 |
* @since 3.0.0 |
| 56 |
*/ |
| 57 |
public function get_items_permissions_check($request) |
| 58 |
{ |
| 59 |
if (!Wpfnl_functions::wpfnl_rest_check_manager_permissions('settings')) { |
| 60 |
return new WP_Error('wpfunnels_rest_cannot_view', __('Sorry, you cannot list resources.', 'wpfnl'), ['status' => rest_authorization_required_code()]); |
| 61 |
} |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Register rest routes |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
*/ |
| 71 |
public function register_routes() |
| 72 |
{ |
| 73 |
register_rest_route($this->namespace, '/' . $this->rest_base, array( |
| 74 |
'args' => array( |
| 75 |
'funnel_id' => array( |
| 76 |
'description' => __('Funnel ID.', 'wpfnl'), |
| 77 |
'type' => 'string', |
| 78 |
), |
| 79 |
'step_id' => array( |
| 80 |
'description' => __('Step ID.', 'wpfnl'), |
| 81 |
'type' => 'string', |
| 82 |
) |
| 83 |
), |
| 84 |
array( |
| 85 |
'methods' => \WP_REST_Server::EDITABLE, |
| 86 |
'callback' => [ |
| 87 |
$this, |
| 88 |
'update_step_meta' |
| 89 |
], |
| 90 |
'permission_callback' => [ |
| 91 |
$this, |
| 92 |
'update_items_permissions_check' |
| 93 |
] , |
| 94 |
) |
| 95 |
)); |
| 96 |
register_rest_route( |
| 97 |
$this->namespace, |
| 98 |
'/' . $this->rest_base . '/(?P<step_id>[\d\.]+)/delete-step', |
| 99 |
array( |
| 100 |
'methods' => \WP_REST_Server::DELETABLE, |
| 101 |
'callback' => array( |
| 102 |
$this, |
| 103 |
'delete_step', |
| 104 |
), |
| 105 |
'permission_callback' => array( |
| 106 |
$this, |
| 107 |
'update_items_permissions_check', |
| 108 |
), |
| 109 |
), |
| 110 |
); |
| 111 |
register_rest_route( |
| 112 |
$this->namespace, |
| 113 |
'/' . $this->rest_base . '/create-step', |
| 114 |
array( |
| 115 |
array( |
| 116 |
'methods' => \WP_REST_Server::EDITABLE, |
| 117 |
'callback' => array( |
| 118 |
$this, |
| 119 |
'create_step', |
| 120 |
), |
| 121 |
'permission_callback' => array( |
| 122 |
$this, |
| 123 |
'update_items_permissions_check', |
| 124 |
), |
| 125 |
), |
| 126 |
) |
| 127 |
); |
| 128 |
|
| 129 |
register_rest_route( |
| 130 |
$this->namespace, |
| 131 |
'/' . $this->rest_base . '/wpfunnel-import-step', |
| 132 |
array( |
| 133 |
array( |
| 134 |
'methods' => \WP_REST_Server::EDITABLE, |
| 135 |
'callback' => array( |
| 136 |
$this, |
| 137 |
'import_step', |
| 138 |
), |
| 139 |
'permission_callback' => array( |
| 140 |
$this, |
| 141 |
'update_items_permissions_check', |
| 142 |
), |
| 143 |
), |
| 144 |
) |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Responsible to create a single step of funnels |
| 150 |
* |
| 151 |
* @param WP_REST_Request $payload Data from request. |
| 152 |
* |
| 153 |
* @return array |
| 154 |
* @since 1.0.0 |
| 155 |
*/ |
| 156 |
public function create_step( $payload ) { |
| 157 |
if ( ! isset( $payload['funnel_id'] , $payload['step_type'] ) ){ |
| 158 |
return $this->prepare_wp_error_response( |
| 159 |
'rest_invalid_request', |
| 160 |
__( 'Invalid rest request.', 'wpfnl' ), |
| 161 |
array( 'status' => 400 ) |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
$funnel_id = $payload['funnel_id']; |
| 166 |
$step_type = $payload['step_type']; |
| 167 |
$step_name = isset( $payload['step_name'] ) ? $payload['step_name'] : $step_type; |
| 168 |
$funnel = Wpfnl::get_instance()->funnel_store; |
| 169 |
$step = Wpfnl::get_instance()->step_store; |
| 170 |
$step_id = $step->create_step( $funnel_id, $step_name, $step_type ); |
| 171 |
|
| 172 |
$step->set_id( $step_id ); |
| 173 |
|
| 174 |
if ( ! $step_id || is_wp_error( $step_id ) ) { |
| 175 |
return $this->prepare_wp_error_response( |
| 176 |
'rest_step_create_failed', |
| 177 |
__( 'Failed to create a step', 'wpfnl' ), |
| 178 |
array( 'status' => 400 ) |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
$funnel->set_id( $funnel_id ); |
| 183 |
$step_edit_link = get_edit_post_link( $step_id ); |
| 184 |
$step_view_link = get_post_permalink( $step_id ); |
| 185 |
|
| 186 |
/** |
| 187 |
* Fires to update _wpfnl_ab_testing_start_settings meta |
| 188 |
* |
| 189 |
* @since 2.7.0 |
| 190 |
* |
| 191 |
* @param int $step_id The ID of the order. |
| 192 |
*/ |
| 193 |
do_action( 'wpfunnels/update_ab_testing_start_settings', $step_id ); //phpcs:ignore |
| 194 |
|
| 195 |
$response = array( |
| 196 |
'success' => true, |
| 197 |
'step_id' => $step_id, |
| 198 |
'step_edit_link' => $step_edit_link, |
| 199 |
'step_view_link' => rtrim( $step_view_link, '/' ), |
| 200 |
'step_title' => $step->get_title(), |
| 201 |
'conversion' => 0, |
| 202 |
'visit' => 0, |
| 203 |
'shouldShowAnalytics' => false, |
| 204 |
'abTestingSettingsData' => Wpfnl::get_instance()->get_default_start_setting( $step_id ), |
| 205 |
); |
| 206 |
|
| 207 |
return rest_ensure_response( $response ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* Update step meta |
| 213 |
* |
| 214 |
* @param WP_REST_Request $request |
| 215 |
* |
| 216 |
* @return \WP_REST_Response |
| 217 |
* |
| 218 |
* @since 2.0.5 |
| 219 |
*/ |
| 220 |
public function update_step_meta( WP_REST_Request $request ) { |
| 221 |
|
| 222 |
$step_id = $request['step_id'] ? $request['step_id'] : ''; |
| 223 |
$funnel_id = $request['funnel_id'] ? $request['funnel_id'] : ''; |
| 224 |
$slug = $request['slug'] ? $request['slug'] : ''; |
| 225 |
$settings = $request->get_params(); |
| 226 |
|
| 227 |
if( is_plugin_active( 'wpfunnels-pro/wpfnl-pro.php' ) && Wpfnl_functions::is_pro_license_activated() ){ |
| 228 |
do_action( 'wpfunnels/before_update_step_meta', $step_id, $funnel_id, $settings ); |
| 229 |
$parent_step_id = get_post_meta( $step_id, '_parent_step_id', true ); |
| 230 |
$step_id = $parent_step_id ? $parent_step_id : $step_id; |
| 231 |
}else{ |
| 232 |
if( isset($settings['funnel_id'], $settings['step_id'], $settings['title'], $settings['slug']) ){ |
| 233 |
$steps = Wpfnl_functions::get_steps($settings['funnel_id']); |
| 234 |
foreach($steps as $key=>$step){ |
| 235 |
if($step['id'] == $settings['step_id']){ |
| 236 |
$steps[$key]['name'] = $settings['title']; |
| 237 |
} |
| 238 |
} |
| 239 |
update_post_meta( $settings['funnel_id'], '_steps_order',$steps); |
| 240 |
wp_update_post([ |
| 241 |
"ID" => $step_id, |
| 242 |
"post_title" => wp_strip_all_tags( $settings['title'] ), |
| 243 |
"post_name" => sanitize_title($settings['slug']), |
| 244 |
]); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
$response = array( |
| 249 |
'success' => true, |
| 250 |
'post_title' => htmlspecialchars_decode(get_the_title($step_id)), |
| 251 |
'permalink' => rtrim( get_the_permalink($step_id), '/' ), |
| 252 |
'slug' => sanitize_title($slug), |
| 253 |
); |
| 254 |
return $this->prepare_item_for_response( $response, $request ); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Common function to update step meta |
| 260 |
* |
| 261 |
* @param WP_REST_Request $request |
| 262 |
* |
| 263 |
* @return \WP_REST_Response |
| 264 |
* |
| 265 |
* @since 2.7.10 |
| 266 |
*/ |
| 267 |
public static function update_step_meta_on_funnel_name_change( $funnel_id, $step_id, $settings ){ |
| 268 |
if( is_plugin_active( 'wpfunnels-pro/wpfnl-pro.php' ) && Wpfnl_functions::is_pro_license_activated() ){ |
| 269 |
/** |
| 270 |
* Fires to update step slug on ab testing variation on funnel name change |
| 271 |
* |
| 272 |
* @since 2.7.0 |
| 273 |
* |
| 274 |
* @param int $step_id The ID of the step. |
| 275 |
* @param int $funnel_id The ID of the funnel to which the step belongs. |
| 276 |
* @param array $settings An array of settings containing the title and slug for the step. |
| 277 |
*/ |
| 278 |
do_action( 'wpfunnels/before_update_step_meta_on_funnel_name_change', $step_id, $funnel_id, $settings ); |
| 279 |
}else{ |
| 280 |
if( isset($settings['funnel_id'], $settings['step_id'], $settings['title'], $settings['slug']) ){ |
| 281 |
$steps = Wpfnl_functions::get_steps($settings['funnel_id']); |
| 282 |
if ( !empty( $steps ) ){ |
| 283 |
foreach($steps as $key=>$step){ |
| 284 |
if($step['id'] == $settings['step_id']){ |
| 285 |
$steps[$key]['name'] = $settings['title']; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
update_post_meta( $settings['funnel_id'], '_steps_order', $steps); |
| 290 |
wp_update_post([ |
| 291 |
"ID" => $step_id, |
| 292 |
"post_title" => wp_strip_all_tags( $settings['title'] ), |
| 293 |
"post_name" => sanitize_title($settings['slug']), |
| 294 |
]); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
/** |
| 301 |
* Delete step and all its data |
| 302 |
* |
| 303 |
* @param WP_REST_Request $payload Data from request. |
| 304 |
* |
| 305 |
* @return array |
| 306 |
* |
| 307 |
* @since 2.7.9 |
| 308 |
*/ |
| 309 |
public function delete_step( $payload ) { |
| 310 |
if ( ! isset ( $payload['step_id'] )){ |
| 311 |
return $this->prepare_wp_error_response( |
| 312 |
'rest_invalid_stepID', |
| 313 |
__( 'Invalid Step ID.', 'wpfnl' ), |
| 314 |
array( 'status' => 404 ) |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
$step_id = sanitize_text_field( $payload['step_id'] ); |
| 319 |
$step_type = get_post_meta( $step_id, '_step_type', true ); |
| 320 |
|
| 321 |
/** |
| 322 |
* Delete the automation data linked with the step |
| 323 |
* |
| 324 |
* @since 2.7.0 |
| 325 |
* |
| 326 |
* @param int $step_id Step ID. |
| 327 |
*/ |
| 328 |
do_action( 'wpfunnels/before_delete_step', $step_id ); //phpcs:ignore |
| 329 |
|
| 330 |
$response = $this->prepare_wp_error_response( |
| 331 |
'rest_invalid_stepID', |
| 332 |
__( 'Invalid Step ID.', 'wpfnl' ), |
| 333 |
array( 'status' => 404 ) |
| 334 |
); |
| 335 |
|
| 336 |
if ( $step_type ) { |
| 337 |
$response = $this->delete_regular_step($step_id); |
| 338 |
} else if ( $step_id ){ // For conditional step. |
| 339 |
$response = $this->conditional_step_response($step_id); |
| 340 |
} |
| 341 |
return $response; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Delete a regular step and all its data. |
| 346 |
* |
| 347 |
* @param int $step_id The ID of the regular step to be deleted. |
| 348 |
* |
| 349 |
* @return array The response containing the success status and message. |
| 350 |
* |
| 351 |
* @since 2.7.9 |
| 352 |
*/ |
| 353 |
private function delete_regular_step( $step_id ) { |
| 354 |
if ( !$step_id ){ |
| 355 |
return $this->prepare_wp_error_response( |
| 356 |
'rest_invalid_stepID', |
| 357 |
__( 'Invalid Step ID.', 'wpfnl' ), |
| 358 |
array( 'status' => 404 ) |
| 359 |
); |
| 360 |
} |
| 361 |
|
| 362 |
$step = new Wpfnl::$instance->step_store(); |
| 363 |
$step->read( $step_id ); |
| 364 |
|
| 365 |
$funnel_id = $step->get_funnel_id(); |
| 366 |
$delete_result = $step->delete( $step_id ); |
| 367 |
$funnel = Wpfnl::$instance->funnel_store; |
| 368 |
$funnel->read($funnel_id); |
| 369 |
|
| 370 |
if ( $delete_result ) { |
| 371 |
return $this->prepare_wp_success_response( 'Step deleted successfully', 200 ); |
| 372 |
} |
| 373 |
|
| 374 |
$response = $this->prepare_wp_error_response( |
| 375 |
'rest_delete_failure', |
| 376 |
__( 'Failed to delete step.', 'wpfnl' ), |
| 377 |
array( 'status' => 500 ) |
| 378 |
); |
| 379 |
|
| 380 |
return $response ; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Response for deleting a conditional step. |
| 385 |
* |
| 386 |
* @param int $step_id The ID of the conditional step to be deleted. |
| 387 |
* |
| 388 |
* @return array The response indicating the success of the deletion. |
| 389 |
* |
| 390 |
* @since 2.7.9 |
| 391 |
*/ |
| 392 |
private function conditional_step_response( $step_id ) { |
| 393 |
if ( !$step_id ){ |
| 394 |
return $this->prepare_wp_error_response( |
| 395 |
'rest_invalid_stepID', |
| 396 |
__( 'Invalid Step ID.', 'wpfnl' ), |
| 397 |
array( 'status' => 404 ) |
| 398 |
); |
| 399 |
} |
| 400 |
|
| 401 |
return $this->prepare_wp_success_response( 'Conditional step deleted successfully',200 ); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
/** |
| 406 |
* Prepare a single setting object for response. |
| 407 |
* |
| 408 |
* @param object $item Setting object. |
| 409 |
* @param WP_REST_Request $request Request object. |
| 410 |
* |
| 411 |
* @return \WP_REST_Response $response Response data. |
| 412 |
* @since 1.0.0 |
| 413 |
*/ |
| 414 |
public function prepare_item_for_response($item, $request) |
| 415 |
{ |
| 416 |
$data = $this->add_additional_fields_to_object($item, $request); |
| 417 |
return rest_ensure_response( $data ); |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Import wp funnel steps from remote servers. |
| 422 |
* |
| 423 |
* @param array $payload |
| 424 |
* |
| 425 |
* @return array |
| 426 |
* @since 1.0.0 |
| 427 |
*/ |
| 428 |
public function import_step( $payload ) { |
| 429 |
$manager_class = new Manager(); |
| 430 |
$source = $manager_class->get_source(!empty( $payload['source'] ) ? $payload['source'] : 'remote' ); |
| 431 |
return $source->import_step($payload); |
| 432 |
} |
| 433 |
|
| 434 |
} |
| 435 |
|