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