class-metaboxes.php
743 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /* |
| 8 | * FooGallery Admin Album MetaBoxes class |
| 9 | */ |
| 10 | |
| 11 | if ( ! class_exists( 'FooGallery_Admin_Album_MetaBoxes' ) ) { |
| 12 | |
| 13 | class FooGallery_Admin_Album_MetaBoxes { |
| 14 | |
| 15 | private $_album; |
| 16 | |
| 17 | public function __construct() { |
| 18 | //add our foogallery metaboxes |
| 19 | add_action( 'add_meta_boxes_' . FOOGALLERY_CPT_ALBUM, array( $this, 'add_meta_boxes' ) ); |
| 20 | |
| 21 | //save extra post data for a gallery |
| 22 | add_action( 'save_post', array( $this, 'save_album' ) ); |
| 23 | |
| 24 | //add scripts used by metaboxes |
| 25 | add_action( 'admin_enqueue_scripts', array( $this, 'include_required_scripts' ) ); |
| 26 | |
| 27 | // Ajax call for getting gallery details |
| 28 | add_action( 'wp_ajax_foogallery_get_gallery_details', array( $this, 'ajax_get_gallery_details' ) ); |
| 29 | |
| 30 | // Ajax call for saving gallery details |
| 31 | add_action( 'wp_ajax_foogallery_save_gallery_details', array( $this, 'ajax_save_gallery_details' ) ); |
| 32 | |
| 33 | // Save details for the gallery |
| 34 | add_action( 'foogallery_album_gallery_details_save', array( $this, 'gallery_details_save' ), 10, 3 ); |
| 35 | } |
| 36 | |
| 37 | public function add_meta_boxes( $post ) { |
| 38 | add_meta_box( |
| 39 | 'foogalleryalbum_galleries', |
| 40 | __( 'Galleries - click a gallery to add it to your album.', 'foogallery' ), |
| 41 | array( $this, 'render_gallery_metabox' ), |
| 42 | FOOGALLERY_CPT_ALBUM, |
| 43 | 'normal', |
| 44 | 'high' |
| 45 | ); |
| 46 | |
| 47 | add_meta_box( |
| 48 | 'foogalleryalbum_settings', |
| 49 | __( 'Settings', 'foogallery' ), |
| 50 | array( $this, 'render_settings_metabox' ), |
| 51 | FOOGALLERY_CPT_ALBUM, |
| 52 | 'normal', |
| 53 | 'high' |
| 54 | ); |
| 55 | |
| 56 | add_meta_box( |
| 57 | 'foogalleryalbum_customcss', |
| 58 | __( 'Custom CSS', 'foogallery' ), |
| 59 | array( $this, 'render_customcss_metabox' ), |
| 60 | FOOGALLERY_CPT_ALBUM, |
| 61 | 'normal', |
| 62 | 'low' |
| 63 | ); |
| 64 | |
| 65 | add_meta_box( |
| 66 | 'foogalleryalbum_shortcode', |
| 67 | __( 'Album Shortcode', 'foogallery' ), |
| 68 | array( $this, 'render_shortcode_metabox' ), |
| 69 | FOOGALLERY_CPT_ALBUM, |
| 70 | 'side', |
| 71 | 'default' |
| 72 | ); |
| 73 | |
| 74 | add_meta_box( |
| 75 | 'foogalleryalbum_sorting', |
| 76 | __( 'Album Sorting', 'foogallery' ), |
| 77 | array( $this, 'render_sorting_metabox' ), |
| 78 | FOOGALLERY_CPT_ALBUM, |
| 79 | 'side', |
| 80 | 'default' |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | public function get_album( $post ) { |
| 85 | if ( ! isset( $this->_album ) ) { |
| 86 | $this->_album = FooGalleryAlbum::get( $post ); |
| 87 | } |
| 88 | |
| 89 | return $this->_album; |
| 90 | } |
| 91 | |
| 92 | public function save_album( $post_id ) { |
| 93 | $post_id = absint( $post_id ); |
| 94 | |
| 95 | // check autosave |
| 96 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 97 | return $post_id; |
| 98 | } |
| 99 | |
| 100 | if ( FOOGALLERY_CPT_ALBUM !== get_post_type( $post_id ) ) { |
| 101 | return $post_id; |
| 102 | } |
| 103 | |
| 104 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 105 | return $post_id; |
| 106 | } |
| 107 | |
| 108 | $nonce_field = FOOGALLERY_CPT_ALBUM . '_nonce'; |
| 109 | |
| 110 | if ( ! isset( $_POST[ $nonce_field ] ) || ! is_scalar( $_POST[ $nonce_field ] ) ) { |
| 111 | return $post_id; |
| 112 | } |
| 113 | |
| 114 | $nonce = sanitize_text_field( wp_unslash( $_POST[ $nonce_field ] ) ); |
| 115 | |
| 116 | // verify nonce |
| 117 | if ( |
| 118 | wp_verify_nonce( $nonce, plugin_basename( FOOGALLERY_FILE ) ) |
| 119 | ) { |
| 120 | //if we get here, we are dealing with the Album custom post type |
| 121 | |
| 122 | /** |
| 123 | * Filters whether an album save is valid before any album meta is written. |
| 124 | * |
| 125 | * Extensions can inspect their submitted fields and return false or a |
| 126 | * WP_Error to prevent the complete album meta save. |
| 127 | * |
| 128 | * @param bool|WP_Error $is_valid Whether the album save should proceed. |
| 129 | * @param int $post_id The album post ID. |
| 130 | * @param array $request The unsanitized submitted request data. |
| 131 | */ |
| 132 | $is_valid = apply_filters( 'foogallery_validate_album_save', true, $post_id, $_POST ); |
| 133 | |
| 134 | if ( ! $is_valid || is_wp_error( $is_valid ) ) { |
| 135 | return $post_id; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Filters the direct gallery IDs before they are saved for an album. |
| 140 | * |
| 141 | * @param array $galleries The submitted direct gallery IDs. |
| 142 | * @param int $post_id The album post ID. |
| 143 | * @param array $request The unsanitized submitted request data. |
| 144 | */ |
| 145 | $galleries = apply_filters( |
| 146 | 'foogallery_save_album_galleries', |
| 147 | explode( ',', $_POST[ FOOGALLERY_ALBUM_META_GALLERIES ] ), |
| 148 | $post_id, |
| 149 | $_POST |
| 150 | ); |
| 151 | update_post_meta( $post_id, FOOGALLERY_ALBUM_META_GALLERIES, $galleries ); |
| 152 | |
| 153 | if ( !empty( $_POST[FOOGALLERY_ALBUM_META_TEMPLATE] ) ) { |
| 154 | update_post_meta( $post_id, FOOGALLERY_ALBUM_META_TEMPLATE, $_POST[FOOGALLERY_ALBUM_META_TEMPLATE] ); |
| 155 | } |
| 156 | |
| 157 | if ( isset( $_POST[FOOGALLERY_ALBUM_META_SORT] ) ) { |
| 158 | update_post_meta( $post_id, FOOGALLERY_ALBUM_META_SORT, $_POST[FOOGALLERY_ALBUM_META_SORT] ); |
| 159 | } |
| 160 | |
| 161 | $settings = isset($_POST['_foogallery_settings']) ? |
| 162 | $_POST['_foogallery_settings'] : array(); |
| 163 | |
| 164 | /** |
| 165 | * Filters album settings before they are saved. |
| 166 | * |
| 167 | * @param array $settings The submitted album settings. |
| 168 | * @param int $post_id The album post ID. |
| 169 | * @param array $request The unsanitized submitted request data. |
| 170 | */ |
| 171 | $settings = apply_filters( 'foogallery_save_album_settings', $settings, $post_id, $_POST ); |
| 172 | |
| 173 | if ( !empty( $settings ) ) { |
| 174 | update_post_meta( $post_id, FOOGALLERY_META_SETTINGS_OLD, $settings ); |
| 175 | } else { |
| 176 | delete_post_meta( $post_id, FOOGALLERY_META_SETTINGS_OLD ); |
| 177 | } |
| 178 | |
| 179 | $custom_css = foogallery_sanitize_full( isset( $_POST[FOOGALLERY_META_CUSTOM_CSS] ) ? |
| 180 | $_POST[FOOGALLERY_META_CUSTOM_CSS] : '' ); |
| 181 | |
| 182 | if ( empty( $custom_css ) ) { |
| 183 | delete_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS ); |
| 184 | } else { |
| 185 | update_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS, $custom_css ); |
| 186 | } |
| 187 | |
| 188 | // update usage for each of the galleries. |
| 189 | foreach ( $galleries as $gallery_id ) { |
| 190 | add_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, $gallery_id, false ); |
| 191 | } |
| 192 | |
| 193 | do_action( 'foogallery_after_save_album', $post_id, $_POST ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public function get_ordered_galleries( $album ) { |
| 198 | //exclude the galleries already added to the album |
| 199 | $excluded_galleries = $album->gallery_ids; |
| 200 | |
| 201 | //allow more galleries to be excluded |
| 202 | $excluded_galleries = apply_filters( 'foogallery_album_excluded_galleries', $excluded_galleries, $album ); |
| 203 | |
| 204 | $args = array(); |
| 205 | |
| 206 | $limit = intval( foogallery_get_setting( 'album_limit_galleries', '' ) ); |
| 207 | |
| 208 | if ( $limit > 0 ) { |
| 209 | $args['nopaging'] = false; |
| 210 | $args['posts_per_page'] = $limit; |
| 211 | } |
| 212 | |
| 213 | //get all other galleries |
| 214 | $galleries = foogallery_get_all_galleries( $excluded_galleries, $args ); |
| 215 | |
| 216 | $album_galleries = $album->galleries(); |
| 217 | |
| 218 | return array_merge( $album_galleries, $galleries ); |
| 219 | } |
| 220 | |
| 221 | public function render_gallery_metabox( $post ) { |
| 222 | $album = $this->get_album( $post ); |
| 223 | |
| 224 | $galleries = $this->get_ordered_galleries( $album ); |
| 225 | |
| 226 | wp_enqueue_style( 'media-views' ); |
| 227 | |
| 228 | ?> |
| 229 | <input type="hidden" name="<?php echo esc_attr( FOOGALLERY_CPT_ALBUM ); ?>_nonce" |
| 230 | id="<?php echo esc_attr( FOOGALLERY_CPT_ALBUM ); ?>_nonce" |
| 231 | value="<?php echo esc_attr( wp_create_nonce( plugin_basename( FOOGALLERY_FILE )) ); ?>"/> |
| 232 | <input type="hidden" name='foogallery_album_galleries' id="foogallery_album_galleries" |
| 233 | value="<?php echo esc_attr( $album->gallery_id_csv() ); ?>"/> |
| 234 | <div> |
| 235 | <?php if ( !$album->has_galleries() ) { ?> |
| 236 | <div class="foogallery-album-error"> |
| 237 | <?php esc_html_e( 'There are no galleries selected for your album yet! Click any gallery to add it to your album.', 'foogallery' ); ?> |
| 238 | </div> |
| 239 | <?php } ?> |
| 240 | |
| 241 | <div class="foogallery-album-info-modal media-modal"> |
| 242 | <div class="media-modal-content"> |
| 243 | <div class="media-frame mode-select"> |
| 244 | <div class="media-frame-title"> |
| 245 | <h1><?php esc_html_e('Edit Gallery Details', 'foogallery'); ?></h1> |
| 246 | <span class="spinner is-active"></span> |
| 247 | </div> |
| 248 | <div class="modal-content"> |
| 249 | <?php wp_nonce_field( 'foogallery_album_gallery_details', 'foogallery_album_gallery_details_nonce', false ); ?> |
| 250 | <div class="gallery-details" data-loading="<?php esc_attr_e( 'Loading details for ', 'foogallery' ); ?>"></div> |
| 251 | </div> |
| 252 | </div> |
| 253 | <div class="media-frame-toolbar"> |
| 254 | <div class="media-toolbar"> |
| 255 | <div class="media-toolbar-secondary"></div> |
| 256 | <div class="media-toolbar-primary search-form"> |
| 257 | <button type="button" class="button media-button button-primary button-large media-button-select gallery-details-save"><?php esc_html_e('Save Gallery Details', 'foogallery'); ?></button> |
| 258 | <span class="spinner"></span> |
| 259 | </div> |
| 260 | </div> |
| 261 | </div> |
| 262 | </div> |
| 263 | <button type="button" class="button-link media-modal-close"> |
| 264 | <span class="media-modal-icon"><span class="screen-reader-text"><?php esc_html_e('Close media panel', 'foogallery'); ?></span></span> |
| 265 | </button> |
| 266 | |
| 267 | </div> |
| 268 | <div class="foogallery-album-info-modal-backdrop media-modal-backdrop"></div> |
| 269 | |
| 270 | |
| 271 | <ul class="foogallery-album-gallery-list"> |
| 272 | <?php |
| 273 | foreach ( $galleries as $gallery ) { |
| 274 | $img_src = foogallery_find_featured_attachment_thumbnail_src( $gallery ); |
| 275 | $images = $gallery->image_count(); |
| 276 | $selected = $album->includes_gallery( $gallery->ID ) ? ' selected' : ''; |
| 277 | $title = $gallery->safe_name(); |
| 278 | /* translators: %s: gallery title. */ |
| 279 | $image_alt = sprintf( __( 'Preview of %s', 'foogallery' ), $title ); |
| 280 | ?> |
| 281 | <li class="foogallery-pile"> |
| 282 | <div class="foogallery-gallery-select landscape<?php echo esc_attr( $selected ); ?>" data-foogallery-id="<?php echo esc_attr( $gallery->ID ); ?>"> |
| 283 | <div style="display: table;"> |
| 284 | <div style="display: table-cell; vertical-align: middle; text-align: center;"> |
| 285 | <img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( $image_alt ); ?>"/> |
| 286 | <h3> |
| 287 | <?php echo esc_html( $title ); ?> |
| 288 | <span><?php echo esc_html( $images ); ?></span> |
| 289 | </h3> |
| 290 | </div> |
| 291 | </div> |
| 292 | <a class="info foogallery-album-info" href="#" |
| 293 | title="<?php esc_html_e( 'Edit Album Info', 'foogallery' ); ?>" |
| 294 | data-gallery-title="<?php echo esc_attr( $title ); ?>" |
| 295 | data-gallery-id="<?php echo esc_attr( $gallery->ID ); ?>"><span class="dashicons dashicons-info"></span> |
| 296 | </a> |
| 297 | </div> |
| 298 | </li> |
| 299 | <?php } ?> |
| 300 | </ul> |
| 301 | <div style="clear: both;"></div> |
| 302 | </div> |
| 303 | <?php |
| 304 | } |
| 305 | |
| 306 | public function render_shortcode_metabox( $post ) { |
| 307 | $album = $this->get_album( $post ); |
| 308 | $shortcode = $album->shortcode(); |
| 309 | ?> |
| 310 | <p class="foogallery-shortcode"> |
| 311 | <input type="text" id="foogallery_copy_shortcode" size="<?php echo absint( strlen( $shortcode ) ); ?>" value="<?php echo esc_attr( $shortcode ); ?>" readonly="readonly" /> |
| 312 | </p> |
| 313 | <p> |
| 314 | <?php esc_html_e( 'Paste the above shortcode into a post or page to show the album.', 'foogallery' ); ?> |
| 315 | </p> |
| 316 | <script> |
| 317 | jQuery(function($) { |
| 318 | var shortcodeInput = document.querySelector('#foogallery_copy_shortcode'); |
| 319 | shortcodeInput.addEventListener('click', function () { |
| 320 | try { |
| 321 | // select the contents |
| 322 | shortcodeInput.select(); |
| 323 | //copy the selection |
| 324 | document.execCommand('copy'); |
| 325 | //show the copied message |
| 326 | $('.foogallery-shortcode-message').remove(); |
| 327 | $(shortcodeInput).after('<p class="foogallery-shortcode-message"><?php esc_html_e( 'Shortcode copied to clipboard :)','foogallery' ); ?></p>'); |
| 328 | } catch(err) { |
| 329 | console.log('Oops, unable to copy!'); |
| 330 | } |
| 331 | }, false); |
| 332 | }); |
| 333 | </script> |
| 334 | <?php |
| 335 | } |
| 336 | |
| 337 | public function render_sorting_metabox( $post ) { |
| 338 | $album = $this->get_album( $post ); |
| 339 | $sorting_options = foogallery_sorting_options( 'album' ); ?> |
| 340 | <p> |
| 341 | <?php esc_html_e('Change the way galleries are sorted within your album. By default, they are sorted in the order you see them.', 'foogallery'); ?> |
| 342 | </p> |
| 343 | <?php |
| 344 | foreach ( $sorting_options as $sorting_key => $sorting_label ) { ?> |
| 345 | <p> |
| 346 | <input type="radio" value="<?php echo esc_attr( $sorting_key ); ?>" <?php checked( $sorting_key === $album->sorting ); ?> id="FooGallerySettings_AlbumSort_<?php echo esc_attr( $sorting_key ); ?>" name="<?php echo esc_attr( FOOGALLERY_ALBUM_META_SORT ); ?>" /> |
| 347 | <label for="FooGallerySettings_AlbumSort_<?php echo esc_attr( $sorting_key ); ?>"><?php echo esc_html( $sorting_label ); ?></label> |
| 348 | </p><?php |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | public function render_settings_metabox( $post ) { |
| 353 | $album = $this->get_album( $post ); |
| 354 | $available_templates = foogallery_album_templates(); |
| 355 | $album_template = foogallery_default_album_template(); |
| 356 | if ( ! empty($album->album_template) ) { |
| 357 | $album_template = $album->album_template; |
| 358 | } |
| 359 | if ( false === $album_template ) { |
| 360 | $album_template = $available_templates[0]['slug']; |
| 361 | } |
| 362 | $hide_help = 'on' == foogallery_get_setting( 'hide_gallery_template_help' ); |
| 363 | ?> |
| 364 | <table class="foogallery-album-metabox-settings"> |
| 365 | <tbody> |
| 366 | <tr class="foogallery_template_field foogallery_template_field_selector"> |
| 367 | <th> |
| 368 | <label for="FooGallerySettings_AlbumTemplate"><?php esc_html_e( 'Album Layout', 'foogallery' ); ?></label> |
| 369 | </th> |
| 370 | <td> |
| 371 | <select id="FooGallerySettings_AlbumTemplate" name="<?php echo esc_attr( FOOGALLERY_ALBUM_META_TEMPLATE ); ?>"> |
| 372 | <?php |
| 373 | foreach ( $available_templates as $template ) { |
| 374 | $selected = ($album_template === $template['slug']) ? 'selected' : ''; |
| 375 | echo "<option " . esc_attr( $selected ) . " value=\"" . esc_attr( $template['slug'] ) . "\">" . esc_html( $template['name'] ) . "</option>"; |
| 376 | } |
| 377 | ?> |
| 378 | </select> |
| 379 | <br /> |
| 380 | <small><?php esc_html_e( 'The album layout that will be used when the album is output to the frontend.', 'foogallery' ); ?></small> |
| 381 | </td> |
| 382 | </tr> |
| 383 | <?php |
| 384 | foreach ( $available_templates as $template ) { |
| 385 | $field_visibility = ($album_template !== $template['slug']) ? 'style="display:none"' : ''; |
| 386 | $section = ''; |
| 387 | $fields = isset( $template['fields'] ) ? $template['fields'] : array(); |
| 388 | foreach ( $fields as $field ) { |
| 389 | //allow for the field to be altered by extensions. |
| 390 | $field = apply_filters( 'foogallery_alter_gallery_template_field', $field, $album ); |
| 391 | |
| 392 | $class ="foogallery_template_field foogallery_template_field-{$template['slug']} foogallery_template_field-{$template['slug']}-{$field['id']}"; |
| 393 | |
| 394 | if ( isset($field['section']) && $field['section'] !== $section ) { |
| 395 | $section = $field['section']; |
| 396 | ?> |
| 397 | <tr class="<?php echo esc_attr( $class ); ?>" <?php echo $field_visibility; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 398 | <td colspan="2"><h4><?php echo esc_html( $section ); ?></h4></td> |
| 399 | </tr> |
| 400 | <?php } |
| 401 | if (isset($field['type']) && 'help' == $field['type'] && $hide_help) { |
| 402 | continue; //skip help if the 'hide help' setting is turned on |
| 403 | } |
| 404 | ?> |
| 405 | <tr class="<?php echo esc_attr( $class ); ?>" <?php echo $field_visibility; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> |
| 406 | <?php if ( isset($field['type']) && 'help' == $field['type'] ) { ?> |
| 407 | <td colspan="2"> |
| 408 | <div class="foogallery-help"> |
| 409 | <?php echo wp_kses_post( $field['desc'] ); ?> |
| 410 | </div> |
| 411 | </td> |
| 412 | <?php } else { ?> |
| 413 | <th> |
| 414 | <label for="FooGallerySettings_<?php echo esc_attr( $template['slug'] . '_' . $field['id'] ); ?>"><?php echo esc_html( $field['title'] ); ?></label> |
| 415 | </th> |
| 416 | <td> |
| 417 | <?php do_action( 'foogallery_render_gallery_template_field', $field, $album, $template ); ?> |
| 418 | </td> |
| 419 | <?php } ?> |
| 420 | </tr> |
| 421 | <?php |
| 422 | } |
| 423 | } |
| 424 | ?> |
| 425 | </tbody> |
| 426 | </table> |
| 427 | <?php |
| 428 | } |
| 429 | |
| 430 | public function render_customcss_metabox( $post ) { |
| 431 | $album = $this->get_album( $post ); |
| 432 | $custom_css = $album->custom_css; |
| 433 | $example = '<code>#foogallery-album-' . $post->ID . ' { }</code>'; |
| 434 | ?> |
| 435 | <p> |
| 436 | <?php /* translators: %s: Example CSS selector for this album. */ ?> |
| 437 | <?php printf( esc_html__( 'Add any custom CSS to target this specific album. For example %s', 'foogallery' ), wp_kses_post( $example ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 438 | </p> |
| 439 | <table id="table_styling" class="form-table"> |
| 440 | <tbody> |
| 441 | <tr> |
| 442 | <td> |
| 443 | <textarea class="foogallery_metabox_custom_css" name="<?php echo esc_attr( FOOGALLERY_META_CUSTOM_CSS ); ?>" type="text"><?php echo esc_textarea( $custom_css ); ?></textarea> |
| 444 | </td> |
| 445 | </tr> |
| 446 | </tbody> |
| 447 | </table> |
| 448 | <?php |
| 449 | } |
| 450 | |
| 451 | public function include_required_scripts() { |
| 452 | if ( FOOGALLERY_CPT_ALBUM === foo_current_screen_post_type() ) { |
| 453 | if ( $this->gallery_descriptions_enabled() ) { |
| 454 | wp_enqueue_editor(); |
| 455 | } |
| 456 | |
| 457 | //include album selection script |
| 458 | $url = FOOGALLERY_ALBUM_URL . 'js/admin-foogallery-album.js'; |
| 459 | wp_enqueue_script( 'admin-foogallery-album', $url, array( 'jquery', 'jquery-ui-core','jquery-ui-sortable' ), FOOGALLERY_VERSION ); |
| 460 | |
| 461 | //include album selection css |
| 462 | $url = FOOGALLERY_ALBUM_URL . 'css/admin-foogallery-album.css'; |
| 463 | wp_enqueue_style( 'admin-foogallery-album', $url, array(), FOOGALLERY_VERSION ); |
| 464 | $url = FOOGALLERY_URL . 'css/admin-foogallery-gallery-piles.css'; |
| 465 | wp_enqueue_style( 'admin-foogallery-gallery-piles', $url, array(), FOOGALLERY_VERSION ); |
| 466 | |
| 467 | //spectrum needed for the colorpicker field |
| 468 | $url = FOOGALLERY_URL . 'lib/spectrum/spectrum.js'; |
| 469 | wp_enqueue_script( 'foogallery-spectrum', $url, array('jquery'), FOOGALLERY_VERSION ); |
| 470 | $url = FOOGALLERY_URL . 'lib/spectrum/spectrum.css'; |
| 471 | wp_enqueue_style( 'foogallery-spectrum', $url, array(), FOOGALLERY_VERSION ); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | private function gallery_descriptions_enabled() { |
| 476 | return 'on' === foogallery_get_setting( 'enable_gallery_descriptions' ); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Validate incoming AJAX context for album gallery details requests. |
| 481 | * |
| 482 | * Reviewers : please note that we cannot check if the gallery belongs to the album, as the user might be in the process of creating an album |
| 483 | * |
| 484 | * @return array|false |
| 485 | */ |
| 486 | private function validate_gallery_details_ajax_context() { |
| 487 | $album_id = isset( $_POST['foogallery_album_id'] ) ? absint( wp_unslash( $_POST['foogallery_album_id'] ) ) : 0; |
| 488 | $foogallery_id = isset( $_POST['foogallery_id'] ) ? absint( wp_unslash( $_POST['foogallery_id'] ) ) : 0; |
| 489 | |
| 490 | if ( $album_id <= 0 || $foogallery_id <= 0 ) { |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | if ( ! current_user_can( 'edit_post', $album_id ) || ! current_user_can( 'edit_post', $foogallery_id ) ) { |
| 495 | return false; |
| 496 | } |
| 497 | |
| 498 | $album = FooGalleryAlbum::get_by_id( $album_id ); |
| 499 | $gallery = FooGallery::get_by_id( $foogallery_id ); |
| 500 | |
| 501 | if ( false === $album || false === $gallery ) { |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | return array( |
| 506 | 'album_id' => $album_id, |
| 507 | 'foogallery_id' => $foogallery_id, |
| 508 | 'gallery' => $gallery, |
| 509 | ); |
| 510 | } |
| 511 | |
| 512 | public function ajax_get_gallery_details() { |
| 513 | if ( check_admin_referer( 'foogallery_album_gallery_details' ) ) { |
| 514 | $context = $this->validate_gallery_details_ajax_context(); |
| 515 | |
| 516 | if ( false !== $context ) { |
| 517 | $foogallery_id = $context['foogallery_id']; |
| 518 | $gallery = $context['gallery']; |
| 519 | $fields = $this->get_gallery_detail_fields( $gallery ); ?> |
| 520 | <form name="foogallery_gallery_details"> |
| 521 | <input type="hidden" name="foogallery_id" id="foogallery_id" value="<?php echo esc_attr( $foogallery_id ); ?>" /> |
| 522 | <table class="gallery-detail-fields"> |
| 523 | <tbody> |
| 524 | <?php foreach ( $fields as $field => $values ) { |
| 525 | $value = array_key_exists( 'value', $values ) ? $values['value'] : get_post_meta( $gallery->ID, $field, true ); |
| 526 | $input_id = 'foogallery-gallery-detail-fields-' . $field; |
| 527 | switch ( $values['input'] ) { |
| 528 | case 'text': |
| 529 | $values['html'] = '<input type="text" id="' . $input_id . '" name="' . $field . '" value="' . esc_attr( foogallery_sanitize_javascript( $value ) ) . '" />'; |
| 530 | break; |
| 531 | |
| 532 | case 'textarea': |
| 533 | $values['html'] = '<textarea id="' . $input_id . '" name="' . $field . '">' . esc_attr( foogallery_sanitize_javascript( $value ) ) . '</textarea>'; |
| 534 | break; |
| 535 | |
| 536 | case 'wysiwyg': |
| 537 | $values['html'] = '<textarea id="' . esc_attr( $input_id ) . '" class="foogallery-gallery-description-editor" name="' . esc_attr( $field ) . '">' . esc_textarea( $value ) . '</textarea>'; |
| 538 | break; |
| 539 | |
| 540 | case 'select': |
| 541 | $html = '<select id="' . $input_id . '" name="' . $field . '">'; |
| 542 | |
| 543 | // If options array is passed |
| 544 | if ( isset( $values['options'] ) ) { |
| 545 | // Browse and add the options |
| 546 | foreach ( $values['options'] as $k => $v ) { |
| 547 | // Set the option selected or not |
| 548 | if ( $value == $k ) |
| 549 | $selected = ' selected="selected"'; |
| 550 | else |
| 551 | $selected = ''; |
| 552 | |
| 553 | $html .= '<option' . $selected . ' value="' . $k . '">' . $v . '</option>'; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | $html .= '</select>'; |
| 558 | |
| 559 | // Set the html content |
| 560 | $values['html'] = $html; |
| 561 | |
| 562 | break; |
| 563 | |
| 564 | case 'checkbox': |
| 565 | // Set the checkbox checked or not |
| 566 | if ( $value == 'on' ) |
| 567 | $checked = ' checked="checked"'; |
| 568 | else |
| 569 | $checked = ''; |
| 570 | |
| 571 | $html = '<input' . $checked . ' type="checkbox" name="' . $field . ']" id="' . $input_id . '" />'; |
| 572 | |
| 573 | $values['html'] = $html; |
| 574 | |
| 575 | break; |
| 576 | |
| 577 | case 'radio': |
| 578 | $html = ''; |
| 579 | |
| 580 | if ( ! empty( $values['options'] ) ) { |
| 581 | $i = 0; |
| 582 | |
| 583 | foreach ( $values['options'] as $k => $v ) { |
| 584 | if ( $value == $k ) |
| 585 | $checked = ' checked="checked"'; |
| 586 | else |
| 587 | $checked = ''; |
| 588 | |
| 589 | $html .= '<input' . $checked . ' value="' . $k . '" type="radio" name="' . $field . ']" id="' . sanitize_key( $field . '_' . $i ) . '" /> <label for="' . sanitize_key( $field . '_' . $i ) . '">' . $v . '</label><br />'; |
| 590 | $i++; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | $values['html'] = $html; |
| 595 | |
| 596 | break; |
| 597 | } ?> |
| 598 | <tr class="foogallery-gallery-detail-fields-<?php echo esc_attr( $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"> |
| 599 | <th scope="row" class="label"> |
| 600 | <label for="foogallery-gallery-detail-fields-<?php echo esc_attr( $field ); ?>"><?php echo esc_html( $values['label'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label> |
| 601 | </th> |
| 602 | <td> |
| 603 | <?php echo $values['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic field HTML ?> |
| 604 | <?php if ( !empty( $values['help'] ) ) { ?><p class="help"><?php echo wp_kses_post( $values['help'] ); ?></p><?php } ?> |
| 605 | </td> |
| 606 | </tr> |
| 607 | <?php } ?> |
| 608 | </tbody> |
| 609 | </table> |
| 610 | </form><?php |
| 611 | } else { |
| 612 | echo '<h2>' . esc_html__( 'Invalid Gallery!', 'foogallery' ) . '</h2>'; |
| 613 | } |
| 614 | } |
| 615 | wp_die( '', '', array( 'response' => null ) ); |
| 616 | } |
| 617 | |
| 618 | public function ajax_save_gallery_details() { |
| 619 | if ( check_admin_referer( 'foogallery_album_gallery_details' ) ) { |
| 620 | $context = $this->validate_gallery_details_ajax_context(); |
| 621 | if ( false === $context ) { |
| 622 | wp_send_json_error( array( |
| 623 | 'message' => __( 'Invalid gallery details request.', 'foogallery' ), |
| 624 | ), 403 ); |
| 625 | } |
| 626 | |
| 627 | $gallery = $context['gallery']; |
| 628 | $fields = $this->get_gallery_detail_fields( $gallery ); |
| 629 | |
| 630 | foreach ( $fields as $field => $values ) { |
| 631 | //for every field, save some info |
| 632 | do_action( 'foogallery_album_gallery_details_save', $field, $values, $gallery ); |
| 633 | } |
| 634 | |
| 635 | wp_send_json_success(); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | public function gallery_details_save($field, $field_args, $gallery) { |
| 640 | if ( ! isset( $_POST[$field] ) ) { |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | if ( 'gallery_description' === $field ) { |
| 645 | if ( ! $this->gallery_descriptions_enabled() ) { |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | $value = wp_unslash( $_POST[$field] ); |
| 650 | $value = is_scalar( $value ) ? wp_kses_post( (string) $value ) : ''; |
| 651 | |
| 652 | $result = wp_update_post( array( |
| 653 | 'ID' => $gallery->ID, |
| 654 | 'post_content' => $value, |
| 655 | ), true ); |
| 656 | |
| 657 | if ( is_wp_error( $result ) ) { |
| 658 | wp_send_json_error( array( |
| 659 | 'message' => $result->get_error_message(), |
| 660 | ), 500 ); |
| 661 | } |
| 662 | |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | if ( 'custom_url' === $field || 'custom_target' === $field ) { |
| 667 | $value = wp_unslash( $_POST[$field] ); |
| 668 | |
| 669 | if ( 'custom_url' === $field ) { |
| 670 | $value = esc_url_raw( $value ); |
| 671 | } else { |
| 672 | $value = sanitize_key( $value ); |
| 673 | } |
| 674 | |
| 675 | update_post_meta( $gallery->ID, $field, $value ); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Get the fields that we want to edit for a gallery from the album management page |
| 681 | * @param $gallery FooGallery |
| 682 | * |
| 683 | * @return mixed|void |
| 684 | */ |
| 685 | public function get_gallery_detail_fields($gallery) { |
| 686 | |
| 687 | $target_options = apply_filters( 'foogallery_gallery_detail_fields_custom_target_options', array( |
| 688 | 'default' => __( 'Default', 'foogallery' ), |
| 689 | '_blank' => __( 'New tab (_blank)', 'foogallery' ), |
| 690 | '_self' => __( 'Same tab (_self)', 'foogallery' ) |
| 691 | ) ); |
| 692 | |
| 693 | $edit_url = get_edit_post_link( $gallery->ID ); |
| 694 | |
| 695 | $fields = array( |
| 696 | 'gallery_title' => array( |
| 697 | 'label' => __( 'Gallery Title', 'foogallery' ), |
| 698 | 'input' => 'html', |
| 699 | 'html' => '<strong>' . $gallery->safe_name() . ' <a href="' . $edit_url . '" target="_blank">' . __( 'Edit Gallery', 'foogallery' ) . '</a></strong>', |
| 700 | ) |
| 701 | ); |
| 702 | |
| 703 | if ( $this->gallery_descriptions_enabled() ) { |
| 704 | $fields['gallery_description'] = array( |
| 705 | 'label' => __( 'Gallery Description', 'foogallery' ), |
| 706 | 'input' => 'wysiwyg', |
| 707 | 'value' => isset( $gallery->_post ) ? $gallery->_post->post_content : '', |
| 708 | 'help' => __( 'Displayed under the gallery title and optionally in album captions.', 'foogallery' ), |
| 709 | ); |
| 710 | } |
| 711 | |
| 712 | $fields = array_merge( $fields, array( |
| 713 | 'gallery_template' => array( |
| 714 | 'label' => __( 'Gallery Layout', 'foogallery' ), |
| 715 | 'input' => 'html', |
| 716 | 'html' => '<strong>' . $gallery->gallery_template_name() . '</strong>', |
| 717 | ), |
| 718 | |
| 719 | 'gallery_media' => array( |
| 720 | 'label' => __( 'Media', 'foogallery' ), |
| 721 | 'input' => 'html', |
| 722 | 'html' => '<strong>' . $gallery->image_count() . '</strong>' |
| 723 | ), |
| 724 | |
| 725 | 'custom_url' => array( |
| 726 | 'label' => __( 'Custom URL', 'foogallery' ), |
| 727 | 'input' => 'text', |
| 728 | 'help' => __( 'Point your gallery to a custom URL', 'foogallery' ) |
| 729 | ), |
| 730 | |
| 731 | 'custom_target' => array( |
| 732 | 'label' => __( 'Custom Target', 'foogallery' ), |
| 733 | 'input' => 'select', |
| 734 | 'help' => __( 'Set a custom target for your gallery', 'foogallery' ), |
| 735 | 'options' => $target_options |
| 736 | ) |
| 737 | ) ); |
| 738 | |
| 739 | return apply_filters( 'foogallery_gallery_detail_fields', $fields ); |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 |