PluginProbe
TablePress – Tables in WordPress made easy / 3.0
TablePress – Tables in WordPress made easy v3.0
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 2.4.4 All 44 releases
← All changes | models/model-post.php +39 -44 2.1.83.0 View file →
@@ -24,14 +24,13 @@
24 24 /**
25 25 * Name of the "Custom Post Type" for the tables.
26 26 *
27 27 * @since 1.0.0
28 - * @var string
29 28 */
30 - protected $post_type = 'tablepress_table';
29 + protected string $post_type = 'tablepress_table';
31 30
32 31 /**
33 - * Init the model by registering the Custom Post Type.
32 + * Inits the model by registering the Custom Post Type.
34 33 *
35 34 * @since 1.0.0
36 35 */
37 36 public function __construct() {
@@ -39,13 +38,13 @@
39 38 $this->_register_post_type(); // We are on WP "init" hook already.
40 39 }
41 40
42 41 /**
43 - * Register the Custom Post Type which the tables use.
42 + * Registers the Custom Post Type which the tables use.
44 43 *
45 44 * @since 1.0.0
46 45 */
47 - protected function _register_post_type() {
46 + protected function _register_post_type(): void {
48 47 /**
49 48 * Filters the "Custom Post Type" that TablePress uses for storing tables in the database.
50 49 *
51 50 * @since 1.0.0
@@ -70,9 +69,9 @@
70 69 * Filters the arguments for the registration of the "Custom Post Type" that TablePress uses.
71 70 *
72 71 * @since 1.0.0
73 72 *
74 - * @param array $post_type_args Arguments for the registration of the TablePress "Custom Post Type".
73 + * @param array<string, mixed> $post_type_args Arguments for the registration of the TablePress "Custom Post Type".
75 74 */
76 75 $post_type_args = apply_filters( 'tablepress_post_type_args', $post_type_args );
77 76 register_post_type( $this->post_type, $post_type_args );
78 77 }
@@ -77,16 +76,16 @@
77 76 register_post_type( $this->post_type, $post_type_args );
78 77 }
79 78
80 79 /**
81 - * Insert a post with the correct Custom Post Type and default values in the the wp_posts table in the database.
80 + * Inserts a post with the correct Custom Post Type and default values in the the wp_posts table in the database.
82 81 *
83 82 * @since 1.0.0
84 83 *
85 - * @param array $post Post to insert.
84 + * @param array<string, mixed> $post Post to insert.
86 85 * @return int|WP_Error Post ID of the inserted post on success, WP_Error on error.
87 86 */
88 - public function insert( array $post ) {
87 + public function insert( array $post ) /* : int|WP_Error */ {
89 88 $default_post = array(
90 89 'ID' => false, // false on new insert, but existing post ID on update.
91 90 'comment_status' => 'closed',
92 91 'ping_status' => 'closed',
@@ -136,9 +135,9 @@
136 135 wp_init_targeted_link_rel_filters();
137 136 }
138 137
139 138 // 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 ) {
139 + if ( 0 === $post_id ) { // @phpstan-ignore identical.alwaysFalse (False-positive in the PHPStan WordPress stubs.)
141 140 return new WP_Error( 'post_insert', '' );
142 141 }
143 142
144 143 return $post_id;
@@ -144,16 +143,16 @@
144 143 return $post_id;
145 144 }
146 145
147 146 /**
148 - * Update an existing post with the correct Custom Post Type and default values in the the wp_posts table in the database.
147 + * Updates an existing post with the correct Custom Post Type and default values in the the wp_posts table in the database.
149 148 *
150 149 * @since 1.0.0
151 150 *
152 - * @param array $post Post.
151 + * @param array<string, mixed> $post Post.
153 152 * @return int|WP_Error Post ID of the updated post on success, WP_Error on error.
154 153 */
155 - public function update( array $post ) {
154 + public function update( array $post ) /* : int|WP_Error */ {
156 155 $default_post = array(
157 156 'ID' => false, // false on new insert, but existing post ID on update.
158 157 'comment_status' => 'closed',
159 158 'ping_status' => 'closed',
@@ -206,9 +205,9 @@
206 205 return $post_id;
207 206 }
208 207
209 208 /**
210 - * Get a post from the wp_posts table in the database.
209 + * Gets a post from the wp_posts table in the database.
211 210 *
212 211 * @since 1.0.0
213 212 *
214 213 * @param int $post_id Post ID.
@@ -213,9 +212,9 @@
213 212 *
214 213 * @param int $post_id Post ID.
215 214 * @return WP_Post|false Post on success, false on error.
216 215 */
217 - public function get( $post_id ) {
216 + public function get( int $post_id ) /* : WP_Post|false */ {
218 217 $post = get_post( $post_id );
219 218 if ( is_null( $post ) ) {
220 219 return false;
221 220 }
@@ -222,9 +221,9 @@
222 221 return $post;
223 222 }
224 223
225 224 /**
226 - * Delete a post (and all revisions) from the wp_posts table in the database.
225 + * Deletes a post (and all revisions) from the wp_posts table in the database.
227 226 *
228 227 * @since 1.0.0
229 228 *
230 229 * @param int $post_id Post ID.
@@ -229,14 +228,14 @@
229 228 *
230 229 * @param int $post_id Post ID.
231 230 * @return WP_Post|false|null Post data on success, false or null on failure.
232 231 */
233 - public function delete( $post_id ) {
232 + public function delete( int $post_id ) /* : WP_Post|false|null */ {
234 233 return wp_delete_post( $post_id, true ); // true means force delete, although for CPTs this is automatic in this function.
235 234 }
236 235
237 236 /**
238 - * Move a post to the trash (if trash is globally enabled), instead of directly deleting the post.
237 + * Moves a post to the trash (if trash is globally enabled), instead of directly deleting the post.
239 238 * (yet unused)
240 239 *
241 240 * @since 1.0.0
242 241 *
@@ -242,27 +241,27 @@
242 241 *
243 242 * @param int $post_id Post ID.
244 243 * @return WP_Post|false|null Post data on success, false or null on failure.
245 244 */
246 - public function trash( $post_id ) {
245 + public function trash( int $post_id ) /* : WP_Post|false|null */ {
247 246 return wp_trash_post( $post_id );
248 247 }
249 248
250 249 /**
251 - * Restore a post from the trash.
250 + * Restores a post from the trash.
252 251 * (yet unused)
253 252 *
254 253 * @since 1.0.0
255 254 *
256 255 * @param int $post_id Post ID.
257 - * @return WP_Post|false Post on success, false on error.
256 + * @return WP_Post|false|null Post on success, false or null on error.
258 257 */
259 - public function untrash( $post_id ) {
258 + public function untrash( int $post_id ) /* : WP_Post|false */ {
260 259 return wp_untrash_post( $post_id );
261 260 }
262 261
263 262 /**
264 - * Load all posts with one query, to prime the cache.
263 + * Loads all posts with one query, to prime the cache.
265 264 *
266 265 * @since 1.0.0
267 266 *
268 267 * @global wpdb $wpdb WordPress database abstraction object.
@@ -267,26 +266,23 @@
267 266 *
268 267 * @global wpdb $wpdb WordPress database abstraction object.
269 268 * @see get_post()
270 269 *
271 - * @param array $all_post_ids List of Post IDs.
270 + * @param int[] $all_post_ids List of Post IDs.
272 271 * @param bool $update_meta_cache Optional. Whether to update the Post Meta Cache (for table options and visibility).
273 272 */
274 - public function load_posts( array $all_post_ids, $update_meta_cache = true ) {
273 + public function load_posts( array $all_post_ids, bool $update_meta_cache = true ): void {
275 274 global $wpdb;
276 275
277 276 // 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 );
277 + while ( ! empty( $all_post_ids ) ) {
278 + $post_ids = array_splice( $all_post_ids, 0, 100 ); // Extract 100 posts at a time.
283 279 // Don't load posts that are in the cache already.
284 280 $post_ids = _get_non_cached_ids( $post_ids, 'posts' );
285 281 if ( ! empty( $post_ids ) ) {
286 282 $post_ids_list = implode( ',', $post_ids );
287 283 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
288 - $posts = $wpdb->get_results( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID IN ({$post_ids_list})" );
284 + $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 285 update_post_cache( $posts );
290 286 if ( $update_meta_cache ) {
291 287 // Get all post meta data for all table posts, @see get_post_meta().
292 288 update_meta_cache( 'post', $post_ids );
@@ -291,14 +287,13 @@
291 287 // Get all post meta data for all table posts, @see get_post_meta().
292 288 update_meta_cache( 'post', $post_ids );
293 289 }
294 290 }
295 - $offset += $length; // next array_slice() $offset.
296 291 }
297 292 }
298 293
299 294 /**
300 - * Count the number of posts with the model's CPT in the wp_posts table in the database.
295 + * Counts the number of posts with the model's CPT in the wp_posts table in the database.
301 296 * (currently for debug only)
302 297 *
303 298 * @since 1.0.0
304 299 *
@@ -303,14 +298,14 @@
303 298 * @since 1.0.0
304 299 *
305 300 * @return int Number of posts.
306 301 */
307 - public function count_posts() {
302 + public function count_posts(): int {
308 303 return array_sum( (array) wp_count_posts( $this->post_type ) ); // Original return value is object with the counts for each post_status.
309 304 }
310 305
311 306 /**
312 - * Add a post meta field to a post.
307 + * Adds a post meta field to a post.
313 308 *
314 309 * @since 1.0.0
315 310 *
316 311 * @param int $post_id ID of the post for which the field shall be added.
@@ -317,9 +312,9 @@
317 312 * @param string $field Name of the post meta field.
318 313 * @param string $value Value of the post meta field (not slashed).
319 314 * @return bool True on success, false on error.
320 315 */
321 - public function add_meta_field( $post_id, $field, $value ) {
316 + public function add_meta_field( int $post_id, string $field, string $value ): bool {
322 317 // WP expects a slashed value.
323 318 $value = wp_slash( $value );
324 319 $success = add_post_meta( $post_id, $field, $value, true ); // true means unique.
325 320 // Make sure that $success is a boolean, as add_post_meta() returns an ID or false.
@@ -327,9 +322,9 @@
327 322 return $success;
328 323 }
329 324
330 325 /**
331 - * Update the value of a post meta field of a post.
326 + * Updatse the value of a post meta field of a post.
332 327 *
333 328 * If the field does not yet exist, it is added.
334 329 *
335 330 * @since 1.0.0
@@ -338,9 +333,9 @@
338 333 * @param string $field Name of the post meta field.
339 334 * @param string $value Value of the post meta field (not slashed).
340 335 * @return bool True on success, false on error.
341 336 */
342 - public function update_meta_field( $post_id, $field, $value ) {
337 + public function update_meta_field( int $post_id, string $field, string $value ): bool {
343 338 $prev_value = (string) get_post_meta( $post_id, $field, true );
344 339 // No need to update, if values are equal (also, update_post_meta() would return false for this).
345 340 if ( $prev_value === $value ) {
346 341 return true;
@@ -351,9 +346,9 @@
351 346 return (bool) update_post_meta( $post_id, $field, $value, $prev_value );
352 347 }
353 348
354 349 /**
355 - * Get the value of a post meta field of a post.
350 + * Gets the value of a post meta field of a post.
356 351 *
357 352 * @since 1.0.0
358 353 *
359 354 * @param int $post_id ID of the post for which the field shall be retrieved.
@@ -359,14 +354,14 @@
359 354 * @param int $post_id ID of the post for which the field shall be retrieved.
360 355 * @param string $field Name of the post meta field.
361 356 * @return string Value of the meta field.
362 357 */
363 - public function get_meta_field( $post_id, $field ) {
358 + public function get_meta_field( int $post_id, string $field ): string {
364 359 return get_post_meta( $post_id, $field, true ); // true means single value.
365 360 }
366 361
367 362 /**
368 - * Delete a post meta field of a post.
363 + * Deletes a post meta field of a post.
369 364 * (yet unused)
370 365 *
371 366 * @since 1.0.0
372 367 *
@@ -373,20 +368,20 @@
373 368 * @param int $post_id ID of the post of which the field shall be deleted.
374 369 * @param string $field Name of the post meta field.
375 370 * @return bool True on success, false on error.
376 371 */
377 - public function delete_meta_field( $post_id, $field ) {
372 + public function delete_meta_field( int $post_id, string $field ): bool {
378 373 return delete_post_meta( $post_id, $field, true ); // true means single value.
379 374 }
380 375
381 376 /**
382 - * Return the Custom Post Type that TablePress uses.
377 + * Returns the Custom Post Type that TablePress uses.
383 378 *
384 379 * @since 1.5.0
385 380 *
386 381 * @return string The used Custom Post Type.
387 382 */
388 - public function get_post_type() {
383 + public function get_post_type(): string {
389 384 return $this->post_type;
390 385 }
391 386
392 387 } // class TablePress_Post_Model