add_control(
* 'gallery',
* [
* 'label' => __( 'Add Images', 'plugin-domain' ),
* 'type' => Controls_Manager::GALLERY,
* 'default' => [],
* ]
* );
*
* PHP usage (inside `Widget_Base::render()` method):
*
* $images = $this->get_settings( 'gallery' );
* foreach ( $images as $image ) {
* echo '';
* }
*
* JS usage (inside `Widget_Base::_content_template()` method):
*
* <# _.each( settings.gallery, function( image ) { #>
*
* <# }); #>
*
* @since 1.0.0
*
* @param string $label Optional. The label that appears above of the
* field. Default is empty.
* @param string $title Optional. The field title that appears on mouse
* hover. Default is empty.
* @param string $description Optional. The description that appears below the
* field. Default is empty.
* @param array $default {
* Optional. Defautl gallery images. An array of images containing the image
* ID and URL: `[ [ 'id' => '', 'url' => '' ], [ 'id' => '', 'url' => '' ], ... ]`
* Default is an empty array.
*
* @type int $id Optional. Image id. Default is empty.
* @type string $url Optional. Image url. Default is empty.
* }
* @param string $separator Optional. Set the position of the control separator.
* Available values are 'default', 'before', 'after'
* and 'none'. 'default' will position the separator
* depending on the control type. 'before' / 'after'
* will position the separator before/after the
* control. 'none' will hide the separator. Default
* is 'none'.
* @param bool $show_label Optional. Whether to display the label. Default is
* true.
* @param bool $label_block Optional. Whether to display the label in a
* separate line. Default is true.
*
* @return array {
* An array of arrays containing the ID and URL for each image:
* `[ [ 'id' => '', 'url' => '' ], [ 'id' => '', 'url' => '' ], ... ]`
*
* @type int $id Media id.
* @type string $url Media url.
* }
*/
class Control_Gallery extends Base_Data_Control {
/**
* Retrieve gallery control type.
*
* @since 1.0.0
* @access public
*
* @return string Control type.
*/
public function get_type() {
return 'gallery';
}
/**
* Import gallery images.
*
* Used to import gallery control files from external sites while importing
* Elementor template JSON file, and replacing the old data.
*
* @since 1.0.0
* @access public
*
* @param array $settings Control settings
*
* @return array Control settings.
*/
public function on_import( $settings ) {
foreach ( $settings as &$attachment ) {
if ( empty( $attachment['url'] ) ) {
continue;
}
$attachment = Plugin::$instance->templates_manager->get_import_images_instance()->import( $attachment );
}
// Filter out attachments that don't exist
$settings = array_filter( $settings );
return $settings;
}
/**
* Render gallery control output in the editor.
*
* Used to generate the control HTML in the editor using Underscore JS
* template. The variables for the class are available using `data` JS
* object.
*
* @since 1.0.0
* @access public
*/
public function content_template() {
?>