Skip to content

Commit

Permalink
Merge pull request #825 from Automattic/feature/176-page-support
Browse files Browse the repository at this point in the history
Add page support
  • Loading branch information
westonruter authored Dec 8, 2017
2 parents eeb502b + 34a0e9e commit edd7a40
Show file tree
Hide file tree
Showing 17 changed files with 489 additions and 99 deletions.
8 changes: 6 additions & 2 deletions assets/css/amp-post-meta-box.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@
margin: 0 8px 0 1px;
}

#amp-status-select {
margin-top: 4px;
#amp-status-select fieldset {
margin: 7px 0 0 1px;
}

#amp-status-select .notice {
margin: 10px 0 -5px 3px;
}

.amp-status-actions {
Expand Down
8 changes: 5 additions & 3 deletions assets/js/amp-post-meta-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ var ampPostMetaBox = ( function( $ ) {
$container.slideToggle( component.toggleSpeed );

// Update status.
$container.data( 'amp-status', status );
$checked.prop( 'checked', true );
$( '.amp-status-text' ).text( $checked.next().text() );
if ( ! component.data.disabled ) {
$container.data( 'amp-status', status );
$checked.prop( 'checked', true );
$( '.amp-status-text' ).text( $checked.next().text() );
}
};

return component;
Expand Down
39 changes: 28 additions & 11 deletions includes/admin/class-amp-post-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ public function init() {
add_filter( 'preview_post_link', array( $this, 'preview_post_link' ) );
}

/**
* Get whether AMP is available for a given post.
*
* This is just calling `post_supports_amp()` but ignoring any user-supplied opt-out for AMP.
*
* @since 0.6
* @see post_supports_amp()
*
* @param WP_Post $post Post.
* @return bool Whether or not AMP is available.
*/
protected function is_amp_available( $post ) {
$support_errors = AMP_Post_Type_Support::get_support_errors( $post );
if ( empty( $support_errors ) ) {
return true;
}
if ( 1 === count( $support_errors ) && 'post-disabled' === $support_errors[0] ) {
return true;
}
return false;
}

/**
* Enqueue admin assets.
*
Expand All @@ -77,8 +99,6 @@ public function enqueue_admin_assets() {
isset( $screen->base )
&&
'post' === $screen->base
&&
post_type_supports( $post->post_type, AMP_QUERY_VAR )
);
if ( ! $validate ) {
return;
Expand All @@ -102,7 +122,7 @@ public function enqueue_admin_assets() {
wp_add_inline_script( self::ASSETS_HANDLE, sprintf( 'ampPostMetaBox.boot( %s );',
wp_json_encode( array(
'previewLink' => esc_url_raw( add_query_arg( AMP_QUERY_VAR, '', get_preview_post_link( $post ) ) ),
'disabled' => (bool) get_post_meta( $post->ID, self::DISABLED_POST_META_KEY, true ),
'disabled' => (bool) get_post_meta( $post->ID, self::DISABLED_POST_META_KEY, true ) || ! $this->is_amp_available( $post ),
'statusInputName' => self::STATUS_INPUT_NAME,
'l10n' => array(
'ampPreviewBtnLabel' => __( 'Preview changes in AMP (opens in new window)', 'amp' ),
Expand All @@ -121,25 +141,22 @@ public function render_status( $post ) {
$verify = (
isset( $post->ID )
&&
isset( $post->post_type )
&&
post_type_supports( $post->post_type, AMP_QUERY_VAR )
&&
current_user_can( 'edit_post', $post->ID )
);

if ( true !== $verify ) {
return;
}

// The following variables are used inside amp-status.php template.
$disabled = (bool) get_post_meta( $post->ID, self::DISABLED_POST_META_KEY, true );
$status = $disabled ? 'disabled' : 'enabled';
$labels = array(
$available = $this->is_amp_available( $post );
$disabled = (bool) get_post_meta( $post->ID, self::DISABLED_POST_META_KEY, true );
$status = $disabled || ! $available ? 'disabled' : 'enabled';
$labels = array(
'enabled' => __( 'Enabled', 'amp' ),
'disabled' => __( 'Disabled', 'amp' ),
);

// The preceding variables are used inside the following amp-status.php template.
include_once AMP__DIR__ . '/templates/admin/amp-status.php';
}

Expand Down
65 changes: 34 additions & 31 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
<?php

/**
* Get AMP permalink.
*
* @since 0.1
*
* @param int $post_id Post ID.
* @return string AMP permalink.
*/
function amp_get_permalink( $post_id ) {

/**
* Filters the AMP permalink to short-circuit normal generation.
*
* Returning a non-false value in this filter will cause the `get_permalink()` to get called and the `amp_get_permalink` filter to not apply.
*
* @since 0.4
*
* @param false $url Short-circuited URL.
* @param int $post_id Post ID.
*/
$pre_url = apply_filters( 'amp_pre_get_permalink', false, $post_id );

if ( false !== $pre_url ) {
return $pre_url;
}

$parsed_url = wp_parse_url( get_permalink( $post_id ) );
$structure = get_option( 'permalink_structure' );
if ( empty( $structure ) ) {
$amp_url = add_query_arg( AMP_QUERY_VAR, 1, get_permalink( $post_id ) );
if ( empty( $structure ) || ! empty( $parsed_url['query'] ) || is_post_type_hierarchical( get_post_type( $post_id ) ) ) {
$amp_url = add_query_arg( AMP_QUERY_VAR, '', get_permalink( $post_id ) );
} else {
$amp_url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( AMP_QUERY_VAR, 'single_amp' );
}

/**
* Filters AMP permalink.
*
* @since 0.2
*
* @param false $amp_url AMP URL.
* @param int $post_id Post ID.
*/
return apply_filters( 'amp_get_permalink', $amp_url, $post_id );
}

/**
* Determine whether a given post supports AMP.
*
* @since 0.1
* @since 0.6 Returns false when post has meta to disable AMP or when page is homepage or page for posts.
* @see AMP_Post_Type_Support::get_support_errors()
*
* @param WP_Post $post Post.
*
* @return bool Whether the post supports AMP.
*/
function post_supports_amp( $post ) {

// Because `add_rewrite_endpoint` doesn't let us target specific post_types.
if ( ! post_type_supports( $post->post_type, AMP_QUERY_VAR ) ) {
return false;
}

// Skip based on postmeta.
if ( ! isset( $post->ID ) || (bool) get_post_meta( $post->ID, AMP_Post_Meta_Box::DISABLED_POST_META_KEY, true ) ) {
return false;
}

if ( post_password_required( $post ) ) {
return false;
}

/**
* Filters whether to skip the post from AMP.
*
* @since 0.3
*
* @param bool $skipped Skipped.
* @param int $post_id Post ID.
* @param WP_Post $post Post.
*/
if ( true === apply_filters( 'amp_skip_post', false, $post->ID, $post ) ) {
return false;
}

return true;
return 0 === count( AMP_Post_Type_Support::get_support_errors( $post ) );
}

/**
Expand Down
32 changes: 20 additions & 12 deletions includes/class-amp-post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ public function get_customizer_setting( $name, $default = null ) {
}

public function load() {
$this->load_parts( array( 'single' ) );
$template = is_page() ? 'page' : 'single';
$this->load_parts( array( $template ) );
}

public function load_parts( $templates ) {
Expand Down Expand Up @@ -238,6 +239,11 @@ private function merge_data_for_key( $key, $value ) {
}
}

/**
* Build post data.
*
* @since 0.2
*/
private function build_post_data() {
$post_title = get_the_title( $this->ID );
$post_publish_timestamp = get_the_date( 'U', $this->ID );
Expand All @@ -254,21 +260,23 @@ private function build_post_data() {
) );

$metadata = array(
'@context' => 'http://schema.org',
'@type' => 'BlogPosting',
'@context' => 'http://schema.org',
'@type' => is_page() ? 'WebPage' : 'BlogPosting',
'mainEntityOfPage' => $this->get( 'canonical_url' ),
'publisher' => array(
'publisher' => array(
'@type' => 'Organization',
'name' => $this->get( 'blog_name' ),
),
'headline' => $post_title,
'datePublished' => date( 'c', $post_publish_timestamp ),
'dateModified' => date( 'c', $post_modified_timestamp ),
'author' => array(
'@type' => 'Person',
'name' => $post_author->display_name,
'name' => $this->get( 'blog_name' ),
),
'headline' => $post_title,
'datePublished' => date( 'c', $post_publish_timestamp ),
'dateModified' => date( 'c', $post_modified_timestamp ),
);
if ( $post_author ) {
$metadata['author'] = array(
'@type' => 'Person',
'name' => html_entity_decode( $post_author->display_name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
);
}

$site_icon_url = $this->get( 'site_icon_url' );
if ( $site_icon_url ) {
Expand Down
55 changes: 54 additions & 1 deletion includes/class-amp-post-type-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function init() {
* @return string[] Post types.
*/
public static function get_builtin_supported_post_types() {
return array_filter( array( 'post' ), 'post_type_exists' );
return array_filter( array( 'post', 'page' ), 'post_type_exists' );
}

/**
Expand Down Expand Up @@ -63,4 +63,57 @@ public static function add_post_type_support() {
add_post_type_support( $post_type, AMP_QUERY_VAR );
}
}

/**
* Return error codes for why a given post does not have AMP support.
*
* @since 0.6
*
* @param WP_Post|int $post Post.
* @return array Error codes for why a given post does not have AMP support.
*/
public static function get_support_errors( $post ) {
if ( ! ( $post instanceof WP_Post ) ) {
$post = get_post( $post );
}
$errors = array();

// Because `add_rewrite_endpoint` doesn't let us target specific post_types.
if ( ! post_type_supports( $post->post_type, AMP_QUERY_VAR ) ) {
$errors[] = 'post-type-support';
}

// Skip based on postmeta.
if ( ! isset( $post->ID ) || (bool) get_post_meta( $post->ID, AMP_Post_Meta_Box::DISABLED_POST_META_KEY, true ) ) {
$errors[] = 'post-disabled';
}

// Homepage and page for posts are not supported yet.
if ( 'page' === get_post_type( $post ) && 'page' === get_option( 'show_on_front' ) ) {
if ( (int) get_option( 'page_for_posts' ) === (int) $post->ID ) {
$errors[] = 'page-for-posts';
} elseif ( (int) get_option( 'page_on_front' ) === (int) $post->ID ) {
$errors[] = 'page-on-front';
}
}

if ( post_password_required( $post ) ) {
$errors[] = 'password-protected';
}

/**
* Filters whether to skip the post from AMP.
*
* @since 0.3
*
* @param bool $skipped Skipped.
* @param int $post_id Post ID.
* @param WP_Post $post Post.
*/
if ( true === apply_filters( 'amp_skip_post', false, $post->ID, $post ) ) {
$errors[] = 'skip-post';
}

return $errors;
}
}
7 changes: 3 additions & 4 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ Many plugins are adding AMP support already. If you handling analytics yourself,

The best place to start is to open a new discussion in the [support forum](https://wordpress.org/support/plugin/amp) with details on what the specific validation error is.

= Why aren't Pages supported yet =

A wise green Yoda once said, "Patience you must have, my young padawan." We're working on it :)

== Changelog ==

= 0.6.0 (unreleased) =

- Add support for the "page" post type, except when used as homepage or page for posts. A new `page.php` is introduced with template parts factored out (`html-start.php`, `header.php`, `footer.php`, `html-end.php`) and re-used from `single.php`. Note that AMP URLs will end in `?amp` instead of `/amp/`. See [#825](https://github.com/Automattic/amp-wp/pull/825). Props technosailor, ThierryA, westonruter.
- Add AMP post preview button alongside non-AMP preview button. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter.
- Add ability to disable AMP on a per-post basis via toggle in publish metabox. See [#813](https://github.com/Automattic/amp-wp/pull/813). Props ThierryA, westonruter.
- Add AMP settings admin screen for managing which post types have AMP support, eliminating the requirement to add `add_post_type_support()` calls in theme or plugin. See [#811](https://github.com/Automattic/amp-wp/pull/811). Props ThierryA, westonruter.
- Add generator meta tag for AMP. See [#810](https://github.com/Automattic/amp-wp/pull/810). Props vaporwavre.
- Add code quality checking via phpcs, eslint, jscs, and jshint. See [#795](https://github.com/Automattic/amp-wp/pull/795). Props westonruter.
Expand Down
Loading

0 comments on commit edd7a40

Please sign in to comment.