module.php
171 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Elementor\Modules\ContainerConverter; |
| 4 | |
| 5 | use Elementor\Controls_Manager; |
| 6 | use Elementor\Controls_Stack; |
| 7 | use Elementor\Plugin; |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; // Exit if accessed directly. |
| 11 | } |
| 12 | |
| 13 | class Module extends \Elementor\Core\Base\Module { |
| 14 | |
| 15 | // Event name dispatched by the buttons. |
| 16 | const EVENT_NAME = 'elementorContainerConverter:convert'; |
| 17 | |
| 18 | /** |
| 19 | * Retrieve the module name. |
| 20 | * |
| 21 | * @return string |
| 22 | */ |
| 23 | public function get_name() { |
| 24 | return 'container-converter'; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Determine whether the module is active. |
| 29 | * |
| 30 | * @return bool |
| 31 | */ |
| 32 | public static function is_active() { |
| 33 | return Plugin::$instance->experiments->is_feature_active( 'container' ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Enqueue the module scripts. |
| 38 | * |
| 39 | * @return void |
| 40 | */ |
| 41 | public function enqueue_scripts() { |
| 42 | wp_enqueue_script( |
| 43 | 'container-converter', |
| 44 | $this->get_js_assets_url( 'container-converter' ), |
| 45 | [ 'elementor-editor' ], |
| 46 | ELEMENTOR_VERSION, |
| 47 | true |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Enqueue the module styles. |
| 53 | * |
| 54 | * @return void |
| 55 | */ |
| 56 | public function enqueue_styles() { |
| 57 | wp_enqueue_style( |
| 58 | 'container-converter', |
| 59 | $this->get_css_assets_url( 'modules/container-converter/editor' ), |
| 60 | [], |
| 61 | ELEMENTOR_VERSION |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Add a convert button to sections. |
| 67 | * |
| 68 | * @param \Elementor\Controls_Stack $controls_stack |
| 69 | * |
| 70 | * @return void |
| 71 | */ |
| 72 | protected function add_section_convert_button( Controls_Stack $controls_stack ) { |
| 73 | if ( ! Plugin::$instance->editor->is_edit_mode() ) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | $controls_stack->start_injection( [ |
| 78 | 'of' => '_title', |
| 79 | ] ); |
| 80 | |
| 81 | $controls_stack->add_control( |
| 82 | 'convert_to_container', |
| 83 | [ |
| 84 | 'type' => Controls_Manager::BUTTON, |
| 85 | 'label' => esc_html__( 'Convert to container', 'elementor' ), |
| 86 | 'text' => esc_html__( 'Convert', 'elementor' ), |
| 87 | 'button_type' => 'default', |
| 88 | 'description' => esc_html__( 'Copies all of the selected sections and columns and pastes them in a container beneath the original.', 'elementor' ), |
| 89 | 'separator' => 'after', |
| 90 | 'event' => static::EVENT_NAME, |
| 91 | ] |
| 92 | ); |
| 93 | |
| 94 | $controls_stack->end_injection(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Add a convert button to page settings. |
| 99 | * |
| 100 | * @param \Elementor\Controls_Stack $controls_stack |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | protected function add_page_convert_button( Controls_Stack $controls_stack ) { |
| 105 | if ( ! Plugin::$instance->editor->is_edit_mode() || ! $this->page_contains_sections( $controls_stack ) || ! Plugin::$instance->role_manager->user_can( 'design' ) ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | $controls_stack->start_injection( [ |
| 110 | 'of' => 'post_title', |
| 111 | 'at' => 'before', |
| 112 | ] ); |
| 113 | |
| 114 | $controls_stack->add_control( |
| 115 | 'convert_to_container', |
| 116 | [ |
| 117 | 'type' => Controls_Manager::BUTTON, |
| 118 | 'label' => esc_html__( 'Convert to container', 'elementor' ), |
| 119 | 'text' => esc_html__( 'Convert', 'elementor' ), |
| 120 | 'button_type' => 'default', |
| 121 | 'description' => esc_html__( 'Copies all of the selected sections and columns and pastes them in a container beneath the original.', 'elementor' ), |
| 122 | 'separator' => 'after', |
| 123 | 'event' => static::EVENT_NAME, |
| 124 | ] |
| 125 | ); |
| 126 | |
| 127 | $controls_stack->end_injection(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Checks if document has any Section elements. |
| 132 | * |
| 133 | * @param \Elementor\Controls_Stack $controls_stack |
| 134 | * |
| 135 | * @return bool |
| 136 | */ |
| 137 | protected function page_contains_sections( $controls_stack ) { |
| 138 | $data = $controls_stack->get_elements_data(); |
| 139 | |
| 140 | if ( ! is_array( $data ) ) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | foreach ( $data as $element ) { |
| 145 | if ( isset( $element['elType'] ) && 'section' === $element['elType'] ) { |
| 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Initialize the Container-Converter module. |
| 155 | * |
| 156 | * @return void |
| 157 | */ |
| 158 | public function __construct() { |
| 159 | add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 160 | add_action( 'elementor/editor/after_enqueue_styles', [ $this, 'enqueue_styles' ] ); |
| 161 | |
| 162 | add_action( 'elementor/element/section/section_layout/after_section_end', function ( Controls_Stack $controls_stack ) { |
| 163 | $this->add_section_convert_button( $controls_stack ); |
| 164 | } ); |
| 165 | |
| 166 | add_action( 'elementor/documents/register_controls', function ( Controls_Stack $controls_stack ) { |
| 167 | $this->add_page_convert_button( $controls_stack ); |
| 168 | } ); |
| 169 | } |
| 170 | } |
| 171 |