refresher = $refresher;
add_action( 'init', [ $this, 'register' ] );
add_filter( 'wp_insert_post_data', [ $this, 'prevent_public_refresh_copy' ], 100, 2 );
add_filter( 'gettext', [ $this, 'change_publish_label' ], 100, 3 );
add_filter( 'gettext_with_context', [ $this, 'change_schedule_label' ], 100, 4 );
}
/** Registers the 'republish', 'future-republish', and 'republish-trash' post statuses. */
public function register(): void {
register_post_status(
'republish',
[
'label' => _x( 'Refresh', 'post status', 'admin-optimizer' ),
'internal' => true,
]
);
register_post_status(
'future-republish',
[
'label' => _x( 'Scheduled Refresh', 'post status', 'admin-optimizer' ),
'protected' => true,
'public' => false,
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
/* translators: %s: Number of refresh copies. */
'label_count' => _n_noop( 'Scheduled Refresh (%s)', 'Scheduled Refreshes (%s)', 'admin-optimizer' ),
]
);
register_post_status(
'republish-trash',
[
'label' => _x( 'Pending Refresh Cleanup', 'post status', 'admin-optimizer' ),
'internal' => true,
]
);
}
/**
* Filters wp_insert_post_data so a refresh copy's status maps to its internal equivalent.
*
* @param array $data Sanitized post data about to be saved.
* @param array $postarr Raw post array passed to wp_insert_post().
* @return array
*/
public function prevent_public_refresh_copy( $data, $postarr ) {
$post_id = ! empty( $postarr['ID'] ) ? absint( $postarr['ID'] ) : 0;
if ( $post_id && $this->refresher->is_refresh_copy( $post_id ) ) {
if ( 'publish' === $data['post_status'] ) {
$data['post_status'] = 'republish';
} elseif ( 'future' === $data['post_status'] ) {
$data['post_status'] = 'future-republish';
}
}
return $data;
}
/**
* Filters the 'Publish' button label to 'Refresh' when editing a refresh copy.
*
* @param string $translation Current translation of $text.
* @param string $text Text being translated.
* @param string $domain Text domain.
* @return string
*/
public function change_publish_label( $translation, $text, $domain ) {
if ( 'default' === $domain && 'Publish' === $text && $this->current_post_is_refresh_copy() ) {
return __( 'Refresh', 'admin-optimizer' );
}
return $translation;
}
/**
* Filters the 'Schedule' button label to 'Schedule Refresh' when editing a refresh copy.
*
* @param string $translation Current translation of $text.
* @param string $text Text being translated.
* @param string $context Disambiguating translation context.
* @param string $domain Text domain.
* @return string
*/
public function change_schedule_label( $translation, $text, $context, $domain ) {
if ( 'default' === $domain && 'Schedule' === $text && 'post action/button label' === $context && $this->current_post_is_refresh_copy() ) {
return __( 'Schedule Refresh', 'admin-optimizer' );
}
return $translation;
}
/** Whether the post currently being edited in wp-admin is a refresh copy. */
private function current_post_is_refresh_copy(): bool {
global $pagenow;
if ( ! in_array( $pagenow, [ 'post.php', 'post-new.php' ], true ) ) {
return false;
}
$post = get_post();
return $post instanceof \WP_Post && $this->refresher->is_refresh_copy( $post->ID );
}
}