| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register funnel |
| 4 |
* |
| 5 |
* @package WPFunnels\CPT |
| 6 |
*/ |
| 7 |
namespace WPFunnels\CPT; |
| 8 |
|
| 9 |
use WPFunnels\Wpfnl_functions; |
| 10 |
|
| 11 |
class Wpfnl_CPT |
| 12 |
{ |
| 13 |
protected $args; |
| 14 |
|
| 15 |
protected $is_gutenberg_editor_active = false; |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
add_action('init', [ $this, 'register_funnel_cpt' ]); |
| 20 |
add_action('init', [ $this, 'register_funnel_steps' ]); |
| 21 |
add_action('init', [ $this, 'register_steps_meta' ]); |
| 22 |
add_filter('post_type_link', [ $this, 'step_post_type_permalinks' ], 10, 3); |
| 23 |
add_action('init', [ $this, 'rewrite_step_rule' ]); |
| 24 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_assets' ] ); |
| 25 |
add_action( 'admin_footer', [ $this, 'print_admin_js_template' ] ); |
| 26 |
|
| 27 |
if ( ! is_admin() ) { |
| 28 |
add_action( 'pre_get_posts', array( $this, 'add_steps_cpt_to_main_query' ), 20 ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Register post type for |
| 35 |
* funnel |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
*/ |
| 39 |
public function register_funnel_cpt() |
| 40 |
{ |
| 41 |
$labels = [ |
| 42 |
'name' => _x('Funnels', 'Funnel general name', 'wpfnl'), |
| 43 |
'singular_name' => _x('Funnel', 'Funnel singular name', 'wpfnl'), |
| 44 |
]; |
| 45 |
$args = [ |
| 46 |
'labels' => $labels, |
| 47 |
'public' => true, |
| 48 |
'publicly_queryable' => true, |
| 49 |
'show_ui' => false, |
| 50 |
'show_in_menu' => true, |
| 51 |
'query_var' => true, |
| 52 |
'capability_type' => 'post', |
| 53 |
'has_archive' => false, |
| 54 |
'hierarchical' => false, |
| 55 |
'show_in_rest' => true, |
| 56 |
'supports' => [ 'thumbnail' ], |
| 57 |
]; |
| 58 |
register_post_type(WPFNL_FUNNELS_POST_TYPE, $args); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Register post type for |
| 64 |
* steps |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
*/ |
| 68 |
public function register_funnel_steps() |
| 69 |
{ |
| 70 |
$permalink_settings = Wpfnl_functions::get_permalink_settings(); |
| 71 |
|
| 72 |
$labels = [ |
| 73 |
'name' => _x('Steps', 'Steps general name', 'wpfnl'), |
| 74 |
'singular_name' => _x('Step', 'Step singular name', 'wpfnl'), |
| 75 |
'edit_item' => __( 'Edit Step', 'wpfnl' ), |
| 76 |
]; |
| 77 |
$args = [ |
| 78 |
'labels' => $labels, |
| 79 |
'public' => true, |
| 80 |
'query_var' => true, |
| 81 |
'can_export' => true, |
| 82 |
'exclude_from_search' => true, |
| 83 |
'show_ui' => true, |
| 84 |
'show_in_menu' => false, |
| 85 |
'show_in_admin_bar' => false, |
| 86 |
'show_in_rest' => true, |
| 87 |
'supports' => [ 'title', 'editor', 'elementor', 'revisions', 'thumbnail', 'custom-fields', ], |
| 88 |
'capability_type' => 'post', |
| 89 |
'map_meta_cap' => true, |
| 90 |
]; |
| 91 |
|
| 92 |
if ( isset($_GET['ct_builder']) && 'true' == $_GET['ct_builder'] ) { |
| 93 |
$args['show_in_admin_bar'] = true; |
| 94 |
} |
| 95 |
|
| 96 |
if (!empty($permalink_settings['structure'])) { |
| 97 |
$structure = 'default'; |
| 98 |
switch ($permalink_settings['structure']) { |
| 99 |
case 'funnel-step': |
| 100 |
$structure = '/wpfunnels/%funnel_name%/wpfunnel_steps'; |
| 101 |
break; |
| 102 |
case 'funnel': |
| 103 |
$structure = '/wpfunnels/%funnel_name%'; |
| 104 |
break; |
| 105 |
case 'step': |
| 106 |
$structure = '%funnel_name%/wpfunnel_steps'; |
| 107 |
break; |
| 108 |
} |
| 109 |
if ($structure !== 'default') { |
| 110 |
$args['rewrite'] = [ |
| 111 |
'slug' => $structure, |
| 112 |
'with_front' => false, |
| 113 |
]; |
| 114 |
} |
| 115 |
} elseif (!empty($permalink_settings['step_base'])) { |
| 116 |
$args['rewrite'] = [ |
| 117 |
'slug' => $permalink_settings['step_base'], |
| 118 |
'with_front' => false, |
| 119 |
]; |
| 120 |
} |
| 121 |
register_post_type(WPFNL_STEPS_POST_TYPE, $args); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Register meta fields for steps post type |
| 127 |
*/ |
| 128 |
public function register_steps_meta() { |
| 129 |
|
| 130 |
register_meta('post', 'order-bump-settings', array( |
| 131 |
'single' => true, |
| 132 |
'type' => 'object', |
| 133 |
'default' => array( |
| 134 |
|
| 135 |
), |
| 136 |
'show_in_rest' => array( |
| 137 |
'schema' => array( |
| 138 |
'type' => 'object', |
| 139 |
'properties' => array( |
| 140 |
'selectedStyle' => array( |
| 141 |
'type' => 'string', |
| 142 |
), |
| 143 |
'position' => array( |
| 144 |
'type' => 'string', |
| 145 |
), |
| 146 |
'product' => array( |
| 147 |
'type' => 'string', |
| 148 |
), |
| 149 |
'quantity' => array( |
| 150 |
'type' => 'string', |
| 151 |
), |
| 152 |
'price' => array( |
| 153 |
'type' => 'string', |
| 154 |
), |
| 155 |
'salePrice' => array( |
| 156 |
'type' => 'string', |
| 157 |
), |
| 158 |
'htmlPrice' => array( |
| 159 |
'type' => 'string', |
| 160 |
), |
| 161 |
'productImage' => array( |
| 162 |
'type' => 'object', |
| 163 |
'properties' => array( |
| 164 |
'id' => array( |
| 165 |
'type' => 'string', |
| 166 |
), |
| 167 |
'url' => array( |
| 168 |
'type' => 'string', |
| 169 |
), |
| 170 |
) |
| 171 |
), |
| 172 |
'highLightText' => array( |
| 173 |
'type' => 'string', |
| 174 |
), |
| 175 |
'checkBoxLabel' => array( |
| 176 |
'type' => 'string', |
| 177 |
), |
| 178 |
'productDescriptionText' => array( |
| 179 |
'type' => 'string', |
| 180 |
), |
| 181 |
'discountOption' => array( |
| 182 |
'type' => 'string', |
| 183 |
), |
| 184 |
'discountapply' => array( |
| 185 |
'type' => 'string', |
| 186 |
), |
| 187 |
'discountPrice' => array( |
| 188 |
'type' => 'string', |
| 189 |
), |
| 190 |
'discountValue' => array( |
| 191 |
'type' => 'string', |
| 192 |
), |
| 193 |
'couponName' => array( |
| 194 |
'type' => 'string', |
| 195 |
), |
| 196 |
'obNextStep' => array( |
| 197 |
'type' => 'string', |
| 198 |
), |
| 199 |
'productName' => array( |
| 200 |
'type' => 'string', |
| 201 |
), |
| 202 |
'productType' => array( |
| 203 |
'type' => 'string', |
| 204 |
), |
| 205 |
'isEnabled' => array( |
| 206 |
'type' => 'string', |
| 207 |
), |
| 208 |
'isReplace' => array( |
| 209 |
'type' => 'string', |
| 210 |
), |
| 211 |
'replace' => array( |
| 212 |
'type' => 'boolean', |
| 213 |
), |
| 214 |
'obPrimaryColor' => array( |
| 215 |
'type' => 'string', |
| 216 |
), |
| 217 |
), |
| 218 |
), |
| 219 |
), |
| 220 |
)); |
| 221 |
|
| 222 |
// $this->register_string_type_meta('order-bump', 'no' ); |
| 223 |
$this->register_string_type_meta('_wpfnl_thankyou_order_overview', 'on' ); |
| 224 |
$this->register_string_type_meta('_wpfnl_thankyou_order_details', 'on' ); |
| 225 |
$this->register_string_type_meta('_wpfnl_thankyou_billing_details', 'on' ); |
| 226 |
$this->register_string_type_meta('_wpfnl_thankyou_shipping_details', 'on' ); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
/** |
| 231 |
* Register string type meta for steps post type. |
| 232 |
* |
| 233 |
* @param $meta_key |
| 234 |
* @param $default_value |
| 235 |
* |
| 236 |
* @since 2.0.3 |
| 237 |
*/ |
| 238 |
public function register_string_type_meta( $meta_key, $default_value ) { |
| 239 |
register_meta( |
| 240 |
'post', |
| 241 |
$meta_key, |
| 242 |
array( |
| 243 |
'single' => true, |
| 244 |
'type' => 'string', |
| 245 |
'show_in_rest' => array( |
| 246 |
'schema' => array( |
| 247 |
'type' => 'string', |
| 248 |
'default' => $default_value, |
| 249 |
), |
| 250 |
), |
| 251 |
'auth_callback' => function(){ return true; } |
| 252 |
) |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
/** |
| 259 |
* Rewrite rules for steps permalink |
| 260 |
* |
| 261 |
* @since 1.0.0 |
| 262 |
*/ |
| 263 |
public function rewrite_step_rule() |
| 264 |
{ |
| 265 |
$permalink_settings = Wpfnl_functions::get_permalink_settings(); |
| 266 |
if (isset($permalink_settings['structure'])) { |
| 267 |
switch ($permalink_settings['structure']) { |
| 268 |
case 'funnel-step': |
| 269 |
add_rewrite_rule('^' . $permalink_settings['funnel_base'] . '/([^/]*)/' . $permalink_settings['step_base'] . '/([^\/]*)/?', 'index.php?wpfunnel_steps=$matches[2]', 'top'); |
| 270 |
break; |
| 271 |
case 'funnel': |
| 272 |
add_rewrite_rule('^' . $permalink_settings['funnel_base'] . '/([^/]*)/([^/]*)/?', 'index.php?wpfunnel_steps=$matches[2]', 'top'); |
| 273 |
break; |
| 274 |
case 'step': |
| 275 |
add_rewrite_rule('([^/]*)/' . $permalink_settings['step_base'] . '/([^\/]*)/?', 'index.php?wpfunnel_steps=$matches[2]', 'top'); |
| 276 |
break; |
| 277 |
default: |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* Steps permalink creation |
| 286 |
* |
| 287 |
* @param $post_link |
| 288 |
* @param $post |
| 289 |
* @param $leavename |
| 290 |
* |
| 291 |
* @return string|string[] |
| 292 |
* @since 1.0.0 |
| 293 |
*/ |
| 294 |
public function step_post_type_permalinks( $post_link, $post, $leavename ) |
| 295 |
{ |
| 296 |
|
| 297 |
if (isset($post->post_type) && WPFNL_STEPS_POST_TYPE == $post->post_type) { |
| 298 |
$funnel_id = get_post_meta($post->ID, '_funnel_id', true); |
| 299 |
$funnel_name = get_post_field('post_title', $funnel_id) ? get_post_field('post_title', $funnel_id) : 'No Title'; |
| 300 |
$funnel_name = sanitize_title($funnel_name); |
| 301 |
$permalink_settings = Wpfnl_functions::get_permalink_settings(); |
| 302 |
|
| 303 |
if (isset($permalink_settings['structure']) && ! empty($permalink_settings['structure'])) { |
| 304 |
if($permalink_settings['structure'] !== 'default') { |
| 305 |
$sep = '/'; |
| 306 |
$post_link = preg_replace('/\/wpfunnels\//', '/'.$permalink_settings['funnel_base'].'/', $post_link); |
| 307 |
$search = [ $sep . '%funnel_name%', $sep . 'wpfunnel_steps' ]; |
| 308 |
$replace = [ $sep . $funnel_name, $sep . $permalink_settings['step_base'] ]; |
| 309 |
$post_link = str_replace($search, $replace, $post_link); |
| 310 |
} else { |
| 311 |
if (isset($_REQUEST['elementor-preview'])) { //phpcs:ignore |
| 312 |
return $post_link; |
| 313 |
} |
| 314 |
|
| 315 |
$structure = get_option('permalink_structure'); |
| 316 |
|
| 317 |
if ('/%postname%/' === $structure) { |
| 318 |
$post_link = str_replace('/' . $post->post_type . '/', '/', $post_link); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
} else { |
| 323 |
|
| 324 |
// If elementor page preview, return post link as it is. |
| 325 |
if (isset($_REQUEST['elementor-preview'])) { //phpcs:ignore |
| 326 |
return $post_link; |
| 327 |
} |
| 328 |
|
| 329 |
$structure = get_option('permalink_structure'); |
| 330 |
|
| 331 |
if ('/%postname%/' === $structure) { |
| 332 |
$post_link = str_replace('/' . $post->post_type . '/', '/', $post_link); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
return $post_link; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
/** |
| 341 |
* Add step post type to the main query |
| 342 |
* |
| 343 |
* @param $query |
| 344 |
* |
| 345 |
* @since 1.0.0 |
| 346 |
*/ |
| 347 |
public function add_steps_cpt_to_main_query( $query ) { |
| 348 |
$post_types = ''; |
| 349 |
if ( ! $query->is_main_query() ) { |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
if ( isset( $query->query['post_type'] ) ) { |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
if ( !isset( $query->query['page'] ) && !isset( $query->query_vars['page_id'] )) { |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
if ( empty( $query->query['name'] ) && empty( $query->query_vars['page_id'] ) ) { |
| 362 |
return; |
| 363 |
} |
| 364 |
if ( isset( $query->query_vars['post_type'] ) && is_array( $query->query_vars['post_type'] ) ) { |
| 365 |
$post_types = $query->get('post_type'); |
| 366 |
if (!empty($post_types)) { |
| 367 |
if(!is_array($post_types)) { |
| 368 |
$post_types = explode(',', $post_types); |
| 369 |
} |
| 370 |
} |
| 371 |
$post_types[] = WPFNL_STEPS_POST_TYPE; |
| 372 |
$post_types = array_map('trim', $post_types); |
| 373 |
$post_types = array_filter($post_types); |
| 374 |
|
| 375 |
$query->set('post_type', $post_types); |
| 376 |
|
| 377 |
} else { |
| 378 |
$query->set( 'post_type', array( 'post', 'page', WPFNL_STEPS_POST_TYPE ) ); |
| 379 |
} |
| 380 |
return $query; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Check if this is gutenberg window |
| 386 |
* |
| 387 |
* @since 2.0.3 |
| 388 |
*/ |
| 389 |
public function enqueue_assets() { |
| 390 |
if (!current_user_can('wpf_manage_funnels')) { |
| 391 |
return; |
| 392 |
} |
| 393 |
$this->is_gutenberg_editor_active = true; |
| 394 |
wp_enqueue_script( 'wpfnl-admin' . '-common', WPFNL_DIR_URL . '/admin/assets/js/wpfnl-admin-common.js', [ 'jquery' ], WPFNL_VERSION, true ); |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Print admin js template - will add back to funnel button on admin |
| 399 |
* |
| 400 |
* @since 2.0.3 |
| 401 |
*/ |
| 402 |
public function print_admin_js_template() { |
| 403 |
if ( ! $this->is_gutenberg_editor_active ) { |
| 404 |
return; |
| 405 |
} |
| 406 |
if (WPFNL_STEPS_POST_TYPE !== get_post_type()) { |
| 407 |
return; |
| 408 |
} |
| 409 |
$funnel_id = get_post_meta( get_the_id(), '_funnel_id', true ); |
| 410 |
$funnel_window_url = add_query_arg( |
| 411 |
array( |
| 412 |
'page' => WPFNL_EDIT_FUNNEL_SLUG, |
| 413 |
'id' => $funnel_id, |
| 414 |
'step_id' => get_the_id(), |
| 415 |
), |
| 416 |
admin_url('admin.php') |
| 417 |
); |
| 418 |
|
| 419 |
|
| 420 |
?> |
| 421 |
|
| 422 |
<script id="wpfnl-gutenberg-button-switch-mode" type="text/html"> |
| 423 |
<div id="wpfnl-switch-mode"> |
| 424 |
<a href="<?php echo esc_url($funnel_window_url); ?>" id="wpfnl-switch-mode-button" type="button" class="button button-primary button-large"> |
| 425 |
<span class="wpfnl-switch-mode-on"><?php echo __( '← Back to Funnel Window', 'wpfnl' ); ?></span> |
| 426 |
</a> |
| 427 |
</div> |
| 428 |
</script> |
| 429 |
<?php |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
function is_elementor(){ |
| 434 |
global $post; |
| 435 |
return \Elementor\Plugin::$instance->db->is_built_with_elementor($post->ID); |
| 436 |
} |
| 437 |
|
| 438 |
} |
| 439 |
|