class-ele-builder-document-base.php
1 year ago
class-ele-builder-documents.php
1 year ago
class-ele-document-singular.php
1 year ago
class-ele-builder-documents.php
234 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Essential Classy Addons For Elementor Documents |
| 4 | * |
| 5 | * @package Ecafe |
| 6 | * @since 3.0.24 |
| 7 | */ |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | if( ! class_exists( 'Ecafe_Elementor_Builder_Documents' ) ) { |
| 13 | class Ecafe_Elementor_Builder_Documents { |
| 14 | |
| 15 | /** |
| 16 | * Instance |
| 17 | */ |
| 18 | private static $instance; |
| 19 | |
| 20 | /** |
| 21 | * Document Type Singular/Archives |
| 22 | */ |
| 23 | protected $doc_type = null; |
| 24 | |
| 25 | /** |
| 26 | * Initiator |
| 27 | */ |
| 28 | public static function get_instance() { |
| 29 | if ( ! isset( self::$instance ) ) { |
| 30 | self::$instance = new self(); |
| 31 | } |
| 32 | return self::$instance; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | |
| 40 | add_action( 'elementor/documents/register', [ $this, 'ec_register_elementor_documents' ] ); |
| 41 | |
| 42 | add_action( 'elementor/template-library/before_get_source_data', [ $this, 'ec_preview_query_switch' ] ); |
| 43 | add_action( 'elementor/template-library/after_get_source_data', [ $this, 'ec_current_query_restore' ] ); |
| 44 | add_action( 'elementor/dynamic_tags/before_render', [ $this, 'ec_preview_query_switch' ] ); |
| 45 | add_action( 'elementor/dynamic_tags/after_render', [ $this, 'ec_current_query_restore' ] ); |
| 46 | |
| 47 | add_filter( 'post_class', array( $this, 'ec_set_post_class' ) ); |
| 48 | add_filter( 'body_class', array( $this, 'ec_set_body_class' ) ); |
| 49 | add_filter( 'the_content', array( $this, 'ec_add_post_product_wrapper' ), 1000000 ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Add product wrapper to content |
| 54 | */ |
| 55 | public function ec_add_post_product_wrapper( $content ) { |
| 56 | |
| 57 | if ( is_singular( ECAFE_POST ) && isset( $_GET['elementor-preview'] ) ) { |
| 58 | $content = sprintf( '<div class="product">%s</div>', $content ); |
| 59 | } |
| 60 | |
| 61 | return $content; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Register appropriate document types for 'ec_theme_builder' post type |
| 66 | * |
| 67 | * @param Elementor\Core\Documents_Manager $documents_manager [description] |
| 68 | * |
| 69 | */ |
| 70 | public function ec_register_elementor_documents( $documents_manager ){ |
| 71 | |
| 72 | $doc_path = ECAFE_CLASSES_URL . 'documents/'; |
| 73 | require $doc_path. 'class-ele-builder-document-base.php'; |
| 74 | require $doc_path . 'class-ele-document-singular.php'; |
| 75 | |
| 76 | $documents_manager->register_document_type( 'ec_theme_builder', 'Ecafe_Ele_Document' ); |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Add 'product' class |
| 82 | */ |
| 83 | public function ec_set_post_class( $classes ) { |
| 84 | |
| 85 | if ( is_singular( ECAFE_POST ) ) { |
| 86 | $classes[] = 'product'; |
| 87 | } |
| 88 | |
| 89 | return $classes; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Add 'single-product' class to body on build post |
| 94 | */ |
| 95 | public function ec_set_body_class( $classes ) { |
| 96 | |
| 97 | if ( is_singular( ECAFE_POST ) ) { |
| 98 | $classes[] = 'woocommerce single-product'; |
| 99 | } |
| 100 | |
| 101 | return $classes; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Set document type |
| 106 | */ |
| 107 | public function set_current_type( $type ) { |
| 108 | $this->doc_type = $type; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get document type |
| 113 | */ |
| 114 | public function get_current_type() { |
| 115 | return $this->doc_type; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * Preview query switch |
| 121 | */ |
| 122 | public function ec_preview_query_switch() { |
| 123 | |
| 124 | $post_id = get_the_ID(); |
| 125 | $document = Elementor\Plugin::instance()->documents->get_doc_or_auto_save( $post_id ); |
| 126 | |
| 127 | if ( ! is_object( $document ) ) { |
| 128 | return null; |
| 129 | } |
| 130 | |
| 131 | $new_query = $this->ec_get_args_preview_query(); |
| 132 | |
| 133 | if ( empty( $new_query ) ) { |
| 134 | return null; |
| 135 | } |
| 136 | |
| 137 | Elementor\Plugin::instance()->db->switch_to_query( $new_query ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Restore default query |
| 142 | */ |
| 143 | public function ec_current_query_restore() { |
| 144 | Elementor\Plugin::instance()->db->restore_current_query(); |
| 145 | } |
| 146 | |
| 147 | public function preview_post_setting( $parent_post_id = '' ){ |
| 148 | $preview = []; |
| 149 | if( empty( $parent_post_id )){ |
| 150 | $post_id = get_the_ID(); |
| 151 | }else{ |
| 152 | $post_id = $parent_post_id; |
| 153 | } |
| 154 | |
| 155 | if ( get_post_type() == ECAFE_POST ) { |
| 156 | $template_type = get_post_meta( $post_id, 'ecafe_build_template_type', true ); |
| 157 | if( $template_type == 'singular' ){ |
| 158 | $singular_prev_type = get_post_meta( $post_id, 'ecafe_build_display_singular', true ); |
| 159 | $singular_prev_id = get_post_meta( $post_id, 'ecafe_build_display_prev', true ); |
| 160 | if( !empty($singular_prev_type) && !empty($singular_prev_id)){ |
| 161 | $preview['type'] = 'singular'; |
| 162 | $preview['preview_type'] = $singular_prev_type; |
| 163 | $preview['preview_id'] = $singular_prev_id; |
| 164 | $preview['preview_url'] = get_permalink( $singular_prev_id ); |
| 165 | } |
| 166 | }else if( $template_type == 'archives' ){ |
| 167 | $archive_prev_type = get_post_meta( $post_id, 'ecafe_build_display_archives', true ); |
| 168 | $archive_prev_id = get_post_meta( $post_id, 'ecafe_build_display_prev', true ); |
| 169 | if( !empty($archive_prev_type) && !empty($archive_prev_id)){ |
| 170 | $preview['type'] = 'archives'; |
| 171 | $preview['preview_type'] = $archive_prev_type; |
| 172 | $preview['preview_id'] = $archive_prev_id; |
| 173 | $preview['preview_url'] = get_category_link( $archive_prev_id ); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return $preview; |
| 179 | } |
| 180 | |
| 181 | public function ec_get_args_preview_query() { |
| 182 | $prev_data = $this->preview_post_setting(); |
| 183 | $preview_id = ''; |
| 184 | if( !empty( $prev_data ) && !empty( $prev_data['preview_id'] ) ){ |
| 185 | $preview_id = $prev_data['preview_id']; |
| 186 | } |
| 187 | |
| 188 | $args = []; |
| 189 | if( isset( $prev_data['type'] ) && !empty( $prev_data['type'] ) ){ |
| 190 | switch ( $prev_data['type'] ) { |
| 191 | case 'singular': |
| 192 | $post = get_post( $preview_id ); |
| 193 | if ( ! $post ) { |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | $args = [ |
| 198 | 'p' => $post->ID, |
| 199 | 'post_type' => $post->post_type, |
| 200 | ]; |
| 201 | break; |
| 202 | case 'archives': |
| 203 | switch ( $prev_data['preview_type'] ) { |
| 204 | default: |
| 205 | $get_term = get_term( $preview_id ); |
| 206 | |
| 207 | if ( $get_term && ! is_wp_error( $get_term ) ) { |
| 208 | $args = [ |
| 209 | 'tax_query' => [ |
| 210 | [ |
| 211 | 'taxonomy' => $get_term->taxonomy, |
| 212 | 'terms' => [ $preview_id ], |
| 213 | 'field' => 'id', |
| 214 | ], |
| 215 | ], |
| 216 | ]; |
| 217 | } |
| 218 | break; |
| 219 | } |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if ( empty( $args ) ) { |
| 225 | $args = [ |
| 226 | 'p' => get_the_ID(), |
| 227 | 'post_type' => get_post_type(), |
| 228 | ]; |
| 229 | } |
| 230 | |
| 231 | return $args; |
| 232 | } |
| 233 | } |
| 234 | } |