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 / classes / class-cartflows-flow-frontend.php

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

356 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Frontend & Markup
4 *
5 * @package CartFlows
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12 /**
13 * Flow Markup
14 *
15 * @since 1.0.0
16 */
17 class Cartflows_Flow_Frontend {
18
19
20 /**
21 * Member Variable
22 *
23 * @var object instance
24 */
25 private static $instance;
26
27 /**
28 * Initiator
29 */
30 public static function get_instance() {
31 if ( ! isset( self::$instance ) ) {
32 self::$instance = new self();
33 }
34 return self::$instance;
35 }
36
37 /**
38 * Constructor
39 */
40 public function __construct() {
41
42 /* Analytics */
43 add_action( 'cartflows_wp_footer', array( $this, 'footer_markup' ) );
44 add_action( 'admin_bar_menu', array( $this, 'add_edit_flow_menu' ), 999 );
45 add_action( 'admin_bar_menu', array( $this, 'update_edit_step_menu_link' ), 999 );
46 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_gcp_color_vars' ), 23 );
47 }
48
49
50
51 /**
52 * Add a new admin bar menu to navigate to edit the flow.
53 *
54 * @param object $admin_bar The object of admin bar.
55 * @return void
56 */
57 public function add_edit_flow_menu( $admin_bar ) {
58
59 if ( wcf()->utils->is_step_post_type() ) {
60 $flow_id = wcf()->utils->get_flow_id();
61
62 if ( ! empty( $flow_id ) ) {
63
64 $path = Cartflows_Helper::get_global_setting( '_cartflows_store_checkout' ) === $flow_id ? 'store-checkout' : 'flows';
65
66 $admin_bar->add_node(
67 array(
68 'id' => 'edit-flow',
69 'title' => '<span class="ab-icon dashicons dashicons-edit"></span>' . esc_html__( 'Edit Funnel', 'cartflows' ),
70 'href' => admin_url( 'admin.php?page=cartflows&path=' . $path . '&action=wcf-edit-flow&flow_id=' . $flow_id ),
71 'meta' => array(
72 'class' => 'wcf-admin_bar-edit_flow--menu',
73 ),
74 )
75 );
76 }
77 }
78 }
79
80 /**
81 * Footer markup
82 */
83 public function footer_markup() {
84
85 if ( wcf()->utils->is_step_post_type() ) {
86 $flow_id = wcf()->utils->get_flow_id();
87
88 $default_strings = array(
89 'message' => __( 'Test mode is currently enabled to help you preview your funnel. You can turn it off anytime from the funnel\'s settings in the admin dashboard.', 'cartflows' ),
90 'link_text' => __( 'Click here to disable it.', 'cartflows' ),
91 );
92
93 $test_mode_strings = array_merge(
94 $default_strings,
95 // Make sure that if empty strings are sent, then assign the default strings.
96 array_filter(
97 apply_filters( 'cartflows_test_mode_strings', $default_strings )
98 )
99 );
100
101 ?>
102 <?php if ( $this->is_flow_testmode( $flow_id ) ) { ?>
103 <div class="wcf-preview-mode">
104 <span><?php echo esc_html( $test_mode_strings['message'] ); ?></span>
105 <?php if ( current_user_can( 'cartflows_manage_flows_steps' ) ) { ?>
106 <?php
107 $path = Cartflows_Helper::get_global_setting( '_cartflows_store_checkout' ) === $flow_id ? 'store-checkout' : 'flows';
108 $flow_edit_link = admin_url( 'admin.php?page=cartflows&path=' . $path . '&action=wcf-edit-flow&flow_id=' . $flow_id );
109 ?>
110 <a href="<?php echo esc_url( $flow_edit_link ); ?>"><?php echo esc_html( $test_mode_strings['link_text'] ); ?></a>
111 <?php } ?>
112 </div>
113 <?php } ?>
114 <?php
115 }
116 }
117
118 /**
119 * Check if flow test mode is enable.
120 *
121 * @since 1.0.0
122 * @param int $flow_id flow ID.
123 *
124 * @return boolean
125 */
126 public function is_flow_testmode( $flow_id = '' ) {
127
128 if ( ! $flow_id ) {
129 $flow_id = wcf()->utils->get_flow_id();
130 }
131
132 $test_mode = wcf()->options->get_flow_meta_value( $flow_id, 'wcf-testing' );
133
134 if ( 'yes' === $test_mode ) {
135 return true;
136 }
137
138 return false;
139 }
140
141 /**
142 * Get steps data.
143 *
144 * @since 1.0.0
145 * @param int $flow_id flow ID.
146 *
147 * @return array
148 */
149 public function get_steps( $flow_id ) {
150
151 $steps = get_post_meta( $flow_id, 'wcf-steps', true );
152
153 if ( ! is_array( $steps ) ) {
154
155 $steps = array();
156 }
157
158 return $steps;
159 }
160
161 /**
162 * Check thank you page exists.
163 *
164 * @since 1.0.0
165 * @param array $order order data.
166 *
167 * @return bool
168 */
169 public function is_thankyou_page_exists( $order ) {
170
171 $thankyou_step_exist = false;
172
173 $flow_id = wcf()->utils->get_flow_id_from_order( $order );
174
175 if ( $flow_id ) {
176
177 $step_id = wcf()->utils->get_checkout_id_from_order( $order );
178
179 // Get control step and flow steps.
180 $wcf_step_obj = wcf_get_step( $step_id );
181 $flow_steps = $wcf_step_obj->get_flow_steps();
182 $control_step = $wcf_step_obj->get_control_step();
183
184 if ( is_array( $flow_steps ) ) {
185
186 $current_step_found = false;
187
188 foreach ( $flow_steps as $index => $data ) {
189
190 if ( $current_step_found ) {
191
192 if ( 'thankyou' === $data['type'] ) {
193
194 $thankyou_step_exist = true;
195 break;
196 }
197 } elseif ( intval( $data['id'] ) === $control_step ) {
198
199
200 $current_step_found = true;
201 }
202 }
203 }
204 }
205
206 return $thankyou_step_exist;
207 }
208
209 /**
210 * Check thank you page exists.
211 *
212 * @since 1.0.0
213 * @param array $order order data.
214 *
215 * @return bool
216 */
217 public function get_thankyou_page_id( $order ) {
218
219 $thankyou_step_id = false;
220
221 $flow_id = wcf()->utils->get_flow_id_from_order( $order );
222
223 if ( $flow_id ) {
224
225 $step_id = wcf()->utils->get_checkout_id_from_order( $order );
226
227 // Get control step and flow steps.
228 $wcf_step_obj = wcf_get_step( $step_id );
229 $flow_steps = $wcf_step_obj->get_flow_steps();
230 $control_step = $wcf_step_obj->get_control_step();
231
232 if ( is_array( $flow_steps ) ) {
233
234 $current_step_found = false;
235
236 foreach ( $flow_steps as $index => $data ) {
237
238 if ( $current_step_found ) {
239
240 if ( 'thankyou' === $data['type'] ) {
241
242 $thankyou_step_id = intval( $data['id'] );
243 break;
244 }
245 } elseif ( intval( $data['id'] ) === $control_step ) {
246
247
248 $current_step_found = true;
249 }
250 }
251 }
252 }
253
254 return $thankyou_step_id;
255 }
256
257 /**
258 * Update the CartFlows steps link as per the latest UI.
259 *
260 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar object.
261 * @since 2.0.0
262 */
263 public function update_edit_step_menu_link( $wp_admin_bar ) {
264
265 if ( wcf()->utils->is_step_post_type() ) {
266
267 global $post;
268
269 if ( ! empty( $post ) ) {
270
271 $edit_node = $wp_admin_bar->get_node( 'edit' );
272
273 if ( $edit_node ) {
274 $post_id = $post->ID;
275 $flow_id = wcf()->utils->get_flow_id_from_step_id( $post_id );
276
277 // Show the edit node only if the page builder option is set to other and gutenberg, else remove/hide the node.
278 if ( in_array( Cartflows_Helper::get_common_setting( 'default_page_builder' ), array( 'other', 'gutenberg' ) ) || Cartflows_Helper::is_instant_layout_enabled( intval( $flow_id ) ) ) {
279 $edit_node->title = esc_html__( 'Edit Design', 'cartflows' );
280 $edit_node->href = $this->get_prepared_edit_step_url( $post->ID );
281 $wp_admin_bar->add_node( $edit_node );
282 } else {
283 $wp_admin_bar->remove_node( 'edit' );
284 }
285 }
286 }
287 }
288 }
289
290 /**
291 * Enqueue the Global Color Pallet CSS vars to the page to use in the page builder settings.
292 *
293 * Note: Currently the GCP support is added for Elementor and Block Builder.
294 *
295 * @param string $css_dependency the CSS file handle as a dependency to add the inline css.
296 * @since 2.0.0
297 * @return void
298 */
299 public function enqueue_gcp_color_vars( $css_dependency = '' ) {
300
301 if ( wcf()->utils->is_step_post_type() ) {
302
303 $flow_id = wcf()->utils->get_flow_id();
304
305 // Return if no flow ID is found.
306 if ( empty( $flow_id ) ) {
307 return;
308 }
309
310 if ( ! Cartflows_Helper::is_gcp_styling_enabled( (int) $flow_id ) ) {
311 return;
312 }
313
314 $gcp_vars = Cartflows_Helper::generate_gcp_css_style( (int) $flow_id );
315 $css_dependency = ! empty( $css_dependency ) ? $css_dependency : 'wcf-frontend-global';
316
317 // Don't print the inline CSS style if no style is generated.
318 if ( ! empty( $gcp_vars ) ) {
319 $output = ':root { ' . $gcp_vars . ' }';
320 wp_add_inline_style( $css_dependency, $output );
321 }
322 }
323 }
324
325 /**
326 * Generates the edit URL for a step based on the page builder and Instant Checkout features.
327 *
328 * @since 2.1.0
329 * @param int $step_id The ID of the step to generate the edit URL for.
330 * @return string $edit_url The edit URL for the specified step.
331 */
332 public function get_prepared_edit_step_url( $step_id ) {
333 // Default Edit Step URL.
334 $edit_url = Cartflows_Helper::get_page_builder_edit_link( $step_id );
335
336 // Get the current flow ID from the step.
337 $flow_id = (int) wcf()->utils->get_flow_id_from_step_id( $step_id );
338
339 // Update the edit URL if instant Checkout is enabled.
340 if ( Cartflows_Helper::is_instant_layout_enabled( (int) $flow_id ) ) {
341 $store_checkout = Cartflows_Helper::get_global_setting( '_cartflows_store_checkout' );
342 $store_checkout_path = intval( $store_checkout ) === intval( $flow_id ) ? 'store-checkout' : 'flows';
343
344 $edit_url = admin_url( 'admin.php?page=' . CARTFLOWS_SLUG . '&path=' . $store_checkout_path . '&action=wcf-edit-flow&flow_id=' . $flow_id . '&step_id=' . $step_id . '&tab=design' );
345 }
346
347 // Return the updated edit URL.
348 return $edit_url;
349 }
350 }
351
352 /**
353 * Kicking this off by calling 'get_instance()' method
354 */
355 Cartflows_Flow_Frontend::get_instance();
356