← All changes
|
admin/modules/template-library/class-manager.php
+144
-27
2.8.16
→
3.13.3
View file →
| @@ -1,13 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Template library manager |
| 4 | - * | |
| 4 | + * | |
| 5 | 5 | * @package |
| 6 | 6 | */ |
| 7 | 7 | namespace WPFunnels\TemplateLibrary; |
| 8 | 8 | |
| 9 | 9 | use WPFunnels\Wpfnl; |
| 10 | +use WPFunnels\Wpfnl_functions; | |
| 10 | 11 | |
| 11 | 12 | class Manager |
| 12 | 13 | { |
| 13 | 14 | |
| @@ -57,9 +58,9 @@ | ||
| 57 | 58 | public function init_ajax() |
| 58 | 59 | { |
| 59 | 60 | $this->validations = [ |
| 60 | 61 | 'logged_in' => true, |
| 61 | - 'user_can' => 'manage_options', | |
| 62 | + 'user_can' => 'wpf_manage_funnels', | |
| 62 | 63 | ]; |
| 63 | 64 | wp_ajax_helper()->handle('wpfunnel-get-templates-data') |
| 64 | 65 | ->with_callback([$this, 'get_funnel_templates']) |
| 65 | 66 | ->with_validation($this->validations); |
| @@ -100,20 +101,14 @@ | ||
| 100 | 101 | add_action('admin_footer', [$this, 'add_default_view']); |
| 101 | 102 | add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); |
| 102 | 103 | } |
| 103 | 104 | |
| 105 | + | |
| 104 | 106 | public function enqueue_scripts($hook) |
| 105 | 107 | { |
| 106 | - if (in_array($hook, ['toplevel_page_wp_funnels'])) { | |
| 107 | - $funnel_id = 0; | |
| 108 | - if (isset($_GET['page'])) { | |
| 109 | - if ($_GET['page'] === 'edit_funnel') { | |
| 110 | - if (isset($_GET['id'])) { | |
| 111 | - $funnel_id = sanitize_text_field($_GET['id']); | |
| 112 | - } | |
| 113 | - } | |
| 114 | - } | |
| 115 | - wp_enqueue_script('funnel-template-library', WPFNL_URL . 'admin/assets/dist/js/template-library.min.js', ['jquery', 'wp-util'], '1.0.0', true); | |
| 108 | + if (in_array($hook, ['wpfunnels_page_store_checkout'], true)) { | |
| 109 | + wp_enqueue_script('funnel-template-library', WPFNL_URL . 'admin/assets/dist/js/template-library.min.js', ['jquery', 'wp-util'], WPFNL_VERSION, true); | |
| 110 | + Wpfnl_functions::enqueue_bundle_style( 'template-library', 'funnel-template-library-bundle' ); | |
| 116 | 111 | } |
| 117 | 112 | } |
| 118 | 113 | |
| 119 | 114 | |
| @@ -137,12 +132,12 @@ | ||
| 137 | 132 | |
| 138 | 133 | |
| 139 | 134 | /** |
| 140 | 135 | * Register sources |
| 141 | - * | |
| 136 | + * | |
| 142 | 137 | * @param $source_class |
| 143 | 138 | * @param array $args |
| 144 | - * | |
| 139 | + * | |
| 145 | 140 | * @return bool|\WP_Error |
| 146 | 141 | * @since 1.0.0 |
| 147 | 142 | */ |
| 148 | 143 | public function register_source($source_class, $args = []) |
| @@ -176,9 +171,9 @@ | ||
| 176 | 171 | /** |
| 177 | 172 | * Get template source by source name. |
| 178 | 173 | * |
| 179 | 174 | * @param $id |
| 180 | - * | |
| 175 | + * | |
| 181 | 176 | * @return false|Wpfnl_Source_Base |
| 182 | 177 | * @since 1.0.0 |
| 183 | 178 | */ |
| 184 | 179 | public function get_source($id) |
| @@ -194,14 +189,20 @@ | ||
| 194 | 189 | /** |
| 195 | 190 | * Get step templates |
| 196 | 191 | * |
| 197 | 192 | * @param $payload |
| 198 | - * | |
| 193 | + * | |
| 199 | 194 | * @return array |
| 200 | 195 | */ |
| 201 | 196 | public function get_step_templates($payload) |
| 202 | 197 | { |
| 203 | 198 | $template_data = $this->get_source('remote')->get_funnels(); |
| 199 | + | |
| 200 | + // Filter for Store Checkout - only show checkout and thankyou templates | |
| 201 | + if (isset($_GET['page']) && $_GET['page'] === 'store_checkout') { | |
| 202 | + $template_data = $this->filter_store_checkout_templates($template_data); | |
| 203 | + } | |
| 204 | + | |
| 204 | 205 | return [ |
| 205 | 206 | 'success' => true, |
| 206 | 207 | 'data' => $template_data, |
| 207 | 208 | ]; |
| @@ -215,8 +216,14 @@ | ||
| 215 | 216 | */ |
| 216 | 217 | public function get_funnel_templates() |
| 217 | 218 | { |
| 218 | 219 | $template_data = $this->get_source('remote')->get_funnels(); |
| 220 | + | |
| 221 | + // Filter for Store Checkout - only show checkout and thankyou templates | |
| 222 | + if (isset($_GET['page']) && $_GET['page'] === 'store_checkout') { | |
| 223 | + $template_data = $this->filter_store_checkout_templates($template_data); | |
| 224 | + } | |
| 225 | + | |
| 219 | 226 | return [ |
| 220 | 227 | 'success' => true, |
| 221 | 228 | 'data' => $template_data, |
| 222 | 229 | ]; |
| @@ -243,9 +250,9 @@ | ||
| 243 | 250 | * Add view |
| 244 | 251 | * |
| 245 | 252 | * @param $view |
| 246 | 253 | * @param string $type |
| 247 | - * | |
| 254 | + * | |
| 248 | 255 | * @since 1.0.0 |
| 249 | 256 | */ |
| 250 | 257 | public function add_view($view, $type = 'path') |
| 251 | 258 | { |
| @@ -259,9 +266,9 @@ | ||
| 259 | 266 | } |
| 260 | 267 | |
| 261 | 268 | /** |
| 262 | 269 | * Print template |
| 263 | - * | |
| 270 | + * | |
| 264 | 271 | * @since 1.0.0 |
| 265 | 272 | */ |
| 266 | 273 | public function print_template() |
| 267 | 274 | { |
| @@ -273,16 +280,34 @@ | ||
| 273 | 280 | * Import templates from |
| 274 | 281 | * Remote server |
| 275 | 282 | * |
| 276 | 283 | * @param $payload |
| 277 | - * | |
| 284 | + * | |
| 278 | 285 | * @return array |
| 279 | 286 | * @since 1.0.0 |
| 280 | 287 | */ |
| 281 | 288 | public function import_funnel($payload) |
| 282 | 289 | { |
| 290 | + // Check if Store Checkout flag is sent from frontend | |
| 291 | + if (isset($payload['is_store_checkout']) && $payload['is_store_checkout']) { | |
| 292 | + // Flag is already in payload | |
| 293 | + } elseif (isset($_GET['page']) && $_GET['page'] === 'store_checkout') { | |
| 294 | + // Fallback: check GET parameter | |
| 295 | + $payload['is_store_checkout'] = true; | |
| 296 | + } | |
| 283 | 297 | $source = $this->get_source($payload['source']); |
| 284 | - return $source->import_funnel($payload); | |
| 298 | + $result = $source->import_funnel($payload); | |
| 299 | + | |
| 300 | + if ( ! empty( $result['success'] ) && ! empty( $result['funnelID'] ) ) { | |
| 301 | + do_action( | |
| 302 | + 'wpfunnels_template_imported', | |
| 303 | + absint( $result['funnelID'] ), | |
| 304 | + 0, | |
| 305 | + sanitize_text_field( $payload['source'] ?? '' ) | |
| 306 | + ); | |
| 307 | + } | |
| 308 | + | |
| 309 | + return $result; | |
| 285 | 310 | } |
| 286 | 311 | |
| 287 | 312 | /** |
| 288 | 313 | * Import wp funnel steps |
| @@ -288,9 +313,9 @@ | ||
| 288 | 313 | * Import wp funnel steps |
| 289 | 314 | * From remote servers |
| 290 | 315 | * |
| 291 | 316 | * @param $payload |
| 292 | - * | |
| 317 | + * | |
| 293 | 318 | * @return array |
| 294 | 319 | * @since 1.0.0 |
| 295 | 320 | */ |
| 296 | 321 | public function import_variation_step($payload) |
| @@ -304,14 +329,25 @@ | ||
| 304 | 329 | * After funnel import |
| 305 | 330 | * Redirect to new funnel edit url |
| 306 | 331 | * |
| 307 | 332 | * @param $payload |
| 308 | - * | |
| 333 | + * | |
| 309 | 334 | * @return array |
| 310 | 335 | * @since 1.0.0 |
| 311 | 336 | */ |
| 312 | 337 | public function after_funnel_creation($payload) |
| 313 | 338 | { |
| 339 | + // Check if Store Checkout flag is sent from frontend | |
| 340 | + if (isset($payload['is_store_checkout']) && $payload['is_store_checkout']) { | |
| 341 | + // Flag is already in payload | |
| 342 | + } elseif (isset($_GET['page']) && $_GET['page'] === 'store_checkout') { | |
| 343 | + // Fallback: check GET parameter | |
| 344 | + $payload['is_store_checkout'] = true; | |
| 345 | + } elseif (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'page=store_checkout') !== false) { | |
| 346 | + // Fallback: check referer | |
| 347 | + $payload['is_store_checkout'] = true; | |
| 348 | + } | |
| 349 | + | |
| 314 | 350 | $source = $this->get_source($payload['source']); |
| 315 | 351 | return $source->after_funnel_creation($payload); |
| 316 | 352 | } |
| 317 | 353 | |
| @@ -319,9 +355,9 @@ | ||
| 319 | 355 | * After funnel import |
| 320 | 356 | * Redirect to new funnel edit url |
| 321 | 357 | * |
| 322 | 358 | * @param $payload |
| 323 | - * | |
| 359 | + * | |
| 324 | 360 | * @return array |
| 325 | 361 | * @since 1.0.0 |
| 326 | 362 | */ |
| 327 | 363 | public function after_step_creation($payload) |
| @@ -335,9 +371,9 @@ | ||
| 335 | 371 | $node_id = 0; |
| 336 | 372 | $step_edit_link = base64_encode(get_edit_post_link($step_id)); |
| 337 | 373 | $step_view_link = base64_encode(get_post_permalink($step_id)); |
| 338 | 374 | |
| 339 | - $funnel_json = get_post_meta($funnel_id, 'funnel_data', true); | |
| 375 | + $funnel_json = get_post_meta($funnel_id, '_funnel_data', true); | |
| 340 | 376 | $funnel_data = $funnel_json; |
| 341 | 377 | |
| 342 | 378 | $node_data = $funnel_data['drawflow']['Home']['data']; |
| 343 | 379 | |
| @@ -374,9 +410,9 @@ | ||
| 374 | 410 | $identifier = json_decode($identifier_json, true); |
| 375 | 411 | $identifier[$node_id] = $step_id; |
| 376 | 412 | $final_identifier = json_encode($identifier); |
| 377 | 413 | |
| 378 | - update_post_meta($funnel_id, 'funnel_data', $funnel_data); | |
| 414 | + update_post_meta($funnel_id, '_funnel_data', $funnel_data); | |
| 379 | 415 | update_post_meta($funnel_id, 'funnel_identifier', $final_identifier); |
| 380 | 416 | |
| 381 | 417 | $source = $this->get_source($payload['source']); |
| 382 | 418 | return $source->after_step_creation($payload); |
| @@ -423,16 +459,97 @@ | ||
| 423 | 459 | } |
| 424 | 460 | |
| 425 | 461 | |
| 426 | 462 | /** |
| 463 | + * Filter templates for Store Checkout - only checkout and thankyou | |
| 464 | + * | |
| 465 | + * @param array $template_data | |
| 466 | + * @return array | |
| 467 | + * @since 3.5.0 | |
| 468 | + */ | |
| 469 | + private function filter_store_checkout_templates($template_data) | |
| 470 | + { | |
| 471 | + if (empty($template_data) || !isset($template_data['steps'])) { | |
| 472 | + return $template_data; | |
| 473 | + } | |
| 474 | + | |
| 475 | + // Filter steps to only include one checkout and one thankyou | |
| 476 | + $filtered_steps = []; | |
| 477 | + $found_checkout = false; | |
| 478 | + $found_thankyou = false; | |
| 479 | + | |
| 480 | + foreach ($template_data['steps'] as $step) { | |
| 481 | + if (isset($step['step_type'])) { | |
| 482 | + if ($step['step_type'] === 'checkout' && !$found_checkout) { | |
| 483 | + $filtered_steps[] = $step; | |
| 484 | + $found_checkout = true; | |
| 485 | + } elseif ($step['step_type'] === 'thankyou' && !$found_thankyou) { | |
| 486 | + $filtered_steps[] = $step; | |
| 487 | + $found_thankyou = true; | |
| 488 | + } | |
| 489 | + } | |
| 490 | + } | |
| 491 | + | |
| 492 | + // Filter templates to only include those with checkout and thankyou steps | |
| 493 | + $filtered_templates = []; | |
| 494 | + if (isset($template_data['templates'])) { | |
| 495 | + foreach ($template_data['templates'] as $template) { | |
| 496 | + $has_checkout = false; | |
| 497 | + $has_thankyou = false; | |
| 498 | + | |
| 499 | + if (isset($template['steps'])) { | |
| 500 | + foreach ($template['steps'] as $step) { | |
| 501 | + if (isset($step['step_type'])) { | |
| 502 | + if ($step['step_type'] === 'checkout') { | |
| 503 | + $has_checkout = true; | |
| 504 | + } elseif ($step['step_type'] === 'thankyou') { | |
| 505 | + $has_thankyou = true; | |
| 506 | + } | |
| 507 | + } | |
| 508 | + } | |
| 509 | + } | |
| 510 | + | |
| 511 | + // Only include templates that have both checkout and thankyou | |
| 512 | + if ($has_checkout && $has_thankyou) { | |
| 513 | + // Filter the template's steps to only include one checkout and one thankyou | |
| 514 | + $filtered_template_steps = []; | |
| 515 | + $found_checkout = false; | |
| 516 | + $found_thankyou = false; | |
| 517 | + | |
| 518 | + foreach ($template['steps'] as $step) { | |
| 519 | + if (isset($step['step_type'])) { | |
| 520 | + if ($step['step_type'] === 'checkout' && !$found_checkout) { | |
| 521 | + $filtered_template_steps[] = $step; | |
| 522 | + $found_checkout = true; | |
| 523 | + } elseif ($step['step_type'] === 'thankyou' && !$found_thankyou) { | |
| 524 | + $filtered_template_steps[] = $step; | |
| 525 | + $found_thankyou = true; | |
| 526 | + } | |
| 527 | + } | |
| 528 | + } | |
| 529 | + | |
| 530 | + $template['steps'] = $filtered_template_steps; | |
| 531 | + $filtered_templates[] = $template; | |
| 532 | + } | |
| 533 | + } | |
| 534 | + } | |
| 535 | + | |
| 536 | + $template_data['steps'] = $filtered_steps; | |
| 537 | + $template_data['templates'] = $filtered_templates; | |
| 538 | + | |
| 539 | + return $template_data; | |
| 540 | + } | |
| 541 | + | |
| 542 | + | |
| 543 | + /** | |
| 427 | 544 | * Activate plugin with ajax call |
| 428 | 545 | * |
| 429 | 546 | * @param $payload |
| 430 | - * | |
| 547 | + * | |
| 431 | 548 | * @since 2.0.0 |
| 432 | 549 | */ |
| 433 | 550 | public function activate_plugin( $payload ) { |
| 434 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 551 | + if ( ! current_user_can( 'wpf_manage_funnels' ) ) { | |
| 435 | 552 | wp_send_json_error( array( |
| 436 | 553 | 'message' => __('Sorry you are not allowed to do this operation', 'wpfnl'), |
| 437 | 554 | ) ); |
| 438 | 555 | } |