| 1 |
<?php |
| 2 |
class MaxGalleriaGalleryWidget extends WP_Widget { |
| 3 |
public function __construct() { |
| 4 |
$widget_id = 'maxgalleria-gallery-widget'; |
| 5 |
$widget_name = __('MaxGalleria Gallery', 'maxgalleria'); |
| 6 |
$widget_args = array('description' => __('Show images from a MaxGalleria gallery.', 'maxgalleria')); |
| 7 |
|
| 8 |
parent::__construct($widget_id, $widget_name, $widget_args); |
| 9 |
} |
| 10 |
|
| 11 |
// Outputs the contents of the widget |
| 12 |
public function widget($args, $instance) { |
| 13 |
$output = ''; |
| 14 |
|
| 15 |
$title = apply_filters('widget_title', $instance['title']); |
| 16 |
$gallery_id = (isset($instance['gallery_id'])) ? $instance['gallery_id'] : ''; |
| 17 |
$columns = (isset($instance['columns'])) ? $instance['columns'] : ''; |
| 18 |
$rows = (isset($instance['rows'])) ? $instance['rows'] : ''; |
| 19 |
$shape = (isset($instance['shape'])) ? $instance['shape'] : ''; |
| 20 |
$view_more_text = (isset($instance['view_more_text'])) ? $instance['view_more_text'] : ''; |
| 21 |
$view_more_url = (isset($instance['view_more_url'])) ? $instance['view_more_url'] : ''; |
| 22 |
|
| 23 |
$output .= $args['before_widget']; |
| 24 |
|
| 25 |
if (isset($title) && $title != '') { |
| 26 |
$output .= $args['before_title'] . $title . $args['after_title']; |
| 27 |
} |
| 28 |
|
| 29 |
if (isset($gallery_id) && $gallery_id != '') { |
| 30 |
$output .= $this->get_output($gallery_id, $columns, $rows, $shape, $view_more_text, $view_more_url); |
| 31 |
} |
| 32 |
|
| 33 |
$output .= $args['after_widget']; |
| 34 |
|
| 35 |
echo $output; |
| 36 |
} |
| 37 |
|
| 38 |
// Outputs the options in the admin |
| 39 |
public function form($instance) { |
| 40 |
$output = ''; |
| 41 |
$galleries = get_posts(array('post_type' => MAXGALLERIA_POST_TYPE, 'post_status' => 'publish', 'numberposts' => -1)); |
| 42 |
|
| 43 |
$thumbs_per_row = array(1 => 1, 2 => 2, 3 => 3); |
| 44 |
$number_of_rows = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5); |
| 45 |
$thumb_shapes = array( |
| 46 |
'landscape' => __('Landscape', 'maxgalleria'), |
| 47 |
'portrait' => __('Portrait', 'maxgalleria'), |
| 48 |
'square' => __('Square', 'maxgalleria') |
| 49 |
); |
| 50 |
|
| 51 |
// Form values |
| 52 |
$title = (isset($instance['title'])) ? $instance['title'] : __('Gallery Widget', 'maxgalleria'); |
| 53 |
$gallery_id = (isset($instance['gallery_id'])) ? $instance['gallery_id'] : ''; |
| 54 |
$columns = (isset($instance['columns'])) ? $instance['columns'] : ''; |
| 55 |
$rows = (isset($instance['rows'])) ? $instance['rows'] : ''; |
| 56 |
$shape = (isset($instance['shape'])) ? $instance['shape'] : ''; |
| 57 |
$view_more_text = (isset($instance['view_more_text'])) ? $instance['view_more_text'] : __('View More', 'maxgalleria'); |
| 58 |
$view_more_url = (isset($instance['view_more_url'])) ? $instance['view_more_url'] : ''; |
| 59 |
|
| 60 |
// Form layout |
| 61 |
$output .= '<table cellpadding="5" style="width: 100%; padding-top: 10px; padding-bottom: 10px;">'; |
| 62 |
$output .= ' <tr>'; |
| 63 |
$output .= ' <td width="100">' . __('Title: ', 'maxgalleria') . '</td>'; |
| 64 |
$output .= ' <td>'; |
| 65 |
$output .= ' <input class="widefat" type="text" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="' . esc_attr($title) . '" />'; |
| 66 |
$output .= ' </td>'; |
| 67 |
$output .= ' </tr>'; |
| 68 |
$output .= ' <tr>'; |
| 69 |
$output .= ' <td>' . __('Gallery: ', 'maxgalleria') . '</td>'; |
| 70 |
$output .= ' <td>'; |
| 71 |
$output .= ' <select class="widefat" id="' . $this->get_field_id('gallery_id') . '" name="' . $this->get_field_name('gallery_id') . '">'; |
| 72 |
$output .= ' <option value="">-- ' . __('Select Gallery', 'maxgalleria') . ' --</option>'; |
| 73 |
foreach ($galleries as $g) { |
| 74 |
$selected = ($gallery_id == $g->ID) ? 'selected="selected"' : ''; |
| 75 |
$output .= '<option value="' . $g->ID . '" ' . $selected . '>' . $g->post_title . '</option>'; |
| 76 |
} |
| 77 |
$output .= ' </select>'; |
| 78 |
$output .= ' </td>'; |
| 79 |
$output .= ' </tr>'; |
| 80 |
$output .= ' <tr>'; |
| 81 |
$output .= ' <td>' . __('Thumbs/Row: ', 'maxgalleria') . '</td>'; |
| 82 |
$output .= ' <td>'; |
| 83 |
$output .= ' <select class="widefat" id="' . $this->get_field_id('columns') . '" name="' . $this->get_field_name('columns') . '">'; |
| 84 |
$output .= ' <option value="">-- ' . __('Select Thumbs per Row', 'maxgalleria') . ' --</option>'; |
| 85 |
foreach ($thumbs_per_row as $key => $name) { |
| 86 |
$selected = ($columns == $key) ? 'selected="selected"' : ''; |
| 87 |
$output .= '<option value="' . $key . '" ' . $selected . '>' . $name . '</option>'; |
| 88 |
} |
| 89 |
$output .= ' </select>'; |
| 90 |
$output .= ' </td>'; |
| 91 |
$output .= ' </tr>'; |
| 92 |
$output .= ' <tr>'; |
| 93 |
$output .= ' <td>' . __('# of Rows: ', 'maxgalleria') . '</td>'; |
| 94 |
$output .= ' <td>'; |
| 95 |
$output .= ' <select class="widefat" id="' . $this->get_field_id('rows') . '" name="' . $this->get_field_name('rows') . '">'; |
| 96 |
$output .= ' <option value="">-- ' . __('Select # of Rows', 'maxgalleria') . ' --</option>'; |
| 97 |
foreach ($number_of_rows as $key => $name) { |
| 98 |
$selected = ($rows == $key) ? 'selected="selected"' : ''; |
| 99 |
$output .= '<option value="' . $key . '" ' . $selected . '>' . $name . '</option>'; |
| 100 |
} |
| 101 |
$output .= ' </select>'; |
| 102 |
$output .= ' </td>'; |
| 103 |
$output .= ' </tr>'; |
| 104 |
$output .= ' <tr>'; |
| 105 |
$output .= ' <td>' . __('Thumb Shape: ', 'maxgalleria') . '</td>'; |
| 106 |
$output .= ' <td>'; |
| 107 |
$output .= ' <select class="widefat" id="' . $this->get_field_id('shape') . '" name="' . $this->get_field_name('shape') . '">'; |
| 108 |
$output .= ' <option value="">-- ' . __('Select Thumb Shape', 'maxgalleria') . ' --</option>'; |
| 109 |
foreach ($thumb_shapes as $key => $name) { |
| 110 |
$selected = ($shape == $key) ? 'selected="selected"' : ''; |
| 111 |
$output .= '<option value="' . $key . '" ' . $selected . '>' . $name . '</option>'; |
| 112 |
} |
| 113 |
$output .= ' </select>'; |
| 114 |
$output .= ' </td>'; |
| 115 |
$output .= ' </tr>'; |
| 116 |
$output .= ' <tr>'; |
| 117 |
$output .= ' <td width="100">' . __('View More Text: ', 'maxgalleria') . '</td>'; |
| 118 |
$output .= ' <td>'; |
| 119 |
$output .= ' <input class="widefat" type="text" id="' . $this->get_field_id('view_more_text') . '" name="' . $this->get_field_name('view_more_text') . '" value="' . esc_attr($view_more_text) . '" />'; |
| 120 |
$output .= ' </td>'; |
| 121 |
$output .= ' </tr>'; |
| 122 |
$output .= ' <tr>'; |
| 123 |
$output .= ' <td>' . __('View More URL: ', 'maxgalleria') . '</td>'; |
| 124 |
$output .= ' <td>'; |
| 125 |
$output .= ' <input class="widefat" type="text" id="' . $this->get_field_id('view_more_url') . '" name="' . $this->get_field_name('view_more_url') . '" value="' . esc_attr($view_more_url) . '" />'; |
| 126 |
$output .= ' </td>'; |
| 127 |
$output .= ' </tr>'; |
| 128 |
$output .= '</table>'; |
| 129 |
|
| 130 |
echo $output; |
| 131 |
} |
| 132 |
|
| 133 |
// Saves the widget options |
| 134 |
public function update($new_instance, $old_instance) { |
| 135 |
$instance = array(); |
| 136 |
$instance['title'] = (isset($new_instance['title'])) ? strip_tags($new_instance['title']) : ''; |
| 137 |
$instance['gallery_id'] = (isset($new_instance['gallery_id'])) ? strip_tags($new_instance['gallery_id']) : ''; |
| 138 |
$instance['columns'] = (isset($new_instance['columns'])) ? strip_tags($new_instance['columns']) : ''; |
| 139 |
$instance['rows'] = (isset($new_instance['rows'])) ? strip_tags($new_instance['rows']) : ''; |
| 140 |
$instance['shape'] = (isset($new_instance['shape'])) ? strip_tags($new_instance['shape']) : ''; |
| 141 |
$instance['view_more_text'] = (isset($new_instance['view_more_text'])) ? strip_tags($new_instance['view_more_text']) : ''; |
| 142 |
$instance['view_more_url'] = (isset($new_instance['view_more_url'])) ? strip_tags($new_instance['view_more_url']) : ''; |
| 143 |
|
| 144 |
return $instance; |
| 145 |
} |
| 146 |
|
| 147 |
private function get_output($gallery_id, $columns, $rows, $shape, $view_more_text, $view_more_url) { |
| 148 |
$output = ''; |
| 149 |
$gallery = get_post($gallery_id); |
| 150 |
|
| 151 |
if (isset($gallery) && $gallery->post_status == 'publish') { |
| 152 |
global $maxgalleria; |
| 153 |
|
| 154 |
// Check to use defaults |
| 155 |
if ($columns == '') $columns = 3; |
| 156 |
if ($rows == '') $rows = 3; |
| 157 |
if ($shape == '') $shape = 'square'; |
| 158 |
if ($view_more_text == '') $view_more_text = __('View More', 'maxgalleria'); |
| 159 |
if ($view_more_url == '') $view_more_url = get_permalink($gallery->ID); |
| 160 |
|
| 161 |
// Set columns class |
| 162 |
$columns_class = ''; |
| 163 |
if ($columns == 1) $columns_class = 'mg-onecol'; |
| 164 |
if ($columns == 2) $columns_class = 'mg-twocol'; |
| 165 |
if ($columns == 3) $columns_class = 'mg-threecol'; |
| 166 |
|
| 167 |
// Determine how many attachments to retrieve |
| 168 |
$number_attachments = $columns * $rows; |
| 169 |
|
| 170 |
// Get gallery attachments |
| 171 |
$args = array( |
| 172 |
'post_parent' => $gallery->ID, |
| 173 |
'post_type' => 'attachment', |
| 174 |
'orderby' => 'menu_order', |
| 175 |
'order' => 'asc', |
| 176 |
'numberposts' => $number_attachments |
| 177 |
); |
| 178 |
$attachments = get_posts($args); |
| 179 |
|
| 180 |
// Add the stylesheet |
| 181 |
$widget_stylesheet = apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_STYLESHEET, MAXGALLERIA_PLUGIN_URL . '/widgets/gallery-widget.css'); |
| 182 |
wp_enqueue_style('maxgalleria-gallery-widget', $widget_stylesheet); |
| 183 |
|
| 184 |
// Build the output |
| 185 |
$output .= apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_BEFORE_OUTPUT, '', $gallery->ID); |
| 186 |
$output .= '<div id="maxgalleria-gallery-widget-' . $gallery->ID . '" class="mg-gallery-widget">'; |
| 187 |
$output .= ' <div class="mg-thumbs ' . $columns_class . '">'; |
| 188 |
$output .= ' <ul>'; |
| 189 |
|
| 190 |
foreach ($attachments as $attachment) { |
| 191 |
$excluded = get_post_meta($attachment->ID, 'maxgallery_attachment_image_exclude', true); |
| 192 |
if (!$excluded) { |
| 193 |
$title = $attachment->post_title; |
| 194 |
$caption = $attachment->post_excerpt; |
| 195 |
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true); |
| 196 |
|
| 197 |
$thumb_image = $maxgalleria->image_gallery->get_thumb_image($attachment, $shape, $columns); |
| 198 |
$thumb_image_element = '<img src="' . $thumb_image['url'] . '" width="' . $thumb_image['width'] . '" height="' . $thumb_image['height'] . '" alt="' . $alt . '" title="' . $title . '" />'; |
| 199 |
|
| 200 |
$output .= ' <li>'; |
| 201 |
$output .= apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_BEFORE_THUMB, ''); |
| 202 |
$output .= apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_THUMB, $thumb_image_element, $thumb_image, $alt, $title); |
| 203 |
$output .= apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_AFTER_THUMB, ''); |
| 204 |
$output .= ' </li>'; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
$output .= ' </ul>'; |
| 209 |
$output .= ' <div class="clear"></div>'; |
| 210 |
$output .= ' </div>'; |
| 211 |
|
| 212 |
// View more |
| 213 |
$view_more = '<a href="' . $view_more_url . '">' . $view_more_text . '</a>'; |
| 214 |
$output .= apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_BEFORE_VIEW_MORE, '<p>'); |
| 215 |
$output .= apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_VIEW_MORE, $view_more, $view_more_url, $view_more_text); |
| 216 |
$output .= apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_AFTER_VIEW_MORE, '</p>'); |
| 217 |
|
| 218 |
$output .= '</div>'; |
| 219 |
$output .= apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_AFTER_OUTPUT, '', $gallery->ID); |
| 220 |
} |
| 221 |
|
| 222 |
return apply_filters(MAXGALLERIA_FILTER_GALLERY_WIDGET_OUTPUT, $output, $gallery->ID); |
| 223 |
} |
| 224 |
} |
| 225 |
?> |