diff --git a/newspack-theme/functions.php b/newspack-theme/functions.php index 2f99e84af..2f742e33a 100755 --- a/newspack-theme/functions.php +++ b/newspack-theme/functions.php @@ -179,6 +179,7 @@ function newspack_setup() { $secondary_color_variation = newspack_adjust_brightness( $secondary_color, -40 ); // Editor color palette. + // phpcs:disable Squiz.Commenting.InlineComment.InvalidEndChar -- Let's not be too precious about the brief comments here. add_theme_support( 'editor-color-palette', array( @@ -224,6 +225,7 @@ function newspack_setup() { ), ) ); + // phpcs:enable Squiz.Commenting.InlineComment.InvalidEndChar add_theme_support( 'editor-gradient-presets', @@ -267,7 +269,7 @@ function newspack_setup() { // Add support for responsive embedded content. add_theme_support( 'responsive-embeds' ); - // Make our theme AMP/PWA Native + // Make our theme AMP/PWA Native. add_theme_support( 'amp', array( @@ -278,7 +280,7 @@ function newspack_setup() { ) ); - // Add custom theme support - post subtitle + // Add custom theme support - post subtitle. add_theme_support( 'post-subtitle' ); } endif; @@ -411,7 +413,7 @@ function newspack_widgets_init() { function newspack_content_width() { $content_width = 780; - // Check if front page or using One-Column Wide template + // Check if front page or using One-Column Wide template. if ( ( is_front_page() && 'posts' !== get_option( 'show_on_front' ) ) || is_page_template( 'single-wide.php' ) ) { $content_width = 1200; } @@ -585,6 +587,7 @@ function newspack_enqueue_scripts() { } // Featured Image options. + // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter -- TODO: Should we set $in_footer? wp_register_script( 'newspack-extend-featured-image-script', get_theme_file_uri( '/js/dist/extend-featured-image-editor.js' ), @@ -814,6 +817,8 @@ function newspack_check_current_template() { * * The 'admin-color-' prefix is used to make sure the classes get moved to the tag in the iframed editor as a work-around. * See https://github.com/WordPress/gutenberg/issues/28538 for more details. + * + * @param string $classes Existing classes. */ function newspack_filter_admin_body_class( $classes ) { if ( ! function_exists( 'get_current_screen' ) ) { @@ -974,6 +979,9 @@ function newspack_register_meta() { /** * Migrate theme settings when switching within the family of Newspack themes. * + * @param string $old_name The name of the old theme. + * @param WP_Theme|false $old_theme The old theme object (default: false). + * * @since Newspack Theme 1.0.0 */ function newspack_migrate_settings( $old_name, $old_theme = false ) { @@ -1123,6 +1131,10 @@ function newspack_sanitize_svgs() { /** * Truncates text to a specific character length, without breaking a character. + * + * @param string $content The text to truncate. + * @param int $length The character length to truncate to. + * @param string $after Text to append after truncation. Default is '...'. */ function newspack_truncate_text( $content, $length, $after = '...' ) { // If content is already shorter than the truncate length, return it. @@ -1130,10 +1142,10 @@ function newspack_truncate_text( $content, $length, $after = '...' ) { return $content; } - // Find the first space after the desired length: + // Find the first space after the desired length. $breakpoint = strpos( $content, ' ', $length ); - // Make sure $breakpoint isn't returning false, and is less than length of content: + // Make sure $breakpoint isn't returning false, and is less than length of content. if ( false !== $breakpoint && $breakpoint < strlen( $content ) - 1 ) { $content = substr( $content, 0, $breakpoint ) . $after; } @@ -1200,9 +1212,11 @@ function newspack_theme_newspack_ads_maybe_use_responsive_placement( $responsive /** * Add a extra span and class to the_archive_title, for easier styling. + * + * @param string $title Archive title to be displayed. */ function newspack_update_the_archive_title( $title ) { - // Split the title into parts so we can wrap them with spans: + // Split the title into parts so we can wrap them with spans. $title_parts = explode( '', $title, 2 ); $title_format = get_theme_mod( 'archive_title_format', 'default' ); @@ -1334,6 +1348,13 @@ function newspack_dequeue_mediaelement() { require get_template_directory() . '/inc/newspack-sponsors.php'; } +/** + * Load Tag Labels compatibility file. + */ +if ( class_exists( '\Newspack\Tag_Labels' ) ) { + require get_template_directory() . '/inc/newspack-tag-labels.php'; +} + /** * Load Newsletters compatibility file. */ @@ -1364,4 +1385,3 @@ function newspack_dequeue_mediaelement() { * Woo Templates cache handling */ require get_template_directory() . '/woocommerce/templates.php'; - diff --git a/newspack-theme/inc/jetpack.php b/newspack-theme/inc/jetpack.php index dd2b26d3a..c53d9a988 100644 --- a/newspack-theme/inc/jetpack.php +++ b/newspack-theme/inc/jetpack.php @@ -41,7 +41,7 @@ function newspack_jetpack_setup() { 'post-details' => array( 'stylesheet' => 'newspack-style', 'date' => '.posted-on', - 'categories' => '.cat-links:not(.sponsor-label)', + 'categories' => '.cat-links:not(.sponsor-label):not(.tag-labels)', 'tags' => '.tags-links', 'author' => '.byline:not(.sponsor-byline), .author-avatar', ), diff --git a/newspack-theme/inc/newspack-tag-labels.php b/newspack-theme/inc/newspack-tag-labels.php new file mode 100644 index 000000000..c5b9ecc74 --- /dev/null +++ b/newspack-theme/inc/newspack-tag-labels.php @@ -0,0 +1,178 @@ +get( 'Version' ) + ); + wp_style_add_data( 'newspack-tag-labels-style', 'rtl', 'replace' ); +} +add_action( 'wp_enqueue_scripts', 'newspack_tag_labels_enqueue_styles' ); + +/** + * Enqueue supplemental block editor styles. + */ +function newspack_tag_labels_editor_styles() { + if ( ! class_exists( 'Newspack\Tag_Labels' ) ) { + return; + } + wp_enqueue_style( 'newspack-tag-labels-editor-styles', get_theme_file_uri( '/styles/newspack-tag-labels-editor.css' ), false, wp_get_theme()->get( 'Version' ), 'all' ); + wp_style_add_data( 'newspack-tag-labels-editor-styles', 'rtl', 'replace' ); +} +add_action( 'enqueue_block_editor_assets', 'newspack_tag_labels_editor_styles' ); + + +/** + * Adds section to customizer for Tag Labels options. + * + * @param WP_Customize_Manager $wp_customize Customizer object. + */ +function newspack_tag_labels_customize_register( $wp_customize ) { + if ( ! class_exists( 'Newspack\Tag_Labels' ) ) { + return; + } + + $wp_customize->add_section( + 'newspack_tag_labels', + array( + 'title' => esc_html__( 'Tag Labels', 'newspack-theme' ), + ) + ); + + $wp_customize->add_setting( + 'tag_labels_hex', + array( + 'default' => '#FED850', + 'sanitize_callback' => 'sanitize_hex_color', + ) + ); + + $wp_customize->add_control( + new WP_Customize_Color_Control( + $wp_customize, + 'tag_labels_hex', + array( + 'label' => esc_html__( 'Tag Label', 'newspack-theme' ), + 'description' => esc_html__( 'Changes the background of the tag label that appears on posts and blocks. It should stand out boldly against your site\'s color scheme.', 'newspack-theme' ), + 'section' => 'newspack_tag_labels', + ) + ) + ); +} +add_action( 'customize_register', 'newspack_tag_labels_customize_register' ); + +/** + * Add custom colors to tag labels. + */ +function newspack_tag_labels_styles() { + if ( ! class_exists( 'Newspack\Tag_Labels' ) ) { + return; + } + + $flag_color = get_theme_mod( 'tag_labels_hex', '#FED850' ); + $flag_color_contrast = newspack_get_color_contrast( $flag_color ); + ?> + +
> @@ -25,10 +28,16 @@ if ( ! empty( $native_sponsors ) ) { // Get label for native post sponsors. newspack_sponsor_label( $native_sponsors ); + if ( $tag_labels ) { + newspack_display_tag_labels( $tag_labels ); + } if ( $display_sponsors_and_categories ) { newspack_categories(); } } else { + if ( $tag_labels ) { + newspack_display_tag_labels( $tag_labels ); + } newspack_categories(); } } @@ -37,7 +46,7 @@ ', esc_url( get_permalink() ) ), '' ); ?>
- +
diff --git a/newspack-theme/template-parts/content/content-excerpt.php b/newspack-theme/template-parts/content/content-excerpt.php index ca9c8ffd1..149ca38ee 100755 --- a/newspack-theme/template-parts/content/content-excerpt.php +++ b/newspack-theme/template-parts/content/content-excerpt.php @@ -14,6 +14,9 @@ $display_sponsors_and_categories = newspack_display_sponsors_and_categories( $native_sponsors ); $display_sponsors_and_authors = newspack_display_sponsors_and_authors( $native_sponsors ); } + +// Get tag labels for this post (null when the Newspack plugin isn't loaded). +$tag_labels = function_exists( 'newspack_get_tag_labels' ) ? newspack_get_tag_labels( get_the_ID() ) : null; ?>
> @@ -25,10 +28,16 @@ if ( ! empty( $native_sponsors ) ) { // Get label for native post sponsors. newspack_sponsor_label( $native_sponsors ); + if ( $tag_labels ) { + newspack_display_tag_labels( $tag_labels ); + } if ( $display_sponsors_and_categories ) { newspack_categories(); } } else { + if ( $tag_labels ) { + newspack_display_tag_labels( $tag_labels ); + } newspack_categories(); } } @@ -37,7 +46,7 @@ ', esc_url( get_permalink() ) ), '' ); ?>
- +
diff --git a/newspack-theme/template-parts/header/entry-header.php b/newspack-theme/template-parts/header/entry-header.php index 7972604ad..8fbc08a8b 100755 --- a/newspack-theme/template-parts/header/entry-header.php +++ b/newspack-theme/template-parts/header/entry-header.php @@ -13,6 +13,9 @@ $display_sponsors_and_authors = newspack_display_sponsors_and_authors( $native_sponsors ); } +// Get tag labels for this post (null when the Newspack plugin isn't loaded). +$tag_labels = function_exists( 'newspack_get_tag_labels' ) ? newspack_get_tag_labels( get_the_ID() ) : null; + // Get page title visibility. $page_hide_title = get_post_meta( $post->ID, 'newspack_hide_page_title', true ); @@ -29,10 +32,16 @@ if ( ! is_page() ) : if ( ! empty( $native_sponsors ) ) { newspack_sponsor_label( $native_sponsors, null, true ); + if ( $tag_labels ) { + newspack_display_tag_labels( $tag_labels ); + } if ( $display_sponsors_and_categories ) { newspack_categories(); } } else { + if ( $tag_labels ) { + newspack_display_tag_labels( $tag_labels ); + } newspack_categories(); } endif; diff --git a/scripts/compile-scss.js b/scripts/compile-scss.js index 50b087c53..f98c04f10 100644 --- a/scripts/compile-scss.js +++ b/scripts/compile-scss.js @@ -132,6 +132,16 @@ const SASS_STYLESHEETS = [ outFile: 'newspack-theme/styles/newspack-sponsors-editor.css', withRTL: true, }, + { + inFile: 'newspack-theme/sass/plugins/newspack-tag-labels.scss', + outFile: 'newspack-theme/styles/newspack-tag-labels.css', + withRTL: true, + }, + { + inFile: 'newspack-theme/sass/plugins/newspack-tag-labels-editor.scss', + outFile: 'newspack-theme/styles/newspack-tag-labels-editor.css', + withRTL: true, + }, { inFile: 'newspack-theme/tribe-events/tribe-events.scss', outFile: 'newspack-theme/tribe-events/tribe-events.css',