*/ public function defaults(): array { return [ 'enabled' => true, 'auto_insert' => true, 'post_types' => [ 'post' ], 'min_headings' => 3, 'max_level' => 6, // Niveau HTML max inclus (h2..h6). Défaut 6 = h2..h6. 'title_text' => 'Contents', 'preserve_data_on_uninstall' => true, ]; } /** * @return array */ public function get(): array { $raw = get_option( self::OPTION, [] ); $raw = is_array( $raw ) ? $raw : []; $merged = array_merge( $this->defaults(), $raw ); // Garde-fous de type (l'option peut avoir été altérée manuellement). $merged['post_types'] = is_array( $merged['post_types'] ) ? array_values( array_filter( array_map( 'strval', $merged['post_types'] ) ) ) : [ 'post' ]; if ( empty( $merged['post_types'] ) ) { $merged['post_types'] = [ 'post' ]; } $merged['min_headings'] = max( 1, min( 10, (int) $merged['min_headings'] ) ); $merged['max_level'] = max( 2, min( 6, (int) $merged['max_level'] ) ); return $merged; } /** * @param string $key Clé. * @param mixed $default Valeur par défaut. * @return mixed */ public function value( string $key, $default = null ) { $all = $this->get(); return array_key_exists( $key, $all ) ? $all[ $key ] : $default; } /** * @param array $input Données brutes. * @return array */ public function save( array $input ): array { // Types de contenu : liste de slugs valides. $post_types = []; if ( isset( $input['post_types'] ) && is_array( $input['post_types'] ) ) { foreach ( $input['post_types'] as $pt ) { $pt = sanitize_key( (string) $pt ); if ( '' !== $pt ) { $post_types[] = $pt; } } } $post_types = array_values( array_unique( $post_types ) ); if ( empty( $post_types ) ) { $post_types = [ 'post' ]; } // Niveau max : reçu comme profondeur (2..5 = nombre de niveaux depuis h2) // et converti en niveau HTML max inclus (h2..hN), borné 2..6. $max_level = isset( $input['max_level'] ) ? (int) $input['max_level'] : 4; $max_level = 1 + max( 1, min( 5, $max_level ) ); // profondeur 1..5 -> niveau 2..6 $max_level = max( 2, min( 6, $max_level ) ); $title_text = isset( $input['title_text'] ) ? sanitize_text_field( (string) $input['title_text'] ) : ''; if ( '' === trim( $title_text ) ) { $title_text = __( 'Contents', 'aiforya-table-of-contents' ); } $clean = [ 'enabled' => ! empty( $input['enabled'] ), 'auto_insert' => ! empty( $input['auto_insert'] ), 'post_types' => $post_types, 'min_headings' => isset( $input['min_headings'] ) ? max( 1, min( 10, (int) $input['min_headings'] ) ) : 3, 'max_level' => $max_level, 'title_text' => $title_text, 'preserve_data_on_uninstall' => ! empty( $input['preserve_data_on_uninstall'] ), ]; update_option( self::OPTION, $clean ); return $clean; } /** * Profondeur (nombre de niveaux depuis h2) dérivée du niveau HTML max. * Ex. max_level 4 (h4) -> profondeur 3 (h2, h3, h4). * * @return int */ public function depth_from_max_level(): int { return max( 1, min( 5, (int) $this->value( 'max_level', 6 ) - 1 ) ); } /** * L'extension est-elle prête à générer un sommaire ? * * @return bool */ public function is_ready(): bool { $s = $this->get(); return ! empty( $s['enabled'] ) && ! empty( $s['post_types'] ); } } endif;