Skip to content

Commit

Permalink
Disable post type checkbox setting if AMP post type support is set
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierryA committed Nov 23, 2017
1 parent d66bc62 commit 99e7bcf
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 62 deletions.
9 changes: 0 additions & 9 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ function amp_init() {
add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK );
add_post_type_support( 'post', AMP_QUERY_VAR );

// Listen to post types settings.
$post_types_supported = AMP_Settings_Post_Types::get_instance()->get_settings_value();

foreach ( $post_types_supported as $post_type_name => $enabled ) {
if ( true === (bool) $enabled ) {
add_post_type_support( $post_type_name, AMP_QUERY_VAR );
}
}

add_filter( 'request', 'amp_force_query_var_value' );
add_action( 'wp', 'amp_maybe_add_actions' );

Expand Down
7 changes: 5 additions & 2 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ function amp_get_permalink( $post_id ) {
}

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 ) ) {
// Listen to post types settings.
$is_enabled_via_setting = (bool) AMP_Settings_Post_Types::get_instance()->get_settings_value( $post->post_type );

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

Expand Down
42 changes: 5 additions & 37 deletions includes/settings/class-amp-settings-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ class AMP_Settings_Post_Types {
*/
protected $setting = array();

/**
* List of post types which are always AMPlified.
*
* @var array
*/
protected $always_on = array( 'post' );

/**
* Constructor.
*/
Expand Down Expand Up @@ -73,32 +66,17 @@ public function register_settings() {
/**
* Getter for settings value.
*
* The value(s) return are not sanitized.
*
* @param string $post_type The post type name.
* @return bool|array Return true if the post type is always on; the setting value otherwise.
* @return bool Return the setting value.
*/
public function get_settings_value( $post_type = false ) {
public function get_settings_value( $post_type ) {
$settings = get_option( AMP_Settings::SETTINGS_KEY, array() );

if ( false !== $post_type ) {
// Always return true if the post type is always on.
if ( true === $this->is_always_on( $post_type ) ) {
return true;
}

if ( isset( $settings[ $this->setting['id'] ][ $post_type ] ) ) {
return $settings[ $this->setting['id'] ][ $post_type ];
}

return false;
if ( isset( $settings[ $this->setting['id'] ][ $post_type ] ) ) {
return (bool) $settings[ $this->setting['id'] ][ $post_type ];
}

if ( empty( $settings[ $this->setting['id'] ] ) || ! is_array( $settings[ $this->setting['id'] ] ) ) {
return array();
}

return $settings[ $this->setting['id'] ];
return false;
}

/**
Expand Down Expand Up @@ -130,16 +108,6 @@ public function get_setting_name( $post_type ) {
return AMP_Settings::SETTINGS_KEY . "[{$id}][{$post_type}]";
}

/**
* Check if a post type is always on.
*
* @param string $post_type The post type name.
* @return bool True if the post type is always on; false otherwise.
*/
public function is_always_on( $post_type ) {
return in_array( $post_type, $this->always_on, true );
}

/**
* Setting renderer.
*/
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/settings/fields/checkbox-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<fieldset>
<?php foreach ( $this->get_supported_post_types() as $post_type ) : ?>
<label>
<input type="checkbox" value="1" name="<?php echo esc_attr( $this->get_setting_name( $post_type->name ) ); ?>"<?php checked( true, (bool) $this->get_settings_value( $post_type->name ) ); ?><?php disabled( true, $this->is_always_on( $post_type->name ) ); ?>> <?php echo esc_html( $post_type->label ); ?>
<input type="checkbox" value="1" name="<?php echo esc_attr( $this->get_setting_name( $post_type->name ) ); ?>"<?php checked( true, (bool) $this->get_settings_value( $post_type->name ) || post_type_supports( $post_type->name, AMP_QUERY_VAR ) ); ?><?php disabled( true, post_type_supports( $post_type->name, AMP_QUERY_VAR ) ); ?>> <?php echo esc_html( $post_type->label ); ?>
</label>
<br>
<?php endforeach; ?>
Expand Down
13 changes: 0 additions & 13 deletions tests/test-class-amp-settings-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public function test_register_settings() {
* @see AMP_Settings_Post_Types::get_settings_value()
*/
public function test_get_settings_value() {
$this->assertEmpty( $this->instance->get_settings_value() );
$this->assertInternalType( 'array', $this->instance->get_settings_value() );
$this->assertFalse( $this->instance->get_settings_value( 'foo' ) );

update_option( AMP_Settings::SETTINGS_KEY, array(
Expand All @@ -90,8 +88,6 @@ public function test_get_settings_value() {
),
) );

$this->assertContains( 'post', $this->instance->get_settings_value() );
$this->assertInternalType( 'array', $this->instance->get_settings_value() );
$this->assertTrue( $this->instance->get_settings_value( 'post' ) );

// Cleanup.
Expand All @@ -117,15 +113,6 @@ public function test_get_setting_name() {
$this->assertEquals( AMP_Settings::SETTINGS_KEY . '[post_types_support][post]', $this->instance->get_setting_name( 'post' ) );
}

/**
* Test is_always_on.
*
* @see AMP_Settings_Post_Types::is_always_on()
*/
public function test_is_always_on() {
$this->assertTrue( $this->instance->is_always_on( 'post' ) );
}

/**
* Test render_setting.
*
Expand Down

0 comments on commit 99e7bcf

Please sign in to comment.