prefix . 'brap_settings'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, setting_name varchar(50) NOT NULL, setting_value varchar(50) NOT NULL, UNIQUE KEY id (id) ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); // Insert default settings if they don't already exist. $default_settings = array( 'add_year_header' => 'off', 'year_header_level' => 'h2', 'add_month_header' => 'on', 'month_header_level' => 'h3', 'month_format' => 'MMM', 'year_format' => 'YYYY', 'remove_db_table_on_uninstall' => 'no', ); foreach ( $default_settings as $name => $value ) { // Check if exists. $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE setting_name = %s", $name ) ); if ( ! $exists ) { $wpdb->insert( $table_name, array( 'setting_name' => $name, 'setting_value' => $value, ), array( '%s', '%s' ) ); } } } /** * Uninstall Hook: Allows table deletion if user chooses so in plugin settings. */ register_uninstall_hook( __FILE__, 'brap_uninstall' ); function brap_uninstall() { global $wpdb; $table_name = $wpdb->prefix . 'brap_settings'; // Check user’s preference in our table for removing data. $remove_data = $wpdb->get_var( $wpdb->prepare( "SELECT setting_value FROM $table_name WHERE setting_name = %s", 'remove_db_table_on_uninstall' ) ); // If user has chosen 'yes', drop the table. if ( $remove_data === 'yes' ) { $wpdb->query( "DROP TABLE IF EXISTS $table_name" ); } } /** * Helper functions to get and update settings. */ function brap_get_setting( $name ) { global $wpdb; $table_name = $wpdb->prefix . 'brap_settings'; $value = $wpdb->get_var( $wpdb->prepare( "SELECT setting_value FROM $table_name WHERE setting_name = %s", $name ) ); return ( ! empty( $value ) ) ? $value : false; } function brap_update_setting( $name, $value ) { global $wpdb; $table_name = $wpdb->prefix . 'brap_settings'; $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE setting_name = %s", $name ) ); if ( $exists ) { $wpdb->update( $table_name, array( 'setting_value' => $value ), array( 'setting_name' => $name ), array( '%s' ), array( '%s' ) ); } else { $wpdb->insert( $table_name, array( 'setting_name' => $name, 'setting_value' => $value, ), array( '%s', '%s' ) ); } } /** * Generates the month-by-month post listing using your stored settings. * * @return string HTML output of the binge reading archive. */ function brap_display_posts_by_month() { // Fetch user settings from our table. $add_year_header = brap_get_setting( 'add_year_header' ); $year_header_level = brap_get_setting( 'year_header_level' ); $add_month_header = brap_get_setting( 'add_month_header' ); $month_header_level = brap_get_setting( 'month_header_level' ); $month_format = brap_get_setting( 'month_format' ); // 'MM' or 'MMM' $year_format = brap_get_setting( 'year_format' ); // 'YY' or 'YYYY' // Make sure we have fallback defaults. $add_year_header = ( $add_year_header ) ? $add_year_header : 'off'; $year_header_level = ( $year_header_level ) ? $year_header_level : 'h2'; $add_month_header = ( $add_month_header ) ? $add_month_header : 'on'; $month_header_level = ( $month_header_level ) ? $month_header_level : 'h3'; $month_format = ( $month_format ) ? $month_format : 'MMM'; $year_format = ( $year_format ) ? $year_format : 'YYYY'; // Query all published posts, most recent first. $post_items = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1, 'order' => 'DESC', 'orderby' => 'date', ) ); // Track last month/year so we know when to start new sections/headings. $current_month = ''; $current_year = ''; $output = ''; if ( $post_items->have_posts() ) { ob_start(); while ( $post_items->have_posts() ) { $post_items->the_post(); $post_year = get_the_date( 'Y' ); $post_month = get_the_date( 'n' ); // numeric month // Build format based on user setting for the heading text. // e.g., 'Feb 23' or '02 2023', etc. $display_year = get_the_date( ( $year_format === 'YYYY' ? 'Y' : 'y' ) ); $display_month = get_the_date( ( $month_format === 'MM' ? 'm' : 'M' ) ); // 'm' or 'M' // If the year changes, close old section/UL if needed, then possibly print a new year heading. if ( $post_year !== $current_year ) { // Close old UL and section if they exist if ( ! empty( $current_year ) ) { echo ''; } // Print year heading if turned on if ( 'on' === $add_year_header ) { // e.g.
' . esc_html__( 'Settings saved.', 'all-posts-archive-page' ) . '