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

Add slug constants for theme support and post type support #1456

Merged
merged 2 commits into from
Sep 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 11 additions & 11 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function amp_force_query_var_value( $query_vars ) {
function amp_maybe_add_actions() {

// Short-circuit when theme supports AMP, as everything is handled by AMP_Theme_Support.
if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
return;
}

Expand Down Expand Up @@ -283,50 +283,50 @@ function amp_correct_query_when_is_front_page( WP_Query $query ) {
/**
* Whether this is in 'canonical mode'.
*
* Themes can register support for this with `add_theme_support( 'amp' )`:
* Themes can register support for this with `add_theme_support( AMP_Theme_Support::SLUG )`:
*
* add_theme_support( 'amp' );
* add_theme_support( AMP_Theme_Support::SLUG );
*
* This will serve templates in native AMP, allowing you to use AMP components in your theme templates.
* If you want to make available in paired mode, where templates are served in AMP or non-AMP documents, do:
*
* add_theme_support( 'amp', array(
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'paired' => true,
* ) );
*
* Paired mode is also implied if you define a template_dir:
*
* add_theme_support( 'amp', array(
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'template_dir' => 'amp',
* ) );
*
* If you want to have AMP-specific templates in addition to serving native AMP, do:
*
* add_theme_support( 'amp', array(
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'paired' => false,
* 'template_dir' => 'amp',
* ) );
*
* If you want to force AMP to always be served on a given template, you can use the templates_supported arg,
* for example to always serve the Category template in AMP:
*
* add_theme_support( 'amp', array(
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'templates_supported' => array(
* 'is_category' => true,
* ),
* ) );
*
* Or if you want to force AMP to be used on all templates:
*
* add_theme_support( 'amp', array(
* add_theme_support( AMP_Theme_Support::SLUG, array(
* 'templates_supported' => 'all',
* ) );
*
* @see AMP_Theme_Support::read_theme_support()
* @return boolean Whether this is in AMP 'canonical' mode, that is whether it is native and there is not separate AMP URL current URL.
*/
function amp_is_canonical() {
if ( ! current_theme_supports( 'amp' ) ) {
if ( ! current_theme_supports( AMP_Theme_Support::SLUG ) ) {
return false;
}

Expand Down Expand Up @@ -457,7 +457,7 @@ function amp_render_post( $post ) {
* Uses the priority of 12 for the 'after_setup_theme' action.
* Many themes run `add_theme_support()` on the 'after_setup_theme' hook, at the default priority of 10.
* And that function's documentation suggests adding it to that action.
* So this enables themes to `add_theme_support( 'amp' )`.
* So this enables themes to `add_theme_support( AMP_Theme_Support::SLUG )`.
* And `amp_init_customizer()` will be able to recognize theme support by calling `amp_is_canonical()`.
*
* @since 0.4
Expand All @@ -481,7 +481,7 @@ function _amp_bootstrap_customizer() {
function amp_redirect_old_slug_to_new_url( $link ) {

if ( is_amp_endpoint() && ! amp_is_canonical() ) {
if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
$link = add_query_arg( amp_get_slug(), '', $link );
} else {
$link = trailingslashit( trailingslashit( $link ) . amp_get_slug() );
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/class-amp-customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function enqueue_preview_scripts() {
true
);

if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
$availability = AMP_Theme_Support::get_template_availability();
$available = $availability['supported'];
} elseif ( is_singular() || $wp_query->is_posts_page ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/class-amp-editor-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function enqueue_block_editor_assets() {
wp_add_inline_script(
'amp-editor-blocks',
sprintf( 'ampEditorBlocks.boot( %s );', wp_json_encode( array(
'hasThemeSupport' => current_theme_supports( 'amp' ),
'hasThemeSupport' => current_theme_supports( AMP_Theme_Support::SLUG ),
) ) )
);

Expand Down
4 changes: 2 additions & 2 deletions includes/admin/class-amp-post-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function enqueue_admin_assets() {
AMP__VERSION
);

if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
$availability = AMP_Theme_Support::get_template_availability( $post );
$support_errors = $availability['errors'];
} else {
Expand Down Expand Up @@ -265,7 +265,7 @@ public function get_status_and_errors( $post ) {
* Checking for template availability will include a check for get_support_errors. Otherwise, if theme support is not present
* then we just check get_support_errors.
*/
if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
$availability = AMP_Theme_Support::get_template_availability( $post );
$status = $availability['supported'] ? self::ENABLED_STATUS : self::DISABLED_STATUS;
$errors = array_diff( $availability['errors'], array( 'post-status-disabled' ) ); // Subtract the status which the metabox will allow to be toggled.
Expand Down
6 changes: 3 additions & 3 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function amp_admin_get_preview_permalink() {
$post_type = (string) apply_filters( 'amp_customizer_post_type', 'post' );

// Make sure the desired post type is actually supported, and if so, prefer it.
$supported_post_types = get_post_types_by_support( amp_get_slug() );
$supported_post_types = get_post_types_by_support( AMP_Post_Type_Support::SLUG );
if ( in_array( $post_type, $supported_post_types, true ) ) {
$supported_post_types = array_unique( array_merge( array( $post_type ), $supported_post_types ) );
}
Expand All @@ -60,7 +60,7 @@ function amp_admin_get_preview_permalink() {
}

// If theme support is present, then bail if the singular template is not supported.
if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
$supported_templates = AMP_Theme_Support::get_supportable_templates();
if ( empty( $supported_templates['is_singular']['supported'] ) ) {
return null;
Expand Down Expand Up @@ -90,7 +90,7 @@ function amp_admin_get_preview_permalink() {
*/
function amp_add_customizer_link() {
/** This filter is documented in includes/settings/class-amp-customizer-design-settings.php */
if ( ! apply_filters( 'amp_customizer_is_enabled', true ) || current_theme_supports( 'amp' ) ) {
if ( ! apply_filters( 'amp_customizer_is_enabled', true ) || current_theme_supports( AMP_Theme_Support::SLUG ) ) {
return;
}

Expand Down
14 changes: 7 additions & 7 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function amp_get_current_url() {
function amp_get_permalink( $post_id ) {

// When theme support is present, the plain query var should always be used.
if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
$permalink = get_permalink( $post_id );
if ( ! amp_is_canonical() ) {
$permalink = add_query_arg( amp_get_slug(), '', $permalink );
Expand Down Expand Up @@ -177,7 +177,7 @@ function amp_add_amphtml_link() {
$current_url = amp_get_current_url();

$amp_url = null;
if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
if ( AMP_Theme_Support::is_paired_available() ) {
$amp_url = add_query_arg( amp_get_slug(), '', $current_url );
}
Expand All @@ -195,7 +195,7 @@ function amp_add_amphtml_link() {
}

// Check to see if there are known unaccepted validation errors for this URL.
if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) ) {
$validation_errors = AMP_Invalid_URL_Post_Type::get_invalid_url_validation_errors( $current_url, array( 'ignore_accepted' => true ) );
$error_count = count( $validation_errors );
if ( $error_count > 0 ) {
Expand Down Expand Up @@ -264,7 +264,7 @@ function is_amp_endpoint() {
false !== get_query_var( amp_get_slug(), false )
);

if ( ! current_theme_supports( 'amp' ) ) {
if ( ! current_theme_supports( AMP_Theme_Support::SLUG ) ) {
return $has_amp_query_var;
}

Expand Down Expand Up @@ -563,7 +563,7 @@ function amp_print_analytics( $analytics ) {
* @return array Embed handlers.
*/
function amp_get_content_embed_handlers( $post = null ) {
if ( current_theme_supports( 'amp' ) && $post ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) && $post ) {
_deprecated_argument( __FUNCTION__, '0.7', esc_html__( 'The $post argument is deprecated when theme supports AMP.', 'amp' ) );
$post = null;
}
Expand Down Expand Up @@ -614,7 +614,7 @@ function amp_get_content_embed_handlers( $post = null ) {
* @return array Embed handlers.
*/
function amp_get_content_sanitizers( $post = null ) {
if ( current_theme_supports( 'amp' ) && $post ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) && $post ) {
_deprecated_argument( __FUNCTION__, '0.7', esc_html__( 'The $post argument is deprecated when theme supports AMP.', 'amp' ) );
$post = null;
}
Expand Down Expand Up @@ -646,7 +646,7 @@ function amp_get_content_sanitizers( $post = null ) {
'add_placeholder' => true,
),
'AMP_Gallery_Block_Sanitizer' => array( // Note: Gallery block sanitizer must come after image sanitizers since itś logic is using the already sanitized images.
'carousel_required' => ! current_theme_supports( 'amp' ), // For back-compat.
'carousel_required' => ! current_theme_supports( AMP_Theme_Support::SLUG ), // For back-compat.
),
'AMP_Block_Sanitizer' => array(), // Note: Block sanitizer must come after embed / media sanitizers since it's logic is using the already sanitized content.
'AMP_Script_Sanitizer' => array(),
Expand Down
4 changes: 2 additions & 2 deletions includes/class-amp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ public function validate_site( $args, $assoc_args ) {
self::$force_crawl_urls = true;
}

if ( ! current_theme_supports( 'amp' ) ) {
if ( ! current_theme_supports( AMP_Theme_Support::SLUG ) ) {
if ( self::$force_crawl_urls ) {
/*
* There is no theme support added programmatically or via options.
* So make sure that theme support is present so that AMP_Validation_Manager::validate_url()
* will use a canonical URL as the basis for obtaining validation results.
*/
add_theme_support( 'amp' );
add_theme_support( AMP_Theme_Support::SLUG );
} else {
WP_CLI::error(
sprintf(
Expand Down
2 changes: 1 addition & 1 deletion includes/class-amp-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public static function handle_wp_die( $error, $title = '', $args = array() ) {
public static function filter_comment_post_redirect( $url, $comment ) {
$theme_support = AMP_Theme_Support::get_theme_support_args();

// Cause a page refresh if amp-live-list is not implemented for comments via add_theme_support( 'amp', array( 'comments_live_list' => true ) ).
// Cause a page refresh if amp-live-list is not implemented for comments via add_theme_support( AMP_Theme_Support::SLUG, array( 'comments_live_list' => true ) ).
if ( empty( $theme_support['comments_live_list'] ) ) {
/*
* Add the comment ID to the URL to force AMP to refresh the page.
Expand Down
15 changes: 11 additions & 4 deletions includes/class-amp-post-type-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
*/
class AMP_Post_Type_Support {

/**
* Post type support slug.
*
* @var string
*/
const SLUG = 'amp';

/**
* Get post types that plugin supports out of the box (which cannot be disabled).
*
Expand Down Expand Up @@ -46,13 +53,13 @@ public static function get_eligible_post_types() {
* @since 0.6
*/
public static function add_post_type_support() {
if ( current_theme_supports( 'amp' ) && AMP_Options_Manager::get_option( 'all_templates_supported' ) ) {
if ( current_theme_supports( AMP_Theme_Support::SLUG ) && AMP_Options_Manager::get_option( 'all_templates_supported' ) ) {
$post_types = self::get_eligible_post_types();
} else {
$post_types = AMP_Options_Manager::get_option( 'supported_post_types', array() );
}
foreach ( $post_types as $post_type ) {
add_post_type_support( $post_type, amp_get_slug() );
add_post_type_support( $post_type, self::SLUG );
}
}

Expand All @@ -70,7 +77,7 @@ public static function get_support_errors( $post ) {
}
$errors = array();

if ( ! post_type_supports( $post->post_type, amp_get_slug() ) ) {
if ( ! post_type_supports( $post->post_type, self::SLUG ) ) {
$errors[] = 'post-type-support';
}

Expand Down Expand Up @@ -102,7 +109,7 @@ public static function get_support_errors( $post ) {
* support is present (in which case AMP_Theme_Support::get_template_availability() determines availability).
*/
$enabled = (
current_theme_supports( 'amp' )
current_theme_supports( AMP_Theme_Support::SLUG )
||
(
! (bool) get_page_template_slug( $post )
Expand Down
21 changes: 14 additions & 7 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class AMP_Theme_Support {

/**
* Theme support slug.
*
* @var string
*/
const SLUG = 'amp';

/**
* Replaced with the necessary scripts depending on components used in output.
*
Expand Down Expand Up @@ -124,7 +131,7 @@ class AMP_Theme_Support {
*/
public static function init() {
self::read_theme_support();
if ( ! current_theme_supports( 'amp' ) ) {
if ( ! current_theme_supports( self::SLUG ) ) {
return;
}

Expand Down Expand Up @@ -164,7 +171,7 @@ public static function is_support_added_via_option() {
*/
public static function read_theme_support() {
$theme_support_option = AMP_Options_Manager::get_option( 'theme_support' );
if ( current_theme_supports( 'amp' ) ) {
if ( current_theme_supports( self::SLUG ) ) {
$args = self::get_theme_support_args();

// Validate theme support usage.
Expand All @@ -184,12 +191,12 @@ public static function read_theme_support() {
}
self::$support_added_via_option = false;
} elseif ( 'disabled' !== $theme_support_option ) {
add_theme_support( 'amp', array(
add_theme_support( self::SLUG, array(
'paired' => ( 'paired' === $theme_support_option ),
) );
self::$support_added_via_option = true;
} elseif ( AMP_Validation_Manager::is_theme_support_forced() ) {
add_theme_support( 'amp' );
add_theme_support( self::SLUG );
}
}

Expand All @@ -203,10 +210,10 @@ public static function read_theme_support() {
* @return array|false Theme support args, or false if theme support is not present.
*/
public static function get_theme_support_args() {
if ( ! current_theme_supports( 'amp' ) ) {
if ( ! current_theme_supports( self::SLUG ) ) {
return false;
}
$support = get_theme_support( 'amp' );
$support = get_theme_support( self::SLUG );
if ( true === $support ) {
return array(
'paired' => false,
Expand Down Expand Up @@ -342,7 +349,7 @@ public static function redirect_ampless_url( $exit = true ) {
* @return bool Whether available.
*/
public static function is_paired_available() {
if ( ! current_theme_supports( 'amp' ) ) {
if ( ! current_theme_supports( self::SLUG ) ) {
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static function check_supported_post_type_update_errors() {
continue;
}

$post_type_supported = post_type_supports( $post_type->name, amp_get_slug() );
$post_type_supported = post_type_supports( $post_type->name, AMP_Post_Type_Support::SLUG );
$is_support_elected = in_array( $post_type->name, $supported_types, true );

$error = null;
Expand Down Expand Up @@ -438,21 +438,21 @@ public static function handle_updated_theme_support_option() {

// Make sure post type support has been added for sake of amp_admin_get_preview_permalink().
foreach ( AMP_Post_Type_Support::get_eligible_post_types() as $post_type ) {
remove_post_type_support( $post_type, amp_get_slug() );
remove_post_type_support( $post_type, AMP_Post_Type_Support::SLUG );
}
AMP_Post_Type_Support::add_post_type_support();

// Ensure theme support flags are set properly according to the new mode so that proper AMP URL can be generated.
$has_theme_support = ( 'native' === $template_mode || 'paired' === $template_mode );
if ( $has_theme_support ) {
$theme_support = current_theme_supports( 'amp' );
$theme_support = current_theme_supports( AMP_Theme_Support::SLUG );
if ( ! is_array( $theme_support ) ) {
$theme_support = array();
}
$theme_support['paired'] = 'paired' === $template_mode;
add_theme_support( 'amp', $theme_support );
add_theme_support( AMP_Theme_Support::SLUG, $theme_support );
} else {
remove_theme_support( 'amp' ); // So that the amp_get_permalink() will work for classic URL.
remove_theme_support( AMP_Theme_Support::SLUG ); // So that the amp_get_permalink() will work for classic URL.
}

$url = amp_admin_get_preview_permalink();
Expand Down
Loading