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), UNIQUE KEY setting_name (setting_name) ) $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 options: MM (01), MMM (Aug), M (8), MMMM (August). 'month_format' => 'MMM', // Year format options: YY (23), YYYY (2023). 'year_format' => 'YYYY', 'remove_db_table_on_uninstall' => 'no', // Default category slug; empty means "all categories". 'category_filter' => '', // Show/hide post dates in list. 'show_post_date' => 'on', ); foreach ( $default_settings as $name => $value ) { $exists = (int) $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 ( 'yes' === $remove_data ) { // dbDelta() cannot drop; direct query required. $wpdb->query( "DROP TABLE IF EXISTS $table_name" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared } } /** * Helper: get setting from custom table. * * @param string $name Setting name. * @return string|false */ 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 ( '' !== $value && null !== $value ) ? $value : false; } /** * Helper: update/insert setting in custom table. * * @param string $name Setting name. * @param string $value Setting value. * @return void */ function brap_update_setting( $name, $value ) { global $wpdb; $table_name = $wpdb->prefix . 'brap_settings'; $exists = (int) $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' ) ); } } /** * Determine the effective category slug to filter by. * * Priority: shortcode attribute > saved setting. Returns '' if none/invalid. * * @param string $shortcode_slug Category slug from shortcode attribute (unsanitized). * @return string Valid slug or empty string. */ function brap_get_effective_category_slug( $shortcode_slug = '' ) { $slug = ''; if ( is_string( $shortcode_slug ) && '' !== $shortcode_slug ) { $slug = sanitize_title( $shortcode_slug ); } else { $setting_slug = brap_get_setting( 'category_filter' ); if ( is_string( $setting_slug ) && '' !== $setting_slug ) { $slug = sanitize_title( $setting_slug ); } } // Validate slug maps to an existing category term. if ( '' !== $slug ) { $term = get_term_by( 'slug', $slug, 'category' ); if ( ! $term || is_wp_error( $term ) ) { return ''; } } return $slug; } /** * Map stored month/year formats to PHP date() tokens used by WP. * * @param string $month_format One of MM, MMM, M, MMMM. * @param string $year_format One of YY, YYYY. * @return array { 'month' => 'm|M|n|F', 'year' => 'y|Y' } */ function brap_get_date_tokens( $month_format, $year_format ) { $allowed_month = array( 'MM', 'MMM', 'M', 'MMMM' ); $allowed_year = array( 'YY', 'YYYY' ); if ( ! in_array( $month_format, $allowed_month, true ) ) { $month_format = 'MMM'; } if ( ! in_array( $year_format, $allowed_year, true ) ) { $year_format = 'YYYY'; } $month_token = 'M'; // default 'Aug'. switch ( $month_format ) { case 'MM': $month_token = 'm'; // 01-12. break; case 'M': $month_token = 'n'; // 1-12 no leading zero. break; case 'MMMM': $month_token = 'F'; // Full month name. break; case 'MMM': default: $month_token = 'M'; // Short month (Aug). break; } $year_token = ( 'YYYY' === $year_format ) ? 'Y' : 'y'; return array( 'month' => $month_token, 'year' => $year_token, ); } /** * Generates the month-by-month post listing using stored settings and optional category filter. * * Shortcode: [binge_archive category="news"] * * @param array $atts Shortcode attributes. * @return string HTML output of the binge reading archive. */ function brap_display_posts_by_month( $atts = array() ) { // Shortcode attributes (category is a slug). $atts = shortcode_atts( array( 'category' => '', ), $atts, 'binge_archive' ); // 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', 'MMM', 'M', 'MMMM'. $year_format = brap_get_setting( 'year_format' ); // 'YY' or 'YYYY'. $show_post_date = brap_get_setting( 'show_post_date' ); // 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'; $show_post_date = $show_post_date ? $show_post_date : 'on'; // Validate select fields against allowed lists. $allowed_heading_levels = array( 'h1','h2','h3','h4','h5','h6' ); if ( ! in_array( $year_header_level, $allowed_heading_levels, true ) ) { $year_header_level = 'h2'; } if ( ! in_array( $month_header_level, $allowed_heading_levels, true ) ) { $month_header_level = 'h3'; } // Convert to date() tokens used by WP's get_the_date(). $tokens = brap_get_date_tokens( $month_format, $year_format ); // Determine effective category filter (slug) with validation. $requested_slug = ''; if ( isset( $atts['category'] ) ) { $requested_slug = is_string( $atts['category'] ) ? $atts['category'] : ''; } $category_slug = brap_get_effective_category_slug( $requested_slug ); // Build WP_Query args. $query_args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'order' => 'DESC', 'orderby' => 'date', 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'update_post_term_cache' => false, ); if ( '' !== $category_slug ) { // Filter by category slug. $query_args['category_name'] = $category_slug; } $post_items = new WP_Query( $query_args ); // 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' ); // numeric 4-digit for grouping. $post_month = get_the_date( 'n' ); // 1-12 for grouping. $display_year = get_the_date( $tokens['year'] ); // 'y' or 'Y'. $display_month = get_the_date( $tokens['month'] ); // 'm', 'M', 'n', or 'F'. // If the year changes, close old section/UL if needed, then possibly print a new year heading. if ( $post_year !== $current_year ) { if ( '' !== $current_year ) { echo ''; // Close previous month section. } if ( 'on' === $add_year_header ) { echo '
'; echo '<' . esc_html( $year_header_level ) . '>' . esc_html( $display_year ) . ''; echo '
'; } $current_year = $post_year; $current_month = ''; } // If the month changes, close old UL if needed and print a new month heading if turned on. if ( $post_month !== $current_month ) { if ( '' !== $current_month ) { echo ''; } echo '
'; if ( 'on' === $add_month_header ) { /* translators: 1: month, 2: year */ $month_year = sprintf( __( '%1$s %2$s', 'all-posts-archive-page' ), $display_month, $display_year ); echo '<' . esc_html( $month_header_level ) . '>' . esc_html( $month_year ) . ''; } echo '
'; wp_reset_postdata(); $output = ob_get_clean(); } else { // Helpful message when no posts (especially when filtering by category). $output = ( '' !== $category_slug ) ? '

' . esc_html__( 'No posts found in the selected category.', 'all-posts-archive-page' ) . '

' : '

' . esc_html__( 'No posts found.', 'all-posts-archive-page' ) . '

'; } return $output; } /** * Registers a shortcode [binge_archive] that displays the archive. */ function brap_init_shortcodes() { add_shortcode( 'binge_archive', 'brap_display_posts_by_month' ); } add_action( 'init', 'brap_init_shortcodes' ); /** * Adds a settings page under "Settings" with our options. */ add_action( 'admin_menu', 'brap_plugin_menu' ); function brap_plugin_menu() { add_options_page( __( 'Binge Reading Archive Settings', 'all-posts-archive-page' ), __( 'Binge Reading Archive', 'all-posts-archive-page' ), 'manage_options', 'all-posts-archive-page', 'brap_admin_page' ); } /** * Admin settings page. */ function brap_admin_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'all-posts-archive-page' ) ); } // Process form submission with nonce. if ( isset( $_POST['brap_save_settings'] ) && isset( $_POST['brap_save_settings_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['brap_save_settings_nonce'] ) ), 'brap_save_settings_action' ) ) { $add_year_header = isset( $_POST['add_year_header'] ) ? 'on' : 'off'; $add_month_header = isset( $_POST['add_month_header'] ) ? 'on' : 'off'; $show_post_date = isset( $_POST['show_post_date'] ) ? 'on' : 'off'; $remove_on_uninstall = isset( $_POST['remove_db_table_on_uninstall'] ) ? 'yes' : 'no'; $year_header_level = isset( $_POST['year_header_level'] ) ? sanitize_text_field( wp_unslash( $_POST['year_header_level'] ) ) : 'h2'; $month_header_level = isset( $_POST['month_header_level'] ) ? sanitize_text_field( wp_unslash( $_POST['month_header_level'] ) ) : 'h3'; $month_format = isset( $_POST['month_format'] ) ? sanitize_text_field( wp_unslash( $_POST['month_format'] ) ) : 'MMM'; $year_format = isset( $_POST['year_format'] ) ? sanitize_text_field( wp_unslash( $_POST['year_format'] ) ) : 'YYYY'; $allowed_heading_levels = array( 'h1','h2','h3','h4','h5','h6' ); if ( ! in_array( $year_header_level, $allowed_heading_levels, true ) ) { $year_header_level = 'h2'; } if ( ! in_array( $month_header_level, $allowed_heading_levels, true ) ) { $month_header_level = 'h3'; } $allowed_month_formats = array( 'MM', 'MMM', 'M', 'MMMM' ); if ( ! in_array( $month_format, $allowed_month_formats, true ) ) { $month_format = 'MMM'; } $allowed_year_formats = array( 'YY', 'YYYY' ); if ( ! in_array( $year_format, $allowed_year_formats, true ) ) { $year_format = 'YYYY'; } // Category filter from a dropdown of categories (slug). $category_filter_slug = ''; if ( isset( $_POST['category_filter'] ) ) { $category_filter_slug = sanitize_title( wp_unslash( $_POST['category_filter'] ) ); if ( '' !== $category_filter_slug ) { $term = get_term_by( 'slug', $category_filter_slug, 'category' ); if ( ! $term || is_wp_error( $term ) ) { $category_filter_slug = ''; } } } brap_update_setting( 'add_year_header', $add_year_header ); brap_update_setting( 'year_header_level', $year_header_level ); brap_update_setting( 'add_month_header', $add_month_header ); brap_update_setting( 'month_header_level', $month_header_level ); brap_update_setting( 'month_format', $month_format ); brap_update_setting( 'year_format', $year_format ); brap_update_setting( 'show_post_date', $show_post_date ); brap_update_setting( 'remove_db_table_on_uninstall', $remove_on_uninstall ); brap_update_setting( 'category_filter', $category_filter_slug ); echo '

' . esc_html__( 'Settings saved.', 'all-posts-archive-page' ) . '

'; } // Fetch current settings. $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' ); $year_format = brap_get_setting( 'year_format' ); $show_post_date = brap_get_setting( 'show_post_date' ); $remove_data = brap_get_setting( 'remove_db_table_on_uninstall' ); $category_filter_value = brap_get_setting( 'category_filter' ); // Categories list for dropdown. $categories = get_categories( array( 'hide_empty' => false, ) ); ?>







[binge_archive]
[binge_archive category="news"]