PluginProbe
Blocks – Reusable Content, Shortcodes & Site Variables / trunk
Blocks – Reusable Content, Shortcodes & Site Variables vtrunk
26.09.03.15 26.08.31.21 26.08.22.20 26.08.22.22 26.08.22.17 26.08.22.13 26.08.21.19 26.08.07.23 26.07.19.14 26.07.13.21 26.07.13.17 26.07.12.13 026.07.07.21 026.07.05.18 026.06.26.20 026.06.26.21 026.06.08.20 026.05.13.14 026.04.29.10 trunk 026.02.22.22 026.03.16.23 026.04.23.13
blocks / includes / signls / src / SiteIdentity.php

SiteIdentity.php in Blocks – Reusable Content, Shortcodes & Site Variables trunk, at includes/signls/src/SiteIdentity.php

61 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shared random WordPress-origin identity.
4 *
5 * @package signls-sdk
6 * @license GPL-3.0-or-later
7 */
8
9 namespace Signls\Sdk\V1;
10
11 defined( 'ABSPATH' ) || exit;
12
13 final class SiteIdentity {
14
15 private const ID_OPTION = 'signls_sdk_site_id';
16
17 private const ORIGIN_OPTION = 'signls_sdk_site_origin';
18
19 public function site_id(): string {
20 $current_origin = $this->origin_fingerprint();
21 $stored_origin_candidate = get_option( self::ORIGIN_OPTION, '' );
22 $site_id_candidate = get_option( self::ID_OPTION, '' );
23 $stored_origin = is_scalar( $stored_origin_candidate ) ? (string) $stored_origin_candidate : '';
24 $site_id = is_scalar( $site_id_candidate ) ? (string) $site_id_candidate : '';
25
26 if ( ! self::valid( $site_id ) || ( '' !== $stored_origin && ! hash_equals( $stored_origin, $current_origin ) ) ) {
27 $site_id = bin2hex( random_bytes( 16 ) );
28 self::replace_option( self::ID_OPTION, $site_id );
29 self::replace_option( self::ORIGIN_OPTION, $current_origin );
30 } elseif ( '' === $stored_origin ) {
31 self::replace_option( self::ORIGIN_OPTION, $current_origin );
32 }
33
34 return $site_id;
35 }
36
37 private function origin_fingerprint(): string {
38 $origin = function_exists( 'home_url' ) ? (string) home_url( '/' ) : 'http://local/';
39 $parts = wp_parse_url( $origin );
40 $parts = is_array( $parts ) ? $parts : array();
41 $scheme = isset( $parts['scheme'] ) ? strtolower( (string) $parts['scheme'] ) : 'http';
42 $host = isset( $parts['host'] ) ? strtolower( rtrim( (string) $parts['host'], '.' ) ) : 'local';
43 $port = isset( $parts['port'] ) ? (int) $parts['port'] : ( 'https' === $scheme ? 443 : 80 );
44 $path = isset( $parts['path'] ) ? '/' . trim( (string) $parts['path'], '/' ) : '/';
45 $path = '/' === $path ? $path : $path . '/';
46 $salt = function_exists( 'wp_salt' ) ? (string) wp_salt( 'auth' ) : 'signls-test-salt';
47 return hash_hmac( 'sha256', $scheme . '://' . $host . ':' . $port . $path, $salt );
48 }
49
50 private static function replace_option( string $name, string $value ): bool {
51 if ( false === get_option( $name, false ) ) {
52 return add_option( $name, $value, '', false );
53 }
54 return update_option( $name, $value, false );
55 }
56
57 private static function valid( string $value ): bool {
58 return 1 === preg_match( '/^[a-f0-9]{32}$/', $value );
59 }
60 }
61