wpr-column-slider.php
3 weeks ago
wpr-custom-css.php
3 weeks ago
wpr-display-conditions.php
3 weeks ago
wpr-equal-height.php
3 weeks ago
wpr-extensions-base.php
3 weeks ago
wpr-parallax.php
3 weeks ago
wpr-particles.php
3 weeks ago
wpr-sticky-section.php
3 weeks ago
wpr-equal-height.php
216 lines
| 1 | <?php |
| 2 | use Elementor\Controls_Manager; |
| 3 | use WprAddons\Classes\Utilities; |
| 4 | |
| 5 | |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; // Exit if accessed directly. |
| 8 | } |
| 9 | |
| 10 | class Wpr_Equal_Height { |
| 11 | public function __construct() { |
| 12 | add_action( 'elementor/element/section/section_advanced/after_section_end', [ $this, 'register_controls' ], 10 ); |
| 13 | add_action( 'elementor/section/print_template', array( $this, '_print_template' ), 10, 2 ); |
| 14 | add_action( 'elementor/frontend/section/before_render', array( $this, '_before_render' ), 10, 1 ); |
| 15 | |
| 16 | // FLEXBOX CONTAINER |
| 17 | add_action( 'elementor/element/container/section_layout/after_section_end', [ $this, 'register_controls' ], 10 ); |
| 18 | add_action( 'elementor/container/print_template', array( $this, '_print_template' ), 10, 2 ); |
| 19 | add_action( 'elementor/frontend/container/before_render', array( $this, '_before_render' ), 10, 1 ); |
| 20 | |
| 21 | } |
| 22 | |
| 23 | public function register_controls( $element ) { |
| 24 | |
| 25 | $element->start_controls_section( |
| 26 | 'wpr_section_equal_height', |
| 27 | [ |
| 28 | 'tab' => Controls_Manager::TAB_ADVANCED, |
| 29 | 'label' => sprintf(esc_html__('Equal Height - %s', 'wpr-addons'), esc_html('RA')), |
| 30 | ] |
| 31 | ); |
| 32 | |
| 33 | $element->add_control( |
| 34 | 'wpr_section_equal_height_update', |
| 35 | [ |
| 36 | 'type' => Controls_Manager::RAW_HTML, |
| 37 | 'raw' => '<div class="elementor-update-preview editor-wpr-preview-update"><span>Update changes to Preview</span><button class="elementor-button elementor-button-success" onclick="elementor.reloadPreview();">Apply</button>', |
| 38 | 'separator' => 'after' |
| 39 | ] |
| 40 | ); |
| 41 | |
| 42 | $element->add_control ( |
| 43 | 'wpr_enable_equal_height', |
| 44 | [ |
| 45 | 'type' => Controls_Manager::SWITCHER, |
| 46 | 'label' => esc_html__( 'Enable Equal Height', 'wpr-addons' ), |
| 47 | 'description' => esc_html__( 'Makes all selected child elements the same height within this section/container. Useful for aligning columns, cards, or widgets in a row so they match regardless of content length. Click "Apply" above after enabling.', 'wpr-addons' ), |
| 48 | 'default' => 'no', |
| 49 | 'return_value' => 'yes', |
| 50 | 'prefix_class' => 'wpr-equal-height-', |
| 51 | 'render_type' => 'template', |
| 52 | ] |
| 53 | ); |
| 54 | |
| 55 | $element->add_control( |
| 56 | 'wpr_equal_height_target_type', |
| 57 | [ |
| 58 | 'label' => esc_html__( 'Equalize', 'wpr-addons' ), |
| 59 | 'description' => esc_html__( 'Choose what to equalize: "Widgets" lets you pick specific widget types from a list, "Custom Selector" lets you target elements by CSS class name.', 'wpr-addons' ), |
| 60 | 'type' => Controls_Manager::SELECT, |
| 61 | 'default' => 'widget', |
| 62 | 'options' => [ |
| 63 | 'widget' => esc_html__( 'Widgets', 'wpr-addons' ), |
| 64 | 'custom' => esc_html__( 'Custom Selector', 'wpr-addons' ), |
| 65 | ], |
| 66 | 'condition' => [ |
| 67 | 'wpr_enable_equal_height' => 'yes', |
| 68 | ], |
| 69 | ] |
| 70 | ); |
| 71 | |
| 72 | $element->add_control( |
| 73 | 'wpr_equal_height_target', |
| 74 | [ |
| 75 | 'label' => esc_html__( 'Widgets', 'wpr-addons' ), |
| 76 | 'description' => esc_html__( 'Select which widget types should have equal height within this section.', 'wpr-addons' ), |
| 77 | 'type' => 'wpr-select2', |
| 78 | 'render_type' => 'template', |
| 79 | 'label_block' => true, |
| 80 | 'multiple' => true, |
| 81 | 'frontend_available' => true, |
| 82 | 'condition' => [ |
| 83 | 'wpr_enable_equal_height' => 'yes', |
| 84 | 'wpr_equal_height_target_type' => 'widget', |
| 85 | ], |
| 86 | ] |
| 87 | ); |
| 88 | |
| 89 | $element->add_control( |
| 90 | 'wpr_equal_height_custom_target', |
| 91 | array( |
| 92 | 'label' => esc_html__( 'Selectors', 'wpr-addons' ), |
| 93 | 'type' => Controls_Manager::TEXT, |
| 94 | 'label_block' => true, |
| 95 | 'placeholder' => esc_html__( '.class-name, .class-name2 .my-custom-class', 'wpr-addons' ), |
| 96 | 'description' => esc_html__( 'Enter CSS selectors separated by commas. All matching elements within this section will be set to equal height.', 'wpr-addons' ), |
| 97 | 'render_type' => 'none', |
| 98 | 'condition' => [ |
| 99 | 'wpr_enable_equal_height' => 'yes', |
| 100 | 'wpr_equal_height_target_type' => 'custom', |
| 101 | ], |
| 102 | ) |
| 103 | ); |
| 104 | |
| 105 | $element->add_control( |
| 106 | 'wpr_enable_equal_height_on', |
| 107 | array( |
| 108 | 'label' => esc_html__( 'Enable Equal Height on', 'wpr-addons' ), |
| 109 | 'description' => esc_html__( 'Choose which screen sizes should apply equal height. Deselect smaller breakpoints if you want columns to stack naturally on mobile.', 'wpr-addons' ), |
| 110 | 'type' => Controls_Manager::SELECT2, |
| 111 | 'multiple' => true, |
| 112 | 'options' => Utilities::get_all_breakpoints(), |
| 113 | 'label_block' => true, |
| 114 | 'default' => Utilities::get_all_breakpoints( 'keys' ), |
| 115 | 'condition' => [ |
| 116 | 'wpr_enable_equal_height' => 'yes', |
| 117 | ], |
| 118 | ) |
| 119 | ); |
| 120 | |
| 121 | $element->end_controls_section(); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | public function enqueue_scripts() { |
| 126 | |
| 127 | // if ( ! wp_script_is( 'pa-eq-height', 'enqueued' ) ) { |
| 128 | // wp_enqueue_script( 'pa-eq-height' ); |
| 129 | // } |
| 130 | |
| 131 | } |
| 132 | |
| 133 | public function _before_render( $element ) { |
| 134 | if ( $element->get_name() !== 'section' && $element->get_name() !== 'container' ) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | $settings = $element->get_settings_for_display(); |
| 139 | |
| 140 | if ( 'yes' === $settings['wpr_enable_equal_height'] ) { |
| 141 | |
| 142 | $target_type = $settings['wpr_equal_height_target_type']; |
| 143 | |
| 144 | if ( 'custom' === $target_type ) { |
| 145 | $target = array_map( 'trim', explode( ',', (string) $settings['wpr_equal_height_custom_target'] ) ); |
| 146 | $target = array_values( array_filter( $target ) ); |
| 147 | } else { |
| 148 | $target = $settings['wpr_equal_height_target'] ?? []; |
| 149 | if ( is_array( $target ) ) { |
| 150 | $target = array_values( $target ); |
| 151 | } elseif ( is_string( $target ) && '' !== $target ) { |
| 152 | $target = [ $target ]; |
| 153 | } else { |
| 154 | $target = []; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | $enable_on = $settings['wpr_enable_equal_height_on'] ?? []; |
| 159 | if ( ! is_array( $enable_on ) ) { |
| 160 | $enable_on = '' !== (string) $enable_on ? [ $enable_on ] : []; |
| 161 | } else { |
| 162 | $enable_on = array_values( $enable_on ); |
| 163 | } |
| 164 | |
| 165 | $equal_height_settings = array( |
| 166 | 'wpr_eh_target-type' => $target_type, |
| 167 | 'wpr_eh_target' => $target, |
| 168 | 'enable_on' => $enable_on, |
| 169 | ); |
| 170 | |
| 171 | $element->add_render_attribute( '_wrapper', 'data-wpr-equal-height', wp_json_encode( $equal_height_settings ) ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | public function _print_template( $template, $widget ) { |
| 176 | if ( $widget->get_name() !== 'section' && $widget->get_name() !== 'container' ) { |
| 177 | return $template; |
| 178 | } |
| 179 | |
| 180 | ob_start(); |
| 181 | |
| 182 | ?> |
| 183 | <# if( 'yes' === settings.wpr_enable_equal_height ) { |
| 184 | |
| 185 | view.addRenderAttribute( 'wpr_equal_height', 'id', 'wpr-equal-height-' + view.getID() ); |
| 186 | var targetType = settings.wpr_equal_height_target_type, |
| 187 | |
| 188 | target = 'custom' === targetType ? settings.wpr_equal_height_custom_target.split(',').map(function(s){ return s.trim(); }).filter(function(s){ return s; }) : settings.wpr_equal_height_target, |
| 189 | |
| 190 | addonSettings = { |
| 191 | 'wpr_eh_target-type': targetType, |
| 192 | 'wpr_eh_target': target, |
| 193 | 'enable_on':settings.wpr_enable_equal_height_on |
| 194 | }; |
| 195 | |
| 196 | view.addRenderAttribute( 'equal_height', { |
| 197 | 'id' : 'wpr-equal-height-' + view.getID(), |
| 198 | 'data-wpr-equal-height': JSON.stringify( addonSettings ) |
| 199 | }); |
| 200 | |
| 201 | #> |
| 202 | <div {{{ view.getRenderAttributeString( 'equal_height' ) }}}></div> |
| 203 | <# } #> |
| 204 | <?php |
| 205 | |
| 206 | // how to render attributes without creating new div using view.addRenderAttributes |
| 207 | $equal_height_content = ob_get_contents(); |
| 208 | |
| 209 | ob_end_clean(); |
| 210 | |
| 211 | return $template . $equal_height_content; |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | |
| 216 | new Wpr_Equal_Height(); |