| 1 |
<?php |
| 2 |
/** |
| 3 |
* Step post type. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
/** |
| 13 |
* Initialization |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
class Cartflows_Permalink { |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Member Variable |
| 22 |
* |
| 23 |
* @var instance |
| 24 |
*/ |
| 25 |
private static $instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initiator |
| 29 |
*/ |
| 30 |
public static function get_instance() { |
| 31 |
if ( ! isset( self::$instance ) ) { |
| 32 |
self::$instance = new self(); |
| 33 |
} |
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
add_filter( 'post_type_link', array( $this, 'post_type_permalinks' ), 10, 3 ); |
| 43 |
add_action( 'init', array( $this, 'rewrite_step_rule' ) ); |
| 44 |
|
| 45 |
if ( ! is_admin() ) { |
| 46 |
|
| 47 |
add_action( 'pre_get_posts', array( $this, 'add_cpt_post_names_to_main_query' ), 20 ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Checks if the permalink base removal is allowed based on permalink structure or filter. |
| 53 |
* |
| 54 |
* @param string $structure The global WordPress permalink structure. |
| 55 |
* @return bool |
| 56 |
*/ |
| 57 |
private function is_permalink_base_removal_allowed( $structure ) { |
| 58 |
// Apply the filter and pass the permalink structure as an argument. |
| 59 |
$allow_remove_permalink_base = apply_filters( 'cartflows_allow_remove_permalink_base', false, $structure ); |
| 60 |
|
| 61 |
return '/%postname%/' === $structure || $allow_remove_permalink_base; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Modify permalink |
| 66 |
* |
| 67 |
* @param string $post_link post link. |
| 68 |
* @param array $post post data. |
| 69 |
* @param string $leavename leave name. |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
public function post_type_permalinks( $post_link, $post, $leavename ) { |
| 73 |
|
| 74 |
if ( isset( $post->post_type ) && CARTFLOWS_STEP_POST_TYPE == $post->post_type ) { |
| 75 |
|
| 76 |
$flow_id = get_post_meta( $post->ID, 'wcf-flow-id', true ); |
| 77 |
$flow_name = get_post_field( 'post_name', $flow_id ); |
| 78 |
$cf_permalink = Cartflows_Helper::get_permalink_settings(); |
| 79 |
|
| 80 |
if ( isset( $cf_permalink['permalink_structure'] ) && ! empty( $cf_permalink['permalink_structure'] ) ) { |
| 81 |
|
| 82 |
$sep = '/'; |
| 83 |
$search = array( $sep . 'cartflows_flow', $sep . '%flowname%', $sep . 'cartflows_step' ); |
| 84 |
$replace = array( $sep . $cf_permalink['permalink_flow_base'], $sep . $flow_name, $sep . $cf_permalink['permalink'] ); |
| 85 |
$post_link = str_replace( $search, $replace, $post_link ); |
| 86 |
} else { |
| 87 |
|
| 88 |
// If elementor page preview, return post link as it is. |
| 89 |
if ( isset( $_REQUEST['elementor-preview'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 90 |
return $post_link; |
| 91 |
} |
| 92 |
|
| 93 |
$structure = get_option( 'permalink_structure' ); |
| 94 |
|
| 95 |
if ( $this->is_permalink_base_removal_allowed( $structure ) ) { |
| 96 |
|
| 97 |
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); |
| 98 |
|
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $post_link; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Reqrite rules for acrtflows step. |
| 108 |
*/ |
| 109 |
public function rewrite_step_rule() { |
| 110 |
|
| 111 |
$cf_permalink = Cartflows_Helper::get_permalink_settings(); |
| 112 |
$structure = get_option( 'permalink_structure' ); |
| 113 |
if ( isset( $cf_permalink['permalink_structure'] ) && ! empty( $cf_permalink['permalink_structure'] ) ) { |
| 114 |
switch ( $cf_permalink['permalink_structure'] ) { |
| 115 |
case '/cartflows_flow/%flowname%/cartflows_step': |
| 116 |
add_rewrite_rule( '^' . $cf_permalink['permalink_flow_base'] . '/([^/]*)/' . $cf_permalink['permalink'] . '/([^\/]*)/?', 'index.php?cartflows_step=$matches[2]', 'top' ); |
| 117 |
break; |
| 118 |
|
| 119 |
case '/cartflows_flow/%flowname%': |
| 120 |
add_rewrite_rule( '^' . $cf_permalink['permalink_flow_base'] . '/([^/]*)/([^/]*)/?', 'index.php?cartflows_step=$matches[2]', 'top' ); |
| 121 |
break; |
| 122 |
|
| 123 |
case '/%flowname%/cartflows_step': |
| 124 |
add_rewrite_rule( '([^/]*)/' . $cf_permalink['permalink'] . '/([^\/]*)/?', 'index.php?cartflows_step=$matches[2]', 'top' ); |
| 125 |
|
| 126 |
break; |
| 127 |
|
| 128 |
default: |
| 129 |
break; |
| 130 |
} |
| 131 |
} elseif ( $this->is_permalink_base_removal_allowed( $structure ) ) { |
| 132 |
/* |
| 133 |
* This checks the Post Name structure OR if the external filter has enabled base removal. |
| 134 |
* We only need the custom rewrite rule if the standard structure is NOT 'Post Name' |
| 135 |
* and base removal is requested via the filter. |
| 136 |
*/ |
| 137 |
if ( '/%postname%/' !== $structure ) { |
| 138 |
$post_type = CARTFLOWS_STEP_POST_TYPE; |
| 139 |
|
| 140 |
// Custom rule for base-less slugs. |
| 141 |
add_rewrite_rule( |
| 142 |
'^([^/]+)/?$', |
| 143 |
'index.php?post_type=' . $post_type . '&name=$matches[1]', |
| 144 |
'top' |
| 145 |
); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Have WordPress match post name to any of our public post types. |
| 152 |
* All of our public post types can have /post-name/ as the slug, so they need to be unique across all posts. |
| 153 |
* By default, WordPress only accounts for posts and pages where the slug is /post-name/. |
| 154 |
* |
| 155 |
* @param WP_Query $query query statement. |
| 156 |
*/ |
| 157 |
public function add_cpt_post_names_to_main_query( $query ) { |
| 158 |
|
| 159 |
// Bail if this is not the main query. |
| 160 |
if ( ! $query->is_main_query() || is_home() ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
// Bail if post_type is already set in query. |
| 165 |
if ( isset( $query->query['post_type'] ) ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
// Bail if this query doesn't match our very specific rewrite rule. |
| 170 |
if ( ! isset( $query->query['thrive-variations'] ) |
| 171 |
&& ! isset( $query->query['page'] ) |
| 172 |
&& ! isset( $query->query_vars['page_id'] ) |
| 173 |
) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
// Bail if we're not querying based on the post name. |
| 178 |
if ( empty( $query->query['name'] ) && empty( $query->query_vars['page_id'] ) ) { |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
// Bail if name is set and query coutt is not 2 and page is not set. |
| 183 |
if ( ! empty( $query->query['name'] ) ) { |
| 184 |
|
| 185 |
if ( 2 !== count( $query->query ) ) { |
| 186 |
return; |
| 187 |
} elseif ( ! isset( $query->query['page'] ) ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// Bail if page_id is set but name is empty and doing ajax. Case - when step is set as homepage. |
| 193 |
if ( ! empty( $query->query_vars['page_id'] ) && empty( $query->query['name'] ) && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
// Bail if page_id is set but page is not true. Added when step is set as homepage. |
| 198 |
if ( ! empty( $query->query_vars['page_id'] ) && empty( $query->is_page ) && ! empty( $query->query['name'] ) ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// Add cartflows step post type to existing post type array. |
| 203 |
if ( isset( $query->query_vars['post_type'] ) && is_array( $query->query_vars['post_type'] ) ) { |
| 204 |
|
| 205 |
$post_types = $query->query_vars['post_type']; |
| 206 |
|
| 207 |
$post_types[] = CARTFLOWS_STEP_POST_TYPE; |
| 208 |
|
| 209 |
$query->set( 'post_type', $post_types ); |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Add the post type in the main query only if the post type in the query is empty. |
| 215 |
*/ |
| 216 |
if ( empty( $query->query_vars['post_type'] ) ) { |
| 217 |
$query->set( 'post_type', array( 'post', 'page', CARTFLOWS_STEP_POST_TYPE ) ); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Kicking this off by calling 'get_instance()' method |
| 224 |
*/ |
| 225 |
Cartflows_Permalink::get_instance(); |
| 226 |
|