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

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