| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Builder; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
|
| 7 |
class PageTemplates { |
| 8 |
const TEMPLATE_HEADER_FOOTER = 'templately_header_footer'; |
| 9 |
private $platform = ''; |
| 10 |
private $callback = null; |
| 11 |
private $module = null; |
| 12 |
private $templates; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
add_filter( 'theme_page_templates', [ $this, 'add_new_template' ] ); |
| 16 |
add_filter( 'theme_post_templates', [ $this, 'add_new_template' ] ); |
| 17 |
add_filter( 'theme_templately_library_templates', [ $this, 'add_new_template' ] ); |
| 18 |
|
| 19 |
// Add a filter to the save post to inject out template into the page cache. |
| 20 |
// add_filter( 'wp_insert_post_data', [ $this, 'register_templates' ] ); |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
public function get_template() { |
| 25 |
if(!empty($this->templates)){ |
| 26 |
return $this->templates; |
| 27 |
} |
| 28 |
$this->templates = [ |
| 29 |
self::TEMPLATE_HEADER_FOOTER => __( 'Templately Gutenberg Template', 'templately' ), |
| 30 |
]; |
| 31 |
return $this->templates; |
| 32 |
} |
| 33 |
|
| 34 |
public function set_platform( $platform ): PageTemplates { |
| 35 |
$this->platform = $platform; |
| 36 |
|
| 37 |
if ( $this->platform === 'elementor' ) { |
| 38 |
$this->module = Plugin::$instance->modules_manager->get_modules( 'page-templates' ); |
| 39 |
} elseif ( $this->platform === 'gutenberg' ) { |
| 40 |
$this->module = new self(); |
| 41 |
} |
| 42 |
|
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
public function set_print_callback( $callback ) { |
| 47 |
if ( $this->platform === 'elementor' ) { |
| 48 |
$this->module->set_print_callback( $callback ); |
| 49 |
|
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$this->callback = $callback; |
| 54 |
} |
| 55 |
|
| 56 |
private function callback() { |
| 57 |
while ( have_posts() ) { |
| 58 |
the_post(); |
| 59 |
the_content(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function print() { |
| 64 |
if ( ! $this->callback ) { |
| 65 |
$this->callback = [ $this, 'callback' ]; |
| 66 |
} |
| 67 |
|
| 68 |
call_user_func( $this->callback ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get page template path. |
| 73 |
* |
| 74 |
* Retrieve the path for any given page template. |
| 75 |
* |
| 76 |
* @param string $page_template The page template name. |
| 77 |
* |
| 78 |
* @return string Page template path. |
| 79 |
* @since 2.0.0 |
| 80 |
* @access public |
| 81 |
* |
| 82 |
*/ |
| 83 |
public function get_template_path( string $page_template ): string { |
| 84 |
if ( $this->platform === 'elementor' ) { |
| 85 |
return $this->module->get_template_path( $page_template ); |
| 86 |
} |
| 87 |
|
| 88 |
$template_path = ''; |
| 89 |
|
| 90 |
$file = TEMPLATELY_PATH . 'views/'; |
| 91 |
|
| 92 |
|
| 93 |
switch ( $page_template ) { |
| 94 |
case self::TEMPLATE_HEADER_FOOTER: |
| 95 |
// removes: Renders a 'viewport' meta tag. |
| 96 |
remove_action( 'wp_head', '_block_template_viewport_meta_tag', 0 ); |
| 97 |
|
| 98 |
if ( $this->platform === 'gutenberg' ) { |
| 99 |
$template_path = $file . 'templates/templately-gutenberg-template.php'; |
| 100 |
} else { |
| 101 |
$template_path = $file . 'templates/header-footer.php'; |
| 102 |
} |
| 103 |
break; |
| 104 |
default: |
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
return $template_path; |
| 109 |
} |
| 110 |
|
| 111 |
public function get_header_footer_template(): string { |
| 112 |
if ( $this->platform === 'elementor' ) { |
| 113 |
return $this->module::TEMPLATE_HEADER_FOOTER; |
| 114 |
} |
| 115 |
|
| 116 |
return self::TEMPLATE_HEADER_FOOTER; |
| 117 |
} |
| 118 |
|
| 119 |
public function add_new_template( $posts_templates ) { |
| 120 |
return array_merge( $posts_templates, $this->get_template() ); |
| 121 |
} |
| 122 |
|
| 123 |
public function register_templates( $atts ) { |
| 124 |
|
| 125 |
foreach ( [ 'page', 'post' ] as $type ) { |
| 126 |
// Create the key used for the themes cache |
| 127 |
$cache_key = $type . '_templates-' . md5( get_theme_root() . '/' . get_stylesheet() ); |
| 128 |
|
| 129 |
// Retrieve the cache list. |
| 130 |
// If it doesn't exist, or it's empty prepare an array. |
| 131 |
$templates = wp_get_theme()->{'get_' . $type . '_templates()'}; |
| 132 |
if ( empty( $templates ) ) { |
| 133 |
$templates = []; |
| 134 |
} |
| 135 |
|
| 136 |
// New cache, therefore remove the old one. |
| 137 |
wp_cache_delete( $cache_key, 'themes' ); |
| 138 |
|
| 139 |
// Now add our template to the list of templates by merging our templates. |
| 140 |
// with the existing templates array from the cache. |
| 141 |
$templates = array_merge( $templates, $this->get_template() ); |
| 142 |
|
| 143 |
// Add the modified cache to allow WordPress to pick it up for listing. |
| 144 |
// available templates. |
| 145 |
wp_cache_add( $cache_key, $templates, 'themes', 1800 ); |
| 146 |
} |
| 147 |
|
| 148 |
return $atts; |
| 149 |
} |
| 150 |
} |