PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.1.36
Gallery : FooGallery v3.1.36
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 / includes / class-gallery-advanced-settings.php
foogallery / includes Last commit date
abilities 2 months ago admin 2 months ago compatibility 2 months ago extensions 2 months ago foopluginbase 2 months ago public 2 months ago thumbs 2 months ago asset-manifest.php 2 months ago class-attachment-filters.php 2 months ago class-command-palette.php 2 months ago class-foogallery-animated-gif-support.php 2 months ago class-foogallery-attachment-custom-class.php 2 months ago class-foogallery-attachment-filename.php 2 months ago class-foogallery-attachment-type.php 2 months ago class-foogallery-attachment.php 2 months ago class-foogallery-cache.php 2 months ago class-foogallery-common-fields.php 2 months ago class-foogallery-crop-position.php 2 months ago class-foogallery-datasource-media_library.php 2 months ago class-foogallery-debug.php 2 months ago class-foogallery-delayed-runtime-loader.php 2 months ago class-foogallery-extensions-compatibility.php 2 months ago class-foogallery-force-https.php 2 months ago class-foogallery-lazyload.php 2 months ago class-foogallery-license-constant-handler.php 2 months ago class-foogallery-lightbox.php 2 months ago class-foogallery-paging.php 2 months ago class-foogallery-password-protect.php 2 months ago class-foogallery-sitemaps.php 2 months ago class-foogallery-widget.php 2 months ago class-foogallery.php 2 months ago class-gallery-advanced-settings.php 2 months ago class-il8n.php 2 months ago class-override-thumbnail.php 2 months ago class-posttypes.php 2 months ago class-previews.php 2 months ago class-retina.php 2 months ago class-thumbnail-dimensions.php 2 months ago class-thumbnails.php 2 months ago class-version-check.php 2 months ago constants.php 2 months ago functions.php 2 months ago includes.php 2 months ago index.php 2 months ago render-functions.php 2 months ago
class-gallery-advanced-settings.php
285 lines
1 <?php
2 /**
3 * Class for adding advanced settings to all gallery templates
4 */
5 if ( ! class_exists( 'FooGallery_Advanced_Gallery_Settings' ) ) {
6
7 class FooGallery_Advanced_Gallery_Settings {
8
9 function __construct() {
10 //add fields to all templates
11 add_filter( 'foogallery_override_gallery_template_fields', array( $this, 'add_advanced_fields' ), 20, 2 );
12
13 //add data options
14 add_filter( 'foogallery_build_container_data_options', array( $this, 'add_data_options' ), 30, 3 );
15
16 //add custom attributes
17 add_filter( 'foogallery_build_container_attributes', array( $this, 'add_container_attributes' ), 10, 3 );
18
19 //sanitize custom attributes when gallery settings are saved
20 add_filter( 'foogallery_save_gallery_settings', array( $this, 'save_custom_attribute_settings' ), 20, 3 );
21
22 //add custom class to container
23 add_filter( 'foogallery_build_class_attribute', array( $this, 'add_custom_class' ), 10, 2 );
24
25 //remove the title attribute from the image
26 add_filter('foogallery_attachment_html_image_attributes', array($this, 'remove_title_attribute'), 99, 3);
27 }
28
29 /**
30 * @param array $attr
31 * @param array $args
32 * @param FooGalleryAttachment $attachment
33 * @return mixed
34 */
35 function remove_title_attribute($attr, $args, $attachment) {
36 //make sure we use a cached value
37 if ( !foogallery_current_gallery_has_cached_value( 'include_title') ) {
38 foogallery_current_gallery_set_cached_value( 'include_title', foogallery_gallery_template_setting( 'include_title', '' ) );
39 }
40
41 if ( 'disabled' === foogallery_current_gallery_get_cached_value( 'include_title' ) ) {
42 if ( array_key_exists( 'title', $attr ) ) {
43 unset( $attr['title'] );
44 }
45 }
46
47 return $attr;
48 }
49
50 /**
51 * Add fields to the gallery template
52 *
53 * @param $fields
54 * @param $template
55 *
56 * @return array
57 */
58 function add_advanced_fields( $fields, $template ) {
59 $fields[] = array(
60 'id' => 'custom_settings',
61 'title' => __( 'Custom Settings', 'foogallery' ),
62 'desc' => __( 'Add any custom settings to the gallery which will be merged with existing settings. To be used by developers only!', 'foogallery' ),
63 'section' => __( 'Advanced', 'foogallery' ),
64 'type' => 'textarea',
65 'default' => '',
66 );
67
68 $custom_attribute_disabled = ! current_user_can( 'manage_options' );
69 $custom_attribute_row_data = $custom_attribute_disabled ? array( 'data-foogallery-locked' => 'true' ) : array();
70
71 $fields[] = array(
72 'id' => 'custom_attribute_key',
73 'title' => __( 'Custom Attribute Key', 'foogallery' ),
74 'desc' => __( 'Used in combination with "Custom Attribute Value" to add a custom attribute to the gallery container. Only administrators can edit this setting. To be used by developers only!', 'foogallery' ),
75 'section' => __( 'Advanced', 'foogallery' ),
76 'type' => 'text',
77 'default' => '',
78 'disabled' => $custom_attribute_disabled,
79 'row_data' => $custom_attribute_row_data,
80 );
81
82 $fields[] = array(
83 'id' => 'custom_attribute_value',
84 'title' => __( 'Custom Attribute Value', 'foogallery' ),
85 'desc' => __( 'Used in combination with "Custom Attribute Key" to add a custom attribute to the gallery container. Only administrators can edit this setting. To be used by developers only!', 'foogallery' ),
86 'section' => __( 'Advanced', 'foogallery' ),
87 'type' => 'text',
88 'default' => '',
89 'disabled' => $custom_attribute_disabled,
90 'row_data' => $custom_attribute_row_data,
91 );
92
93 $fields[] = array(
94 'id' => 'custom_class',
95 'title' => __( 'Custom Gallery Class', 'foogallery' ),
96 'desc' => __( 'Add a custom class to the gallery container.', 'foogallery' ),
97 'section' => __( 'Advanced', 'foogallery' ),
98 'type' => 'text',
99 'default' => '',
100 );
101
102 $fields[] = array(
103 'id' => 'include_title',
104 'title' => __( 'Image Title Attribute', 'foogallery' ),
105 'desc' => __( 'You can choose to include a title attribute on the thumbnail image or not.', 'foogallery' ),
106 'section' => __( 'Advanced', 'foogallery' ),
107 'type' => 'radio',
108 'default' => '',
109 'choices' => array(
110 'disabled' => __( 'Disabled', 'foogallery' ),
111 '' => __( 'Enabled', 'foogallery' ),
112 ),
113 'row_data' => array(
114 'data-foogallery-change-selector' => 'input:radio',
115 'data-foogallery-preview' => 'shortcode'
116 )
117 );
118
119 return $fields;
120 }
121
122 /**
123 * Add the required data options
124 *
125 * @param $options
126 * @param $gallery FooGallery
127 *
128 * @param $attributes array
129 *
130 * @return array
131 */
132 function add_data_options($options, $gallery, $attributes) {
133 $custom_settings = foogallery_gallery_template_setting( 'custom_settings', '' );
134
135 if ( !empty( $custom_settings ) ) {
136 $settings_array = @json_decode($custom_settings, true);
137
138 if ( isset( $settings_array ) ) {
139 $options = array_replace_recursive( $options, $settings_array );
140 }
141 }
142
143 return $options;
144 }
145
146 /**
147 * Returns a saved custom attribute setting for the current gallery template.
148 *
149 * @param FooGallery $gallery Gallery object.
150 * @param string $key Setting key without the template prefix.
151 *
152 * @return string
153 */
154 function get_saved_custom_attribute_setting( $gallery, $key ) {
155 $setting_key = $gallery->gallery_template . '_' . $key;
156
157 if ( isset( $gallery->settings ) && is_array( $gallery->settings ) && array_key_exists( $setting_key, $gallery->settings ) ) {
158 return $gallery->settings[ $setting_key ];
159 }
160
161 return '';
162 }
163
164 /**
165 * Returns true when a gallery setting key stores custom attribute data.
166 *
167 * @param string $key Setting key.
168 *
169 * @return bool
170 */
171 function is_custom_attribute_setting_key( $key ) {
172 return is_string( $key ) && 1 === preg_match( '/_custom_attribute_(?:key|value)$/', $key );
173 }
174
175 /**
176 * Sanitizes and authorizes saved custom attribute settings.
177 *
178 * @param array $settings Incoming settings.
179 * @param int $post_id Gallery post ID.
180 * @param array $post_data Submitted post data or save context.
181 *
182 * @return array
183 */
184 function save_custom_attribute_settings( $settings, $post_id, $post_data ) {
185 $settings = is_array( $settings ) ? $settings : array();
186
187 if ( ! current_user_can( 'manage_options' ) ) {
188 foreach ( array_keys( $settings ) as $setting_key ) {
189 if ( $this->is_custom_attribute_setting_key( $setting_key ) ) {
190 unset( $settings[ $setting_key ] );
191 }
192 }
193
194 $existing_settings = get_post_meta( $post_id, FOOGALLERY_META_SETTINGS, true );
195 if ( is_array( $existing_settings ) ) {
196 foreach ( $existing_settings as $setting_key => $setting_value ) {
197 if ( $this->is_custom_attribute_setting_key( $setting_key ) ) {
198 $settings[ $setting_key ] = $setting_value;
199 }
200 }
201 }
202
203 return $settings;
204 }
205
206 $gallery = ( $post_id > 0 && class_exists( 'FooGallery' ) ) ? FooGallery::get_by_id( $post_id ) : null;
207 $handled_value_keys = array();
208
209 foreach ( array_keys( $settings ) as $setting_key ) {
210 if ( ! is_string( $setting_key ) || '_custom_attribute_key' !== substr( $setting_key, -21 ) ) {
211 continue;
212 }
213
214 $value_key = substr( $setting_key, 0, -21 ) . '_custom_attribute_value';
215 $handled_value_keys[] = $value_key;
216
217 $custom_attribute_key = foogallery_sanitize_custom_attribute_key( $settings[ $setting_key ], $gallery );
218 $custom_attribute_value = array_key_exists( $value_key, $settings ) ? foogallery_sanitize_custom_attribute_value( $settings[ $value_key ] ) : '';
219
220 if ( '' === $custom_attribute_key || '' === $custom_attribute_value ) {
221 unset( $settings[ $setting_key ], $settings[ $value_key ] );
222 continue;
223 }
224
225 $settings[ $setting_key ] = $custom_attribute_key;
226 $settings[ $value_key ] = $custom_attribute_value;
227 }
228
229 foreach ( array_keys( $settings ) as $setting_key ) {
230 if ( is_string( $setting_key ) && '_custom_attribute_value' === substr( $setting_key, -23 ) && ! in_array( $setting_key, $handled_value_keys, true ) ) {
231 unset( $settings[ $setting_key ] );
232 }
233 }
234
235 return $settings;
236 }
237
238 /**
239 * Adds a custom attribute to the gallery container attributes
240 *
241 * @param $attributes
242 * @param $gallery
243 *
244 * @return mixed
245 */
246 function add_container_attributes( $attributes, $gallery ) {
247 global $current_foogallery;
248
249 if ( $current_foogallery === $gallery ) {
250 $custom_attribute_key = foogallery_sanitize_custom_attribute_key( $this->get_saved_custom_attribute_setting( $gallery, 'custom_attribute_key' ), $gallery );
251 $custom_attribute_value = foogallery_sanitize_custom_attribute_value( $this->get_saved_custom_attribute_setting( $gallery, 'custom_attribute_value' ) );
252
253 if ( !empty( $custom_attribute_key ) && !empty( $custom_attribute_value ) ) {
254 $attributes[$custom_attribute_key] = $custom_attribute_value;
255 }
256 }
257
258 return $attributes;
259 }
260
261
262 /**
263 * Add the custom class to the array of classes
264 *
265 * @param $classes
266 * @param $gallery
267 *
268 * @return array
269 */
270 function add_custom_class( $classes, $gallery ) {
271 global $current_foogallery;
272
273 if ( $current_foogallery === $gallery ) {
274 $custom_class = sanitize_title( foogallery_gallery_template_setting( 'custom_class', '' ) );
275
276 if ( !empty( $custom_class ) ) {
277 $classes[] = $custom_class;
278 }
279 }
280
281 return $classes;
282 }
283 }
284 }
285