| 1 |
<?php |
| 2 |
/** |
| 3 |
* Markup |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Checkout Markup |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
class Cartflows_Landing_Markup { |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Member Variable |
| 18 |
* |
| 19 |
* @var object instance |
| 20 |
*/ |
| 21 |
private static $instance; |
| 22 |
|
| 23 |
/** |
| 24 |
* Initiator |
| 25 |
*/ |
| 26 |
public static function get_instance() { |
| 27 |
if ( ! isset( self::$instance ) ) { |
| 28 |
self::$instance = new self; |
| 29 |
} |
| 30 |
return self::$instance; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
|
| 38 |
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
| 39 |
add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
| 40 |
|
| 41 |
if ( is_admin() ) { |
| 42 |
add_filter( 'wp_dropdown_pages', array( $this, 'wp_dropdown_pages' ) ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add landing pages in WordPress reading section. |
| 48 |
* |
| 49 |
* @param array $output output. |
| 50 |
*/ |
| 51 |
function wp_dropdown_pages( $output ) { |
| 52 |
|
| 53 |
global $pagenow; |
| 54 |
|
| 55 |
if ( ( 'options-reading.php' === $pagenow || 'customize.php' === $pagenow ) && preg_match( '#page_on_front#', $output ) ) { |
| 56 |
|
| 57 |
$args = array( |
| 58 |
'post_type' => CARTFLOWS_STEP_POST_TYPE, |
| 59 |
'numberposts' => 100, |
| 60 |
'meta_query' => array( |
| 61 |
'relation' => 'OR', |
| 62 |
array( |
| 63 |
'key' => 'wcf-step-type', |
| 64 |
'value' => 'landing', |
| 65 |
), |
| 66 |
array( |
| 67 |
'key' => 'wcf-step-type', |
| 68 |
'value' => 'checkout', |
| 69 |
), |
| 70 |
), |
| 71 |
); |
| 72 |
|
| 73 |
$landing_pages = get_posts( $args ); |
| 74 |
|
| 75 |
if ( is_array( $landing_pages ) && ! empty( $landing_pages ) ) { |
| 76 |
|
| 77 |
$cartflows_custom_option = ''; |
| 78 |
|
| 79 |
$front_page_id = get_option( 'page_on_front' ); |
| 80 |
|
| 81 |
foreach ( $landing_pages as $key => $landing_page ) { |
| 82 |
|
| 83 |
$selected = selected( $front_page_id, $landing_page->ID, false ); |
| 84 |
|
| 85 |
$cartflows_custom_option .= "<option value=\"{$landing_page->ID}\"{$selected}>{$landing_page->post_title} ( #{$landing_page->ID} - CartFlows )</option>"; |
| 86 |
} |
| 87 |
|
| 88 |
$cartflows_custom_option .= '</select>'; |
| 89 |
|
| 90 |
$output = str_replace( '</select>', $cartflows_custom_option, $output ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return $output; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Set post query. |
| 99 |
* |
| 100 |
* @param string $query post query. |
| 101 |
*/ |
| 102 |
function pre_get_posts( $query ) { |
| 103 |
|
| 104 |
if ( $query->is_main_query() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 105 |
|
| 106 |
$post_type = $query->get( 'post_type' ); |
| 107 |
|
| 108 |
$page_id = $query->get( 'page_id' ); |
| 109 |
|
| 110 |
if ( empty( $post_type ) && ! empty( $page_id ) ) { |
| 111 |
$query->set( 'post_type', get_post_type( $page_id ) ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Redirect to homepage if landing page set as home page. |
| 118 |
*/ |
| 119 |
function template_redirect() { |
| 120 |
|
| 121 |
$compatibiliy = Cartflows_Compatibility::get_instance(); |
| 122 |
|
| 123 |
// Do not redirect for page builder preview. |
| 124 |
if ( $compatibiliy->is_page_builder_preview() ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
global $post; |
| 129 |
|
| 130 |
if ( is_singular() && ! is_front_page() && get_option( 'page_on_front' ) == $post->ID ) { |
| 131 |
wp_safe_redirect( site_url(), 301 ); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Kicking this off by calling 'get_instance()' method |
| 138 |
*/ |
| 139 |
Cartflows_Landing_Markup::get_instance(); |
| 140 |
|