*/ public static function defaults(): array { return array( 'verification' => '', 'ga4_id' => '', 'gtm_id' => '', 'consent_denied' => false, 'track_editors' => false, ); } /** * Cleans values before they are saved. * * @param array $values Values. * @return array */ public static function sanitize( array $values ): array { $values = array_merge( self::defaults(), $values ); return array( 'verification' => self::verification( (string) $values['verification'] ), 'ga4_id' => self::match( (string) $values['ga4_id'], '/^G-[A-Z0-9]{6,12}$/' ), 'gtm_id' => self::match( (string) $values['gtm_id'], '/^GTM-[A-Z0-9]{4,10}$/' ), 'consent_denied' => (bool) $values['consent_denied'], 'track_editors' => (bool) $values['track_editors'], ); } /** * The verification code from either the bare code or the whole meta tag Google shows. * * @param string $raw What the owner pasted. */ public static function verification( string $raw ): string { $raw = trim( $raw ); if ( preg_match( '/content\s*=\s*["\']([^"\']+)["\']/i', $raw, $found ) ) { $raw = $found[1]; } return preg_match( '/^[A-Za-z0-9_-]{20,100}$/', $raw ) ? $raw : ''; } /** * Keeps a value only when it has the exact shape, after trimming and upper-casing. * * @param string $raw Pasted value. * @param string $pattern Pattern of the allowed shape. */ private static function match( string $raw, string $pattern ): string { $raw = strtoupper( trim( $raw ) ); return preg_match( $pattern, $raw ) ? $raw : ''; } }