PluginProbe
Gallery : FooGallery / 3.3.2
Gallery : FooGallery v3.3.2
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / extensions / default-templates / default / class-default-gallery-template.php
class-default-gallery-template.php
339 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 if ( ! class_exists( 'FooGallery_Default_Gallery_Template' ) ) {
8
9 define( 'FOOGALLERY_DEFAULT_GALLERY_TEMPLATE_URL', plugin_dir_url( __FILE__ ) );
10
11 class FooGallery_Default_Gallery_Template {
12
13 const TEMPLATE_ID = 'default';
14
15 const THUMBNAIL_DIMENSIONS_DEFAULT = array(
16 'width' => 270,
17 'height' => 230,
18 );
19
20 /**
21 * Wire up everything we need to run the extension
22 */
23 function __construct() {
24 // @formatter:off
25 add_filter( 'foogallery_gallery_templates', array( $this, 'add_template' ) );
26
27 add_filter( 'foogallery_gallery_templates_files', array( $this, 'register_myself' ) );
28
29 //build up the thumb dimensions from some arguments
30 add_filter( 'foogallery_calculate_thumbnail_dimensions-default', array( $this, 'build_thumbnail_dimensions_from_arguments' ), 10, 2 );
31
32 //build up the thumb dimensions on save
33 add_filter( 'foogallery_template_thumbnail_dimensions-default', array( $this, 'get_thumbnail_dimensions' ), 10, 2 );
34
35 //build up the arguments needed for rendering this template
36 add_filter( 'foogallery_gallery_template_arguments-default', array( $this, 'build_gallery_template_arguments' ) );
37
38 // add a style block for the gallery
39 add_action( 'foogallery_template_style_block-default', array( $this, 'add_css' ), 10, 2 );
40
41 // set defaults for the gallery
42 add_filter( 'foogallery_override_gallery_template_fields-default', array( $this, 'set_default_fields' ), 10, 2 );
43
44 //alter the crop value if needed
45 add_filter( 'foogallery_render_gallery_template_field_value', array( $this, 'alter_field_value'), 10, 4 );
46
47 // handle aliases for default template settings
48 add_filter( 'foogallery_gallery_template_argument_alias', array( $this, 'handle_alias' ), 10, 2 );
49 // @formatter:on
50 }
51
52 /**
53 * Handle aliases for gallery template settings saved in meta.
54 *
55 * @param string $key The key of the setting.
56 * @param string $template The template slug.
57 * @return string
58 */
59 function handle_alias( $key, $template ) {
60 if ( 'spacing' === $key && self::TEMPLATE_ID === $template ) {
61 return 'gap';
62 }
63 return $key;
64 }
65
66 /**
67 * Make sure the spacing value is set correctly from legacy values.
68 */
69 function alter_field_value( $value, $field, $gallery, $template ) {
70 //only do something if we are dealing with the thumbnail_dimensions field in this template
71 if ( self::TEMPLATE_ID === $template['slug'] && 'spacing' === $field['id'] ) {
72 if ( strpos( $value, 'fg-gutter-' ) === 0 ) {
73 $value = foogallery_intval( $value );
74 }
75 }
76
77 return $value;
78 }
79
80 /**
81 * Add css to the page for the gallery
82 *
83 * @param $gallery FooGallery
84 */
85 function add_css( $css, $gallery ) {
86 $id = $gallery->container_id();
87 $dimensions = foogallery_gallery_template_setting( 'thumbnail_dimensions' , self::THUMBNAIL_DIMENSIONS_DEFAULT );
88 $layout = foogallery_gallery_template_setting( 'layout' );
89 $width = 0;
90 $height = 0;
91 if ( is_array( $dimensions ) && array_key_exists( 'width', $dimensions ) && intval( $dimensions['width'] ) > 0 ) {
92 $width = intval( $dimensions['width'] );
93 }
94 if ( is_array( $dimensions ) && array_key_exists( 'height', $dimensions ) && intval( $dimensions['height'] ) > 0 ) {
95 $height = intval( $dimensions['height'] );
96 }
97 if ( !empty( $layout ) ) {
98 if ( $width > 0 && $height > 0 ) {
99 $css[] = '#' . $id . ' { --fg-column-max-width: ' . $width . 'px; }';
100 }
101 } else {
102 //default layout. Do as before.
103 if ( $width > 0 ) {
104 $css[] = '#' . $id . ' .fg-image { width: ' . $width . 'px; }';
105 }
106 }
107 $spacing = foogallery_intval( foogallery_gallery_template_setting( 'spacing', '10' ) );
108 if ( $spacing >= 0 ) {
109 $css[] = '#' . $id . ' { --fg-gutter: ' . $spacing . 'px; }';
110 }
111
112 $mobile_spacing = foogallery_gallery_template_mobile_setting( 'mobile_spacing', null );
113 if ( is_scalar( $mobile_spacing ) && '' !== trim( (string) $mobile_spacing ) ) {
114 $mobile_spacing = max( 0, min( 100, foogallery_intval( $mobile_spacing ) ) );
115 $mobile_rule = '#' . $id . ' { --fg-gutter: ' . $mobile_spacing . 'px; }';
116 $css[] = '@media only screen and (max-width: ' . foogallery_get_mobile_size() . 'px) { ' . $mobile_rule . ' }';
117 $css[] = '.foogallery-preview-wrapper.viewport-mobile ' . $mobile_rule;
118 }
119
120 return $css;
121 }
122
123 /**
124 * Register myself so that all associated JS and CSS files can be found and automatically included
125 *
126 * @param $extensions
127 *
128 * @return array
129 */
130 function register_myself( $extensions ) {
131 $extensions[] = __FILE__;
132
133 return $extensions;
134 }
135
136 /**
137 * Add our gallery template to the list of templates available for every gallery
138 *
139 * @param $gallery_templates
140 *
141 * @return array
142 */
143 function add_template( $gallery_templates ) {
144 $gallery_templates[self::TEMPLATE_ID] = array(
145 'slug' => self::TEMPLATE_ID,
146 'name' => __( 'Responsive', 'foogallery' ),
147 'preview_support' => true,
148 'common_fields_support' => true,
149 'paging_support' => true,
150 'lazyload_support' => true,
151 'mandatory_classes' => 'fg-default',
152 'thumbnail_dimensions' => true,
153 'filtering_support' => true,
154 'enqueue_core' => true,
155 'icon' => '<svg viewBox="0 0 24 24">
156 <rect x="3" y="3" width="7" height="7"/>
157 <rect x="14" y="3" width="7" height="7"/>
158 <rect x="3" y="14" width="7" height="7"/>
159 <rect x="14" y="14" width="7" height="7"/>
160 </svg>',
161 'fields' => array(
162 array(
163 'id' => 'thumbnail_dimensions',
164 'title' => __( 'Thumbnail Size', 'foogallery' ),
165 'desc' => __( 'Choose the size of your thumbnails.', 'foogallery' ),
166 'section_id' => 'general',
167 'alias' => 'thumbnail_size',
168 'type' => 'thumb_size_no_crop',
169 'for' => 'thumbnail_dimensions_width',
170 'default' => self::THUMBNAIL_DIMENSIONS_DEFAULT,
171 'row_data' => array(
172 'data-foogallery-change-selector' => 'input',
173 'data-foogallery-preview' => 'shortcode'
174 )
175 ),
176 array(
177 'id' => 'layout',
178 'title' => __( 'Columns', 'foogallery' ),
179 'desc' => __( 'The number of columns to use. Auto will use all available space.', 'foogallery' ),
180 'section_id' => 'general',
181 'default' => '',
182 'type' => 'radio',
183 'alias' => 'columns',
184 'class' => 'foogallery-radios-stacked',
185 'choices' => array(
186 '' => __( 'Auto', 'foogallery' ),
187 'fg-d-col1' => __( '1 Column', 'foogallery' ),
188 'fg-d-col2' => __( '2 Columns', 'foogallery' ),
189 'fg-d-col3' => __( '3 Columns', 'foogallery' ),
190 'fg-d-col4' => __( '4 Columns', 'foogallery' ),
191 'fg-d-col5' => __( '5 Columns', 'foogallery' ),
192 'fg-d-col6' => __( '6 Columns', 'foogallery' ),
193 ),
194 'mobile' => array(
195 'free' => true,
196 'id' => 'mobile_columns',
197 'alias' => 'mobile_columns',
198 'default' => 'fg-m-col2',
199 'choices' => array(
200 '' => __( 'Auto', 'foogallery' ),
201 'fg-m-col1' => __( '1 Column', 'foogallery' ),
202 'fg-m-col2' => __( '2 Columns', 'foogallery' ),
203 'fg-m-col3' => __( '3 Columns', 'foogallery' ),
204 ),
205 ),
206 'row_data' => array(
207 'data-foogallery-change-selector' => 'input:radio',
208 'data-foogallery-preview' => 'shortcode'
209 )
210 ),
211 array(
212 'id' => 'thumbnail_link',
213 'title' => __( 'Thumbnail Link', 'foogallery' ),
214 'section_id' => 'general',
215 'default' => 'image',
216 'type' => 'thumb_link',
217 ),
218 array(
219 'id' => 'lightbox',
220 'type' => 'lightbox',
221 ),
222 array(
223 'id' => 'spacing',
224 'title' => __( 'Thumbnail Gap', 'foogallery' ),
225 'desc' => __( 'The spacing or gap between thumbnails in the gallery.', 'foogallery' ),
226 'section_id' => 'general',
227 'alias' => 'gap',
228 'type' => 'slider',
229 'min' => 0,
230 'max' => 100,
231 'step' => 1,
232 'default' => '10',
233 'mobile' => true,
234 'row_data' => array(
235 'data-foogallery-change-selector' => 'range-input',
236 'data-foogallery-preview' => 'shortcode'
237 )
238 ),
239 array(
240 'id' => 'alignment',
241 'title' => __( 'Alignment', 'foogallery' ),
242 'desc' => __( 'The horizontal alignment of the thumbnails inside the gallery.', 'foogallery' ),
243 'section_id' => 'general',
244 'default' => 'fg-center',
245 'type' => 'radio',
246 'choices' => array(
247 'fg-left' => __( 'Left', 'foogallery' ),
248 'fg-center' => __( 'Center', 'foogallery' ),
249 'fg-right' => __( 'Right', 'foogallery' ),
250 ),
251 'row_data' => array(
252 'data-foogallery-change-selector' => 'input:radio',
253 'data-foogallery-preview' => 'shortcode'
254 )
255 )
256 )
257 );
258
259 return $gallery_templates;
260 }
261
262 /**
263 * Builds thumb dimensions from arguments
264 *
265 * @param array $dimensions
266 * @param array $arguments
267 *
268 * @return mixed
269 */
270 function build_thumbnail_dimensions_from_arguments( $dimensions, $arguments ) {
271 if ( array_key_exists( 'thumbnail_dimensions', $arguments ) ) {
272 return array(
273 'height' => intval( $arguments['thumbnail_dimensions']['height'] ),
274 'width' => intval( $arguments['thumbnail_dimensions']['width'] ),
275 'crop' => '1'
276 );
277 }
278
279 return null;
280 }
281
282 /**
283 * Get the thumb dimensions arguments saved for the gallery for this gallery template
284 *
285 * @param array $dimensions
286 * @param FooGallery $foogallery
287 *
288 * @return mixed
289 */
290 function get_thumbnail_dimensions( $dimensions, $foogallery ) {
291 $dimensions = $foogallery->get_meta( 'default_thumbnail_dimensions', self::THUMBNAIL_DIMENSIONS_DEFAULT );
292 $dimensions['crop'] = true;
293
294 return $dimensions;
295 }
296
297 /**
298 * Build up the arguments needed for rendering this gallery template
299 *
300 * @param $args
301 *
302 * @return array
303 */
304 function build_gallery_template_arguments( $args ) {
305 $args = foogallery_gallery_template_setting( 'thumbnail_dimensions', self::THUMBNAIL_DIMENSIONS_DEFAULT );
306 $args['crop'] = '1'; //we now force thumbs to be cropped
307 $args['link'] = foogallery_gallery_template_setting( 'thumbnail_link', 'image' );
308
309 return $args;
310 }
311
312 /**
313 * Set default values for the gallery template
314 *
315 * @uses "foogallery_override_gallery_template_fields"
316 * @param $fields
317 * @param $template
318 *
319 * @return array
320 */
321 function set_default_fields( $fields, $template ) {
322 //update specific fields
323 foreach ($fields as &$field) {
324 if ( 'hover_effect_type' === $field['id'] ) {
325 $field['default'] = 'preset';
326 } else if ( 'hover_effect_preset' === $field['id'] ) {
327 $field['default'] = 'fg-preset fg-brad';
328 } else if ( 'border_size' === $field['id'] ) {
329 $field['default'] = '';
330 } else if ( 'drop_shadow' === $field['id'] ) {
331 $field['default'] = 'fg-shadow-medium';
332 }
333 }
334
335 return $fields;
336 }
337 }
338 }
339