Skip to content

Commit

Permalink
Use dedicated constant AMP_Post_Type_Support::SLUG instead of variabl…
Browse files Browse the repository at this point in the history
…e amp_get_slug()
  • Loading branch information
westonruter committed Sep 23, 2018
1 parent 97e6d75 commit 9a874b1
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion 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 Down
11 changes: 9 additions & 2 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 @@ -52,7 +59,7 @@ public static function add_post_type_support() {
$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
4 changes: 2 additions & 2 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,7 +438,7 @@ 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();

Expand Down
2 changes: 1 addition & 1 deletion includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public function render_supported_templates() {
id="<?php echo esc_attr( $element_id ); ?>"
name="<?php echo esc_attr( $element_name ); ?>"
value="<?php echo esc_attr( $post_type->name ); ?>"
<?php checked( post_type_supports( $post_type->name, amp_get_slug() ) ); ?>
<?php checked( post_type_supports( $post_type->name, AMP_Post_Type_Support::SLUG ) ); ?>
>
<label for="<?php echo esc_attr( $element_id ); ?>">
<?php echo esc_html( $post_type->label ); ?>
Expand Down
4 changes: 2 additions & 2 deletions tests/test-amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public function test_amp_get_content_sanitizers_deprecated_param() {
* @covers \post_supports_amp()
*/
public function test_post_supports_amp() {
add_post_type_support( 'page', amp_get_slug() );
add_post_type_support( 'page', AMP_Post_Type_Support::SLUG );

// Test disabled by default for page for posts and show on front.
update_option( 'show_on_front', 'page' );
Expand All @@ -556,7 +556,7 @@ public function test_post_supports_amp() {
$this->assertFalse( post_supports_amp( $post ) );

// Reset.
remove_post_type_support( 'page', amp_get_slug() );
remove_post_type_support( 'page', AMP_Post_Type_Support::SLUG );
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/test-class-amp-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function test_render_status() {
wp_set_current_user( $this->factory()->user->create( array(
'role' => 'administrator',
) ) );
add_post_type_support( 'post', amp_get_slug() );
add_post_type_support( 'post', AMP_Post_Type_Support::SLUG );
$amp_status_markup = '<div class="misc-pub-section misc-amp-status"';
$checkbox_enabled = '<input id="amp-status-enabled" type="radio" name="amp_status" value="enabled" checked=\'checked\'>';

Expand All @@ -143,13 +143,13 @@ public function test_render_status() {
$this->assertContains( $checkbox_enabled, $output );

// Post type no longer supports AMP, so no status input.
remove_post_type_support( 'post', amp_get_slug() );
remove_post_type_support( 'post', AMP_Post_Type_Support::SLUG );
ob_start();
$this->instance->render_status( $post );
$output = ob_get_clean();
$this->assertContains( 'post type does not support it', $output );
$this->assertNotContains( $checkbox_enabled, $output );
add_post_type_support( 'post', amp_get_slug() );
add_post_type_support( 'post', AMP_Post_Type_Support::SLUG );

// No template is available to render the post.
add_filter( 'amp_supportable_templates', '__return_empty_array' );
Expand All @@ -161,7 +161,7 @@ public function test_render_status() {
$this->assertNotContains( $checkbox_enabled, $output );

// User doesn't have the capability to display the metabox.
add_post_type_support( 'post', amp_get_slug() );
add_post_type_support( 'post', AMP_Post_Type_Support::SLUG );
wp_set_current_user( $this->factory()->user->create( array(
'role' => 'subscriber',
) ) );
Expand Down Expand Up @@ -197,15 +197,15 @@ public function test_get_status_and_errors() {
);

// If post type doesn't support AMP, this method should return AMP as being disabled.
remove_post_type_support( 'post', amp_get_slug() );
remove_post_type_support( 'post', AMP_Post_Type_Support::SLUG );
$this->assertEquals(
array(
'status' => 'disabled',
'errors' => array( 'post-type-support' ),
),
$this->instance->get_status_and_errors( $post )
);
add_post_type_support( 'post', amp_get_slug() );
add_post_type_support( 'post', AMP_Post_Type_Support::SLUG );

// There's no template to render this post, so this method should also return AMP as disabled.
add_filter( 'amp_supportable_templates', '__return_empty_array' );
Expand Down
10 changes: 5 additions & 5 deletions tests/test-class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,15 @@ public function test_check_supported_post_type_update_errors() {
AMP_Options_Manager::update_option( 'all_templates_supported', false );
foreach ( get_post_types() as $post_type ) {
if ( 'foo' !== $post_type ) {
remove_post_type_support( $post_type, amp_get_slug() );
remove_post_type_support( $post_type, AMP_Post_Type_Support::SLUG );
}
}
AMP_Options_Manager::update_option( 'supported_post_types', array( 'foo' ) );
AMP_Options_Manager::check_supported_post_type_update_errors();
$this->assertEmpty( get_settings_errors() );

// Test when 'all_templates_supported' is not selected, and theme support is also disabled.
add_post_type_support( 'post', amp_get_slug() );
add_post_type_support( 'post', AMP_Post_Type_Support::SLUG );
AMP_Options_Manager::update_option( 'theme_support', 'disabled' );
AMP_Options_Manager::update_option( 'all_templates_supported', false );
AMP_Options_Manager::update_option( 'supported_post_types', array( 'post' ) );
Expand All @@ -261,8 +261,8 @@ public function test_check_supported_post_type_update_errors() {
$this->assertEquals( 'foo_deactivation_error', $settings_errors[0]['code'] );

// Activation error.
remove_post_type_support( 'post', amp_get_slug() );
remove_post_type_support( 'foo', amp_get_slug() );
remove_post_type_support( 'post', AMP_Post_Type_Support::SLUG );
remove_post_type_support( 'foo', AMP_Post_Type_Support::SLUG );
AMP_Options_Manager::update_option( 'supported_post_types', array( 'foo' ) );
AMP_Options_Manager::update_option( 'theme_support', 'disabled' );
AMP_Options_Manager::check_supported_post_type_update_errors();
Expand All @@ -274,7 +274,7 @@ public function test_check_supported_post_type_update_errors() {

// Deactivation error.
AMP_Options_Manager::update_option( 'supported_post_types', array() );
add_post_type_support( 'foo', amp_get_slug() );
add_post_type_support( 'foo', AMP_Post_Type_Support::SLUG );
AMP_Options_Manager::check_supported_post_type_update_errors();
$errors = get_settings_errors();
$this->assertCount( 1, $errors );
Expand Down
8 changes: 4 additions & 4 deletions tests/test-class-amp-post-type-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function test_add_post_type_support() {
AMP_Options_Manager::update_option( 'supported_post_types', array( 'post', 'poem' ) );

AMP_Post_Type_Support::add_post_type_support();
$this->assertTrue( post_type_supports( 'post', amp_get_slug() ) );
$this->assertTrue( post_type_supports( 'poem', amp_get_slug() ) );
$this->assertFalse( post_type_supports( 'book', amp_get_slug() ) );
$this->assertTrue( post_type_supports( 'post', AMP_Post_Type_Support::SLUG ) );
$this->assertTrue( post_type_supports( 'poem', AMP_Post_Type_Support::SLUG ) );
$this->assertFalse( post_type_supports( 'book', AMP_Post_Type_Support::SLUG ) );
}

/**
Expand All @@ -86,7 +86,7 @@ public function test_get_support_error() {
// Post type support.
$book_id = $this->factory()->post->create( array( 'post_type' => 'book' ) );
$this->assertEquals( array( 'post-type-support' ), AMP_Post_Type_Support::get_support_errors( $book_id ) );
add_post_type_support( 'book', amp_get_slug() );
add_post_type_support( 'book', AMP_Post_Type_Support::SLUG );
$this->assertEmpty( AMP_Post_Type_Support::get_support_errors( $book_id ) );

// Password-protected.
Expand Down
4 changes: 2 additions & 2 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,12 @@ public function test_get_template_availability() {
$this->assertEquals( 'is_special', $availability['template'] );
$this->assertFalse( $availability['immutable'] );

remove_post_type_support( 'page', 'amp' );
remove_post_type_support( 'page', AMP_Post_Type_Support::SLUG );
$availability = AMP_Theme_Support::get_template_availability( $this->factory()->post->create_and_get( array( 'post_type' => 'page' ) ) );
$this->assertFalse( $availability['supported'] );
$this->assertEquals( array( 'post-type-support' ), $availability['errors'] );
$this->assertEquals( 'is_page', $availability['template'] );
add_post_type_support( 'page', 'amp' );
add_post_type_support( 'page', AMP_Post_Type_Support::SLUG );
$availability = AMP_Theme_Support::get_template_availability( $this->factory()->post->create_and_get( array( 'post_type' => 'page' ) ) );
$this->assertTrue( $availability['supported'] );

Expand Down

0 comments on commit 9a874b1

Please sign in to comment.