PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-post-list / src / class-post-list.php

class-post-list.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at jetpack_vendor/automattic/jetpack-post-list/src/class-post-list.php

258 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The Post List Admin Area.
4 *
5 * @package automattic/jetpack-post-list
6 */
7
8 namespace Automattic\Jetpack\Post_List;
9
10 use Automattic\Jetpack\Assets;
11 use WP_Post;
12 use WP_Screen;
13
14 /**
15 * The Post_List Admin Area
16 */
17 class Post_List {
18
19 const PACKAGE_VERSION = '0.10.1';
20 const FEATURE = 'enhanced_post_list';
21
22 /**
23 * The configuration method that is called from the jetpack-config package.
24 *
25 * @return Post_List Post_List instance.
26 */
27 public static function configure() {
28 add_post_type_support( 'post', self::FEATURE );
29 add_post_type_support( 'page', self::FEATURE );
30 return self::setup();
31 }
32
33 /**
34 * Convenience function to create an instance of the class and register the
35 * filters and actions. It allows for the feature to be set up without adding
36 * support for posts and pages.
37 *
38 * @return Post_List Post_List instance.
39 */
40 public static function setup() {
41 $post_list = self::get_instance();
42 $post_list->register();
43 return $post_list;
44 }
45
46 /**
47 * Initialize the Post List UI.
48 *
49 * @return Post_List Post_List instance.
50 */
51 public static function get_instance() {
52 return new Post_List();
53 }
54
55 /**
56 * Sets up Post List action callbacks.
57 */
58 public function register() {
59 if ( ! did_action( 'jetpack_on_posts_list_init' ) ) {
60 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
61 add_action( 'current_screen', array( $this, 'add_filters_and_actions_for_screen' ) );
62 add_filter( 'default_hidden_columns', array( $this, 'adjust_default_columns' ), 10, 2 );
63
64 if ( wp_doing_ajax() && ! empty( $_POST['action'] ) && 'inline-save' === $_POST['action'] && check_ajax_referer( 'inlineeditnonce', '_inline_edit' ) ) {
65 $this->add_filters_and_actions_quick_edit();
66 }
67
68 /**
69 * Action called after initializing Post_List Admin resources.
70 *
71 * @since 0.1.0
72 */
73 do_action( 'jetpack_on_posts_list_init' );
74 }
75 }
76
77 /**
78 * Enqueue scripts.
79 *
80 * @param string $hook Page hook.
81 */
82 public function enqueue_scripts( $hook ) {
83 if ( 'edit.php' === $hook ) {
84 wp_enqueue_style(
85 'jetpack_posts_list_ui_style',
86 plugin_dir_url( __DIR__ ) . './src/style.css',
87 array(),
88 self::PACKAGE_VERSION
89 );
90 wp_style_add_data(
91 'jetpack_posts_list_ui_style',
92 'rtl',
93 plugin_dir_url( __DIR__ ) . './src/rtl.css'
94 );
95 Assets::register_script(
96 'jetpack_posts_list',
97 '../build/index.js',
98 __FILE__,
99 array(
100 'in_footer' => true,
101 'enqueue' => true,
102 'textdomain' => 'jetpack-post-list',
103 )
104 );
105 }
106 }
107
108 /**
109 * Checks the current post type and customizes the post
110 * list columns if it is appropriate to do so.
111 *
112 * @param string $post_type The post type associated with the current request.
113 */
114 public function maybe_customize_columns( $post_type ) {
115 // Add the thumbnail column if this is specifically "Posts" or "Pages".
116 if ( post_type_supports( $post_type, self::FEATURE ) ) {
117 // Add the thumbnail column to the "Posts" admin table.
118 add_filter( 'manage_posts_columns', array( $this, 'add_thumbnail_column' ), 10, 2 );
119 add_action( 'manage_posts_custom_column', array( $this, 'populate_thumbnail_rows' ), 10, 2 );
120
121 // Add the thumbnail column to the "Pages" admin table.
122 add_filter( 'manage_pages_columns', array( $this, 'add_thumbnail_column' ) );
123 add_action( 'manage_pages_custom_column', array( $this, 'populate_thumbnail_rows' ), 10, 2 );
124 }
125 }
126
127 /**
128 * If the current_screen has 'edit' as the base, add filters and actions to change the post list tables.
129 *
130 * @param object $current_screen The current screen.
131 */
132 public function add_filters_and_actions_for_screen( $current_screen ) {
133 if ( 'edit' !== $current_screen->base ) {
134 return;
135 }
136
137 $this->maybe_customize_columns( $current_screen->post_type );
138 $this->maybe_add_copy_link_action( $current_screen->post_type );
139 }
140
141 /**
142 * Checks the current post type and adds the Copy link post
143 * action if it is appropriate to do so.
144 *
145 * @param string $post_type The post type associated with the current request.
146 */
147 public function maybe_add_copy_link_action( $post_type ) {
148 if ( ! is_post_type_viewable( $post_type ) ) {
149 return;
150 }
151 add_filter( 'post_row_actions', array( $this, 'add_copy_link_action' ), 20, 2 );
152 add_filter( 'page_row_actions', array( $this, 'add_copy_link_action' ), 20, 2 );
153 }
154
155 /**
156 * Adds the Copy link post action which copies the post link to the clipboard.
157 *
158 * @param array $post_actions The current array of post actions.
159 * @param WP_Post $post The current post in the post list table.
160 *
161 * @return array The modified post actions array.
162 */
163 public function add_copy_link_action( $post_actions, $post ) {
164 if ( $post->post_status === 'trash' ) {
165 return $post_actions;
166 }
167
168 $post_actions['copy-link'] = sprintf(
169 '<a href="%1$s" aria-label="%2$s" class="jetpack-post-list__copy-link-action">%3$s</a>',
170 esc_url( get_permalink( $post ) ),
171 esc_html__( 'Copy link to clipboard', 'jetpack-post-list' ),
172 esc_html__( 'Copy link', 'jetpack-post-list' )
173 );
174 return $post_actions;
175 }
176
177 /**
178 * Checks if the current quick edit save is for a post list
179 * where we want to customize the columns. If so, then we call
180 * the methods to register the actions.
181 */
182 public function add_filters_and_actions_quick_edit() {
183 // We've already verified the nonce in the register method above, and we're not performing
184 // any action on these POST arguments.
185 // phpcs:disable WordPress.Security.NonceVerification.Missing
186 if ( ! empty( $_POST['screen'] ) && str_starts_with( sanitize_key( $_POST['screen'] ), 'edit-' ) && ! empty( $_POST['post_type'] ) ) {
187 $type = sanitize_key( $_POST['post_type'] );
188 $this->maybe_customize_columns( $type );
189 }
190 // phpcs:enable WordPress.Security.NonceVerification.Missing
191 }
192
193 /**
194 * Adds a new column header for displaying the thumbnail of a post.
195 *
196 * @param array $columns An array of column names.
197 * @param string $post_type The post type being displayed in the post list. Defaults to 'page'.
198 * @return array An array of column names.
199 */
200 public function add_thumbnail_column( $columns, $post_type = 'page' ) {
201 if ( ! post_type_supports( $post_type, 'thumbnail' ) ) {
202 return $columns;
203 }
204
205 $new_column = array( 'thumbnail' => '<span>' . __( 'Thumbnail', 'jetpack-post-list' ) . '</span>' );
206 $keys = array_keys( $columns );
207 $position = array_search( 'title', $keys, true );
208
209 // If 'title' not found, don't insert the thumbnail column.
210 if ( false !== $position ) {
211 $columns = array_merge( array_slice( $columns, 0, $position ), $new_column, array_slice( $columns, $position ) );
212 }
213
214 return $columns;
215 }
216
217 /**
218 * Displays the thumbnail content.
219 *
220 * @param string $column The name of the column to display.
221 * @param int $post_id The current post ID.
222 */
223 public function populate_thumbnail_rows( $column, $post_id ) {
224 if ( 'thumbnail' !== $column ) {
225 return;
226 }
227
228 $thumbnail = Post_Thumbnail::get_post_thumbnail( get_post( $post_id ) );
229 if ( $thumbnail ) {
230 echo '<img src="' . esc_url( $thumbnail['thumb'] ) . '" alt="' . esc_attr( $thumbnail['alt'] ) . '" height="50" width="50" />';
231 } else {
232 echo '<span class="dashicons dashicons-format-image" title="No thumbnail found."></span>';
233 }
234 }
235
236 /**
237 * Removes the tags and columns from the posts and pages
238 * screens if the screen options haven't been changed from
239 * the default.
240 *
241 * @param array $cols The columns to hide.
242 * @param WP_Screen $screen The current screen object.
243 * @return array The columns to hide by default.
244 */
245 public function adjust_default_columns( $cols, $screen ) {
246 if ( ! ( 'edit' === $screen->base && post_type_supports( $screen->post_type, self::FEATURE ) ) ) {
247 return $cols;
248 }
249
250 $cols[] = 'tags';
251 if ( 'post' === $screen->post_type ) {
252 $cols[] = 'categories';
253 }
254
255 return $cols;
256 }
257 }
258