wpr-conditions-manager.php
5 days ago
wpr-editor-hooks.php
5 days ago
wpr-render-templates.php
5 days ago
wpr-templates-actions.php
5 days ago
wpr-templates-category-filter.php
5 days ago
wpr-templates-library.php
5 days ago
wpr-templates-loop.php
5 days ago
wpr-templates-modal-popups.php
5 days ago
wpr-templates-shortcode.php
5 days ago
wpr-render-templates.php
314 lines
| 1 | <?php |
| 2 | namespace WprAddons\Admin\Includes; |
| 3 | |
| 4 | use WprAddons\Classes\Utilities; |
| 5 | use Elementor\Core\Base\Elements_Iteration_Actions\Assets; |
| 6 | use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * WPR_Render_Templates setup |
| 14 | * |
| 15 | * @since 1.0 |
| 16 | */ |
| 17 | class WPR_Render_Templates { |
| 18 | |
| 19 | /** |
| 20 | ** Instance of Elemenntor Frontend class. |
| 21 | * |
| 22 | ** @var \Elementor\Frontend() |
| 23 | */ |
| 24 | private static $elementor_instance; |
| 25 | |
| 26 | /** |
| 27 | ** Get Current Theme. |
| 28 | */ |
| 29 | public $current_theme; |
| 30 | |
| 31 | /** |
| 32 | ** Royal Themes Array. |
| 33 | */ |
| 34 | public $royal_themes; |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | ** Constructor |
| 39 | */ |
| 40 | public function __construct( $only_hf = false ) { |
| 41 | |
| 42 | // Elementor Frontend |
| 43 | self::$elementor_instance = \Elementor\Plugin::instance(); |
| 44 | |
| 45 | // Ative Theme |
| 46 | $this->current_theme = get_template(); |
| 47 | |
| 48 | // Royal Themes |
| 49 | $this->royal_themes = ['ashe', 'ashe-pro', 'ashe-pro-premium', 'bard', 'bard-pro', 'bard-pro-premium']; |
| 50 | |
| 51 | // Popular Themes |
| 52 | if ( 'astra' === $this->current_theme ) { |
| 53 | require_once(__DIR__ . '/../templates/views/astra/class-astra-compat.php'); |
| 54 | |
| 55 | } elseif ( 'generatepress' === $this->current_theme ) { |
| 56 | require_once(__DIR__ . '/../templates/views/generatepress/class-generatepress-compat.php'); |
| 57 | |
| 58 | } elseif ( 'oceanwp' === $this->current_theme ) { |
| 59 | require_once(__DIR__ . '/../templates/views/oceanwp/class-oceanwp-compat.php'); |
| 60 | |
| 61 | } elseif ( 'storefront' === $this->current_theme ) { |
| 62 | require_once(__DIR__ . '/../templates/views/storefront/class-storefront-compat.php'); |
| 63 | |
| 64 | // Other Themes |
| 65 | } else { |
| 66 | add_action( 'wp', [ $this, 'global_compatibility' ] ); |
| 67 | } |
| 68 | |
| 69 | // Scripts and Styles |
| 70 | add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 71 | |
| 72 | // Theme Builder |
| 73 | if ( !$only_hf ) { // Prevent Loading in Header or Footer Templates |
| 74 | add_filter( 'template_include', [ $this, 'convert_to_canvas' ], 12 ); // 12 after WP Pages and WooCommerce. |
| 75 | add_action( 'elementor/page_templates/canvas/wpr_print_content', [ $this, 'canvas_page_content_display' ] ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public function global_compatibility() { |
| 80 | add_action( 'get_header', [ $this, 'replace_header' ] ); |
| 81 | add_action( 'elementor/page_templates/canvas/before_content', [ $this, 'add_canvas_header' ] ); |
| 82 | |
| 83 | add_action( 'get_footer', [ $this, 'replace_footer' ] ); |
| 84 | add_action( 'elementor/page_templates/canvas/after_content', [ $this, 'add_canvas_footer' ], 9 ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | ** Check if a Template has Conditions |
| 89 | */ |
| 90 | public function is_template_available( $type ) { |
| 91 | if ( 'content' === $type ) { |
| 92 | return !is_null(WPR_Conditions_Manager::canvas_page_content_display_conditions()) ? true : false; |
| 93 | } else { |
| 94 | $conditions = json_decode( get_option('wpr_'. $type .'_conditions', '[]'), true ); |
| 95 | $template = WPR_Conditions_Manager::header_footer_display_conditions( $conditions ); |
| 96 | |
| 97 | return (!empty( $conditions ) && !is_null($template)) ? true : false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | ** Header |
| 103 | */ |
| 104 | public function replace_header() { |
| 105 | if ( $this->is_template_available('header') ) { |
| 106 | if ( ! in_array($this->current_theme, $this->royal_themes) ) { |
| 107 | require __DIR__ . '/../templates/views/theme-header.php'; |
| 108 | } else { |
| 109 | require __DIR__ . '/../templates/views/royal/theme-header-royal.php'; |
| 110 | } |
| 111 | |
| 112 | $templates = []; |
| 113 | $templates[] = 'header.php'; |
| 114 | |
| 115 | remove_all_actions( 'wp_head' ); // Avoid running wp_head hooks again. |
| 116 | |
| 117 | ob_start(); |
| 118 | locate_template( $templates, true ); |
| 119 | ob_get_clean(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public function add_canvas_header() { |
| 124 | if ( $this->is_template_available('header') ) { |
| 125 | $conditions = json_decode( get_option('wpr_header_conditions', '[]'), true ); |
| 126 | $template_slug = WPR_Conditions_Manager::header_footer_display_conditions($conditions); |
| 127 | $template_id = Utilities::get_template_id($template_slug); |
| 128 | |
| 129 | $show_on_canvas = get_post_meta($template_id, 'wpr_header_show_on_canvas', true); |
| 130 | |
| 131 | if ( defined('ICL_LANGUAGE_CODE') ) { |
| 132 | $default_language_code = apply_filters('wpml_default_language', null); |
| 133 | $current_language_code = apply_filters('wpml_current_language', null); |
| 134 | |
| 135 | if ( $current_language_code && $current_language_code !== $default_language_code ) { |
| 136 | $show_on_canvas = 'true'; |
| 137 | $translated_id = apply_filters('wpml_object_id', $template_id, 'wpr_templates', true, $current_language_code); |
| 138 | if ( $translated_id && (int) $translated_id !== (int) $template_id ) { |
| 139 | $template_id = $translated_id; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // if ( !empty($show_on_canvas) && 'true' === $show_on_canvas && 0 === strpos($template_slug, 'user-header-') ) { |
| 145 | if ( !empty($show_on_canvas) && 'true' === $show_on_canvas && !is_null($template_slug) ) { |
| 146 | Utilities::render_elementor_template($template_slug, $template_id); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | ** Footer |
| 153 | */ |
| 154 | public function replace_footer() { |
| 155 | if ( $this->is_template_available('footer') ) { |
| 156 | if ( ! in_array($this->current_theme, $this->royal_themes) ) { |
| 157 | require __DIR__ . '/../templates/views/theme-footer.php'; |
| 158 | } else { |
| 159 | require __DIR__ . '/../templates/views/royal/theme-footer-royal.php'; |
| 160 | } |
| 161 | |
| 162 | $templates = []; |
| 163 | $templates[] = 'footer.php'; |
| 164 | |
| 165 | remove_all_actions( 'wp_footer' ); // Avoid running wp_footer hooks again. |
| 166 | |
| 167 | ob_start(); |
| 168 | locate_template( $templates, true ); |
| 169 | ob_get_clean(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | public function add_canvas_footer() { |
| 174 | if ( $this->is_template_available('footer') ) { |
| 175 | $conditions = json_decode( get_option('wpr_footer_conditions', '[]'), true ); |
| 176 | $template_slug = WPR_Conditions_Manager::header_footer_display_conditions($conditions); |
| 177 | $template_id = Utilities::get_template_id($template_slug); |
| 178 | |
| 179 | $show_on_canvas = get_post_meta($template_id, 'wpr_footer_show_on_canvas', true); |
| 180 | |
| 181 | if ( defined('ICL_LANGUAGE_CODE') ) { |
| 182 | $default_language_code = apply_filters('wpml_default_language', null); |
| 183 | $current_language_code = apply_filters('wpml_current_language', null); |
| 184 | |
| 185 | if ( $current_language_code && $current_language_code !== $default_language_code ) { |
| 186 | $show_on_canvas = 'true'; |
| 187 | $translated_id = apply_filters('wpml_object_id', $template_id, 'wpr_templates', true, $current_language_code); |
| 188 | if ( $translated_id && (int) $translated_id !== (int) $template_id ) { |
| 189 | $template_id = $translated_id; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // if ( !empty($show_on_canvas) && 'true' === $show_on_canvas && 0 === strpos($template_slug, 'user-footer-') ) { |
| 195 | if ( !empty($show_on_canvas) && 'true' === $show_on_canvas && !is_null($template_slug) ) { |
| 196 | Utilities::render_elementor_template($template_slug, $template_id); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | public function convert_to_canvas( $template ) { |
| 202 | $is_theme_builder_edit = \Elementor\Plugin::$instance->preview->is_preview_mode() && Utilities::is_theme_builder_template() ? true : false; |
| 203 | $_wp_page_template = get_post_meta(get_the_ID(), '_wp_page_template', true); |
| 204 | |
| 205 | if ( $this->is_template_available('content') || $is_theme_builder_edit ) { |
| 206 | if ( (is_page() || is_single()) && 'elementor_canvas' === $_wp_page_template && !$is_theme_builder_edit ) { |
| 207 | return $template; |
| 208 | } else { |
| 209 | return WPR_ADDONS_PATH . 'admin/templates/wpr-canvas.php'; |
| 210 | } |
| 211 | } else { |
| 212 | return $template; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | ** Theme Builder Content Display |
| 218 | */ |
| 219 | public function canvas_page_content_display() { |
| 220 | // Get Template |
| 221 | $template = WPR_Conditions_Manager::canvas_page_content_display_conditions(); |
| 222 | |
| 223 | // Display Template |
| 224 | Utilities::render_elementor_template( $template ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Enqueue styles and scripts. |
| 229 | */ |
| 230 | public function enqueue_scripts() { |
| 231 | |
| 232 | if ( !class_exists( '\Elementor\Plugin' ) ) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | // Elementor Frontend // Maybe we dont need this (should be extra, because we have everything below) |
| 237 | // self::$elementor_instance->frontend->enqueue_styles(); |
| 238 | |
| 239 | // if ( class_exists( '\ElementorPro\Plugin' ) ) { |
| 240 | // $elementor_pro = \ElementorPro\Plugin::instance(); |
| 241 | // $elementor_pro->enqueue_frontend_scripts(); |
| 242 | // } |
| 243 | |
| 244 | // Load Header Template CSS File |
| 245 | $heder_conditions = json_decode( get_option('wpr_header_conditions', '[]'), true ); |
| 246 | $heder_template = WPR_Conditions_Manager::header_footer_display_conditions($heder_conditions); |
| 247 | $header_template_id = !is_null($heder_template) ? Utilities::get_template_id($heder_template) : false; |
| 248 | |
| 249 | if ( false !== $header_template_id ) { |
| 250 | |
| 251 | // Load Header Template Assets (Elementor Widget) |
| 252 | if ( ! self::$elementor_instance->preview->is_preview_mode() ) { |
| 253 | $page_assets = get_post_meta( $header_template_id, Assets::ASSETS_META_KEY, true ); |
| 254 | if ( ! empty( $page_assets ) ) { |
| 255 | self::$elementor_instance->assets_loader->enable_assets( $page_assets ); |
| 256 | } |
| 257 | |
| 258 | $css_file = Post_CSS::create( get_the_ID() ); |
| 259 | $css_file->enqueue(); |
| 260 | } |
| 261 | |
| 262 | // Header Template CSS File |
| 263 | if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) { |
| 264 | $header_css_file = new \Elementor\Core\Files\CSS\Post( $header_template_id ); |
| 265 | } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) { |
| 266 | $header_css_file = new \Elementor\Post_CSS_File( $header_template_id ); |
| 267 | } |
| 268 | |
| 269 | $header_css_file->enqueue(); |
| 270 | } |
| 271 | |
| 272 | // Load Footer Template CSS File |
| 273 | $footer_conditions = json_decode( get_option('wpr_footer_conditions', '[]'), true ); |
| 274 | $footer_template = WPR_Conditions_Manager::header_footer_display_conditions($footer_conditions); |
| 275 | $footer_template_id = !is_null($footer_template) ? Utilities::get_template_id($footer_template) : false; |
| 276 | |
| 277 | if ( false !== $footer_template_id ) { |
| 278 | // Load Footer Template Assets (Elementor Widget) |
| 279 | if ( ! self::$elementor_instance->preview->is_preview_mode() ) { |
| 280 | $page_assets = get_post_meta( $footer_template_id, Assets::ASSETS_META_KEY, true ); |
| 281 | if ( ! empty( $page_assets ) ) { |
| 282 | self::$elementor_instance->assets_loader->enable_assets( $page_assets ); |
| 283 | } |
| 284 | |
| 285 | $css_file = Post_CSS::create( get_the_ID() ); |
| 286 | $css_file->enqueue(); |
| 287 | } |
| 288 | |
| 289 | // Footer Template CSS File |
| 290 | if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) { |
| 291 | $footer_css_file = new \Elementor\Core\Files\CSS\Post( $footer_template_id ); |
| 292 | } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) { |
| 293 | $footer_css_file = new \Elementor\Post_CSS_File( $footer_template_id ); |
| 294 | } |
| 295 | |
| 296 | $footer_css_file->enqueue(); |
| 297 | } |
| 298 | |
| 299 | // Load Canvas Content Template CSS File |
| 300 | $canvas_conditions = WPR_Conditions_Manager::canvas_page_content_display_conditions(); |
| 301 | $canvas_template_id = !empty($canvas_conditions) ? Utilities::get_template_id($canvas_conditions) : false; |
| 302 | |
| 303 | if ( false !== $canvas_template_id ) { |
| 304 | if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) { |
| 305 | $footer_css_file = new \Elementor\Core\Files\CSS\Post( $canvas_template_id ); |
| 306 | } elseif ( class_exists( '\Elementor\Post_CSS_File' ) ) { |
| 307 | $footer_css_file = new \Elementor\Post_CSS_File( $canvas_template_id ); |
| 308 | } |
| 309 | |
| 310 | $footer_css_file->enqueue(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | } |