Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/Automattic/amp-wp into a…
Browse files Browse the repository at this point in the history
…dd/864-native-widget-support
  • Loading branch information
westonruter committed Jan 23, 2018
2 parents fc48208 + b21e5d7 commit da6cee6
Show file tree
Hide file tree
Showing 50 changed files with 887 additions and 522 deletions.
63 changes: 53 additions & 10 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function amp_after_setup_theme() {
add_filter( 'amp_post_template_analytics', 'amp_add_custom_analytics' );
add_action( 'wp_loaded', 'amp_post_meta_box' );
add_action( 'wp_loaded', 'amp_add_options_menu' );
add_action( 'parse_query', 'amp_correct_query_when_is_front_page' );
AMP_Post_Type_Support::add_post_type_support();
}
add_action( 'after_setup_theme', 'amp_after_setup_theme', 5 );
Expand Down Expand Up @@ -124,6 +125,7 @@ function amp_force_query_var_value( $query_vars ) {
* If the request is for an AMP page and this is in 'canonical mode,' redirect to the non-AMP page.
* It won't need this plugin's template system, nor the frontend actions like the 'rel' link.
*
* @global WP_Query $wp_query
* @since 0.2
* @return void
*/
Expand All @@ -141,19 +143,20 @@ function amp_maybe_add_actions() {
}

// The remaining logic here is for paired mode running in themes that don't support AMP, the template system in AMP<=0.6.
if ( ! is_singular() || is_feed() ) {
global $wp_query;
if ( ! ( is_singular() || $wp_query->is_posts_page ) || is_feed() ) {
return;
}

// Cannot use `get_queried_object` before canonical redirect; see <https://core.trac.wordpress.org/ticket/35344>.
global $wp_query;
$post = $wp_query->post;

$supports = post_supports_amp( $post );

if ( ! $supports ) {
if ( $is_amp_endpoint && isset( $post->ID ) ) {
wp_safe_redirect( get_permalink( $post->ID ), 301 );
/**
* Queried post object.
*
* @var WP_Post $post
*/
$post = get_queried_object();
if ( ! post_supports_amp( $post ) ) {
if ( $is_amp_endpoint ) {
wp_safe_redirect( get_permalink( $post->ID ), 302 ); // Temporary redirect because AMP may be supported in future.
exit;
}
return;
Expand All @@ -166,6 +169,46 @@ function amp_maybe_add_actions() {
}
}

/**
* 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_QUERY_VAR, 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_QUERY_VAR, '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.'
*
Expand Down
26 changes: 21 additions & 5 deletions assets/js/amp-customize-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,29 @@ var ampCustomizeControls = ( function( api, $ ) {
component.boot = function boot( data ) {
component.data = data;

// Defaults.
function initPanel() {
api.panel( component.data.panelId, component.panelReady );
}

if ( api.state ) {
component.addState();
api.bind( 'ready', initPanel );
} else { // WP<4.9.
api.bind( 'ready', function() {
component.addState(); // Needed for WP<4.9.
initPanel();
} );
}
};

/**
* Add state for AMP.
*
* @returns {void}
*/
component.addState = function addState() {
api.state.add( 'ampEnabled', new api.Value( false ) );
api.state.add( 'ampAvailable', new api.Value( false ) );

api.bind( 'ready', function() {
api.panel( component.data.panelId, component.panelReady );
} );
};

/**
Expand Down
14 changes: 9 additions & 5 deletions assets/js/amp-post-meta-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ var ampPostMetaBox = ( function( $ ) {
* @since 0.6
*/
data: {
canonical: false,
canonical: false, // Overridden by amp_is_canonical().
previewLink: '',
disabled: false,
enabled: true, // Overridden by post_supports_amp( $post ).
canSupport: true, // Overridden by count( AMP_Post_Type_Support::get_support_errors( $post ) ) === 0.
statusInputName: '',
l10n: {
ampPreviewBtnLabel: ''
Expand Down Expand Up @@ -59,7 +60,8 @@ var ampPostMetaBox = ( function( $ ) {
component.boot = function boot( data ) {
component.data = data;
$( document ).ready( function() {
if ( ! component.data.disabled && ! component.data.canonical ) {
component.statusRadioInputs = $( '[name="' + component.data.statusInputName + '"]' );
if ( component.data.enabled && ! component.data.canonical ) {
component.addPreviewButton();
}
component.listen();
Expand All @@ -78,8 +80,10 @@ var ampPostMetaBox = ( function( $ ) {
component.onAmpPreviewButtonClick();
} );

component.statusRadioInputs.prop( 'disabled', true ); // Prevent cementing setting default status as overridden status.
$( '.edit-amp-status, [href="#amp_status"]' ).click( function( e ) {
e.preventDefault();
component.statusRadioInputs.prop( 'disabled', false );
component.toggleAmpStatus( $( e.target ) );
} );

Expand Down Expand Up @@ -148,7 +152,7 @@ var ampPostMetaBox = ( function( $ ) {

// Don't modify status on cancel button click.
if ( ! $target.hasClass( 'button-cancel' ) ) {
status = $( '[name="' + component.data.statusInputName + '"]:checked' ).val();
status = component.statusRadioInputs.filter( ':checked' ).val();
}

$checked = $( '#amp-status-' + status );
Expand All @@ -162,7 +166,7 @@ var ampPostMetaBox = ( function( $ ) {
$container.slideToggle( component.toggleSpeed );

// Update status.
if ( ! component.data.disabled ) {
if ( component.data.canSupport ) {
$container.data( 'amp-status', status );
$checked.prop( 'checked', true );
$( '.amp-status-text' ).text( $checked.next().text() );
Expand Down
64 changes: 47 additions & 17 deletions bin/amphtml-update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Follow the steps below to generate a new version of the allowed tags class:
- Download a copy of the latet AMPHTML repository from github:
git clone [email protected]:ampproject/amphtml.git
- Copy this file into the repo's validator subdirectory:
Expand Down Expand Up @@ -149,6 +149,8 @@ def GenerateHeaderPHP(out):
out.append(' * Note: This file only contains tags that are relevant to the `body` of')
out.append(' * an AMP page. To include additional elements modify the variable')
out.append(' * `mandatory_parent_blacklist` in the amp_wp_build.py script.')
out.append(' *')
out.append(' * phpcs:ignoreFile')
out.append(' */')
out.append('class AMP_Allowed_Tags_Generated {')
out.append('')
Expand Down Expand Up @@ -223,15 +225,15 @@ def GenerateAttributesPHP(out, attributes, indent_level = 4):
indent = ''
for i in range(0,indent_level):
indent += '\t'

sorted_attributes = sorted(attributes.items())
for (attribute, values) in collections.OrderedDict(sorted_attributes).iteritems():
logging.info('generating php for attribute: %s...' % attribute.lower())
out.append('%s\'%s\' => array(' % (indent, attribute.lower()))
GeneratePropertiesPHP(out, values)
out.append('%s),' % indent)
logging.info('...done with: %s' % attribute.lower())

out.append('')
logging.info('... done')

Expand Down Expand Up @@ -305,26 +307,42 @@ def GenerateFooterPHP(out):
logging.info('entering ...')

# Output the footer.
out.append('\tpublic static function get_allowed_tags() {')
out.append('\t\treturn self::$allowed_tags;')
out.append('\t}')
out.append('')
out.append('''
/**
* Get allowed tags.
*
* @since 0.5
* @return array Allowed tags.
*/
public static function get_allowed_tags() {
return self::$allowed_tags;
}
/**
* Get list of globally-allowed attributes.
*
* @since 0.5
* @return array Allowed tag.
*/
public static function get_allowed_attributes() {
return self::$globally_allowed_attrs;
}
/**
* Get layout attributes.
*
* @since 0.5
* @return array Allowed tag.
*/
public static function get_layout_attributes() {
return self::$layout_allowed_attrs;
}''')

out.append('\tpublic static function get_allowed_attributes() {')
out.append('\t\treturn self::$globally_allowed_attrs;')
out.append('\t}')
out.append('')

out.append('\tpublic static function get_layout_attributes() {')
out.append('\t\treturn self::$layout_allowed_attrs;')
out.append('\t}')
out.append('')

out.append('}')
out.append('')

out.append('?>')
out.append('')
logging.info('... done')


Expand Down Expand Up @@ -434,6 +452,18 @@ def GetTagRules(tag_spec):
also_requires_tag_list.append(UnicodeEscape(also_requires_tag))
tag_rules['also_requires_tag'] = {'also_requires_tag': also_requires_tag_list}

if hasattr(tag_spec, 'requires_extension') and len( tag_spec.requires_extension ) != 0:
requires_extension_list = []
for requires_extension in tag_spec.requires_extension:
requires_extension_list.append(requires_extension)
tag_rules['requires_extension'] = {'requires_extension': requires_extension_list}

if hasattr(tag_spec, 'also_requires_tag_warning') and len( tag_spec.also_requires_tag_warning ) != 0:
also_requires_tag_warning_list = []
for also_requires_tag_warning in tag_spec.also_requires_tag_warning:
also_requires_tag_warning_list.append(also_requires_tag_warning)
tag_rules['also_requires_tag_warning'] = {'also_requires_tag_warning': also_requires_tag_warning_list}

if tag_spec.disallowed_ancestor:
disallowed_ancestor_list = []
for disallowed_ancestor in tag_spec.disallowed_ancestor:
Expand Down
3 changes: 2 additions & 1 deletion bin/amphtml-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ fi

# Run script.
python amphtml-update.py
cp amp_wp/class-amp-allowed-tags-generated.php ../../../includes/sanitizers/
mv amp_wp/class-amp-allowed-tags-generated.php ../../../includes/sanitizers/
rm -r amp_wp
Loading

0 comments on commit da6cee6

Please sign in to comment.