PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.2.6
Gallery : FooGallery v3.2.6
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
foogallery / extensions / default-templates / default Last commit date
js 1 month ago class-default-gallery-template.php 1 month ago gallery-default.php 1 month ago
class-default-gallery-template.php
337 lines
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 return $css;
113 }
114
115 /**
116 * Register myself so that all associated JS and CSS files can be found and automatically included
117 *
118 * @param $extensions
119 *
120 * @return array
121 */
122 function register_myself( $extensions ) {
123 $extensions[] = __FILE__;
124
125 return $extensions;
126 }
127
128 /**
129 * Add our gallery template to the list of templates available for every gallery
130 *
131 * @param $gallery_templates
132 *
133 * @return array
134 */
135 function add_template( $gallery_templates ) {
136 $gallery_templates[self::TEMPLATE_ID] = array(
137 'slug' => self::TEMPLATE_ID,
138 'name' => __( 'Responsive', 'foogallery' ),
139 'preview_support' => true,
140 'common_fields_support' => true,
141 'paging_support' => true,
142 'lazyload_support' => true,
143 'mandatory_classes' => 'fg-default',
144 'thumbnail_dimensions' => true,
145 'filtering_support' => true,
146 'enqueue_core' => true,
147 'icon' => '<svg viewBox="0 0 24 24">
148 <rect x="3" y="3" width="7" height="7"/>
149 <rect x="14" y="3" width="7" height="7"/>
150 <rect x="3" y="14" width="7" height="7"/>
151 <rect x="14" y="14" width="7" height="7"/>
152 </svg>',
153 'fields' => array(
154 array(
155 'id' => 'thumbnail_dimensions',
156 'title' => __( 'Thumbnail Size', 'foogallery' ),
157 'desc' => __( 'Choose the size of your thumbnails.', 'foogallery' ),
158 'section' => __( 'General', 'foogallery' ),
159 'alias' => 'thumbnail_size',
160 'type' => 'thumb_size_no_crop',
161 'for' => 'thumbnail_dimensions_width',
162 'default' => self::THUMBNAIL_DIMENSIONS_DEFAULT,
163 'row_data' => array(
164 'data-foogallery-change-selector' => 'input',
165 'data-foogallery-preview' => 'shortcode'
166 )
167 ),
168 array(
169 'id' => 'layout',
170 'title' => __( 'Columns', 'foogallery' ),
171 'desc' => __( 'The number of columns to use. Auto will use all available space. Otherwise, you can force the number of columns to show on desktop.', 'foogallery' ),
172 'section' => __( 'General', 'foogallery' ),
173 'default' => '',
174 'type' => 'radio',
175 'alias' => 'columns',
176 'class' => 'foogallery-radios-stacked',
177 'choices' => array(
178 '' => __( 'Auto', 'foogallery' ),
179 'fg-d-col1' => __( '1 Column', 'foogallery' ),
180 'fg-d-col2' => __( '2 Columns', 'foogallery' ),
181 'fg-d-col3' => __( '3 Columns', 'foogallery' ),
182 'fg-d-col4' => __( '4 Columns', 'foogallery' ),
183 'fg-d-col5' => __( '5 Columns', 'foogallery' ),
184 'fg-d-col6' => __( '6 Columns', 'foogallery' ),
185 ),
186 'row_data' => array(
187 'data-foogallery-change-selector' => 'input:radio',
188 'data-foogallery-preview' => 'shortcode'
189 )
190 ),
191 array(
192 'id' => 'mobile_columns',
193 'title' => __( 'Mobile Columns', 'foogallery' ),
194 'desc' => __( 'Number of columns to show on mobile (screen widths less than 600px)', 'foogallery' ),
195 'section' => __( 'General', 'foogallery' ),
196 'default' => '',
197 'type' => 'radio',
198 'class' => 'foogallery-radios-stacked',
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 'row_data' => array(
206 'data-foogallery-change-selector' => 'input:radio',
207 'data-foogallery-preview' => 'shortcode'
208 )
209 ),
210 array(
211 'id' => 'thumbnail_link',
212 'title' => __( 'Thumbnail Link', 'foogallery' ),
213 'section' => __( 'General', 'foogallery' ),
214 'default' => 'image',
215 'type' => 'thumb_link',
216 ),
217 array(
218 'id' => 'lightbox',
219 'type' => 'lightbox',
220 ),
221 array(
222 'id' => 'spacing',
223 'title' => __( 'Thumbnail Gap', 'foogallery' ),
224 'desc' => __( 'The spacing or gap between thumbnails in the gallery.', 'foogallery' ),
225 'section' => __( 'General', 'foogallery' ),
226 'alias' => 'gap',
227 'type' => 'slider',
228 'min' => 0,
229 'max' => 100,
230 'step' => 1,
231 'default' => '10',
232 'row_data' => array(
233 'data-foogallery-change-selector' => 'range-input',
234 'data-foogallery-preview' => 'shortcode'
235 )
236 ),
237 array(
238 'id' => 'alignment',
239 'title' => __( 'Alignment', 'foogallery' ),
240 'desc' => __( 'The horizontal alignment of the thumbnails inside the gallery.', 'foogallery' ),
241 'section' => __( 'General', 'foogallery' ),
242 'default' => 'fg-center',
243 'type' => 'radio',
244 'choices' => array(
245 'fg-left' => __( 'Left', 'foogallery' ),
246 'fg-center' => __( 'Center', 'foogallery' ),
247 'fg-right' => __( 'Right', 'foogallery' ),
248 ),
249 'row_data' => array(
250 'data-foogallery-change-selector' => 'input:radio',
251 'data-foogallery-preview' => 'shortcode'
252 )
253 )
254 )
255 );
256
257 return $gallery_templates;
258 }
259
260 /**
261 * Builds thumb dimensions from arguments
262 *
263 * @param array $dimensions
264 * @param array $arguments
265 *
266 * @return mixed
267 */
268 function build_thumbnail_dimensions_from_arguments( $dimensions, $arguments ) {
269 if ( array_key_exists( 'thumbnail_dimensions', $arguments ) ) {
270 return array(
271 'height' => intval( $arguments['thumbnail_dimensions']['height'] ),
272 'width' => intval( $arguments['thumbnail_dimensions']['width'] ),
273 'crop' => '1'
274 );
275 }
276
277 return null;
278 }
279
280 /**
281 * Get the thumb dimensions arguments saved for the gallery for this gallery template
282 *
283 * @param array $dimensions
284 * @param FooGallery $foogallery
285 *
286 * @return mixed
287 */
288 function get_thumbnail_dimensions( $dimensions, $foogallery ) {
289 $dimensions = $foogallery->get_meta( 'default_thumbnail_dimensions', self::THUMBNAIL_DIMENSIONS_DEFAULT );
290 $dimensions['crop'] = true;
291
292 return $dimensions;
293 }
294
295 /**
296 * Build up the arguments needed for rendering this gallery template
297 *
298 * @param $args
299 *
300 * @return array
301 */
302 function build_gallery_template_arguments( $args ) {
303 $args = foogallery_gallery_template_setting( 'thumbnail_dimensions', self::THUMBNAIL_DIMENSIONS_DEFAULT );
304 $args['crop'] = '1'; //we now force thumbs to be cropped
305 $args['link'] = foogallery_gallery_template_setting( 'thumbnail_link', 'image' );
306
307 return $args;
308 }
309
310 /**
311 * Set default values for the gallery template
312 *
313 * @uses "foogallery_override_gallery_template_fields"
314 * @param $fields
315 * @param $template
316 *
317 * @return array
318 */
319 function set_default_fields( $fields, $template ) {
320 //update specific fields
321 foreach ($fields as &$field) {
322 if ( 'hover_effect_type' === $field['id'] ) {
323 $field['default'] = 'preset';
324 } else if ( 'hover_effect_preset' === $field['id'] ) {
325 $field['default'] = 'fg-preset fg-brad';
326 } else if ( 'border_size' === $field['id'] ) {
327 $field['default'] = '';
328 } else if ( 'drop_shadow' === $field['id'] ) {
329 $field['default'] = 'fg-shadow-medium';
330 }
331 }
332
333 return $fields;
334 }
335 }
336 }
337