has_support()
&&
(
AMP_Theme_Support::TRANSITIONAL_MODE_SLUG === AMP_Options_Manager::get_option( Option::THEME_SUPPORT )
||
(
AMP_Theme_Support::READER_MODE_SLUG === AMP_Options_Manager::get_option( Option::THEME_SUPPORT )
&&
get_stylesheet() === AMP_Options_Manager::get_option( Option::READER_THEME )
)
)
&&
amp_is_dev_mode()
&&
is_user_logged_in()
);
}
/**
* Get the list of service IDs required for this service to be registered.
*
* @return string[] List of required services.
*/
public static function get_requirements() {
return [
'dependency_support',
];
}
/**
* PairedBrowsing constructor.
*
* @param UserAccess $dev_tools_user_access DevTools User Access.
* @param PairedRouting $paired_routing Paired Routing.
*/
public function __construct( UserAccess $dev_tools_user_access, PairedRouting $paired_routing ) {
$this->dev_tools_user_access = $dev_tools_user_access;
$this->paired_routing = $paired_routing;
}
/**
* Adds the filters.
*/
public function register() {
add_action( 'wp', [ $this, 'init_frontend' ], PHP_INT_MAX );
add_filter( 'amp_dev_mode_element_xpaths', [ $this, 'filter_dev_mode_element_xpaths' ] );
add_filter( 'amp_validated_url_status_actions', [ $this, 'filter_validated_url_status_actions' ], 10, 2 );
}
/**
* Filter Dev Mode XPaths to include the inline script used by the client.
*
* @param string[] $xpaths Element XPaths.
* @return string[] XPaths.
*/
public function filter_dev_mode_element_xpaths( $xpaths ) {
$xpaths[] = '//script[ @id = "amp-paired-browsing-client-js-before" ]';
return $xpaths;
}
/**
* Filter the status actions for a validated URL to add the paired browsing link.
*
* @param string[] $actions Action links.
* @param WP_Post $post AMP Validated URL post.
* @return string[] Actions.
*/
public function filter_validated_url_status_actions( $actions, WP_Post $post ) {
$actions['paired_browsing'] = sprintf(
'%s',
esc_url( $this->get_paired_browsing_url( AMP_Validated_URL_Post_Type::get_url_from_post( $post ) ) ),
esc_html__( 'Paired Browsing', 'amp' )
);
return $actions;
}
/**
* Initialize frontend.
*/
public function init_frontend() {
if ( ! amp_is_available() ) {
return;
}
if ( isset( $_GET[ self::APP_QUERY_VAR ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$this->init_app();
} else {
$this->init_client();
}
}
/**
* Set up app.
*
* This is the parent request that has the iframes for both AMP and non-AMP.
*/
public function init_app() {
add_action( 'template_redirect', [ $this, 'ensure_app_location' ] );
add_filter( 'template_include', [ $this, 'filter_template_include_for_app' ], PHP_INT_MAX );
}
/**
* Set up client.
*
* Make sure pages have the paired browsing client script so that the app can interact with it.
*/
public function init_client() {
add_action( 'admin_bar_menu', [ $this, 'add_admin_bar_menu_item' ], 102 );
/**
* Fires before registering plugin assets that may require core asset polyfills.
*
* @internal
*/
do_action( 'amp_register_polyfills' );
$handle = 'amp-paired-browsing-client';
$asset = require AMP__DIR__ . '/assets/js/amp-paired-browsing-client.asset.php';
$dependencies = $asset['dependencies'];
$version = $asset['version'];
wp_enqueue_script(
$handle,
amp_get_asset_url( '/js/amp-paired-browsing-client.js' ),
$dependencies,
$version,
true
);
$is_amp_request = amp_is_request();
$current_url = amp_get_current_url();
$amp_url = $is_amp_request ? $current_url : $this->paired_routing->add_endpoint( $current_url );
$non_amp_url = ! $is_amp_request ? $current_url : $this->paired_routing->remove_endpoint( $current_url );
wp_add_inline_script(
$handle,
sprintf(
'var ampPairedBrowsingClientData = %s;',
wp_json_encode(
[
'isAmpDocument' => $is_amp_request,
'ampUrl' => $amp_url,
'nonAmpUrl' => $non_amp_url,
]
)
),
'before'
);
// Mark enqueued script for AMP dev mode so that it is not removed.
// @todo Revisit with .
$dev_mode_handles = array_merge(
[ $handle, 'wp-i18n', 'wp-hooks', 'regenerator-runtime', 'wp-polyfill' ],
$dependencies
);
add_filter(
'script_loader_tag',
static function ( $tag, $script_handle ) use ( $dev_mode_handles ) {
if ( amp_is_request() && in_array( $script_handle, $dev_mode_handles, true ) ) {
$tag = preg_replace( '/(?<=