Skip to content

Instantly share code, notes, and snippets.

@michael-ecklund
Last active April 2, 2016 15:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-ecklund/853fcbf1a8a67998a837 to your computer and use it in GitHub Desktop.
Save michael-ecklund/853fcbf1a8a67998a837 to your computer and use it in GitHub Desktop.
Complete rewrite in code which is more "understandable" to me. Exact same code as core code found `post_submit_meta_box();` here: `/wp-admin/includes/meta-boxes.php`
/**
* Displays post submit form fields.
*
* @since 2.7.0
*
* @global string $action
*
* @param WP_Post $post Current post object.
* @param array $args {
* Array of arguments for building the post submit meta box.
*
* @type string $id Meta box ID.
* @type string $title Meta box title.
* @type callable $callback Meta box display callback.
* @type array $args Extra meta box arguments.
* }
*/
public function meta_box_actions( $post, $args = array() ) {
global $action;
$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 );
// BoF #submitpost .submitbox
echo '<div class="submitbox" id="submitpost">' . PHP_EOL;
// BoF #minor-publishing
echo '<div id="minor-publishing">' . PHP_EOL;
// Hidden submit button early on so that the browser chooses the right button when form is submitted with Return key
echo '<div style="display:none;">' . PHP_EOL;
submit_button( __( 'Save' ), 'button', 'save' );
echo '</div>' . PHP_EOL;
// BoF #minor-publishing-actions
echo '<div id="minor-publishing-actions">' . PHP_EOL;
// BoF #save-action
echo '<div id="save-action">' . PHP_EOL;
if ( $post->post_status != 'publish' && $post->post_status != 'future' && $post->post_status != 'pending' ) {
if ( $post->post_status == 'private' ) {
$style = 'style="display:none;"';
} else {
$style = '';
}
echo '<input ' . $style . ' type="submit" name="save" id="save-post" value="' . esc_attr( 'Save Draft' ) . '" class="button" />' . PHP_EOL;
echo '<span class="spinner"></span>' . PHP_EOL;
} elseif ( $post->post_status == 'pending' && $can_publish ) {
echo '<input type="submit" name="save" id="save-post" value="' . esc_attr( 'Save as Pending' ) . '" class="button" />' . PHP_EOL;
echo '<span class="spinner"></span>' . PHP_EOL;
}
echo '</div><!-- end #save-action -->' . PHP_EOL;
// EoF #save-action
if ( is_post_type_viewable( $post_type_object ) ) {
// BoF #preview-action
echo '<div id="preview-action">' . PHP_EOL;
$preview_link = esc_url( get_preview_post_link( $post ) );
if ( $post->post_status == 'publish' ) {
$preview_button = 'Preview Changes';
} else {
$preview_button = 'Preview';
}
echo '<a class="preview button" href="' . $preview_link . '" target="wp-preview-' . (int) $post->ID . '" id="post-preview">' . $preview_button . '</a>' . PHP_EOL;
echo '<input type="hidden" name="wp-preview" id="wp-preview" value="" />' . PHP_EOL;
echo '</div><!-- end #preview-action -->' . PHP_EOL;
// EoF #preview-action
}// public post type
/**
* Fires before the post time/date setting in the Publish meta box.
*
* @since 4.4.0
*
* @param WP_Post $post WP_Post object for the current post.
*/
do_action( 'post_submitbox_minor_actions', $post );
echo '<div class="clear"></div>' . PHP_EOL;
echo '</div><!-- #minor-publishing-actions -->' . PHP_EOL;
// EoF #minor-publishing-actions
// BoF #misc-publishing-actions
echo '<div id="misc-publishing-actions">' . PHP_EOL;
// BoF .misc-pub-section .misc-pub-post-status
echo '<div class="misc-pub-section misc-pub-post-status">' . PHP_EOL;
echo '<label for="post_status">Status:</label>' . PHP_EOL;
echo '<span id="post-status-display">' . PHP_EOL;
switch ( $post->post_status ) {
case 'private':
_e( 'Privately Published' );
break;
case 'publish':
_e( 'Published' );
break;
case 'future':
_e( 'Scheduled' );
break;
case 'pending':
_e( 'Pending Review' );
break;
case 'draft':
case 'auto-draft':
_e( 'Draft' );
break;
}
echo '</span>' . PHP_EOL;
if ( $post->post_status == 'publish' || $post->post_status == 'private' || $can_publish ) {
if ( $post->post_status == 'private' ) {
$style = 'style="display:none"';
} else {
$style = '';
}
echo '<a href="#post_status" ' . $style . ' class="edit-post-status hide-if-no-js"><span aria-hidden="true">Edit</span> <span class="screen-reader-text">Edit status</span></a>' . PHP_EOL;
// BoF #post-status-select .hide-if-js
echo '<div id="post-status-select" class="hide-if-js">' . PHP_EOL;
echo '<input type="hidden" name="hidden_post_status" id="hidden_post_status" value="' . esc_attr( $post->post_status ) . '" />' . PHP_EOL;
echo '<select name="post_status" id="post_status">' . PHP_EOL;
if ( $post->post_status == 'publish' ) {
echo '<option ' . selected( $post->post_status, 'publish', false ) . ' value="publish">Published</option>' . PHP_EOL;
} elseif ( $post->post_status == 'private' ) {
echo '<option ' . selected( $post->post_status, 'private', false ) . ' value="publish">Privately Published</option>' . PHP_EOL;
} elseif ( $post->post_status == 'future' ) {
echo '<option ' . selected( $post->post_status, 'future', false ) . ' value="future">Scheduled</option>' . PHP_EOL;
}
echo '<option ' . selected( $post->post_status, 'pending', false ) . ' value="pending">Pending Review</option>' . PHP_EOL;
if ( $post->post_status == 'auto-draft' ) {
echo '<option ' . selected( $post->post_status, 'auto-draft', false ) . ' value="draft">Draft</option>' . PHP_EOL;
} else {
echo '<option ' . selected( $post->post_status, 'draft', false ) . ' value="draft">Draft</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;
echo '<a href="#post_status" class="save-post-status hide-if-no-js button">OK</a>' . PHP_EOL;
echo '<a href="#post_status" class="cancel-post-status hide-if-no-js button-cancel">Cancel</a>' . PHP_EOL;
echo '</div><!-- end #post-status-select .hide-if-js -->' . PHP_EOL;
// EoF #post-status-select .hide-if-js
}
echo '</div><!-- end .misc-pub-section .misc-pub-post-status -->' . PHP_EOL;
// EoF .misc-pub-section .misc-pub-post-status
// BoF #visibility .misc-pub-section .misc-pub-visibility
echo '<div class="misc-pub-section misc-pub-visibility" id="visibility">' . PHP_EOL;
echo 'Visibility: <span id="post-visibility-display">' . PHP_EOL;
if ( $post->post_status == 'private' ) {
$post->post_password = '';
$visibility = 'private';
$visibility_trans = __( 'Private' );
} elseif ( ! empty( $post->post_password ) ) {
$visibility = 'password';
$visibility_trans = __( 'Password protected' );
} elseif ( $post_type == 'post' && is_sticky( $post->ID ) ) {
$visibility = 'public';
$visibility_trans = __( 'Public, Sticky' );
} else {
$visibility = 'public';
$visibility_trans = __( 'Public' );
}
echo esc_html( $visibility_trans );
echo '</span>' . PHP_EOL;
if ( $can_publish ) {
echo '<a href="#visibility" class="edit-visibility hide-if-no-js"><span aria-hidden="true">Edit</span> <span class="screen-reader-text">Edit visibility</span></a>' . PHP_EOL;
// BoF #post-visibility-select .hide-if-js
echo '<div id="post-visibility-select" class="hide-if-js">' . PHP_EOL;
echo '<input type="hidden" name="hidden_post_password" id="hidden-post-password" value="'.esc_attr($post->post_password).'" />' . PHP_EOL;
if ( $post_type == 'post' ) {
echo '<input type="checkbox" style="display:none" name="hidden_post_sticky" id="hidden-post-sticky" value="sticky" ' . checked( is_sticky( $post->ID ), true, false ) . ' />' . PHP_EOL;
}
echo '<input type="hidden" name="hidden_post_visibility" id="hidden-post-visibility" value="' . esc_attr( $visibility ) . '" />' . PHP_EOL;
echo '<input type="radio" name="visibility" id="visibility-radio-public" value="public" ' . checked( $visibility, 'public', false ) . ' /> <label for="visibility-radio-public" class="selectit">Public</label><br />' . PHP_EOL;
if ( $post_type == 'post' && current_user_can( 'edit_others_posts' ) ) {
echo '<span id="sticky-span"><input id="sticky" name="sticky" type="checkbox" value="sticky" ' . checked( is_sticky( $post->ID ), true, false ) . ' /> <label for="sticky" class="selectit">Stick this post to the front page</label><br /></span>' . PHP_EOL;
}
echo '<input type="radio" name="visibility" id="visibility-radio-password" value="password" ' . checked( $visibility, 'password', false ) . ' /> <label for="visibility-radio-password" class="selectit">Password protected</label><br />' . PHP_EOL;
echo '<span id="password-span"><label for="post_password">Password:</label> <input type="text" name="post_password" id="post_password" value="' . esc_attr( $post->post_password ) . '" maxlength="20" /><br /></span>' . PHP_EOL;
echo '<input type="radio" name="visibility" id="visibility-radio-private" value="private" ' . checked( $visibility, 'private', false ) . ' /> <label for="visibility-radio-private" class="selectit">Private</label><br />' . PHP_EOL;
echo '<p>' . PHP_EOL;
echo '<a href="#visibility" class="save-post-visibility hide-if-no-js button">OK</a>' . PHP_EOL;
echo '<a href="#visibility" class="cancel-post-visibility hide-if-no-js button-cancel">Cancel</a>' . PHP_EOL;
echo '</p>' . PHP_EOL;
echo '</div><!-- end #post-visibility-select .hide-if-js -->' . PHP_EOL;
// RoF #post-visibility-select .hide-if-js
}
echo '</div><!-- #visibility .misc-pub-section .misc-pub-visibility -->' . PHP_EOL;
// EoF #visibility .misc-pub-section .misc-pub-visibility
/* translators: Publish box date format, see http://php.net/date */
$datef = __( 'M j, Y @ H:i' );
if ( $post->ID != 0 ) {
if ( 'future' == $post->post_status ) { // scheduled for publishing at a future date
$stamp = __( 'Scheduled for: <b>%1$s</b>' );
} elseif ( $post->post_status == 'publish' || $post->post_status == 'private' ) { // already published
$stamp = __( 'Published on: <b>%1$s</b>' );
} elseif ( $post->post_date_gmt == '0000-00-00 00:00:00' ) { // draft, 1 or more saves, no date specified
$stamp = __( 'Publish <b>immediately</b>' );
} elseif ( time() < strtotime( $post->post_date_gmt . ' +0000' ) ) { // draft, 1 or more saves, future date specified
$stamp = __( 'Schedule for: <b>%1$s</b>' );
} else { // draft, 1 or more saves, date specified
$stamp = __( 'Publish on: <b>%1$s</b>' );
}
$date = date_i18n( $datef, strtotime( $post->post_date ) );
} else { // draft (no saves, and thus no date specified)
$stamp = __( 'Publish <b>immediately</b>' );
$date = date_i18n( $datef, strtotime( current_time( 'mysql' ) ) );
}
if ( ! empty( $args['args']['revisions_count'] ) ) {
$revisions_to_keep = wp_revisions_to_keep( $post );
}
// BoF .misc-pub-section .misc-pub-revisions
echo '<div class="misc-pub-section misc-pub-revisions">' . PHP_EOL;
if ( $revisions_to_keep > 0 && $revisions_to_keep <= $args['args']['revisions_count'] ) {
echo '<span title="' . esc_attr( sprintf( __( 'Your site is configured to keep only the last %s revisions.' ), number_format_i18n( $revisions_to_keep ) ) ) . '">' . PHP_EOL;
printf( __( 'Revisions: %s' ), '<b>' . number_format_i18n( $args['args']['revisions_count'] ) . '+</b>' );
echo '</span>' . PHP_EOL;
} else {
printf( __( 'Revisions: %s' ), '<b>' . number_format_i18n( $args['args']['revisions_count'] ) . '</b>' );
}
echo ' <a class="hide-if-no-js" href="' . esc_url( get_edit_post_link( $args['args']['revision_id'] ) ) . '"><span aria-hidden="true">Browse</span> <span class="screen-reader-text">Browse revisions</span></a>' . PHP_EOL;
echo '</div><!-- end .misc-pub-section .misc-pub-revisions -->' . PHP_EOL;
// EoF .misc-pub-section .misc-pub-revisions
if ( $can_publish ) {// Contributors don't get to choose the date of publish
// BoF .misc-pub-section .curtime .misc-pub-curtime
echo '<div class="misc-pub-section curtime misc-pub-curtime">' . PHP_EOL;
echo '<span id="timestamp">' . sprintf( $stamp, $date ) . '</span>' . PHP_EOL;
echo '<a href="#edit_timestamp" class="edit-timestamp hide-if-no-js"><span aria-hidden="true">Edit</span> <span class="screen-reader-text">Edit date and time</span></a>' . PHP_EOL;
echo '<fieldset id="timestampdiv" class="hide-if-js">' . PHP_EOL;
echo '<legend class="screen-reader-text">Date and time</legend>' . PHP_EOL;
touch_time( ( $action === 'edit' ), 1 );
echo '</fieldset>' . PHP_EOL;
echo '</div><!-- end .misc-pub-section .curtime .misc-pub-curtime -->' . PHP_EOL;
// EoF .misc-pub-section .curtime .misc-pub-curtime
}
/**
* Fires after the post time/date setting in the Publish meta box.
*
* @since 2.9.0
* @since 4.4.0 Added the `$post` parameter.
*
* @param WP_Post $post WP_Post object for the current post.
*/
do_action( 'post_submitbox_misc_actions', $post );
echo '</div><!-- end #minor-publishing-actions -->' . PHP_EOL;
// EoF #minor-publishing-actions
echo '<div class="clear"></div>' . PHP_EOL;
echo '</div><!-- end #minor-publishing -->' . PHP_EOL;
// EoF #minor-publishing
// BoF #major-publishing-actions
echo '<div id="major-publishing-actions">' . PHP_EOL;
/**
* Fires at the beginning of the publishing actions section of the Publish meta box.
*
* @since 2.7.0
*/
do_action( 'post_submitbox_start' );
// BoF #delete-action
echo '<div id="delete-action">' . PHP_EOL;
if ( current_user_can( "delete_post", $post->ID ) ) {
if ( ! EMPTY_TRASH_DAYS ) {
$delete_text = 'Delete Permanently';
} else {
$delete_text = 'Move to Trash';
}
echo '<a class="submitdelete deletion" href="' . get_delete_post_link( $post->ID ) . '">' . $delete_text . '</a>' . PHP_EOL;
}
echo '</div><!-- end #delete-action -->' . PHP_EOL;
// EoF #delete-action
// BoF #publishing-action
echo '<div id="publishing-action">' . PHP_EOL;
echo '<span class="spinner"></span>' . PHP_EOL;
if ( ! in_array( $post->post_status, array( 'publish', 'future', 'private' ) ) || 0 == $post->ID ) {
if ( $can_publish ) {
if ( ! empty( $post->post_date_gmt ) && time() < strtotime( $post->post_date_gmt . ' +0000' ) ) {
echo '<input name="original_publish" type="hidden" id="original_publish" value="' . esc_attr( 'Schedule' ) . '" />' . PHP_EOL;
submit_button( 'Schedule', 'primary button-large', 'publish', false );
} else {
echo '<input name="original_publish" type="hidden" id="original_publish" value="' . esc_attr( 'Publish' ) . '" />' . PHP_EOL;
submit_button( 'Publish', 'primary button-large', 'publish', false );
}
} else {
echo '<input name="original_publish" type="hidden" id="original_publish" value="' . esc_attr( 'Submit for Review' ) . '" />' . PHP_EOL;
submit_button( 'Submit for Review', 'primary button-large', 'publish', false );
}
} else {
echo '<input name="original_publish" type="hidden" id="original_publish" value="' . esc_attr( 'Update' ) . '" />' . PHP_EOL;
echo '<input name="save" type="submit" class="button button-primary button-large" id="publish" value="' . esc_attr( 'Update' ) . '" />' . PHP_EOL;
}
echo '</div><!-- end #publishing-action -->' . PHP_EOL;
// EoF #publishing-action
echo '<div class="clear"></div>' . PHP_EOL;
echo '</div><!-- end #major-publishing-actions -->' . PHP_EOL;
// EoF #major-publishing-actions
echo '</div><!-- end #submitpost .submitbox -->' . PHP_EOL;
// EoF #submitpost .submitbox
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment