|null */ private ?array $cache = null; /** * Registers hooks. * * @return void */ public function register(): void { add_action( 'init', $this->register_setting( ... ) ); add_action( 'add_option_' . self::OPTION, $this->flush_cache( ... ) ); add_action( 'update_option_' . self::OPTION, $this->flush_cache( ... ) ); add_action( 'delete_option_' . self::OPTION, $this->flush_cache( ... ) ); } /** * Registers the option with a sanitize callback and defaults. * * @return void */ public function register_setting(): void { register_setting( self::GROUP, self::OPTION, array( 'type' => 'object', 'sanitize_callback' => $this->sanitize( ... ), 'default' => $this->defaults(), 'show_in_rest' => false, ) ); } /** * Clears the request cache after the option changed. * * @return void */ public function flush_cache(): void { $this->cache = null; } /** * Product type slugs that can get their own text. * * @return string[] */ public function type_keys(): array { /** * Filters the product types that can get their own button text. * * @param string[] $types Product type slugs, e.g. "simple". */ $types = apply_filters( 'adtct_product_types', Product_Type::values() ); $types = array_map( static fn ( mixed $type ): string => is_string( $type ) ? sanitize_key( $type ) : '', (array) $types ); return array_values( array_unique( array_filter( $types ) ) ); } /** * Default settings. * * @return array */ public function defaults(): array { $texts = array(); foreach ( $this->type_keys() as $type ) { foreach ( Context::cases() as $context ) { $texts[ $type ][ $context->value ] = ''; } } return array( 'enabled' => true, 'texts' => $texts, 'out_of_stock' => '', 'added_message' => '', ); } /** * All settings, merged with defaults (only strings and booleans survive). * * @return array */ public function all(): array { if ( null !== $this->cache ) { return $this->cache; } $stored = get_option( self::OPTION, array() ); $stored = is_array( $stored ) ? $stored : array(); $settings = $this->defaults(); if ( isset( $stored['enabled'] ) ) { $settings['enabled'] = (bool) $stored['enabled']; } foreach ( array_keys( $settings['texts'] ) as $type ) { foreach ( Context::cases() as $context ) { $value = $stored['texts'][ $type ][ $context->value ] ?? null; if ( is_string( $value ) ) { $settings['texts'][ $type ][ $context->value ] = $value; } } } foreach ( self::TEXT_KEYS as $key ) { if ( isset( $stored[ $key ] ) && is_string( $stored[ $key ] ) ) { $settings[ $key ] = $stored[ $key ]; } } $this->cache = $settings; return $settings; } /** * Whether custom texts are switched on. * * @return bool */ public function is_enabled(): bool { return (bool) $this->all()['enabled']; } /** * Text configured for a product type and context. * * @param string $type Product type slug. * @param Context $context Button context. * @return string */ public function type_text( string $type, Context $context ): string { $value = $this->all()['texts'][ $type ][ $context->value ] ?? ''; return is_string( $value ) ? $value : ''; } /** * A top-level text setting ("out_of_stock" or "added_message"). * * @param string $key Setting key. * @return string */ public function text( string $key ): string { $value = $this->all()[ $key ] ?? ''; return is_string( $value ) ? $value : ''; } /** * Sanitizes the complete settings array. * * Types that are missing in the input (e.g. hidden because their extension is * inactive) keep their stored value. * * @param mixed $input Raw input. * @return array */ public function sanitize( mixed $input ): array { $input = is_array( $input ) ? $input : array(); $current = $this->all(); $output = $this->defaults(); $output['enabled'] = ! empty( $input['enabled'] ); foreach ( array_keys( $output['texts'] ) as $type ) { foreach ( Context::cases() as $context ) { $output['texts'][ $type ][ $context->value ] = isset( $input['texts'][ $type ][ $context->value ] ) ? self::clean_text( $input['texts'][ $type ][ $context->value ] ) : $current['texts'][ $type ][ $context->value ]; } } foreach ( self::TEXT_KEYS as $key ) { $output[ $key ] = isset( $input[ $key ] ) ? self::clean_text( $input[ $key ] ) : ''; } return $output; } /** * Sanitizes and stores settings. * * @param array $settings Settings. * @return void */ public function save( array $settings ): void { update_option( self::OPTION, $this->sanitize( $settings ) ); $this->flush_cache(); } /** * Sanitizes a single text value: plain text, max. MAX_LENGTH characters. * * @param mixed $value Raw value. * @return string */ public static function clean_text( mixed $value ): string { if ( ! is_scalar( $value ) ) { return ''; } $value = sanitize_text_field( (string) $value ); return trim( mb_substr( $value, 0, self::MAX_LENGTH ) ); } }