self::REDACTED, 'type' => $type, 'redacted' => true, ); } if ( is_object( $value ) ) { $value = json_decode( (string) wp_json_encode( $value ), true ); } if ( is_array( $value ) ) { $json = (string) wp_json_encode( $value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); if ( strlen( $json ) > self::MAX_STRUCT_BYTES ) { return array( 'value' => null, 'type' => $type, 'summarized' => sprintf( '%s with %d items, %d bytes (too large to show)', $type, count( $value ), strlen( $json ) ), ); } return array( 'value' => self::redact_nested( $value ), 'type' => $type, ); } if ( is_string( $value ) ) { if ( is_serialized( $value ) ) { return array( 'value' => null, 'type' => 'serialized', 'summarized' => sprintf( 'serialized data, %d bytes (not shown)', strlen( $value ) ), ); } if ( mb_strlen( $value ) > self::MAX_VALUE_CHARS ) { return array( 'value' => mb_substr( $value, 0, self::MAX_VALUE_CHARS ) . '…', 'type' => $type, 'summarized' => sprintf( 'text of %d characters, truncated', mb_strlen( $value ) ), ); } } return array( 'value' => $value, 'type' => $type, ); } /** * Redacts sensitive-looking keys inside a (small) array value. * * @param array $value Value. * @return array */ private static function redact_nested( array $value ): array { foreach ( $value as $k => $v ) { if ( is_string( $k ) && self::is_sensitive( $k ) ) { $value[ $k ] = self::REDACTED; } elseif ( is_array( $v ) ) { $value[ $k ] = self::redact_nested( $v ); } } return $value; } /** * Simple type name of a value. * * @param mixed $value Value. */ public static function type_of( $value ): string { if ( null === $value || '' === $value ) { return 'empty'; } if ( is_bool( $value ) ) { return 'bool'; } if ( is_int( $value ) || is_float( $value ) ) { return 'number'; } if ( is_array( $value ) ) { return array_is_list( $value ) ? 'list' : 'object'; } if ( is_object( $value ) ) { return 'object'; } $value = (string) $value; if ( is_serialized( $value ) ) { return 'serialized'; } if ( is_numeric( $value ) ) { return 'number'; } if ( in_array( strtolower( $value ), array( 'yes', 'no', 'true', 'false', 'on', 'off' ), true ) ) { return 'bool'; } if ( preg_match( '/^field_[0-9a-f]{6,}$/D', $value ) ) { return 'acf_reference'; } if ( preg_match( '/^[\[{]/', $value ) && null !== json_decode( $value ) ) { return 'json'; } if ( preg_match( '/^\d{4}-\d{2}-\d{2}/', $value ) || preg_match( '/^\d{8}$/', $value ) ) { return 'date'; } if ( preg_match( '#^https?://#i', $value ) ) { return 'url'; } if ( preg_match( '/<[a-z][^>]*>/i', $value ) ) { return 'html'; } return 'string'; } }