source = $source; } /** * Overrides a single value in the underlying source array. * Used when one metabox needs to hand a merged/derived value to a * later-running box that reads the same key from this same instance * (e.g. a box that injects a synthesized condition into 'conditions_tree' * before Conditions_Box::save() re-reads it in the same save request). * * @param string $key * @param mixed $value * * @return void */ public function override( string $key, mixed $value ): void { $this->source[ $key ] = $value; } /** * Retrieves a value without any sanitization. * Needs to be sanitized manually. * * @param string $key * @param string $default * * @return string */ public function getUnsanitized( string $key, mixed $default = '' ): mixed { if ( ! isset( $this->source[ $key ] ) ) { return $default; } return wp_unslash( $this->source[ $key ] ); } /** * Retrieves a value as a sanitized plain text string. * This is the safe default for any text input. * * @param string $key The key of the value to retrieve. * @param string $default The default value to return if the key doesn't exist. * * @return string The sanitized string. */ public function getString( string $key, string $default = '' ): string { if ( ! isset( $this->source[ $key ] ) ) { return $default; } $value = wp_unslash( $this->source[ $key ] ); return ! is_scalar( $value ) ? $default : sanitize_text_field( (string) $value ); } /** * Retrieves a value as a sanitized HTML class. * * @param string $key The key of the value to retrieve. * @param string $default The default value to return if the key doesn't exist. * * @return string The sanitized string. */ public function getHtmlClass( string $key, string $default = '' ): string { if ( ! isset( $this->source[ $key ] ) ) { return $default; } $value = wp_unslash( $this->source[ $key ] ); return ! is_scalar( $value ) ? $default : sanitize_html_class( (string) $value ); } /** * Retrieves a space-separated list of CSS classes, each sanitized on its own. * * @param string $key The key of the value to retrieve. * @param string $default The default value to return if the key doesn't exist. * * @return string */ public function getHtmlClasses( string $key, string $default = '' ): string { if ( ! isset( $this->source[ $key ] ) ) { return $default; } $value = wp_unslash( $this->source[ $key ] ); return ! is_scalar( $value ) ? $default : Util::sanitize_html_classes( (string) $value ); } /** * Retrieves a value and sanitizes it with the same rules as post content. * Wrapper for wp_kses_post(). * * @param string $key The key of the value to retrieve. * @param string $default The default value. * * @return string The sanitized string. */ public function getKsesPost( string $key, string $default = '' ): string { if ( ! isset( $this->source[ $key ] ) ) { return $default; } $value = wp_unslash( $this->source[ $key ] ); if ( ! is_scalar( $value ) ) { return $default; } return wp_kses_post( (string) $value ); } /** * Retrieves custom code (like