'count_post', 'plural' => 'count_posts', 'ajax' => false, ) ); } /** * Populates $this->items using table_data() and sets pagination args. */ public function prepare_items() { /** * Records per page. */ $per_page = $this->get_items_per_page( 'advance_user_posts_per_page', 10 ); /** * Retrieves the list of column titles for the table. */ $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); /** * Set up column headers with column names, hidden columns, and sortable columns. */ $this->_column_headers = array( $columns, $hidden, $sortable ); /** * Retrieve and prepare example data for display instead of running a DB query. * * Uses the plugin's in-memory example data and performs array-based sorting and pagination. * For real-world use, implement sorting and pagination in the database query to avoid * loading and processing large datasets in PHP. */ $data = $this->table_data( $this->post_type, $per_page ); $this->inline_edit( $this->post_type ); $this->items = array(); if ( isset( $data['posts'] ) && is_array( $data['posts'] ) ) { $this->items = $data['posts']; } /** * REQUIRED. Register pagination options & calculations using safe defaults. */ $total_items = 0; $max_page = 0; if ( is_array( $data ) ) { if ( array_key_exists( 'total_post', $data ) ) { $total_items = (int) $data['total_post']; } if ( array_key_exists( 'max_page', $data ) ) { $max_page = (int) $data['max_page']; } } $this->set_pagination_args( array( 'total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => $max_page, ) ); } /** * Override the parent columns method. Defines the columns to use in your listing table. * * Returned array keys are column slugs and values are the column titles * displayed in the table header. * * @return array Columns to display (key => title). */ public function get_columns() { $columns = array( 'cb' => '', 'title' => 'Title', 'author' => 'Author', 'date' => 'Date', ); return $columns; } /** * Retrieves table row data for a specific post type. * * @param string $post_type Post type to query. * @param int $per_page Number of items to display per page. * * @return array Row data. */ private function table_data( $post_type, $per_page ) { $data = array(); global $post; $current_page = $this->get_pagenum(); $author_id = isset( $_GET['user_id'] ) ? sanitize_text_field( wp_unslash( $_GET['user_id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! $author_id ) { return $data; } $page_post = new WP_Query( array( 'post_type' => $post_type, 'posts_per_page' => $per_page, 'author' => $author_id, 'post_status' => 'any', 'paged' => $current_page, ) ); $data['max_page'] = $page_post->max_num_pages; $data['total_post'] = $page_post->found_posts; while ( $page_post->have_posts() ) { $page_post->the_post(); $data['posts'][] = array( 'title' => '' . get_the_title() . '', 'author' => get_the_author(), 'post_type' => $post_type, 'date' => $post->post_date, 'id' => $post->ID, ); } wp_reset_postdata(); return $data; } /** * Define what data to show on each column of the table. * * @param array $item Row item. * @param string $column_name Column name. * @return mixed */ public function column_default( $item, $column_name ) { switch ( $column_name ) { case 'author': case 'date': return $item[ $column_name ]; default: // Show the whole array for troubleshooting purposes. return print_r($item, true); //phpcs:ignore } } /** * Title column output. * * @param array $item Row item. * @return string HTML for the title column. */ public function column_title( $item ) { return sprintf( '%1$s ', $item['title'] ); } /** * Define bulk actions (uses WP_List_Table default dropdown). * * @return array Bulk actions. */ protected function get_bulk_actions() { $actions = array( 'edit' => __( 'Edit', 'advance-user-post-crud' ), ); return $actions; } /** * Handles bulk actions submitted from the list table. * * @param string $post_type Optional. The post type for which the bulk action is being processed. Default empty. */ public function process_bulk_action( $post_type = '' ) { $action = $this->current_action(); if ( 'edit' !== $action ) { return; } $nonce = isset( $_POST['adv_user_crud_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['adv_user_crud_nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'adv_user_crud_bulk_edit' ) ) { return array( 'type' => 'error', 'message' => __( 'Security check failed.', 'advance-user-post-crud' ), ); } if ( ! current_user_can( 'edit_posts' ) ) { return array( 'type' => 'error', 'message' => __( 'You do not have permission to edit posts.', 'advance-user-post-crud' ), ); } $post_ids = isset( $_POST[ $post_type ] ) && is_array( $_POST[ $post_type ] ) ? array_map( 'absint', wp_unslash( $_POST[ $post_type ] ) ) : array(); $new_author = isset( $_POST['post_author'] ) ? absint( wp_unslash( $_POST['post_author'] ) ) : 0; if ( empty( $post_ids ) ) { return array( 'type' => 'warning', 'message' => __( 'No posts selected.', 'advance-user-post-crud' ), ); } /** * Check new author permissions. */ $author = get_user_by( 'id', $new_author ); if ( ! $author || ! user_can( $author->ID, 'edit_posts' ) ) { return array( 'type' => 'error', 'message' => __( 'Invalid author selected.', 'advance-user-post-crud' ), ); } $flag = 0; foreach ( $post_ids as $post_id ) { $post = get_post( $post_id ); if ( ! $post || ! current_user_can( 'edit_post', $post_id ) ) { continue; } $result = wp_update_post( array( 'ID' => $post_id, 'post_author' => $new_author, ), true ); if ( ! is_wp_error( $result ) ) { ++$flag; } } if ( $flag > 0 ) { return array( 'type' => 'success', // translators: %d: number of updated posts. 'message' => sprintf( __( '%d posts successfully updated.', 'advance-user-post-crud' ), $flag ), ); } return array( 'type' => 'warning', 'message' => __( 'No posts were updated.', 'advance-user-post-crud' ), ); } /** * Render bulk edit option. * * @param array $item Row item. * * @return string HTML for the checkbox column. */ public function column_cb( $item ) { return sprintf( '', esc_attr( $item['post_type'] . '-' . $item['id'] ), esc_attr( $item['post_type'] ), esc_attr( $item['id'] ) ); } /** * Add bulk edit option. * * @param string $post_type Post type slug. */ public function inline_edit( $post_type = 'post' ) { $screen = get_current_screen(); $post_type_object = get_post_type_object( $post_type ); $user_id = isset( $_GET['user_id'] ) ? absint( $_GET['user_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>