| 1 |
<?php |
| 2 |
|
| 3 |
use Sellkit\Global_Checkout\Checkout; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Funnel_Homepage. |
| 9 |
* Handle funnel steps as home page. |
| 10 |
* |
| 11 |
* @since 1.9.6 |
| 12 |
*/ |
| 13 |
class Funnel_Homepage { |
| 14 |
|
| 15 |
/** |
| 16 |
* Valid funnel types. |
| 17 |
* |
| 18 |
* @since 1.9.6 |
| 19 |
* @var array |
| 20 |
* @access private |
| 21 |
*/ |
| 22 |
private const VALID_TYPES = [ 'sales-page', 'checkout' ]; |
| 23 |
|
| 24 |
/** |
| 25 |
* First step as a homepage. |
| 26 |
* |
| 27 |
* @since 1.9.6 |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
public static $first_step; |
| 31 |
|
| 32 |
/** |
| 33 |
* Funnel_Homepage constructor. |
| 34 |
* |
| 35 |
* @since 1.9.6 |
| 36 |
*/ |
| 37 |
public function __construct() { |
| 38 |
add_action( 'pre_get_posts', [ $this, 'pre_get_posts' ] ); |
| 39 |
|
| 40 |
if ( is_admin() ) { |
| 41 |
add_filter( 'wp_dropdown_pages', [ $this, 'wp_dropdown_pages' ], 10, 1 ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Set the post type to sellkit_step if the page_id is a step. |
| 47 |
* |
| 48 |
* @param \WP_Query $query The WP_Query object. |
| 49 |
* @since 1.9.6 |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function pre_get_posts( $query ) { |
| 53 |
if ( $query->is_main_query() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 54 |
|
| 55 |
$post_type = $query->get( 'post_type' ); |
| 56 |
|
| 57 |
$page_id = (int) $query->get( 'page_id' ); |
| 58 |
|
| 59 |
if ( empty( $post_type ) && ! empty( $page_id ) ) { |
| 60 |
$post_type_from_id = get_post_type( $page_id ); |
| 61 |
$status = self::check_funnel_step_status( $page_id, $post_type_from_id ); |
| 62 |
|
| 63 |
if ( $status ) { |
| 64 |
$query->set( 'post_type', $post_type_from_id ); |
| 65 |
|
| 66 |
if ( intval( $page_id ) === intval( get_option( 'page_on_front', 0 ) ) ) { |
| 67 |
new Sellkit_Funnel( $page_id ); |
| 68 |
self::$first_step = $page_id; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Add funnel steps to the dropdown list. |
| 77 |
* |
| 78 |
* @param string $output The HTML output. |
| 79 |
* @since 1.9.6 |
| 80 |
* @return string |
| 81 |
* @SuppressWarnings(PHPMD.NPathComplexity) |
| 82 |
*/ |
| 83 |
public function wp_dropdown_pages( $output ) { |
| 84 |
if ( ! $this->is_homepage_for_frontend( $output ) ) { |
| 85 |
return $output; |
| 86 |
} |
| 87 |
|
| 88 |
$args = [ |
| 89 |
'post_type' => 'sellkit_step', |
| 90 |
'post_status' => 'publish', |
| 91 |
'posts_per_page' => 100, |
| 92 |
]; |
| 93 |
|
| 94 |
$query = new \WP_Query( $args ); |
| 95 |
|
| 96 |
if ( ! $query->have_posts() ) { |
| 97 |
return $output; |
| 98 |
} |
| 99 |
|
| 100 |
$front_page_id = get_option( 'page_on_front', 0 ); |
| 101 |
$global_checkout_id = get_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ); |
| 102 |
|
| 103 |
$steps = get_posts( $args ); |
| 104 |
|
| 105 |
$custom_options = ''; |
| 106 |
|
| 107 |
foreach ( $steps as $step ) { |
| 108 |
$step_data = []; |
| 109 |
|
| 110 |
if ( isset( $step->ID ) ) { |
| 111 |
$step_data = get_post_meta( $step->ID, 'step_data', true ); |
| 112 |
} |
| 113 |
|
| 114 |
if ( empty( $step_data ) ) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
|
| 118 |
if ( isset( $step_data['funnel_id'] ) && intval( $global_checkout_id ) === intval( $step_data['funnel_id'] ) ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
if ( isset( $step_data['type'] ) && ! in_array( $step_data['type']['key'], self::VALID_TYPES, true ) ) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
$funnel_id = isset( $step_data['funnel_id'] ) ? $step_data['funnel_id'] : 0; |
| 127 |
|
| 128 |
if ( ! empty( $funnel_id ) && 'publish' !== get_post_status( $funnel_id ) ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
if ( 'publish' !== $step_data['status'] ) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
$selected = selected( $front_page_id, $step->ID, false ); |
| 137 |
|
| 138 |
$title = sprintf( |
| 139 |
'%1$s (%2$s)', |
| 140 |
$step->post_title, |
| 141 |
esc_html__( 'Sellkit', 'sellkit' ) |
| 142 |
); |
| 143 |
|
| 144 |
$custom_options .= sprintf( |
| 145 |
'<option value="%1$s" %2$s>%3$s</option>', |
| 146 |
esc_attr( $step->ID ), |
| 147 |
esc_attr( $selected ), |
| 148 |
esc_html( $title ) |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
$custom_options .= '</select>'; |
| 153 |
|
| 154 |
$output = str_replace( '</select>', $custom_options, $output ); |
| 155 |
|
| 156 |
return $output; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Check if the current page is the homepage for frontend. |
| 161 |
* |
| 162 |
* @param string $output The HTML output. |
| 163 |
* @since 1.9.6 |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
private function is_homepage_for_frontend( $output ) { |
| 167 |
global $pagenow; |
| 168 |
|
| 169 |
if ( ( 'options-reading.php' === $pagenow || 'customize.php' === $pagenow ) && preg_match( '#page_on_front#', $output ) ) { |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Check funnel step status. |
| 178 |
* |
| 179 |
* @param int $step_id The step ID. |
| 180 |
* @param string $post_type The post type. |
| 181 |
* @since 1.9.6 |
| 182 |
* @return bool |
| 183 |
*/ |
| 184 |
public static function check_funnel_step_status( $step_id, $post_type ) { |
| 185 |
if ( 'sellkit_step' !== $post_type ) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
$step_data = get_post_meta( $step_id, 'step_data', true ); |
| 190 |
|
| 191 |
if ( empty( $step_data ) ) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
$funnel_status = get_post_status( $step_data['funnel_id'] ); |
| 196 |
|
| 197 |
if ( 'publish' !== $step_data['status'] || 'publish' !== $funnel_status ) { |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
return true; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
new Funnel_Homepage(); |
| 206 |
|