Last active
February 21, 2024 01:19
-
-
Save westonruter/0d09371a2b2c991f344fa67b728975c5 to your computer and use it in GitHub Desktop.
Admin PWA proof of concept for https://github.com/GoogleChromeLabs/pwa-wp/issues/295
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
<?php | |
/** | |
* Plugin Name: Admin PWA | |
* Plugin URI: https://github.com/GoogleChromeLabs/pwa-wp/issues/295 | |
* Description: Proof of concept to turn the WordPress admin into a PWA. | |
* Version: 0.1.0 | |
* Author: Weston Ruter | |
* Author URI: https://weston.ruter.net/ | |
* Text Domain: pwa | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* Requires Plugins: pwa | |
* Requires PHP: 5.6 | |
*/ | |
namespace AdminPWA; | |
/** | |
* Gets manifest overrides which are merged on top of the non-admin manifest. | |
* | |
* @return array<string, mixed> Manifest overrides. | |
*/ | |
function get_admin_web_app_manifest_overrides(): array { | |
$manifest = array(); | |
$manifest['start_url'] = admin_url( '/' ); | |
$manifest['scope'] = admin_url( '/' ); | |
$manifest['display'] = 'standalone'; | |
// Add shortcuts for adding new items of each public post type. | |
foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $post_type ) { | |
if ( 'attachment' === $post_type->name || ! current_user_can( $post_type->cap->create_posts ) ) { | |
continue; | |
} | |
$manifest['shortcuts'][] = array( | |
'name' => $post_type->labels->new_item, | |
'url' => add_query_arg( 'post_type', $post_type->name, admin_url( 'post-new.php' ) ), | |
// TODO: Add 'icons'. | |
); | |
} | |
return $manifest; | |
} | |
add_action( | |
'admin_head', | |
static function () { | |
global $wp_web_app_manifest; | |
if ( empty( $wp_web_app_manifest ) || ! method_exists( $wp_web_app_manifest, 'get_manifest' ) ) { | |
// Bail if PWA plugin is not active or the class name changed in core merge. | |
return; | |
} | |
$manifest = array_merge( $wp_web_app_manifest->get_manifest(), get_admin_web_app_manifest_overrides() ); | |
// Props ellatrix for the idea to use a data: URL for the web app manifest. | |
?> | |
<link rel="manifest" href="<?php echo esc_attr( 'data:application/manifest+json;base64,' . base64_encode( wp_json_encode( $manifest ) ) ); ?>"> | |
<?php | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To avoid the use of the data: URL (which can apparently not always be ideal), there is this alternative. Replace the
admin_head
action callback above with: