PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.0
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / modules / gutenberg / classes / class-cartflows-gutenberg-editor.php

class-cartflows-gutenberg-editor.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.0, at modules/gutenberg/classes/class-cartflows-gutenberg-editor.php

244 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Gutenburg Editor Compatibility.
4 *
5 * @package CartFlows
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12 /**
13 * Checkout Markup
14 *
15 * @since 1.6.15
16 */
17 class Cartflows_Gutenberg_Editor {
18
19 /**
20 * Member Variable
21 *
22 * @var object instance
23 */
24 private static $instance;
25
26 /**
27 * Initiator
28 */
29 public static function get_instance() {
30
31 if ( ! isset( self::$instance ) ) {
32 self::$instance = new self();
33 }
34
35 return self::$instance;
36 }
37
38 /**
39 * Constructor
40 */
41 public function __construct() {
42
43 add_action( 'admin_init', array( $this, 'gutenberg_editor_compatibility' ) );
44
45 add_action( 'save_post_cartflows_step', array( $this, 'update_required_step_meta_data' ), 10, 3 );
46
47 add_action( 'admin_init', array( $this, 'add_gcp_colors_to_block_default_color_pallet' ), 12 );
48 }
49
50 /**
51 * Function to add the CartFlows flow's Global Color Pallet to block editor's default color pallet.
52 * This will display the flow's GCP colors in the color component of Gutenberg.
53 *
54 * @since 2.0.0
55 * @return void
56 */
57 public function add_gcp_colors_to_block_default_color_pallet() {
58
59 $post_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
60 $post_type = get_post_type( $post_id );
61 $flow_id = wcf()->utils->get_flow_id_from_step_id( $post_id );
62
63 if ( ! empty( $flow_id ) && Cartflows_Helper::is_gcp_styling_enabled( (int) $flow_id ) && CARTFLOWS_STEP_POST_TYPE === $post_type ) {
64
65 // Try to get the current theme default color palette.
66 $default_color_pallet = current( (array) get_theme_support( 'editor-color-palette' ) );
67
68 // Get default core color palette from wp-includes/theme.json.
69 if ( false === $default_color_pallet && class_exists( 'WP_Theme_JSON_Resolver' ) ) {
70 $settings = WP_Theme_JSON_Resolver::get_core_data()->get_settings();
71
72 if ( isset( $settings['color']['palette']['default'] ) ) {
73 $default_color_pallet = $settings['color']['palette']['default'];
74 }
75 }
76
77 $new_color_palette = Cartflows_Helper::generate_css_var_array( $flow_id );
78
79 // Merge the old and new color palettes.
80 if ( ! empty( $default_color_pallet ) ) {
81 $new_color_palette = array_merge( $default_color_pallet, $new_color_palette );
82 }
83 // Apply the color palette containing the original colors and the new colors.
84 add_theme_support( 'editor-color-palette', $new_color_palette );
85
86 }
87
88 }
89
90 /**
91 * Before checkout shortcode actions.
92 *
93 * @param int $post_ID step id.
94 * @param object $post post data.
95 * @param bool $update is updated.
96 */
97 public function update_required_step_meta_data( $post_ID, $post, $update ) {
98
99 $step_type = get_post_meta( $post_ID, 'wcf-step-type', true );
100 $block_data = '';
101
102 switch ( $step_type ) {
103
104 case 'checkout':
105 $post_content = parse_blocks( $post->post_content );
106 $block_data = $this->gutenberg_find_block_recursive( $post_content, 'wcfb/checkout-form' );
107 $meta_keys = array(
108 'layout' => 'wcf-checkout-layout',
109 );
110 break;
111
112 case 'optin':
113 break;
114
115 default:
116 }
117
118 if ( is_array( $block_data ) && ! empty( $block_data ) && isset( $block_data['attrs'] ) ) {
119
120 foreach ( $meta_keys as $key => $meta_key ) {
121
122 if ( isset( $block_data['attrs'][ $key ] ) ) {
123 update_post_meta( $post_ID, $meta_key, $block_data['attrs'][ $key ] );
124 }
125 }
126 }
127 }
128
129 /**
130 * Get the block data.
131 *
132 * @param array $elements elements data.
133 * @param string $slug widget name.
134 */
135 public function gutenberg_find_block_recursive( $elements, $slug = 'wcfb/checkout-form' ) {
136 foreach ( $elements as $element ) {
137 if ( $slug === $element['blockName'] ) {
138 return $element;
139 }
140 if ( ! empty( $element['innerBlocks'] ) ) {
141 $element = $this->gutenberg_find_block_recursive( $element['innerBlocks'] );
142 if ( $element ) {
143 return $element;
144 }
145 }
146 }
147 return false;
148 }
149
150 /**
151 * Gutenburg editor compatibility.
152 */
153 public function gutenberg_editor_compatibility() {
154 // Calling this function on 'admin_init'. No nonce required.
155 //phpcs:disable WordPress.Security.NonceVerification
156 if ( is_admin() && isset( $_REQUEST['action'] ) ) {
157
158 $current_post_id = false;
159
160 if ( 'edit' === $_REQUEST['action'] && isset( $_GET['post'] ) ) {
161 $current_post_id = intval( $_GET['post'] );
162 } elseif ( isset( $_REQUEST['cartflows_gb'] ) && isset( $_POST['id'] ) ) {
163 $current_post_id = intval( $_POST['id'] );
164 }
165 //phpcs:enable WordPress.Security.NonceVerification
166 if ( $current_post_id ) {
167
168 $current_post_type = get_post_type( $current_post_id );
169
170 if ( wcf()->utils->is_step_post_type( $current_post_type ) ) {
171
172 if ( wcf()->is_woo_active ) {
173
174 $this->maybe_init_cart();
175
176 /* Load woo templates from plugin */
177 $cf_frontend = Cartflows_Frontend::get_instance();
178 add_filter( 'woocommerce_locate_template', array( $cf_frontend, 'override_woo_template' ), 20, 3 );
179
180 add_action( 'cartflows_gutenberg_before_checkout_shortcode', array( $this, 'before_gb_checkout_shortcode_actions' ) );
181
182 add_action( 'cartflows_gutenberg_before_optin_shortcode', array( $this, 'before_gb_optin_shortcode_actions' ) );
183
184 }
185
186 do_action( 'cartflows_gutenberg_editor_compatibility', $current_post_id );
187 }
188 }
189 }
190 }
191
192 /**
193 * Before checkout shortcode actions.
194 */
195 public function maybe_init_cart() {
196
197 wc()->frontend_includes();
198
199 $has_cart = is_a( WC()->cart, 'WC_Cart' );
200
201 if ( ! $has_cart ) {
202 $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
203 WC()->session = new $session_class();
204 WC()->session->init();
205 WC()->cart = new \WC_Cart();
206 WC()->customer = new \WC_Customer( get_current_user_id(), true );
207
208 }
209
210 /* For preview */
211 add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
212 }
213
214 /**
215 * Before checkout shortcode actions.
216 *
217 * @param int $checkout_id checkout id.
218 */
219 public function before_gb_checkout_shortcode_actions( $checkout_id ) {
220
221 // Added to modify the fields labels and placeholders to display it in the preview mode.
222 Cartflows_Checkout_Fields::get_instance()->checkout_field_actions();
223
224 Cartflows_Checkout_Markup::get_instance()->before_checkout_shortcode_actions();
225
226 do_action( 'cartflows_checkout_before_shortcode', $checkout_id );
227 }
228
229 /**
230 * Before optin shortcode actions.
231 *
232 * @param int $checkout_id checkout id.
233 */
234 public function before_gb_optin_shortcode_actions( $checkout_id ) {
235
236 do_action( 'cartflows_optin_before_shortcode', $checkout_id );
237 }
238 }
239
240 /**
241 * Kicking this off by calling 'get_instance()' method
242 */
243 Cartflows_Gutenberg_Editor::get_instance();
244