| @@ -1,201 +1,208 @@ | ||
| 1 | -<?php | |
| 2 | -namespace Easyel\EasyElements\Widgets; | |
| 3 | -use Elementor\Controls_Manager; | |
| 4 | -use Elementor\Widget_Base; | |
| 5 | - | |
| 6 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 7 | - exit; | |
| 8 | -} | |
| 9 | - | |
| 10 | -class Easyel_free_Featured_Image_Widget extends \Elementor\Widget_Base { | |
| 11 | - | |
| 12 | - public function get_name() { | |
| 13 | - return 'eel-featured-image'; | |
| 14 | - } | |
| 15 | - | |
| 16 | - public function get_title() { | |
| 17 | - return __( 'Featured Image', 'easy-elements' ); | |
| 18 | - } | |
| 19 | - | |
| 20 | - public function get_icon() { | |
| 21 | - return 'easyicon easyelIcon-image-carousel'; | |
| 22 | - } | |
| 23 | - | |
| 24 | - public function get_categories() { | |
| 25 | - return [ 'easyelements_category' ]; | |
| 26 | - } | |
| 27 | - public function get_keywords() { | |
| 28 | - return [ 'image', 'thumbnail', 'link', 'featured' ]; | |
| 29 | - } | |
| 30 | - | |
| 31 | - public function get_style_depends() { | |
| 32 | - return [ | |
| 33 | - 'eel-featured-image', | |
| 34 | - ]; | |
| 35 | - } | |
| 36 | - | |
| 37 | - protected function register_controls() { | |
| 38 | - | |
| 39 | - $this->start_controls_section( | |
| 40 | - 'content_section', | |
| 41 | - [ | |
| 42 | - 'label' => esc_html__('Featured Image Settings', 'easy-elements'), | |
| 43 | - 'tab' => Controls_Manager::TAB_CONTENT, | |
| 44 | - ] | |
| 45 | - ); | |
| 46 | - | |
| 47 | - $this->add_responsive_control( | |
| 48 | - 'featured_image_width', | |
| 49 | - [ | |
| 50 | - 'label' => esc_html__('Width', 'easy-elements'), | |
| 51 | - 'type' => Controls_Manager::SLIDER, | |
| 52 | - 'size_units' => ['px', '%'], | |
| 53 | - 'range' => [ | |
| 54 | - 'px' => [ | |
| 55 | - 'min' => 0, | |
| 56 | - 'max' => 1000, | |
| 57 | - ], | |
| 58 | - '%' => [ | |
| 59 | - 'min' => 0, | |
| 60 | - 'max' => 100, | |
| 61 | - ], | |
| 62 | - ], | |
| 63 | - 'selectors' => [ | |
| 64 | - '{{WRAPPER}} .eel-featured-image-inner img' => 'width: {{SIZE}}{{UNIT}};', | |
| 65 | - ], | |
| 66 | - ] | |
| 67 | - ); | |
| 68 | - | |
| 69 | - $this->add_responsive_control( | |
| 70 | - 'featured_image_height', | |
| 71 | - [ | |
| 72 | - 'label' => esc_html__('Height', 'easy-elements'), | |
| 73 | - 'type' => Controls_Manager::SLIDER, | |
| 74 | - 'size_units' => ['px', '%'], | |
| 75 | - 'range' => [ | |
| 76 | - 'px' => [ | |
| 77 | - 'min' => 100, | |
| 78 | - 'max' => 1000, | |
| 79 | - ], | |
| 80 | - '%' => [ | |
| 81 | - 'min' => 100, | |
| 82 | - 'max' => 100, | |
| 83 | - ], | |
| 84 | - ], | |
| 85 | - 'selectors' => [ | |
| 86 | - '{{WRAPPER}} .eel-featured-image-inner img' => 'height: {{SIZE}}{{UNIT}};', | |
| 87 | - ], | |
| 88 | - ] | |
| 89 | - ); | |
| 90 | - | |
| 91 | - $this->add_responsive_control( | |
| 92 | - 'border_radius', | |
| 93 | - [ | |
| 94 | - 'label' => esc_html__('Border Radius', 'easy-elements'), | |
| 95 | - 'type' => Controls_Manager::DIMENSIONS, | |
| 96 | - 'size_units' => ['px'], | |
| 97 | - 'range' => [ | |
| 98 | - 'px' => [ | |
| 99 | - 'min' => 0, | |
| 100 | - 'max' => 100, | |
| 101 | - ], | |
| 102 | - ], | |
| 103 | - 'selectors' => [ | |
| 104 | - '.eel-featured-image-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;', | |
| 105 | - ], | |
| 106 | - ] | |
| 107 | - ); | |
| 108 | - | |
| 109 | - // Image Size Control | |
| 110 | - $this->add_control( | |
| 111 | - 'image_size', | |
| 112 | - [ | |
| 113 | - 'label' => esc_html__('Image Size', 'easy-elements'), | |
| 114 | - 'type' => Controls_Manager::SELECT, | |
| 115 | - 'default' => 'full', | |
| 116 | - 'options' => [ | |
| 117 | - 'thumbnail' => esc_html__('Thumbnail', 'easy-elements'), | |
| 118 | - 'medium' => esc_html__('Medium', 'easy-elements'), | |
| 119 | - 'large' => esc_html__('Large', 'easy-elements'), | |
| 120 | - 'full' => esc_html__('Full', 'easy-elements'), | |
| 121 | - ], | |
| 122 | - ] | |
| 123 | - ); | |
| 124 | - | |
| 125 | - // Add show/hide image link control | |
| 126 | - $this->add_control( | |
| 127 | - 'show_image_link', | |
| 128 | - [ | |
| 129 | - 'label' => esc_html__('Show Image Link', 'easy-elements'), | |
| 130 | - 'type' => Controls_Manager::SWITCHER, | |
| 131 | - 'label_on' => esc_html__('Show', 'easy-elements'), | |
| 132 | - 'label_off' => esc_html__('Hide', 'easy-elements'), | |
| 133 | - 'return_value' => 'yes', | |
| 134 | - 'default' => 'yes', | |
| 135 | - ] | |
| 136 | - ); | |
| 137 | - | |
| 138 | - $this->end_controls_section(); | |
| 139 | - | |
| 140 | - } | |
| 141 | - | |
| 142 | - protected function render() { | |
| 143 | - $settings = $this->get_settings_for_display(); | |
| 144 | - $image_size = $settings['image_size'] ?? 'full'; | |
| 145 | - $editor_mode = \Elementor\Plugin::instance()->editor->is_edit_mode(); | |
| 146 | - $wrapper_classes = 'eel-featured-image-wrapper' . ( $editor_mode ? ' ' : '' ); | |
| 147 | - | |
| 148 | - $post_id = function_exists('easyel_get_prepared_post_id') | |
| 149 | - ? easyel_get_prepared_post_id() | |
| 150 | - : ( isset($GLOBALS['post']->ID) ? $GLOBALS['post']->ID : 0 ); | |
| 151 | - // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 152 | - if ( !$post_id && isset($_GET['preview_id']) ) { | |
| 153 | - // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 154 | - $post_id = intval(sanitize_text_field(wp_unslash($_GET['preview_id']))); | |
| 155 | - } | |
| 156 | - | |
| 157 | - $image_url = ''; | |
| 158 | - $image_alt = ''; | |
| 159 | - $post_link = ''; | |
| 160 | - | |
| 161 | - if ( $post_id && has_post_thumbnail( $post_id ) ) { | |
| 162 | - $image_url = get_the_post_thumbnail_url( $post_id, $image_size ); | |
| 163 | - $image_alt = get_the_title( $post_id ); | |
| 164 | - $post_link = get_permalink( $post_id ); | |
| 165 | - } | |
| 166 | - | |
| 167 | - ?> | |
| 168 | - <div class="<?php echo esc_attr( $wrapper_classes ); ?>"> | |
| 169 | - <div class="eel-featured-image-inner"> | |
| 170 | - <?php if ( !empty($image_url) ) : ?> | |
| 171 | - <?php if ( ($settings['show_image_link'] ?? '') === 'yes' && !empty($post_link) ) : ?> | |
| 172 | - <a href="<?php echo esc_url( $post_link ); ?>" class="eel-featured-image-link"> | |
| 173 | - <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $image_alt ?: __('Featured Image', 'easy-elements') ); ?>" class="eel-featured-image-img" loading="lazy" /> | |
| 174 | - </a> | |
| 175 | - <?php else : ?> | |
| 176 | - <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $image_alt ?: __('Featured Image', 'easy-elements') ); ?>" class="eel-featured-image-img" loading="lazy" /> | |
| 177 | - <?php endif; ?> | |
| 178 | - <?php elseif ( $editor_mode ) : ?> | |
| 179 | - <div class="eel-featured-image-placeholder"> | |
| 180 | - <svg width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg" class="eel-featured-image-placeholder-icon"> | |
| 181 | - <defs> | |
| 182 | - <linearGradient id="imgGradient" x1="0" y1="0" x2="64" y2="64" gradientUnits="userSpaceOnUse"> | |
| 183 | - <stop stop-color="#7fd7ff"/> | |
| 184 | - <stop offset="1" stop-color="#0073e6"/> | |
| 185 | - </linearGradient> | |
| 186 | - </defs> | |
| 187 | - <rect x="8" y="14" width="48" height="36" rx="5" fill="#f0f6ff" stroke="url(#imgGradient)" stroke-width="2.5"/> | |
| 188 | - <circle cx="22" cy="28" r="5" fill="#b3d4fc" /> | |
| 189 | - <path d="M14 44L26 32L36 42L44 34L52 42" stroke="#b3d4fc" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/> | |
| 190 | - </svg> | |
| 191 | - <div class="eel-featured-image-placeholder-title"><?php esc_html_e('No Featured Image', 'easy-elements'); ?></div> | |
| 192 | - <div class="eel-featured-image-placeholder-desc"><?php esc_html_e('Please add a featured image to display here.', 'easy-elements'); ?></div> | |
| 193 | - <a href="#" class="eel-featured-image-placeholder-btn" tabindex="-1"><?php esc_html_e('Set Featured Image', 'easy-elements'); ?></a> | |
| 194 | - </div> | |
| 195 | - <?php endif; ?> | |
| 196 | - </div> | |
| 197 | - </div> | |
| 198 | - <?php | |
| 199 | - } | |
| 200 | - | |
| 201 | -} | |
| 1 | +<?php | |
| 2 | +use Elementor\Controls_Manager; | |
| 3 | +use Elementor\Widget_Base; | |
| 4 | + | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | + | |
| 9 | +if( class_exists("Easyel_Featured_Image_Widget")) { | |
| 10 | + return; | |
| 11 | +} | |
| 12 | + | |
| 13 | +class Easyel_Featured_Image_Widget extends \Elementor\Widget_Base { | |
| 14 | + | |
| 15 | + public function get_style_depends() { | |
| 16 | + $handle = 'eel-featured-image'; | |
| 17 | + $css_path = plugin_dir_path( __FILE__ ) . 'css/featured-image.css'; | |
| 18 | + | |
| 19 | + if ( ! wp_style_is( $handle, 'registered' ) && file_exists( $css_path ) ) { | |
| 20 | + wp_register_style( $handle, plugins_url( 'css/featured-image.css', __FILE__ ), [], defined( 'WP_DEBUG' ) && WP_DEBUG ? filemtime( $css_path ) : '1.0.0' ); | |
| 21 | + } | |
| 22 | + return [ $handle ]; | |
| 23 | + } | |
| 24 | + | |
| 25 | + public function get_name() { | |
| 26 | + return 'eel-featured-image'; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public function get_title() { | |
| 30 | + return __( 'Featured Image', 'easy-elements' ); | |
| 31 | + } | |
| 32 | + | |
| 33 | + public function get_icon() { | |
| 34 | + return 'easyicon easyelIcon-image-carousel'; | |
| 35 | + } | |
| 36 | + | |
| 37 | + public function get_categories() { | |
| 38 | + return [ 'easyelements_category_pro' ]; | |
| 39 | + } | |
| 40 | + public function get_keywords() { | |
| 41 | + return [ 'image', 'thumbnail', 'link', 'featured' ]; | |
| 42 | + } | |
| 43 | + protected function register_controls() { | |
| 44 | + | |
| 45 | + $this->start_controls_section( | |
| 46 | + 'content_section', | |
| 47 | + [ | |
| 48 | + 'label' => esc_html__('Featured Image Settings', 'easy-elements'), | |
| 49 | + 'tab' => Controls_Manager::TAB_CONTENT, | |
| 50 | + ] | |
| 51 | + ); | |
| 52 | + | |
| 53 | + $this->add_control( | |
| 54 | + 'featured_image_width', | |
| 55 | + [ | |
| 56 | + 'label' => esc_html__('Width', 'easy-elements'), | |
| 57 | + 'type' => Controls_Manager::SLIDER, | |
| 58 | + 'size_units' => ['px', '%'], | |
| 59 | + 'range' => [ | |
| 60 | + 'px' => [ | |
| 61 | + 'min' => 0, | |
| 62 | + 'max' => 1000, | |
| 63 | + ], | |
| 64 | + '%' => [ | |
| 65 | + 'min' => 0, | |
| 66 | + 'max' => 100, | |
| 67 | + ], | |
| 68 | + ], | |
| 69 | + 'selectors' => [ | |
| 70 | + '{{WRAPPER}} .eel-featured-image-inner img' => 'width: {{SIZE}}{{UNIT}};', | |
| 71 | + ], | |
| 72 | + ] | |
| 73 | + ); | |
| 74 | + | |
| 75 | + $this->add_control( | |
| 76 | + 'featured_image_height', | |
| 77 | + [ | |
| 78 | + 'label' => esc_html__('Height', 'easy-elements'), | |
| 79 | + 'type' => Controls_Manager::SLIDER, | |
| 80 | + 'size_units' => ['px', '%'], | |
| 81 | + 'range' => [ | |
| 82 | + 'px' => [ | |
| 83 | + 'min' => 100, | |
| 84 | + 'max' => 1000, | |
| 85 | + ], | |
| 86 | + '%' => [ | |
| 87 | + 'min' => 100, | |
| 88 | + 'max' => 100, | |
| 89 | + ], | |
| 90 | + ], | |
| 91 | + 'selectors' => [ | |
| 92 | + '{{WRAPPER}} .eel-featured-image-inner img' => 'height: {{SIZE}}{{UNIT}};', | |
| 93 | + ], | |
| 94 | + ] | |
| 95 | + ); | |
| 96 | + | |
| 97 | + $this->add_control( | |
| 98 | + 'border_radius', | |
| 99 | + [ | |
| 100 | + 'label' => esc_html__('Border Radius', 'easy-elements'), | |
| 101 | + 'type' => Controls_Manager::DIMENSIONS, | |
| 102 | + 'size_units' => ['px'], | |
| 103 | + 'range' => [ | |
| 104 | + 'px' => [ | |
| 105 | + 'min' => 0, | |
| 106 | + 'max' => 100, | |
| 107 | + ], | |
| 108 | + ], | |
| 109 | + 'selectors' => [ | |
| 110 | + '.eel-featured-image-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;', | |
| 111 | + ], | |
| 112 | + ] | |
| 113 | + ); | |
| 114 | + | |
| 115 | + // Image Size Control | |
| 116 | + $this->add_control( | |
| 117 | + 'image_size', | |
| 118 | + [ | |
| 119 | + 'label' => esc_html__('Image Size', 'easy-elements'), | |
| 120 | + 'type' => Controls_Manager::SELECT, | |
| 121 | + 'default' => 'full', | |
| 122 | + 'options' => [ | |
| 123 | + 'thumbnail' => esc_html__('Thumbnail', 'easy-elements'), | |
| 124 | + 'medium' => esc_html__('Medium', 'easy-elements'), | |
| 125 | + 'large' => esc_html__('Large', 'easy-elements'), | |
| 126 | + 'full' => esc_html__('Full', 'easy-elements'), | |
| 127 | + ], | |
| 128 | + ] | |
| 129 | + ); | |
| 130 | + | |
| 131 | + // Add show/hide image link control | |
| 132 | + $this->add_control( | |
| 133 | + 'show_image_link', | |
| 134 | + [ | |
| 135 | + 'label' => esc_html__('Show Image Link', 'easy-elements'), | |
| 136 | + 'type' => Controls_Manager::SWITCHER, | |
| 137 | + 'label_on' => esc_html__('Show', 'easy-elements'), | |
| 138 | + 'label_off' => esc_html__('Hide', 'easy-elements'), | |
| 139 | + 'return_value' => 'yes', | |
| 140 | + 'default' => 'yes', | |
| 141 | + ] | |
| 142 | + ); | |
| 143 | + | |
| 144 | + $this->end_controls_section(); | |
| 145 | + | |
| 146 | + } | |
| 147 | + | |
| 148 | + protected function render() { | |
| 149 | + $settings = $this->get_settings_for_display(); | |
| 150 | + $image_size = isset($settings['image_size']) ? $settings['image_size'] : 'full'; | |
| 151 | + // Show preview in Elementor editor | |
| 152 | + $editor_mode = ( \Elementor\Plugin::instance()->editor->is_edit_mode() ); | |
| 153 | + $wrapper_classes = 'eel-featured-image-wrapper'; | |
| 154 | + if ( $editor_mode ) { | |
| 155 | + $wrapper_classes .= ' eel-featured-image-editor-preview'; | |
| 156 | + } | |
| 157 | + global $post; | |
| 158 | + $post_id = 0; | |
| 159 | + if ( isset( $post ) && isset( $post->ID ) ) { | |
| 160 | + $post_id = $post->ID; | |
| 161 | + } elseif ( isset( $_GET['preview_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 162 | + $preview_id = sanitize_text_field( wp_unslash( $_GET['preview_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 163 | + $post_id = intval( $preview_id ); | |
| 164 | + } | |
| 165 | + if ( $post_id && has_post_thumbnail( $post_id ) ) { | |
| 166 | + $image_url = get_the_post_thumbnail_url( $post_id, $image_size ); | |
| 167 | + $image_alt = get_the_title( $post_id ); | |
| 168 | + $post_link = get_permalink( $post_id ); | |
| 169 | + } else { | |
| 170 | + | |
| 171 | + } | |
| 172 | + ?> | |
| 173 | + <div class="<?php echo esc_attr( $wrapper_classes ); ?>"> | |
| 174 | + <div class="eel-featured-image-inner"> | |
| 175 | + <?php if ( !empty($image_url) ) : ?> | |
| 176 | + <?php if ( !empty($settings['show_image_link']) && $settings['show_image_link'] === 'yes' && !empty($post_link) ) : ?> | |
| 177 | + <a href="<?php echo esc_url( $post_link ); ?>" class="eel-featured-image-link"> | |
| 178 | + <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $image_alt ?: __('Featured Image', 'easy-elements') ); ?>" class="eel-featured-image-img" loading="lazy" /> | |
| 179 | + </a> | |
| 180 | + <?php else : ?> | |
| 181 | + <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $image_alt ?: __('Featured Image', 'easy-elements') ); ?>" class="eel-featured-image-img" loading="lazy" /> | |
| 182 | + <?php endif; ?> | |
| 183 | + <?php else : ?> | |
| 184 | + <?php if ( $editor_mode ) { ?> | |
| 185 | + <div class="eel-featured-image-placeholder"> | |
| 186 | + <svg width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg" class="eel-featured-image-placeholder-icon"> | |
| 187 | + <defs> | |
| 188 | + <linearGradient id="imgGradient" x1="0" y1="0" x2="64" y2="64" gradientUnits="userSpaceOnUse"> | |
| 189 | + <stop stop-color="#7fd7ff"/> | |
| 190 | + <stop offset="1" stop-color="#0073e6"/> | |
| 191 | + </linearGradient> | |
| 192 | + </defs> | |
| 193 | + <rect x="8" y="14" width="48" height="36" rx="5" fill="#f0f6ff" stroke="url(#imgGradient)" stroke-width="2.5"/> | |
| 194 | + <circle cx="22" cy="28" r="5" fill="#b3d4fc" /> | |
| 195 | + <path d="M14 44L26 32L36 42L44 34L52 42" stroke="#b3d4fc" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/> | |
| 196 | + </svg> | |
| 197 | + <div class="eel-featured-image-placeholder-title">No Featured Image</div> | |
| 198 | + <div class="eel-featured-image-placeholder-desc">Please add a featured image to display here.</div> | |
| 199 | + <?php if ( $editor_mode ) : ?> | |
| 200 | + <a href="#" class="eel-featured-image-placeholder-btn" tabindex="-1">Set Featured Image</a> | |
| 201 | + <?php endif; ?> | |
| 202 | + </div> | |
| 203 | + <?php } endif; ?> | |
| 204 | + </div> | |
| 205 | + </div> | |
| 206 | + <?php | |
| 207 | + } | |
| 208 | +} | |