From afdeac9b4a3d9bb9f43605b3c8f4790df100ccc7 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Thu, 3 Oct 2024 23:41:53 +0200 Subject: [PATCH] Add option to fix line breaks --- DEVELOPMENT.md | 2 +- includes/functions/_setup-wordpress.php | 32 +++++++++++++++++++ .../functions/settings/_register_settings.php | 7 ++++ .../settings/_settings_page_general.php | 11 +++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 1e1b9060..bcf8f421 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -372,7 +372,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook | `show_admin_bar` | `__return_false` | `strip_shortcodes_tagnames` | `fictioneer_exempt_shortcodes_from_removal` | `style_loader_tag` | `fictioneer_add_font_awesome_integrity` -| `the_content` | `fictioneer_embed_consent_wrappers`, `fictioneer_add_lightbox_to_post_images`, `fictioneer_add_chapter_paragraph_id`, `fictioneer_replace_br_with_whitespace` +| `the_content` | `fictioneer_embed_consent_wrappers`, `fictioneer_add_lightbox_to_post_images`, `fictioneer_add_chapter_paragraph_id`, `fictioneer_replace_br_with_whitespace`, `fictioneer_fix_line_breaks` | `the_password_form` | `fictioneer_password_form`, `fictioneer_unlock_with_patreon` | `the_content_more_link` | `fictioneer_wrap_read_more_link` | `the_excerpt_rss` | `fictioneer_filter_rss_excerpt` diff --git a/includes/functions/_setup-wordpress.php b/includes/functions/_setup-wordpress.php index e05ee2bd..f6fdf728 100644 --- a/includes/functions/_setup-wordpress.php +++ b/includes/functions/_setup-wordpress.php @@ -647,6 +647,38 @@ function fictioneer_add_chapter_paragraph_id( $content ) { } add_filter( 'the_content', 'fictioneer_add_chapter_paragraph_id', 10, 1 ); +/** + * Fixes line breaks before paragraphs are added + * + * @since 5.25.0 + * + * @param string $content The content. + * + * @return string The modified content. + */ + +function fictioneer_fix_line_breaks( $content ) { + // Return early if... + if ( + get_post_type() !== 'fcn_chapter' || + ! in_the_loop() || + ! is_main_query() || + post_password_required() + ) { + return $content; + } + + // Replace single new lines + $content = str_replace( ["\r\n", "\r"], "\n", $content ); + $content = preg_replace( '/([^\n])\n([^\n])/', "$1\n\n$2", $content ); + + return $content; +} + +if ( get_option( 'fictioneer_enable_line_break_fix' ) ) { + add_filter( 'the_content', 'fictioneer_fix_line_breaks', 1, 1 ); +} + // ============================================================================= // ADD LIGHTBOX TO POST IMAGES // ============================================================================= diff --git a/includes/functions/settings/_register_settings.php b/includes/functions/settings/_register_settings.php index f229adaf..0753eb8a 100644 --- a/includes/functions/settings/_register_settings.php +++ b/includes/functions/settings/_register_settings.php @@ -713,6 +713,12 @@ define( 'FICTIONEER_OPTIONS', array( 'group' => 'fictioneer-settings-general-group', 'sanitize_callback' => 'fictioneer_sanitize_checkbox', 'default' => 0 + ), + 'fictioneer_enable_line_break_fix' => array( + 'name' => 'fictioneer_enable_line_break_fix', + 'group' => 'fictioneer-settings-general-group', + 'sanitize_callback' => 'fictioneer_sanitize_checkbox', + 'default' => 0 ) ), 'integers' => array( @@ -1183,6 +1189,7 @@ function fictioneer_get_option_label( $option ) { 'fictioneer_enable_global_splide' => __( 'Enable Splide slider globally', 'fictioneer' ), 'fictioneer_log_posts' => __( 'Log all post updates', 'fictioneer' ), 'fictioneer_disable_menu_transients' => __( 'Disable menu Transient caching', 'fictioneer' ), + 'fictioneer_enable_line_break_fix' => __( 'Enable fixing of chapter paragraphs', 'fictioneer' ), ); } diff --git a/includes/functions/settings/_settings_page_general.php b/includes/functions/settings/_settings_page_general.php index 9e866a7a..9e734a3b 100644 --- a/includes/functions/settings/_settings_page_general.php +++ b/includes/functions/settings/_settings_page_general.php @@ -222,6 +222,17 @@ $images = get_template_directory_uri() . '/img/documentation/'; ?> +