diff --git a/inc/admin/class-listtable.php b/inc/admin/class-listtable.php index 83331b9..ae25fec 100644 --- a/inc/admin/class-listtable.php +++ b/inc/admin/class-listtable.php @@ -49,6 +49,7 @@ public function get_columns() { $c = [ 'cb' => '', 'name' => __( 'Name', 'oauth2' ), + 'approved' => __( 'Approved', 'oauth2' ), 'description' => __( 'Description', 'oauth2' ), ]; @@ -103,17 +104,8 @@ protected function column_name( $item ) { ]; $post_type_object = get_post_type_object( $item->post_type ); - if ( current_user_can( $post_type_object->cap->publish_posts ) && 'publish' !== $item->post_status ) { - $publish_link = add_query_arg( - [ - 'page' => 'rest-oauth2-apps', - 'action' => 'approve', - 'id' => $item->ID, - ], - admin_url( 'users.php' ) - ); - - $publish_link = wp_nonce_url( $publish_link, 'rest-oauth2-approve:' . $item->ID ); + if ( current_user_can( $post_type_object->cap->publish_posts ) && in_array( $item->post_status, Client::UNAPPROVED_STATUSES, true ) ) { + $publish_link = get_approve_url( $item ); $actions['app-approve'] = sprintf( '%s', esc_url( $publish_link ), @@ -128,7 +120,7 @@ protected function column_name( $item ) { _post_states( $item ); $title = sprintf( '%s%s', - $edit_link, + esc_url( $edit_link ), $title, ob_get_clean() ); @@ -136,6 +128,19 @@ protected function column_name( $item ) { return $title . ' ' . $action_html; } + /** + * Print whether the client is approved. + * + * @param \WP_Post $item Post object. + */ + public function column_approved( $item ) { + if ( 'publish' === $item->post_status ) { + esc_html_e( 'Yes', 'oauth2' ); + } else { + esc_html_e( 'No', 'oauth2' ); + } + } + /** * @param \WP_Post $item Post object. * @return string Content of the column. diff --git a/inc/admin/namespace.php b/inc/admin/namespace.php index ea5192b..bbcabff 100644 --- a/inc/admin/namespace.php +++ b/inc/admin/namespace.php @@ -9,6 +9,7 @@ use WP\OAuth2\Client; use WP_Error; +use WP_Post; const BASE_SLUG = 'rest-oauth2-apps'; @@ -46,6 +47,26 @@ function get_url( $params = [] ) { return add_query_arg( urlencode_deep( $params ), $url ); } +/** + * Get the URL that approves a client. + * + * @param WP_Post|int $client_post Client post or post ID. + * @param array|string $params Extra query parameters, such as `return=edit`. + * + * @return string Approve URL including the nonce. + */ +function get_approve_url( $client_post, $params = [] ) { + $id = $client_post instanceof WP_Post ? $client_post->ID : absint( $client_post ); + $url = get_url( + [ + 'action' => 'approve', + 'id' => $id, + ] + wp_parse_args( $params ) + ); + + return wp_nonce_url( $url, 'rest-oauth2-approve:' . $id ); +} + /** * Get the current page action. * @@ -273,6 +294,41 @@ function handle_edit_submit( ?Client $consumer = null ) { exit; } +/** + * Output the approval status notice for a client, with an approve button where allowed. + * + * @param WP_Post|int $client_post Client post or post ID. + */ +function render_approval_status( $client_post ) { + $client_post = get_post( $client_post ); + if ( ! $client_post ) { + return; + } + + if ( 'publish' === $client_post->post_status ) { + echo '
' . esc_html__( 'This application is approved.', 'oauth2' ) . '