Skip to content

Commit

Permalink
Prevent amp-state for nav menu toggle being inserted outside of body.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz authored and swissspidy committed May 15, 2019
1 parent 39de1da commit 96ccc1e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 84 deletions.
171 changes: 89 additions & 82 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Plugin URI: https://amp-wp.org
* Author: AMP Project Contributors
* Author URI: https://github.com/ampproject/amp-wp/graphs/contributors
* Version: 1.1.1
* Version: 1.2-alpha
* Text Domain: amp
* Domain Path: /languages/
* License: GPLv2 or later
Expand All @@ -14,108 +14,118 @@
*/

/**
* Print admin notice regarding having an old version of PHP.
* Errors encountered while loading the plugin.
*
* @since 0.7
* This has to be a global for the same of PHP 5.2.
*
* @var \WP_Error $_amp_load_errors
*/
function _amp_print_php_version_admin_notice() {
?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: %s: required PHP version */
esc_html__( 'The AMP plugin requires PHP %s. Please contact your host to update your PHP version.', 'amp' ),
'5.4+'
);
?>
</p>
</div>
<?php
}
global $_amp_load_errors;

$_amp_load_errors = new \WP_Error();

if ( version_compare( phpversion(), '5.4', '<' ) ) {
add_action( 'admin_notices', '_amp_print_php_version_admin_notice' );
return;
$_amp_load_errors->add(
'insufficient_php_version',
sprintf(
/* translators: %s: required PHP version */
__( 'The AMP plugin requires PHP %s. Please contact your host to update your PHP version.', 'amp' ),
'5.4+'
)
);
}

/**
* Print admin notice regarding DOM extension is not installed.
*
* @since 1.0
*/
function _amp_print_php_dom_document_notice() {
?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: %s: PHP extension name */
esc_html__( 'The AMP plugin requires the %s extension in PHP. Please contact your host to install this extension.', 'amp' ),
'DOM'
);
?>
</p>
</div>
<?php
}
if ( ! class_exists( 'DOMDocument' ) ) {
add_action( 'admin_notices', '_amp_print_php_dom_document_notice' );
return;
// See composer.json for this list.
$_amp_required_extensions = array(
'curl',
'date',
'dom',
'iconv',
'libxml',
'spl',
);
$_amp_missing_extensions = array();
foreach ( $_amp_required_extensions as $_amp_required_extension ) {
if ( ! extension_loaded( $_amp_required_extension ) ) {
$_amp_missing_extensions[] = $_amp_required_extension;
}
}
if ( count( $_amp_missing_extensions ) > 0 ) {
foreach ( $_amp_missing_extensions as &$_amp_missing_extension ) {
$_amp_missing_extension = "<code>$_amp_missing_extension</code>"; // Not using array_map() w/ closure due to PHP 5.2 support.
}

/**
* Print admin notice regarding DOM extension is not installed.
*
* @since 1.0.1
*/
function _amp_print_php_missing_iconv_notice() {
?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: %s: PHP extension name */
esc_html__( 'The AMP plugin requires the %s extension in PHP. Please contact your host to install this extension.', 'amp' ),
'iconv'
);
?>
</p>
</div>
<?php
$_amp_load_errors->add(
'missing_extension',
sprintf(
/* translators: %s is list of missing extensions */
_n(
'The following PHP extension is missing: %s. Please contact your host to finish installation.',
'The following PHP extensions are missing: %s. Please contact your host to finish installation.',
count( $_amp_missing_extensions ),
'amp'
),
implode( ', ', $_amp_missing_extensions )
)
);
}
if ( ! function_exists( 'iconv' ) ) {
add_action( 'admin_notices', '_amp_print_php_missing_iconv_notice' );
return;
unset( $_amp_required_extensions, $_amp_missing_extensions, $_amp_required_extension, $_amp_missing_extension );

if ( ! file_exists( __DIR__ . '/vendor/autoload.php' ) || ! file_exists( __DIR__ . '/vendor/sabberworm/php-css-parser' ) || ! file_exists( __DIR__ . '/assets/js/amp-block-editor-toggle-compiled.js' ) ) {
$_amp_load_errors->add(
'build_required',
sprintf(
/* translators: %s: composer install && npm install && npm run build */
__( 'You appear to be running the AMP plugin from source. Please do %s to finish installation.', 'amp' ), // phpcs:ignore WordPress.Security.EscapeOutput
'<code>composer install && npm install && npm run build</code>'
)
);
}

/**
* Print admin notice when composer install has not been performed.
* Displays an admin notice about why the plugin is unable to load.
*
* @since 1.0
* @since 1.1.2
* @global \WP_Error $_amp_load_errors
*/
function _amp_print_build_needed_notice() {
function _amp_show_load_errors_admin_notice() {
global $_amp_load_errors;
?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: %s: composer install && npm install && npm run build */
__( 'You appear to be running the AMP plugin from source. Please do %s to finish installation.', 'amp' ), // phpcs:ignore WordPress.Security.EscapeOutput
'<code>composer install && npm install && npm run build</code>'
);
?>
<strong><?php esc_html_e( 'AMP plugin unable to initialize.', 'amp' ); ?></strong>
<ul>
<?php foreach ( array_keys( $_amp_load_errors->errors ) as $error_code ) : ?>
<?php foreach ( $_amp_load_errors->get_error_messages( $error_code ) as $message ) : ?>
<li>
<?php echo wp_kses_post( $message ); ?>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
</p>
</div>
<?php
}
if ( ! file_exists( __DIR__ . '/vendor/autoload.php' ) || ! file_exists( __DIR__ . '/vendor/sabberworm/php-css-parser' ) || ! file_exists( __DIR__ . '/assets/js/amp-block-editor-toggle-compiled.js' ) ) {
add_action( 'admin_notices', '_amp_print_build_needed_notice' );

// Abort if dependencies are not satisfied.
if ( ! empty( $_amp_load_errors->errors ) ) {
add_action( 'admin_notices', '_amp_show_load_errors_admin_notice' );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$messages = array( __( 'AMP plugin unable to initialize.', 'amp' ) );
foreach ( array_keys( $_amp_load_errors->errors ) as $error_code ) {
$messages = array_merge( $messages, $_amp_load_errors->get_error_messages( $error_code ) );
}
$message = implode( "\n * ", $messages );
$message = str_replace( array( '<code>', '</code>' ), '`', $message );
WP_CLI::warning( $message );
}
return;
}

define( 'AMP__FILE__', __FILE__ );
define( 'AMP__DIR__', dirname( __FILE__ ) );
define( 'AMP__VERSION', '1.1.1' );
define( 'AMP__VERSION', '1.2-alpha' );

/**
* Print admin notice if plugin installed with incorrect slug (which impacts WordPress's auto-update system).
Expand Down Expand Up @@ -262,14 +272,11 @@ function amp_init() {
add_action( 'wp_loaded', 'amp_add_options_menu' );
add_action( 'wp_loaded', 'amp_admin_pointer' );
add_action( 'parse_query', 'amp_correct_query_when_is_front_page' );
add_action( 'admin_bar_menu', 'amp_add_admin_bar_view_link', 100 );

// Redirect the old url of amp page to the updated url.
add_filter( 'old_slug_redirect_url', 'amp_redirect_old_slug_to_new_url' );

if ( class_exists( 'Jetpack' ) && ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) && version_compare( JETPACK__VERSION, '6.2-alpha', '<' ) ) {
require_once AMP__DIR__ . '/jetpack-helper.php';
}

// Add actions for legacy post templates.
add_action( 'wp', 'amp_maybe_add_actions' );

Expand Down
5 changes: 5 additions & 0 deletions includes/sanitizers/class-amp-nav-menu-toggle-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public function sanitize() {
$script_el->appendChild( $this->dom->createTextNode( wp_json_encode( $expanded ) ) );
$state_el->appendChild( $script_el );
$nav_el->parentNode->insertBefore( $state_el, $nav_el );
if ( 'body' === $nav_el->nodeName ) {
$nav_el->insertBefore( $state_el, $nav_el->firstChild );
} else {
$nav_el->parentNode->insertBefore( $state_el, $nav_el );
}

$button_on = sprintf( "tap:AMP.setState({ $state_id: ! $state_id })" );
$button_el->setAttribute( 'on', $button_on );
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Enable AMP on your WordPress site, the WordPress way.
**Contributors:** [automattic](https://profiles.wordpress.org/automattic), [xwp](https://profiles.wordpress.org/xwp), [google](https://profiles.wordpress.org/google), [westonruter](https://profiles.wordpress.org/westonruter), [ryankienstra](https://profiles.wordpress.org/ryankienstra), [batmoo](https://profiles.wordpress.org/batmoo), [stubgo](https://profiles.wordpress.org/stubgo), [albertomedina](https://profiles.wordpress.org/albertomedina), [tweetythierry](https://profiles.wordpress.org/tweetythierry), [joshuawold](https://profiles.wordpress.org/joshuawold), [postphotos](https://profiles.wordpress.org/postphotos)
**Tags:** [amp](https://wordpress.org/plugins/tags/amp), [mobile](https://wordpress.org/plugins/tags/mobile)
**Requires at least:** 4.9
**Tested up to:** 5.1
**Tested up to:** 5.2
**Stable tag:** 1.1.1
**License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html)
**Requires PHP:** 5.4
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contributors: automattic, xwp, google, westonruter, ryankienstra, batmoo, stubgo, albertomedina, tweetythierry, joshuawold, postphotos
Tags: amp, mobile
Requires at least: 4.9
Tested up to: 5.1
Tested up to: 5.2
Stable tag: 1.1.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down

0 comments on commit 96ccc1e

Please sign in to comment.