/** * Setup functionality needed by the widget. * * @since 3.0.0 */ public static function load() { add_action('init', array(__CLASS__, 'l10n')); add_action('widgets_init', array(__CLASS__, 'register_widget')); if (is_simple_image_widget_legacy()) { return; } add_action('init', array(__CLASS__, 'init')); add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_scripts')); add_action('admin_head-widgets.php', array(__CLASS__, 'admin_head_widgets')); add_action('admin_footer-widgets.php', array(__CLASS__, 'admin_footer_widgets')); }
/** * Set up the widget. * * @since 3.0.0 */ public function load() { self::load_textdomain(); add_action('widgets_init', array($this, 'register_widget')); $compat = new Simple_Image_Widget_Legacy(); $compat->load(); if (is_simple_image_widget_legacy()) { return; } add_action('init', array($this, 'register_assets')); add_action('sidebar_admin_setup', array($this, 'enqueue_admin_assets')); add_filter('screen_settings', array($this, 'widgets_screen_settings'), 10, 2); add_action('wp_ajax_simple_image_widget_preferences', array($this, 'ajax_save_user_preferences')); }
/** * Display the form to edit widget settings. * * @since 3.0.0 * * @param array $instance The widget settings. */ public function form($instance) { $instance = wp_parse_args((array) $instance, array('alt' => '', 'image' => '', 'image_id' => '', 'image_size' => 'full', 'link' => '', 'link_classes' => '', 'link_text' => '', 'new_window' => '', 'title' => '', 'text' => '')); $instance['image_id'] = absint($instance['image_id']); $instance['title'] = wp_strip_all_tags($instance['title']); $button_class = array('button', 'button-hero', 'simple-image-widget-control-choose'); $image_id = $instance['image_id']; /** * The list of fields to display. * * The order of fields can be modified, new fields can be registered, or * existing fields can be removed here. Use the widget type id to limit * fields to a particular type of widget. * * @since 3.0.0 * * @param array $fields List of field ids. * @param string $id_base Widget type id. */ $fields = (array) apply_filters('simple_image_widget_fields', $this->form_fields(), $this->id_base); ?> <div class="simple-image-widget-form"> <?php /** * Display additional information or HTML before the widget edit form. * * @since 3.0.0 * * @param array $instance The widget setttings. * @param string $id_base Widget type id. */ do_action('simple_image_widget_form_before', $instance, $this->id_base); ?> <p> <label for="<?php echo esc_attr($this->get_field_id('title')); ?> "><?php _e('Title:', 'simple-image-widget'); ?> </label> <input type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?> " id="<?php echo esc_attr($this->get_field_id('title')); ?> " value="<?php echo esc_attr($instance['title']); ?> " class="widefat"> </p> <?php if (!is_simple_image_widget_legacy()) { ?> <p class="simple-image-widget-control<?php echo $image_id ? ' has-image' : ''; ?> " data-title="<?php esc_attr_e('Choose an Image', 'simple-image-widget'); ?> " data-update-text="<?php esc_attr_e('Update Image', 'simple-image-widget'); ?> " data-target=".image-id"> <?php if ($image_id) { echo wp_get_attachment_image($image_id, 'medium', false); unset($button_class[array_search('button-hero', $button_class)]); } ?> <input type="hidden" name="<?php echo esc_attr($this->get_field_name('image_id')); ?> " id="<?php echo esc_attr($this->get_field_id('image_id')); ?> " value="<?php echo absint($image_id); ?> " class="image-id simple-image-widget-control-target"> <a href="#" class="<?php echo esc_attr(join(' ', $button_class)); ?> "><?php _e('Choose an Image', 'simple-image-widget'); ?> </a> </p> <?php } ?> <?php if (!empty($fields)) { foreach ($fields as $field) { switch ($field) { case 'image_size': $sizes = $this->get_image_sizes($image_id); ?> <p class="<?php echo esc_attr($this->siw_field_class('image_size')); ?> "> <label for="<?php echo esc_attr($this->get_field_id('image_size')); ?> "><?php _e('Size:', 'simple-image-widget'); ?> </label> <select name="<?php echo esc_attr($this->get_field_name('image_size')); ?> " id="<?php echo esc_attr($this->get_field_id('image_size')); ?> " class="widefat image-size"<?php echo sizeof($sizes) < 2 ? ' disabled="disabled"' : ''; ?> > <?php foreach ($sizes as $id => $label) { printf('<option value="%s"%s>%s</option>', esc_attr($id), selected($instance['image_size'], $id, false), esc_html($label)); } ?> </select> </p> <?php break; case 'link': ?> <p class="<?php echo esc_attr($this->siw_field_class('link')); ?> "> <label for="<?php echo esc_attr($this->get_field_id('link')); ?> "><?php _e('Link:', 'simple-image-widget'); ?> </label> <input type="text" name="<?php echo esc_attr($this->get_field_name('link')); ?> " id="<?php echo esc_attr($this->get_field_id('link')); ?> " value="<?php echo esc_url($instance['link']); ?> " class="widefat"> </p> <p class="<?php echo esc_attr($this->siw_field_class('new_window')); ?> " style="margin-top: -0.75em; padding-left: 2px"> <label for="<?php echo esc_attr($this->get_field_id('new_window')); ?> "> <input type="checkbox" name="<?php echo esc_attr($this->get_field_name('new_window')); ?> " id="<?php echo esc_attr($this->get_field_id('new_window')); ?> " <?php checked($instance['new_window']); ?> > <?php _e('Open in new window?', 'simple-image-widget'); ?> </label> </p> <?php break; case 'link_classes': ?> <p class="<?php echo esc_attr($this->siw_field_class('link_classes')); ?> "> <label for="<?php echo esc_attr($this->get_field_id('link_classes')); ?> "><?php _e('Link Classes:', 'simple-image-widget'); ?> </label> <input type="text" name="<?php echo esc_attr($this->get_field_name('link_classes')); ?> " id="<?php echo esc_attr($this->get_field_id('link_classes')); ?> " value="<?php echo esc_attr($instance['link_classes']); ?> " class="widefat"> </p> <?php break; case 'link_text': ?> <p class="<?php echo esc_attr($this->siw_field_class('link_text')); ?> "> <label for="<?php echo esc_attr($this->get_field_id('link_text')); ?> "><?php _e('Link Text:', 'simple-image-widget'); ?> </label> <input type="text" name="<?php echo esc_attr($this->get_field_name('link_text')); ?> " id="<?php echo esc_attr($this->get_field_id('link_text')); ?> " value="<?php echo esc_attr($instance['link_text']); ?> " class="widefat"> </p> <?php break; case 'text': ?> <p class="<?php echo esc_attr($this->siw_field_class('text')); ?> "> <label for="<?php echo esc_attr($this->get_field_id('text')); ?> "><?php _e('Text:', 'simple-image-widget'); ?> </label> <textarea name="<?php echo esc_attr($this->get_field_name('text')); ?> " id="<?php echo esc_attr($this->get_field_id('text')); ?> " rows="4" class="widefat"><?php echo esc_textarea($instance['text']); ?> </textarea> </p> <?php break; default: /** * Display a custom field. * * This action will fire for custom fields * registered with the 'simple_image_widget_fields' * filter. * * @since 3.0.0 * * @param array $instance The widget setttings. * @param string $widget Widget instance. */ do_action('simple_image_widget_field-' . sanitize_key($field), $instance, $this); } } } /** * Display additional information or HTML after the widget edit form. * * @since 3.0.0 * * @param array $instance The widget setttings. * @param string $id_base Widget type id. */ do_action('simple_image_widget_form_after', $instance, $this->id_base); ?> </div><!-- /.simple-image-widget-form --> <?php }
/** * Display legacy fields in the widget edit form. * * @since 4.0.0 * * @param array $instance The widget instance settings. * @param WP_Widget $widget Widget instance. */ public function display_fields($instance, $widget) { if (is_simple_image_widget_legacy() || !empty($instance['image'])) { ?> <div class="simple-image-widget-legacy-fields"> <?php if (!is_simple_image_widget_legacy()) { ?> <p> <em><?php _e('These fields are here to maintain your data from an earlier version.', 'simple-image-widget'); ?> </em> </p> <p> <em><?php _e('Select an image, then clear these values, and they will disappear when you save the widget.', 'simple-image-widget'); ?> </em> </p> <?php } ?> <p> <label for="<?php echo esc_attr($widget->get_field_id('image')); ?> "><?php _e('Image URL:', 'simple-image-widget'); ?> </label> <input type="text" name="<?php echo esc_attr($widget->get_field_name('image')); ?> " id="<?php echo esc_attr($widget->get_field_id('image')); ?> " value="<?php echo esc_url($instance['image']); ?> " class="widefat"> </p> <p> <label for="<?php echo esc_attr($widget->get_field_id('alt')); ?> "><?php _e('Alternate Text:', 'simple-image-widget'); ?> </label> <input type="text" name="<?php echo esc_attr($widget->get_field_name('alt')); ?> " id="<?php echo esc_attr($widget->get_field_id('alt')); ?> " value="<?php echo esc_attr($instance['alt']); ?> " class="widefat"> </p> </div> <?php } }
/** * The list of extra fields that should be shown in the widget form. * * Can be easily overloaded by a child class. * * @since 3.0.0 */ function form_fields() { $fields = array('link', 'link_text', 'text'); // Don't show the image size field for users with older WordPress versions. if (!is_simple_image_widget_legacy()) { array_unshift($fields, 'image_size'); } return $fields; }
function form($instance) { $defaults = array('author' => '', 'qty' => '', 'category' => array(), 'autoplay' => '5000', 'orderby' => 'date', 'asc' => 'DESC', 'style' => 'title', 'excerpt_length' => '17'); $instance = wp_parse_args($instance, $defaults); extract($instance); $button_class = array('button', 'button-hero', 'simple-image-widget-control-choose'); $terms = get_terms('category'); $all_terms = array(); foreach ($terms as $term) { $all_terms[$term->term_id] = $term->name; } $orderby_options = array('date' => __('Date', 'engine'), 'title' => __('Title', 'engine'), 'rand' => __('Random', 'engine'), 'comment_count' => __('Comments', 'engine')); $order_options = array('DESC' => 'DESC', 'ASC' => 'ASC'); $style_options = array('title' => __('Title Only', 'engine'), 'title_meta' => __('Title & Meta', 'engine'), 'title_meta_thumb_side' => __('Title, Meta & Thumb (thumb on side)', 'engine'), 'title_meta_thumb_1' => __('Title, Meta & Thumb (1 column)', 'engine'), 'title_meta_thumb_2' => __('Title, Meta & Thumb (2 columns)', 'engine'), 'title_meta_thumb_3' => __('Title, Meta & Thumb (3 columns)', 'engine'), 'title_meta_thumb_4' => __('Title, Meta & Thumb (4 columns)', 'engine'), 'title_meta_excerpt' => __('Title, Meta & Excerpt', 'engine')); ?> <p class="description"> <label for="<?php echo $this->get_field_id('title'); ?> "> <?php _e('Title (optional)', 'engine'); ?> <?php echo aq_field_input('title', $block_id, $title, $size = 'full'); ?> </label> </p> <p class="description"> <label for="<?php echo $this->get_field_id('author'); ?> "> <?php _e('Author', 'engine'); ?> <?php echo aq_field_input('author', $block_id, $author, $size = 'full'); ?> </label> </p> <?php if (!is_simple_image_widget_legacy()) { ?> <p class="simple-image-widget-control<?php echo $image_id ? ' has-image' : ''; ?> " data-title="<?php esc_attr_e('Choose an Image', 'simple-image-widget'); ?> " data-update-text="<?php esc_attr_e('Update Image', 'simple-image-widget'); ?> " data-target=".image-id"> <?php if ($image_id) { echo wp_get_attachment_image($image_id, 'thumbnail', false); //echo wp_get_attachment_image( $image_id, array(200,200), false ); unset($button_class[array_search('button-hero', $button_class)]); } ?> <input type="hidden" name="<?php echo esc_attr($this->get_field_name('image_id')); ?> " id="<?php echo esc_attr($this->get_field_id('image_id')); ?> " value="<?php echo absint($image_id); ?> " class="image-id simple-image-widget-control-target"> <a href="#" class="<?php echo esc_attr(join(' ', $button_class)); ?> "><?php _e('Choose an Image', 'simple-image-widget'); ?> </a> </p> <?php } ?> <p class="description"> <label for="<?php echo $this->get_field_id('style'); ?> "> <?php _e('Style', 'engine'); ?> <?php echo aq_field_select('style', $block_id, $style_options, $style); ?> </label> </p> <p class="description"> <label for="<?php echo $this->get_field_id('qty'); ?> "> <?php _e('Number of Posts', 'engine'); ?> <?php echo aq_field_input('qty', $block_id, $qty, $size = 'full'); ?> </label> </p> <p class="description"> <label for="<?php echo $this->get_field_id('category'); ?> "> <?php _e('Categories', 'engine'); ?> <?php echo aq_field_multiselect('category', $block_id, $all_terms, $category); ?> </label> </p> <p class="description"> <label for="<?php echo $this->get_field_id('orderby'); ?> "> <?php _e('Order By', 'engine'); ?> <?php echo aq_field_select('orderby', $block_id, $orderby_options, $orderby); ?> </label> </p> <p class="description"> <label for="<?php echo $this->get_field_id('asc'); ?> "> <?php _e('Order', 'engine'); ?> <?php echo aq_field_select('asc', $block_id, $order_options, $asc); ?> </label> </p> <p class="description"> <label for="<?php echo $this->get_field_id('excerpt_length'); ?> "> <?php _e('Excerpt Length (optional)', 'engine'); ?> <?php echo aq_field_input('excerpt_length', $block_id, $excerpt_length, $size = 'full'); ?> </label> </p> <?php }