| 1 |
<?php |
| 2 |
namespace Elementor\Modules\AtomicWidgets\Widgets; |
| 3 |
|
| 4 |
use Elementor\Utils; |
| 5 |
use Elementor\Modules\AtomicWidgets\Controls\Section; |
| 6 |
use Elementor\Modules\AtomicWidgets\Controls\Types\Select_Control; |
| 7 |
use Elementor\Modules\AtomicWidgets\Base\Atomic_Widget_Base; |
| 8 |
use Elementor\Modules\AtomicWidgets\Schema\Atomic_Prop; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
class Atomic_Image extends Atomic_Widget_Base { |
| 15 |
public function get_icon() { |
| 16 |
return 'eicon-image'; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_title() { |
| 20 |
return esc_html__( 'Atomic Image', 'elementor' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function get_name() { |
| 24 |
return 'a-image'; |
| 25 |
} |
| 26 |
|
| 27 |
protected function render() { |
| 28 |
$settings = $this->get_atomic_settings(); |
| 29 |
|
| 30 |
// TODO: Replace with actual URL prop |
| 31 |
$image_url = $settings['url']; |
| 32 |
|
| 33 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 34 |
echo "<img src='" . esc_url( $image_url ) . "' />"; |
| 35 |
} |
| 36 |
|
| 37 |
private function get_image_size_options() { |
| 38 |
$wp_image_sizes = self::get_wp_image_sizes(); |
| 39 |
|
| 40 |
$image_sizes = []; |
| 41 |
|
| 42 |
foreach ( $wp_image_sizes as $size_key => $size_attributes ) { |
| 43 |
|
| 44 |
$control_title = ucwords( str_replace( '_', ' ', $size_key ) ); |
| 45 |
|
| 46 |
if ( is_array( $size_attributes ) ) { |
| 47 |
$control_title .= sprintf( ' - %d*%d', $size_attributes['width'], $size_attributes['height'] ); |
| 48 |
} |
| 49 |
|
| 50 |
$image_sizes[] = [ |
| 51 |
'label' => $control_title, |
| 52 |
'value' => $size_key, |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
$image_sizes[] = [ |
| 57 |
'label' => esc_html__( 'Full', 'elementor' ), |
| 58 |
'value' => 'full', |
| 59 |
]; |
| 60 |
|
| 61 |
return $image_sizes; |
| 62 |
} |
| 63 |
|
| 64 |
private static function get_wp_image_sizes() { |
| 65 |
$default_image_sizes = get_intermediate_image_sizes(); |
| 66 |
$additional_sizes = wp_get_additional_image_sizes(); |
| 67 |
|
| 68 |
$image_sizes = []; |
| 69 |
|
| 70 |
foreach ( $default_image_sizes as $size ) { |
| 71 |
$image_sizes[ $size ] = [ |
| 72 |
'width' => (int) get_option( $size . '_size_w' ), |
| 73 |
'height' => (int) get_option( $size . '_size_h' ), |
| 74 |
'crop' => (bool) get_option( $size . '_crop' ), |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
if ( $additional_sizes ) { |
| 79 |
$image_sizes = array_merge( $image_sizes, $additional_sizes ); |
| 80 |
} |
| 81 |
|
| 82 |
// /** This filter is documented in wp-admin/includes/media.php */ |
| 83 |
return apply_filters( 'image_size_names_choose', $image_sizes ); |
| 84 |
} |
| 85 |
|
| 86 |
protected function define_atomic_controls(): array { |
| 87 |
$options = $this->get_image_size_options(); |
| 88 |
|
| 89 |
$resolution_control = Select_Control::bind_to( 'image_size' ) |
| 90 |
->set_label( esc_html__( 'Image Resolution', 'elementor' ) ) |
| 91 |
->set_options( $options ); |
| 92 |
|
| 93 |
$content_section = Section::make() |
| 94 |
->set_label( esc_html__( 'Content', 'elementor' ) ) |
| 95 |
->set_items( [ |
| 96 |
$resolution_control, |
| 97 |
]); |
| 98 |
|
| 99 |
return [ |
| 100 |
$content_section, |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
protected static function define_props_schema(): array { |
| 105 |
return [ |
| 106 |
'image_size' => Atomic_Prop::make() |
| 107 |
->default( 'large' ), |
| 108 |
|
| 109 |
'url' => Atomic_Prop::make() |
| 110 |
->default( Utils::get_placeholder_image_src() ), |
| 111 |
]; |
| 112 |
} |
| 113 |
} |
| 114 |
|