Skip to content

Commit

Permalink
Merge pull request #4177 from ampproject/add/disabling-parsed-css-tra…
Browse files Browse the repository at this point in the history
…nsient-caching

Add ability to prevent storing parsed CSS in transients
  • Loading branch information
westonruter authored Mar 23, 2020
2 parents fff8bed + e53a3da commit e6e86dc
Show file tree
Hide file tree
Showing 16 changed files with 1,321 additions and 366 deletions.
307 changes: 4 additions & 303 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
*/

define( 'AMP__FILE__', __FILE__ );
define( 'AMP__DIR__', __DIR__ );
define( 'AMP__DIR__', dirname( __FILE__ ) );
define( 'AMP__VERSION', '1.5.0-alpha' );

/**
* Errors encountered while loading the plugin.
*
* This has to be a global for the same of PHP 5.2.
* This has to be a global for the sake of PHP 5.2.
*
* @var WP_Error $_amp_load_errors
*/
Expand Down Expand Up @@ -200,7 +200,7 @@ function _amp_show_load_errors_admin_notice() {
}
$message = implode( "\n * ", $messages );
$message = str_replace( array( '<code>', '</code>' ), '`', $message );
$message = html_entity_decode( $message, ENT_QUOTES );
$message = html_entity_decode( $message, ENT_QUOTES, 'UTF-8' );

if ( ! class_exists( 'WP_CLI' ) ) {
echo "$message\n"; // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
Expand All @@ -214,7 +214,6 @@ function _amp_show_load_errors_admin_notice() {
return;
}


/**
* Print admin notice if plugin installed with incorrect slug (which impacts WordPress's auto-update system).
*
Expand Down Expand Up @@ -270,304 +269,6 @@ function _amp_xdebug_admin_notice() {

register_activation_hook( __FILE__, 'amp_activate' );

/**
* Handle activation of plugin.
*
* @since 0.2
*/
function amp_activate() {
amp_after_setup_theme();
if ( ! did_action( 'amp_init' ) ) {
amp_init();
}
flush_rewrite_rules();
}

register_deactivation_hook( __FILE__, 'amp_deactivate' );

/**
* Handle deactivation of plugin.
*
* @since 0.2
*/
function amp_deactivate() {
// We need to manually remove the amp endpoint.
global $wp_rewrite;
foreach ( $wp_rewrite->endpoints as $index => $endpoint ) {
if ( amp_get_slug() === $endpoint[1] ) {
unset( $wp_rewrite->endpoints[ $index ] );
break;
}
}

flush_rewrite_rules( false );
}

// The plugins_loaded action is the earliest we can run this since that is when pluggable.php has been required and wp_hash() is available.
add_action( 'plugins_loaded', [ 'AMP_Validation_Manager', 'init_validate_request' ], ~PHP_INT_MAX );

/*
* Register AMP scripts regardless of whether AMP is enabled or it is the AMP endpoint
* for the sake of being able to use AMP components on non-AMP documents ("dirty AMP").
*/
add_action( 'wp_default_scripts', 'amp_register_default_scripts' );

// Ensure async and custom-element/custom-template attributes are present on script tags.
add_filter( 'script_loader_tag', 'amp_filter_script_loader_tag', PHP_INT_MAX, 2 );

// Ensure crossorigin=anonymous is added to font links.
add_filter( 'style_loader_tag', 'amp_filter_font_style_loader_tag_with_crossorigin_anonymous', 10, 4 );

/**
* Set up AMP.
*
* This function must be invoked through the 'after_setup_theme' action to allow
* the AMP setting to declare the post types support earlier than plugins/theme.
*
* @since 0.6
*/
function amp_after_setup_theme() {
amp_get_slug(); // Ensure AMP_QUERY_VAR is set.

/**
* Filters whether AMP is enabled on the current site.
*
* Useful if the plugin is network activated and you want to turn it off on select sites.
*
* @since 0.2
*/
if ( false === apply_filters( 'amp_is_enabled', true ) ) {
return;
}

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
*/
function amp_init() {

/**
* Triggers on init when AMP plugin is active.
*
* @since 0.3
*/
do_action( 'amp_init' );

add_filter( 'allowed_redirect_hosts', array( 'AMP_HTTP', 'filter_allowed_redirect_hosts' ) );
AMP_HTTP::purge_amp_query_vars();
AMP_HTTP::send_cors_headers();
AMP_HTTP::handle_xhr_request();
AMP_Theme_Support::init();
AMP_Validation_Manager::init();
AMP_Service_Worker::init();
add_action( 'admin_init', 'AMP_Options_Manager::register_settings' );
add_action( 'wp_loaded', 'amp_add_options_menu' );
add_action( 'wp_loaded', 'amp_bootstrap_admin' );

add_rewrite_endpoint( amp_get_slug(), EP_PERMALINK );
AMP_Post_Type_Support::add_post_type_support();
add_action( 'init', array( 'AMP_Post_Type_Support', 'add_post_type_support' ), 1000 ); // After post types have been defined.
add_action( 'parse_query', 'amp_correct_query_when_is_front_page' );
add_action( 'admin_bar_menu', 'amp_add_admin_bar_view_link', 100 );
add_action( 'wp_loaded', 'amp_editor_core_blocks' );
add_action( 'amp_plugin_update', 'remove_amp_story_templates' );
add_filter( 'request', 'amp_force_query_var_value' );

// 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 ( defined( 'WP_CLI' ) && WP_CLI ) {
if ( class_exists( 'WP_CLI\Dispatcher\CommandNamespace' ) ) {
WP_CLI::add_command( 'amp', 'AMP_CLI_Namespace' );
}

WP_CLI::add_command( 'amp validation', 'AMP_CLI_Validation_Command' );
}

/*
* Broadcast plugin updates.
* Note that AMP_Options_Manager::get_option( 'version', '0.0' ) cannot be used because
* version was new option added, and in that case default would never be used for a site
* upgrading from a version prior to 1.0. So this is why get_option() is currently used.
*/
$options = get_option( AMP_Options_Manager::OPTION_NAME, array() );
$old_version = isset( $options['version'] ) ? $options['version'] : '0.0';
if ( AMP__VERSION !== $old_version ) {
/**
* Triggers when after amp_init when the plugin version has updated.
*
* @param string $old_version Old version.
*/
do_action( 'amp_plugin_update', $old_version );
AMP_Options_Manager::update_option( 'version', AMP__VERSION );
}
}

/**
* Make sure the `amp` query var has an explicit value.
*
* This avoids issues when filtering the deprecated `query_string` hook.
*
* @since 0.3.3
*
* @param array $query_vars Query vars.
* @return array Query vars.
*/
function amp_force_query_var_value( $query_vars ) {
if ( isset( $query_vars[ amp_get_slug() ] ) && '' === $query_vars[ amp_get_slug() ] ) {
$query_vars[ amp_get_slug() ] = 1;
}
return $query_vars;
}

/**
* Fix up WP_Query for front page when amp query var is present.
*
* Normally the front page would not get served if a query var is present other than preview, page, paged, and cpage.
*
* @since 0.6
* @see WP_Query::parse_query()
* @link https://github.com/WordPress/wordpress-develop/blob/0baa8ae85c670d338e78e408f8d6e301c6410c86/src/wp-includes/class-wp-query.php#L951-L971
*
* @param WP_Query $query Query.
*/
function amp_correct_query_when_is_front_page( WP_Query $query ) {
$is_front_page_query = (
$query->is_main_query()
&&
$query->is_home()
&&
// Is AMP endpoint.
false !== $query->get( amp_get_slug(), false )
&&
// Is query not yet fixed uo up to be front page.
! $query->is_front_page()
&&
// Is showing pages on front.
'page' === get_option( 'show_on_front' )
&&
// Has page on front set.
get_option( 'page_on_front' )
&&
// See line in WP_Query::parse_query() at <https://github.com/WordPress/wordpress-develop/blob/0baa8ae/src/wp-includes/class-wp-query.php#L961>.
0 === count( array_diff( array_keys( wp_parse_args( $query->query ) ), array( amp_get_slug(), 'preview', 'page', 'paged', 'cpage' ) ) )
);
if ( $is_front_page_query ) {
$query->is_home = false;
$query->is_page = true;
$query->is_singular = true;
$query->set( 'page_id', get_option( 'page_on_front' ) );
}
}

/**
* Whether this is in 'canonical mode'.
*
* Themes can register support for this with `add_theme_support( AMP_Theme_Support::SLUG )`:
*
* add_theme_support( AMP_Theme_Support::SLUG );
*
* This will serve templates in AMP-first, allowing you to use AMP components in your theme templates.
* If you want to make available in transitional mode, where templates are served in AMP or non-AMP documents, do:
*
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'paired' => true,
* ) );
*
* Transitional mode is also implied if you define a template_dir:
*
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'template_dir' => 'amp',
* ) );
*
* If you want to have AMP-specific templates in addition to serving AMP-first, do:
*
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'paired' => false,
* 'template_dir' => 'amp',
* ) );
*
* If you want to force AMP to always be served on a given template, you can use the templates_supported arg,
* for example to always serve the Category template in AMP:
*
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'templates_supported' => array(
* 'is_category' => true,
* ),
* ) );
*
* Or if you want to force AMP to be used on all templates:
*
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'templates_supported' => 'all',
* ) );
*
* @see AMP_Theme_Support::read_theme_support()
* @return boolean Whether this is in AMP 'canonical' mode, that is whether it is AMP-first and there is not a separate (paired) AMP URL.
*/
function amp_is_canonical() {
if ( ! current_theme_supports( AMP_Theme_Support::SLUG ) ) {
return false;
}

$args = AMP_Theme_Support::get_theme_support_args();
if ( isset( $args[ AMP_Theme_Support::PAIRED_FLAG ] ) ) {
return empty( $args[ AMP_Theme_Support::PAIRED_FLAG ] );
}

// If there is a template_dir, then transitional mode is implied.
return empty( $args['template_dir'] );
}

/**
* Add frontend actions.
*
* @since 0.2
*/
function amp_add_frontend_actions() {
add_action( 'wp_head', 'amp_add_amphtml_link' );
}

/**
* Bootstraps the AMP customizer.
*
* Uses the priority of 12 for the 'after_setup_theme' action.
* Many themes run `add_theme_support()` on the 'after_setup_theme' hook, at the default priority of 10.
* And that function's documentation suggests adding it to that action.
* So this enables themes to `add_theme_support( AMP_Theme_Support::SLUG )`.
* And `amp_init_customizer()` will be able to recognize theme support by calling `amp_is_canonical()`.
*
* @since 0.4
*/
function _amp_bootstrap_customizer() {
add_action( 'after_setup_theme', 'amp_init_customizer', 12 );
}
add_action( 'plugins_loaded', '_amp_bootstrap_customizer', 9 ); // Should be hooked before priority 10 on 'plugins_loaded' to properly unhook core panels.

/**
* Redirects the old AMP URL to the new AMP URL.
*
* If post slug is updated the amp page with old post slug will be redirected to the updated url.
*
* @since 0.5
*
* @param string $link New URL of the post.
* @return string URL to be redirected.
*/
function amp_redirect_old_slug_to_new_url( $link ) {

if ( is_amp_endpoint() && ! amp_is_canonical() ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
$link = add_query_arg( amp_get_slug(), '', $link );
} else {
$link = trailingslashit( trailingslashit( $link ) . amp_get_slug() );
}
}

return $link;
}
amp_bootstrap_plugin();
Loading

0 comments on commit e6e86dc

Please sign in to comment.