| 1 |
<?php |
| 2 |
/** |
| 3 |
* Markup |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
/** |
| 13 |
* Checkout Markup |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
class Cartflows_Landing_Markup { |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Member Variable |
| 22 |
* |
| 23 |
* @var object 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_action( 'pre_get_posts', array( $this, 'wcf_pre_get_posts' ) ); |
| 43 |
add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
| 44 |
|
| 45 |
if ( is_admin() ) { |
| 46 |
add_filter( 'wp_dropdown_pages', array( $this, 'add_steps_to_dropdown_pages' ) ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Add landing pages in WordPress reading section. |
| 52 |
* |
| 53 |
* @param array $output output. |
| 54 |
* |
| 55 |
* @return array. |
| 56 |
*/ |
| 57 |
public function add_steps_to_dropdown_pages( $output ) { |
| 58 |
|
| 59 |
global $pagenow; |
| 60 |
|
| 61 |
if ( ( 'options-reading.php' === $pagenow || 'customize.php' === $pagenow ) && preg_match( '#page_on_front#', $output ) ) { |
| 62 |
|
| 63 |
$args = array( |
| 64 |
'post_type' => CARTFLOWS_STEP_POST_TYPE, |
| 65 |
'numberposts' => 100, |
| 66 |
'meta_query' => array( //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 67 |
array( |
| 68 |
'key' => 'wcf-step-type', |
| 69 |
'value' => array( 'landing', 'checkout', 'optin' ), |
| 70 |
'compare' => 'IN', |
| 71 |
), |
| 72 |
), |
| 73 |
); |
| 74 |
|
| 75 |
$landing_pages = get_posts( $args ); |
| 76 |
|
| 77 |
if ( is_array( $landing_pages ) && ! empty( $landing_pages ) ) { |
| 78 |
|
| 79 |
$cartflows_custom_option = ''; |
| 80 |
|
| 81 |
$front_page_id = get_option( 'page_on_front' ); |
| 82 |
|
| 83 |
foreach ( $landing_pages as $key => $landing_page ) { |
| 84 |
|
| 85 |
$selected = selected( $front_page_id, $landing_page->ID, false ); |
| 86 |
|
| 87 |
$cartflows_custom_option .= "<option value=\"{$landing_page->ID}\"{$selected}>{$landing_page->post_title} ( #{$landing_page->ID} - CartFlows )</option>"; |
| 88 |
} |
| 89 |
|
| 90 |
$cartflows_custom_option .= '</select>'; |
| 91 |
|
| 92 |
$output = str_replace( '</select>', $cartflows_custom_option, $output ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return $output; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Set post query. |
| 101 |
* |
| 102 |
* @param WP_Query $query post query. |
| 103 |
*/ |
| 104 |
public function wcf_pre_get_posts( $query ) { |
| 105 |
|
| 106 |
if ( $query->is_main_query() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 107 |
|
| 108 |
$post_type = $query->get( 'post_type' ); |
| 109 |
|
| 110 |
$page_id = (int) $query->get( 'page_id' ); |
| 111 |
|
| 112 |
if ( empty( $post_type ) && ! empty( $page_id ) ) { |
| 113 |
$post_type_from_id = get_post_type( $page_id ); |
| 114 |
|
| 115 |
/** |
| 116 |
* Add the CartFlows post type in the main query. |
| 117 |
* Skip the rest of the post types which are not of CartFlows. |
| 118 |
*/ |
| 119 |
if ( CARTFLOWS_STEP_POST_TYPE === $post_type_from_id ) { |
| 120 |
$query->set( 'post_type', $post_type_from_id ); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Redirect to homepage if landing page set as home page. |
| 128 |
*/ |
| 129 |
public function template_redirect() { |
| 130 |
|
| 131 |
if ( ! wcf()->utils->is_step_post_type() ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$compatibiliy = Cartflows_Compatibility::get_instance(); |
| 136 |
|
| 137 |
// Do not redirect for page builder preview. |
| 138 |
if ( $compatibiliy->is_page_builder_preview() ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
global $post; |
| 143 |
|
| 144 |
if ( is_singular() && ! is_front_page() && get_option( 'page_on_front' ) == $post->ID ) { |
| 145 |
wp_safe_redirect( site_url(), 301 ); |
| 146 |
exit; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Kicking this off by calling 'get_instance()' method |
| 153 |
*/ |
| 154 |
Cartflows_Landing_Markup::get_instance(); |
| 155 |
|