__( 'Lovely Notes', 'lovely-notes' ), 'text_lines' => 10, ); } public static function get() { $value = get_option( self::OPTION, array() ); $defaults = self::defaults(); $value = is_array( $value ) ? $value : array(); $name = isset( $value['name'] ) && is_scalar( $value['name'] ) ? sanitize_text_field( (string) $value['name'] ) : ''; return array( 'name' => '' !== $name ? $name : $defaults['name'], 'text_lines' => self::normalize_lines( $value['text_lines'] ?? $defaults['text_lines'] ), ); } public static function get_name() { return self::get()['name']; } /** A height expressed in ordinary lines, including paragraph spacing. */ public static function normalize_lines( $value ) { return is_scalar( $value ) && is_numeric( $value ) ? max( 1, min( 100, (int) $value ) ) : 10; } public function register_settings() { register_setting( 'lovely_notes', self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize' ), 'default' => self::defaults(), ) ); add_settings_section( 'lovely_notes_reading', __( 'Votre microblog', 'lovely-notes' ), '__return_false', self::PAGE ); add_settings_field( 'lovely_notes_name', __( 'Nom du microblog', 'lovely-notes' ), array( $this, 'name_field' ), self::PAGE, 'lovely_notes_reading', array( 'label_for' => 'lovely_notes_name' ) ); add_settings_field( 'lovely_notes_text_lines', __( 'Nombre de lignes visibles', 'lovely-notes' ), array( $this, 'lines_field' ), self::PAGE, 'lovely_notes_reading', array( 'label_for' => 'lovely_notes_text_lines' ) ); } public function register_page() { add_submenu_page( 'edit.php?post_type=lovely_note', __( 'Réglages de Lovely Notes', 'lovely-notes' ), __( 'Réglages', 'lovely-notes' ), 'manage_options', self::PAGE, array( $this, 'render_page' ) ); } public function sanitize( $value ) { $value = is_array( $value ) ? $value : array(); $defaults = self::defaults(); $name = isset( $value['name'] ) && is_scalar( $value['name'] ) ? sanitize_text_field( (string) $value['name'] ) : ''; $name = function_exists( 'mb_substr' ) ? mb_substr( $name, 0, 100 ) : substr( $name, 0, 100 ); return array( 'name' => '' !== $name ? $name : $defaults['name'], 'text_lines' => self::normalize_lines( $value['text_lines'] ?? $defaults['text_lines'] ), ); } public function name_field() { ?>
' . esc_html__( 'Réglages', 'lovely-notes' ) . '' ); return $links; } public function settings_added( $option, $value ) { $this->settings_updated( self::defaults(), $value ); } /** Keep an automatically named archive in sync without overwriting a manual title. */ public function settings_updated( $old, $new ) { $defaults = self::defaults(); $old_name = is_array( $old ) && ! empty( $old['name'] ) ? (string) $old['name'] : $defaults['name']; $new_name = is_array( $new ) && ! empty( $new['name'] ) ? (string) $new['name'] : $defaults['name']; $page_id = absint( get_option( Lovely_Notes_Archive::OPTION_PAGE_ID ) ); $page = $page_id ? get_post( $page_id ) : null; $old_title = $defaults['name'] === $old_name ? __( 'Toutes les notes', 'lovely-notes' ) : $old_name; if ( $page instanceof WP_Post && 'page' === $page->post_type && has_shortcode( $page->post_content, 'lovely_notes_archive' ) && $page->post_title === $old_title && $old_name !== $new_name ) { wp_update_post( array( 'ID' => $page_id, 'post_title' => $defaults['name'] === $new_name ? __( 'Toutes les notes', 'lovely-notes' ) : $new_name ) ); } // The settings affect every public reader; this hook is a no-op without LiteSpeed. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Documented LiteSpeed API hook, not a plugin-defined hook. do_action( 'litespeed_purge_all' ); } }