| 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 |
/** |
| 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' ), |
| 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 |
// Bail if this query doesn't match our very specific rewrite rule. |
| 278 |
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) { |
| 279 |
return; |
| 280 |
} |
| 281 |
// Bail if we're not querying based on the post name. |
| 282 |
if ( empty( $query->query['name'] ) ) { |
| 283 |
return; |
| 284 |
} |
| 285 |
// Add CPT to the list of post types WP will include when it queries based on the post name. |
| 286 |
$query->set( 'post_type', array( 'post', 'page', CARTFLOWS_STEP_POST_TYPE ) ); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Modify permalink |
| 291 |
* |
| 292 |
* @param string $post_link post link. |
| 293 |
* @param array $post post data. |
| 294 |
* @param string $leavename leave name. |
| 295 |
* @return string |
| 296 |
*/ |
| 297 |
function post_type_permalinks( $post_link, $post, $leavename ) { |
| 298 |
|
| 299 |
// If elementor page preview, return post link as it is. |
| 300 |
if ( isset( $_REQUEST['elementor-preview'] ) ) { |
| 301 |
return $post_link; |
| 302 |
} |
| 303 |
|
| 304 |
$structure = get_option( 'permalink_structure' ); |
| 305 |
|
| 306 |
if ( '/%postname%/' === $structure ) { |
| 307 |
|
| 308 |
if ( isset( $post->post_type ) && CARTFLOWS_STEP_POST_TYPE == $post->post_type ) { |
| 309 |
|
| 310 |
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
return $post_link; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Prevent slug duplicated |
| 319 |
* |
| 320 |
* @param string $slug post slug. |
| 321 |
* @param int $post_ID post id. |
| 322 |
* @param string $post_status post status. |
| 323 |
* @param string $post_type post type. |
| 324 |
* @param int $post_parent post parent id. |
| 325 |
* @param string $original_slug original slug. |
| 326 |
* @return string |
| 327 |
*/ |
| 328 |
function prevent_slug_duplicates( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) { |
| 329 |
|
| 330 |
$check_post_types = array( |
| 331 |
'post', |
| 332 |
'page', |
| 333 |
CARTFLOWS_STEP_POST_TYPE, |
| 334 |
); |
| 335 |
|
| 336 |
if ( ! in_array( $post_type, $check_post_types ) ) { |
| 337 |
return $slug; |
| 338 |
} |
| 339 |
|
| 340 |
if ( CARTFLOWS_STEP_POST_TYPE == $post_type ) { |
| 341 |
// Saving a post, check for duplicates in POST or PAGE post types. |
| 342 |
$post_match = get_page_by_path( $slug, 'OBJECT', 'post' ); |
| 343 |
$page_match = get_page_by_path( $slug, 'OBJECT', 'page' ); |
| 344 |
|
| 345 |
if ( $post_match || $page_match ) { |
| 346 |
$slug .= '-2'; |
| 347 |
} |
| 348 |
} else { |
| 349 |
|
| 350 |
// Saving a POST or PAGE, check for duplicates in CARTFLOWS_STEP_POST_TYPE post type. |
| 351 |
$custom_post_type_match = get_page_by_path( $slug, 'OBJECT', CARTFLOWS_STEP_POST_TYPE ); |
| 352 |
|
| 353 |
if ( $custom_post_type_match ) { |
| 354 |
$slug .= '-2'; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
return $slug; |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Add Update messages for any custom post type |
| 363 |
* |
| 364 |
* @param array $messages Array of default messages. |
| 365 |
*/ |
| 366 |
function post_update_messages( $messages ) { |
| 367 |
|
| 368 |
$custom_post_type = get_post_type( get_the_ID() ); |
| 369 |
|
| 370 |
if ( CARTFLOWS_STEP_POST_TYPE == $custom_post_type ) { |
| 371 |
|
| 372 |
$obj = get_post_type_object( $custom_post_type ); |
| 373 |
$singular_name = $obj->labels->singular_name; |
| 374 |
$messages[ $custom_post_type ] = array( |
| 375 |
0 => '', // Unused. Messages start at index 1. |
| 376 |
/* translators: %s: singular custom post type name */ |
| 377 |
1 => sprintf( __( '%s updated.', 'cartflows' ), $singular_name ), |
| 378 |
/* translators: %s: singular custom post type name */ |
| 379 |
2 => sprintf( __( 'Custom %s updated.', 'cartflows' ), $singular_name ), |
| 380 |
/* translators: %s: singular custom post type name */ |
| 381 |
3 => sprintf( __( 'Custom %s deleted.', 'cartflows' ), $singular_name ), |
| 382 |
/* translators: %s: singular custom post type name */ |
| 383 |
4 => sprintf( __( '%s updated.', 'cartflows' ), $singular_name ), |
| 384 |
/* translators: %1$s: singular custom post type name ,%2$s: date and time of the revision */ |
| 385 |
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, |
| 386 |
/* translators: %s: singular custom post type name */ |
| 387 |
6 => sprintf( __( '%s published.', 'cartflows' ), $singular_name ), |
| 388 |
/* translators: %s: singular custom post type name */ |
| 389 |
7 => sprintf( __( '%s saved.', 'cartflows' ), $singular_name ), |
| 390 |
/* translators: %s: singular custom post type name */ |
| 391 |
8 => sprintf( __( '%s submitted.', 'cartflows' ), $singular_name ), |
| 392 |
/* translators: %s: singular custom post type name */ |
| 393 |
9 => sprintf( __( '%s scheduled for.', 'cartflows' ), $singular_name ), |
| 394 |
/* translators: %s: singular custom post type name */ |
| 395 |
10 => sprintf( __( '%s draft updated.', 'cartflows' ), $singular_name ), |
| 396 |
); |
| 397 |
} |
| 398 |
|
| 399 |
return $messages; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Add Terms for Taxonomy. |
| 404 |
* |
| 405 |
* => Example. |
| 406 |
* |
| 407 |
* $taxonomy = '{taxonomy}'; |
| 408 |
* $terms = array( |
| 409 |
* array( |
| 410 |
* 'name' => 'Free', |
| 411 |
* ), |
| 412 |
* array( |
| 413 |
* 'name' => 'Premium', |
| 414 |
* ), |
| 415 |
* ); |
| 416 |
* |
| 417 |
* self::add_terms( $taxonomy, $terms ); |
| 418 |
* |
| 419 |
* @since 1.0.0 |
| 420 |
* @param string $taxonomy Taxonomy Name. |
| 421 |
* @param array $terms Terms list. |
| 422 |
* @return void |
| 423 |
*/ |
| 424 |
function add_terms( $taxonomy = '', $terms = array() ) { |
| 425 |
|
| 426 |
foreach ( $terms as $key => $term ) { |
| 427 |
|
| 428 |
$term_exist = term_exists( $term['name'], $taxonomy ); |
| 429 |
|
| 430 |
if ( empty( $term_exist ) ) { |
| 431 |
|
| 432 |
/** |
| 433 |
* Add additional args if passed from request. |
| 434 |
* |
| 435 |
* @see https://codex.wordpress.org/Function_Reference/wp_insert_term |
| 436 |
*/ |
| 437 |
if ( array_key_exists( 'args', $term ) ) { |
| 438 |
wp_insert_term( $term['name'], $taxonomy, $term['args'] ); |
| 439 |
} else { |
| 440 |
wp_insert_term( $term['name'], $taxonomy ); |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Kicking this off by calling 'get_instance()' method |
| 449 |
*/ |
| 450 |
Cartflows_Step_Post_Type::get_instance(); |
| 451 |
|