register(); return $post_list; } /** * Initialize the Post List UI. * * @return Post_List Post_List instance. */ public static function get_instance() { return new Post_List(); } /** * Sets up Post List action callbacks. */ public function register() { if ( ! did_action( 'jetpack_on_posts_list_init' ) ) { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'current_screen', array( $this, 'add_filters_and_actions_for_screen' ) ); add_filter( 'default_hidden_columns', array( $this, 'adjust_default_columns' ), 10, 2 ); if ( wp_doing_ajax() && ! empty( $_POST['action'] ) && 'inline-save' === $_POST['action'] && check_ajax_referer( 'inlineeditnonce', '_inline_edit' ) ) { $this->add_filters_and_actions_quick_edit(); } /** * Action called after initializing Post_List Admin resources. * * @since 0.1.0 */ do_action( 'jetpack_on_posts_list_init' ); } } /** * Enqueue scripts. * * @param string $hook Page hook. */ public function enqueue_scripts( $hook ) { if ( 'edit.php' === $hook ) { wp_enqueue_style( 'jetpack_posts_list_ui_style', plugin_dir_url( __DIR__ ) . './src/style.css', array(), self::PACKAGE_VERSION ); wp_style_add_data( 'jetpack_posts_list_ui_style', 'rtl', plugin_dir_url( __DIR__ ) . './src/rtl.css' ); Assets::register_script( 'jetpack_posts_list', '../build/index.js', __FILE__, array( 'in_footer' => true, 'enqueue' => true, 'textdomain' => 'jetpack-post-list', ) ); } } /** * Checks the current post type and customizes the post * list columns if it is appropriate to do so. * * @param string $post_type The post type associated with the current request. */ public function maybe_customize_columns( $post_type ) { // Add the thumbnail column if this is specifically "Posts" or "Pages". if ( post_type_supports( $post_type, self::FEATURE ) ) { // Add the thumbnail column to the "Posts" admin table. add_filter( 'manage_posts_columns', array( $this, 'add_thumbnail_column' ), 10, 2 ); add_action( 'manage_posts_custom_column', array( $this, 'populate_thumbnail_rows' ), 10, 2 ); // Add the thumbnail column to the "Pages" admin table. add_filter( 'manage_pages_columns', array( $this, 'add_thumbnail_column' ) ); add_action( 'manage_pages_custom_column', array( $this, 'populate_thumbnail_rows' ), 10, 2 ); } } /** * If the current_screen has 'edit' as the base, add filters and actions to change the post list tables. * * @param object $current_screen The current screen. */ public function add_filters_and_actions_for_screen( $current_screen ) { if ( 'edit' !== $current_screen->base ) { return; } $this->maybe_customize_columns( $current_screen->post_type ); $this->maybe_add_copy_link_action( $current_screen->post_type ); } /** * Checks the current post type and adds the Copy link post * action if it is appropriate to do so. * * @param string $post_type The post type associated with the current request. */ public function maybe_add_copy_link_action( $post_type ) { if ( ! is_post_type_viewable( $post_type ) ) { return; } add_filter( 'post_row_actions', array( $this, 'add_copy_link_action' ), 20, 2 ); add_filter( 'page_row_actions', array( $this, 'add_copy_link_action' ), 20, 2 ); } /** * Adds the Copy link post action which copies the post link to the clipboard. * * @param array $post_actions The current array of post actions. * @param WP_Post $post The current post in the post list table. * * @return array The modified post actions array. */ public function add_copy_link_action( $post_actions, $post ) { if ( $post->post_status === 'trash' ) { return $post_actions; } $post_actions['copy-link'] = sprintf( '%3$s', esc_url( get_permalink( $post ) ), esc_html__( 'Copy link to clipboard', 'jetpack-post-list' ), esc_html__( 'Copy link', 'jetpack-post-list' ) ); return $post_actions; } /** * Checks if the current quick edit save is for a post list * where we want to customize the columns. If so, then we call * the methods to register the actions. */ public function add_filters_and_actions_quick_edit() { // We've already verified the nonce in the register method above, and we're not performing // any action on these POST arguments. // phpcs:disable WordPress.Security.NonceVerification.Missing if ( ! empty( $_POST['screen'] ) && str_starts_with( sanitize_key( $_POST['screen'] ), 'edit-' ) && ! empty( $_POST['post_type'] ) ) { $type = sanitize_key( $_POST['post_type'] ); $this->maybe_customize_columns( $type ); } // phpcs:enable WordPress.Security.NonceVerification.Missing } /** * Adds a new column header for displaying the thumbnail of a post. * * @param array $columns An array of column names. * @param string $post_type The post type being displayed in the post list. Defaults to 'page'. * @return array An array of column names. */ public function add_thumbnail_column( $columns, $post_type = 'page' ) { if ( ! post_type_supports( $post_type, 'thumbnail' ) ) { return $columns; } $new_column = array( 'thumbnail' => '' . __( 'Thumbnail', 'jetpack-post-list' ) . '' ); $keys = array_keys( $columns ); $position = array_search( 'title', $keys, true ); // If 'title' not found, don't insert the thumbnail column. if ( false !== $position ) { $columns = array_merge( array_slice( $columns, 0, $position ), $new_column, array_slice( $columns, $position ) ); } return $columns; } /** * Displays the thumbnail content. * * @param string $column The name of the column to display. * @param int $post_id The current post ID. */ public function populate_thumbnail_rows( $column, $post_id ) { if ( 'thumbnail' !== $column ) { return; } $thumbnail = Post_Thumbnail::get_post_thumbnail( get_post( $post_id ) ); if ( $thumbnail ) { echo '' . esc_attr( $thumbnail['alt'] ) . ''; } else { echo ''; } } /** * Removes the tags and columns from the posts and pages * screens if the screen options haven't been changed from * the default. * * @param array $cols The columns to hide. * @param WP_Screen $screen The current screen object. * @return array The columns to hide by default. */ public function adjust_default_columns( $cols, $screen ) { if ( ! ( 'edit' === $screen->base && post_type_supports( $screen->post_type, self::FEATURE ) ) ) { return $cols; } $cols[] = 'tags'; if ( 'post' === $screen->post_type ) { $cols[] = 'categories'; } return $cols; } }