-
Notifications
You must be signed in to change notification settings - Fork 71
feat: add support for tag labels #2613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jason10lee
wants to merge
21
commits into
trunk
Choose a base branch
from
feat/tag-labels
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+372
−11
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6287b21
feat: add theme support for tags as labels
jason10lee c9835a2
fix: continuity checks for tag labels support
jason10lee 3c78f8b
fix: separate consecutive labels
jason10lee 1dfc474
fix: apply suggestions from Copilot code review
jason10lee 54fcccb
fix: tweaks; Copilot suggestions
jason10lee 96a4038
refactor: move tag labels out to separate line
jason10lee ed492ba
feat: add tag label color to theme customizer
jason10lee fcb3b67
feat: add block and editor styles
jason10lee abbb6a0
fix: apply Copilot suggestions
jason10lee 3f55f2e
fix: skip styles if Tag Labels support not present
jason10lee 6b9b843
fix: enable RTL conversion for tag label CSS
jason10lee de3bed7
refactor: centralized HTML generation function and interface to `news…
jason10lee 1a3df39
style: new lint from old code
jason10lee ef8091f
Merge trunk into feat/tag-labels and resolve conflicts
Copilot ef7dcd6
fix: bump tag-labels flag specificity over wpnbha/wpnbpc
jason10lee 62ec20e
fix: initialize $tag_labels so reads are safe without the Newspack pl…
jason10lee b7fd4b4
fix: exempt tag-labels from archive + Jetpack cat-links suppression
jason10lee 9257154
fix: restore wp_kses_post on $subtitle in entry-header.php
jason10lee ab27387
fix: register RTL variants for tag-labels stylesheets
jason10lee 09da3a8
docs: correct misleading phpcs:ignore rationale on subtitle echoes
jason10lee 2ac4b26
fix: bump tag-labels flag specificity over wpnbha/wpnbpc on frontend
jason10lee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| <?php | ||
| /** | ||
| * Newspack Tag Labels shim | ||
| * | ||
| * @package Newspack | ||
| */ | ||
|
|
||
| if ( ! function_exists( 'newspack_get_tag_labels' ) ) : | ||
| /** | ||
| * Returns array of tag labels for the given post. | ||
| * | ||
| * @param int|WP_Post|null $post Post to check. | ||
| * | ||
| * @return array|null | ||
| */ | ||
| function newspack_get_tag_labels( $post = null ) { | ||
|
|
||
| if ( class_exists( '\Newspack\Tag_Labels' ) && method_exists( '\Newspack\Tag_Labels', 'get_labels_for_post' ) ) { | ||
| return \Newspack\Tag_Labels::get_labels_for_post( $post ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| endif; | ||
|
|
||
|
|
||
| if ( ! function_exists( 'newspack_generate_tag_labels' ) ) : | ||
| /** | ||
| * Generates HTML for given tag labels. | ||
| * | ||
| * @param array $labels Labels to display. | ||
| * @param bool $links Whether to include links to tag archives. | ||
| * @param array $outer_classes Classes to apply to the outer container. | ||
| * @param array $inner_classes Classes to apply to the inner container. | ||
| * | ||
| * @return string Tag labels as HTML. | ||
| */ | ||
| function newspack_generate_tag_labels( $labels = null, $links = true, $outer_classes = array( 'tag-labels' ), $inner_classes = array( 'tag-label', 'flag' ) ) { | ||
| if ( class_exists( '\Newspack\Tag_Labels' ) && method_exists( '\Newspack\Tag_Labels', 'generate_html' ) ) { | ||
| return \Newspack\Tag_Labels::generate_html( $labels, $links, $outer_classes, $inner_classes, 'span' ); | ||
| } | ||
|
|
||
| return ''; | ||
| } | ||
| endif; | ||
|
|
||
| if ( ! function_exists( 'newspack_display_tag_labels' ) ) : | ||
| /** | ||
| * Outputs HTML for given tag labels. | ||
| * | ||
| * @param array $labels Labels to display. | ||
| * @param bool $links Whether to include links to tag archives. | ||
| * | ||
| * @return null | ||
| */ | ||
| function newspack_display_tag_labels( $labels = null, $links = true ) { | ||
| if ( class_exists( '\Newspack\Tag_Labels' ) && method_exists( '\Newspack\Tag_Labels', 'display' ) ) { | ||
| \Newspack\Tag_Labels::display( $labels, $links, 'span' ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| endif; | ||
|
|
||
| /** | ||
| * Enqueue styles. | ||
| */ | ||
| function newspack_tag_labels_enqueue_styles() { | ||
| if ( ( defined( 'WP_CLI' ) && WP_CLI ) || is_admin() || ! class_exists( 'Newspack\Tag_Labels' ) ) { | ||
| return; | ||
| } | ||
| wp_enqueue_style( | ||
| 'newspack-tag-labels-style', | ||
| get_template_directory_uri() . '/styles/newspack-tag-labels.css', | ||
| array( 'newspack-style' ), | ||
| wp_get_theme()->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 ); | ||
| ?> | ||
| <style> | ||
| .tag-labels .flag, | ||
| amp-script .tag-labels .flag, | ||
| .wpnbha .tag-labels a.flag, | ||
| .featured-image-behind .tag-labels a.flag { | ||
| background: <?php echo esc_attr( $flag_color ); ?>; | ||
| color: <?php echo esc_attr( $flag_color_contrast ); ?>; | ||
| } | ||
| </style> | ||
| <?php | ||
| } | ||
| add_action( 'wp_head', 'newspack_tag_labels_styles' ); | ||
|
|
||
| /** | ||
| * Add custom colors for tag labels to editor. | ||
| */ | ||
| function newspack_tag_labels_styles_editor() { | ||
| 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 ); | ||
|
|
||
| $tag_labels_customizations = ' | ||
| .editor-styles-wrapper .tag-labels .flag { | ||
| background: ' . esc_attr( $flag_color ) . '; | ||
| color: ' . esc_attr( $flag_color_contrast ) . '; | ||
| } | ||
| '; | ||
|
|
||
| wp_add_inline_style( 'newspack-tag-labels-editor-styles', $tag_labels_customizations ); | ||
| } | ||
| add_action( 'enqueue_block_editor_assets', 'newspack_tag_labels_styles_editor' ); | ||
|
jason10lee marked this conversation as resolved.
|
||
32 changes: 32 additions & 0 deletions
32
newspack-theme/sass/plugins/newspack-tag-labels-editor.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| @use "../variables-site/variables-site"; | ||
| @use "../mixins/mixins-main"; | ||
| @use "../variables-site/structure"; | ||
|
|
||
| // Newspack Blocks | ||
| .tag-labels { | ||
| // Use `.tag-label.flag` so this beats `.wpnbha .cat-links a` / `.wpnbpc .cat-links a` on specificity. | ||
| .tag-label.flag { | ||
| background: var(--newspack-theme-color-highlight); | ||
| color: var(--newspack-theme-color-text-main); | ||
| line-height: 1; | ||
| padding: 0.3em 0.5em; | ||
|
jason10lee marked this conversation as resolved.
|
||
| text-transform: uppercase; | ||
| } | ||
| } | ||
|
|
||
| .block-editor { | ||
| article { | ||
| .tag-labels, | ||
| .cat-links:has(+ .tag-labels) { | ||
| display: inline-flex; | ||
| } | ||
| .tag-label + .tag-label { | ||
| margin-left: 0.3em; // Don't collapse multiple labels. | ||
| } | ||
| .tag-labels a, | ||
| .tag-labels a:hover { | ||
| padding: 0.3em 0.5em; | ||
| text-decoration: none; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.