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

Add post date control and preview #202

Merged
merged 85 commits into from
Aug 2, 2016
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
996c2ca
Post Date - Initial text input
johnregan3 Jul 23, 2016
adaf8cd
Post Date - Add basic custom control
johnregan3 Jul 24, 2016
59f5419
Post Date - Simplify, cleanup
johnregan3 Jul 25, 2016
7bedfab
Post Date - Iniital Tests
johnregan3 Jul 25, 2016
a73a67b
Post Date - Code cleanup
johnregan3 Jul 25, 2016
ddeed3b
Post Date - WIP
johnregan3 Jul 25, 2016
128d4bc
Post Date - New Control
johnregan3 Jul 26, 2016
c76f7a4
Merge branch 'develop' into feature/issue-56-post-date-control
johnregan3 Jul 26, 2016
a1f6ca0
Post Date - CSS, Code cleanup
johnregan3 Jul 26, 2016
d274099
Post Date - Change comment type
johnregan3 Jul 26, 2016
f8c68ee
Post Date - JS Date Handlers
johnregan3 Jul 26, 2016
37b3a87
Post Date - Code cleanup
johnregan3 Jul 26, 2016
a67829a
Post Date - Documentation
johnregan3 Jul 26, 2016
a7226a5
Post Date - Feature working
johnregan3 Jul 27, 2016
4798cdf
Post Date - Remove Date formatter
johnregan3 Jul 27, 2016
3c7acd9
Post Date - 'Published on' text
johnregan3 Jul 27, 2016
ca92ac4
Post Date - Code cleanup
johnregan3 Jul 27, 2016
0081baf
Post Date - Modify eslint for consistent-this
johnregan3 Jul 27, 2016
092d3bc
Post Date - correct syntax for eslint
johnregan3 Jul 27, 2016
0ac0e77
Post Date - Input validation
johnregan3 Jul 27, 2016
c59b673
Post Date - Documentation
johnregan3 Jul 27, 2016
e2ad701
Post Date - Code cleanup
johnregan3 Jul 27, 2016
4b56523
Post Date - Code cleanup
johnregan3 Jul 27, 2016
6fd3084
Post Date - Hidden update Post Status
johnregan3 Jul 27, 2016
4a5c6c5
Post Date - Update Post Status
johnregan3 Jul 28, 2016
e6a9048
Post Date - JSDocs
johnregan3 Jul 28, 2016
ef76128
Post Date - JSDoc update
johnregan3 Jul 28, 2016
1ca7394
Post Date - Refactor and new file
johnregan3 Jul 28, 2016
097f211
Post Date - Saving post_date_gmt & post_status
johnregan3 Jul 28, 2016
4e58a2d
Rename customize-post-date to customize-post-date-control
westonruter Jul 28, 2016
467ebb8
Remove 'Post' from Status and Date control labels
westonruter Jul 28, 2016
62adabe
Include timezone and/or GMT offset in date control description
westonruter Jul 29, 2016
517cb2d
Add missing phpdoc and translator comment
westonruter Jul 29, 2016
4845ec2
Refactor how date fields are bound with the post_date setting property.
westonruter Jul 29, 2016
c79172e
Remove obsolete CSS rule
westonruter Jul 29, 2016
e032ebb
Allow post type registration to override date control label
westonruter Jul 29, 2016
dd10ae7
Prevent selective refresh requests from dropping queriedPostId
westonruter Jul 29, 2016
60a6f6a
Eliminate post_date_gmt and post_modified_gmt from setting value
westonruter Jul 29, 2016
4caf74d
Hide date control in edit post screen preview; add todo for sync
westonruter Jul 29, 2016
f382ef9
Fix eslint issues across plugin
westonruter Jul 29, 2016
faf80fa
Prevent low-fidelity live preview of post title from running at refresh
westonruter Jul 29, 2016
1dcaa23
Add getCurrentTime function
westonruter Jul 29, 2016
a0b0382
Use getDate (of month) instead of uncorrect getDay (of week)
westonruter Jul 29, 2016
7b2e6cb
Prevent erroneously showing post navigation link when status is trash
westonruter Jul 29, 2016
75bec41
Add dedicated post_status control, sync publish/future status with po…
westonruter Jul 29, 2016
ce5d417
Ensure JS sets post_author to integer
westonruter Jul 30, 2016
aab10bf
Prevent infinite partial fallbackRefresh when a setting is invalid
westonruter Jul 30, 2016
c70bd59
Turn off fallback_refresh for author partials
westonruter Jul 30, 2016
0df59d2
Let auto-draft posts be inserted with empty dates
westonruter Jul 30, 2016
43baeca
Allow draft posts to have empty dates which translate to current date…
westonruter Jul 30, 2016
0262640
Add button to reset post_date to current time
westonruter Jul 30, 2016
3b0d8ae
Merge branch 'develop' of https://github.com/xwp/wp-customize-posts i…
westonruter Jul 30, 2016
0efcaea
Force archives to refresh when dates change so posts can bre reordered
westonruter Jul 30, 2016
5680d0d
Filter the_posts to re-order after previewed changes have been applied
westonruter Jul 30, 2016
0ec6824
Do full refreshes on archive views when a post field is modified that…
westonruter Jul 30, 2016
28bc885
Add tests for Customize_Posts_Plugin and WP_Customize_Post_Date_Control
westonruter Jul 30, 2016
410f9cb
Add test for render_post_date
westonruter Jul 30, 2016
29077d4
Add tests for WP_Customize_Post_Setting
westonruter Jul 30, 2016
899ce61
Add tests and stubs for stubs
westonruter Jul 31, 2016
7f2e87c
Implement incomplete tests for WP_Customize_Posts
westonruter Jul 31, 2016
86e448c
Implement tests for WP_Customize_Posts_Preview
westonruter Jul 31, 2016
8dec6c8
Move month choices to main component class
westonruter Jul 31, 2016
641b7eb
Ensure that empty dates are not leaked when a setting is invalid
westonruter Jul 31, 2016
9b1fcdd
Attempt to fix Test_WP_Customize_Posts::test_enqueue_scripts double v…
westonruter Jul 31, 2016
7975f86
Force all post field partials to refresh even if an unrelated field w…
westonruter Aug 1, 2016
1b37d71
Leave partial placements in a loading state after they get refreshed …
westonruter Aug 1, 2016
76afa9f
Move deferred partial into its own file; clean up dependencies
westonruter Aug 1, 2016
d86532e
Move settingValidities collection into separate JS file
westonruter Aug 1, 2016
57bad17
Add scheduled countdown when status is future and time is after now
westonruter Aug 1, 2016
fb90a0a
Add grunt build to npm postinstall script
westonruter Aug 1, 2016
191b923
Use small for date control description; fix call to format_gmt_offset
westonruter Aug 1, 2016
70a7fd4
Re-use tz_string var
westonruter Aug 1, 2016
d95eaac
Update wp-plugin-dev-lib 449182d...ce4fce1: Use __FILE__ instead of _…
westonruter Aug 1, 2016
a385a60
Skip grunt build if not PHP>5.2
westonruter Aug 1, 2016
ebf331e
Only run grunt build if PHP>=5.3
westonruter Aug 1, 2016
18364e2
Improvement: Refine UI for post date control reset button 💯
Aug 1, 2016
04efe42
Fix: Wierd redirect by adding buttons type
Aug 1, 2016
ad6751d
Merge pull request #208 from ahmadawais/feature/issue-56-post-date-co…
westonruter Aug 1, 2016
276f8fb
Use link instead of button to re-use styles
westonruter Aug 1, 2016
41b8d0b
Show local timezone abbr as details summary on same line as title
westonruter Aug 2, 2016
2bac360
Update wp-plugin-dev-lib 449182d...4aba13a: Merge pull request #194 f…
westonruter Aug 2, 2016
b293245
Fix display of GMT offset
westonruter Aug 2, 2016
a7315e1
Fix reset post time by passing in post_date_gmt as empty as well
westonruter Aug 2, 2016
ce956fd
Fix race condition when saving publish post at current time
westonruter Aug 2, 2016
b7bcbe1
Improve logic for updating selected status option
westonruter Aug 2, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions css/wp-customize-post-date-control.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.customize-control-post_date {
background: #f00;
}
58 changes: 58 additions & 0 deletions js/customize-post-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
if ( postTypeObj.supports.author ) {
section.addAuthorControl();
}
section.addPostDateControl();
},

/**
Expand Down Expand Up @@ -479,6 +480,63 @@
return control;
},

/**
* Add post date control.
*
* @returns {wp.customize.Control} Added control.
*/
addPostDateControl: function() {
var section = this, control, setting = api( section.id ), postData, date, dateArray = {};

control = new api.controlConstructor.dynamic( section.id + '[post_date]', {
params: {
section: section.id,
priority: 21,
label: api.Posts.data.l10n.fieldPostDateLabel,
active: true,
settings: {
'default': setting.id
},
type: 'post_date',
setting_property: 'post_date_gmt'
}
} );

postData = _.clone( control.setting.get() );
date = new Date( postData.post_date_gmt );
dateArray.date = date.getDate().toString();
dateArray.month = ( date.getMonth() + 1 );
if ( dateArray.month <= 9 ) {
dateArray.month = '0' + dateArray.month;
}
dateArray.year = date.getFullYear().toString();
dateArray.hour = date.getHours().toString();
dateArray.min = date.getMinutes().toString();
control.params.date_data = dateArray;

control.deferred.embedded.done( function() {
_.each( control.params.date_data, function( val, type ) {
var input = control.container.find( '.date-input.' + type );
input.val( val );
} );
} );

// Override preview trying to de-activate control not present in preview context. See WP Trac #37270.
control.active.validate = function() {
return true;
};

// Register.
section.postFieldControls.post_date_gmt = control;
api.control.add( control.id, control );

if ( control.notifications ) {
control.notifications.add = section.addPostFieldControlNotification;
control.notifications.setting_property = control.params.setting_property;
}
return control;
},

/**
* Add post content control.
*
Expand Down
5 changes: 5 additions & 0 deletions php/class-customize-posts-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,10 @@ public function register_styles( WP_Styles $wp_styles ) {
$src = plugins_url( 'css/edit-post-preview-customize' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls' );
$wp_styles->add( $handle, $src, $deps, $this->version );

$handle = 'wp-customize-post-date-control';
$src = plugins_url( 'css/wp-customize-post-date-control' . $suffix, dirname( __FILE__ ) );
$deps = array();
$wp_styles->add( $handle, $src, $deps, $this->version );
}
}
115 changes: 115 additions & 0 deletions php/class-wp-customize-post-date-control.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Customize Post Date Control Class
*
* @package WordPress
* @subpackage Customize
*/


/**
* Class WP_Customize_Post_Date_Control
*/
class WP_Customize_Post_Date_Control extends WP_Customize_Dynamic_Control {

/**
* Type of control, used by JS.
*
* @access public
* @var string
*/
public $type = 'post_date';

/**
* Get the data to export to the client via JSON.
*
* @return array Array of parameters passed to the JavaScript.
*/
public function json() {
$exported = parent::json();
$exported['month_choices'] = $this->get_month_choices();
$exported['date_inputs'] = array(
'month',
'date',
'year',
'hour',
'min',
);
return $exported;
}

/**
* Render the Underscore template for this control.
*
* @access protected
* @codeCoverageIgnore
*/
protected function content_template() {
$data = $this->json();
?>
<#
_.defaults( data, <?php echo wp_json_encode( $data ) ?> );
data.input_id = 'input-' + String( Math.random() );
#>
<span class="customize-control-title"><label for="{{ data.input_id }}">{{ data.label }}</label></span>
<# if ( data.description ) { #>
<span class="description customize-control-description">{{ data.description }}</span>
<# } #>

<# _.each( data.date_inputs, function( type ) { #>
<# if ( 'month' === type ) { #>
<select class="date-input {{ type }}">
<# _.each( data.month_choices, function( choice ) { #>
<#
if ( _.isObject( choice ) && ! _.isUndefined( choice.text ) && ! _.isUndefined( choice.value ) ) {
text = choice.text;
value = choice.value;
}
#>
<option value="{{ value }}">{{ text }}</option>
<# } ); #>
</select>
<# } else { #>
<input
type="text"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnregan3 Can we use number as input type with max and min attributes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PatelUtkarsh I would love to do that. I think it would help greatly with validation. @westonruter How closely do we want to match the existing interface? It uses simple text inputs.

Copy link
Member

@PatelUtkarsh PatelUtkarsh Jul 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In number input that arrows(up and down) make it smaller(input needs more space) so maybe that's why the core is not using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PatelUtkarsh You know, I'm not really sure. I think they would just rather leave it alone since it's been used for so long. However, I think it's time for an update of that whole metabox. in addition to the UI changes, the JS for the date function could be greatly improved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How closely do we want to match the existing interface? It uses simple text inputs.

I really don't like the existing interface 😄

class="date-input {{ type }}"
value=""
/>
<# } #>
<# }); #>
<input
id="{{ data.input_id }}"
type="text"
<# _.each( data.input_attrs, function( value, key ) { #>
{{{ key }}}="{{ value }}"
<# } ) #>
<# if ( data.setting_property ) { #>
data-customize-setting-property-link="{{ data.setting_property }}"
<# } #>
/>
<?php
}

/**
* Generate options for the month Select.
*
* Based on touch_time().
*
* @see touch_time()
*
* @return array
*/
public function get_month_choices() {
global $wp_locale;
$months = array();
for ( $i = 1; $i < 13; $i = $i + 1 ) {
$month_number = zeroise( $i, 2 );
$month_text = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );

/* translators: 1: month number (01, 02, etc.), 2: month abbreviation */
$months[ $i ]['text'] = sprintf( __( '%1$s-%2$s', 'customize-posts' ), $month_number, $month_text );
$months[ $i ]['value'] = $month_number;
}
return $months;
}
}
3 changes: 3 additions & 0 deletions php/class-wp-customize-posts-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ public function get_post_field_partial_schema( $field_id = '' ) {
'post_status' => array(
'fallback_refresh' => true,
),
'post_date' => array(
'fallback_refresh' => true,
),
'post_content' => array(
'selector' => '.entry-content',
),
Expand Down
3 changes: 3 additions & 0 deletions php/class-wp-customize-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function __construct( WP_Customize_Manager $manager ) {
require_once dirname( __FILE__ ) . '/class-wp-customize-post-discussion-fields-control.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-setting.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-postmeta-setting.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-date-control.php';
require_once ABSPATH . WPINC . '/customize/class-wp-customize-partial.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-field-partial.php';

Expand Down Expand Up @@ -285,6 +286,7 @@ public function register_constructs() {
$this->manager->register_section_type( 'WP_Customize_Post_Section' );
$this->manager->register_control_type( 'WP_Customize_Dynamic_Control' );
$this->manager->register_control_type( 'WP_Customize_Post_Discussion_Fields_Control' );
$this->manager->register_control_type( 'WP_Customize_Post_Date_Control' );

$panel_priority = 900; // Before widgets.

Expand Down Expand Up @@ -580,6 +582,7 @@ public function enqueue_scripts() {
'fieldTitleLabel' => __( 'Title', 'customize-posts' ),
'fieldSlugLabel' => __( 'Slug', 'customize-posts' ),
'fieldPostStatusLabel' => __( 'Post Status', 'customize-posts' ),
'fieldPostDateLabel' => __( 'Post Date', 'customize-posts' ),
'fieldContentLabel' => __( 'Content', 'customize-posts' ),
'fieldExcerptLabel' => __( 'Excerpt', 'customize-posts' ),
'fieldDiscussionLabel' => __( 'Discussion', 'customize-posts' ),
Expand Down
136 changes: 136 additions & 0 deletions tests/php/test-class-wp-customize-post-date-control.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Tests for Test_WP_Customize_Post_Date_Control.
*
* @package WordPress
* @subpackage Customize
*/

/**
* Class Test_WP_Customize_Post_Date_Control
*/
class Test_WP_Customize_Post_Date_Control extends WP_UnitTestCase {

/**
* Customize Manager instance.
*
* @var WP_Customize_Manager
*/
public $wp_customize;

/**
* Component.
*
* @var WP_Customize_Posts
*/
public $posts;

/**
* Setup.
*
* @inheritdoc
*/
public function setUp() {
parent::setUp();
require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
$GLOBALS['wp_customize'] = new WP_Customize_Manager();
$this->wp_customize = $GLOBALS['wp_customize'];
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$this->posts = new WP_Customize_Posts( $this->wp_customize );
}

/**
* Teardown.
*
* @inheritdoc
*/
function tearDown() {
$this->wp_customize = null;
$this->posts = null;
unset( $GLOBALS['wp_customize'] );
unset( $GLOBALS['wp_scripts'] );
unset( $_REQUEST['nonce'] );
unset( $_REQUEST['customize_preview_post_nonce'] );
unset( $_REQUEST['wp_customize'] );
unset( $_GET['previewed_post'] );
parent::tearDown();
}

/**
* Do Customizer boot actions.
*/
function do_customize_boot_actions() {
// Remove actions that call add_theme_support( 'title-tag' ).
remove_action( 'after_setup_theme', 'twentyfifteen_setup' );
remove_action( 'after_setup_theme', 'twentysixteen_setup' );

$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['customized'] = '';
do_action( 'setup_theme' );
$_REQUEST['nonce'] = wp_create_nonce( 'preview-customize_' . $this->wp_customize->theme()->get_stylesheet() );
$_REQUEST['customize_preview_post_nonce'] = wp_create_nonce( 'customize_preview_post' );
do_action( 'after_setup_theme' );
do_action( 'customize_register', $this->wp_customize );
$this->wp_customize->customize_preview_init();
do_action( 'wp', $GLOBALS['wp'] );
$_REQUEST['wp_customize'] = 'on';
$_GET['previewed_post'] = 123;
}

/**
* Creates the dynamic control.
*
* @param int $setting_id The setting ID.
*/
public function control( $setting_id ) {
$args = array(
'label' => 'Heading Text',
'section' => $setting_id,
'settings' => $setting_id,
'priority' => 1,
'field_type' => 'post_date',
'setting_property' => 'post_date',
'input_attrs' => array( 'data-test' => 'value-test' ),
);
return new WP_Customize_Post_Date_Control( $this->wp_customize, 'dynamic_control_id', $args );
}

/**
* Filter to register a setting, section, & control.
*/
public function customize_register() {
$setting_id = 'post[post][123]';

$this->wp_customize->add_setting( $setting_id );

$setting = new WP_Customize_Post_Section( $this->wp_customize, $setting_id, array(
'panel' => 'posts[post]',
'post_setting' => $this->wp_customize->get_setting( $setting_id ),
'priority' => 1,
) );
$this->wp_customize->add_section( $setting );

$control = $this->control( $setting_id );
$this->wp_customize->add_control( $control );
}

/**
* Test export data to JS.
*
* @see WP_Customize_Dynamic_Control::json()
*/
public function test_json() {
add_action( 'customize_register', array( $this, 'customize_register' ), 15 );
$this->do_customize_boot_actions();

$control = $this->control( 'post[post][123]' );
$json = $control->json();

$this->assertArrayHasKey( 'input_attrs', $json );
$this->assertArrayHasKey( 'field_type', $json );
$this->assertArrayHasKey( 'setting_property', $json );
$this->assertEquals( array( 'data-test' => 'value-test' ), $json['input_attrs'] );
$this->assertEquals( 'post_date', $json['field_type'] );
$this->assertEquals( 'post_date', $json['setting_property'] );
}
}