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

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