PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.10
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.10
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.10, at modules/flow/classes/class-cartflows-step-post-type.php

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