Skip to content

Commit

Permalink
Merge branch '0.7' of https://github.com/Automattic/amp-wp into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed May 10, 2018
2 parents ac37c38 + 4dd1023 commit f15aa74
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 86 deletions.
1 change: 0 additions & 1 deletion amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ function amp_init() {

add_rewrite_endpoint( amp_get_slug(), EP_PERMALINK );

AMP_Validation_Utils::init();
AMP_Theme_Support::init();
AMP_Post_Type_Support::add_post_type_support();
add_filter( 'request', 'amp_force_query_var_value' );
Expand Down
1 change: 1 addition & 0 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public static function init() {
}

self::$init_start_time = microtime( true );
AMP_Validation_Utils::init();

self::purge_amp_query_vars();
self::handle_xhr_request();
Expand Down
79 changes: 30 additions & 49 deletions includes/utils/class-amp-validation-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,12 @@ class AMP_Validation_Utils {
* @return void
*/
public static function init() {
if ( current_theme_supports( 'amp' ) ) {
add_action( 'init', array( __CLASS__, 'register_post_type' ) );
add_filter( 'dashboard_glance_items', array( __CLASS__, 'filter_dashboard_glance_items' ) );
add_action( 'rightnow_end', array( __CLASS__, 'print_dashboard_glance_styles' ) );
add_action( 'save_post', array( __CLASS__, 'handle_save_post_prompting_validation' ), 10, 2 );
add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_validation' ) );
add_action( 'rest_api_init', array( __CLASS__, 'add_rest_api_fields' ) );
}

add_action( 'init', array( __CLASS__, 'register_post_type' ) );
add_filter( 'dashboard_glance_items', array( __CLASS__, 'filter_dashboard_glance_items' ) );
add_action( 'rightnow_end', array( __CLASS__, 'print_dashboard_glance_styles' ) );
add_action( 'save_post', array( __CLASS__, 'handle_save_post_prompting_validation' ), 10, 2 );
add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_validation' ) );
add_action( 'rest_api_init', array( __CLASS__, 'add_rest_api_fields' ) );
add_action( 'edit_form_top', array( __CLASS__, 'print_edit_form_validation_status' ), 10, 2 );
add_action( 'all_admin_notices', array( __CLASS__, 'plugin_notice' ) );
add_filter( 'manage_' . self::POST_TYPE_SLUG . '_posts_columns', array( __CLASS__, 'add_post_columns' ) );
Expand Down Expand Up @@ -578,57 +575,41 @@ public static function print_edit_form_validation_status( $post ) {
return;
}

$url = null;
$validation_status_post = null;
$validation_errors = array();

// Incorporate frontend validation status if there is a known URL for the post.
$existing_validation_errors = self::get_existing_validation_errors( $post );
if ( isset( $existing_validation_errors ) ) {
$validation_errors = $existing_validation_errors;
// Skip if the post type is not viewable on the frontend, since we need a permalink to validate.
if ( ! is_post_type_viewable( $post->post_type ) ) {
return;
}

// If no results from URL are available, validate post content outside frontend context.
if ( empty( $validation_errors ) && post_type_supports( $post->post_type, 'editor' ) ) {
self::process_markup( $post->post_content );
$validation_errors = array_merge(
$validation_errors,
self::$validation_errors
);
self::reset_validation_results();
$url = amp_get_permalink( $post->ID );
$validation_status_post = self::get_validation_status_post( $url );

// Make sure original post is restored after applying shortcodes which could change it.
$GLOBALS['post'] = $post; // WPCS: override ok.
setup_postdata( $post );
// No validation status exists yet, so there is nothing to show.
if ( ! $validation_status_post ) {
return;
}

if ( empty( $validation_errors ) ) {
$validation_errors = json_decode( $validation_status_post->post_content, true );

// No validation errors so abort.
if ( empty( $validation_errors ) || ! is_array( $validation_errors ) ) {
return;
}

echo '<div class="notice notice-warning">';
echo '<p>';
esc_html_e( 'Warning: There is content which fails AMP validation; it will be stripped when served as AMP.', 'amp' );
if ( $validation_status_post || $url ) {
if ( $validation_status_post ) {
echo sprintf(
' <a href="%s" target="_blank">%s</a>',
esc_url( get_edit_post_link( $validation_status_post ) ),
esc_html__( 'Details', 'amp' )
);
}
if ( $url ) {
if ( $validation_status_post ) {
echo ' | ';
}
echo sprintf(
' <a href="%s" aria-label="%s" target="_blank">%s</a>',
esc_url( self::get_debug_url( $url ) ),
esc_attr__( 'Validate URL on frontend but without invalid elements/attributes removed', 'amp' ),
esc_html__( 'Debug', 'amp' )
);
}
}
echo sprintf(
' <a href="%s" target="_blank">%s</a>',
esc_url( get_edit_post_link( $validation_status_post ) ),
esc_html__( 'Details', 'amp' )
);
echo ' | ';
echo sprintf(
' <a href="%s" aria-label="%s" target="_blank">%s</a>',
esc_url( self::get_debug_url( $url ) ),
esc_attr__( 'Validate URL on frontend but without invalid elements/attributes removed', 'amp' ),
esc_html__( 'Debug', 'amp' )
);
echo '</p>';

$results = self::summarize_validation_errors( array_unique( $validation_errors, SORT_REGULAR ) );
Expand Down
70 changes: 53 additions & 17 deletions jetpack-helper.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
<?php
/**
* Jetpack bits.
*
* @todo Move this into Jetpack. See https://github.com/Automattic/amp-wp/issues/1021
* @package AMP
*/

// Jetpack bits.

add_action( 'pre_amp_render_post', 'amp_jetpack_mods' );
add_action( 'template_redirect', 'amp_jetpack_mods', 9 );

/**
* Disable Jetpack features that are not compatible with AMP.
*
**/
* @since 0.2
*/
function amp_jetpack_mods() {
if ( ! is_amp_endpoint() ) {
return;
}
if ( Jetpack::is_module_active( 'stats' ) ) {
add_action( 'amp_post_template_footer', 'jetpack_amp_add_stats_pixel' );
}
amp_jetpack_disable_sharing();
amp_jetpack_disable_related_posts();
add_filter( 'videopress_shortcode_options', 'amp_videopress_enable_freedom_mode' );
}

/**
* Disable Jetpack sharing.
*
* @since 0.3
*/
function amp_jetpack_disable_sharing() {
add_filter( 'sharing_show', '__return_false', 100 );
}
Expand All @@ -25,15 +39,21 @@ function amp_jetpack_disable_sharing() {
*
* That placeholder is useless since we can't ouput, and don't want to output Related Posts in AMP.
*
**/
* @since 0.2
*/
function amp_jetpack_disable_related_posts() {
if ( class_exists( 'Jetpack_RelatedPosts' ) ) {
$jprp = Jetpack_RelatedPosts::init();
remove_filter( 'the_content', array( $jprp, 'filter_add_target_to_dom' ), 40 );
}
}

function jetpack_amp_add_stats_pixel( $amp_template ) {
/**
* Add Jetpack stats pixel.
*
* @since 0.3.2
*/
function jetpack_amp_add_stats_pixel() {
if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
return;
}
Expand All @@ -47,25 +67,41 @@ function jetpack_amp_add_stats_pixel( $amp_template ) {
*
* Looks something like:
* https://pixel.wp.com/g.gif?v=ext&j=1%3A3.9.1&blog=1234&post=5678&tz=-4&srv=example.com&host=example.com&ref=&rand=0.4107963021218808
*
* @since 0.3.2
*/
function jetpack_amp_build_stats_pixel_url() {
global $wp_the_query;
if ( function_exists( 'stats_build_view_data' ) ) { // added in https://github.com/Automattic/jetpack/pull/3445
if ( function_exists( 'stats_build_view_data' ) ) { // Added in <https://github.com/Automattic/jetpack/pull/3445>.
$data = stats_build_view_data();
} else {
$blog = Jetpack_Options::get_option( 'id' );
$tz = get_option( 'gmt_offset' );
$v = 'ext';
$blog = Jetpack_Options::get_option( 'id' );
$tz = get_option( 'gmt_offset' );
$v = 'ext';
$blog_url = wp_parse_url( site_url() );
$srv = $blog_url['host'];
$j = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION );
$post = $wp_the_query->get_queried_object_id();
$data = compact( 'v', 'j', 'blog', 'post', 'tz', 'srv' );
$srv = $blog_url['host'];
$j = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION );
$post = $wp_the_query->get_queried_object_id();
$data = compact( 'v', 'j', 'blog', 'post', 'tz', 'srv' );
}

$data['host'] = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; // input var ok.
$data['rand'] = 'RANDOM'; // amp placeholder
$data['ref'] = 'DOCUMENT_REFERRER'; // amp placeholder
$data = array_map( 'rawurlencode' , $data );
$data['rand'] = 'RANDOM'; // AMP placeholder.
$data['ref'] = 'DOCUMENT_REFERRER'; // AMP placeholder.
$data = array_map( 'rawurlencode', $data );
return add_query_arg( $data, 'https://pixel.wp.com/g.gif' );
}

/**
* Force videopress to use html5 player that would generate <video /> tag
* that will be later converted to <amp-video />
*
* @since 0.7.1
*
* @param array $options videopress shortcode options.
* @return array videopress shortcode options with `freedom` set to true
*/
function amp_videopress_enable_freedom_mode( $options ) {
$options['freedom'] = true;
return $options;
}
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ Follow along with or [contribute](https://github.com/Automattic/amp-wp/blob/deve
### 1.0 (unreleased) ###
- ...

### 0.7.1 (Unreleased) ###
- Limit showing AMP validation warnings to when `amp` theme support is present. See [#1132](https://github.com/Automattic/amp-wp/pull/1132).
- Supply the extracted dimensions to images determined to need them; fixes regression from 0.6 this is key for Gutenberg compat. See [#1117](https://github.com/Automattic/amp-wp/pull/1117).
- Ensure before/after is amended to filtered comment_reply_link. See [#1118](https://github.com/Automattic/amp-wp/pull/1118).
- Force VideoPress to use html5 player for AMP. See [#1125](https://github.com/Automattic/amp-wp/pull/1125). Props yurynix.

See [0.7.1 milestone](https://github.com/Automattic/amp-wp/milestone/8?closed=1).

### 0.7.0 (2018-05-03) ###
- Render an entire site as "Native AMP" if the theme calls `add_theme_support( 'amp' )`. See [#857](https://github.com/Automattic/amp-wp/pull/857), [#852](https://github.com/Automattic/amp-wp/pull/852), [#865](https://github.com/Automattic/amp-wp/pull/865), [#888](https://github.com/Automattic/amp-wp/pull/888). Props westonruter, kaitnyl, ThierryA.
- Use the AMP spec to automatically discover the required AMP component scripts to include on the page while post-processing. See [#882](https://github.com/Automattic/amp-wp/pull/882), [#885](https://github.com/Automattic/amp-wp/pull/885). Props westonruter.
Expand Down
8 changes: 8 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ Follow along with or [contribute](https://github.com/Automattic/amp-wp/blob/deve
= 1.0 (unreleased) =
- ...

= 0.7.1 (Unreleased) =
- Limit showing AMP validation warnings to when `amp` theme support is present. See [#1132](https://github.com/Automattic/amp-wp/pull/1132).
- Supply the extracted dimensions to images determined to need them; fixes regression from 0.6 this is key for Gutenberg compat. See [#1117](https://github.com/Automattic/amp-wp/pull/1117).
- Ensure before/after is amended to filtered comment_reply_link. See [#1118](https://github.com/Automattic/amp-wp/pull/1118).
- Force VideoPress to use html5 player for AMP. See [#1125](https://github.com/Automattic/amp-wp/pull/1125). Props yurynix.

See [0.7.1 milestone](https://github.com/Automattic/amp-wp/milestone/8?closed=1).

= 0.7.0 (2018-05-03) =

- Render an entire site as "Native AMP" if the theme calls `add_theme_support( 'amp' )`. See [#857](https://github.com/Automattic/amp-wp/pull/857), [#852](https://github.com/Automattic/amp-wp/pull/852), [#865](https://github.com/Automattic/amp-wp/pull/865), [#888](https://github.com/Automattic/amp-wp/pull/888). Props westonruter, kaitnyl, ThierryA.
Expand Down
51 changes: 32 additions & 19 deletions tests/test-class-amp-validation-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ public function test_reset_validation_results() {
* @covers AMP_Validation_Utils::print_edit_form_validation_status()
*/
public function test_print_edit_form_validation_status() {
add_theme_support( 'amp' );

AMP_Validation_Utils::register_post_type();
$this->set_capability();
$post = $this->factory()->post->create_and_get();
ob_start();
Expand All @@ -386,26 +389,30 @@ public function test_print_edit_form_validation_status() {
$this->assertNotContains( 'notice notice-warning', $output );
$this->assertNotContains( 'Warning:', $output );

$post->post_content = $this->disallowed_tag;
$this->create_custom_post(
array(
array(
'code' => AMP_Validation_Utils::INVALID_ELEMENT_CODE,
'node_name' => $this->disallowed_tag_name,
'parent_name' => 'div',
'node_attributes' => array(),
'sources' => array(
array(
'type' => 'plugin',
'name' => $this->plugin_name,
),
),
),
),
amp_get_permalink( $post->ID )
);
ob_start();
AMP_Validation_Utils::print_edit_form_validation_status( $post );
$output = ob_get_clean();

$this->assertContains( 'notice notice-warning', $output );
$this->assertContains( 'Warning:', $output );
$this->assertContains( '<code>script</code>', $output );
AMP_Validation_Utils::reset_validation_results();

$youtube = 'https://www.youtube.com/watch?v=GGS-tKTXw4Y';
$post->post_content = $youtube;
ob_start();
AMP_Validation_Utils::print_edit_form_validation_status( $post );
$output = ob_get_clean();

// The YouTube embed handler should convert the URL into a valid AMP element.
$this->assertNotContains( 'notice notice-warning', $output );
$this->assertNotContains( 'Warning:', $output );
AMP_Validation_Utils::reset_validation_results();
}

/**
Expand All @@ -420,7 +427,7 @@ public function test_get_existing_validation_errors() {
$this->assertEquals( null, AMP_Validation_Utils::get_existing_validation_errors( $post ) );

// Create an error custom post for the $post_id, so the function will return existing errors.
$this->create_custom_post( amp_get_permalink( $post->ID ) );
$this->create_custom_post( array(), amp_get_permalink( $post->ID ) );
$this->assertEquals(
$this->get_mock_errors(),
AMP_Validation_Utils::get_existing_validation_errors( $post )
Expand Down Expand Up @@ -1512,7 +1519,7 @@ public function test_rest_field_amp_validation() {
) );

// Create an error custom post for the ID, so this will return the errors in the field.
$this->create_custom_post( amp_get_permalink( $id ) );
$this->create_custom_post( array(), amp_get_permalink( $id ) );

// Make sure capability check is honored.
$this->assertNull( AMP_Validation_Utils::get_amp_validity_rest_field(
Expand Down Expand Up @@ -1548,12 +1555,15 @@ public function test_rest_field_amp_validation() {
/**
* Creates and inserts a custom post.
*
* @param string|null $url The URL where there are errors, or null.
* @param array $errors Validation errors to populate.
* @param string $url URL that the errors occur on. Defaults to the home page.
* @return int|WP_Error $error_post The ID of new custom post, or an error.
*/
public function create_custom_post( $url = null ) {
$url = isset( $url ) ? $url : home_url( '/' );
$content = wp_json_encode( $this->get_mock_errors() );
public function create_custom_post( $errors = null, $url = null ) {
if ( ! $errors ) {
$errors = $this->get_mock_errors();
}
$content = wp_json_encode( $errors );
$encoded_errors = md5( $content );
$post_args = array(
'post_type' => AMP_Validation_Utils::POST_TYPE_SLUG,
Expand All @@ -1562,6 +1572,9 @@ public function create_custom_post( $url = null ) {
'post_status' => 'publish',
);
$error_post = wp_insert_post( wp_slash( $post_args ) );
if ( ! $url ) {
$url = home_url( '/' );
}
update_post_meta( $error_post, AMP_Validation_Utils::AMP_URL_META, $url );
return $error_post;
}
Expand Down

0 comments on commit f15aa74

Please sign in to comment.