Skip to content

Commit

Permalink
WIP5
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 27, 2018
1 parent 718e643 commit 1d2053c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 76 deletions.
2 changes: 1 addition & 1 deletion includes/class-amp-post-type-support.php
Expand Up @@ -29,7 +29,7 @@ public static function get_builtin_supported_post_types() {
public static function get_eligible_post_types() {
return array_merge(
self::get_builtin_supported_post_types(),
array( 'page' ),
array( 'page', 'attachment' ),
array_values( get_post_types(
array(
'public' => true,
Expand Down
85 changes: 85 additions & 0 deletions includes/class-amp-theme-support.php
Expand Up @@ -449,6 +449,91 @@ public static function get_template_availability( $query = null ) {
return true;
}

/**
* Get conditionals which are used for determining availability.
*
* @return array Selections.
*/
public static function get_template_conditional_options() {
$templates = array(
'home' => array(
'label' => __( 'Homepage', 'amp' ),
'callback' => 'is_front_page',
),
'blog' => array(
'label' => __( 'Blog', 'amp' ),
'callback' => 'is_home',
),
'archives' => array(
'label' => __( 'Archives', 'amp' ),
'description' => __( 'Including index pages for categories, tags, authors, and dates.', 'amp' ),
'callback' => 'is_archive',
'children' => array(
'author' => array(
'label' => __( 'Author', 'amp' ),
'callback' => 'is_author',
),
'date' => array(
'label' => __( 'Date', 'amp' ),
'callback' => 'is_date',
),
),
),
'search' => array(
'label' => __( 'Search', 'amp' ),
'callback' => 'is_search',
),
'404' => array(
'label' => __( 'Not Found (404)', 'amp' ),
'callback' => 'is_404',
),
'other' => array(
'label' => __( 'Other', 'amp' ),
'callback' => '__return_true',
),
);

if ( taxonomy_exists( 'category' ) ) {
$templates['archives']['children']['category'] = array(
'label' => get_taxonomy( 'category' )->labels->name,
'callback' => 'is_category',
);
}
if ( taxonomy_exists( 'post_tag' ) ) {
$templates['archives']['children']['tag'] = array(
'label' => get_taxonomy( 'post_tag' )->labels->name,
'callback' => 'is_tag',
);
}
$taxonomy_args = array(
'_builtin' => false,
'publicly_queryable' => true,
);
foreach ( get_taxonomies( $taxonomy_args, 'objects' ) as $taxonomy ) {
$templates['archives']['children'][ 'tax_' . $taxonomy->name ] = array(
'label' => $taxonomy->labels->name,
'callback' => function ( WP_Query $query ) use ( $taxonomy ) {
return $query->is_tax( $taxonomy->name );
},
);
}

$post_type_args = array(
'has_archive' => true,
'publicly_queryable' => true,
);
foreach ( get_post_types( $post_type_args, 'objects' ) as $post_type ) {
$templates['archives']['children'][ 'post_type_archive_' . $post_type->name ] = array(
'label' => $post_type->labels->archives,
'callback' => function ( WP_Query $query ) use ( $post_type ) {
return $query->is_post_type_archive( $post_type->name );
},
);
}

return $templates;
}

/**
* Register hooks.
*/
Expand Down
8 changes: 4 additions & 4 deletions includes/options/class-amp-options-manager.php
Expand Up @@ -120,10 +120,10 @@ public static function validate_options( $new_options ) {
$options['theme_support'] = $new_options['theme_support'];
}

$options['force_sanitization'] = ! empty( $new_options['force_sanitization'] );
$options['accept_tree_shaking'] = ! empty( $new_options['accept_tree_shaking'] );
$options['disable_admin_bar'] = ! empty( $new_options['disable_admin_bar'] );
$options['non_singular_supported'] = ! empty( $new_options['non_singular_supported'] );
$options['force_sanitization'] = ! empty( $new_options['force_sanitization'] );
$options['accept_tree_shaking'] = ! empty( $new_options['accept_tree_shaking'] );
$options['disable_admin_bar'] = ! empty( $new_options['disable_admin_bar'] );
$options['all_templates_supported'] = ! empty( $new_options['all_templates_supported'] );

// Validate post type support.
if ( isset( $new_options['supported_post_types'] ) ) {
Expand Down
133 changes: 62 additions & 71 deletions includes/options/class-amp-options-menu.php
Expand Up @@ -278,13 +278,7 @@ public function render_validation_handling() {
*/
public function render_supported_templates() {
?>
<script>
jQuery( 'input[type=radio][name="amp-options[theme_support]"]' ).change( function() {
jQuery( 'fieldset.non_singular_supported, fieldset.all_templates_supported' ).toggleClass( 'hidden', 'disabled' === this.value );
} ).filter( ':checked' ).trigger( 'change' );
</script>

<fieldset class="all_templates_supported">
<fieldset id="all_templates_supported_fieldset">
<p>
<label for="all_templates_supported">
<input id="all_templates_supported" type="checkbox" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[all_templates_supported]' ); ?>" <?php checked( AMP_Options_Manager::get_option( 'all_templates_supported' ) ); ?>>
Expand All @@ -296,88 +290,85 @@ public function render_supported_templates() {
</p>
</fieldset>

<fieldset>
<?php
$builtin_support = AMP_Post_Type_Support::get_builtin_supported_post_types();
$element_name = AMP_Options_Manager::OPTION_NAME . '[supported_post_types][]';
?>
<legend>
<h4 class="title"><?php esc_html_e( 'Singular', 'amp' ); ?></h4>
</legend>

<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>
<ul>
<?php foreach ( array_map( 'get_post_type_object', AMP_Post_Type_Support::get_eligible_post_types() ) as $post_type ) : ?>
<?php
$element_id = AMP_Options_Manager::OPTION_NAME . "-supported_post_types-{$post_type->name}";
$is_builtin = in_array( $post_type->name, $builtin_support, true );
?>
<?php if ( $is_builtin ) : ?>
<input type="hidden" name="<?php echo esc_attr( $element_name ); ?>" value="<?php echo esc_attr( $post_type->name ); ?>">
<?php endif; ?>
<li>
<?php $element_id = AMP_Options_Manager::OPTION_NAME . "-supported_post_types-{$post_type->name}"; ?>
<input
type="checkbox"
id="<?php echo esc_attr( $element_id ); ?>"
name="<?php echo esc_attr( $element_name ); ?>"
value="<?php echo esc_attr( $post_type->name ); ?>"
<?php checked( true, post_type_supports( $post_type->name, amp_get_slug() ) ); ?>
<?php disabled( $is_builtin ); ?>
>
<!-- @todo Hidden to capture actual state? -->
<label for="<?php echo esc_attr( $element_id ); ?>">
<?php echo esc_html( $post_type->label ); ?>
</label>
<br>
</li>
<?php endforeach; ?>
<p class="description">
<?php esc_html_e( 'Select the content types that you would like to be made available in AMP.', 'amp' ); ?>
</p>
</ul>
</fieldset>

<fieldset id="non_singular_templates_fieldset">
<style>
#non_singular_templates_fieldset ul ul {
margin-left: 40px;
}
</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() ); ?>
</fieldset>

<script>
(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' ) );
}

templateModeInputs.on( 'change', updateFieldsetVisibility );
allTemplatesSupportedInput.on( 'click', updateFieldsetVisibility );
updateFieldsetVisibility();
})( jQuery );
</script>
<?php
// This is inspired by Jetpack's Widget Conditions.
// @todo We need to construct a WP_Query for each? There will neeed to
$templates = array(
'author' => array(
'' => __( 'All author pages', 'amp' ),
),
'category' => array_merge(
array(
'' => __( 'All category pages', 'amp' ),
)
),
'tag' => array_merge(
array(
'' => __( 'All tag pages', 'amp' ),
)
),
'date' => array(
'' => __( 'All date archives', 'amp' ),
'day' => __( 'Daily archives', 'amp' ),
'month' => __( 'Monthly archives', 'amp' ),
'year' => __( 'Yearly archives', 'amp' ),
),
'page' => array(
'front' => __( 'Front page', 'amp' ),
'posts' => __( 'Posts page', 'amp' ),
'archive' => __( 'Archive page', 'amp' ),
'404' => __( '404 error page', 'amp' ),
'search' => __( 'Search results', 'amp' ),
),
// @todo Post type archives.
// @todo Custom taxonomies.
);
}

/**
* @param array $options Options.
* @param bool $parent_checked
*/
private function list_template_conditional_options( $options, $parent_checked = false ) {
$element_name = AMP_Options_Manager::OPTION_NAME . '[supported_templates][]';
?>
<ul>
<?php foreach ( $options as $id => $option ) : ?>
<?php
$has_children = ! empty( $option['children'] );
$element_id = AMP_Options_Manager::OPTION_NAME . '-supported-templates-' . $id;
?>
<li>
<input type="checkbox" id="<?php echo esc_attr( $element_id ); ?>" name="<?php echo esc_attr( $element_name ); ?>" value="<?php echo esc_attr( $id ); ?>">
<label for="<?php echo esc_attr( $element_id ); ?>">
<?php echo esc_html( $option['label'] ); ?>
</label>

<fieldset class="non_singular_supported">
<p>
<label for="non_singular_supported">
<input id="non_singular_supported" type="checkbox" name="<?php echo esc_attr( AMP_Options_Manager::OPTION_NAME . '[non_singular_supported]' ); ?>" <?php checked( AMP_Options_Manager::get_option( 'non_singular_supported' ) ); ?>>
<?php esc_html_e( 'Serve non-singular templates as AMP.', 'amp' ); ?>
</label>
</p>
<p class="description">
<?php esc_html_e( 'Non-singular means templates like categories, date archives, author pages, and so on.', 'amp' ); ?>
</p>
</fieldset>
<?php if ( $has_children ) : ?>
<?php self::list_template_conditional_options( $option['children'] ); ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
}

Expand Down

0 comments on commit 1d2053c

Please sign in to comment.