Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:ampproject/amp-wp into add-css-c…
Browse files Browse the repository at this point in the history
…aching-reenable-button

* 'develop' of github.com:ampproject/amp-wp:
  Restore unification of multi-page post content in Reader mode (#4547)
  Prevent styles from being removed when in Customizer preview with Standard mode (#4553)
  Omit Jetpack from being activated during PHPUnit test runs
  Use title case for Paired Browsing link in edit post screen (#4540)
  Ensure that validation query vars persist through redirects (#4544)
  Update dependency babel-jest to v25.2.6 (#4510)
  Update dependency css-loader to v3.5.0 (#4537)
  Update dependency autoprefixer to v9.7.6 (#4539)
  Add requirements to plugin file header (#4543)
  Force status code of validation responses to be 200 (#4533)
  Update optimizer test specs (#4527)
  Bump stable tag to 1.5.2
  Cache response status and headers when fetching external stylesheets (#4509)
  Fix securing multi-line mustache templates (#4521)
  Add CSS monitoring time series to Site Health debugging info (#4519)
  Update hostname used for WordPress TV embeds to fix external HTTP requests (#4524)
  Mock Imgur embed tests
  Mock Facebook embed tests
  Standardize file and class names for embed handlers
  • Loading branch information
westonruter committed Apr 10, 2020
2 parents a52b391 + 1868ea2 commit daa92ca
Show file tree
Hide file tree
Showing 67 changed files with 641 additions and 357 deletions.
2 changes: 2 additions & 0 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Text Domain: amp
* Domain Path: /languages/
* License: GPLv2 or later
* Requires at least: 4.9
* Requires PHP: 5.6
*
* @package AMP
*/
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 @@ -2023,6 +2023,7 @@ public static function prepare_response( $response, $args = [] ) {

// Respond early with results if performing a validate request.
if ( AMP_Validation_Manager::$is_validate_request ) {
status_header( 200 );
header( 'Content-Type: application/json; charset=utf-8' );
return wp_json_encode(
AMP_Validation_Manager::get_validate_response_data( $sanitization_results ),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 11 additions & 2 deletions includes/templates/class-amp-post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,17 @@ private function build_post_comments_data() {
* Build post content.
*/
private function build_post_content() {
/** This filter is documented in wp-includes/post-template.php */
$content = apply_filters( 'the_content', get_the_content( null, false, $this->post ) );
if ( post_password_required( $this->post ) ) {
$content = get_the_password_form( $this->post );
} else {
/**
* This filter is documented in wp-includes/post-template.php.
*
* Note: This is intentionally not using get_the_content() because the legacy behavior of posts in
* Reader mode is to display multi-page posts as a single page without any pagination links.
*/
$content = apply_filters( 'the_content', $this->post->post_content );
}

$this->add_data_by_key( 'post_amp_content', $content );
}
Expand Down
2 changes: 1 addition & 1 deletion includes/validation/class-amp-validated-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,7 @@ public static function print_status_meta_box( $post ) {
printf(
' | <a href="%s">%s</a>',
esc_url( AMP_Theme_Support::get_paired_browsing_url( self::get_url_from_post( $post ) ) ),
esc_html__( 'Paired browsing', 'amp' )
esc_html__( 'Paired Browsing', 'amp' )
);
}
?>
Expand Down
4 changes: 4 additions & 0 deletions includes/validation/class-amp-validation-error-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,10 @@ public static function get_source_key_label( $key, $validation_error ) {
return __( 'Invalid attribute', 'amp' );
case 'required_attr_value':
return __( 'Required attribute value', 'amp' );
case 'url':
return __( 'URL', 'amp' );
case 'message':
return __( 'Message', 'amp' );
default:
return $key;
}
Expand Down
2 changes: 1 addition & 1 deletion includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ public static function validate_url( $url ) {
break;
}

$validation_url = $location_header;
$validation_url = add_query_arg( $added_query_vars, $location_header );
}

if ( is_wp_error( $r ) ) {
Expand Down
2 changes: 2 additions & 0 deletions lib/common/phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ parameters:
- %currentWorkingDirectory%/src/
autoload_files:
- %currentWorkingDirectory%/vendor/autoload.php
ignoreErrors:
- '#^PHPDoc tag @throws with type AmpProject\\Exception\\FailedRemoteRequest is not subtype of Throwable$#'
2 changes: 2 additions & 0 deletions lib/common/src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ interface Attribute
const ALL_AMP4ADS = [self::AMP4ADS, self::AMP4ADS_EMOJI, self::AMP4ADS_EMOJI_ALT];
const ALL_AMP4EMAIL = [self::AMP4EMAIL, self::AMP4EMAIL_EMOJI, self::AMP4EMAIL_EMOJI_ALT];

const ALL_BOILERPLATES = [self::AMP_BOILERPLATE, self::AMP4ADS_BOILERPLATE, self::AMP4EMAIL_BOILERPLATE];

const TYPE_HTML = 'text/html';
const TYPE_JSON = 'application/json';
const TYPE_LD_JSON = 'application/ld+json';
Expand Down
2 changes: 1 addition & 1 deletion lib/common/src/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ private function maybeRestoreNoscriptElements($html)
private function secureMustacheScriptTemplates($html)
{
return preg_replace(
'#<script(\s[^>]*?template=(["\']?)amp-mustache\2[^>]*)>(.*?)</script\s*?>#i',
'#<script(\s[^>]*?template=(["\']?)amp-mustache\2[^>]*)>(.*?)</script\s*?>#is',
'<tmp-script$1>$3</tmp-script>',
$html
);
Expand Down
13 changes: 13 additions & 0 deletions lib/common/src/Exception/FailedRemoteRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace AmpProject\Exception;

/**
* Marker interface to enable consumers to catch all exceptions for failed remote requests.
*
* @package ampproject/common
*/
interface FailedRemoteRequest extends AmpException
{

}
27 changes: 27 additions & 0 deletions lib/common/src/Exception/FailedToGetCachedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace AmpProject\Exception;

use RuntimeException;

/**
* Exception thrown when a cached remote response could not be retrieved.
*
* @package ampproject/common
*/
final class FailedToGetCachedResponse extends RuntimeException implements FailedRemoteRequest
{

/**
* Instantiate a FailedToGetCachedResponseData exception for a URL if the cached response data could not be retrieved.
*
* @param string $url URL that failed to be fetched.
* @return self
*/
public static function withUrl($url)
{
$message = "Failed to retrieve the cached response for the URL '{$url}'.";

return new self($message);
}
}
2 changes: 1 addition & 1 deletion lib/common/src/Exception/FailedToGetFromRemoteUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @package ampproject/common
*/
final class FailedToGetFromRemoteUrl extends RuntimeException implements AmpException
final class FailedToGetFromRemoteUrl extends RuntimeException implements FailedRemoteRequest
{

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/common/src/RemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AmpProject;

use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\Exception\FailedRemoteRequest;

/**
* Interface for abstracting away the transport that is being used for making remote requests.
Expand All @@ -19,7 +19,7 @@ interface RemoteGetRequest
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url);
}
3 changes: 2 additions & 1 deletion lib/common/src/RemoteRequest/CurlRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AmpProject\RemoteRequest;

use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\RemoteGetRequest;
use AmpProject\Response;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function __construct($sslVerify = true, $timeout = self::DEFAULT_TIMEOUT,
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/common/src/RemoteRequest/FallbackRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AmpProject\RemoteRequest;

use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\RemoteGetRequest;
use AmpProject\Response;
use Exception;
Expand Down Expand Up @@ -55,7 +55,7 @@ private function addRemoteGetRequestInstance(RemoteGetRequest $remoteGetRequest)
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url)
{
Expand Down
3 changes: 2 additions & 1 deletion lib/common/src/RemoteRequest/FilesystemRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AmpProject\RemoteRequest;

use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\RemoteGetRequest;
use AmpProject\Response;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function __construct($argumentMap)
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/common/src/RemoteRequest/StubbedRemoteGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AmpProject\RemoteRequest;

use AmpProject\Exception\FailedToGetFromRemoteUrl;
use AmpProject\Exception\FailedRemoteRequest;
use AmpProject\RemoteGetRequest;
use AmpProject\Response;
use LogicException;
Expand Down Expand Up @@ -37,7 +37,7 @@ public function __construct($argumentMap)
*
* @param string $url URL to get.
* @return Response Response for the executed request.
* @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed.
* @throws FailedRemoteRequest If retrieving the contents from the URL failed.
*/
public function get($url)
{
Expand Down
33 changes: 33 additions & 0 deletions lib/common/tests/Dom/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,39 @@ public function dataDomDocument()
'<!DOCTYPE html><html>' . $head . '<body><script template="amp-mustache" type="text/plain" id="foo"><table><tr>{{#example}}<td></td>{{/example}}</tr></table></script><script type="text/plain" template="amp-mustache" id="example"><p>{{#baz}}This is inside a template{{/baz}}</p></script></body></html>',
'<!DOCTYPE html><html>' . $head . '<body><script template="amp-mustache" type="text/plain" id="foo"><table><tr>{{#example}}<td></td>{{/example}}</tr></table></script><script type="text/plain" template="amp-mustache" id="example"><p>{{#baz}}This is inside a template{{/baz}}</p></script></body></html>',
],
'multiline_mustache_templates_appear' => [
'utf-8',
'
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body>
<script type="text/plain" template="amp-mustache">
<table>
<tr>
{{#foo}}<td></td>{{/foo}}
</tr>
</table>
</script>
</body>
</html>
',
'
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body>
<script type="text/plain" template="amp-mustache">
<table>
<tr>
{{#foo}}<td></td>{{/foo}}
</tr>
</table>
</script>
</body>
</html>
',
],
];
}

Expand Down
5 changes: 3 additions & 2 deletions lib/optimizer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"cs": "if [ -z $TEST_SKIP_PHPCS ]; then phpcs --standard=PSR12 -s -n src tests; fi",
"lint": "if [ -z $TEST_SKIP_LINTING ]; then parallel-lint -j 10 --colors --exclude vendor .; fi",
"post-update-cmd": [
"rm -rf tests/spec && bin/sync-amp-toolbox-test-suite.php",
"@update-test-specs",
"bin/sync-amp-runtime-local-fallback-resources.php"
],
"test": [
Expand All @@ -72,6 +72,7 @@
"@analyze"
],
"analyze": "if [ -z $TEST_SKIP_PHPSTAN ]; then phpstan analyze --ansi; fi",
"unit": "if [ -z $TEST_SKIP_PHPUNIT ]; then phpunit --colors=always; fi"
"unit": "if [ -z $TEST_SKIP_PHPUNIT ]; then phpunit --colors=always; fi",
"update-test-specs": "rm -rf tests/spec && bin/sync-amp-toolbox-test-suite.php"
}
}
2 changes: 1 addition & 1 deletion lib/optimizer/resources/local_fallback/rtv/metadata
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"ampRuntimeVersion":"012002251816300","ampCssUrl":"https://cdn.ampproject.org/rtv/012002251816300/v0.css","canaryPercentage":"0.005","diversions":["002002251816300","032002251816300","022002251816300"],"ltsRuntimeVersion":"012002191527100","ltsCssUrl":"https://cdn.ampproject.org/rtv/012002191527100/v0.css"}
{"ampRuntimeVersion":"012004030010070","ampCssUrl":"https://cdn.ampproject.org/rtv/012004030010070/v0.css","canaryPercentage":"0.005","diversions":["002004012111560","002004012158290","022004030010070","032004012111560","032004012158290","042004030033570"],"ltsRuntimeVersion":"012002251816300","ltsCssUrl":"https://cdn.ampproject.org/rtv/012002251816300/v0.css"}
52 changes: 32 additions & 20 deletions lib/optimizer/src/Transformer/AmpBoilerplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

use AmpProject\Amp;
use AmpProject\Attribute;
use AmpProject\DevMode;
use AmpProject\Dom\Document;
use AmpProject\Optimizer\ErrorCollection;
use AmpProject\Optimizer\Transformer;
use AmpProject\Tag;
use DOMElement;

/**
* Transformer that removes <style> and <noscript> tags in <head>, keeping only the amp-custom style tag. It then
* inserts the amp-boilerplate.
* Transformer that removes AMP boilerplate <style> and <noscript> tags in <head>, keeping only the amp-custom style tag.
* It then (re-)inserts the amp-boilerplate unless the document is marked with the i-amphtml-no-boilerplate attribute.
*
* This is ported from the Go optimizer.
*
Expand Down Expand Up @@ -68,31 +67,44 @@ public function transform(Document $document, ErrorCollection $errors)
}

/**
* Remove all <style> and <noscript> tags except for the <style amp-custom> tag.
* Remove all <style> and <noscript> tags which are for the boilerplate.
*
* @param Document $document Document to remove the tags from.
*/
private function removeStyleAndNoscriptTags(Document $document)
{
$headNode = $document->head->firstChild;
$devMode = DevMode::isActiveForDocument($document);
while ($headNode) {
$nextSibling = $headNode->nextSibling;
if ($headNode instanceof DOMElement && (! $devMode || ! DevMode::hasExemptionForNode($headNode))) {
switch ($headNode->tagName) {
case Tag::STYLE:
if (! $headNode->hasAttribute(Attribute::AMP_CUSTOM)) {
$document->head->removeChild($headNode);
}
break;
case Tag::NOSCRIPT:
$document->head->removeChild($headNode);
break;
}
/**
* Style element.
*
* @var DOMElement $style
*/
foreach (iterator_to_array($document->head->getElementsByTagName('style')) as $style) {
if (! $this->isBoilerplateStyle($style)) {
continue;
}
if (Tag::NOSCRIPT === $style->parentNode->nodeName) {
$style->parentNode->parentNode->removeChild($style->parentNode);
} else {
$style->parentNode->removeChild($style);
}
}
}

$headNode = $nextSibling;
/**
* Check whether an element is a boilerplate style.
*
* @param DOMElement $element Element to check.
* @return bool Whether the element is a boilerplate style.
*/
private function isBoilerplateStyle(DOMElement $element)
{
foreach (Attribute::ALL_BOILERPLATES as $boilerplate) {
if ($element->hasAttribute($boilerplate)) {
return true;
}
}

return false;
}

/**
Expand Down
Loading

0 comments on commit daa92ca

Please sign in to comment.