| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gutenberg block |
| 4 |
* |
| 5 |
* @package WPFunnels\Widgets\Gutenberg |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Widgets\Gutenberg; |
| 8 |
|
| 9 |
use WPFunnels\Meta\Wpfnl_Default_Meta; |
| 10 |
use WPFunnels\Traits\SingletonTrait; |
| 11 |
use WPFunnels\Wpfnl; |
| 12 |
use WPFunnels\Wpfnl_functions; |
| 13 |
|
| 14 |
class Wpfnl_Gutenberg_Editor { |
| 15 |
|
| 16 |
use SingletonTrait; |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->gb_compatibility(); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
private function gb_compatibility() { |
| 24 |
add_action( 'admin_init', array( $this, 'editor_compatibility' ) ); |
| 25 |
add_action( 'wpfunnels/before_gb_checkout_form_ajax', array( $this, 'load_gb_cf_ajax_action' ), 10, 2 ); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Editor compatibility action for gutenberg |
| 31 |
* |
| 32 |
* @since 2.0.3 |
| 33 |
*/ |
| 34 |
public function editor_compatibility() { |
| 35 |
|
| 36 |
if( Wpfnl_functions::is_step_edit_page() && Wpfnl_functions::is_wc_active() ) { |
| 37 |
if(is_admin()) { |
| 38 |
add_filter( 'wpfunnels/show_dummy_order_details', '__return_true' ); |
| 39 |
} |
| 40 |
$this->before_checkout_actions(); |
| 41 |
$frontend = Wpfnl::get_instance()->frontend; |
| 42 |
add_filter('woocommerce_locate_template', array( $frontend, 'wpfunnels_woocommerce_locate_template' ), 20, 3); |
| 43 |
|
| 44 |
$step_id = isset($_POST['post_id']) ? intval( $_POST['post_id'] ) : 0; |
| 45 |
|
| 46 |
$show_coupon = Wpfnl::get_instance()->meta->get_checkout_meta_value( $step_id, '_wpfnl_checkout_coupon' ); |
| 47 |
if( 'no' === $show_coupon ) { |
| 48 |
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form' ); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Before checkout shortcode actions |
| 55 |
* |
| 56 |
* @since 2.0.3 |
| 57 |
*/ |
| 58 |
private function before_checkout_actions() { |
| 59 |
wc()->frontend_includes(); |
| 60 |
/* For preview */ |
| 61 |
add_filter('woocommerce_checkout_redirect_empty_cart', '__return_false'); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* This will be only triggered on checkout form block. and only in the |
| 67 |
* editor page on ajax call |
| 68 |
* |
| 69 |
* @param $checkout_id |
| 70 |
* @param $post_data |
| 71 |
*/ |
| 72 |
public function load_gb_cf_ajax_action( $checkout_id, $post_data ) { |
| 73 |
$frontend = Wpfnl::get_instance()->frontend; |
| 74 |
add_filter('woocommerce_locate_template', array( $frontend, 'wpfunnels_woocommerce_locate_template' ), 20, 3); |
| 75 |
|
| 76 |
$default_settings = Wpfnl_Default_Meta::get_default_order_bump_meta(); |
| 77 |
$ob_data = isset( $post_data['order_bump_data'] ) ? $post_data['order_bump_data'] : $default_settings; |
| 78 |
$ob_settings = $this->get_order_bump_settings_for_preview( $checkout_id, $ob_data ); |
| 79 |
|
| 80 |
update_post_meta($checkout_id, 'order-bump-settings', $ob_settings); |
| 81 |
do_action('wpfunnels/gb_render_order_bump_ajax', $checkout_id, $ob_settings ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Get order bump settings for preview |
| 87 |
* |
| 88 |
* @param $post_id |
| 89 |
* @param $post_data |
| 90 |
* |
| 91 |
* @return mixed |
| 92 |
* |
| 93 |
* @since 2.0.4 |
| 94 |
*/ |
| 95 |
private function get_order_bump_settings_for_preview( $post_id, $post_data ) { |
| 96 |
// $order_bump_settings = get_post_meta( $post_id, 'order-bump-settings', true ); |
| 97 |
$order_bump_settings = Wpfnl_functions::get_ob_settings( $post_id ); |
| 98 |
if( $order_bump_settings ) { |
| 99 |
return $this->replace_ob_settings_with_block_data( $order_bump_settings, $post_data ); |
| 100 |
} |
| 101 |
return []; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Replace ob settings with widget data |
| 107 |
* |
| 108 |
* @param $order_bump_settings |
| 109 |
* @param $post_data |
| 110 |
* |
| 111 |
* @return mixed |
| 112 |
* |
| 113 |
* @since 2.0.4 |
| 114 |
*/ |
| 115 |
private function replace_ob_settings_with_block_data( $order_bump_settings, $post_data ) { |
| 116 |
if( empty($post_data) ) { |
| 117 |
return $order_bump_settings; |
| 118 |
} |
| 119 |
|
| 120 |
if( is_array( $post_data ) ){ |
| 121 |
foreach( $post_data as $key => $data ){ |
| 122 |
$order_bump_settings[$key]['checkBoxLabel'] = isset( $data['checkBoxLabel'] ) ? $data['checkBoxLabel'] : $data['checkBoxLabel']; |
| 123 |
$order_bump_settings[$key]['highLightText'] = isset( $data['highLightText'] ) ? $data['highLightText'] : $data['highLightText']; |
| 124 |
$order_bump_settings[$key]['productDescriptionText'] = isset( $data['productDescriptionText'] ) ? $data['productDescriptionText'] : $data['productDescriptionText']; |
| 125 |
$order_bump_settings[$key]['position'] = isset( $data['position'] ) ? $data['position'] : $data['position']; |
| 126 |
$order_bump_settings[$key]['selectedStyle'] = isset( $data['selectedStyle'] ) ? $data['selectedStyle'] : $data['selectedStyle']; |
| 127 |
$order_bump_settings[$key]['productImage'] = isset( $data['productImage'] ) ? $data['productImage'] : $data['productImage']; |
| 128 |
$order_bump_settings[$key]['isEnabled'] = isset( $data['isEnabled'] ) ? $data['isEnabled'] : $data['isEnabled']; |
| 129 |
|
| 130 |
} |
| 131 |
return $order_bump_settings; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
} |
| 136 |
} |
| 137 |
|