mirror of
https://github.com/WordPress/five-for-the-future.git
synced 2025-07-02 01:01:18 +03:00
Add ability to toggle deactivated status from edit screen actions
This commit is contained in:
parent
e4deb6809a
commit
f7f7ab4989
|
@ -25,6 +25,11 @@ add_action( 'pre_get_posts', __NAMESPACE__ . '\filter_query' );
|
|||
// List table columns.
|
||||
add_filter( 'manage_edit-' . CPT_ID . '_columns', __NAMESPACE__ . '\add_list_table_columns' );
|
||||
add_action( 'manage_' . CPT_ID . '_posts_custom_column', __NAMESPACE__ . '\populate_list_table_columns', 10, 2 );
|
||||
// Deactivate & reactivate handling.
|
||||
add_filter( 'post_row_actions', __NAMESPACE__ . '\add_row_action', 10, 2 );
|
||||
add_action( 'post_action_deactivate', __NAMESPACE__ . '\handle_activation_action', 10, 3 );
|
||||
add_action( 'post_action_reactivate', __NAMESPACE__ . '\handle_activation_action', 10, 3 );
|
||||
add_action( 'admin_notices', __NAMESPACE__ . '\action_success_message' );
|
||||
|
||||
// Front end hooks.
|
||||
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
|
||||
|
@ -130,6 +135,95 @@ function register_custom_post_status() {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject deactivate/reactivate actions into row actions.
|
||||
*
|
||||
* @return array An array of row action links.
|
||||
*/
|
||||
function add_row_action( $actions, $post ) {
|
||||
// Not a pledge, or can't edit the post.
|
||||
if ( CPT_ID !== $post->post_type || ! current_user_can( 'edit_post', $post->ID ) ) {
|
||||
return $actions;
|
||||
}
|
||||
$post_type_object = get_post_type_object( $post->post_type );
|
||||
if ( FiveForTheFuture\PREFIX . '-deactivated' === $post->post_status ) {
|
||||
$actions['reactivate'] = sprintf(
|
||||
'<a href="%s" aria-label="%s">%s</a>',
|
||||
wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=reactivate', $post->ID ) ), 'reactivate-post_' . $post->ID ),
|
||||
/* translators: %s: Post title. */
|
||||
esc_attr( sprintf( __( 'Reactivate pledge “%s”', 'wporg-5ftf' ), $post->post_title ) ),
|
||||
__( 'Reactivate', 'wporg-5ftf' )
|
||||
);
|
||||
} else {
|
||||
unset( $actions['trash'] );
|
||||
$actions['deactivate'] = sprintf(
|
||||
'<a href="%s" aria-label="%s">%s</a>',
|
||||
wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=deactivate', $post->ID ) ), 'deactivate-post_' . $post->ID ),
|
||||
/* translators: %s: Post title. */
|
||||
esc_attr( sprintf( __( 'Deactivate pledge “%s”', 'wporg-5ftf' ), $post->post_title ) ),
|
||||
__( 'Deactivate', 'wporg-5ftf' )
|
||||
);
|
||||
}
|
||||
return $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger the post status change when deactivate or reactivate actions are seen.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function handle_activation_action( $post_id ) {
|
||||
$action = $_REQUEST['action'];
|
||||
if ( ! in_array( $action, [ 'deactivate', 'reactivate' ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post = get_post( $post_id );
|
||||
if ( ! is_a( $post, 'WP_Post' ) || CPT_ID !== $post->post_type ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'edit_post', $post->ID ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sendback = wp_get_referer();
|
||||
$sendback = remove_query_arg( [ 'deactivated', 'reactivated' ], $sendback );
|
||||
|
||||
if ( 'deactivate' === $action ) {
|
||||
wp_update_post( array(
|
||||
'ID' => $post_id,
|
||||
'post_status' => FiveForTheFuture\PREFIX . '-deactivated',
|
||||
) );
|
||||
wp_redirect( add_query_arg( 'deactivated', 1, $sendback ) );
|
||||
exit();
|
||||
} else {
|
||||
wp_update_post( array(
|
||||
'ID' => $post_id,
|
||||
'post_status' => 'publish',
|
||||
) );
|
||||
wp_redirect( add_query_arg( 'reactivated', 1, $sendback ) );
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Output success messages when a pledge is deactivated or reactivated.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function action_success_message() {
|
||||
if ( isset( $_GET['deactivated'] ) ) : ?>
|
||||
<div id="message" class="notice notice-success is-dismissable">
|
||||
<p><?php esc_html_e( 'Pledge deactivated.', 'wporg-5ftf' ); ?></p>
|
||||
</div>
|
||||
<?php elseif ( isset( $_GET['reactivated'] ) ) : ?>
|
||||
<div id="message" class="notice notice-success is-dismissable">
|
||||
<p><?php esc_html_e( 'Pledge reactivated.', 'wporg-5ftf' ); ?></p>
|
||||
</div>
|
||||
<?php endif;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add columns to the Pledges list table.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue