Skip to content

Commit

Permalink
WIP7
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 28, 2018
1 parent 2eda94b commit 3377ecf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
37 changes: 23 additions & 14 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,16 @@ public static function get_template_conditional_options() {
'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',
),
),
),
'author' => array(
'label' => __( 'Author', 'amp' ),
'callback' => 'is_author',
'parent' => 'archives',
),
'date' => array(
'label' => __( 'Date', 'amp' ),
'callback' => 'is_date',
'parent' => 'archives',
),
'search' => array(
'label' => __( 'Search', 'amp' ),
Expand All @@ -494,24 +494,28 @@ public static function get_template_conditional_options() {
);

if ( taxonomy_exists( 'category' ) ) {
$templates['archives']['children']['category'] = array(
$templates['tax[category]'] = array(
'label' => get_taxonomy( 'category' )->labels->name,
'callback' => 'is_category',
'parent' => 'archives',
);
}
if ( taxonomy_exists( 'post_tag' ) ) {
$templates['archives']['children']['tag'] = array(
$templates['tax[post_tag]'] = array(
'label' => get_taxonomy( 'post_tag' )->labels->name,
'callback' => 'is_tag',
'parent' => 'archives',
);
}

$taxonomy_args = array(
'_builtin' => false,
'publicly_queryable' => true,
);
foreach ( get_taxonomies( $taxonomy_args, 'objects' ) as $taxonomy ) {
$templates['archives']['children'][ 'tax_' . $taxonomy->name ] = array(
$templates[ sprintf( 'tax[%s]', $taxonomy->name ) ] = array(
'label' => $taxonomy->labels->name,
'parent' => 'archives',
'callback' => function ( WP_Query $query ) use ( $taxonomy ) {
return $query->is_tax( $taxonomy->name );
},
Expand All @@ -523,14 +527,19 @@ public static function get_template_conditional_options() {
'publicly_queryable' => true,
);
foreach ( get_post_types( $post_type_args, 'objects' ) as $post_type ) {
$templates['archives']['children'][ 'post_type_archive_' . $post_type->name ] = array(
$templates[ sprintf( 'post_type_archive[%s]', $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 );
},
);
}

$supported_templates = AMP_Options_Manager::get_option( 'supported_templates' );
foreach ( $templates as $id => &$template ) {
$template['supported'] = in_array( $id, $supported_templates, true );
}

return $templates;
}

Expand Down
15 changes: 11 additions & 4 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ class AMP_Options_Manager {
'force_sanitization' => false,
'accept_tree_shaking' => false,
'disable_admin_bar' => false,
'all_templates_supported' => false,
'supported_templates' => array(
'single',
),
'all_templates_supported' => true,
'supported_templates' => array(),
);

/**
Expand Down Expand Up @@ -137,6 +135,15 @@ public static function validate_options( $new_options ) {
}
}

// Validate supported templates.
$options['supported_templates'] = array();
if ( isset( $new_options['supported_templates'] ) ) {
$options['supported_templates'] = array_intersect(
$new_options['supported_templates'],
array_keys( AMP_Theme_Support::get_template_conditional_options() )
);
}

// Validate analytics.
if ( isset( $new_options['analytics'] ) ) {
foreach ( $new_options['analytics'] as $id => $data ) {
Expand Down
30 changes: 20 additions & 10 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ 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() ); ?>
<?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 ( $ ) {
Expand Down Expand Up @@ -365,27 +367,35 @@ function updateFieldsetVisibility() {
}

/**
* @param array $options Options.
* @param bool $parent_checked
* List template conditional options.
*
* @param array $options Options.
* @param string|null $parent ID of the parent option.
*/
private function list_template_conditional_options( $options, $parent_checked = false ) {
private function list_template_conditional_options( $options, $parent = null ) {
$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;
$element_id = AMP_Options_Manager::OPTION_NAME . '-supported-templates-' . $id;
if ( $parent ? empty( $option['parent'] ) || $parent !== $option['parent'] : ! empty( $option['parent'] ) ) {
continue;
}
?>
<li>
<input type="checkbox" id="<?php echo esc_attr( $element_id ); ?>" name="<?php echo esc_attr( $element_name ); ?>" value="<?php echo esc_attr( $id ); ?>">
<input
type="checkbox"
id="<?php echo esc_attr( $element_id ); ?>"
name="<?php echo esc_attr( $element_name ); ?>"
value="<?php echo esc_attr( $id ); ?>"
<?php checked( ! empty( $option['supported'] ) ); ?>
>
<label for="<?php echo esc_attr( $element_id ); ?>">
<?php echo esc_html( $option['label'] ); ?>
</label>

<?php if ( $has_children ) : ?>
<?php self::list_template_conditional_options( $option['children'] ); ?>
<?php endif; ?>
<?php self::list_template_conditional_options( $options, $id ); ?>
</li>
<?php endforeach; ?>
</ul>
Expand Down

0 comments on commit 3377ecf

Please sign in to comment.