Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 20 additions & 7 deletions src/js/_enqueues/admin/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ jQuery( function($) {
postId = $('#post_ID').val() || 0,
$submitpost = $('#submitpost'),
releaseLock = true,
statusBeforePrivate = null,
$postVisibilitySelect = $('#post-visibility-select'),
$timestampdiv = $('#timestampdiv'),
$postStatusSelect = $('#post-status-select'),
Expand Down Expand Up @@ -757,7 +758,7 @@ jQuery( function($) {
if ( ! $timestampdiv.length )
return true;

var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
var attemptedDate, originalDate, currentDate, publishOn, selectedPostStatus, postStatus = $('#post_status'),
optPublish = $('option[value="publish"]', postStatus), aa = $('#aa').val(),
mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();

Expand Down Expand Up @@ -823,15 +824,23 @@ jQuery( function($) {
// Add "privately published" to post status when applies.
if ( $postVisibilitySelect.find('input:radio:checked').val() == 'private' ) {
$('#publish').val( __( 'Update' ) );
if ( postStatus.val() !== 'publish' ) {
statusBeforePrivate = postStatus.val();
}
if ( 0 === optPublish.length ) {
postStatus.append('<option value="publish">' + __( 'Privately Published' ) + '</option>');
postStatus.append('<option value="publish" data-private-status-option="true">' + __( 'Privately Published' ) + '</option>');
} else {
optPublish.html( __( 'Privately Published' ) );
}
$('option[value="publish"]', postStatus).prop('selected', true);
$('#misc-publishing-actions .edit-post-status').hide();
} else {
if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
if ( null !== statusBeforePrivate ) {
optPublish.html( __( 'Published' ) );
optPublish.filter('[data-private-status-option]').remove();
postStatus.val( statusBeforePrivate );
statusBeforePrivate = null;
} else if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
if ( optPublish.length ) {
optPublish.remove();
postStatus.val($('#hidden_post_status').val());
Expand All @@ -843,22 +852,26 @@ jQuery( function($) {
$('#misc-publishing-actions .edit-post-status').show();
}

selectedPostStatus = $('option:selected', postStatus);

// Update "Status:" to currently selected status.
$('#post-status-display').text(
// Remove any potential tags from post status text.
wp.sanitize.stripTagsAndEncodeText( $('option:selected', postStatus).text() )
wp.sanitize.stripTagsAndEncodeText( selectedPostStatus.text() )
);

// Show or hide the "Save Draft" button.
if (
$('option:selected', postStatus).val() == 'private' ||
$('option:selected', postStatus).val() == 'publish'
selectedPostStatus.val() == 'private' ||
selectedPostStatus.val() == 'publish'
) {
$('#save-post').hide();
} else {
$('#save-post').show();
if ( $('option:selected', postStatus).val() == 'pending' ) {
if ( selectedPostStatus.val() == 'pending' ) {
$('#save-post').show().val( __( 'Save as Pending' ) );
} else if ( selectedPostStatus.attr( 'data-save-text' ) ) {
$('#save-post').show().val( selectedPostStatus.attr( 'data-save-text' ) );
} else {
$('#save-post').show().val( __( 'Save Draft' ) );
}
Expand Down
54 changes: 48 additions & 6 deletions src/wp-admin/includes/meta-boxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
function post_submit_meta_box( $post, $args = array() ) {
global $action;

$post_id = (int) $post->ID;
$post_type = $post->post_type;
$post_type_object = get_post_type_object( $post_type );
$can_publish = current_user_can( $post_type_object->cap->publish_posts );
$post_id = (int) $post->ID;
$post_type = $post->post_type;
$post_type_object = get_post_type_object( $post_type );
$post_status_object = get_post_status_object( $post->post_status );
$can_publish = current_user_can( $post_type_object->cap->publish_posts );
?>
<div class="submitbox" id="submitpost">

Expand All @@ -48,12 +49,16 @@ function post_submit_meta_box( $post, $args = array() ) {
<div id="save-action">
<?php
if ( ! in_array( $post->post_status, array( 'publish', 'future', 'pending' ), true ) ) {
$private_style = '';
$private_style = '';
$save_button_text = __( 'Save Draft' );
if ( 'private' === $post->post_status ) {
$private_style = 'style="display:none"';
} elseif ( $post_status_object && ! $post_status_object->_builtin ) {
/* translators: %s: Post status label. */
$save_button_text = sprintf( __( 'Save as %s' ), $post_status_object->label );
}
?>
<input <?php echo $private_style; ?> type="submit" name="save" id="save-post" value="<?php esc_attr_e( 'Save Draft' ); ?>" class="button" />
<input <?php echo $private_style; ?> type="submit" name="save" id="save-post" value="<?php echo esc_attr( $save_button_text ); ?>" class="button" />
<span class="spinner"></span>
<?php } elseif ( 'pending' === $post->post_status && $can_publish ) { ?>
<input type="submit" name="save" id="save-post" value="<?php esc_attr_e( 'Save as Pending' ); ?>" class="button" />
Expand Down Expand Up @@ -121,6 +126,11 @@ function post_submit_meta_box( $post, $args = array() ) {
case 'auto-draft':
_e( 'Draft' );
break;
default:
if ( $post_status_object ) {
echo esc_html( $post_status_object->label );
}
break;
}
?>
</span>
Expand Down Expand Up @@ -161,6 +171,38 @@ function post_submit_meta_box( $post, $args = array() ) {
<?php else : ?>
<option<?php selected( $post->post_status, 'draft' ); ?> value='draft'><?php _e( 'Draft' ); ?></option>
<?php endif; ?>
<?php
/**
* Filters the custom post statuses available in the Publish meta box.
*
* The current registered custom post status is always included.
* This filter controls presentation only and does not grant permission
* to use a status.
*
* @since 7.1.0
*
* @param string[] $custom_post_statuses Custom post status names.
* @param WP_Post $post Current post object.
*/
$custom_post_statuses = apply_filters( 'post_submitbox_custom_statuses', array(), $post );

if ( $post_status_object && ! $post_status_object->_builtin && ! in_array( $post->post_status, $custom_post_statuses, true ) ) {
$custom_post_statuses[] = $post->post_status;
}

foreach ( $custom_post_statuses as $custom_post_status_name ) {
$custom_post_status = get_post_status_object( $custom_post_status_name );
if ( ! $custom_post_status || $custom_post_status->_builtin ) {
continue;
}

/* translators: %s: Post status label. */
$save_button_text = sprintf( __( 'Save as %s' ), $custom_post_status->label );
?>
<option<?php selected( $post->post_status, $custom_post_status->name ); ?> value="<?php echo esc_attr( $custom_post_status->name ); ?>" data-save-text="<?php echo esc_attr( $save_button_text ); ?>"><?php echo esc_html( $custom_post_status->label ); ?></option>
<?php
}
?>
</select>
<a href="#post_status" class="save-post-status hide-if-no-js button"><?php _e( 'OK' ); ?></a>
<a href="#post_status" class="cancel-post-status hide-if-no-js button-cancel"><?php _e( 'Cancel' ); ?></a>
Expand Down
138 changes: 138 additions & 0 deletions tests/phpunit/tests/admin/metaBoxes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* @group admin
*/
class Tests_Admin_MetaBoxes extends WP_UnitTestCase {
/**
* Editor user ID.
*
* @var int
*/
private static $editor_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
}

public function tear_down() {
remove_filter( 'post_submitbox_custom_statuses', array( $this, 'filter_post_submitbox_custom_statuses' ) );
_unregister_post_status( 'reviewed' );
_unregister_post_status( 'hidden-status' );
unset( $GLOBALS['post'], $GLOBALS['action'] );

parent::tear_down();
}

/**
* Adds the reviewed status to the post Publish meta box.
*
* @param string[] $statuses Post status names.
* @param WP_Post $post Current post object.
* @return string[] Post status names.
*/
public function filter_post_submitbox_custom_statuses( $statuses, $post ) {
if ( 'post' === $post->post_type ) {
$statuses[] = 'reviewed';
}

return $statuses;
}

/**
* @ticket 12706
*
* @covers ::post_submit_meta_box
*/
public function test_post_submit_meta_box_includes_filtered_custom_post_statuses() {
require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';

register_post_status(
'reviewed',
array(
'label' => 'Reviewed & approved',
'protected' => true,
'show_in_admin_status_list' => false,
)
);
register_post_status(
'hidden-status',
array(
'label' => 'Hidden status',
'protected' => true,
'show_in_admin_status_list' => true,
)
);
add_filter( 'post_submitbox_custom_statuses', array( $this, 'filter_post_submitbox_custom_statuses' ), 10, 2 );

$post = self::factory()->post->create_and_get();

wp_set_current_user( self::$editor_id );
$GLOBALS['post'] = $post;
$GLOBALS['action'] = 'edit';

ob_start();
post_submit_meta_box( $post );
$output = ob_get_clean();

$this->assertStringContainsString( 'value="reviewed" data-save-text="Save as Reviewed &amp; approved">Reviewed &amp; approved</option>', $output );
$this->assertStringNotContainsString( 'Hidden status', $output );

$page = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) );
$GLOBALS['post'] = $page;

ob_start();
post_submit_meta_box( $page );
$page_output = ob_get_clean();

$this->assertStringNotContainsString( 'Reviewed &amp; approved', $page_output );
}

/**
* @ticket 12706
*
* @covers ::post_submit_meta_box
* @covers ::_wp_translate_postdata
*/
public function test_post_submit_meta_box_includes_and_preserves_current_custom_post_status() {
require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';

register_post_status(
'hidden-status',
array(
'label' => 'Hidden status',
'protected' => true,
)
);

wp_set_current_user( self::$editor_id );
$post = self::factory()->post->create_and_get(
array(
'post_author' => self::$editor_id,
'post_status' => 'hidden-status',
)
);

$GLOBALS['post'] = $post;
$GLOBALS['action'] = 'edit';

ob_start();
post_submit_meta_box( $post );
$output = ob_get_clean();

$this->assertMatchesRegularExpression( '/<span id="post-status-display">\s*Hidden status\s*<\/span>/', $output );
$this->assertMatchesRegularExpression( '/<option[^>]+selected=[\'\"]selected[\'\"][^>]+value=[\'\"]hidden-status[\'\"][^>]*>Hidden status<\/option>/', $output );
$this->assertStringContainsString( 'value="Save as Hidden status"', $output );

$translated_post_data = _wp_translate_postdata(
true,
array(
'post_ID' => $post->ID,
'post_type' => 'post',
'post_status' => 'hidden-status',
'save' => 'Save as Hidden status',
)
);

$this->assertSame( 'hidden-status', $translated_post_data['post_status'] );
}
}
1 change: 1 addition & 0 deletions tests/qunit/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
<script src="wp-admin/js/widgets/test-media-gallery-widget.js"></script>
<script src="wp-admin/js/widgets/test-media-video-widget.js"></script>
<script src="wp-includes/js/media/test-models.js"></script>
<script src="wp-admin/js/post.js"></script>

<!-- Customizer templates for sections -->
<script type="text/html" id="tmpl-customize-section-default">
Expand Down
Loading
Loading