[ 'clean', 'login' ], * 'modules' => [ 'clean' => [ 'emojis' => 1, ... ], 'login' => [ ... ] ], * ] */ defined( 'ABSPATH' ) || exit; final class OPTK_Settings { private const PAGE = 'optimise-creative-toolkit'; /** * Branded page header shared by every toolkit screen. * * @param string $title Page title (already translated). * @param string $subtitle Optional line under the title (already translated). */ public static function header( string $title, string $subtitle = '' ): void { ?>

modules = $modules; } public function init(): void { add_action( 'admin_menu', [ $this, 'menu' ] ); add_action( 'admin_init', [ $this, 'register' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'assets' ] ); add_filter( 'plugin_action_links_' . plugin_basename( OPTK_FILE ), [ $this, 'action_link' ] ); ( new OPTK_Transfer() )->init(); } public function menu(): void { add_options_page( __( 'Optimise Creative Toolkit', 'optimise-creative-toolkit' ), __( 'Optimise Creative Toolkit', 'optimise-creative-toolkit' ), 'manage_options', self::PAGE, [ $this, 'render' ] ); } public function register(): void { register_setting( self::PAGE, OPTK_OPTION, [ 'type' => 'array', 'sanitize_callback' => [ $this, 'sanitize' ], 'default' => [ 'enabled' => [], 'modules' => [] ], ] ); } public function assets( string $hook ): void { if ( 'settings_page_' . self::PAGE !== $hook ) { return; } wp_enqueue_style( 'optk-admin', OPTK_URL . 'assets/admin.css', [], OPTK_VERSION ); } public function action_link( array $links ): array { $url = admin_url( 'options-general.php?page=' . self::PAGE ); array_unshift( $links, '' . esc_html__( 'Settings', 'optimise-creative-toolkit' ) . '' ); return $links; } public function sanitize( $input ): array { $input = is_array( $input ) ? $input : []; $clean = [ 'enabled' => [], 'modules' => [] ]; foreach ( (array) ( $input['enabled'] ?? [] ) as $id ) { $id = sanitize_key( $id ); if ( isset( $this->modules[ $id ] ) ) { $clean['enabled'][] = $id; } } foreach ( $this->modules as $id => $module ) { $raw = $input['modules'][ $id ] ?? []; foreach ( $module->fields() as $field ) { $key = $field['key']; $clean['modules'][ $id ][ $key ] = $this->sanitize_field( $field, $raw[ $key ] ?? null ); } } return $clean; } private function sanitize_field( array $field, $value ) { switch ( $field['type'] ) { case 'checkbox': return empty( $value ) ? 0 : 1; case 'number': return (int) $value; case 'url': return esc_url_raw( (string) $value ); case 'color': return sanitize_hex_color( (string) $value ) ?: ( $field['default'] ?? '' ); case 'textarea': return sanitize_textarea_field( (string) $value ); case 'select': return array_key_exists( (string) $value, $field['options'] ?? [] ) ? (string) $value : ( $field['default'] ?? '' ); default: return sanitize_text_field( (string) $value ); } } public function render(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $saved = get_option( OPTK_OPTION, [] ); $enabled = (array) ( $saved['enabled'] ?? [] ); ?>
modules as $id => $module ) : $is_on = in_array( $id, $enabled, true ); $base = OPTK_OPTION . '[modules][' . $id . ']'; ?>

description() ); ?>

fields() ) : ?>
fields() as $field ) { $value = $saved['modules'][ $id ][ $field['key'] ] ?? ( $field['default'] ?? '' ); $this->render_field( $field, $base . '[' . $field['key'] . ']', $value ); } ?>

'; if ( 'checkbox' === $type ) { printf( '', esc_attr( $name ), checked( ! empty( $value ), true, false ), esc_html( $field['label'] ) ); } else { printf( '', esc_attr( $id ), esc_html( $field['label'] ) ); if ( 'textarea' === $type ) { printf( '', esc_attr( $id ), esc_attr( $name ), esc_textarea( (string) $value ) ); } elseif ( 'select' === $type ) { printf( ''; } else { $input_type = in_array( $type, [ 'url', 'number', 'color' ], true ) ? $type : 'text'; printf( '', esc_attr( $input_type ), esc_attr( $id ), esc_attr( $name ), esc_attr( (string) $value ) ); } } if ( ! empty( $field['help'] ) ) { echo '

' . esc_html( $field['help'] ) . '

'; } echo ''; } }