class-gallery-api.php
4 months ago
class-gallery-base.php
4 months ago
class-gallery-config.php
4 months ago
class-gallery-design.php
4 months ago
class-gallery-field-provider.php
4 months ago
class-gallery-images.php
4 months ago
class-gallery-lightbox.php
4 months ago
class-gallery-misc.php
4 months ago
class-gallery-paging.php
4 months ago
trait-gallery-ajax.php
4 months ago
trait-gallery-duplicate.php
4 months ago
trait-gallery-image-methods.php
2 weeks ago
trait-gallery-preview.php
4 months ago
trait-gallery-sanitize.php
4 months ago
class-gallery-images.php
431 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsive Lightbox Gallery Images Settings |
| 4 | * |
| 5 | * Handles gallery images tab field definitions and native field rendering. |
| 6 | * |
| 7 | * @package Responsive_Lightbox |
| 8 | */ |
| 9 | |
| 10 | // exit if accessed directly |
| 11 | if ( ! defined( 'ABSPATH' ) ) |
| 12 | exit; |
| 13 | |
| 14 | /** |
| 15 | * Responsive_Lightbox_Gallery_Images class. |
| 16 | * |
| 17 | * Gallery images tab - provides field definitions and rendering via adapter. |
| 18 | * |
| 19 | * @class Responsive_Lightbox_Gallery_Images |
| 20 | */ |
| 21 | class Responsive_Lightbox_Gallery_Images extends Responsive_Lightbox_Gallery_Base { |
| 22 | |
| 23 | /** |
| 24 | * Tab key identifier. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | const TAB_KEY = 'images'; |
| 29 | |
| 30 | /** |
| 31 | * Get tab label for the Images tab. |
| 32 | * |
| 33 | * @return string Tab label. |
| 34 | */ |
| 35 | public function get_tab_label() { |
| 36 | return __( 'Images', 'responsive-lightbox' ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get default menu items for the Images tab. |
| 41 | * |
| 42 | * @return array Default menu items. |
| 43 | */ |
| 44 | public function get_default_menu_items() { |
| 45 | return [ |
| 46 | 'media' => __( 'Media Library', 'responsive-lightbox' ), |
| 47 | 'featured' => __( 'Featured Image', 'responsive-lightbox' ), |
| 48 | 'folders' => __( 'Folders', 'responsive-lightbox' ), |
| 49 | 'remote_library' => __( 'Remote Library', 'responsive-lightbox' ), |
| 50 | ]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get menu items for the Images tab. |
| 55 | * |
| 56 | * Provides menu items with labels and disabled state based on plugin settings. |
| 57 | * |
| 58 | * @return array Menu items array with labels and disabled flags. |
| 59 | */ |
| 60 | public function get_menu_items() { |
| 61 | // Start with adapter-owned default menu items |
| 62 | $menu_items = $this->get_default_menu_items(); |
| 63 | |
| 64 | // Allow add-ons to modify menu items via filter |
| 65 | $menu_items = apply_filters( 'rl_gallery_images_menu_items', $menu_items ); |
| 66 | |
| 67 | // Defensive: Ensure we have at least 'media' as fallback |
| 68 | if ( empty( $menu_items ) || ! is_array( $menu_items ) ) { |
| 69 | $menu_items = [ 'media' => __( 'Media Library', 'responsive-lightbox' ) ]; |
| 70 | } |
| 71 | |
| 72 | // Add disabled state information |
| 73 | foreach ( $menu_items as $menu_item => $label ) { |
| 74 | $menu_items[$menu_item] = [ |
| 75 | 'label' => $label, |
| 76 | 'disabled' => $this->is_menu_item_disabled( $menu_item ), |
| 77 | 'disabled_reason' => $this->get_menu_item_disabled_reason( $menu_item ) |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | return $menu_items; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Check if a menu item should be disabled. |
| 86 | * |
| 87 | * @param string $menu_item Menu item key. |
| 88 | * @return bool True if disabled. |
| 89 | */ |
| 90 | public function is_menu_item_disabled( $menu_item ) { |
| 91 | $rl = Responsive_Lightbox(); |
| 92 | |
| 93 | switch ( $menu_item ) { |
| 94 | case 'remote_library': |
| 95 | return ! $rl->options['remote_library']['active']; |
| 96 | case 'folders': |
| 97 | return ! $rl->options['folders']['active']; |
| 98 | default: |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the reason why a menu item is disabled. |
| 105 | * |
| 106 | * @param string $menu_item Menu item key. |
| 107 | * @return string Disabled reason message. |
| 108 | */ |
| 109 | public function get_menu_item_disabled_reason( $menu_item ) { |
| 110 | switch ( $menu_item ) { |
| 111 | case 'remote_library': |
| 112 | return __( 'Remote Library is disabled. Enable it in the settings.', 'responsive-lightbox' ); |
| 113 | case 'folders': |
| 114 | return __( 'Media Folders are disabled. Enable it in the settings.', 'responsive-lightbox' ); |
| 115 | default: |
| 116 | return ''; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Normalize menu item for AJAX requests. |
| 122 | * |
| 123 | * Ensures the menu item is valid and exists in available menu items. |
| 124 | * |
| 125 | * @param string $menu_item Requested menu item. |
| 126 | * @return string Normalized menu item. |
| 127 | */ |
| 128 | public function normalize_menu_item( $menu_item ) { |
| 129 | $available_menu_items = array_keys( $this->get_menu_items() ); |
| 130 | |
| 131 | // If no menu item specified or invalid, use first available |
| 132 | if ( ! is_string( $menu_item ) || $menu_item === '' || ! in_array( $menu_item, $available_menu_items, true ) ) { |
| 133 | return $available_menu_items[0] ?? 'media'; |
| 134 | } |
| 135 | |
| 136 | return $menu_item; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get tab data for rendering. |
| 141 | * |
| 142 | * Images tab requires menu item to determine field set. |
| 143 | * |
| 144 | * @param string $menu_item Menu item (media, featured, folders, remote_library). |
| 145 | * @return array Tab data with fields. |
| 146 | */ |
| 147 | public function get_tab_data( $menu_item = '' ) { |
| 148 | $rl = Responsive_Lightbox(); |
| 149 | |
| 150 | $fields = [ |
| 151 | 'media' => $this->get_media_fields(), |
| 152 | 'featured' => $this->get_featured_fields(), |
| 153 | ]; |
| 154 | |
| 155 | if ( ! empty( $rl->options['folders']['active'] ) && isset( $rl->folders ) ) { |
| 156 | $fields['folders'] = $this->get_folders_fields(); |
| 157 | } |
| 158 | |
| 159 | if ( ! empty( $rl->options['remote_library']['active'] ) && isset( $rl->remote_library ) ) { |
| 160 | $fields['remote_library'] = $this->get_remote_library_fields(); |
| 161 | } |
| 162 | |
| 163 | if ( empty( $menu_item ) ) { |
| 164 | return $fields; |
| 165 | } |
| 166 | |
| 167 | if ( isset( $fields[$menu_item] ) ) { |
| 168 | return [ $menu_item => $fields[$menu_item] ]; |
| 169 | } |
| 170 | |
| 171 | return []; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get media library fields for the Images tab. |
| 176 | * |
| 177 | * @return array |
| 178 | */ |
| 179 | private function get_media_fields() { |
| 180 | return [ |
| 181 | 'attachments' => [ |
| 182 | 'title' => '', |
| 183 | 'type' => 'media_library', |
| 184 | 'default' => [ |
| 185 | 'ids' => [], |
| 186 | 'exclude' => [], |
| 187 | 'embed' => [] |
| 188 | ], |
| 189 | 'preview' => [ |
| 190 | 'pagination' => true, |
| 191 | 'draggable' => true, |
| 192 | 'editable' => true, |
| 193 | 'removable' => true, |
| 194 | 'changeable' => true |
| 195 | ] |
| 196 | ] |
| 197 | ]; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get featured content fields for the Images tab. |
| 202 | * |
| 203 | * @return array |
| 204 | */ |
| 205 | private function get_featured_fields() { |
| 206 | return [ |
| 207 | 'attachments' => [ |
| 208 | 'title' => '', |
| 209 | 'type' => 'media_preview', |
| 210 | 'default' => [ |
| 211 | 'exclude' => [] |
| 212 | ], |
| 213 | 'preview' => [ |
| 214 | 'pagination' => true, |
| 215 | 'draggable' => false, |
| 216 | 'editable' => true, |
| 217 | 'removable' => false, |
| 218 | 'changeable' => false |
| 219 | ] |
| 220 | ], |
| 221 | 'number_of_posts' => [ |
| 222 | 'title' => __( 'Number of Posts', 'responsive-lightbox' ), |
| 223 | 'type' => 'number', |
| 224 | 'description' => __( 'Enter the number of posts.', 'responsive-lightbox' ), |
| 225 | 'default' => 10, |
| 226 | 'min' => 0 |
| 227 | ], |
| 228 | 'orderby' => [ |
| 229 | 'title' => __( 'Posts Sorting', 'responsive-lightbox' ), |
| 230 | 'type' => 'select', |
| 231 | 'description' => __( 'Select the posts sorting.', 'responsive-lightbox' ), |
| 232 | 'default' => 'date', |
| 233 | 'options' => [ |
| 234 | 'id' => __( 'ID', 'responsive-lightbox' ), |
| 235 | 'author' => __( 'Author', 'responsive-lightbox' ), |
| 236 | 'title' => __( 'Title', 'responsive-lightbox' ), |
| 237 | 'name' => __( 'Slug', 'responsive-lightbox' ), |
| 238 | 'date' => __( 'Date', 'responsive-lightbox' ), |
| 239 | 'modified' => __( 'Last modified date', 'responsive-lightbox' ), |
| 240 | 'parent' => __( 'Parent ID', 'responsive-lightbox' ), |
| 241 | 'rand' => __( 'Random', 'responsive-lightbox' ) |
| 242 | ] |
| 243 | ], |
| 244 | 'order' => [ |
| 245 | 'title' => __( 'Posts Order', 'responsive-lightbox' ), |
| 246 | 'type' => 'radio', |
| 247 | 'description' => __( 'Select the posts order.', 'responsive-lightbox' ), |
| 248 | 'default' => 'asc', |
| 249 | 'options' => [ |
| 250 | 'asc' => __( 'Ascending', 'responsive-lightbox' ), |
| 251 | 'desc' => __( 'Descending', 'responsive-lightbox' ) |
| 252 | ] |
| 253 | ], |
| 254 | 'offset' => [ |
| 255 | 'title' => __( 'Posts Offset', 'responsive-lightbox' ), |
| 256 | 'type' => 'number', |
| 257 | 'description' => __( 'Enter the posts offset.', 'responsive-lightbox' ), |
| 258 | 'default' => 0, |
| 259 | 'min' => 0 |
| 260 | ], |
| 261 | 'image_source' => [ |
| 262 | 'title' => __( 'Image Source', 'responsive-lightbox' ), |
| 263 | 'type' => 'radio', |
| 264 | 'description' => __( 'Select the image source.', 'responsive-lightbox' ), |
| 265 | 'default' => 'thumbnails', |
| 266 | 'options' => [ |
| 267 | 'thumbnails' => __( 'Post Thumbnails', 'responsive-lightbox' ), |
| 268 | 'attached_images' => __( 'Post Attached Images', 'responsive-lightbox' ) |
| 269 | ] |
| 270 | ], |
| 271 | 'images_per_post' => [ |
| 272 | 'title' => __( 'Images per Post', 'responsive-lightbox' ), |
| 273 | 'type' => 'number', |
| 274 | 'description' => __( 'Enter maximum number of images for a post.', 'responsive-lightbox' ), |
| 275 | 'default' => 1, |
| 276 | 'min' => 1 |
| 277 | ], |
| 278 | 'post_type' => [ |
| 279 | 'title' => __( 'Post Type', 'responsive-lightbox' ), |
| 280 | 'type' => 'multiselect', |
| 281 | 'description' => __( 'Select the post types to query.', 'responsive-lightbox' ), |
| 282 | 'options' => [], |
| 283 | 'default' => [] |
| 284 | ], |
| 285 | 'post_status' => [ |
| 286 | 'title' => __( 'Post Status', 'responsive-lightbox' ), |
| 287 | 'type' => 'multiselect', |
| 288 | 'description' => __( 'Select the post status.', 'responsive-lightbox' ), |
| 289 | 'options' => [], |
| 290 | 'default' => [] |
| 291 | ], |
| 292 | 'post_format' => [ |
| 293 | 'title' => __( 'Post Format', 'responsive-lightbox' ), |
| 294 | 'type' => 'multiselect', |
| 295 | 'description' => __( 'Select the post format.', 'responsive-lightbox' ), |
| 296 | 'options' => [], |
| 297 | 'default' => [] |
| 298 | ], |
| 299 | 'post_term' => [ |
| 300 | 'title' => __( 'Post Term', 'responsive-lightbox' ), |
| 301 | 'type' => 'multiselect', |
| 302 | 'description' => __( 'Select the post taxonomy terms to query.', 'responsive-lightbox' ), |
| 303 | 'options' => [], |
| 304 | 'default' => [] |
| 305 | ], |
| 306 | 'post_author' => [ |
| 307 | 'title' => __( 'Post Author', 'responsive-lightbox' ), |
| 308 | 'type' => 'multiselect', |
| 309 | 'description' => __( 'Select the post author.', 'responsive-lightbox' ), |
| 310 | 'options' => [], |
| 311 | 'default' => [] |
| 312 | ], |
| 313 | 'page_parent' => [ |
| 314 | 'title' => __( 'Page Parent', 'responsive-lightbox' ), |
| 315 | 'type' => 'multiselect', |
| 316 | 'description' => __( 'Select the post parent.', 'responsive-lightbox' ), |
| 317 | 'options' => [], |
| 318 | 'default' => [] |
| 319 | ], |
| 320 | 'page_template' => [ |
| 321 | 'title' => __( 'Page Template', 'responsive-lightbox' ), |
| 322 | 'type' => 'multiselect', |
| 323 | 'description' => __( 'Select the page template.', 'responsive-lightbox' ), |
| 324 | 'options' => [], |
| 325 | 'default' => [] |
| 326 | ] |
| 327 | ]; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Get folders fields for the Images tab. |
| 332 | * |
| 333 | * @return array |
| 334 | */ |
| 335 | private function get_folders_fields() { |
| 336 | $rl = Responsive_Lightbox(); |
| 337 | $taxonomy = 'rl_media_folder'; |
| 338 | if ( isset( $rl->folders ) && method_exists( $rl->folders, 'get_active_taxonomy' ) ) { |
| 339 | $taxonomy = $rl->folders->get_active_taxonomy(); |
| 340 | } |
| 341 | |
| 342 | return [ |
| 343 | 'attachments' => [ |
| 344 | 'title' => '', |
| 345 | 'type' => 'media_preview', |
| 346 | 'default' => [ |
| 347 | 'exclude' => [] |
| 348 | ], |
| 349 | 'preview' => [ |
| 350 | 'pagination' => true, |
| 351 | 'draggable' => false, |
| 352 | 'editable' => true, |
| 353 | 'removable' => false, |
| 354 | 'changeable' => false |
| 355 | ] |
| 356 | ], |
| 357 | 'folder' => [ |
| 358 | 'title' => __( 'Media Folder', 'responsive-lightbox' ), |
| 359 | 'type' => 'taxonomy', |
| 360 | 'description' => __( 'Select media folder.', 'responsive-lightbox' ), |
| 361 | 'default' => [ |
| 362 | 'id' => 0, |
| 363 | 'children' => false |
| 364 | ], |
| 365 | 'include_children' => true, |
| 366 | 'taxonomy' => $taxonomy |
| 367 | ] |
| 368 | ]; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Get remote library fields for the Images tab. |
| 373 | * |
| 374 | * @return array |
| 375 | */ |
| 376 | private function get_remote_library_fields() { |
| 377 | $rl = Responsive_Lightbox(); |
| 378 | $options = [ |
| 379 | 'all' => __( 'All Media Providers', 'responsive-lightbox' ) |
| 380 | ]; |
| 381 | |
| 382 | if ( isset( $rl->remote_library ) ) { |
| 383 | $providers = $rl->remote_library->get_providers(); |
| 384 | $active_providers = $rl->remote_library->get_active_providers(); |
| 385 | |
| 386 | foreach ( $active_providers as $provider ) { |
| 387 | if ( isset( $providers[$provider]['name'] ) ) { |
| 388 | $options[$provider] = $providers[$provider]['name']; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return [ |
| 394 | 'attachments' => [ |
| 395 | 'title' => '', |
| 396 | 'type' => 'media_preview', |
| 397 | 'default' => [ |
| 398 | 'exclude' => [] |
| 399 | ], |
| 400 | 'preview' => [ |
| 401 | 'pagination' => true, |
| 402 | 'draggable' => false, |
| 403 | 'editable' => false, |
| 404 | 'removable' => false, |
| 405 | 'changeable' => false |
| 406 | ] |
| 407 | ], |
| 408 | 'media_search' => [ |
| 409 | 'title' => __( 'Search String', 'responsive-lightbox' ), |
| 410 | 'type' => 'text', |
| 411 | 'description' => __( 'Enter the search phrase.', 'responsive-lightbox' ), |
| 412 | 'default' => '' |
| 413 | ], |
| 414 | 'media_provider' => [ |
| 415 | 'title' => __( 'Media Provider', 'responsive-lightbox' ), |
| 416 | 'type' => 'select', |
| 417 | 'description' => __( 'Select which remote library should be used.', 'responsive-lightbox' ), |
| 418 | 'default' => 'all', |
| 419 | 'options' => $options |
| 420 | ], |
| 421 | 'response_data' => [ |
| 422 | 'title' => '', |
| 423 | 'type' => 'hidden', |
| 424 | 'description' => '', |
| 425 | 'default' => '', |
| 426 | 'callback' => [ $rl->remote_library, 'remote_library_response_data' ] |
| 427 | ] |
| 428 | ]; |
| 429 | } |
| 430 | } |
| 431 |