assets
2 weeks ago
widgets
4 months ago
class-ecs-container-layout-module.php
2 weeks ago
class-ecs-custom-layout-document.php
4 months ago
class-ecs-custom-layout-document.php
143 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ECS Custom Layout — Elementor Document Type |
| 4 | * |
| 5 | * Appears in: |
| 6 | * - Elementor Pro Theme Builder (when Pro is active) via Theme_Document base |
| 7 | * - Elementor Template Library (free-only) via Post base |
| 8 | * |
| 9 | * No display conditions — the template is selected manually per-container |
| 10 | * in the Container Layout panel, so conditions are not needed. |
| 11 | */ |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | use Elementor\Controls_Manager; |
| 18 | |
| 19 | /** |
| 20 | * Dynamic parent class selection. |
| 21 | * |
| 22 | * PHP does not allow `extends $variable`, so we declare an abstract |
| 23 | * intermediate class conditionally before the concrete class is defined. |
| 24 | * This is the standard PHP pattern for optional-dependency inheritance. |
| 25 | */ |
| 26 | if ( ! class_exists( 'ECS_Custom_Layout_Document_Base', false ) ) { |
| 27 | if ( class_exists( '\ElementorPro\Modules\ThemeBuilder\Documents\Theme_Document' ) ) { |
| 28 | // Elementor Pro is active → appear in Theme Builder section. |
| 29 | abstract class ECS_Custom_Layout_Document_Base |
| 30 | extends \ElementorPro\Modules\ThemeBuilder\Documents\Theme_Document {} |
| 31 | } else { |
| 32 | // Free Elementor only → appear in Template Library. |
| 33 | abstract class ECS_Custom_Layout_Document_Base |
| 34 | extends \Elementor\Core\DocumentTypes\Post {} |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | class ECS_Custom_Layout_Document extends ECS_Custom_Layout_Document_Base { |
| 39 | |
| 40 | /** |
| 41 | * Unique type string — stored as _elementor_template_type post meta. |
| 42 | */ |
| 43 | public static function get_type(): string { |
| 44 | return 'ecs_custom_layout'; |
| 45 | } |
| 46 | |
| 47 | public static function get_properties(): array { |
| 48 | $properties = parent::get_properties(); |
| 49 | |
| 50 | $properties['support_wp_page_templates'] = false; |
| 51 | $properties['show_in_finder'] = true; |
| 52 | $properties['show_on_admin_bar'] = true; |
| 53 | |
| 54 | // Pro Theme Builder: group under the "theme" tab, no conditions needed. |
| 55 | $properties['admin_tab_group'] = 'theme'; |
| 56 | $properties['support_conditions'] = false; |
| 57 | $properties['support_site_editor'] = true; |
| 58 | |
| 59 | return $properties; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Static title used for the tab label in Theme Builder / Template Library. |
| 64 | */ |
| 65 | public static function get_title(): string { |
| 66 | return esc_html__( 'ECS Custom Layout', 'ele-custom-skin' ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Required by Theme_Document (Pro) for the library UI. |
| 71 | */ |
| 72 | public static function get_plural_title(): string { |
| 73 | return esc_html__( 'ECS Custom Layouts', 'ele-custom-skin' ); |
| 74 | } |
| 75 | |
| 76 | public function get_css_wrapper_selector(): string { |
| 77 | return 'body.elementor-template-type-ecs_custom_layout'; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Add a "Recommended" category at the top of the widget panel, |
| 82 | * positioned right after "Favorites" (same pattern as Elementor Pro Loop Item). |
| 83 | */ |
| 84 | protected static function get_editor_panel_categories(): array { |
| 85 | $new_categories = [ |
| 86 | 'recommended' => [ |
| 87 | 'title' => esc_html__( 'Recommended', 'ele-custom-skin' ), |
| 88 | ], |
| 89 | ]; |
| 90 | |
| 91 | $parent_categories = parent::get_editor_panel_categories(); |
| 92 | $favorites_index = array_search( 'favorites', array_keys( $parent_categories ), true ); |
| 93 | |
| 94 | if ( false !== $favorites_index ) { |
| 95 | return array_slice( $parent_categories, 0, $favorites_index + 1, true ) |
| 96 | + $new_categories |
| 97 | + array_slice( $parent_categories, $favorites_index + 1, null, true ); |
| 98 | } |
| 99 | |
| 100 | return $new_categories + $parent_categories; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Put DTE Container Placeholder in the "Recommended" category so it |
| 105 | * appears first in the widget panel when editing a ECS Custom Layout. |
| 106 | */ |
| 107 | public function get_initial_config(): array { |
| 108 | $config = parent::get_initial_config(); |
| 109 | |
| 110 | $config['panel']['widgets_settings']['ecs_container_placeholder'] = [ |
| 111 | 'categories' => [ 'recommended' ], |
| 112 | 'show_in_panel' => true, |
| 113 | ]; |
| 114 | |
| 115 | return $config; |
| 116 | } |
| 117 | |
| 118 | protected function register_controls(): void { |
| 119 | parent::register_controls(); |
| 120 | |
| 121 | $this->start_controls_section( |
| 122 | 'ecs_layout_settings', |
| 123 | [ |
| 124 | 'label' => esc_html__( 'ECS Layout Settings', 'ele-custom-skin' ), |
| 125 | 'tab' => Controls_Manager::TAB_SETTINGS, |
| 126 | ] |
| 127 | ); |
| 128 | |
| 129 | $this->add_control( |
| 130 | 'ecs_child_wrapper_class', |
| 131 | [ |
| 132 | 'label' => esc_html__( 'Child Wrapper Class', 'ele-custom-skin' ), |
| 133 | 'type' => Controls_Manager::TEXT, |
| 134 | 'default' => 'ecs-layout-item', |
| 135 | 'placeholder' => 'ecs-layout-item', |
| 136 | 'description' => esc_html__( 'CSS class applied to the <div> wrapping each injected child.', 'ele-custom-skin' ), |
| 137 | ] |
| 138 | ); |
| 139 | |
| 140 | $this->end_controls_section(); |
| 141 | } |
| 142 | } |
| 143 |