Skip to content

Commit c38e609

Browse files
committed
Initial commit
This is a first step of a migration of an app shell experimental implementation proposed in ampproject/amp-wp#1519
0 parents  commit c38e609

14 files changed

+18741
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# WordPress Coding Standards
2+
# https://make.wordpress.org/core/handbook/coding-standards/
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
indent_style = tab
12+
13+
[{.babelrc,.eslintrc,.rtlcssrc,*.json,*.yml}]
14+
indent_style = space
15+
indent_size = 2
16+
17+
[{*.md}]
18+
trim_trailing_whitespace = false

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/*.min.js
2+
**/node_modules/**
3+
**/vendor/**

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/vendor
3+
/build
4+
node_modules
5+
assets/js/**/*.js
6+
assets/js/*.asset.php
7+
assets/js/*.map
8+
.idea/

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lts/*

LICENSE

Lines changed: 340 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# AMP App Shell for WordPress
2+
3+
Add support for app shell navigation via AMP Shadow DOM.
4+
5+
This is a WordPress plugin which you can activate to see the app shell navigation
6+
in action. It extends the functionality of the [AMP plugin](https://github.com/ampproject/amp-wp).
7+
8+
Tha app shell navigation based on AMP Shadow DOM is an experimental feature initially
9+
proposed in [ampproject/amp-wp#1519](https://github.com/ampproject/amp-wp/pull/1519).
10+
11+
In order for the plugin to work, you will have to install the
12+
[AMP](https://github.com/ampproject/amp-wp) and [PWA](https://wordpress.org/plugins/pwa/) plugins.
13+
14+
Note that you will also have to run `composer install && npm install && npm run build`
15+
prior to activating the plugin.

amp-app-shell.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
/**
3+
* Plugin Name: AMP App Shell for WordPress
4+
* Description: Support for app shell navigation via AMP Shadow DOM.
5+
* Plugin URI: https://github.com/xwp/amp-app-shell/
6+
* Author: AMP Project Contributors, XWP
7+
* Author URI: https://github.com/xwp/amp-app-shell/graphs/contributors
8+
* Version: 0.1.0
9+
* Text Domain: amp-app-shell
10+
* Domain Path: /languages/
11+
* License: GPLv2 or later
12+
* Requires at least: 4.9
13+
* Requires PHP: 5.6
14+
*
15+
* @package AmpAppShell
16+
*/
17+
18+
define( 'AMP_APP_SHELL__FILE__', __FILE__ );
19+
define( 'AMP_APP_SHELL__DIR__', dirname( __FILE__ ) );
20+
define( 'AMP_APP_SHELL__VERSION', '0.1.0' );
21+
22+
/**
23+
* Errors encountered while loading the plugin.
24+
*
25+
* This has to be a global for the sake of PHP 5.2.
26+
*
27+
* @var WP_Error $_amp_app_shell_load_errors
28+
*/
29+
global $_amp_app_shell_load_errors;
30+
31+
$_amp_app_shell_load_errors = new WP_Error();
32+
33+
if ( version_compare( phpversion(), '5.6', '<' ) ) {
34+
$_amp_app_shell_load_errors->add(
35+
'insufficient_php_version',
36+
sprintf(
37+
/* translators: %s: required PHP version */
38+
__( 'The AMP App Shell plugin requires PHP %s. Please contact your host to update your PHP version.', 'amp-app-shell' ),
39+
'5.6+'
40+
)
41+
);
42+
}
43+
44+
if ( ! file_exists( AMP_APP_SHELL__DIR__ . '/assets/js/amp-wp-app-shell.js' ) ) {
45+
$_amp_app_shell_load_errors->add(
46+
'build_required',
47+
sprintf(
48+
/* translators: %s: composer install && npm install && npm run build */
49+
__( 'You appear to be running the AMP App Shell plugin from source. Please do %s to finish installation.', 'amp-app-shell' ), // phpcs:ignore WordPress.Security.EscapeOutput
50+
'<code>composer install &amp;&amp; npm install &amp;&amp; npm run build</code>'
51+
)
52+
);
53+
}
54+
55+
function _amp_app_shell_check_dependecies() {
56+
global $_amp_app_shell_load_errors;
57+
58+
if ( ! is_plugin_active( 'amp/amp.php' ) ) {
59+
$_amp_app_shell_load_errors->add(
60+
'amp_plugin_required',
61+
sprintf(
62+
/* translators: %s: <a href="https://wordpress.org/plugins/amp/" target="_blank">AMP</a> */
63+
__( 'It seems that %s plugin is not installed. Please install and activate the plugin to finish installation.', 'amp-app-shell' ), // phpcs:ignore WordPress.Security.EscapeOutput
64+
'<a href="https://wordpress.org/plugins/amp/" target="_blank">AMP</a>'
65+
)
66+
);
67+
}
68+
69+
if ( ! is_plugin_active( 'pwa/pwa.php' ) ) {
70+
$_amp_app_shell_load_errors->add(
71+
'amp_plugin_required',
72+
sprintf(
73+
/* translators: %s: <a href="https://wordpress.org/plugins/pwa/" target="_blank">PWA</a> */
74+
__( 'It seems that %s plugin is not installed. Please install and activate the plugin to finish installation.', 'amp-app-shell' ), // phpcs:ignore WordPress.Security.EscapeOutput
75+
'<a href="https://wordpress.org/plugins/pwa/" target="_blank">PWA</a>'
76+
)
77+
);
78+
}
79+
80+
// Abort if dependencies are not satisfied.
81+
if ( ! empty( $_amp_app_shell_load_errors->errors ) ) {
82+
add_action( 'admin_notices', '_amp_app_shell_show_load_errors_admin_notice' );
83+
}
84+
}
85+
86+
add_action( 'admin_init', '_amp_app_shell_check_dependecies' );
87+
88+
/**
89+
* Displays an admin notice about why the plugin is unable to load.
90+
*
91+
* @since 1.1.2
92+
* @global WP_Error $_amp_app_shell_load_errors
93+
*/
94+
function _amp_app_shell_show_load_errors_admin_notice() {
95+
global $_amp_app_shell_load_errors;
96+
?>
97+
<div class="notice notice-error">
98+
<p>
99+
<strong><?php esc_html_e( 'AMP App Shell plugin unable to initialize.', 'amp-app-shell' ); ?></strong>
100+
<ul>
101+
<?php foreach ( array_keys( $_amp_app_shell_load_errors->errors ) as $error_code ) : ?>
102+
<?php foreach ( $_amp_app_shell_load_errors->get_error_messages( $error_code ) as $message ) : ?>
103+
<li>
104+
<?php echo wp_kses_post( $message ); ?>
105+
</li>
106+
<?php endforeach; ?>
107+
<?php endforeach; ?>
108+
</ul>
109+
</p>
110+
</div>
111+
<?php
112+
}
113+
114+
/**
115+
* Print admin notice if plugin installed with incorrect slug (which impacts WordPress's auto-update system).
116+
*
117+
* @since 1.0
118+
*/
119+
function _amp_app_shell_incorrect_plugin_slug_admin_notice() {
120+
$actual_slug = basename( AMP_APP_SHELL__DIR__ );
121+
?>
122+
<div class="notice notice-warning">
123+
<p>
124+
<?php
125+
echo wp_kses_post(
126+
sprintf(
127+
/* translators: %1$s is the current directory name, and %2$s is the required directory name */
128+
__( 'You appear to have installed the AMP App Shell plugin incorrectly. It is currently installed in the <code>%1$s</code> directory, but it needs to be placed in a directory named <code>%2$s</code>. Please rename the directory.', 'amp-app-shell' ),
129+
$actual_slug,
130+
'amp-app-shell'
131+
)
132+
);
133+
?>
134+
</p>
135+
</div>
136+
<?php
137+
}
138+
139+
if ( 'amp-app-shell' !== basename( AMP_APP_SHELL__DIR__ ) ) {
140+
add_action( 'admin_notices', '_amp_app_shell_incorrect_plugin_slug_admin_notice' );
141+
}
142+
143+
require_once AMP_APP_SHELL__DIR__ . '/includes/amp-app-shell-helper-functions.php';
144+
145+
register_activation_hook( __FILE__, 'amp_app_shell_activate' );
146+
147+
amp_app_shell_bootstrap_plugin();

0 commit comments

Comments
 (0)