Skip to content
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

Add web app manifest display setting in Reading settings page #985

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,4 @@ function _pwa_deactivate_plugin() {
$wp_web_app_manifest->init();

require_once PWA_PLUGIN_DIR . '/wp-admin/options-reading-offline-browsing.php';
require_once PWA_PLUGIN_DIR . '/wp-admin/options-web-app-manifest-display.php';
8 changes: 8 additions & 0 deletions tests/test-class-wp-web-app-manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ public function test_get_manifest() {
);
$this->assertEquals( $expected_manifest, $actual_manifest );

// Update display.
update_option( 'web_app_manifest_display', 'standalone' );

$actual_manifest = $this->instance->get_manifest();
$expected_manifest['display'] = 'standalone';

$this->assertEquals( $expected_manifest, $actual_manifest );

// Check that icon purpose is `any maskable` if site icon is maskable.
$actual_manifest = $this->instance->get_manifest();
$this->assertEquals( $expected_manifest, $actual_manifest );
Expand Down
71 changes: 71 additions & 0 deletions wp-admin/options-web-app-manifest-display.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Web App manifest display field for the reading settings administration panel.
*
* As part of a core merge, the code in this file would go inside wp-admin/options-reading.php
*
* @package PWA
*/

namespace PWA_WP;

/**
* Register web app manifest display setting.
*/
function register_web_app_manifest_display_setting() {
register_setting(
'reading',
'web_app_manifest_display',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
)
);
Comment on lines +16 to +23
Copy link
Collaborator

@westonruter westonruter May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make sure this doesn't end up adding web_app_manifest_display as an autoloaded option, since it is only used in the web app manifest request.

I think this may mean adding a call to add_option before this call.

add_option( 'web_app_manifest_display', 'minimal-ui', '', false );

Although there may be a better place for it.

I realize this should also be done for the short_name field.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for the offline_browsing option.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For existing installs that have offline_browsing and short_name options that are autoloaded, they should switch to non-autoloaded.

}
add_action( 'init', __NAMESPACE__ . '\register_web_app_manifest_display_setting' );

/**
* Register web app manifest display setting field.
*/
function add_web_app_manifest_display_setting_field() {
add_settings_field(
'web_app_manifest_display',
__( 'Display', 'pwa' ),
__NAMESPACE__ . '\render_web_app_manifest_display_setting_field',
'reading'
);
}
add_action( 'admin_init', __NAMESPACE__ . '\add_web_app_manifest_display_setting_field' );

/**
* Render web app manifest display setting field.
*/
function render_web_app_manifest_display_setting_field() {
// See: <https://developer.mozilla.org/en-US/docs/Web/Manifest/display#values>.
$allowed_values = array( 'fullscreen', 'standalone', 'minimal-ui', 'browser' );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While probably out of scope of this UI, there is also a display_override property: https://developer.chrome.com/articles/display-override/


?>
<fieldset>
<legend class="screen-reader-text"><span><?php esc_html_e( 'Web App manifest display', 'pwa' ); ?> </span></legend>

<label for="web_app_manifest_display">
<?php esc_html_e( 'Web App manifest display', 'pwa' ); ?>
</label>

<select name="web_app_manifest_display" id="web_app_manifest_display">
<?php foreach ( $allowed_values as $value ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, get_option( 'web_app_manifest_display', 'minimal-ui' ) ); ?>><?php echo esc_html( $value ); ?></option>
<?php endforeach; ?>
</select>

<p class="description">
<?php
echo wp_kses(
__( 'This controls how the web app is displayed when launched from the home screen. See <a href="https://developer.mozilla.org/en-US/docs/Web/Manifest/display" rel="noreferrer noopener" target="_blank">MDN documentation</a> for more information.', 'pwa' ),
array( 'a' => array_fill_keys( array( 'href', 'rel', 'target' ), true ) )
);
?>
</p>
</fieldset>
<?php
}
2 changes: 1 addition & 1 deletion wp-includes/class-wp-web-app-manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function get_manifest() {
$manifest = array(
'name' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES, 'utf-8' ),
'start_url' => home_url( '/' ),
'display' => 'minimal-ui',
'display' => get_option( 'web_app_manifest_display', 'minimal-ui' ),
'dir' => is_rtl() ? 'rtl' : 'ltr',
);

Expand Down