preview-manager.php
77 lines
| 1 | <?php |
| 2 | namespace Auxin\Plugin\CoreElements\Elementor\Modules\ThemeBuilder\Classes; |
| 3 | |
| 4 | use Elementor\Plugin; |
| 5 | use Auxin\Plugin\CoreElements\Elementor\Modules\ThemeBuilder\Module; |
| 6 | use Auxin\Plugin\CoreElements\Elementor\Modules\ThemeBuilder\Theme_Document; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly |
| 10 | } |
| 11 | |
| 12 | class Preview_Manager { |
| 13 | |
| 14 | public function __construct() { |
| 15 | add_filter( 'elementor/query_control/get_query_args/current_query', [ $this, 'filter_query_control_args' ] ); |
| 16 | add_filter( 'elementor/theme/posts_archive/query_posts/query_vars', [ $this, 'filter_query_control_args' ] ); |
| 17 | add_filter( 'elementor/dynamic_tags/post_terms/taxonomy_args', [ $this, 'filter_post_terms_taxonomy_arg' ] ); |
| 18 | |
| 19 | add_action( 'elementor/template-library/before_get_source_data', [ $this, 'switch_to_preview_query' ] ); |
| 20 | add_action( 'elementor/template-library/after_get_source_data', [ $this, 'restore_current_query' ] ); |
| 21 | add_action( 'elementor/dynamic_tags/before_render', [ $this, 'switch_to_preview_query' ] ); |
| 22 | add_action( 'elementor/dynamic_tags/after_render', [ $this, 'restore_current_query' ] ); |
| 23 | } |
| 24 | |
| 25 | public function filter_post_terms_taxonomy_arg( $taxonomy_args ) { |
| 26 | $current_post_id = get_the_ID(); |
| 27 | $document = Module::instance()->get_document( $current_post_id ); |
| 28 | |
| 29 | if ( $document ) { |
| 30 | // Show all taxonomies |
| 31 | unset( $taxonomy_args['object_type'] ); |
| 32 | } |
| 33 | |
| 34 | return $taxonomy_args; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @access public |
| 39 | * |
| 40 | * @param $query_vars array |
| 41 | * |
| 42 | * @return array |
| 43 | */ |
| 44 | public function filter_query_control_args( $query_vars ) { |
| 45 | $document = Plugin::$instance->documents->get_doc_or_auto_save( get_the_ID() ); |
| 46 | |
| 47 | if ( $document && $document instanceof Theme_Document ) { |
| 48 | $query_vars = $document->get_preview_as_query_args(); |
| 49 | } |
| 50 | |
| 51 | return $query_vars; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @access public |
| 56 | */ |
| 57 | public function switch_to_preview_query() { |
| 58 | $current_post_id = get_the_ID(); |
| 59 | $document = Plugin::$instance->documents->get_doc_or_auto_save( $current_post_id ); |
| 60 | |
| 61 | if ( ! $document || ! $document instanceof Theme_Document ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | $new_query_vars = $document->get_preview_as_query_args(); |
| 66 | |
| 67 | Plugin::$instance->db->switch_to_query( $new_query_vars, true ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @access public |
| 72 | */ |
| 73 | public function restore_current_query() { |
| 74 | Plugin::$instance->db->restore_current_query(); |
| 75 | } |
| 76 | } |
| 77 |