-
Notifications
You must be signed in to change notification settings - Fork 100
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
Automatically copy name to short_name in web app manifest if not greater than 12 characters #212
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
606fc6c
Automatically copy name to short_name in web app manifest if <= 12 chars
westonruter ab28700
Add site health check for short_name in web app manifest
westonruter 5d78752
Update since tags
westonruter 0d41abd
Set dist to trusty
westonruter 7b40cb5
Use placeholders in translation strings
westonruter 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 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 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 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 |
---|---|---|
|
@@ -33,6 +33,16 @@ class WP_Web_App_Manifest { | |
*/ | ||
const REST_ROUTE = '/web-app-manifest'; | ||
|
||
/** | ||
* Maximum length for short_name. | ||
* | ||
* @since 0.4 | ||
* @link https://developers.google.com/web/tools/lighthouse/audits/manifest-contains-short_name | ||
* @link https://developer.chrome.com/apps/manifest/name#short_name | ||
* @var int | ||
*/ | ||
const SHORT_NAME_MAX_LENGTH = 12; | ||
|
||
/** | ||
* The default manifest icon sizes. | ||
* | ||
|
@@ -51,6 +61,7 @@ class WP_Web_App_Manifest { | |
public function init() { | ||
add_action( 'wp_head', array( $this, 'manifest_link_and_meta' ) ); | ||
add_action( 'rest_api_init', array( $this, 'register_manifest_rest_route' ) ); | ||
add_filter( 'site_status_tests', array( $this, 'add_short_name_site_status_test' ) ); | ||
} | ||
|
||
/** | ||
|
@@ -123,6 +134,19 @@ public function get_manifest() { | |
'display' => 'minimal-ui', | ||
'dir' => is_rtl() ? 'rtl' : 'ltr', | ||
); | ||
|
||
/* | ||
* If the name is 12 characters or less, use it as the short_name. Lighthouse complains when the short_name | ||
* is absent, even when the name is 12 characters or less. Chrome's max recommended short_name length is 12 | ||
* characters. | ||
* | ||
* https://developers.google.com/web/tools/lighthouse/audits/manifest-contains-short_name | ||
* https://developer.chrome.com/apps/manifest/name#short_name | ||
*/ | ||
if ( strlen( $manifest['name'] ) <= self::SHORT_NAME_MAX_LENGTH ) { | ||
$manifest['short_name'] = $manifest['name']; | ||
} | ||
|
||
$language = get_bloginfo( 'language' ); | ||
if ( $language ) { | ||
$manifest['lang'] = $language; | ||
|
@@ -155,6 +179,87 @@ public function get_manifest() { | |
return apply_filters( 'web_app_manifest', $manifest ); | ||
} | ||
|
||
/** | ||
* Register test for lacking short_name in web app manifest. | ||
* | ||
* @since 0.4 | ||
* | ||
* @param array $tests Tests. | ||
* @return array Tests. | ||
*/ | ||
public function add_short_name_site_status_test( $tests ) { | ||
$tests['direct']['web_app_manifest_short_name'] = array( | ||
'label' => __( 'Short Name in Web App Manifest', 'pwa' ), | ||
'test' => array( $this, 'test_short_name_present_in_manifest' ), | ||
); | ||
return $tests; | ||
} | ||
|
||
/** | ||
* Test that web app manifest contains a short_name. | ||
* | ||
* @since 0.4 | ||
* @todo Add test for PNG site icon. | ||
* | ||
* @return array Test results. | ||
*/ | ||
public function test_short_name_present_in_manifest() { | ||
$manifest = $this->get_manifest(); | ||
|
||
/* translators: %d is the max length as a number */ | ||
$description = sprintf( __( 'The <code>short_name</code> is a short version of your website’s name which is displayed when there is not enough space for the full name, for example with the site icon on a phone’s homescreen. It should be a maximum of %d characters long.', 'pwa' ), self::SHORT_NAME_MAX_LENGTH ); | ||
|
||
$actions = __( 'You currently may use <code>web_app_manifest</code> filter to set the short name, for example in your theme’s <code>functions.php</code>.', 'pwa' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, I'd use placeholders for the filter and file name here. |
||
|
||
if ( empty( $manifest['short_name'] ) ) { | ||
$result = array( | ||
'label' => __( 'Web App Manifest lacks a short_name entry', 'pwa' ), | ||
'status' => 'recommended', | ||
'badge' => array( | ||
'label' => __( 'Progressive Web App', 'pwa' ), | ||
'color' => 'orange', | ||
), | ||
'description' => wp_kses_post( sprintf( '<p>%s</p>', $description ) ), | ||
'actions' => wp_kses_post( $actions ), | ||
); | ||
} elseif ( strlen( $manifest['short_name'] ) > self::SHORT_NAME_MAX_LENGTH ) { | ||
$result = array( | ||
'label' => | ||
sprintf( | ||
/* translators: %1$s is the short name */ | ||
__( 'Web App Manifest has a short_name (%s) that is too long', 'pwa' ), | ||
esc_html( $manifest['short_name'] ) | ||
), | ||
'status' => 'recommended', | ||
'badge' => array( | ||
'label' => __( 'Progressive Web App', 'pwa' ), | ||
'color' => 'orange', | ||
), | ||
'description' => wp_kses_post( sprintf( '<p>%s</p>', $description ) ), | ||
'actions' => wp_kses_post( $actions ), | ||
); | ||
} else { | ||
$result = array( | ||
'label' => | ||
sprintf( | ||
/* translators: %1$s is the short name */ | ||
__( 'Web App Manifest has a short_name (%s)', 'pwa' ), | ||
esc_html( $manifest['short_name'] ) | ||
), | ||
'status' => 'good', | ||
'badge' => array( | ||
'label' => __( 'Progressive Web App', 'pwa' ), | ||
'color' => 'green', | ||
), | ||
'description' => wp_kses_post( sprintf( '<p>%s</p>', $description ) ), | ||
); | ||
} | ||
|
||
$result['test'] = 'web_app_manifest_short_name'; | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Registers the rest route to get the manifest. | ||
*/ | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use a placeholder for
short_name
. Also, the first sentence is very long. I'd split it up to make translators' lives easier.