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
trait-gallery-preview.php
121 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Responsive Lightbox Gallery Preview & Revision Trait. |
| 8 | * |
| 9 | * Handles gallery preview and revision functionality. |
| 10 | * |
| 11 | * @trait Responsive_Lightbox_Gallery_Preview |
| 12 | */ |
| 13 | trait Responsive_Lightbox_Gallery_Preview { |
| 14 | |
| 15 | /** |
| 16 | * Save gallery revision metadata. |
| 17 | * |
| 18 | * @param int $revision_id |
| 19 | * @return void |
| 20 | */ |
| 21 | public function save_revision( $revision_id ) { |
| 22 | // get revision |
| 23 | $revision = get_post( $revision_id ); |
| 24 | |
| 25 | // get gallery ID |
| 26 | $post_id = $revision->post_parent; |
| 27 | |
| 28 | // is it rl gallery? |
| 29 | if ( get_post_type( $post_id ) !== 'rl_gallery' ) |
| 30 | return; |
| 31 | |
| 32 | $this->revision_id = $revision_id; |
| 33 | |
| 34 | if ( ! wp_is_post_revision( $revision_id ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || empty( $_POST['rl_gallery'] ) ) |
| 35 | return; |
| 36 | |
| 37 | // save revisioned meta data |
| 38 | $this->save_gallery( wp_unslash( $_POST ), $revision_id, true ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Update preview link. |
| 43 | * |
| 44 | * @param string $link Preview link |
| 45 | * @return string |
| 46 | */ |
| 47 | public function preview_post_link( $link ) { |
| 48 | // add gallery revision id |
| 49 | if ( property_exists( $this, 'revision_id' ) && ! is_null( $this->revision_id ) ) { |
| 50 | $post_id = wp_get_post_parent_id( $this->revision_id ); |
| 51 | |
| 52 | // is it valid rl_gallery post? |
| 53 | if ( $post_id && get_post_type( $post_id ) === 'rl_gallery' ) |
| 54 | return add_query_arg( 'rl_gallery_revision_id', $this->revision_id, $link ); |
| 55 | } |
| 56 | |
| 57 | return $link; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Delete gallery revision at shutdown. |
| 62 | * |
| 63 | * @global object $post |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | public function shutdown_preview() { |
| 68 | // is it a frontend preview? |
| 69 | if ( is_preview() && isset( $_GET['rl_gallery_revision_id'] ) ) { |
| 70 | global $post; |
| 71 | |
| 72 | // cast revision ID |
| 73 | $revision_id = (int) $_GET['rl_gallery_revision_id']; |
| 74 | |
| 75 | // is it a valid revision? |
| 76 | if ( get_post_type( $post->ID ) === 'rl_gallery' && wp_is_post_revision( $revision_id ) === (int) $post->ID ) |
| 77 | wp_delete_post_revision( $revision_id ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Filter gallery meta data needed for frontend gallery preview. |
| 83 | * |
| 84 | * @param mixed $value Meta value to filter |
| 85 | * @param int $object_id |
| 86 | * @param string $meta_key Meta key to filter a value for |
| 87 | * @param bool $single Whether to return a single value |
| 88 | * @return mixed |
| 89 | */ |
| 90 | public function filter_preview_metadata( $value, $object_id, $meta_key, $single ) { |
| 91 | // ignore other post types |
| 92 | if ( get_post_type( $object_id ) !== 'rl_gallery' ) |
| 93 | return $value; |
| 94 | |
| 95 | // get current post |
| 96 | $post = get_post(); |
| 97 | |
| 98 | // prepare keys |
| 99 | $keys = array( '_rl_featured_image_type', '_rl_featured_image', '_rl_images_count', '_thumbnail_id' ); |
| 100 | |
| 101 | // add other metakeys |
| 102 | foreach ( array_keys( $this->tabs ) as $key ) { |
| 103 | $keys[] = '_rl_' . $key; |
| 104 | } |
| 105 | |
| 106 | // restrict only to specified data |
| 107 | if ( empty( $post ) || (int) $post->ID !== (int) $object_id || ! in_array( $meta_key, $keys, true ) || $post->post_type === 'revision' ) |
| 108 | return $value; |
| 109 | |
| 110 | // grab the last autosave |
| 111 | $preview = wp_get_post_autosave( $post->ID ); |
| 112 | |
| 113 | // invalid revision? |
| 114 | if ( ! is_object( $preview ) ) |
| 115 | return $value; |
| 116 | |
| 117 | // finally replace metadata |
| 118 | return array( get_post_meta( $preview->ID, $meta_key, $single ) ); |
| 119 | } |
| 120 | } |
| 121 |