PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
← All changes | models/model-post.php +47 -71 2.1.7 → 3.4 View file →
@@ -7,8 +7,10 @@
7 7 * @author Tobias Bäthge
8 8 * @since 1.0.0
9 9 */
10 10
11 +declare(strict_types=1);
12 +
11 13 // Prohibit direct script loading.
12 14 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13 15
14 16 /**
@@ -24,14 +26,14 @@
24 26 /**
25 27 * Name of the "Custom Post Type" for the tables.
26 28 *
27 29 * @since 1.0.0
28 - * @var string
30 + * @var lowercase-string&non-empty-string
29 31 */
30 - protected $post_type = 'tablepress_table';
32 + protected string $post_type = 'tablepress_table';
31 33
32 34 /**
33 - * Init the model by registering the Custom Post Type.
35 + * Inits the model by registering the Custom Post Type.
34 36 *
35 37 * @since 1.0.0
36 38 */
37 39 public function __construct() {
@@ -39,19 +41,19 @@
39 41 $this->_register_post_type(); // We are on WP "init" hook already.
40 42 }
41 43
42 44 /**
43 - * Register the Custom Post Type which the tables use.
45 + * Registers the Custom Post Type which the tables use.
44 46 *
45 47 * @since 1.0.0
46 48 */
47 - protected function _register_post_type() {
49 + protected function _register_post_type(): void {
48 50 /**
49 51 * Filters the "Custom Post Type" that TablePress uses for storing tables in the database.
50 52 *
51 53 * @since 1.0.0
52 54 *
53 - * @param string $post_type The "Custom Post Type" that TablePress uses.
55 + * @param lowercase-string&non-empty-string $post_type The "Custom Post Type" that TablePress uses.
54 56 */
55 57 $this->post_type = apply_filters( 'tablepress_post_type', $this->post_type );
56 58 $post_type_args = array(
57 59 'labels' => array(
@@ -70,9 +72,9 @@
70 72 * Filters the arguments for the registration of the "Custom Post Type" that TablePress uses.
71 73 *
72 74 * @since 1.0.0
73 75 *
74 - * @param array $post_type_args Arguments for the registration of the TablePress "Custom Post Type".
76 + * @param array<string, mixed> $post_type_args Arguments for the registration of the TablePress "Custom Post Type".
75 77 */
76 78 $post_type_args = apply_filters( 'tablepress_post_type_args', $post_type_args );
77 79 register_post_type( $this->post_type, $post_type_args );
78 80 }
@@ -77,16 +79,16 @@
77 79 register_post_type( $this->post_type, $post_type_args );
78 80 }
79 81
80 82 /**
81 - * Insert a post with the correct Custom Post Type and default values in the the wp_posts table in the database.
83 + * Inserts a post with the correct Custom Post Type and default values in the wp_posts table in the database.
82 84 *
83 85 * @since 1.0.0
84 86 *
85 - * @param array $post Post to insert.
87 + * @param array<string, mixed> $post Post to insert.
86 88 * @return int|WP_Error Post ID of the inserted post on success, WP_Error on error.
87 89 */
88 - public function insert( array $post ) {
90 + public function insert( array $post ) /* : int|WP_Error */ {
89 91 $default_post = array(
90 92 'ID' => false, // false on new insert, but existing post ID on update.
91 93 'comment_status' => 'closed',
92 94 'ping_status' => 'closed',
@@ -117,14 +119,8 @@
117 119 if ( $has_kses ) {
118 120 kses_remove_filters();
119 121 }
120 122
121 - // Remove filter that adds `rel="noopener" to <a> HTML tags, but destroys JSON code. See https://core.trac.wordpress.org/ticket/46316.
122 - $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
123 - if ( $has_targeted_link_rel_filters ) {
124 - wp_remove_targeted_link_rel_filters();
125 - }
126 -
127 123 $post_id = wp_insert_post( $post, true );
128 124
129 125 // Restore removed content filters.
130 126 add_filter( 'content_save_pre', 'balanceTags', 50 );
@@ -131,14 +127,11 @@
131 127 add_filter( 'excerpt_save_pre', 'balanceTags', 50 );
132 128 if ( $has_kses ) {
133 129 kses_init_filters();
134 130 }
135 - if ( $has_targeted_link_rel_filters ) {
136 - wp_init_targeted_link_rel_filters();
137 - }
138 131
139 132 // In rare cases, `wp_insert_post()` returns 0 as the post ID, when an error happens, so it's converted to a WP_Error here.
140 - if ( 0 === $post_id ) {
133 + if ( 0 === $post_id ) { // @phpstan-ignore identical.alwaysFalse (False-positive in the PHPStan WordPress stubs.)
141 134 return new WP_Error( 'post_insert', '' );
142 135 }
143 136
144 137 return $post_id;
@@ -144,16 +137,16 @@
144 137 return $post_id;
145 138 }
146 139
147 140 /**
148 - * Update an existing post with the correct Custom Post Type and default values in the the wp_posts table in the database.
141 + * Updates an existing post with the correct Custom Post Type and default values in the wp_posts table in the database.
149 142 *
150 143 * @since 1.0.0
151 144 *
152 - * @param array $post Post.
145 + * @param array<string, mixed> $post Post.
153 146 * @return int|WP_Error Post ID of the updated post on success, WP_Error on error.
154 147 */
155 - public function update( array $post ) {
148 + public function update( array $post ) /* : int|WP_Error */ {
156 149 $default_post = array(
157 150 'ID' => false, // false on new insert, but existing post ID on update.
158 151 'comment_status' => 'closed',
159 152 'ping_status' => 'closed',
@@ -184,14 +177,8 @@
184 177 if ( $has_kses ) {
185 178 kses_remove_filters();
186 179 }
187 180
188 - // Remove filter that adds `rel="noopener" to <a> HTML tags, but destroys JSON code. See https://core.trac.wordpress.org/ticket/46316.
189 - $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
190 - if ( $has_targeted_link_rel_filters ) {
191 - wp_remove_targeted_link_rel_filters();
192 - }
193 -
194 181 $post_id = wp_update_post( $post, true );
195 182
196 183 // Restore removed content filters.
197 184 add_filter( 'content_save_pre', 'balanceTags', 50 );
@@ -198,17 +185,14 @@
198 185 add_filter( 'excerpt_save_pre', 'balanceTags', 50 );
199 186 if ( $has_kses ) {
200 187 kses_init_filters();
201 188 }
202 - if ( $has_targeted_link_rel_filters ) {
203 - wp_init_targeted_link_rel_filters();
204 - }
205 189
206 190 return $post_id;
207 191 }
208 192
209 193 /**
210 - * Get a post from the wp_posts table in the database.
194 + * Gets a post from the wp_posts table in the database.
211 195 *
212 196 * @since 1.0.0
213 197 *
214 198 * @param int $post_id Post ID.
@@ -213,9 +197,9 @@
213 197 *
214 198 * @param int $post_id Post ID.
215 199 * @return WP_Post|false Post on success, false on error.
216 200 */
217 - public function get( $post_id ) {
201 + public function get( int $post_id ) /* : WP_Post|false */ {
218 202 $post = get_post( $post_id );
219 203 if ( is_null( $post ) ) {
220 204 return false;
221 205 }
@@ -222,9 +206,9 @@
222 206 return $post;
223 207 }
224 208
225 209 /**
226 - * Delete a post (and all revisions) from the wp_posts table in the database.
210 + * Deletes a post (and all revisions) from the wp_posts table in the database.
227 211 *
228 212 * @since 1.0.0
229 213 *
230 214 * @param int $post_id Post ID.
@@ -229,14 +213,14 @@
229 213 *
230 214 * @param int $post_id Post ID.
231 215 * @return WP_Post|false|null Post data on success, false or null on failure.
232 216 */
233 - public function delete( $post_id ) {
217 + public function delete( int $post_id ) /* : WP_Post|false|null */ {
234 218 return wp_delete_post( $post_id, true ); // true means force delete, although for CPTs this is automatic in this function.
235 219 }
236 220
237 221 /**
238 - * Move a post to the trash (if trash is globally enabled), instead of directly deleting the post.
222 + * Moves a post to the trash (if trash is globally enabled), instead of directly deleting the post.
239 223 * (yet unused)
240 224 *
241 225 * @since 1.0.0
242 226 *
@@ -242,27 +226,27 @@
242 226 *
243 227 * @param int $post_id Post ID.
244 228 * @return WP_Post|false|null Post data on success, false or null on failure.
245 229 */
246 - public function trash( $post_id ) {
230 + public function trash( int $post_id ) /* : WP_Post|false|null */ {
247 231 return wp_trash_post( $post_id );
248 232 }
249 233
250 234 /**
251 - * Restore a post from the trash.
235 + * Restores a post from the trash.
252 236 * (yet unused)
253 237 *
254 238 * @since 1.0.0
255 239 *
256 240 * @param int $post_id Post ID.
257 - * @return WP_Post|false Post on success, false on error.
241 + * @return WP_Post|false|null Post on success, false or null on error.
258 242 */
259 - public function untrash( $post_id ) {
243 + public function untrash( int $post_id ) /* : WP_Post|false */ {
260 244 return wp_untrash_post( $post_id );
261 245 }
262 246
263 247 /**
264 - * Load all posts with one query, to prime the cache.
248 + * Loads all posts with one query, to prime the cache.
265 249 *
266 250 * @since 1.0.0
267 251 *
268 252 * @global wpdb $wpdb WordPress database abstraction object.
@@ -267,26 +251,23 @@
267 251 *
268 252 * @global wpdb $wpdb WordPress database abstraction object.
269 253 * @see get_post()
270 254 *
271 - * @param array $all_post_ids List of Post IDs.
255 + * @param int[] $all_post_ids List of Post IDs.
272 256 * @param bool $update_meta_cache Optional. Whether to update the Post Meta Cache (for table options and visibility).
273 257 */
274 - public function load_posts( array $all_post_ids, $update_meta_cache = true ) {
258 + public function load_posts( array $all_post_ids, bool $update_meta_cache = true ): void {
275 259 global $wpdb;
276 260
277 261 // Split post loading, to save memory.
278 - $offset = 0;
279 - $length = 100; // 100 posts at a time
280 - $number_of_posts = count( $all_post_ids );
281 - while ( $offset < $number_of_posts ) {
282 - $post_ids = array_slice( $all_post_ids, $offset, $length );
262 + while ( ! empty( $all_post_ids ) ) {
263 + $post_ids = array_splice( $all_post_ids, 0, 100 ); // Extract 100 posts at a time.
283 264 // Don't load posts that are in the cache already.
284 265 $post_ids = _get_non_cached_ids( $post_ids, 'posts' );
285 266 if ( ! empty( $post_ids ) ) {
286 267 $post_ids_list = implode( ',', $post_ids );
287 268 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
288 - $posts = $wpdb->get_results( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID IN ({$post_ids_list})" );
269 + $posts = $wpdb->get_results( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID IN ({$post_ids_list})" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
289 270 update_post_cache( $posts );
290 271 if ( $update_meta_cache ) {
291 272 // Get all post meta data for all table posts, @see get_post_meta().
292 273 update_meta_cache( 'post', $post_ids );
@@ -291,14 +272,13 @@
291 272 // Get all post meta data for all table posts, @see get_post_meta().
292 273 update_meta_cache( 'post', $post_ids );
293 274 }
294 275 }
295 - $offset += $length; // next array_slice() $offset.
296 276 }
297 277 }
298 278
299 279 /**
300 - * Count the number of posts with the model's CPT in the wp_posts table in the database.
280 + * Counts the number of posts with the model's CPT in the wp_posts table in the database.
301 281 * (currently for debug only)
302 282 *
303 283 * @since 1.0.0
304 284 *
@@ -303,14 +283,14 @@
303 283 * @since 1.0.0
304 284 *
305 285 * @return int Number of posts.
306 286 */
307 - public function count_posts() {
287 + public function count_posts(): int {
308 288 return array_sum( (array) wp_count_posts( $this->post_type ) ); // Original return value is object with the counts for each post_status.
309 289 }
310 290
311 291 /**
312 - * Add a post meta field to a post.
292 + * Adds a post meta field to a post.
313 293 *
314 294 * @since 1.0.0
315 295 *
316 296 * @param int $post_id ID of the post for which the field shall be added.
@@ -317,12 +297,10 @@
317 297 * @param string $field Name of the post meta field.
318 298 * @param string $value Value of the post meta field (not slashed).
319 299 * @return bool True on success, false on error.
320 300 */
321 - public function add_meta_field( $post_id, $field, $value ) {
322 - // WP expects a slashed value.
323 - $value = wp_slash( $value );
324 - $success = add_post_meta( $post_id, $field, $value, true ); // true means unique.
301 + public function add_meta_field( int $post_id, string $field, string $value ): bool {
302 + $success = add_post_meta( $post_id, wp_slash( $field ), wp_slash( $value ), true ); // WP expects slashed values, `true` means unique.
325 303 // Make sure that $success is a boolean, as add_post_meta() returns an ID or false.
326 304 $success = ( false === $success ) ? false : true;
327 305 return $success;
328 306 }
@@ -327,9 +305,9 @@
327 305 return $success;
328 306 }
329 307
330 308 /**
331 - * Update the value of a post meta field of a post.
309 + * Updatse the value of a post meta field of a post.
332 310 *
333 311 * If the field does not yet exist, it is added.
334 312 *
335 313 * @since 1.0.0
@@ -338,9 +316,9 @@
338 316 * @param string $field Name of the post meta field.
339 317 * @param string $value Value of the post meta field (not slashed).
340 318 * @return bool True on success, false on error.
341 319 */
342 - public function update_meta_field( $post_id, $field, $value ) {
320 + public function update_meta_field( int $post_id, string $field, string $value ): bool {
343 321 $prev_value = (string) get_post_meta( $post_id, $field, true );
344 322 // No need to update, if values are equal (also, update_post_meta() would return false for this).
345 323 if ( $prev_value === $value ) {
346 324 return true;
@@ -345,15 +323,13 @@
345 323 if ( $prev_value === $value ) {
346 324 return true;
347 325 }
348 326
349 - // WP expects a slashed value.
350 - $value = wp_slash( $value );
351 - return (bool) update_post_meta( $post_id, $field, $value, $prev_value );
327 + return (bool) update_post_meta( $post_id, wp_slash( $field ), wp_slash( $value ), $prev_value ); // WP expects slashed values.
352 328 }
353 329
354 330 /**
355 - * Get the value of a post meta field of a post.
331 + * Gets the value of a post meta field of a post.
356 332 *
357 333 * @since 1.0.0
358 334 *
359 335 * @param int $post_id ID of the post for which the field shall be retrieved.
@@ -359,14 +335,14 @@
359 335 * @param int $post_id ID of the post for which the field shall be retrieved.
360 336 * @param string $field Name of the post meta field.
361 337 * @return string Value of the meta field.
362 338 */
363 - public function get_meta_field( $post_id, $field ) {
364 - return get_post_meta( $post_id, $field, true ); // true means single value.
339 + public function get_meta_field( int $post_id, string $field ): string {
340 + return get_post_meta( $post_id, $field, true ); // `true` means single value.
365 341 }
366 342
367 343 /**
368 - * Delete a post meta field of a post.
344 + * Deletes a post meta field of a post.
369 345 * (yet unused)
370 346 *
371 347 * @since 1.0.0
372 348 *
@@ -373,20 +349,20 @@
373 349 * @param int $post_id ID of the post of which the field shall be deleted.
374 350 * @param string $field Name of the post meta field.
375 351 * @return bool True on success, false on error.
376 352 */
377 - public function delete_meta_field( $post_id, $field ) {
378 - return delete_post_meta( $post_id, $field, true ); // true means single value.
353 + public function delete_meta_field( int $post_id, string $field ): bool {
354 + return delete_post_meta( $post_id, wp_slash( $field ) ); // WP expects a slashed value.
379 355 }
380 356
381 357 /**
382 - * Return the Custom Post Type that TablePress uses.
358 + * Returns the Custom Post Type that TablePress uses.
383 359 *
384 360 * @since 1.5.0
385 361 *
386 362 * @return string The used Custom Post Type.
387 363 */
388 - public function get_post_type() {
364 + public function get_post_type(): string {
389 365 return $this->post_type;
390 366 }
391 367
392 368 } // class TablePress_Post_Model