-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4999 from ampproject/add/reader-theme-late-query-…
…var-checking Add checking for whether AMP query var is defined too late for Reader themes
- Loading branch information
Showing
8 changed files
with
282 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* Class AmpSlugCustomizationWatcher. | ||
* | ||
* @package AmpProject\AmpWP | ||
*/ | ||
|
||
namespace AmpProject\AmpWP; | ||
|
||
use AmpProject\AmpWP\Infrastructure\Registerable; | ||
use AmpProject\AmpWP\Infrastructure\Service; | ||
|
||
/** | ||
* Service for redirecting mobile users to the AMP version of a page. | ||
* | ||
* @package AmpProject\AmpWP | ||
*/ | ||
final class AmpSlugCustomizationWatcher implements Service, Registerable { | ||
|
||
/** | ||
* Whether the slug was customized early (at plugins_loaded action, priority 8). | ||
* | ||
* @var bool | ||
*/ | ||
protected $is_customized_early = false; | ||
|
||
/** | ||
* Whether the slug was customized early (at after_setup_theme action, priority 4). | ||
* | ||
* @var bool | ||
*/ | ||
protected $is_customized_late = false; | ||
|
||
/** | ||
* Register. | ||
*/ | ||
public function register() { | ||
// This is at priority 8 because ReaderThemeLoader::override_theme runs at priority 9, which in turn runs right | ||
// before _wp_customize_include at priority 10. A slug is customized early if it is customized at priority 8. | ||
add_action( 'plugins_loaded', [ $this, 'determine_early_customization' ], 8 ); | ||
} | ||
|
||
/** | ||
* Whether the slug was customized early (at plugins_loaded action, priority 8). | ||
* | ||
* @return bool | ||
*/ | ||
public function did_customize_early() { | ||
return $this->is_customized_early; | ||
} | ||
|
||
/** | ||
* Whether the slug was customized early (at after_setup_theme action, priority 4). | ||
* | ||
* @return bool | ||
*/ | ||
public function did_customize_late() { | ||
return $this->is_customized_late; | ||
} | ||
|
||
/** | ||
* Determine if the slug was customized early. | ||
* | ||
* Early customization happens by plugins_loaded action at priority 8; this is required in order for the slug to be | ||
* used by `ReaderThemeLoader::override_theme()` which runs at priority 9; this method in turn must run before | ||
* before `_wp_customize_include()` which runs at plugins_loaded priority 10. At that point the current theme gets | ||
* determined, so for Reader themes to apply the logic in `ReaderThemeLoader` must run beforehand. | ||
*/ | ||
public function determine_early_customization() { | ||
if ( QueryVars::AMP !== amp_get_slug() ) { | ||
$this->is_customized_early = true; | ||
} else { | ||
add_action( 'after_setup_theme', [ $this, 'determine_late_customization' ], 4 ); | ||
} | ||
} | ||
|
||
/** | ||
* Determine if the slug was defined late. | ||
* | ||
* Late slug customization often happens when a theme itself defines `AMP_QUERY_VAR`. This is too late for the plugin | ||
* to be able to offer Reader themes which must have `AMP_QUERY_VAR` defined by plugins_loaded priority 9. Also, | ||
* defining `AMP_QUERY_VAR` is fundamentally incompatible since loading a Reader theme means preventing the original | ||
* theme from ever being loaded, and thus the theme's customized `AMP_QUERY_VAR` will never be read. | ||
* | ||
* This method must run before `amp_after_setup_theme()` which runs at the after_setup_theme action priority 5. In | ||
* this function, the `amp_get_slug()` function is called which will then set the query var for the remainder of the | ||
* request. | ||
* | ||
* @see amp_after_setup_theme() | ||
*/ | ||
public function determine_late_customization() { | ||
if ( QueryVars::AMP !== amp_get_slug() ) { | ||
$this->is_customized_late = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.