| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Preview; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class Public_Preview_Handler { |
| 12 |
|
| 13 |
const OVERRIDDEN_META_KEYS = [ |
| 14 |
'_elementor_data', |
| 15 |
'_elementor_page_settings', |
| 16 |
'_elementor_css', |
| 17 |
'_elementor_edit_mode', |
| 18 |
'_elementor_template_type', |
| 19 |
'_elementor_version', |
| 20 |
'_elementor_pro_version', |
| 21 |
'_elementor_element_cache', |
| 22 |
]; |
| 23 |
|
| 24 |
private ?int $active_post_id = null; |
| 25 |
private ?int $active_revision_id = null; |
| 26 |
private array $revision_meta_cache = []; |
| 27 |
|
| 28 |
public function register(): void { |
| 29 |
add_action( 'parse_request', [ $this, 'maybe_activate' ], 1 ); |
| 30 |
} |
| 31 |
|
| 32 |
public function maybe_activate( \WP $wp ): void { |
| 33 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 34 |
$raw_token = isset( $_GET[ Preview_Token::QUERY_ARG ] ) ? sanitize_text_field( wp_unslash( $_GET[ Preview_Token::QUERY_ARG ] ) ) : ''; |
| 35 |
|
| 36 |
if ( '' === $raw_token ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
$claims = Preview_Token::decode( $raw_token, Preview_Token::secret() ); |
| 41 |
|
| 42 |
if ( null === $claims ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
if ( Preview_Token::is_expired( $claims, time() ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if ( ! $this->is_valid_post( $claims['post_id'] ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! $this->is_valid_revision( $claims['post_id'], $claims['revision_id'] ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$this->active_post_id = $claims['post_id']; |
| 59 |
$this->active_revision_id = $claims['revision_id']; |
| 60 |
$this->prime_revision_meta_cache( $claims['revision_id'] ); |
| 61 |
|
| 62 |
$this->rewrite_main_query( $wp, $claims['post_id'] ); |
| 63 |
$this->install_render_filters(); |
| 64 |
$this->send_privacy_headers(); |
| 65 |
} |
| 66 |
|
| 67 |
private function is_valid_post( int $post_id ): bool { |
| 68 |
if ( $post_id <= 0 ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
$post = get_post( $post_id ); |
| 73 |
|
| 74 |
if ( ! $post || 'revision' === $post->post_type ) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
$document = Plugin::$instance->documents->get( $post_id ); |
| 79 |
|
| 80 |
return $document && $document->is_built_with_elementor(); |
| 81 |
} |
| 82 |
|
| 83 |
private function is_valid_revision( int $post_id, int $revision_id ): bool { |
| 84 |
$revision = get_post( $revision_id ); |
| 85 |
|
| 86 |
if ( ! $revision || 'revision' !== $revision->post_type ) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
return (int) $revision->post_parent === $post_id; |
| 91 |
} |
| 92 |
|
| 93 |
private function rewrite_main_query( \WP $wp, int $post_id ): void { |
| 94 |
$post = get_post( $post_id ); |
| 95 |
|
| 96 |
if ( ! $post ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
$wp->query_vars = [ |
| 101 |
'p' => $post_id, |
| 102 |
'post_type' => $post->post_type, |
| 103 |
'post_status' => [ 'publish', 'draft', 'pending', 'private', 'future', 'auto-draft' ], |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
private function install_render_filters(): void { |
| 108 |
add_filter( 'get_post_metadata', [ $this, 'filter_post_metadata' ], 10, 4 ); |
| 109 |
add_filter( 'posts_results', [ $this, 'filter_posts_results' ], 10, 1 ); |
| 110 |
add_filter( 'the_preview', [ $this, 'filter_the_preview' ], 10, 1 ); |
| 111 |
} |
| 112 |
|
| 113 |
public function filter_post_metadata( $value, $object_id, $meta_key, $single ) { |
| 114 |
if ( (int) $object_id !== $this->active_post_id ) { |
| 115 |
return $value; |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! in_array( $meta_key, self::OVERRIDDEN_META_KEYS, true ) ) { |
| 119 |
return $value; |
| 120 |
} |
| 121 |
|
| 122 |
if ( ! array_key_exists( $meta_key, $this->revision_meta_cache ) ) { |
| 123 |
return $value; |
| 124 |
} |
| 125 |
|
| 126 |
return $this->revision_meta_cache[ $meta_key ]; |
| 127 |
} |
| 128 |
|
| 129 |
private function prime_revision_meta_cache( int $revision_id ): void { |
| 130 |
foreach ( self::OVERRIDDEN_META_KEYS as $meta_key ) { |
| 131 |
$values = get_post_meta( $revision_id, $meta_key, false ); |
| 132 |
|
| 133 |
if ( is_array( $values ) && ! empty( $values ) ) { |
| 134 |
$this->revision_meta_cache[ $meta_key ] = $values; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public function filter_posts_results( $posts ) { |
| 140 |
if ( ! is_array( $posts ) || null === $this->active_post_id ) { |
| 141 |
return $posts; |
| 142 |
} |
| 143 |
|
| 144 |
foreach ( $posts as $post ) { |
| 145 |
if ( isset( $post->ID ) && (int) $post->ID === $this->active_post_id ) { |
| 146 |
$post->post_status = 'publish'; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return $posts; |
| 151 |
} |
| 152 |
|
| 153 |
public function filter_the_preview( $post ) { |
| 154 |
if ( isset( $post->ID ) && (int) $post->ID === $this->active_post_id ) { |
| 155 |
$post->post_status = 'publish'; |
| 156 |
} |
| 157 |
|
| 158 |
return $post; |
| 159 |
} |
| 160 |
|
| 161 |
private function send_privacy_headers(): void { |
| 162 |
if ( headers_sent() ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
header( 'X-Robots-Tag: noindex, nofollow, noarchive' ); |
| 167 |
header( 'Cache-Control: private, no-store, max-age=0' ); |
| 168 |
header( 'Referrer-Policy: no-referrer' ); |
| 169 |
} |
| 170 |
} |
| 171 |
|