theme-page-document.php
391 lines
| 1 | <?php |
| 2 | namespace Auxin\Plugin\CoreElements\Elementor\Modules\ThemeBuilder; |
| 3 | |
| 4 | use Elementor\Controls_Manager; |
| 5 | use Elementor\Core\DocumentTypes\Post; |
| 6 | use Elementor\Modules\PageTemplates\Module as Single; |
| 7 | use Elementor\TemplateLibrary\Source_Local; |
| 8 | use Elementor\Modules\Library\Documents\Library_Document; |
| 9 | use Auxin\Plugin\CoreElements\Elementor\Modules\ThemeBuilder\Module; |
| 10 | use Elementor\Plugin; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // Exit if accessed directly |
| 14 | } |
| 15 | |
| 16 | abstract class Theme_Document extends Library_Document { |
| 17 | |
| 18 | const LOCATION_META_KEY = '_elementor_location'; |
| 19 | |
| 20 | public function get_location_label() { |
| 21 | $location = $this->get_location(); |
| 22 | $locations_settings = Module::instance()->get_locations_manager()->get_location( $location ); |
| 23 | $label = ''; |
| 24 | $is_section_doc_type = 'section' === $this->get_name(); |
| 25 | |
| 26 | if ( $location ) { |
| 27 | if ( $is_section_doc_type ) { |
| 28 | $label .= isset( $locations_settings['label'] ) ? $locations_settings['label'] : $location; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | $supported = true; |
| 33 | |
| 34 | if ( $is_section_doc_type ) { |
| 35 | if ( $location && ! $locations_settings ) { |
| 36 | $supported = false; |
| 37 | } |
| 38 | } elseif ( ! $location || ! $locations_settings ) { |
| 39 | $supported = false; |
| 40 | } |
| 41 | |
| 42 | if ( ! $supported ) { |
| 43 | $label .= ' (' . __( 'Unsupported', 'auxin-elements' ) . ')'; |
| 44 | } |
| 45 | |
| 46 | return $label; |
| 47 | } |
| 48 | |
| 49 | public function before_get_content() { |
| 50 | $preview_manager = Module::instance()->get_preview_manager(); |
| 51 | $preview_manager->switch_to_preview_query(); |
| 52 | } |
| 53 | |
| 54 | public function after_get_content() { |
| 55 | $preview_manager = Module::instance()->get_preview_manager(); |
| 56 | $preview_manager->restore_current_query(); |
| 57 | } |
| 58 | |
| 59 | public function get_content( $with_css = false ) { |
| 60 | $this->before_get_content(); |
| 61 | $content = parent::get_content( $with_css ); |
| 62 | $this->after_get_content(); |
| 63 | |
| 64 | return $content; |
| 65 | } |
| 66 | |
| 67 | public function print_content() { |
| 68 | $plugin = Plugin::instance(); |
| 69 | |
| 70 | if ( $plugin->preview->is_preview_mode( get_the_ID() ) ) { |
| 71 | echo $plugin->preview->builder_wrapper( '' ); |
| 72 | } else { |
| 73 | echo $this->get_content(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public static function get_preview_as_default() { |
| 78 | return ''; |
| 79 | } |
| 80 | |
| 81 | public static function get_preview_as_options() { |
| 82 | return []; |
| 83 | } |
| 84 | |
| 85 | public function get_container_attributes() { |
| 86 | $attributes = parent::get_container_attributes(); |
| 87 | |
| 88 | if ( is_singular() ) { |
| 89 | $post_classes = get_post_class( '', get_the_ID() ); |
| 90 | |
| 91 | $attributes['class'] .= ' ' . implode( ' ', $post_classes ); |
| 92 | } |
| 93 | |
| 94 | return $attributes; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @static |
| 99 | * @since 2.0.0 |
| 100 | * @access public |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | public function get_edit_url() { |
| 105 | $url = parent::get_edit_url(); |
| 106 | |
| 107 | if ( isset( $_GET['action'] ) && 'elementor_new_post' === $_GET['action'] ) { |
| 108 | $url .= '#library'; |
| 109 | } |
| 110 | |
| 111 | return $url; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | protected function register_controls() { |
| 116 | parent::register_controls(); |
| 117 | |
| 118 | // $this->start_controls_section( |
| 119 | // 'preview_settings', |
| 120 | // [ |
| 121 | // 'label' => __( 'Preview Settings', 'auxin-elements' ), |
| 122 | // 'tab' => Controls_Manager::TAB_SETTINGS, |
| 123 | // ] |
| 124 | // ); |
| 125 | |
| 126 | // $this->add_control( |
| 127 | // 'preview_type', |
| 128 | // [ |
| 129 | // 'label' => __( 'Preview Dynamic Content as', 'auxin-elements' ), |
| 130 | // 'label_block' => true, |
| 131 | // 'type' => Controls_Manager::SELECT, |
| 132 | // 'default' => $this::get_preview_as_default(), |
| 133 | // 'groups' => $this::get_preview_as_options(), |
| 134 | // 'export' => false, |
| 135 | // ] |
| 136 | // ); |
| 137 | |
| 138 | // $this->add_control( |
| 139 | // 'preview_id', |
| 140 | // [ |
| 141 | // 'type' => 'aux-query', |
| 142 | // 'label_block' => true, |
| 143 | // 'filter_type' => '', |
| 144 | // 'object_type' => '', |
| 145 | // 'separator' => 'none', |
| 146 | // 'export' => false, |
| 147 | // 'condition' => [ |
| 148 | // 'preview_type!' => [ |
| 149 | // '', |
| 150 | // 'search', |
| 151 | // ], |
| 152 | // ], |
| 153 | // ] |
| 154 | // ); |
| 155 | |
| 156 | // $this->add_control( |
| 157 | // 'preview_search_term', |
| 158 | // [ |
| 159 | // 'label' => __( 'Search Term', 'auxin-elements' ), |
| 160 | // 'export' => false, |
| 161 | // 'condition' => [ |
| 162 | // 'preview_type' => 'search', |
| 163 | // ], |
| 164 | // ] |
| 165 | // ); |
| 166 | |
| 167 | // $this->add_control( |
| 168 | // 'apply_preview', |
| 169 | // [ |
| 170 | // 'type' => Controls_Manager::BUTTON, |
| 171 | // 'label' => __( 'Apply & Preview', 'auxin-elements' ), |
| 172 | // 'label_block' => true, |
| 173 | // 'show_label' => false, |
| 174 | // 'text' => __( 'Apply & Preview', 'auxin-elements' ), |
| 175 | // 'separator' => 'none', |
| 176 | // 'event' => 'elementorThemeBuilder:ApplyPreview', |
| 177 | // ] |
| 178 | // ); |
| 179 | |
| 180 | // $this->end_controls_section(); |
| 181 | } |
| 182 | |
| 183 | public function get_elements_raw_data( $data = null, $with_html_content = false ) { |
| 184 | $preview_manager = Module::instance()->get_preview_manager(); |
| 185 | |
| 186 | $preview_manager->switch_to_preview_query(); |
| 187 | |
| 188 | $editor_data = parent::get_elements_raw_data( $data, $with_html_content ); |
| 189 | |
| 190 | $preview_manager->restore_current_query(); |
| 191 | |
| 192 | return $editor_data; |
| 193 | } |
| 194 | |
| 195 | public function render_element( $data ) { |
| 196 | $preview_manager = Module::instance()->get_preview_manager(); |
| 197 | |
| 198 | $preview_manager->switch_to_preview_query(); |
| 199 | |
| 200 | $render_html = parent::render_element( $data ); |
| 201 | |
| 202 | $preview_manager->restore_current_query(); |
| 203 | |
| 204 | return $render_html; |
| 205 | } |
| 206 | |
| 207 | public function get_wp_preview_url() { |
| 208 | $preview_id = (int) $this->get_settings( 'preview_id' ); |
| 209 | $post_id = $this->get_main_id(); |
| 210 | |
| 211 | $preview_type = $this->get_settings( 'preview_type' ); |
| 212 | list( $preview_category, $preview_object_type ) = $preview_type ? array_pad( explode( '/', $preview_type ), 2, '' ) : array_pad( [], 2, '' ); |
| 213 | |
| 214 | $home_url = trailingslashit( home_url() ); |
| 215 | |
| 216 | switch ( $preview_category ) { |
| 217 | case 'archive': |
| 218 | switch ( $preview_object_type ) { |
| 219 | case 'author': |
| 220 | if ( empty( $preview_id ) ) { |
| 221 | $preview_id = get_current_user_id(); |
| 222 | } |
| 223 | $preview_url = get_author_posts_url( $preview_id ); |
| 224 | break; |
| 225 | case 'date': |
| 226 | $preview_url = add_query_arg( 'year', date( 'Y' ), $home_url ); |
| 227 | break; |
| 228 | } |
| 229 | break; |
| 230 | case 'search': |
| 231 | $preview_url = add_query_arg( 's', $this->get_settings( 'preview_search_term' ), $home_url ); |
| 232 | break; |
| 233 | case 'taxonomy': |
| 234 | $term = get_term( $preview_id ); |
| 235 | |
| 236 | if ( $term && ! is_wp_error( $term ) ) { |
| 237 | $preview_url = get_term_link( $preview_id ); |
| 238 | } |
| 239 | |
| 240 | break; |
| 241 | case 'page': |
| 242 | switch ( $preview_object_type ) { |
| 243 | case 'home': |
| 244 | $preview_url = get_post_type_archive_link( 'post' ); |
| 245 | break; |
| 246 | case 'front': |
| 247 | $preview_url = $home_url; |
| 248 | break; |
| 249 | case '404': |
| 250 | $preview_url = add_query_arg( 'p', '-1', $home_url ); |
| 251 | break; |
| 252 | } |
| 253 | break; |
| 254 | case 'post_type_archive': |
| 255 | $post_type = $preview_object_type; |
| 256 | if ( post_type_exists( $post_type ) ) { |
| 257 | $preview_url = get_post_type_archive_link( $post_type ); |
| 258 | } |
| 259 | break; |
| 260 | case 'single': |
| 261 | $post = get_post( $preview_id ); |
| 262 | if ( $post ) { |
| 263 | $preview_url = get_permalink( $post ); |
| 264 | } |
| 265 | break; |
| 266 | } // End switch(). |
| 267 | |
| 268 | if ( empty( $preview_url ) ) { |
| 269 | $preview_url = $this->get_permalink(); |
| 270 | } |
| 271 | |
| 272 | $query_args = [ |
| 273 | 'preview' => true, |
| 274 | 'preview_nonce' => wp_create_nonce( 'post_preview_' . $post_id ), |
| 275 | 'theme_template_id' => $post_id, |
| 276 | ]; |
| 277 | |
| 278 | $preview_url = set_url_scheme( add_query_arg( $query_args, $preview_url ) ); |
| 279 | $preview_url = apply_filters( 'elementor/document/wp_preview_url', $preview_url, $this ); |
| 280 | |
| 281 | return $preview_url; |
| 282 | } |
| 283 | |
| 284 | public function get_preview_as_query_args() { |
| 285 | $preview_id = (int) $this->get_settings( 'preview_id' ); |
| 286 | |
| 287 | $preview_type = $this->get_settings( 'preview_type' ); |
| 288 | list( $preview_category, $preview_object_type ) = $preview_type ? array_pad( explode( '/', $preview_type ), 2, '' ) : array_pad( [], 2, '' ); |
| 289 | |
| 290 | switch ( $preview_category ) { |
| 291 | case 'archive': |
| 292 | switch ( $preview_object_type ) { |
| 293 | case 'author': |
| 294 | if ( empty( $preview_id ) ) { |
| 295 | $preview_id = get_current_user_id(); |
| 296 | } |
| 297 | |
| 298 | $query_args = [ |
| 299 | 'author' => $preview_id, |
| 300 | ]; |
| 301 | break; |
| 302 | case 'date': |
| 303 | $query_args = [ |
| 304 | 'year' => date( 'Y' ), |
| 305 | ]; |
| 306 | break; |
| 307 | case 'recent_posts': |
| 308 | $query_args = [ |
| 309 | 'post_type' => 'post', |
| 310 | ]; |
| 311 | break; |
| 312 | } |
| 313 | break; |
| 314 | case 'search': |
| 315 | $query_args = [ |
| 316 | 's' => $this->get_settings( 'preview_search_term' ), |
| 317 | ]; |
| 318 | break; |
| 319 | case 'taxonomy': |
| 320 | $term = get_term( $preview_id ); |
| 321 | |
| 322 | if ( $term && ! is_wp_error( $term ) ) { |
| 323 | $query_args = [ |
| 324 | 'tax_query' => [ |
| 325 | [ |
| 326 | 'taxonomy' => $term->taxonomy, |
| 327 | 'terms' => [ $preview_id ], |
| 328 | 'field' => 'id', |
| 329 | ], |
| 330 | ], |
| 331 | ]; |
| 332 | } |
| 333 | break; |
| 334 | case 'page': |
| 335 | switch ( $preview_object_type ) { |
| 336 | case 'home': |
| 337 | $query_args = []; |
| 338 | break; |
| 339 | case 'front': |
| 340 | $query_args = [ |
| 341 | 'p' => get_option( 'page_on_front' ), |
| 342 | 'post_type' => 'page', |
| 343 | ]; |
| 344 | break; |
| 345 | case '404': |
| 346 | $query_args = [ |
| 347 | 'p' => -1, |
| 348 | ]; |
| 349 | break; |
| 350 | } |
| 351 | break; |
| 352 | case 'post_type_archive': |
| 353 | $post_type = $preview_object_type; |
| 354 | if ( post_type_exists( $post_type ) ) { |
| 355 | $query_args = [ |
| 356 | 'post_type' => $post_type, |
| 357 | ]; |
| 358 | } |
| 359 | break; |
| 360 | case 'single': |
| 361 | $post = get_post( $preview_id ); |
| 362 | if ( ! $post ) { |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | $query_args = [ |
| 367 | 'p' => $post->ID, |
| 368 | 'post_type' => $post->post_type, |
| 369 | ]; |
| 370 | } // End switch(). |
| 371 | |
| 372 | if ( empty( $query_args ) ) { |
| 373 | $query_args = [ |
| 374 | 'p' => $this->get_main_id(), |
| 375 | 'post_type' => $this->get_main_post()->post_type, |
| 376 | ]; |
| 377 | } |
| 378 | |
| 379 | return $query_args; |
| 380 | } |
| 381 | |
| 382 | public function get_location() { |
| 383 | $value = self::get_property( 'location' ); |
| 384 | if ( ! $value ) { |
| 385 | $value = $this->get_main_meta( self::LOCATION_META_KEY ); |
| 386 | } |
| 387 | |
| 388 | return $value; |
| 389 | } |
| 390 | } |
| 391 |