class-admin-notice-custom-css.php
4 weeks ago
class-admin-notices.php
4 weeks ago
class-admin.php
4 weeks ago
class-attachment-fields.php
4 weeks ago
class-columns.php
4 weeks ago
class-demo-content.php
4 weeks ago
class-extensions.php
4 weeks ago
class-gallery-attachment-modal.php
4 weeks ago
class-gallery-datasources.php
4 weeks ago
class-gallery-editor.php
4 weeks ago
class-gallery-metabox-fields.php
4 weeks ago
class-gallery-metabox-items.php
4 weeks ago
class-gallery-metabox-settings-helper.php
4 weeks ago
class-gallery-metabox-settings.php
4 weeks ago
class-gallery-metabox-template.php
4 weeks ago
class-gallery-metaboxes.php
4 weeks ago
class-menu.php
4 weeks ago
class-pro-promotion.php
4 weeks ago
class-settings.php
4 weeks ago
class-silent-installer-skin.php
4 weeks ago
class-trial-mode.php
4 weeks ago
demo-content-galleries.php
4 weeks ago
demo-content-images.php
4 weeks ago
index.php
4 weeks ago
pro-features.php
4 weeks ago
view-features.php
4 weeks ago
view-help-demos.php
4 weeks ago
view-help-getting-started.php
4 weeks ago
view-help-pro.php
4 weeks ago
view-help.php
4 weeks ago
view-system-info.php
4 weeks ago
class-gallery-metabox-items.php
430 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Class to handle adding the Items metabox to a gallery |
| 9 | */ |
| 10 | if ( ! class_exists( 'FooGallery_Admin_Gallery_MetaBox_Items' ) ) { |
| 11 | |
| 12 | class FooGallery_Admin_Gallery_MetaBox_Items { |
| 13 | |
| 14 | /** |
| 15 | * FooGallery_Admin_Gallery_MetaBox_Items constructor. |
| 16 | */ |
| 17 | function __construct() { |
| 18 | add_action( 'add_meta_boxes_' . FOOGALLERY_CPT_GALLERY, array( $this, 'add_items_metabox' ), 7 ); |
| 19 | |
| 20 | //enqueue assets for the items metabox |
| 21 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 22 | |
| 23 | // Ajax call for generating a gallery preview |
| 24 | add_action( 'wp_ajax_foogallery_preview', array( $this, 'ajax_gallery_preview' ) ); |
| 25 | |
| 26 | //handle previews that have no attachments |
| 27 | add_action( 'foogallery_template_no_attachments', array( $this, 'preview_no_attachments' ) ); |
| 28 | } |
| 29 | |
| 30 | public function add_items_metabox( $post ) { |
| 31 | add_meta_box( |
| 32 | 'foogallery_items', |
| 33 | __( 'Gallery Items', 'foogallery' ) . '<span class="foogallery-gallery-items-metabox-title spinner is-active"></span>', |
| 34 | array( $this, 'render_gallery_items_metabox' ), |
| 35 | FOOGALLERY_CPT_GALLERY, |
| 36 | 'normal', |
| 37 | 'high' |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | public function render_gallery_items_metabox( $post ) { |
| 42 | $gallery = foogallery_admin_get_current_gallery( $post ); |
| 43 | |
| 44 | //attempt to load default gallery settings from another gallery, as per FooGallery settings page |
| 45 | $gallery->load_default_settings_if_new(); |
| 46 | |
| 47 | //attempt to load default attachments from the settings page |
| 48 | if ( $gallery->is_new() ) { |
| 49 | $default_attachments = foogallery_get_setting( 'default_gallery_attachments' ); |
| 50 | if ( !empty($default_attachments) ) { |
| 51 | $gallery->attachment_ids = explode( ',', $default_attachments ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | $this->prepare_invalid_datasource_for_recovery( $gallery ); |
| 56 | |
| 57 | $mode = $gallery->get_meta( 'foogallery_items_view', 'manage' ); |
| 58 | |
| 59 | if ( empty($mode) || $gallery->is_new() ) { |
| 60 | $mode = 'manage'; |
| 61 | } |
| 62 | $has_items = !$gallery->is_empty(); |
| 63 | |
| 64 | do_action( 'foogallery_gallery_metabox_items', $gallery ); |
| 65 | ?> |
| 66 | <?php $this->render_invalid_datasource_notice( $gallery ); ?> |
| 67 | <div class="foogallery-hidden foogallery-items-view-switch-container"> |
| 68 | <div class="foogallery-items-view-switch"> |
| 69 | <a href="#manage" data-value="manage" data-container=".foogallery-items-view-manage" class="<?php echo $mode==='manage' ? 'current' : ''; ?>"><?php esc_html_e('Manage Items', 'foogallery'); ?></a> |
| 70 | <a href="#preview" data-value="preview" data-container=".foogallery-items-view-preview" class="<?php echo $mode==='preview' ? 'current' : ''; ?>"><?php esc_html_e('Gallery Preview', 'foogallery'); ?></a> |
| 71 | </div> |
| 72 | <div class="foogallery-preview-actions"> |
| 73 | <button type="button" class="foogallery-preview-refresh-btn" title="<?php esc_attr_e('Refresh Preview', 'foogallery'); ?>"> |
| 74 | <span class="dashicons dashicons-update"></span> |
| 75 | </button> |
| 76 | <span></span> |
| 77 | <button type="button" class="foogallery-viewport-btn active" data-viewport="desktop" title="<?php esc_attr_e('Desktop View', 'foogallery'); ?>"> |
| 78 | <span class="dashicons dashicons-desktop"></span> |
| 79 | </button> |
| 80 | <button type="button" class="foogallery-viewport-btn" data-viewport="tablet" title="<?php esc_attr_e('Tablet View', 'foogallery'); ?>"> |
| 81 | <span class="dashicons dashicons-tablet"></span> |
| 82 | </button> |
| 83 | <button type="button" class="foogallery-viewport-btn" data-viewport="mobile" title="<?php esc_attr_e('Mobile View', 'foogallery'); ?>"> |
| 84 | <span class="dashicons dashicons-smartphone"></span> |
| 85 | </button> |
| 86 | </div> |
| 87 | <span id="foogallery_preview_spinner" class="spinner"></span> |
| 88 | <input type="hidden" id="foogallery_items_view_input" value="<?php echo esc_attr( $mode ); ?>" name="<?php echo esc_attr( FOOGALLERY_META_SETTINGS . '[foogallery_items_view]' ); ?>" /> <!-- phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --> |
| 89 | </div> |
| 90 | |
| 91 | <div class="foogallery-items-view foogallery-items-view-manage <?php echo $mode==='manage' ? '' : 'hidden'; ?>"> |
| 92 | <input type="hidden" name="<?php echo esc_attr( FOOGALLERY_CPT_GALLERY ); ?>_nonce" id="<?php echo esc_attr( FOOGALLERY_CPT_GALLERY ); ?>_nonce" value="<?php echo esc_attr( wp_create_nonce( plugin_basename( FOOGALLERY_FILE ) ) ); ?>"/> <!-- phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --> |
| 93 | <div class="foogallery-items-list"> |
| 94 | <div class="foogallery-items-empty <?php echo $has_items ? 'hidden' : ''; ?>" style="padding-top:20px; text-align: center"> |
| 95 | <p><?php esc_html_e('Your gallery is currently empty. Add items to see a preview.','foogallery'); ?></p> |
| 96 | </div> |
| 97 | <?php do_action( 'foogallery_gallery_metabox_items_list', $gallery ); ?> |
| 98 | </div> |
| 99 | <div class="foogallery-items-add <?php echo $has_items ? 'foogallery-hidden' : ''; ?>"> |
| 100 | <?php do_action( 'foogallery_gallery_metabox_items_add', $gallery ); ?> |
| 101 | </div> |
| 102 | </div> |
| 103 | <div class="foogallery-items-view foogallery-items-view-preview <?php echo $mode==='preview' ? '' : 'foogallery-hidden'; ?>"> |
| 104 | <!-- Wrap existing preview container --> |
| 105 | <div class="foogallery-preview-wrapper viewport-desktop"> |
| 106 | <div class="foogallery_preview_container <?php echo $mode==='preview' ? '' : 'foogallery-preview-force-refresh'; ?>"> |
| 107 | <?php |
| 108 | if ( $has_items && $mode === 'preview' ) { |
| 109 | foogallery_render_gallery( $gallery->ID ); |
| 110 | } else if ( $has_items && $mode === 'manage' ) { |
| 111 | echo '<div style="padding:20px; text-align: center">'; |
| 112 | echo '<h3>' . esc_html__( 'Generating preview...', 'foogallery' ) . '</h3>'; |
| 113 | echo '</div>'; |
| 114 | } else { |
| 115 | $this->render_empty_gallery_preview(); |
| 116 | } |
| 117 | ?> |
| 118 | </div> |
| 119 | </div> |
| 120 | |
| 121 | <div style="clear: both"></div> |
| 122 | <?php wp_nonce_field( 'foogallery_preview', 'foogallery_preview', false ); ?> |
| 123 | </div> |
| 124 | <?php |
| 125 | } |
| 126 | |
| 127 | private function prepare_invalid_datasource_for_recovery( $gallery ) { |
| 128 | if ( ! is_object( $gallery ) || ! isset( $gallery->datasource_name ) ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | $datasource_name = is_scalar( $gallery->datasource_name ) ? trim( (string) $gallery->datasource_name ) : ''; |
| 133 | if ( '' === $datasource_name ) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | $datasources = foogallery_gallery_datasources(); |
| 138 | if ( is_array( $datasources ) && array_key_exists( $datasource_name, $datasources ) ) { |
| 139 | $this->prepare_invalid_datasource_value_for_recovery( $gallery ); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | $recovered_datasource_name = $this->recover_datasource_name( $datasource_name, $datasources ); |
| 144 | |
| 145 | $gallery->_foogallery_invalid_datasource_name = $datasource_name; |
| 146 | $gallery->_foogallery_recovered_datasource_name = $recovered_datasource_name; |
| 147 | $gallery->_foogallery_recovered_datasource_label = $this->get_datasource_label( $recovered_datasource_name, $datasources ); |
| 148 | $gallery->datasource_name = $recovered_datasource_name; |
| 149 | |
| 150 | if ( foogallery_default_datasource() === $recovered_datasource_name || ! isset( $gallery->datasource_value ) || ! is_array( $gallery->datasource_value ) ) { |
| 151 | $gallery->datasource_value = array(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private function prepare_invalid_datasource_value_for_recovery( $gallery ) { |
| 156 | if ( ! isset( $gallery->datasource_name ) || foogallery_default_datasource() === $gallery->datasource_name ) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if ( ! isset( $gallery->datasource_value ) || empty( $gallery->datasource_value ) || is_array( $gallery->datasource_value ) ) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | if ( is_string( $gallery->datasource_value ) ) { |
| 165 | $decoded_value = json_decode( stripslashes( $gallery->datasource_value ), true ); |
| 166 | if ( is_array( $decoded_value ) ) { |
| 167 | $gallery->datasource_value = $decoded_value; |
| 168 | return; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | $gallery->_foogallery_invalid_datasource_value = true; |
| 173 | $gallery->_foogallery_recovered_datasource_name = $gallery->datasource_name; |
| 174 | $gallery->_foogallery_recovered_datasource_label = $this->get_datasource_label( $gallery->datasource_name, foogallery_gallery_datasources() ); |
| 175 | $gallery->datasource_value = array(); |
| 176 | } |
| 177 | |
| 178 | private function recover_datasource_name( $datasource_name, $datasources ) { |
| 179 | $candidates = array( |
| 180 | str_replace( '-', '_', $datasource_name ), |
| 181 | sanitize_key( str_replace( '-', '_', $datasource_name ) ), |
| 182 | ); |
| 183 | |
| 184 | if ( is_array( $datasources ) ) { |
| 185 | foreach ( array_unique( array_filter( $candidates ) ) as $candidate ) { |
| 186 | if ( array_key_exists( $candidate, $datasources ) ) { |
| 187 | return $candidate; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | $default_datasource = foogallery_default_datasource(); |
| 193 | if ( is_array( $datasources ) && array_key_exists( $default_datasource, $datasources ) ) { |
| 194 | return $default_datasource; |
| 195 | } |
| 196 | |
| 197 | if ( is_array( $datasources ) ) { |
| 198 | foreach ( $datasources as $datasource_key => $datasource ) { |
| 199 | return $datasource_key; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return $default_datasource; |
| 204 | } |
| 205 | |
| 206 | private function render_invalid_datasource_notice( $gallery ) { |
| 207 | if ( ! is_object( $gallery ) ) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | $messages = array(); |
| 212 | |
| 213 | if ( ! empty( $gallery->_foogallery_invalid_datasource_name ) ) { |
| 214 | $messages[] = sprintf( |
| 215 | /* translators: 1: invalid datasource value, 2: recovered datasource label, 3: recovered datasource value. */ |
| 216 | __( 'This gallery has an invalid datasource value "%1$s". FooGallery is temporarily showing the %2$s UI so you can recover it. Update the gallery to save the corrected datasource value "%3$s".', 'foogallery' ), |
| 217 | $gallery->_foogallery_invalid_datasource_name, |
| 218 | $gallery->_foogallery_recovered_datasource_label, |
| 219 | $gallery->_foogallery_recovered_datasource_name |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | if ( ! empty( $gallery->_foogallery_invalid_datasource_value ) ) { |
| 224 | $messages[] = sprintf( |
| 225 | /* translators: %s: recovered datasource label. */ |
| 226 | __( 'This gallery has an invalid datasource configuration value. FooGallery is temporarily showing the %s UI with empty source settings so you can recover it.', 'foogallery' ), |
| 227 | $gallery->_foogallery_recovered_datasource_label |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | if ( empty( $messages ) ) { |
| 232 | return; |
| 233 | } |
| 234 | ?> |
| 235 | <div class="notice notice-error inline"> |
| 236 | <?php foreach ( $messages as $message ) { ?> |
| 237 | <p><?php echo esc_html( $message ); ?></p> |
| 238 | <?php } ?> |
| 239 | </div> |
| 240 | <?php |
| 241 | } |
| 242 | |
| 243 | private function get_datasource_label( $datasource_name, $datasources ) { |
| 244 | if ( is_array( $datasources ) && isset( $datasources[ $datasource_name ] ) && is_array( $datasources[ $datasource_name ] ) ) { |
| 245 | $datasource = $datasources[ $datasource_name ]; |
| 246 | foreach ( array( 'menu', 'label', 'name' ) as $label_key ) { |
| 247 | if ( ! empty( $datasource[ $label_key ] ) && is_scalar( $datasource[ $label_key ] ) ) { |
| 248 | return (string) $datasource[ $label_key ]; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return $datasource_name; |
| 254 | } |
| 255 | |
| 256 | public function render_empty_gallery_preview( $message = '' ) { |
| 257 | if ( empty($message) ) { |
| 258 | $message = __( 'Please add media to your gallery to see a preview!', 'foogallery' ); |
| 259 | } |
| 260 | echo '<div class="foogallery-preview-empty" style="padding:20px; text-align: center">'; |
| 261 | echo '<h3>' . esc_html__( 'Please add media to your gallery to see a preview!', 'foogallery' ) . '</h3>'; |
| 262 | echo '</div>'; |
| 263 | } |
| 264 | |
| 265 | public function ajax_gallery_preview() { |
| 266 | if ( ! check_ajax_referer( 'foogallery_preview', 'foogallery_preview_nonce', false ) ) { |
| 267 | wp_send_json_error( |
| 268 | array( 'message' => __( 'Invalid security token.', 'foogallery' ) ), |
| 269 | 403 |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | $post_data = wp_unslash( $_POST ); |
| 274 | $foogallery_id = isset( $post_data['foogallery_id'] ) ? absint( $post_data['foogallery_id'] ) : 0; |
| 275 | if ( ! $foogallery_id ) { |
| 276 | wp_send_json_error( |
| 277 | array( 'message' => __( 'Invalid gallery ID.', 'foogallery' ) ), |
| 278 | 400 |
| 279 | ); |
| 280 | } |
| 281 | |
| 282 | if ( ! current_user_can( 'edit_post', $foogallery_id ) ) { |
| 283 | wp_send_json_error( |
| 284 | array( 'message' => __( 'Insufficient permissions.', 'foogallery' ) ), |
| 285 | 403 |
| 286 | ); |
| 287 | } |
| 288 | |
| 289 | $template = isset( $post_data['foogallery_template'] ) ? sanitize_key( $post_data['foogallery_template'] ) : foogallery_default_gallery_template(); |
| 290 | $template = apply_filters( 'foogallery_preview_template', $template, $foogallery_id ); |
| 291 | |
| 292 | //check that the template supports previews |
| 293 | $gallery_template = foogallery_get_gallery_template( $template ); |
| 294 | if ( isset( $gallery_template['preview_support'] ) && true === $gallery_template['preview_support'] ) { |
| 295 | |
| 296 | global $foogallery_gallery_preview; |
| 297 | |
| 298 | $foogallery_gallery_preview = true; |
| 299 | |
| 300 | $attachment_ids = array(); |
| 301 | if ( isset( $post_data['foogallery_attachments'] ) ) { |
| 302 | $attachment_ids = $post_data['foogallery_attachments']; |
| 303 | if ( is_string( $attachment_ids ) ) { |
| 304 | $attachment_ids = array_map( 'absint', array_filter( explode( ',', $attachment_ids ) ) ); |
| 305 | } else { |
| 306 | $attachment_ids = array_map( 'absint', (array) $attachment_ids ); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | $args = array( |
| 311 | 'template' => $template, |
| 312 | 'attachment_ids' => $attachment_ids, |
| 313 | 'preview' => true, |
| 314 | ); |
| 315 | |
| 316 | $args = $this->extract_preview_arguments( $args, $post_data, $template ); |
| 317 | |
| 318 | $args = apply_filters( 'foogallery_preview_arguments', $args, $post_data, $template ); |
| 319 | $args = apply_filters( 'foogallery_preview_arguments-' . $template, $args, $post_data ); |
| 320 | |
| 321 | if ( foogallery_is_debug() ) { |
| 322 | echo '<pre style="display: none">' . esc_html__( 'Preview Debug Arguments:', 'foogallery' ) . '<br>' . esc_html( print_r( $args, true ) ) . '</pre> <!-- phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -->'; |
| 323 | } |
| 324 | |
| 325 | do_action( 'foogallery_preview_before_render', $foogallery_id, $args ); |
| 326 | |
| 327 | foogallery_render_gallery( $foogallery_id, $args ); |
| 328 | |
| 329 | do_action( 'foogallery_preview_after_render', $foogallery_id, $args ); |
| 330 | |
| 331 | $foogallery_gallery_preview = false; |
| 332 | |
| 333 | } else { |
| 334 | echo '<div style="padding:20px 50px 50px 50px; text-align: center">'; |
| 335 | echo '<h3>' . esc_html__( 'Preview not available!', 'foogallery' ) . '</h3>'; |
| 336 | echo esc_html__( 'Sorry, but this gallery template does not support live previews. Please update the gallery in order to see what the gallery will look like.', 'foogallery' ); |
| 337 | echo '</div>'; |
| 338 | } |
| 339 | |
| 340 | wp_die(); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Extract all the preview arguments from the post data |
| 345 | * |
| 346 | * @param $args |
| 347 | * @param $post_data |
| 348 | * @param $template |
| 349 | * |
| 350 | * @return mixed |
| 351 | */ |
| 352 | private function extract_preview_arguments( $args, $post_data, $template ) { |
| 353 | if ( array_key_exists( FOOGALLERY_META_SETTINGS, $post_data ) ) { |
| 354 | $settings = $post_data[FOOGALLERY_META_SETTINGS]; |
| 355 | foreach ( $settings as $key => $value ) { |
| 356 | if ( strpos( $key, $template . '_' ) === 0 ) { |
| 357 | $args[ $this->str_replace_first( $template . '_', '', $key ) ] = $value; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if ( array_key_exists( FOOGALLERY_META_SORT, $post_data ) ) { |
| 363 | $args['sort'] = $this->sanitize_preview_sort( $post_data[FOOGALLERY_META_SORT] ); |
| 364 | } |
| 365 | |
| 366 | return $args; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Sanitize the preview sort value against the available sort options. |
| 371 | * |
| 372 | * @param string $sort The requested sort value. |
| 373 | * |
| 374 | * @return string |
| 375 | */ |
| 376 | private function sanitize_preview_sort( $sort ) { |
| 377 | $sort = sanitize_text_field( $sort ); |
| 378 | |
| 379 | if ( array_key_exists( $sort, foogallery_sorting_options() ) ) { |
| 380 | return $sort; |
| 381 | } |
| 382 | |
| 383 | return ''; |
| 384 | } |
| 385 | |
| 386 | function str_replace_first($search, $replace, $subject) { |
| 387 | $pos = strpos($subject, $search); |
| 388 | if ($pos !== false) { |
| 389 | return substr_replace($subject, $replace, $pos, strlen($search)); |
| 390 | } |
| 391 | return $subject; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Handle gallery previews where there are no attachments |
| 396 | * |
| 397 | * @param $foogallery FooGallery |
| 398 | */ |
| 399 | public function preview_no_attachments( $foogallery ) { |
| 400 | global $foogallery_gallery_preview; |
| 401 | |
| 402 | if ( isset( $foogallery_gallery_preview ) && true === $foogallery_gallery_preview ) { |
| 403 | $message = ''; |
| 404 | if ( foogallery_default_datasource() !== $foogallery->datasource_name ) { |
| 405 | $message = __( 'No items were found for this gallery.', 'foogallery' ); |
| 406 | } |
| 407 | $this->render_empty_gallery_preview( $message ); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | public function enqueue_assets() { |
| 412 | $screen_id = foo_current_screen_id(); |
| 413 | |
| 414 | //only include scripts if we on the foogallery add/edit page |
| 415 | if ( FOOGALLERY_CPT_GALLERY === $screen_id || |
| 416 | 'edit-' . FOOGALLERY_CPT_GALLERY === $screen_id ) { |
| 417 | |
| 418 | //enqueue any dependencies from extensions or gallery templates |
| 419 | do_action( 'foogallery_enqueue_preview_dependencies' ); |
| 420 | //add core foogallery files for preview |
| 421 | foogallery_enqueue_core_gallery_template_style(); |
| 422 | foogallery_enqueue_core_gallery_template_script(); |
| 423 | |
| 424 | //make sure we have jquery UI sortable enqueued |
| 425 | wp_enqueue_script( 'jquery-ui-sortable'); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 |