Skip to content

Commit

Permalink
Issue #842: Merge in develop, resolve conflicts.
Browse files Browse the repository at this point in the history
Mostly resolve in favor of develop.
But apply some changes from branch add/842-plugin-validation.
Like the constant AMP_Validation_Utils::CALLBACK_KEY,
instead of 'remove_invalid_callback'.
  • Loading branch information
Ryan Kienstra committed Feb 26, 2018
2 parents 54e15cf + 665df93 commit 7e049ae
Show file tree
Hide file tree
Showing 18 changed files with 1,124 additions and 222 deletions.
33 changes: 14 additions & 19 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,14 @@ function amp_after_setup_theme() {
define( 'AMP_QUERY_VAR', apply_filters( 'amp_query_var', 'amp' ) );
}

add_action( 'init', 'amp_init' );
add_action( 'widgets_init', 'AMP_Theme_Support::register_widgets' ); // @todo Let this be called by AMP_Theme_Support::init().
add_action( 'init', 'AMP_Theme_Support::setup_commenting' ); // @todo Let this be called by AMP_Theme_Support::init().
add_action( 'admin_init', 'AMP_Options_Manager::register_settings' );
add_action( 'wp_loaded', 'amp_post_meta_box' );
add_action( 'wp_loaded', 'amp_add_options_menu' );
add_action( 'parse_query', 'amp_correct_query_when_is_front_page' );
AMP_Post_Type_Support::add_post_type_support();
AMP_Validation_Utils::init();
add_action( 'init', 'amp_init', 0 ); // Must be 0 because widgets_init happens at init priority 1.
}
add_action( 'after_setup_theme', 'amp_after_setup_theme', 5 );

/**
* Init AMP.
*
* @since 0.1
* @global string $pagenow
*/
function amp_init() {

Expand All @@ -118,16 +109,24 @@ function amp_init() {

add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK );

AMP_Theme_Support::init();
AMP_Post_Type_Support::add_post_type_support();
AMP_Validation_Utils::init();
add_filter( 'request', 'amp_force_query_var_value' );
add_action( 'wp', 'amp_maybe_add_actions' );
add_action( 'admin_init', 'AMP_Options_Manager::register_settings' );
add_action( 'wp_loaded', 'amp_post_meta_box' );
add_action( 'wp_loaded', 'amp_add_options_menu' );
add_action( 'parse_query', 'amp_correct_query_when_is_front_page' );

// 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 ) ) {
require_once AMP__DIR__ . '/jetpack-helper.php';
}
amp_handle_xhr_request();

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

// Make sure the `amp` query var has an explicit value.
Expand All @@ -150,15 +149,9 @@ function amp_force_query_var_value( $query_vars ) {
* @return void
*/
function amp_maybe_add_actions() {
$is_amp_endpoint = is_amp_endpoint();

// Add hooks for when a themes that support AMP.
// Short-circuit when theme supports AMP, as everything is handled by AMP_Theme_Support.
if ( current_theme_supports( 'amp' ) ) {
if ( $is_amp_endpoint ) {
AMP_Theme_Support::init();
} else {
amp_add_frontend_actions();
}
return;
}

Expand All @@ -168,6 +161,8 @@ function amp_maybe_add_actions() {
return;
}

$is_amp_endpoint = is_amp_endpoint();

/**
* Queried post object.
*
Expand Down
11 changes: 11 additions & 0 deletions assets/css/amp-default.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.amp-wp-enforced-sizes {
/** Our sizes fallback is 100vw, and we have a padding on the container; the max-width here prevents the element from overflowing. **/
max-width: 100%;
margin: 0 auto;
}

.amp-wp-unknown-size img {
/** Worst case scenario when we can't figure out dimensions for an image. **/
/** Force the image into a box of fixed dimensions and use object-fit to scale. **/
object-fit: contain;
}
19 changes: 19 additions & 0 deletions assets/css/amp-playlist-shortcode.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* For the custom AMP implementation of the 'playlist' shortcode.
*/
.wp-playlist .wp-playlist-current-item img {
margin-right: 0;
}

.wp-playlist .wp-playlist-current-item amp-img {
float: left;
margin-right: 10px;
}

.wp-playlist audio {
display: block;
}

.wp-playlist .amp-carousel-button {
visibility: hidden;
}
2 changes: 2 additions & 0 deletions assets/js/amp-post-meta-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ var ampPostMetaBox = ( function( $ ) {
editAmpStatus.fadeToggle( component.toggleSpeed, function() {
if ( editAmpStatus.is( ':visible' ) ) {
editAmpStatus.focus();
} else {
$container.find( 'input[type="radio"]' ).first().focus();
}
} );
$container.slideToggle( component.toggleSpeed );
Expand Down
4 changes: 4 additions & 0 deletions includes/admin/class-amp-post-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public function enqueue_admin_assets() {
isset( $screen->base )
&&
'post' === $screen->base
&&
is_post_type_viewable( $post->post_type )
);
if ( ! $validate ) {
return;
Expand Down Expand Up @@ -165,6 +167,8 @@ public function render_status( $post ) {
$verify = (
isset( $post->ID )
&&
is_post_type_viewable( $post->post_type )
&&
current_user_can( 'edit_post', $post->ID )
&&
! amp_is_canonical()
Expand Down
92 changes: 1 addition & 91 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ function amp_get_content_embed_handlers( $post = null ) {
'AMP_Vine_Embed_Handler' => array(),
'AMP_Facebook_Embed_Handler' => array(),
'AMP_Pinterest_Embed_Handler' => array(),
'AMP_Playlist_Embed_Handler' => array(),
'AMP_Reddit_Embed_Handler' => array(),
'AMP_Tumblr_Embed_Handler' => array(),
'AMP_Gallery_Embed_Handler' => array(),
Expand Down Expand Up @@ -483,94 +484,3 @@ function amp_print_schemaorg_metadata() {
<script type="application/ld+json"><?php echo wp_json_encode( $metadata ); ?></script>
<?php
}

/**
* Hook into a form submissions, such as comment the form or some other .
*
* @since 0.7.0
* @global string $pagenow
*/
function amp_handle_xhr_request() {
global $pagenow;
if ( ! isset( $_GET['__amp_source_origin'] ) ) { // WPCS: CSRF ok. Beware of AMP_Theme_Support::purge_amp_query_vars().
return;
}

if ( isset( $pagenow ) && 'wp-comments-post.php' === $pagenow ) {
// We don't need any data, so just send a success.
add_filter( 'comment_post_redirect', function() {
// We don't need any data, so just send a success.
wp_send_json_success();
}, PHP_INT_MAX, 2 );
amp_handle_xhr_headers_output();
} elseif ( isset( $_GET['_wp_amp_action_xhr_converted'] ) ) { // WPCS: CSRF ok.
add_filter( 'wp_redirect', 'amp_intercept_post_request_redirect', PHP_INT_MAX, 2 );
amp_handle_xhr_headers_output();
}
}

/**
* Handle the AMP XHR headers and output errors.
*
* @since 0.7.0
*/
function amp_handle_xhr_headers_output() {
// Add die handler for AMP error display.
add_filter( 'wp_die_handler', function() {
/**
* New error handler for AMP form submission.
*
* @param WP_Error|string $error The error to handle.
*/
return function( $error ) {
status_header( 400 );
if ( is_wp_error( $error ) ) {
$error = $error->get_error_message();
}
$amp_mustache_allowed_html_tags = array( 'strong', 'b', 'em', 'i', 'u', 's', 'small', 'mark', 'del', 'ins', 'sup', 'sub' );
wp_send_json( array(
'error' => wp_kses( $error, array_fill_keys( $amp_mustache_allowed_html_tags, array() ) ),
) );
};
} );

// Send AMP header.
$origin = esc_url_raw( wp_unslash( $_GET['__amp_source_origin'] ) ); // WPCS: CSRF ok.
header( 'AMP-Access-Control-Allow-Source-Origin: ' . $origin, true );
}

/**
* Intercept the response to a non-comment POST request.
*
* @since 0.7.0
* @param string $location The location to redirect to.
*/
function amp_intercept_post_request_redirect( $location ) {

// Make sure relative redirects get made absolute.
$parsed_location = array_merge(
array(
'scheme' => 'https',
'host' => wp_parse_url( home_url(), PHP_URL_HOST ),
'path' => strtok( wp_unslash( $_SERVER['REQUEST_URI'] ), '?' ),
),
wp_parse_url( $location )
);

$absolute_location = $parsed_location['scheme'] . '://' . $parsed_location['host'];
if ( isset( $parsed_location['port'] ) ) {
$absolute_location .= ':' . $parsed_location['port'];
}
$absolute_location .= $parsed_location['path'];
if ( isset( $parsed_location['query'] ) ) {
$absolute_location .= '?' . $parsed_location['query'];
}
if ( isset( $parsed_location['fragment'] ) ) {
$absolute_location .= '#' . $parsed_location['fragment'];
}

header( 'AMP-Redirect-To: ' . $absolute_location );
header( 'Access-Control-Expose-Headers: AMP-Redirect-To' );
// Send json success as no data is required.
wp_send_json_success();
}
1 change: 1 addition & 0 deletions includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class AMP_Autoloader {
'AMP_Issuu_Embed_Handler' => 'includes/embeds/class-amp-issuu-embed-handler',
'AMP_Meetup_Embed_Handler' => 'includes/embeds/class-amp-meetup-embed-handler',
'AMP_Pinterest_Embed_Handler' => 'includes/embeds/class-amp-pinterest-embed',
'AMP_Playlist_Embed_Handler' => 'includes/embeds/class-amp-playlist-embed-handler',
'AMP_Reddit_Embed_Handler' => 'includes/embeds/class-amp-reddit-embed-handler',
'AMP_SoundCloud_Embed_Handler' => 'includes/embeds/class-amp-soundcloud-embed',
'AMP_Tumblr_Embed_Handler' => 'includes/embeds/class-amp-tumblr-embed-handler',
Expand Down
Loading

0 comments on commit 7e049ae

Please sign in to comment.