| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Builder\Managers; |
| 4 |
|
| 5 |
use Templately\Builder\PageTemplates; |
| 6 |
use Templately\Builder\Source; |
| 7 |
use Templately\Builder\ThemeBuilder; |
| 8 |
use Templately\Builder\Types\BaseTemplate; |
| 9 |
use Templately\Builder\Types\ThemeTemplate; |
| 10 |
|
| 11 |
class LocationManager { |
| 12 |
/** |
| 13 |
* @var array<string, ThemeTemplate> |
| 14 |
*/ |
| 15 |
public $locations_queue = []; |
| 16 |
public $locations_skipped = []; |
| 17 |
public $locations_printed = []; |
| 18 |
public $did_locations = []; |
| 19 |
|
| 20 |
protected $locations = []; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var ThemeBuilder |
| 24 |
*/ |
| 25 |
protected $builder; |
| 26 |
|
| 27 |
public function __construct( $builder ) { |
| 28 |
$this->builder = $builder; |
| 29 |
|
| 30 |
/** |
| 31 |
* Don't know yet if we need it or not. |
| 32 |
*/ |
| 33 |
$this->set_locations(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Priority is 13, |
| 37 |
* Because it should be run after elementor & woocommerce |
| 38 |
*/ |
| 39 |
add_filter( 'template_include', [ $this, 'template_include' ], 13 ); |
| 40 |
} |
| 41 |
|
| 42 |
private function set_locations() { |
| 43 |
$this->locations = [ |
| 44 |
'header' => [ |
| 45 |
'label' => __( 'Header', 'templately' ), |
| 46 |
'multiple' => false |
| 47 |
], |
| 48 |
'footer' => [ |
| 49 |
'label' => __( 'Footer', 'templately' ), |
| 50 |
'multiple' => false |
| 51 |
], |
| 52 |
'archive' => [ |
| 53 |
'label' => __( 'Archive', 'templately' ), |
| 54 |
'multiple' => false |
| 55 |
], |
| 56 |
'single' => [ |
| 57 |
'label' => __( 'Single', 'templately' ), |
| 58 |
'multiple' => false |
| 59 |
] |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
public function template_include( $template_path ) { |
| 64 |
$location = ''; |
| 65 |
$page_template_module = $this->get_template_module(); |
| 66 |
|
| 67 |
/** |
| 68 |
* Return if it is Elementor Template. |
| 69 |
* This should be elementor's responsibility. |
| 70 |
*/ |
| 71 |
|
| 72 |
if ( get_post_type( get_the_ID() ) === 'elementor_library' ) { |
| 73 |
return $template_path; |
| 74 |
} |
| 75 |
else if( $this->get_platform(get_the_ID()) === 'elementor' && !class_exists('Elementor\Plugin') ) { |
| 76 |
return $template_path; |
| 77 |
} |
| 78 |
|
| 79 |
if ( is_singular() ) { |
| 80 |
/** |
| 81 |
* @var BaseTemplate $template |
| 82 |
*/ |
| 83 |
$template = $this->builder::$templates_manager->get( get_the_ID() ); |
| 84 |
|
| 85 |
if ( $template && $template->get_property( 'support_wp_page_templates' ) ) { |
| 86 |
$page_template_module->set_platform( $template->get_platform() ); |
| 87 |
$wp_page_template = $template->get_meta( '_wp_page_template' ); |
| 88 |
|
| 89 |
$_custom_template_path = $page_template_module->get_template_path( $wp_page_template ); |
| 90 |
|
| 91 |
|
| 92 |
if ( empty( $_custom_template_path ) ) { |
| 93 |
$location = 'single'; |
| 94 |
|
| 95 |
$templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location ); |
| 96 |
|
| 97 |
if ( empty( $templates_for_location ) ) { |
| 98 |
return $template_path; |
| 99 |
} |
| 100 |
|
| 101 |
$template_id = key( $templates_for_location ); |
| 102 |
|
| 103 |
$template = $templates_for_location[ $template_id ]; |
| 104 |
$page_template = $template->get_meta( '_wp_page_template' ); |
| 105 |
$platform = $template->get_platform(); |
| 106 |
|
| 107 |
if ( ! empty( $platform ) ) { |
| 108 |
$page_template_module->set_platform( $platform ); |
| 109 |
} |
| 110 |
$path = $page_template_module->get_template_path( $page_template ); |
| 111 |
$page_template_module->set_print_callback( function () use ( $location ) { |
| 112 |
$this->do_location( $location ); |
| 113 |
} ); |
| 114 |
set_query_var( 'using_templately_template', 1 ); |
| 115 |
|
| 116 |
return $path; |
| 117 |
} |
| 118 |
|
| 119 |
if ( $wp_page_template && $wp_page_template !== 'default' ) { |
| 120 |
set_query_var( 'using_templately_template', 1 ); |
| 121 |
|
| 122 |
return $_custom_template_path; |
| 123 |
} |
| 124 |
} |
| 125 |
} else { |
| 126 |
$template = false; |
| 127 |
} |
| 128 |
|
| 129 |
if ( $template instanceof ThemeTemplate ) { |
| 130 |
$location = $template->get_location(); |
| 131 |
} elseif ( function_exists( 'is_shop' ) && is_shop() ) { |
| 132 |
$location = 'archive'; |
| 133 |
} elseif ( is_archive() || is_tax() || is_home() || is_search() ) { |
| 134 |
$location = 'archive'; |
| 135 |
} elseif ( is_singular() || is_404() ) { |
| 136 |
$location = 'single'; |
| 137 |
} |
| 138 |
|
| 139 |
if ( $location ) { |
| 140 |
$templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location ); |
| 141 |
|
| 142 |
if ( empty( $templates_for_location ) ) { |
| 143 |
set_query_var( 'using_templately_template', 1 ); |
| 144 |
|
| 145 |
return $template_path; |
| 146 |
} |
| 147 |
|
| 148 |
if ( 'single' === $location || 'archive' === $location ) { |
| 149 |
$template_id = key( $templates_for_location ); |
| 150 |
$template = $templates_for_location[ $template_id ]; |
| 151 |
$document_page_template = $template->get_meta( '_wp_page_template' ); |
| 152 |
$platform = $template->get_platform(); |
| 153 |
|
| 154 |
if ( ! empty( $platform ) ) { |
| 155 |
$page_template_module->set_platform( $platform ); |
| 156 |
} |
| 157 |
|
| 158 |
if ( $document_page_template ) { |
| 159 |
$page_template = $document_page_template; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
$is_header_footer = 'header' === $location || 'footer' === $location; |
| 164 |
if ( empty( $page_template ) && ! $is_header_footer ) { |
| 165 |
$page_template = $page_template_module->get_header_footer_template(); |
| 166 |
} |
| 167 |
|
| 168 |
if ( ! empty( $page_template ) ) { |
| 169 |
$path = $page_template_module->get_template_path( $page_template ); |
| 170 |
|
| 171 |
if ( $path ) { |
| 172 |
$page_template_module->set_print_callback( function () use ( $location ) { |
| 173 |
$this->do_location( $location ); |
| 174 |
} ); |
| 175 |
set_query_var( 'using_templately_template', 1 ); |
| 176 |
$template_path = $path; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return $template_path; |
| 181 |
} |
| 182 |
|
| 183 |
private function get_platform($post_id) { |
| 184 |
$post_type = get_post_type( get_the_ID() ); |
| 185 |
if ( $post_type == Source::CPT ) { |
| 186 |
$platform = get_post_meta( $post_id, Source::PLATFORM_META_KEY, true ); |
| 187 |
} elseif ( get_post_meta( $post_id, '_elementor_edit_mode', true ) == 'builder' || $post_type == 'elementor_library' ) { |
| 188 |
$platform = 'elementor'; |
| 189 |
} else { |
| 190 |
$platform = 'gutenberg'; |
| 191 |
} |
| 192 |
return $platform; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get page templating modules and set platform if needed. |
| 197 |
* |
| 198 |
* @param string $platform |
| 199 |
* |
| 200 |
* @return PageTemplates |
| 201 |
*/ |
| 202 |
private function get_template_module( string $platform = '' ): PageTemplates { |
| 203 |
$module = templately()->theme_builder::$page_template_module; |
| 204 |
|
| 205 |
if ( ! empty( $platform ) ) { |
| 206 |
$module = $module->set_platform( $platform ); |
| 207 |
} |
| 208 |
|
| 209 |
return $module; |
| 210 |
} |
| 211 |
|
| 212 |
public function get_location( $location ) { |
| 213 |
$locations = $this->get_locations(); |
| 214 |
|
| 215 |
return $locations[ $location ] ?? []; |
| 216 |
} |
| 217 |
|
| 218 |
public function get_locations(): array { |
| 219 |
$this->register_locations(); |
| 220 |
|
| 221 |
return $this->locations; |
| 222 |
} |
| 223 |
|
| 224 |
public function register_locations() { |
| 225 |
if ( ! did_action( 'templately_locations' ) ) { |
| 226 |
do_action( 'templately_locations', $this ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Getting the Idea From Elementor itself. |
| 232 |
* |
| 233 |
* @param $location |
| 234 |
* |
| 235 |
* @return bool |
| 236 |
*/ |
| 237 |
public function do_location( $location ): bool { |
| 238 |
$templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location ); |
| 239 |
|
| 240 |
foreach ( $templates_for_location as $template_id => $template ) { |
| 241 |
$this->add_template_to_location( $location, $template_id ); |
| 242 |
} |
| 243 |
|
| 244 |
if ( empty( $this->locations_queue[ $location ] ) ) { |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
while ( ! empty( $this->locations_queue[ $location ] ) ) { |
| 249 |
$template_id = key( $this->locations_queue[ $location ] ); |
| 250 |
$template = $this->builder->get_template( $template_id ); |
| 251 |
|
| 252 |
if ( ! $template || $this->is_printed( $location, $template_id ) ) { |
| 253 |
$this->skip_template_from_location( $location, $template_id ); |
| 254 |
continue; |
| 255 |
} |
| 256 |
|
| 257 |
if ( empty( $documents_by_conditions[ $template_id ] ) ) { |
| 258 |
$post_status = get_post_status( $template_id ); |
| 259 |
if ( 'publish' !== $post_status ) { |
| 260 |
$this->skip_template_from_location( $location, $template_id ); |
| 261 |
continue; |
| 262 |
} |
| 263 |
} |
| 264 |
$template->print_content(); |
| 265 |
$this->did_locations[] = $location; |
| 266 |
|
| 267 |
$this->set_is_printed( $location, $template_id ); |
| 268 |
} |
| 269 |
|
| 270 |
return true; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* @param string $location |
| 275 |
* @param integer $template_id |
| 276 |
*/ |
| 277 |
public function add_template_to_location( string $location, int $template_id ) { |
| 278 |
if ( isset( $this->locations_skipped[ $location ][ $template_id ] ) ) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
if ( ! isset( $this->locations_queue[ $location ] ) ) { |
| 283 |
$this->locations_queue[ $location ] = []; |
| 284 |
} |
| 285 |
|
| 286 |
$this->locations_queue[ $location ][ $template_id ] = $template_id; |
| 287 |
} |
| 288 |
|
| 289 |
public function is_printed( $location, $template_id ): bool { |
| 290 |
return isset( $this->locations_printed[ $location ][ $template_id ] ); |
| 291 |
} |
| 292 |
|
| 293 |
public function skip_template_from_location( $location, $template_id ) { |
| 294 |
$this->remove_template_from_location( $location, $template_id ); |
| 295 |
|
| 296 |
if ( ! isset( $this->locations_skipped[ $location ] ) ) { |
| 297 |
$this->locations_skipped[ $location ] = []; |
| 298 |
} |
| 299 |
|
| 300 |
$this->locations_skipped[ $location ][ $template_id ] = $template_id; |
| 301 |
} |
| 302 |
|
| 303 |
public function remove_template_from_location( $location, $template_id ) { |
| 304 |
unset( $this->locations_queue[ $location ][ $template_id ] ); |
| 305 |
} |
| 306 |
|
| 307 |
public function set_is_printed( $location, $template_id ) { |
| 308 |
if ( ! isset( $this->locations_printed[ $location ] ) ) { |
| 309 |
$this->locations_printed[ $location ] = []; |
| 310 |
} |
| 311 |
|
| 312 |
$this->locations_printed[ $location ][ $template_id ] = $template_id; |
| 313 |
$this->remove_template_from_location( $location, $template_id ); |
| 314 |
} |
| 315 |
} |