Skip to content

Commit

Permalink
Use ScannableURLs REST endpoint for getting preview URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
delawski committed Oct 13, 2021
1 parent 2c9e080 commit ffe1c0e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 102 deletions.
13 changes: 5 additions & 8 deletions assets/src/onboarding-wizard/pages/done/use-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@
*/
import { useContext, useMemo, useState } from '@wordpress/element';

/**
* External dependencies
*/
import { PREVIEW_URLS } from 'amp-settings'; // From WP inline script.

/**
* Internal dependencies
*/
import { Options } from '../../../components/options-context-provider';
import { SiteScan } from '../../../components/site-scan-context-provider';
import { STANDARD } from '../../../common/constants';

export function usePreview() {
const { scannableUrls } = useContext( SiteScan );
const { editedOptions: { theme_support: themeSupport } } = useContext( Options );
const [ isPreviewingAMP, setIsPreviewingAMP ] = useState( themeSupport !== STANDARD );
const [ previewedPageType, setPreviewedPageType ] = useState( PREVIEW_URLS[ 0 ].type );
const [ previewedPageType, setPreviewedPageType ] = useState( scannableUrls[ 0 ].type );

const toggleIsPreviewingAMP = () => setIsPreviewingAMP( ( mode ) => ! mode );
const setActivePreviewLink = ( link ) => setPreviewedPageType( link.type );

const previewLinks = useMemo( () => PREVIEW_URLS.map( ( { url, amp_url: ampUrl, type, label } ) => ( {
const previewLinks = useMemo( () => scannableUrls.map( ( { url, amp_url: ampUrl, type, label } ) => ( {
type,
label,
url: isPreviewingAMP ? ampUrl : url,
isActive: type === previewedPageType,
} ) ), [ isPreviewingAMP, previewedPageType ] );
} ) ), [ isPreviewingAMP, previewedPageType, scannableUrls ] );

const previewUrl = useMemo( () => previewLinks.find( ( link ) => link.isActive )?.url, [ previewLinks ] );

Expand Down
45 changes: 9 additions & 36 deletions src/Admin/OnboardingWizardSubmenuPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,19 @@ final class OnboardingWizardSubmenuPage implements Delayed, Registerable, Servic
*/
private $loading_error;

/**
* ScannableURLProvider instance.
*
* @var ScannableURLProvider
*/
private $scannable_url_provider;

/**
* OnboardingWizardSubmenuPage constructor.
*
* @param GoogleFonts $google_fonts An instance of the GoogleFonts service.
* @param ReaderThemes $reader_themes An instance of the ReaderThemes class.
* @param RESTPreloader $rest_preloader An instance of the RESTPreloader class.
* @param LoadingError $loading_error An instance of the LoadingError class.
* @param ScannableURLProvider $scannable_url_provider An instance of the ScannableURLProvider class.
* @param GoogleFonts $google_fonts An instance of the GoogleFonts service.
* @param ReaderThemes $reader_themes An instance of the ReaderThemes class.
* @param RESTPreloader $rest_preloader An instance of the RESTPreloader class.
* @param LoadingError $loading_error An instance of the LoadingError class.
*/
public function __construct( GoogleFonts $google_fonts, ReaderThemes $reader_themes, RESTPreloader $rest_preloader, LoadingError $loading_error, ScannableURLProvider $scannable_url_provider ) {
$this->google_fonts = $google_fonts;
$this->reader_themes = $reader_themes;
$this->rest_preloader = $rest_preloader;
$this->loading_error = $loading_error;
$this->scannable_url_provider = $scannable_url_provider;
public function __construct( GoogleFonts $google_fonts, ReaderThemes $reader_themes, RESTPreloader $rest_preloader, LoadingError $loading_error ) {
$this->google_fonts = $google_fonts;
$this->reader_themes = $reader_themes;
$this->rest_preloader = $rest_preloader;
$this->loading_error = $loading_error;
}

/**
Expand Down Expand Up @@ -257,7 +248,6 @@ public function enqueue_assets( $hook_suffix ) {
'SCANNABLE_URLS_REST_PATH' => '/amp/v1/scannable-urls',
'SETTINGS_LINK' => $amp_settings_link,
'OPTIONS_REST_PATH' => '/amp/v1/options',
'PREVIEW_URLS' => $this->get_preview_urls( $this->scannable_url_provider->get_urls() ),
'READER_THEMES_REST_PATH' => '/amp/v1/reader-themes',
'UPDATES_NONCE' => wp_create_nonce( 'updates' ),
'USER_FIELD_DEVELOPER_TOOLS_ENABLED' => UserAccess::USER_FIELD_DEVELOPER_TOOLS_ENABLED,
Expand Down Expand Up @@ -322,21 +312,4 @@ public function get_close_link() {
// Default to the AMP Settings page if a referrer link could not be determined.
return menu_page_url( AMP_Options_Manager::OPTION_NAME, false );
}

/**
* Add AMP URLs to the list of scannable URLs.
*
* @since 2.2
*
* @param array $scannable_urls Array of scannable URLs.
*
* @return array Preview URLs.
*/
public function get_preview_urls( $scannable_urls ) {
foreach ( $scannable_urls as &$scannable_url ) {
$scannable_url['amp_url'] = amp_add_paired_endpoint( $scannable_url['url'] );
}

return $scannable_urls;
}
}
20 changes: 13 additions & 7 deletions src/Validation/ScannableURLsRestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAna
return rest_ensure_response(
array_map(
static function ( $entry ) use ( $nonce ) {
$entry['validate_url'] = amp_add_paired_endpoint(
add_query_arg(
[
AMP_Validation_Manager::VALIDATE_QUERY_VAR => $nonce,
],
$entry['url']
)
$entry['amp_url'] = amp_add_paired_endpoint( $entry['url'] );
$entry['validate_url'] = add_query_arg(
[
AMP_Validation_Manager::VALIDATE_QUERY_VAR => $nonce,
],
$entry['amp_url']
);

return $entry;
Expand Down Expand Up @@ -120,6 +119,13 @@ public function get_item_schema() {
'readonly' => true,
'context' => [ 'view' ],
],
'amp_url' => [
'description' => __( 'AMP URL.', 'amp' ),
'type' => 'string',
'format' => 'uri',
'readonly' => true,
'context' => [ 'view' ],
],
'type' => [
'description' => __( 'Page type.', 'amp' ),
'type' => 'string',
Expand Down
51 changes: 0 additions & 51 deletions tests/php/src/Admin/OnboardingWizardSubmenuPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,55 +167,4 @@ public function test_get_close_link( $referrer_link_callback, $expected_referrer
$this->onboarding_wizard_submenu_page->get_close_link()
);
}

/**
* Tests OnboardingWizardSubmenuPage::get_preview_urls()
*
* @covers ::get_preview_urls()
*/
public function test_get_preview_urls() {
$scannable_urls = [
[
'type' => 'home',
'url' => 'https://example.com',
'label' => 'Homepage',
],
[
'type' => 'page',
'url' => 'https://example.com/sample-page',
'label' => 'Page',
],
[
'type' => 'search',
'url' => 'https://example.com/?s=foobar',
'label' => 'Search Results',
],
];

$expected_urls = [
[
'type' => 'home',
'url' => 'https://example.com',
'amp_url' => amp_add_paired_endpoint( 'https://example.com' ),
'label' => 'Homepage',
],
[
'type' => 'page',
'url' => 'https://example.com/sample-page',
'amp_url' => amp_add_paired_endpoint( 'https://example.com/sample-page' ),
'label' => 'Page',
],
[
'type' => 'search',
'url' => 'https://example.com/?s=foobar',
'amp_url' => amp_add_paired_endpoint( 'https://example.com/?s=foobar' ),
'label' => 'Search Results',
],
];

$this->assertEquals(
$expected_urls,
$this->onboarding_wizard_submenu_page->get_preview_urls( $scannable_urls )
);
}
}

0 comments on commit ffe1c0e

Please sign in to comment.