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

Commit ec73112

Browse files
committed
Connect post preview customizer with post formats
1 parent 25634ec commit ec73112

8 files changed

+76
-46
lines changed

js/edit-post-preview-admin.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ var EditPostPreviewAdmin = (function( $ ) {
3838
wasMobile,
3939
parentId,
4040
menuOrder,
41-
request;
41+
request,
42+
postFormat;
4243

4344
event.preventDefault();
4445

@@ -75,9 +76,17 @@ var EditPostPreviewAdmin = (function( $ ) {
7576
ping_status: $( '#ping_status' ).prop( 'checked' ) ? 'open' : 'closed',
7677
post_author: parseInt( $( '#post_author_override' ).val(), 10 )
7778
};
78-
postSettingId = 'post[' + postType + '][' + postId + ']';
79+
postSettingId = 'post[' + postType + '][' + String( postId ) + ']';
7980
settings[ postSettingId ] = postSettingValue;
8081

82+
postFormat = $( '#post-formats-select input[name=post_format]:checked' ).val();
83+
if ( '0' === postFormat ) {
84+
postFormat = 'standard';
85+
}
86+
if ( postFormat ) {
87+
settings[ 'post_terms[' + postType + '][' + String( postId ) + '][post_format]' ] = postFormat;
88+
}
89+
8190
// Allow plugins to inject additional settings to preview.
8291
wp.customize.trigger( 'settings-from-edit-post-screen', settings );
8392

@@ -86,15 +95,15 @@ var EditPostPreviewAdmin = (function( $ ) {
8695
sessionStorage.setItem( 'previewedCustomizePostSettings[' + postId + ']', JSON.stringify( settings ) );
8796
wp.customize.Loader.open( component.data.customize_url );
8897
wp.customize.Loader.settings.browser.mobile = wasMobile;
89-
component.bindChangesFromCustomizer( postSettingId, editor );
98+
component.bindChangesFromCustomizer( postId, postType, editor );
9099
} else {
91100
$btn.addClass( 'disabled' );
92101
component.previewButtonSpinner.addClass( 'is-active is-active-preview' );
93102
request = wp.ajax.post( 'customize_posts_update_changeset', {
94103
customize_posts_update_changeset_nonce: component.data.customize_posts_update_changeset_nonce,
95104
previewed_post: component.data.previewed_post,
96105
customize_url: component.data.customize_url,
97-
input_data: postSettingValue
106+
settings: JSON.stringify( settings )
98107
} );
99108

100109
request.fail( function( resp ) {
@@ -108,7 +117,7 @@ var EditPostPreviewAdmin = (function( $ ) {
108117
request.done( function( resp ) {
109118
wp.customize.Loader.open( resp.customize_url );
110119
wp.customize.Loader.settings.browser.mobile = wasMobile;
111-
component.bindChangesFromCustomizer( postSettingId, editor );
120+
component.bindChangesFromCustomizer( postId, postType, editor );
112121
} );
113122

114123
request.always( function() {
@@ -121,13 +130,17 @@ var EditPostPreviewAdmin = (function( $ ) {
121130
/**
122131
* Sync changes from the Customizer to the post input fields.
123132
*
124-
* @param {string} postSettingId post setting id.
125-
* @param {object} editor Tinymce object.
133+
* @param {int} postId - Post ID.
134+
* @param {string} postType - Post type.
135+
* @param {object} editor - Tinymce object.
126136
* @return {void}
127137
*/
128-
component.bindChangesFromCustomizer = function( postSettingId, editor ) {
138+
component.bindChangesFromCustomizer = function( postId, postType, editor ) {
139+
var postSettingId, postFormatSettingId;
140+
postSettingId = 'post[' + postType + '][' + String( postId ) + ']';
141+
postFormatSettingId = 'post_terms[' + postType + '][' + String( postId ) + '][post_format]';
129142
wp.customize.Loader.messenger.bind( 'customize-post-settings-data', function( data ) {
130-
var settingParentId;
143+
var settingParentId, postFormatRadioSelector;
131144
if ( data[ postSettingId ] ) {
132145
$( '#title' ).val( data[ postSettingId ].post_title ).trigger( 'change' );
133146
if ( editor ) {
@@ -147,6 +160,14 @@ var EditPostPreviewAdmin = (function( $ ) {
147160
$( '#new-post-slug' ).val( data[ postSettingId ].post_name );
148161
$( '#editable-post-name, #editable-post-name-full' ).text( data[ postSettingId ].post_name );
149162
}
163+
if ( data[ postFormatSettingId ] ) {
164+
if ( 'standard' === data[ postFormatSettingId ] ) {
165+
postFormatRadioSelector = '#post-format-0';
166+
} else {
167+
postFormatRadioSelector = '#post-format-' + data[ postFormatSettingId ];
168+
}
169+
$( postFormatRadioSelector ).prop( 'checked', true ).trigger( 'change' );
170+
}
150171

151172
// Let plugins handle updates.
152173
wp.customize.trigger( 'settings-from-customizer', data );

js/edit-post-preview-customize.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@ var EditPostPreviewCustomize = (function( $, api ) {
101101
} );
102102

103103
// Start listening to changes to the post and postmeta.
104-
api( 'post[' + component.data.previewed_post.post_type + '][' + component.data.previewed_post.ID + ']', function( setting ) {
105-
setting.bind( function( data ) {
106-
var settings = {};
107-
settings[ setting.id ] = data;
108-
component.sendSettingsToEditPostScreen( settings );
109-
} );
110-
} );
104+
api.bind( 'change', function( setting ) {
105+
var settings = {};
106+
settings[ setting.id ] = setting.get();
107+
component.sendSettingsToEditPostScreen( settings );
108+
});
111109

112110
component.parentFrame.send( 'customize-post-preview-ready' );
113111
};

php/class-edit-post-preview.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,14 @@ public function update_post_changeset() {
227227
} elseif ( empty( $_POST['customize_url'] ) ) {
228228
status_header( 400 );
229229
wp_send_json_error( 'missing_customize_url' );
230-
} elseif ( empty( $_POST['input_data'] ) || ! is_array( $_POST['input_data'] ) ) {
230+
} elseif ( empty( $_POST['settings'] ) ) {
231231
status_header( 400 );
232-
wp_send_json_error( 'missing_input_data' );
232+
wp_send_json_error( 'missing_settings' );
233+
}
234+
$setting_values = json_decode( wp_unslash( $_POST['settings'] ), true );
235+
if ( ! is_array( $setting_values ) ) {
236+
status_header( 400 );
237+
wp_send_json_error( 'invalid_settings' );
233238
}
234239

235240
$previewed_post_id = intval( wp_unslash( $_POST['previewed_post'] ) );
@@ -293,25 +298,21 @@ public function update_post_changeset() {
293298
*/
294299
$wp_customize_posts = $wp_customize->posts;
295300

296-
$settings = $wp_customize_posts->get_settings( array( $previewed_post_id ) );
297-
$setting = array_shift( $settings );
301+
if ( ! did_action( 'customize_register' ) ) {
302+
remove_action( 'customize_register', array( $wp_customize, 'register_controls' ) );
303+
$wp_customize->register_controls(); // Due to race condition with themes that set customize_register priority to 10.
304+
do_action( 'customize_register', $wp_customize );
305+
}
306+
if ( ! did_action( 'customize_posts_register_meta' ) ) {
307+
$wp_customize_posts->register_meta();
308+
}
298309

299-
if ( ! $setting ) {
300-
status_header( 404 );
301-
wp_send_json_error( 'setting_not_found' );
302-
return;
303-
} elseif ( ! $setting->check_capabilities() ) {
304-
status_header( 403 );
305-
wp_send_json_error( 'changeset_already_published' );
306-
return;
310+
$wp_customize->add_dynamic_settings( array_keys( $setting_values ) );
311+
312+
foreach ( $setting_values as $setting_id => $setting_value ) {
313+
$wp_customize->set_post_value( $setting_id, $setting_value );
307314
}
308-
$setting->preview();
309315

310-
// Note that save_changeset_post() will handle validation and sanitization.
311-
$wp_customize->set_post_value( $setting->id, wp_array_slice_assoc(
312-
array_merge( $setting->value(), wp_unslash( $_POST['input_data'] ) ),
313-
array_keys( $setting->default )
314-
) );
315316
$response = $wp_customize->save_changeset_post();
316317
if ( is_wp_error( $response ) ) {
317318
wp_send_json_error( $response->get_error_code() );

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ static function get_post_terms_setting_id( WP_Post $post, $taxonomy ) {
115115
* @return array Term IDs.
116116
*/
117117
public function value() {
118-
$terms = wp_get_post_terms( $this->post_id, $this->taxonomy, array(
119-
'fields' => 'ids',
120-
) );
121-
if ( is_wp_error( $terms ) ) {
118+
$terms = get_the_terms( $this->post_id, $this->taxonomy );
119+
if ( ! is_array( $terms ) ) {
122120
return array();
123121
}
124-
return $terms;
122+
return wp_list_pluck( $terms, 'term_id' );
125123
}
126124

127125
/**

php/class-wp-customize-posts-preview.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,11 @@ public function filter_get_the_terms_to_preview( $terms, $post_id, $taxonomy ) {
12101210
return $terms;
12111211
}
12121212

1213-
/** @var WP_Customize_Post_Terms_Setting $previewed_setting */
1213+
/**
1214+
* Previewed setting.
1215+
*
1216+
* @var WP_Customize_Post_Terms_Setting $previewed_setting
1217+
*/
12141218
$previewed_setting = $this->previewed_post_terms_settings[ $post_id ][ $taxonomy ];
12151219

12161220
$post_value = $previewed_setting->post_value( null );

php/class-wp-customize-posts.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ 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 );
98+
$this->configure_builtins();
99+
add_action( 'init', array( $this, 'configure_builtins' ), 1 ); // Because built-ins get registered twice.
99100
add_action( 'init', array( $this, 'register_meta' ), 100 );
100101
add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 );
101102
add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_customize_dynamic_setting_class' ), 5, 3 );
@@ -544,6 +545,9 @@ public function filter_customize_dynamic_setting_args( $args, $setting_id ) {
544545
if ( empty( $taxonomy->show_in_customizer ) ) {
545546
return $args;
546547
}
548+
if ( 'post_format' === $taxonomy->name && ! post_type_supports( $matches['post_type'], 'post-formats' ) ) { // @todo Remove from being hard-coded?
549+
return $args;
550+
}
547551
if ( false === $args ) {
548552
$args = array();
549553
}

tests/php/test-ajax-class-wp-customize-posts.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,15 @@ public function test_update_post_changeset_successes() {
486486
'ping_status' => 'closed',
487487
'post_author' => $user_id,
488488
);
489+
$settings = array();
490+
$setting_id = "post[post][$post_id]";
491+
$settings[ $setting_id ] = $input_data;
489492

490493
$_POST = wp_slash( array(
491494
'customize_posts_update_changeset_nonce' => wp_create_nonce( 'customize_posts_update_changeset' ),
492495
'previewed_post' => $post_id,
493496
'customize_url' => wp_customize_url(),
494-
'input_data' => $input_data,
497+
'settings' => wp_json_encode( $settings ),
495498
) );
496499
$this->make_ajax_call( 'customize_posts_update_changeset' );
497500
$response = json_decode( $this->_last_response, true );
@@ -501,8 +504,7 @@ public function test_update_post_changeset_successes() {
501504
$this->assertArrayHasKey( 'response', $response['data'] );
502505
$this->assertArrayHasKey( 'setting_validities', $response['data']['response'] );
503506

504-
$setting_key = "post[post][$post_id]";
505-
$this->assertTrue( $response['data']['response']['setting_validities'][ $setting_key ] );
507+
$this->assertTrue( $response['data']['response']['setting_validities'][ $setting_id ] );
506508

507509
$customize_url_parts = parse_url( $response['data']['customize_url'] );
508510
parse_str( $customize_url_parts['query'], $url_query );
@@ -524,7 +526,7 @@ public function test_update_post_changeset_successes() {
524526
$settings_data = json_decode( $post->post_content, true );
525527

526528
foreach( $settings_data as $key => $data ) {
527-
$this->assertEquals( $setting_key, $key );
529+
$this->assertEquals( $setting_id, $key );
528530
$this->assertArraySubset( $input_data, $data['value'] );
529531
}
530532
}

tests/php/test-class-wp-customize-posts.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,13 @@ public function test_get_settings() {
748748
$this->assertEqualSets(
749749
array(
750750
WP_Customize_Post_Setting::get_post_setting_id( get_post( $published_post_id ) ),
751+
WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( get_post( $published_post_id ), 'post_format' ),
751752
WP_Customize_Post_Setting::get_post_setting_id( get_post( $trashed_post_id ) ),
753+
WP_Customize_Post_Terms_Setting::get_post_terms_setting_id( get_post( $trashed_post_id ), 'post_format' ),
752754
WP_Customize_Post_Setting::get_post_setting_id( get_post( $draft_page_id ) ),
753755
sprintf( 'nav_menu_item[%s]', $nav_menu_item_id ),
754756
WP_Customize_Postmeta_Setting::get_post_meta_setting_id( get_post( $published_post_id ), 'baz' ),
755-
WP_Customize_Postmeta_Setting::get_post_meta_setting_id( get_post( $trashed_post_id ), 'baz' )
757+
WP_Customize_Postmeta_Setting::get_post_meta_setting_id( get_post( $trashed_post_id ), 'baz' ),
756758
),
757759
array_keys( $settings_params )
758760
);

0 commit comments

Comments
 (0)