| 1 |
<?php |
| 2 |
/** |
| 3 |
* Page template |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\PageTemplates; |
| 8 |
|
| 9 |
use WPFunnels\Base_Manager; |
| 10 |
use WPFunnels\Wpfnl_functions; |
| 11 |
|
| 12 |
class Manager extends Base_Manager { |
| 13 |
|
| 14 |
|
| 15 |
const TEMPLATE_Boxed = 'wpfunnels_boxed'; |
| 16 |
|
| 17 |
|
| 18 |
const TEMPLATE_DEFAULT = 'wpfunnels_default'; |
| 19 |
|
| 20 |
|
| 21 |
const TEMPLATE_FULLWIDTH_WITHOUT_HEADER_FOOTER = 'wpfunnels_fullwidth_without_header_footer'; |
| 22 |
|
| 23 |
|
| 24 |
const TEMPLATE_CHECKOUT = 'wpfunnels_checkout'; |
| 25 |
|
| 26 |
|
| 27 |
protected $print_callback; |
| 28 |
|
| 29 |
/** |
| 30 |
* Body class of the page templates |
| 31 |
* |
| 32 |
* @var $body_class |
| 33 |
*/ |
| 34 |
protected $body_class = array(); |
| 35 |
|
| 36 |
|
| 37 |
public function __construct() { |
| 38 |
add_action( 'init', [ $this, 'add_wp_templates_support' ] ); |
| 39 |
add_filter( 'template_include', [ $this, 'template_include' ], 90 ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
public function add_wp_templates_support() { |
| 44 |
$post_types = array( WPFNL_STEPS_POST_TYPE ); |
| 45 |
|
| 46 |
foreach ( $post_types as $post_type ) { |
| 47 |
add_filter( "theme_{$post_type}_templates", [ $this, 'add_funnel_page_templates' ], 9999, 4 ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Overrides the template used for rendering a page. |
| 54 |
* |
| 55 |
* This method is called by the WordPress template_include filter hook. |
| 56 |
* It allows the WP Funnels plugin to override the template used for rendering a page. |
| 57 |
* |
| 58 |
* @param string $template The path to the template file being used. |
| 59 |
* @return string The modified template path. |
| 60 |
*/ |
| 61 |
public function template_include( $template ) { |
| 62 |
if ( is_singular() ) { |
| 63 |
|
| 64 |
global $post; |
| 65 |
|
| 66 |
// Return template if post is empty |
| 67 |
if ( ! $post ) { |
| 68 |
return $template; |
| 69 |
} |
| 70 |
|
| 71 |
if ( is_object( $post ) && WPFNL_STEPS_POST_TYPE === $post->post_type ) { |
| 72 |
add_filter( 'next_post_rel_link', '__return_empty_string' ); |
| 73 |
add_filter( 'previous_post_rel_link', '__return_empty_string' ); |
| 74 |
$page_template = apply_filters( 'wpfunnels/page_template', $this->get_page_template($post->ID) ); |
| 75 |
$template_path = $this->get_template_path( $page_template ); |
| 76 |
|
| 77 |
/** |
| 78 |
* Checks if changing the page template is allowed and if a template path is provided. |
| 79 |
* |
| 80 |
* @param bool $allow_change_page_template Whether changing the page template is allowed. |
| 81 |
* @param string $template_path The path to the template file. |
| 82 |
* @return bool Whether changing the page template is allowed and a template path is provided. |
| 83 |
* |
| 84 |
* @since 3.1.0 |
| 85 |
*/ |
| 86 |
if ( apply_filters('wpfunnels/allow_to_change_page_template', true) && $template_path ) { |
| 87 |
$template = $template_path; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $template; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
private function get_page_template($post_id) { |
| 97 |
return get_post_meta( $post_id, '_wp_page_template', true ); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Add funnel page template |
| 103 |
* |
| 104 |
* @param $page_templates |
| 105 |
* @param $wp_theme |
| 106 |
* @param $post |
| 107 |
* |
| 108 |
* @return array |
| 109 |
* |
| 110 |
* @since 1.0.0 |
| 111 |
*/ |
| 112 |
public function add_funnel_page_templates( $page_templates, $wp_theme, $post ) { |
| 113 |
$wpfnl_templates = [ |
| 114 |
self::TEMPLATE_DEFAULT => _x( 'WPFunnels Default', 'Page Template', 'wpfnl' ), |
| 115 |
self::TEMPLATE_FULLWIDTH_WITHOUT_HEADER_FOOTER => _x( 'WPFunnels Canvas', 'Page Template', 'wpfnl' ), |
| 116 |
self::TEMPLATE_Boxed => _x( 'WPFunnels Boxed', 'Page Template', 'wpfnl' ), |
| 117 |
]; |
| 118 |
|
| 119 |
if ( apply_filters( 'wpfunnels/show_all_page_templates', false ) ) { |
| 120 |
$wpfnl_templates = $wpfnl_templates + $page_templates; |
| 121 |
} |
| 122 |
|
| 123 |
return $wpfnl_templates; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
public function set_print_callback( $callback ) { |
| 128 |
$this->print_callback = $callback; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
public function print_callback() { |
| 133 |
while ( have_posts() ) : |
| 134 |
the_post(); |
| 135 |
the_content(); |
| 136 |
endwhile; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
public function print_content() { |
| 141 |
|
| 142 |
if ( ! $this->print_callback ) { |
| 143 |
$this->print_callback = [ $this, 'print_callback' ]; |
| 144 |
} |
| 145 |
|
| 146 |
call_user_func( $this->print_callback ); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
public function get_template_path( $page_template ) { |
| 151 |
$template_path = ''; |
| 152 |
switch ( $page_template ) { |
| 153 |
case self::TEMPLATE_DEFAULT: |
| 154 |
$template_path = __DIR__ . '/templates/template-default.php'; |
| 155 |
$this->body_class[] = $page_template; |
| 156 |
break; |
| 157 |
case self::TEMPLATE_FULLWIDTH_WITHOUT_HEADER_FOOTER: |
| 158 |
$template_path = __DIR__ . '/templates/template-canvas.php'; |
| 159 |
$this->body_class[] = $page_template; |
| 160 |
break; |
| 161 |
case self::TEMPLATE_Boxed: |
| 162 |
$template_path = __DIR__ . '/templates/template-boxed.php'; |
| 163 |
$this->body_class[] = $page_template; |
| 164 |
break; |
| 165 |
} |
| 166 |
if( file_exists($template_path) ) { |
| 167 |
add_filter( 'body_class', array( $this, 'body_class' ) ); |
| 168 |
return $template_path; |
| 169 |
} |
| 170 |
return $template_path; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* Assign body clas for specific template |
| 176 |
* |
| 177 |
* @param $classes |
| 178 |
* |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
public function body_class( $classes ) { |
| 182 |
return array_merge( $classes, $this->body_class ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
public function action_register_template_control( $document ) { |
| 187 |
if ( $document instanceof PageBase || $document instanceof LibraryPageDocument ) { |
| 188 |
$this->register_template_control( $document ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
public function register_template_control( $document, $control_id = 'template' ) { |
| 194 |
if ( ! Utils::is_cpt_custom_templates_supported() ) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
require_once ABSPATH . '/wp-admin/includes/template.php'; |
| 199 |
|
| 200 |
$document->start_injection( [ |
| 201 |
'of' => 'post_status', |
| 202 |
'fallback' => [ |
| 203 |
'of' => 'post_title', |
| 204 |
], |
| 205 |
] ); |
| 206 |
|
| 207 |
$control_options = [ |
| 208 |
'options' => array_flip( get_page_templates( null, $document->get_main_post()->post_type ) ), |
| 209 |
]; |
| 210 |
|
| 211 |
$this->add_template_controls( $document, $control_id, $control_options ); |
| 212 |
|
| 213 |
$document->end_injection(); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public function add_template_controls( Document $document, $control_id, $control_options ) { |
| 218 |
// Default Control Options |
| 219 |
$default_control_options = [ |
| 220 |
'label' => __( 'Page Layout', 'wpfnl' ), |
| 221 |
'type' => Controls_Manager::SELECT, |
| 222 |
'default' => 'wpfunnels_default', |
| 223 |
'options' => [ |
| 224 |
'default' => __( 'Default', 'wpfnl' ), |
| 225 |
], |
| 226 |
]; |
| 227 |
|
| 228 |
$control_options = array_replace_recursive( $default_control_options, $control_options ); |
| 229 |
|
| 230 |
$document->add_control( |
| 231 |
$control_id, |
| 232 |
$control_options |
| 233 |
); |
| 234 |
|
| 235 |
$document->add_control( |
| 236 |
$control_id . '_default_description', |
| 237 |
[ |
| 238 |
'type' => Controls_Manager::RAW_HTML, |
| 239 |
'raw' => '<b>' . __( 'Default Page Template from your theme', 'wpfnl' ) . '</b>', |
| 240 |
'content_classes' => 'elementor-descriptor', |
| 241 |
'condition' => [ |
| 242 |
$control_id => 'default', |
| 243 |
], |
| 244 |
] |
| 245 |
); |
| 246 |
|
| 247 |
$document->add_control( |
| 248 |
$control_id . '_canvas_description', |
| 249 |
[ |
| 250 |
'type' => Controls_Manager::RAW_HTML, |
| 251 |
'raw' => '<b>' . __( 'No header, no footer, just Elementor', 'wpfnl' ) . '</b>', |
| 252 |
'content_classes' => 'elementor-descriptor', |
| 253 |
'condition' => [ |
| 254 |
$control_id => self::TEMPLATE_Boxed, |
| 255 |
], |
| 256 |
] |
| 257 |
); |
| 258 |
|
| 259 |
$document->add_control( |
| 260 |
$control_id . '_header_footer_description', |
| 261 |
[ |
| 262 |
'type' => Controls_Manager::RAW_HTML, |
| 263 |
'raw' => '<b>' . __( 'This template includes the header, full-width content and footer', 'wpfnl' ) . '</b>', |
| 264 |
'content_classes' => 'elementor-descriptor', |
| 265 |
'condition' => [ |
| 266 |
$control_id => self::TEMPLATE_HEADER_FOOTER, |
| 267 |
], |
| 268 |
] |
| 269 |
); |
| 270 |
|
| 271 |
if ( $document instanceof Kit ) { |
| 272 |
$document->add_control( |
| 273 |
'reload_preview_description', |
| 274 |
[ |
| 275 |
'type' => Controls_Manager::RAW_HTML, |
| 276 |
'raw' => __( 'Changes will be reflected in the preview only after the page reloads.', 'wpfnl' ), |
| 277 |
'content_classes' => 'elementor-descriptor', |
| 278 |
] |
| 279 |
); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
public static function body_open() { |
| 285 |
if ( function_exists( 'wp_body_open' ) ) { |
| 286 |
wp_body_open(); |
| 287 |
} else { |
| 288 |
do_action( 'wp_body_open' ); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
public function get_name() { |
| 294 |
return 'page-templates'; |
| 295 |
} |
| 296 |
|
| 297 |
} |
| 298 |
|