PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / mcp / preview / preview-token.php

preview-token.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/mcp/preview/preview-token.php

78 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Mcp\Preview;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 class Preview_Token {
10
11 const QUERY_ARG = 'elementor_preview_token';
12 const SECRET_NAMESPACE = 'elementor_preview_link_v1';
13
14 public static function encode( int $post_id, int $revision_id, int $expires_at, string $secret ): string {
15 $payload = self::base64url_encode( wp_json_encode( [
16 'p' => $post_id,
17 'r' => $revision_id,
18 'e' => $expires_at,
19 ] ) );
20
21 $signature = self::sign( $payload, $secret );
22
23 return $payload . '.' . $signature;
24 }
25
26 public static function decode( string $token, string $secret ): ?array {
27 $parts = explode( '.', $token );
28
29 if ( count( $parts ) !== 2 ) {
30 return null;
31 }
32
33 [ $payload, $signature ] = $parts;
34
35 if ( ! hash_equals( self::sign( $payload, $secret ), $signature ) ) {
36 return null;
37 }
38
39 $decoded = json_decode( self::base64url_decode( $payload ), true );
40
41 if ( ! is_array( $decoded ) || ! isset( $decoded['p'], $decoded['r'], $decoded['e'] ) ) {
42 return null;
43 }
44
45 return [
46 'post_id' => (int) $decoded['p'],
47 'revision_id' => (int) $decoded['r'],
48 'expires_at' => (int) $decoded['e'],
49 ];
50 }
51
52 public static function is_expired( array $claims, int $now ): bool {
53 return $now >= $claims['expires_at'];
54 }
55
56 public static function secret(): string {
57 return wp_salt( 'auth' ) . self::SECRET_NAMESPACE;
58 }
59
60 private static function sign( string $payload, string $secret ): string {
61 return self::base64url_encode( hash_hmac( 'sha256', $payload, $secret, true ) );
62 }
63
64 private static function base64url_encode( string $data ): string {
65 return rtrim( strtr( base64_encode( $data ), '+/', '-_' ), '=' );
66 }
67
68 private static function base64url_decode( string $data ): string {
69 $pad = strlen( $data ) % 4;
70
71 if ( $pad > 0 ) {
72 $data .= str_repeat( '=', 4 - $pad );
73 }
74
75 return base64_decode( strtr( $data, '-_', '+/' ) );
76 }
77 }
78