| 1 |
<?php |
| 2 |
namespace ABlocksThemeBuilder; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocksThemeBuilder\Traits\RenderContent; |
| 9 |
|
| 10 |
abstract class AbstractCompatibilityBase { |
| 11 |
use RenderContent; |
| 12 |
protected $theme; |
| 13 |
protected $header_hook = 'ablocks/templates/theme_builder/header'; |
| 14 |
protected $footer_hook = 'ablocks/templates/theme_builder/footer'; |
| 15 |
protected $before_footer_hook = 'ablocks/templates/theme_builder/footer'; |
| 16 |
protected $enabled_header; |
| 17 |
protected $enabled_before_footer; |
| 18 |
protected $enabled_footer; |
| 19 |
|
| 20 |
public function __construct( $theme ) { |
| 21 |
$this->theme = $theme; |
| 22 |
} |
| 23 |
|
| 24 |
public function init() { |
| 25 |
$this->enabled_header = $this->enabled_header(); |
| 26 |
// Header |
| 27 |
if ( $this->enabled_header ) { |
| 28 |
add_action( 'get_header', [ $this, 'header_template' ] ); |
| 29 |
add_action( $this->header_hook, [ $this, 'render_header' ] ); |
| 30 |
} |
| 31 |
|
| 32 |
// Footer |
| 33 |
$this->enabled_footer = $this->enabled_footer(); |
| 34 |
$this->enabled_before_footer = $this->enabled_before_footer(); |
| 35 |
if ( $this->enabled_footer || $this->enabled_before_footer ) { |
| 36 |
add_action( 'get_footer', [ $this, 'footer_template' ] ); |
| 37 |
} |
| 38 |
// Before Footer |
| 39 |
if ( $this->enabled_before_footer ) { |
| 40 |
add_action( $this->before_footer_hook, [ $this, 'render_before_footer' ], 5 ); |
| 41 |
} |
| 42 |
if ( $this->enabled_footer ) { |
| 43 |
add_action( $this->footer_hook, [ $this, 'render_footer' ] ); |
| 44 |
} |
| 45 |
|
| 46 |
do_action('ablocks_theme_builder_after_dispatch', [ |
| 47 |
'header' => $this->enabled_header, |
| 48 |
'before_footer' => $this->enabled_before_footer, |
| 49 |
'footer' => $this->enabled_footer, |
| 50 |
]); |
| 51 |
} |
| 52 |
|
| 53 |
public function enabled_header() { |
| 54 |
return $this->get_settings( 'header' ); |
| 55 |
} |
| 56 |
|
| 57 |
public function enabled_footer() { |
| 58 |
return $this->get_settings( 'footer' ); |
| 59 |
} |
| 60 |
|
| 61 |
public function enabled_before_footer() { |
| 62 |
return $this->get_settings( 'before-footer' ); |
| 63 |
} |
| 64 |
|
| 65 |
public function get_settings( $type ) { |
| 66 |
return Helper::get_template_id( $type ); |
| 67 |
} |
| 68 |
|
| 69 |
public function header_template() { |
| 70 |
require ABLOCKS_ROOT_DIR_PATH . $this->get_template_path( 'header.php' ); |
| 71 |
$templates = []; |
| 72 |
$templates[] = 'header.php'; |
| 73 |
// Avoid running wp_head hooks again. |
| 74 |
remove_all_actions( 'wp_head' ); |
| 75 |
ob_start(); |
| 76 |
locate_template( $templates, true ); |
| 77 |
ob_get_clean(); |
| 78 |
} |
| 79 |
public function footer_template() { |
| 80 |
require ABLOCKS_ROOT_DIR_PATH . $this->get_template_path( 'footer.php' ); |
| 81 |
$templates = []; |
| 82 |
$templates[] = 'footer.php'; |
| 83 |
// Avoid running wp_footer hooks again. |
| 84 |
remove_all_actions( 'wp_footer' ); |
| 85 |
ob_start(); |
| 86 |
locate_template( $templates, true ); |
| 87 |
ob_get_clean(); |
| 88 |
} |
| 89 |
|
| 90 |
public function get_template_path( $suffix ) { |
| 91 |
$prefix = ! empty( $this->theme ) ? "{$this->theme}-" : ''; |
| 92 |
return "templates/theme-builder/{$prefix}{$suffix}"; |
| 93 |
} |
| 94 |
// To be overridden by specific themes |
| 95 |
abstract public function render_header(); |
| 96 |
abstract public function render_footer(); |
| 97 |
abstract public function render_before_footer(); |
| 98 |
} |
| 99 |
|