PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.13
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.13
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 / flow / classes / class-cartflows-step-post-type.php

class-cartflows-step-post-type.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.13, at modules/flow/classes/class-cartflows-step-post-type.php

487 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Step post type.
4 *
5 * @package CartFlows
6 */
7
8 /**
9 * Initialization
10 *
11 * @since 1.0.0
12 */
13 class Cartflows_Step_Post_Type {
14
15
16 /**
17 * Member Variable
18 *
19 * @var instance
20 */
21 private static $instance;
22
23 /**
24 * Member Variable
25 *
26 * @var body_classes
27 */
28 private $body_classes = array();
29
30 /**
31 * Initiator
32 */
33 public static function get_instance() {
34 if ( ! isset( self::$instance ) ) {
35 self::$instance = new self;
36 }
37 return self::$instance;
38 }
39
40 /**
41 * Constructor
42 */
43 public function __construct() {
44
45 add_action( 'init', array( $this, 'step_post_type' ) );
46 add_action( 'init', array( $this, 'add_wp_templates_support' ) );
47 add_filter( 'post_updated_messages', array( $this, 'post_update_messages' ) );
48 add_filter( 'post_type_link', array( $this, 'post_type_permalinks' ), 10, 3 );
49 add_filter( 'wp_unique_post_slug', array( $this, 'prevent_slug_duplicates' ), 10, 6 );
50 add_action( 'pre_get_posts', array( $this, 'add_cpt_post_names_to_main_query' ), 20 );
51
52 add_filter( 'template_include', array( $this, 'load_page_template' ), 99 );
53 add_filter( 'template_redirect', array( $this, 'query_fix' ), 3 );
54 }
55
56
57 /**
58 * Trys to load page.php for a header, footer or part theme layout.
59 *
60 * @since 1.0.0
61 * @param string $template The current template to be loaded.
62 * @return string
63 */
64 function load_page_template( $template ) {
65
66 global $post;
67
68 if ( 'string' == gettype( $template ) && is_object( $post ) && CARTFLOWS_STEP_POST_TYPE === $post->post_type ) {
69
70 /**
71 * Remove Next/Prev Navigation
72 * add_filter('next_post_link', '__return_empty_string');
73 * add_filter('previous_post_link', '__return_empty_string');
74 *
75 * $page = locate_template( array( 'page.php' ) );
76 *
77 * if ( ! empty( $page ) ) {
78 * return $page;
79 * }
80 */
81
82 /* Remove Next / Previous Rel Link */
83 remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
84 add_filter( 'next_post_rel_link', '__return_empty_string' );
85 add_filter( 'previous_post_rel_link', '__return_empty_string' );
86
87 $page_template = get_post_meta( _get_wcf_step_id(), '_wp_page_template', true );
88
89 $page_template = apply_filters( 'cartflows_page_template', $page_template );
90
91 $file = '';
92
93 switch ( $page_template ) {
94
95 case 'cartflows-default':
96 $file = CARTFLOWS_FLOW_DIR . 'templates/template-default.php';
97 $this->body_classes[] = $page_template;
98 break;
99 case 'cartflows-canvas':
100 $file = CARTFLOWS_FLOW_DIR . 'templates/template-canvas.php';
101 $this->body_classes[] = $page_template;
102 break;
103 default:
104 $file = CARTFLOWS_FLOW_DIR . 'templates/template-default.php';
105 $this->body_classes[] = 'cartflows-default';
106 break;
107 }
108
109 // Just to be safe, we check if the file exist first.
110 if ( file_exists( $file ) ) {
111
112 /* Add Body Class */
113 add_filter( 'body_class', [ $this, 'body_class' ] );
114
115 return $file;
116 } else {
117 echo $file;
118 }
119 }
120
121 return $template;
122 }
123
124 /**
125 * Body classes.
126 *
127 * @since 1.0.0
128 * @param array $classes Body classes.
129 * @return array
130 */
131 function body_class( $classes = [] ) {
132
133 $classes = array_merge( $classes, $this->body_classes );
134
135 return $classes;
136 }
137
138 /**
139 * Create custom post type
140 */
141 function step_post_type() {
142
143 $labels = array(
144 'name' => esc_html_x( 'Steps', 'flow step general name', 'cartflows' ),
145 'singular_name' => esc_html_x( 'Step', 'flow step singular name', 'cartflows' ),
146 'search_items' => esc_html__( 'Search Steps', 'cartflows' ),
147 'all_items' => esc_html__( 'All Steps', 'cartflows' ),
148 'edit_item' => esc_html__( 'Edit Step', 'cartflows' ),
149 'view_item' => esc_html__( 'View Step', 'cartflows' ),
150 'add_new' => esc_html__( 'Add New', 'cartflows' ),
151 'update_item' => esc_html__( 'Update Step', 'cartflows' ),
152 'add_new_item' => esc_html__( 'Add New', 'cartflows' ),
153 'new_item_name' => esc_html__( 'New Step Name', 'cartflows' ),
154 );
155
156 $args = array(
157 'labels' => $labels,
158 'public' => true,
159 'query_var' => true,
160 'can_export' => true,
161 'exclude_from_search' => true,
162 'show_ui' => true,
163 'show_in_menu' => false,
164 'show_in_admin_bar' => true,
165 'supports' => array( 'title', 'editor', 'elementor', 'revisions' ),
166 'capability_type' => 'post',
167 'capabilities' => array(
168 'create_posts' => 'do_not_allow', // Prior to Wordpress 4.5, this was false.
169 ),
170 'map_meta_cap' => true,
171 );
172
173 register_post_type( CARTFLOWS_STEP_POST_TYPE, $args );
174
175 $args = array(
176 'label' => __( 'Step Type', 'cartflows' ),
177 'public' => false,
178 'rewrite' => false,
179 'hierarchical' => false,
180 );
181
182 register_taxonomy( CARTFLOWS_TAXONOMY_STEP_TYPE, CARTFLOWS_STEP_POST_TYPE, $args );
183
184 $args = array(
185 'label' => __( 'Step Flow', 'cartflows' ),
186 'public' => false,
187 'rewrite' => false,
188 'hierarchical' => false,
189 );
190
191 register_taxonomy( CARTFLOWS_TAXONOMY_STEP_FLOW, CARTFLOWS_STEP_POST_TYPE, $args );
192
193 /**
194 * Register 'Elementor' & 'Beaver Builder' site types.
195 *
196 * @see self::add_terms();
197 */
198 $taxonomy = CARTFLOWS_TAXONOMY_STEP_TYPE;
199
200 $terms = array(
201 array(
202 'name' => __( 'Landing', 'cartflows' ),
203 'args' => array(
204 'slug' => 'landing',
205 ),
206 ),
207 array(
208 'name' => __( 'Checkout', 'cartflows' ),
209 'args' => array(
210 'slug' => 'checkout',
211 ),
212 ),
213 array(
214 'name' => __( 'Thank You', 'cartflows' ),
215 'args' => array(
216 'slug' => 'thankyou',
217 ),
218 ),
219 array(
220 'name' => __( 'Upsell', 'cartflows' ),
221 'args' => array(
222 'slug' => 'upsell',
223 ),
224 ),
225 array(
226 'name' => __( 'Downsell', 'cartflows' ),
227 'args' => array(
228 'slug' => 'downsell',
229 ),
230 ),
231 );
232
233 $this->add_terms( $taxonomy, $terms );
234 }
235
236 /**
237 * Add WordPress templates.
238 *
239 * Adds Cartflows templates to steps
240 *
241 * @since 1.0.0
242 * @access public
243 */
244 function add_wp_templates_support() {
245 add_filter( 'theme_' . CARTFLOWS_STEP_POST_TYPE . '_templates', array( $this, 'add_page_templates' ), 99, 4 );
246 }
247
248 /**
249 * Add page templates.
250 *
251 * @since 1.0.0
252 * @access public
253 *
254 * @param array $page_templates Array of page templates.
255 *
256 * @param object $wp_theme wp theme.
257 * @param object $post post.
258 *
259 * @return array Page templates.
260 */
261 function add_page_templates( $page_templates, $wp_theme, $post ) {
262
263 $page_templates = array(
264 'cartflows-canvas' => _x( 'Template for Page Builders', 'cartflows' ),
265 );
266
267 return $page_templates;
268 }
269
270 /**
271 * Have WordPress match postname to any of our public post types.
272 * All of our public post types can have /post-name/ as the slug, so they need to be unique across all posts.
273 * By default, WordPress only accounts for posts and pages where the slug is /post-name/.
274 *
275 * @param string $query query statement.
276 */
277 function add_cpt_post_names_to_main_query( $query ) {
278
279 // Bail if this is not the main query.
280 if ( ! $query->is_main_query() ) {
281 return;
282 }
283
284 // Bail if this query doesn't match our very specific rewrite rule.
285 if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
286 return;
287 }
288
289 // Bail if we're not querying based on the post name.
290 if ( empty( $query->query['name'] ) ) {
291 return;
292 }
293
294 // Add cartflows step post type to existing post type array.
295 if ( isset( $query->query_vars['post_type'] ) && is_array( $query->query_vars['post_type'] ) ) {
296
297 $post_types = $query->query_vars['post_type'];
298
299 $post_types[] = CARTFLOWS_STEP_POST_TYPE;
300
301 $query->set( 'post_type', $post_types );
302
303 } else {
304
305 // Add CPT to the list of post types WP will include when it queries based on the post name.
306 $query->set( 'post_type', array( 'post', 'page', CARTFLOWS_STEP_POST_TYPE ) );
307 }
308 }
309
310 /**
311 * Query fixe throwing error on 404 page due our post type changes.
312 * We are setting post_type as empty array to fix the issue.
313 * Ther error was throwing due to redirect_canonical function
314 * This fix is apply for 404 page only
315 */
316 function query_fix() {
317
318 global $wp_query;
319
320 if ( $wp_query->is_404() ) {
321 $wp_query->set( 'post_type', array() );
322 }
323 }
324
325 /**
326 * Modify permalink
327 *
328 * @param string $post_link post link.
329 * @param array $post post data.
330 * @param string $leavename leave name.
331 * @return string
332 */
333 function post_type_permalinks( $post_link, $post, $leavename ) {
334
335 // If elementor page preview, return post link as it is.
336 if ( isset( $_REQUEST['elementor-preview'] ) ) {
337 return $post_link;
338 }
339
340 $structure = get_option( 'permalink_structure' );
341
342 if ( '/%postname%/' === $structure ) {
343
344 if ( isset( $post->post_type ) && CARTFLOWS_STEP_POST_TYPE == $post->post_type ) {
345
346 $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
347 }
348 }
349
350 return $post_link;
351 }
352
353 /**
354 * Prevent slug duplicated
355 *
356 * @param string $slug post slug.
357 * @param int $post_ID post id.
358 * @param string $post_status post status.
359 * @param string $post_type post type.
360 * @param int $post_parent post parent id.
361 * @param string $original_slug original slug.
362 * @return string
363 */
364 function prevent_slug_duplicates( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
365
366 $check_post_types = array(
367 'post',
368 'page',
369 CARTFLOWS_STEP_POST_TYPE,
370 );
371
372 if ( ! in_array( $post_type, $check_post_types ) ) {
373 return $slug;
374 }
375
376 if ( CARTFLOWS_STEP_POST_TYPE == $post_type ) {
377 // Saving a post, check for duplicates in POST or PAGE post types.
378 $post_match = get_page_by_path( $slug, 'OBJECT', 'post' );
379 $page_match = get_page_by_path( $slug, 'OBJECT', 'page' );
380
381 if ( $post_match || $page_match ) {
382 $slug .= '-2';
383 }
384 } else {
385
386 // Saving a POST or PAGE, check for duplicates in CARTFLOWS_STEP_POST_TYPE post type.
387 $custom_post_type_match = get_page_by_path( $slug, 'OBJECT', CARTFLOWS_STEP_POST_TYPE );
388
389 if ( $custom_post_type_match ) {
390 $slug .= '-2';
391 }
392 }
393
394 return $slug;
395 }
396
397 /**
398 * Add Update messages for any custom post type
399 *
400 * @param array $messages Array of default messages.
401 */
402 function post_update_messages( $messages ) {
403
404 $custom_post_type = get_post_type( get_the_ID() );
405
406 if ( CARTFLOWS_STEP_POST_TYPE == $custom_post_type ) {
407
408 $obj = get_post_type_object( $custom_post_type );
409 $singular_name = $obj->labels->singular_name;
410 $messages[ $custom_post_type ] = array(
411 0 => '', // Unused. Messages start at index 1.
412 /* translators: %s: singular custom post type name */
413 1 => sprintf( __( '%s updated.', 'cartflows' ), $singular_name ),
414 /* translators: %s: singular custom post type name */
415 2 => sprintf( __( 'Custom %s updated.', 'cartflows' ), $singular_name ),
416 /* translators: %s: singular custom post type name */
417 3 => sprintf( __( 'Custom %s deleted.', 'cartflows' ), $singular_name ),
418 /* translators: %s: singular custom post type name */
419 4 => sprintf( __( '%s updated.', 'cartflows' ), $singular_name ),
420 /* translators: %1$s: singular custom post type name ,%2$s: date and time of the revision */
421 5 => isset( $_GET['revision'] ) ? sprintf( __( '%1$s restored to revision from %2$s', 'cartflows' ), $singular_name, wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
422 /* translators: %s: singular custom post type name */
423 6 => sprintf( __( '%s published.', 'cartflows' ), $singular_name ),
424 /* translators: %s: singular custom post type name */
425 7 => sprintf( __( '%s saved.', 'cartflows' ), $singular_name ),
426 /* translators: %s: singular custom post type name */
427 8 => sprintf( __( '%s submitted.', 'cartflows' ), $singular_name ),
428 /* translators: %s: singular custom post type name */
429 9 => sprintf( __( '%s scheduled for.', 'cartflows' ), $singular_name ),
430 /* translators: %s: singular custom post type name */
431 10 => sprintf( __( '%s draft updated.', 'cartflows' ), $singular_name ),
432 );
433 }
434
435 return $messages;
436 }
437
438 /**
439 * Add Terms for Taxonomy.
440 *
441 * => Example.
442 *
443 * $taxonomy = '{taxonomy}';
444 * $terms = array(
445 * array(
446 * 'name' => 'Free',
447 * ),
448 * array(
449 * 'name' => 'Premium',
450 * ),
451 * );
452 *
453 * self::add_terms( $taxonomy, $terms );
454 *
455 * @since 1.0.0
456 * @param string $taxonomy Taxonomy Name.
457 * @param array $terms Terms list.
458 * @return void
459 */
460 function add_terms( $taxonomy = '', $terms = array() ) {
461
462 foreach ( $terms as $key => $term ) {
463
464 $term_exist = term_exists( $term['name'], $taxonomy );
465
466 if ( empty( $term_exist ) ) {
467
468 /**
469 * Add additional args if passed from request.
470 *
471 * @see https://codex.wordpress.org/Function_Reference/wp_insert_term
472 */
473 if ( array_key_exists( 'args', $term ) ) {
474 wp_insert_term( $term['name'], $taxonomy, $term['args'] );
475 } else {
476 wp_insert_term( $term['name'], $taxonomy );
477 }
478 }
479 }
480 }
481 }
482
483 /**
484 * Kicking this off by calling 'get_instance()' method
485 */
486 Cartflows_Step_Post_Type::get_instance();
487