| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | if ( ! defined( 'ABSPATH' ) ) { |
| 13: | exit; |
| 14: | } |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | class EPL_Elementor_Template_Router { |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | private static $rendering_cards = array(); |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | private static $css_rendered = array(); |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | private static $card_override_stack = array(); |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | private static $card_listener = null; |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | private static $active_archive_page_id = 0; |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | private $single_template_cache = array(); |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | public function __construct() { |
| 94: | add_action( 'wp', array( $this, 'maybe_route_single' ), 20 ); |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | add_action( 'init', array( $this, 'hook_card_override_listener' ), 20 ); |
| 103: | add_filter( 'template_include', array( $this, 'maybe_route_archive' ), 10 ); |
| 104: | add_filter( 'epl_elementor_preview_listing_id', array( $this, 'filter_preview_listing_id' ) ); |
| 105: | } |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | private static function pro_theme_builder_wins( $location ) { |
| 116: | return function_exists( 'elementor_location_exits' ) && elementor_location_exits( $location, true ); |
| 117: | } |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | public static function begin_card_override( $override ) { |
| 135: | if ( empty( self::$card_override_stack ) && self::$card_listener ) { |
| 136: | add_action( 'epl_loop_template_listing', self::$card_listener, 10, 1 ); |
| 137: | } |
| 138: | self::$card_override_stack[] = is_array( $override ) ? $override : absint( $override ); |
| 139: | } |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | public static function end_card_override() { |
| 147: | array_pop( self::$card_override_stack ); |
| 148: | if ( empty( self::$card_override_stack ) && self::$card_listener ) { |
| 149: | remove_action( 'epl_loop_template_listing', self::$card_listener, 10 ); |
| 150: | } |
| 151: | } |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | private static function get_card_override( $post_type = '' ) { |
| 161: | if ( empty( self::$card_override_stack ) ) { |
| 162: | return 0; |
| 163: | } |
| 164: | |
| 165: | $override = end( self::$card_override_stack ); |
| 166: | |
| 167: | if ( ! is_array( $override ) ) { |
| 168: | return (int) $override; |
| 169: | } |
| 170: | |
| 171: | if ( $post_type && ! empty( $override[ $post_type ] ) ) { |
| 172: | return absint( $override[ $post_type ] ); |
| 173: | } |
| 174: | |
| 175: | return ! empty( $override['_default'] ) ? absint( $override['_default'] ) : 0; |
| 176: | } |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | public function maybe_route_single() { |
| 188: | if ( EPL_Elementor::is_editor() || ! function_exists( 'is_epl_post_single' ) || ! is_epl_post_single() ) { |
| 189: | return; |
| 190: | } |
| 191: | |
| 192: | $post = get_post(); |
| 193: | if ( ! $post || self::pro_theme_builder_wins( 'single' ) ) { |
| 194: | return; |
| 195: | } |
| 196: | |
| 197: | $template_id = $this->get_single_template_id( $post->post_type ); |
| 198: | if ( ! $template_id || ! apply_filters( 'epl_elementor_single_template_enabled', true, $template_id, $post->post_type ) ) { |
| 199: | return; |
| 200: | } |
| 201: | |
| 202: | add_action( |
| 203: | 'wp_enqueue_scripts', |
| 204: | function () use ( $template_id ) { |
| 205: | \Elementor\Core\Files\CSS\Post::create( $template_id )->enqueue(); |
| 206: | } |
| 207: | ); |
| 208: | |
| 209: | add_action( |
| 210: | 'epl_single_template', |
| 211: | function () use ( $template_id ) { |
| 212: | echo '<div class="epl-elementor-single-template">'; |
| 213: | echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $template_id, false ); |
| 214: | echo '</div>'; |
| 215: | } |
| 216: | ); |
| 217: | } |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | private function get_single_template_id( $post_type ) { |
| 232: | if ( isset( $this->single_template_cache[ $post_type ] ) ) { |
| 233: | return $this->single_template_cache[ $post_type ]; |
| 234: | } |
| 235: | |
| 236: | $forced = absint( apply_filters( 'epl_elementor_single_template_id', 0, $post_type ) ); |
| 237: | if ( $forced ) { |
| 238: | $this->single_template_cache[ $post_type ] = $forced; |
| 239: | return $forced; |
| 240: | } |
| 241: | |
| 242: | $candidates = get_posts( |
| 243: | array( |
| 244: | 'post_type' => 'elementor_library', |
| 245: | 'post_status' => 'publish', |
| 246: | 'posts_per_page' => -1, |
| 247: | 'orderby' => 'modified', |
| 248: | 'order' => 'DESC', |
| 249: | 'fields' => 'ids', |
| 250: | 'meta_query' => array( |
| 251: | array( |
| 252: | 'key' => '_elementor_template_type', |
| 253: | 'value' => EPL_Elementor_Single_Document::TYPE, |
| 254: | ), |
| 255: | ), |
| 256: | ) |
| 257: | ); |
| 258: | |
| 259: | $fallback = 0; |
| 260: | foreach ( $candidates as $candidate_id ) { |
| 261: | |
| 262: | if ( ! user_can( (int) get_post_field( 'post_author', $candidate_id ), 'edit_theme_options' ) ) { |
| 263: | continue; |
| 264: | } |
| 265: | |
| 266: | $applies_to = (array) get_post_meta( $candidate_id, '_epl_apply_post_types', true ); |
| 267: | |
| 268: | if ( in_array( $post_type, $applies_to, true ) ) { |
| 269: | $this->single_template_cache[ $post_type ] = (int) $candidate_id; |
| 270: | return $this->single_template_cache[ $post_type ]; |
| 271: | } |
| 272: | |
| 273: | if ( ! $fallback && empty( $applies_to ) ) { |
| 274: | $fallback = (int) $candidate_id; |
| 275: | } |
| 276: | } |
| 277: | |
| 278: | $this->single_template_cache[ $post_type ] = $fallback; |
| 279: | return $fallback; |
| 280: | } |
| 281: | |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | |
| 298: | |
| 299: | public static function render_card( $template_id ) { |
| 300: | $template_id = absint( $template_id ); |
| 301: | |
| 302: | if ( ! self::is_renderable_card( $template_id ) || isset( self::$rendering_cards[ $template_id ] ) ) { |
| 303: | return false; |
| 304: | } |
| 305: | |
| 306: | self::$rendering_cards[ $template_id ] = true; |
| 307: | |
| 308: | $with_css = empty( self::$css_rendered[ $template_id ] ); |
| 309: | self::$css_rendered[ $template_id ] = true; |
| 310: | |
| 311: | if ( $with_css ) { |
| 312: | \Elementor\Core\Files\CSS\Post::create( $template_id )->enqueue(); |
| 313: | } |
| 314: | |
| 315: | |
| 316: | |
| 317: | |
| 318: | add_filter( 'elementor/element/should_render_shortcode', '__return_false' ); |
| 319: | echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $template_id, false ); |
| 320: | remove_filter( 'elementor/element/should_render_shortcode', '__return_false' ); |
| 321: | |
| 322: | unset( self::$rendering_cards[ $template_id ] ); |
| 323: | |
| 324: | return true; |
| 325: | } |
| 326: | |
| 327: | |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | |
| 336: | |
| 337: | public static function is_renderable_card( $template_id ) { |
| 338: | $template_id = absint( $template_id ); |
| 339: | |
| 340: | if ( ! $template_id || 'publish' !== get_post_status( $template_id ) ) { |
| 341: | return false; |
| 342: | } |
| 343: | |
| 344: | return EPL_Elementor_Loop_Card_Document::TYPE === get_post_meta( $template_id, '_elementor_template_type', true ); |
| 345: | } |
| 346: | |
| 347: | |
| 348: | |
| 349: | |
| 350: | |
| 351: | |
| 352: | |
| 353: | |
| 354: | |
| 355: | |
| 356: | public function hook_card_override_listener() { |
| 357: | |
| 358: | |
| 359: | self::$card_listener = array( $this, 'render_card_override' ); |
| 360: | } |
| 361: | |
| 362: | |
| 363: | |
| 364: | |
| 365: | |
| 366: | |
| 367: | |
| 368: | |
| 369: | |
| 370: | public function render_card_override( $vars ) { |
| 371: | $post_type = isset( $vars['property']->post->post_type ) ? $vars['property']->post->post_type : ''; |
| 372: | $override = self::get_card_override( $post_type ); |
| 373: | if ( $override && self::render_card( $override ) ) { |
| 374: | return; |
| 375: | } |
| 376: | |
| 377: | $this->render_php_fallback( $vars ); |
| 378: | } |
| 379: | |
| 380: | |
| 381: | |
| 382: | |
| 383: | |
| 384: | |
| 385: | |
| 386: | private function render_php_fallback( $vars ) { |
| 387: | $template = isset( $vars['template'] ) ? $vars['template'] : 'default'; |
| 388: | $default_value = isset( $vars['default_value'] ) ? $vars['default_value'] : 'default'; |
| 389: | $template_name = apply_filters( 'epl_property_blog_template', 'loop-listing-blog-' . $template . '.php', $vars ); |
| 390: | epl_get_template_part( $template_name, array(), 'loop-listing-blog-' . $default_value . '.php' ); |
| 391: | } |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: | public function filter_preview_listing_id( $requested_id ) { |
| 402: | if ( $requested_id || ! isset( \Elementor\Plugin::$instance->documents ) ) { |
| 403: | return $requested_id; |
| 404: | } |
| 405: | |
| 406: | $document = \Elementor\Plugin::$instance->documents->get_current(); |
| 407: | if ( ! $document || ! in_array( $document->get_name(), array( EPL_Elementor_Single_Document::TYPE, EPL_Elementor_Loop_Card_Document::TYPE ), true ) ) { |
| 408: | return $requested_id; |
| 409: | } |
| 410: | |
| 411: | return absint( $document->get_settings( 'epl_preview_listing' ) ); |
| 412: | } |
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | |
| 419: | |
| 420: | |
| 421: | |
| 422: | |
| 423: | |
| 424: | |
| 425: | public static function get_active_archive_page_id() { |
| 426: | return self::$active_archive_page_id; |
| 427: | } |
| 428: | |
| 429: | |
| 430: | |
| 431: | |
| 432: | |
| 433: | |
| 434: | |
| 435: | |
| 436: | |
| 437: | |
| 438: | |
| 439: | |
| 440: | |
| 441: | |
| 442: | public function maybe_route_archive( $template ) { |
| 443: | if ( EPL_Elementor::is_editor() ) { |
| 444: | return $template; |
| 445: | } |
| 446: | |
| 447: | $page_id = $this->resolve_archive_page_id(); |
| 448: | if ( ! $page_id || self::pro_theme_builder_wins( 'archive' ) ) { |
| 449: | return $template; |
| 450: | } |
| 451: | |
| 452: | self::$active_archive_page_id = $page_id; |
| 453: | |
| 454: | add_action( |
| 455: | 'wp_enqueue_scripts', |
| 456: | function () use ( $page_id ) { |
| 457: | if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) { |
| 458: | \Elementor\Core\Files\CSS\Post::create( $page_id )->enqueue(); |
| 459: | } |
| 460: | } |
| 461: | ); |
| 462: | |
| 463: | return EPL_PATH_TEMPLATES_POST_TYPES . 'elementor/archive-page.php'; |
| 464: | } |
| 465: | |
| 466: | |
| 467: | |
| 468: | |
| 469: | |
| 470: | |
| 471: | |
| 472: | |
| 473: | |
| 474: | |
| 475: | |
| 476: | |
| 477: | |
| 478: | private function get_queried_listing_type() { |
| 479: | $active = array_keys( epl_get_active_post_types() ); |
| 480: | |
| 481: | foreach ( $active as $post_type ) { |
| 482: | if ( is_post_type_archive( $post_type ) ) { |
| 483: | return $post_type; |
| 484: | } |
| 485: | } |
| 486: | |
| 487: | $queried = get_query_var( 'post_type' ); |
| 488: | $queried = array_values( array_intersect( array_filter( (array) $queried ), $active ) ); |
| 489: | |
| 490: | return ( 1 === count( $queried ) ) ? sanitize_key( $queried[0] ) : ''; |
| 491: | } |
| 492: | |
| 493: | |
| 494: | |
| 495: | |
| 496: | |
| 497: | |
| 498: | |
| 499: | |
| 500: | |
| 501: | private function resolve_archive_page_id() { |
| 502: | if ( 'yes' !== epl_get_option( 'epl_archive_page_enabled', 'no' ) ) { |
| 503: | return 0; |
| 504: | } |
| 505: | |
| 506: | $is_archive = function_exists( 'is_epl_post_archive' ) && is_epl_post_archive(); |
| 507: | $is_search = function_exists( 'epl_is_search' ) && epl_is_search() && 'yes' === epl_get_option( 'epl_archive_page_apply_search', 'yes' ); |
| 508: | $is_taxonomy = ( is_tax( 'location' ) || is_tax( 'tax_feature' ) || is_tax( 'tax_business_listing' ) ) && 'yes' === epl_get_option( 'epl_archive_page_apply_taxonomy', 'yes' ); |
| 509: | |
| 510: | if ( ! $is_archive && ! $is_search && ! $is_taxonomy ) { |
| 511: | return 0; |
| 512: | } |
| 513: | |
| 514: | |
| 515: | |
| 516: | $page_id = 0; |
| 517: | |
| 518: | $queried_type = $this->get_queried_listing_type(); |
| 519: | if ( $queried_type ) { |
| 520: | $page_id = absint( epl_get_option( 'epl_archive_page_id_' . $queried_type, '' ) ); |
| 521: | } |
| 522: | |
| 523: | if ( ! $page_id ) { |
| 524: | $page_id = absint( epl_get_option( 'epl_archive_page_id', '' ) ); |
| 525: | } |
| 526: | |
| 527: | $page_id = absint( apply_filters( 'epl_archive_page_id', $page_id, $queried_type ) ); |
| 528: | if ( ! $page_id ) { |
| 529: | return 0; |
| 530: | } |
| 531: | |
| 532: | $page = get_post( $page_id ); |
| 533: | if ( ! $page || 'page' !== $page->post_type || 'publish' !== $page->post_status || post_password_required( $page ) ) { |
| 534: | return 0; |
| 535: | } |
| 536: | |
| 537: | return $page_id; |
| 538: | } |
| 539: | } |
| 540: | |