Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AMP modes #2550

Merged
merged 21 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bb506c3
Always allow switching themes with built-in Transitional support to N…
westonruter Jun 8, 2019
7952fce
Prevent AMP validation from appearing in Reader mode on non-Story posts
westonruter Jun 8, 2019
a24e715
Remove dead options from validated environment
westonruter Jun 8, 2019
0ba0495
Move AMP Stories section setting right after template mode
westonruter Jun 9, 2019
d7680cb
Add AMP experiences checkboxes
westonruter Jun 10, 2019
98efeb0
Conditionally do functionality for website and stories of experienced…
westonruter Jun 10, 2019
6f8a8d8
Ensure rewrite rules are flushed properly when toggling website exper…
westonruter Jun 10, 2019
07cbb55
Replace most Native instances with Standard
westonruter Jun 10, 2019
ea75efc
Hide validation screens when website experience disabled
westonruter Jun 10, 2019
f180458
Fix typo in validation message
westonruter Jun 10, 2019
55f1f1f
Add experiences to generator meta tag; use new mode identifiers
westonruter Jun 10, 2019
4d7920f
Upate constants for modes, use instead of strings
westonruter Jun 10, 2019
00a756a
Eliminate obsolete enable_amp_stories option
westonruter Jun 10, 2019
05dda18
Fix availability of Latest Stories block when Website experience disa…
westonruter Jun 10, 2019
6e12cf1
Update website experience description
westonruter Jun 11, 2019
2704a26
Use AMP-first terminology
westonruter Jun 11, 2019
bbc6f0f
Further clarify comments
westonruter Jun 11, 2019
9737cd7
Ensure auto-accepting sanitization is always performed for stories; s…
westonruter Jun 11, 2019
5ff39d2
Do not modify editor styles for non-story post types
swissspidy Jun 11, 2019
5589b0e
Simplify the block editor JS
swissspidy Jun 11, 2019
631bcd6
“AMP first” -> “AMP-first” everywhere
swissspidy Jun 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ function amp_init() {
add_action( 'admin_init', 'AMP_Options_Manager::register_settings' );
add_action( 'wp_loaded', 'amp_add_options_menu' );
add_action( 'wp_loaded', 'amp_admin_pointer' );
add_action( 'wp_loaded', 'amp_post_meta_box' ); // Used in both Website and Stories experiences.

if ( AMP_Options_Manager::is_website_experience_enabled() ) {
add_rewrite_endpoint( amp_get_slug(), EP_PERMALINK );
Expand All @@ -351,7 +352,6 @@ function amp_init() {
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( 'wp_loaded', 'amp_post_meta_box' );
add_filter( 'request', 'amp_force_query_var_value' );

// Add actions for reader mode templates.
Expand Down
47 changes: 30 additions & 17 deletions assets/src/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ import './store';

const { ampLatestStoriesBlockData } = window;

addFilter( 'blocks.registerBlockType', 'ampEditorBlocks/addAttributes', addAMPAttributes );
addFilter( 'blocks.getSaveElement', 'ampEditorBlocks/filterSave', filterBlocksSave );
addFilter( 'editor.BlockEdit', 'ampEditorBlocks/filterEdit', filterBlocksEdit, 20 );
addFilter( 'blocks.getSaveContent.extraProps', 'ampEditorBlocks/addExtraAttributes', addAMPExtraProps );
addFilter( 'editor.PostFeaturedImage', 'ampEditorBlocks/withFeaturedImageNotice', withFeaturedImageNotice );
addFilter( 'editor.MediaUpload', 'ampEditorBlocks/addCroppedFeaturedImage', ( InitialMediaUpload ) => withCroppedFeaturedImage( InitialMediaUpload, getMinimumFeaturedImageDimensions() ) );
// Add filters if AMP for Website experience is enabled.
if ( select( 'amp/block-editor' ).isWebsiteEnabled() ) {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
addFilter( 'blocks.registerBlockType', 'ampEditorBlocks/addAttributes', addAMPAttributes );
addFilter( 'blocks.getSaveElement', 'ampEditorBlocks/filterSave', filterBlocksSave );
addFilter( 'editor.BlockEdit', 'ampEditorBlocks/filterEdit', filterBlocksEdit, 20 );
addFilter( 'blocks.getSaveContent.extraProps', 'ampEditorBlocks/addExtraAttributes', addAMPExtraProps );
addFilter( 'editor.PostFeaturedImage', 'ampEditorBlocks/withFeaturedImageNotice', withFeaturedImageNotice );
addFilter( 'editor.MediaUpload', 'ampEditorBlocks/addCroppedFeaturedImage', ( InitialMediaUpload ) => withCroppedFeaturedImage( InitialMediaUpload, getMinimumFeaturedImageDimensions() ) );

const plugins = require.context( './plugins', true, /.*\.js$/ );
const plugins = require.context( './plugins', true, /.*\.js$/ );

plugins.keys().forEach( ( modulePath ) => {
const { name, render, icon } = plugins( modulePath );
plugins.keys().forEach( ( modulePath ) => {
const { name, render, icon } = plugins( modulePath );

registerPlugin( name, { icon, render } );
} );
registerPlugin( name, { icon, render } );
} );
}

/*
* If there's no theme support, unregister blocks that are only meant for AMP.
Expand All @@ -52,14 +55,24 @@ const blocks = require.context( './blocks', true, /(?<!test\/)index\.js$/ );
blocks.keys().forEach( ( modulePath ) => {
const { name, settings } = blocks( modulePath );

// Prevent registering latest-stories block if not enabled.
if ( 'amp/amp-latest-stories' === name && typeof ampLatestStoriesBlockData === 'undefined' ) {
return;
}
const isLatestStoriesBlock = 'amp/amp-latest-stories' === name;

const blockRequiresAmp = AMP_DEPENDENT_BLOCKS.includes( name );
const shouldRegister = (
(
select( 'amp/block-editor' ).isWebsiteEnabled() &&
(
( ! isLatestStoriesBlock && select( 'amp/block-editor' ).isStandardMode() && AMP_DEPENDENT_BLOCKS.includes( name ) ) ||
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
( isLatestStoriesBlock && select( 'amp/block-editor' ).isStoriesEnabled() )
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
)
) ||
(
select( 'amp/block-editor' ).isStoriesEnabled() &&
isLatestStoriesBlock &&
typeof ampLatestStoriesBlockData !== 'undefined'
)
);

if ( ! blockRequiresAmp || select( 'amp/block-editor' ).isNativeAMP() ) {
if ( shouldRegister ) {
registerBlockType( name, settings );
}
} );
30 changes: 25 additions & 5 deletions assets/src/block-editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,36 @@ export function hasThemeSupport( state ) {
}

/**
* Returns whether the current site uses AMP first (as opposed to paired).
*
* @todo Rename to isStandardAMP or isAMPFirst?
* Returns whether the current site is in Standard mode (AMP first) as opposed to Transitional (paired).
*
* @param {Object} state Editor state.
*
* @return {boolean} Whether the current site uses AMP first.
westonruter marked this conversation as resolved.
Show resolved Hide resolved
*/
export function isNativeAMP( state ) {
return Boolean( state.isNativeAMP );
export function isStandardMode( state ) {
return Boolean( state.isStandardMode );
}

/**
* Returns whether the website experience is enabled.
*
* @param {Object} state Editor state.
*
* @return {boolean} Whether website experienced enabled.
*/
export function isWebsiteEnabled( state ) {
return Boolean( state.isWebsiteEnabled );
}

/**
* Returns whether the stories experience is enabled.
*
* @param {Object} state Editor state.
*
* @return {boolean} Whether stories experienced enabled.
*/
export function isStoriesEnabled( state ) {
return Boolean( state.isStoriesEnabled );
}

export function getDefaultStatus( state ) {
Expand Down
4 changes: 3 additions & 1 deletion includes/admin/class-amp-post-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ public function enqueue_block_assets() {
'possibleStatuses' => array( self::ENABLED_STATUS, self::DISABLED_STATUS ),
'defaultStatus' => $enabled_status,
'errorMessages' => $error_messages,
'isWebsiteEnabled' => AMP_Options_Manager::is_website_experience_enabled(),
'isStoriesEnabled' => AMP_Options_Manager::is_stories_experience_enabled(),
'hasThemeSupport' => current_theme_supports( AMP_Theme_Support::SLUG ),
'isNativeAMP' => amp_is_canonical(),
'isStandardMode' => amp_is_canonical(),
);

wp_localize_script(
Expand Down