Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit 25634ec

Browse files
committed
Add initial working post format selector
[ci skip]
1 parent b3f62f6 commit 25634ec

File tree

4 files changed

+61
-28
lines changed

4 files changed

+61
-28
lines changed

js/customize-post-section.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@
282282
section.addStatusControl();
283283
section.addDateControl();
284284
}
285-
if ( postTypeObj.supports['post-formats'] ) {
285+
if ( api.Posts.data.themeSupports['post-formats'] && postTypeObj.supports['post-formats'] ) {
286286
section.addPostFormatControl();
287287
}
288288
if ( postTypeObj.supports['page-attributes'] || postTypeObj.supports.parent ) {
@@ -639,27 +639,33 @@
639639
/**
640640
* Add post format control.
641641
*
642-
* @todo Move this into a separate file.
642+
* @todo Move this into a separate file/component like Page Template and Featured Image.
643+
*
644+
* @see post_format_meta_box() in PHP.
643645
*
644646
* @returns {wp.customize.Control} Added control.
645647
*/
646648
addPostFormatControl: function addPostFormatControl() {
647-
var section = this, control, setting = api( section.id ), controlId, settingId, params, postTypeObj, supports;
649+
var section = this, control, settingId, setting, controlId, postTypeObj, postFormats;
648650

649-
// @todo Get whether or not the theme has indicated support for post formats.
650-
supports = api.Posts.data.postTypes[ section.params.post_type ].supports;
651+
postTypeObj = api.Posts.data.postTypes[ section.params.post_type ];
652+
if ( ! api.Posts.data.themeSupports['post-formats'] || ! postTypeObj.supports['post-formats'] ) {
653+
return null;
654+
}
651655

652-
if ( ! supports['post-formats'] || 'page' !== section.params.post_type ) {
656+
settingId = 'post_terms[' + section.params.post_type + '][' + String( section.params.post_id ) + '][post_format]';
657+
setting = api( settingId );
658+
if ( ! setting ) {
653659
return null;
654660
}
655661

662+
// Add in the current one if it isn't there yet, in case the current theme doesn't support it
663+
postFormats = _.uniq(
664+
[ 'standard' ].concat( api.Posts.data.themeSupports['post-formats'] ).concat( [ setting.get() ] )
665+
);
656666

657-
return;
658-
postTypeObj = api.Posts.data.postTypes[ section.params.post_type ];
659-
settingId = 'postterms[' + section.params.post_type + '][' + String( section.params.post_id ) + '][post_format]';
660667
controlId = section.id + '[post_format]';
661-
662-
control = new api.controlConstructor.dynamic( section.id + '[post_format]', {
668+
control = new api.controlConstructor.dynamic( controlId, {
663669
params: {
664670
section: section.id,
665671
priority: 65,
@@ -669,7 +675,15 @@
669675
'default': setting.id
670676
},
671677
field_type: 'select',
672-
choices: postTypeObj['post-formats']
678+
input_attrs: {
679+
'data-customize-setting-link': settingId // @todo The need for this needs to be eliminated in core.
680+
},
681+
choices: _.map( postFormats, function( postFormat ) {
682+
return {
683+
text: api.Posts.data.l10n.postFormatStrings[ postFormat ] || postFormat,
684+
value: postFormat
685+
};
686+
} )
673687
}
674688
} );
675689

js/customize-posts.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,36 @@
7373
component.parseSettingId = function parseSettingId( settingId ) {
7474
var parsed = {}, idParts;
7575
idParts = settingId.replace( /]/g, '' ).split( '[' );
76-
if ( 'post' !== idParts[0] && 'postmeta' !== idParts[0] ) {
77-
return null;
78-
}
79-
parsed.settingType = idParts[0];
80-
if ( 'post' === parsed.settingType && 3 !== idParts.length || 'postmeta' === parsed.settingType && 4 !== idParts.length ) {
81-
return null;
82-
}
76+
parsed.settingType = idParts.shift();
8377

84-
parsed.postType = idParts[1];
78+
parsed.postType = idParts.shift();
8579
if ( ! parsed.postType ) {
8680
return null;
8781
}
8882

89-
parsed.postId = parseInt( idParts[2], 10 );
90-
if ( isNaN( parsed.postId ) || parsed.postId <= 0 ) {
83+
parsed.postId = parseInt( idParts.shift(), 10 );
84+
if ( ! parsed.postId || isNaN( parsed.postId ) || parsed.postId <= 0 ) {
9185
return null;
9286
}
9387

9488
if ( 'postmeta' === parsed.settingType ) {
95-
parsed.metaKey = idParts[3];
89+
parsed.metaKey = idParts.shift();
9690
if ( ! parsed.metaKey ) {
9791
return null;
9892
}
93+
} else if ( 'post_terms' === parsed.settingType ) {
94+
parsed.taxonomy = idParts.shift();
95+
if ( ! parsed.taxonomy ) {
96+
return null;
97+
}
98+
} else if ( 'post' !== parsed.settingType ) {
99+
return null;
99100
}
101+
102+
if ( 0 !== idParts.length ) {
103+
return null; // Too many ID parts.
104+
}
105+
100106
return parsed;
101107
};
102108

php/class-wp-customize-post-format-setting.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,18 @@ public function sanitize( $format ) {
6060
$value = array();
6161
if ( 'standard' !== $format ) {
6262
$slug = 'post-format-' . $format;
63-
$term = get_term_by( 'slug', $format, $this->taxonomy );
63+
$term = get_term_by( 'slug', $slug, $this->taxonomy );
6464

6565
// Make sure the post format term exists.
66-
if ( ! $term ) {
66+
if ( $term instanceof WP_Term ) {
67+
$value[] = $term->term_id;
68+
} else {
6769
$term = wp_insert_term( $slug, $this->taxonomy );
6870
if ( is_wp_error( $term ) ) {
6971
return $has_setting_validation ? $term : null;
7072
}
73+
$value[] = $term['term_id'];
7174
}
72-
$value[] = $term->term_id;
7375
}
7476

7577
$value = parent::sanitize( $value );

php/class-wp-customize-posts.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function __construct( WP_Customize_Manager $manager ) {
9595
add_action( 'customize_register', array( $this, 'ensure_static_front_page_constructs_registered' ), 11 );
9696
add_action( 'customize_register', array( $this, 'register_constructs' ), 20 );
9797
add_filter( 'map_meta_cap', array( $this, 'filter_map_meta_cap' ), 10, 4 );
98+
add_action( 'init', array( $this, 'configure_builtins' ), 1 );
9899
add_action( 'init', array( $this, 'register_meta' ), 100 );
99100
add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 );
100101
add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_customize_dynamic_setting_class' ), 5, 3 );
@@ -229,6 +230,7 @@ public function configure_builtins() {
229230
if ( post_type_exists( 'attachment' ) && ! isset( $wp_post_types['attachment']->show_in_customizer ) ) {
230231
$wp_post_types['attachment']->show_in_customizer = false;
231232
}
233+
232234
if ( taxonomy_exists( 'post_format' ) ) {
233235
if ( ! isset( $wp_taxonomies['post_format']->show_in_customizer ) ) {
234236
$wp_taxonomies['post_format']->show_in_customizer = true;
@@ -448,7 +450,6 @@ public function register_constructs() {
448450
$panel_priority = 900; // Before widgets.
449451

450452
// Note that this does not include nav_menu_item.
451-
$this->configure_builtins();
452453
foreach ( $this->get_post_types() as $post_type_object ) {
453454
if ( empty( $post_type_object->show_in_customizer ) ) {
454455
continue;
@@ -638,7 +639,7 @@ public function register_post_type_meta_settings( $post ) {
638639
*/
639640
public function register_post_terms_settings( $post ) {
640641
$setting_ids = array();
641-
foreach ( get_taxonomies() as $taxonomy ) {
642+
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
642643
if ( empty( $taxonomy->show_in_customizer ) ) {
643644
continue;
644645
}
@@ -853,7 +854,16 @@ public function enqueue_scripts() {
853854
);
854855
}
855856

857+
$theme_supports = array();
858+
if ( current_theme_supports( 'post-formats' ) ) {
859+
$theme_support = get_theme_support( 'post-formats' );
860+
if ( isset( $theme_support[0] ) && is_array( $theme_support[0] ) ) {
861+
$theme_supports['post-formats'] = $theme_support[0];
862+
}
863+
}
864+
856865
$exports = array(
866+
'themeSupports' => $theme_supports,
857867
'postTypes' => $post_types,
858868
'postStatusChoices' => $this->get_post_status_choices(),
859869
'authorChoices' => $this->get_author_choices(), // @todo Use Ajax to fetch this data or Customize Object Selector (once it supports users).
@@ -900,6 +910,7 @@ public function enqueue_scripts() {
900910

901911
/* translators: %s post type */
902912
'jumpToPostPlaceholder' => __( 'Jump to %s', 'customize-posts' ),
913+
'postFormatStrings' => get_post_format_strings(),
903914
),
904915
);
905916

0 commit comments

Comments
 (0)