Skip to content

Commit

Permalink
WIP6
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 28, 2018
1 parent 1d2053c commit 2eda94b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
31 changes: 14 additions & 17 deletions includes/class-amp-post-type-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class AMP_Post_Type_Support {
/**
* Get post types that plugin supports out of the box (which cannot be disabled).
*
* @deprecated
* @return string[] Post types.
*/
public static function get_builtin_supported_post_types() {
_deprecated_function( __METHOD__, '1.0' );
return array_filter( array( 'post' ), 'post_type_exists' );
}

Expand All @@ -27,17 +29,12 @@ public static function get_builtin_supported_post_types() {
* @return string[] Post types eligible for AMP.
*/
public static function get_eligible_post_types() {
return array_merge(
self::get_builtin_supported_post_types(),
array( 'page', 'attachment' ),
array_values( get_post_types(
array(
'public' => true,
'_builtin' => false,
),
'names'
) )
);
return array_values( get_post_types(
array(
'public' => true,
),
'names'
) );
}

/**
Expand All @@ -49,10 +46,11 @@ public static function get_eligible_post_types() {
* @since 0.6
*/
public static function add_post_type_support() {
$post_types = array_merge(
self::get_builtin_supported_post_types(),
AMP_Options_Manager::get_option( 'supported_post_types', array() )
);
if ( current_theme_supports( 'amp' ) && 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() );
}
Expand All @@ -72,8 +70,7 @@ public static function get_support_errors( $post ) {
}
$errors = array();

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

Expand Down
7 changes: 3 additions & 4 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AMP_Options_Manager {
*/
protected static $defaults = array(
'theme_support' => 'disabled',
'supported_post_types' => array(),
'supported_post_types' => array( 'post' ),
'analytics' => array(),
'force_sanitization' => false,
'accept_tree_shaking' => false,
Expand Down Expand Up @@ -126,8 +126,8 @@ public static function validate_options( $new_options ) {
$options['all_templates_supported'] = ! empty( $new_options['all_templates_supported'] );

// Validate post type support.
$options['supported_post_types'] = array();
if ( isset( $new_options['supported_post_types'] ) ) {
$options['supported_post_types'] = array();
foreach ( $new_options['supported_post_types'] as $post_type ) {
if ( ! post_type_exists( $post_type ) ) {
add_settings_error( self::OPTION_NAME, 'unknown_post_type', __( 'Unrecognized post type.', 'amp' ) );
Expand Down Expand Up @@ -192,11 +192,10 @@ public static function validate_options( $new_options ) {
* @see add_settings_error()
*/
public static function check_supported_post_type_update_errors() {
$builtin_support = AMP_Post_Type_Support::get_builtin_supported_post_types();
$supported_types = self::get_option( 'supported_post_types', array() );
foreach ( AMP_Post_Type_Support::get_eligible_post_types() as $name ) {
$post_type = get_post_type_object( $name );
if ( empty( $post_type ) || in_array( $post_type->name, $builtin_support, true ) ) {
if ( empty( $post_type ) ) {
continue;
}

Expand Down
28 changes: 24 additions & 4 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ public function render_supported_templates() {
<fieldset id="singular_templates_fieldset">
<?php $element_name = AMP_Options_Manager::OPTION_NAME . '[supported_post_types][]'; ?>
<h4 class="title"><?php esc_html_e( 'Singular Templates', 'amp' ); ?></h4>
<p>
<?php esc_html_e( 'The following content types will be available as AMP by default, but you can override this on an item-by-item basis:', 'amp' ); ?>
</p>
<ul>
<?php foreach ( array_map( 'get_post_type_object', AMP_Post_Type_Support::get_eligible_post_types() ) as $post_type ) : ?>
<li>
Expand All @@ -304,7 +307,6 @@ public function render_supported_templates() {
value="<?php echo esc_attr( $post_type->name ); ?>"
<?php checked( true, post_type_supports( $post_type->name, amp_get_slug() ) ); ?>
>
<!-- @todo Hidden to capture actual state? -->
<label for="<?php echo esc_attr( $element_id ); ?>">
<?php echo esc_html( $post_type->label ); ?>
</label>
Expand All @@ -321,19 +323,37 @@ public function render_supported_templates() {
</style>
<h4 class="title"><?php esc_html_e( 'Non-Singular Templates', 'amp' ); ?></h4>
<?php self::list_template_conditional_options( AMP_Theme_Support::get_template_conditional_options() ); ?>
<script>
// Let clicks on parent items automatically cause the children checkboxes to have same checked state applied.
(function ( $ ) {
$( '#non_singular_templates_fieldset input[type=checkbox]' ).on( 'click', function() {
$( this ).siblings( 'ul' ).find( 'input[type=checkbox]' ).prop( 'checked', this.checked );
} );
})( jQuery );
</script>
</fieldset>

<script>
// Update the visibility of the fieldsets based on the selected template mode and then whether all templates are indicated to be supported.
(function ( $ ) {
var templateModeInputs, themeSupportDisabledInput, allTemplatesSupportedInput;
templateModeInputs = $( 'input[type=radio][name="amp-options[theme_support]"]' );
themeSupportDisabledInput = $( '#theme_support_disabled' );
allTemplatesSupportedInput = $( '#all_templates_supported' );

function updateFieldsetVisibility() {
$( '#all_templates_supported_fieldset' ).toggleClass( 'hidden', themeSupportDisabledInput.prop( 'checked' ) );
$( '#singular_templates_fieldset' ).toggleClass( 'hidden', allTemplatesSupportedInput.prop( 'checked' ) && ! themeSupportDisabledInput.prop( 'checked' ) );
$( '#non_singular_templates_fieldset' ).toggleClass( 'hidden', allTemplatesSupportedInput.prop( 'checked' ) || themeSupportDisabledInput.prop( 'checked' ) );
$( '#all_templates_supported_fieldset, #singular_templates_fieldset > .title' ).toggleClass(
'hidden',
themeSupportDisabledInput.prop( 'checked' )
);
$( '#singular_templates_fieldset' ).toggleClass(
'hidden',
allTemplatesSupportedInput.prop( 'checked' ) && ! themeSupportDisabledInput.prop( 'checked' )
);
$( '#non_singular_templates_fieldset' ).toggleClass(
'hidden',
allTemplatesSupportedInput.prop( 'checked' ) || themeSupportDisabledInput.prop( 'checked' )
);
}

templateModeInputs.on( 'change', updateFieldsetVisibility );
Expand Down
12 changes: 2 additions & 10 deletions tests/test-class-amp-post-type-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ public function tearDown() {
unregister_post_type( 'secret' );
}

/**
* Test get_builtin_supported_post_types.
*
* @covers AMP_Post_Type_Support::get_builtin_supported_post_types()
*/
public function test_get_builtin_supported_post_types() {
$this->assertEquals( array( 'post' ), AMP_Post_Type_Support::get_builtin_supported_post_types() );
}

/**
* Test get_eligible_post_types.
*
Expand All @@ -46,10 +37,11 @@ public function test_get_eligible_post_types() {
'public' => false,
) );

$this->assertEquals(
$this->assertEqualSets(
array(
'post',
'page',
'attachment',
'book',
),
AMP_Post_Type_Support::get_eligible_post_types()
Expand Down

0 comments on commit 2eda94b

Please sign in to comment.