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