>> */ private array $mirrors = array(); /** * Registered tabs. * * @var array */ private array $tabs = array(); /** * Registered sections, keyed by tab id. * * @var array> */ private array $sections = array(); /** * Registered fields, keyed by tab id then section id. * * @var array>> */ private array $fields = array(); /** * Which plugin registered each field, keyed by tab, section, then field id. * * @var array>> */ private array $field_owners = array(); /** * The plugin currently registering settings -- 'free', 'pro', 'woo', * 'tools', etc. Set by `ModuleManager` around each module's * `register()`, so a section or field is tagged with the plugin that * added it without any call site having to say so. The Settings screen * uses it to color-code Pro/Woo/Tools settings. * * @var string */ private string $owner = 'free'; /** * Set the plugin that owns everything registered from now on. * * @param string $owner Owner key, e.g. 'free' or 'pro'. * @return void */ public function set_owner( string $owner ): void { $this->owner = sanitize_key( $owner ); } /** * The plugin currently registering settings. * * @return string */ public function current_owner(): string { return $this->owner; } /** * Register a settings tab. First registration wins if called twice with * the same id (so multiple modules can safely share a tab). * * @param string $id Tab id. * @param string $label Tab label. * @param string $description Shown as the tab's default documentation panel content. * @param int $priority Ordering among tabs. * @param string $page Which admin page shows this tab (see `get_tabs_for_page()`) -- * 'default' is Free's own Settings screen; a dedicated page * (e.g. the Woo Add-on's own submenu) uses its own key so its * tabs don't also clutter Free's Settings screen, and vice versa. * @return void */ public function add_tab( string $id, string $label, string $description = '', int $priority = 10, string $page = 'default' ): void { if ( isset( $this->tabs[ $id ] ) ) { return; } $this->tabs[ $id ] = array( 'id' => $id, 'label' => $label, 'description' => $description, 'priority' => $priority, 'page' => $page, ); } /** * Register a section within a tab. * * @param string $tab Parent tab id (must already be registered). * @param string $id Section id, unique within the tab. * @param string $label Section label. * @param string $description Section description. * @param int $priority Ordering within the tab. * @return void */ public function add_section( string $tab, string $id, string $label, string $description = '', int $priority = 10 ): void { if ( ! isset( $this->tabs[ $tab ] ) || isset( $this->sections[ $tab ][ $id ] ) ) { return; } $this->sections[ $tab ][ $id ] = array( 'id' => $id, 'label' => $label, 'description' => $description, 'priority' => $priority, 'owner' => $this->owner, ); } /** * Register a field within a section. * * @param string $tab Parent tab id. * @param string $section Parent section id (must already be registered). * @param Field $field The field. * @return void */ public function add_field( string $tab, string $section, Field $field ): void { if ( ! isset( $this->sections[ $tab ][ $section ] ) ) { return; } $this->fields[ $tab ][ $section ][ $field->id ] = $field; $this->field_owners[ $tab ][ $section ][ $field->id ] = $this->owner; } /** * Also show a field that lives in another tab/section here, so related * settings can be edited in one place. Nothing is copied: the mirror * reads the source's own stored value, and saving this tab writes back * into the source tab's option (see `save_mirrors()`), so both places * always show the same value. The source is resolved when rendering or * saving, so it may be registered later (e.g. by Pro). * * @param string $tab Tab to show the field in. * @param string $section Section to show the field in (must exist by render time). * @param string $source_tab Tab the field belongs to. * @param string $source_section Section the field belongs to. * @param string $field_id Field id. * @param int $priority Ordering among this section's mirrors. * @param string $heading Heading shown above this section's mirrors * (the first non-empty one wins). Defaults * to "Related emails". * @return void */ public function add_mirror( string $tab, string $section, string $source_tab, string $source_section, string $field_id, int $priority = 10, string $heading = '' ): void { $this->mirrors[ $tab ][ $section ][] = array( 'tab' => $source_tab, 'section' => $source_section, 'field' => $field_id, 'priority' => $priority, 'heading' => $heading, ); } /** * A section's mirrored fields whose source exists, in priority order. * * @param string $tab Tab id. * @param string $section Section id. * @return array */ public function get_mirrors( string $tab, string $section ): array { $list = $this->mirrors[ $tab ][ $section ] ?? array(); usort( $list, static fn ( $a, $b ) => $a['priority'] <=> $b['priority'] ); $resolved = array(); foreach ( $list as $mirror ) { $field = $this->fields[ $mirror['tab'] ][ $mirror['section'] ][ $mirror['field'] ] ?? null; if ( $field && $mirror['tab'] !== $tab ) { $resolved[] = array( 'tab' => $mirror['tab'], 'section' => $mirror['section'], 'field' => $field, 'owner' => $this->get_field_owner( $mirror['tab'], $mirror['section'], $mirror['field'] ), 'heading' => $mirror['heading'], ); } } return $resolved; } /** * When a tab's own form is saved, write its mirrored fields' submitted * values into their source tabs' options. Only runs for that tab's own * options.php submission (whose nonce options.php has already checked), * never for imports or programmatic updates. * * @param string $tab Tab id being saved. * @return void */ private function save_mirrors( string $tab ): void { // phpcs:disable WordPress.Security.NonceVerification.Missing -- options.php verified this option page's nonce before saving. if ( empty( $this->mirrors[ $tab ] ) || ! isset( $_POST['option_page'] ) || $this->option_name( $tab ) !== $_POST['option_page'] || ! current_user_can( 'manage_options' ) ) { return; } $updates = array(); foreach ( array_keys( $this->mirrors[ $tab ] ) as $section ) { foreach ( $this->get_mirrors( $tab, $section ) as $mirror ) { $option = $this->option_name( $mirror['tab'] ); $id = $mirror['field']->id; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized by the field's own sanitizer below. $posted = isset( $_POST[ $option ] ) && is_array( $_POST[ $option ] ) ? wp_unslash( $_POST[ $option ] ) : array(); if ( ! isset( $posted[ $mirror['section'] ] ) || ! is_array( $posted[ $mirror['section'] ] ) || ! array_key_exists( $id, $posted[ $mirror['section'] ] ) ) { continue; } // Start from the stored values merged over defaults -- exactly // what saving the source tab itself would submit -- so the // source tab's sanitizer never sees a key as missing. if ( ! isset( $updates[ $option ] ) ) { $updates[ $option ] = $this->complete_values( $mirror['tab'] ); } $updates[ $option ][ $mirror['section'] ][ $id ] = $mirror['field']->sanitize( $posted[ $mirror['section'] ][ $id ] ); } } // phpcs:enable WordPress.Security.NonceVerification.Missing foreach ( $updates as $option => $value ) { update_option( $option, $value ); } } /** * A tab's values as its own form would submit them: stored values over * defaults, with a structured (keyed-array) value also filled in from * its default keys, e.g. an email stored as just `{ enabled }`. * * @param string $tab Tab id. * @return array> */ private function complete_values( string $tab ): array { $values = $this->get_values( $tab ); foreach ( $this->get_sections( $tab ) as $section_id => $section ) { foreach ( $this->get_fields( $tab, $section_id ) as $field ) { $value = $values[ $section_id ][ $field->id ] ?? null; if ( is_array( $field->default ) && $field->default && ! array_is_list( $field->default ) && is_array( $value ) ) { $values[ $section_id ][ $field->id ] = array_merge( $field->default, $value ); } } } return $values; } /** * Which plugin registered a field ('free' if unknown). * * @param string $tab Tab id. * @param string $section Section id. * @param string $field_id Field id. * @return string */ public function get_field_owner( string $tab, string $section, string $field_id ): string { return $this->field_owners[ $tab ][ $section ][ $field_id ] ?? 'free'; } /** * All registered tabs, ordered by priority, regardless of which page * they belong to -- used internally (sanitizing, persisting, and * defaulting every tab's values must happen for all of them, not just * the ones a particular page renders). Settings pages render via * `get_tabs_for_page()` instead. * * @return array */ public function get_tabs(): array { $tabs = $this->tabs; uasort( $tabs, static fn ( $a, $b ) => $a['priority'] <=> $b['priority'] ); return $tabs; } /** * Registered tabs belonging to one admin page, ordered by priority. * * @param string $page The page key passed to `add_tab()` (default 'default'). * @return array */ public function get_tabs_for_page( string $page = 'default' ): array { return array_filter( $this->get_tabs(), static fn ( array $tab ): bool => $page === $tab['page'] ); } /** * Whether a tab exists. * * @param string $tab Tab id. * @return bool */ public function has_tab( string $tab ): bool { return isset( $this->tabs[ $tab ] ); } /** * A tab's sections, ordered by priority. * * @param string $tab Tab id. * @return array */ public function get_sections( string $tab ): array { $sections = $this->sections[ $tab ] ?? array(); uasort( $sections, static fn ( $a, $b ) => $a['priority'] <=> $b['priority'] ); return $sections; } /** * A section's fields, ordered by priority. * * @param string $tab Tab id. * @param string $section Section id. * @return Field[] */ public function get_fields( string $tab, string $section ): array { $fields = $this->fields[ $tab ][ $section ] ?? array(); uasort( $fields, static fn ( Field $a, Field $b ) => $a->priority <=> $b->priority ); return $fields; } /** * The WordPress option name a tab's values are stored under. * * @param string $tab Tab id. * @return string */ public function option_name( string $tab ): string { return 'aiou_settings_' . $tab; } /** * Default values for every field in a tab, nested by section. * * Nesting by section (rather than one flat field-id => value map for * the whole tab) is deliberate: it's what lets two different sections * of the same tab each safely use a field id like `enabled` without * colliding on the same stored value. * * @param string $tab Tab id. * @return array> */ public function defaults( string $tab ): array { $defaults = array(); foreach ( $this->get_sections( $tab ) as $section_id => $section ) { foreach ( $this->get_fields( $tab, $section_id ) as $field ) { $defaults[ $section_id ][ $field->id ] = $field->default; } } return $defaults; } /** * Saved values for a tab, nested by section, merged over defaults. * * @param string $tab Tab id. * @return array> */ public function get_values( string $tab ): array { $stored = (array) get_option( $this->option_name( $tab ), array() ); $merged = $this->defaults( $tab ); foreach ( $stored as $section_id => $section_values ) { if ( ! is_array( $section_values ) ) { continue; } foreach ( $section_values as $field_id => $value ) { $merged[ $section_id ][ $field_id ] = $value; } } return $merged; } /** * A single field's current value. * * @param string $tab Tab id. * @param string $section Section id. * @param string $field_id Field id. * @param mixed $default Fallback if the tab/section/field isn't registered. * @return mixed */ public function get_value( string $tab, string $section, string $field_id, mixed $default = null ): mixed { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound -- `default` is the clearest name for this concept; consistent with Field::$default. $values = $this->get_values( $tab ); return $values[ $section ][ $field_id ] ?? $default; } /** * Sanitize a raw input array for a tab, section by section, field by * field. Unknown sections/fields in the input are dropped; every known * field is present in the output (using its own sanitizer, or its type * default). * * @param string $tab Tab id. * @param array $input Raw input, nested by section (from $_POST or an import file). * @return array> */ public function sanitize( string $tab, array $input ): array { $output = array(); foreach ( $this->get_sections( $tab ) as $section_id => $section ) { $fields = $this->get_fields( $tab, $section_id ); foreach ( $fields as $field ) { $output[ $section_id ][ $field->id ] = $field->sanitize( $input[ $section_id ][ $field->id ] ?? null ); } // A toggle can't stay on while a setting it depends on is off. foreach ( array_keys( $this->inactive_fields( $tab, $section_id, $output[ $section_id ] ?? array() ) ) as $field_id ) { if ( 'checkbox' === $fields[ $field_id ]->type ) { $output[ $section_id ][ $field_id ] = 0; } } } return $output; } /** * Whether a `depends_on` controller's value counts as "on": a ticked * checkbox, or a select set to anything but an empty or "off" choice. * * @param mixed $value Controller value. * @return bool */ public static function is_on( mixed $value ): bool { if ( is_array( $value ) ) { return (bool) $value; } return ! in_array( (string) $value, array( '', '0', 'off' ), true ); } /** * A section's fields that are switched off because a field they depend * on -- directly, or further up a `depends_on` chain -- is off. * * @param string $tab Tab id. * @param string $section Section id. * @param array $values The section's values. * @return array Keyed by field id. */ public function inactive_fields( string $tab, string $section, array $values ): array { $fields = $this->get_fields( $tab, $section ); $inactive = array(); foreach ( $fields as $field_id => $field ) { $parent = (string) ( $field->args['depends_on'] ?? '' ); $depth = 0; while ( '' !== $parent && isset( $fields[ $parent ] ) && $depth++ < 10 ) { if ( ! self::is_on( $values[ $parent ] ?? $fields[ $parent ]->default ) ) { $inactive[ $field_id ] = true; break; } $parent = (string) ( $fields[ $parent ]->args['depends_on'] ?? '' ); } } return $inactive; } /** * Register every tab with the core Settings API so `options.php` can * persist it. Call on `admin_init`. * * @return void */ public function register_with_wp(): void { foreach ( $this->get_tabs() as $tab_id => $tab ) { register_setting( $this->option_name( $tab_id ), $this->option_name( $tab_id ), array( 'type' => 'array', 'sanitize_callback' => function ( $input ) use ( $tab_id ) { $this->save_mirrors( $tab_id ); return $this->sanitize( $tab_id, (array) $input ); }, 'default' => $this->defaults( $tab_id ), ) ); } } }