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