| @@ -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,9 +38,9 @@ | ||
| 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 | 46 | protected function _register_post_type(): void { |
| @@ -77,9 +76,9 @@ | ||
| 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 | 84 | * @param array<string, mixed> $post Post to insert. |
| @@ -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 ) { // @phpstan-ignore-line (False-positive in the PHPStan WordPress stubs.) | |
| 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,9 +143,9 @@ | ||
| 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 | 151 | * @param array<string, mixed> $post Post. |
| @@ -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. |
| @@ -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. |
| @@ -234,9 +233,9 @@ | ||
| 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 | * |
| @@ -247,9 +246,9 @@ | ||
| 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 | * |
| @@ -260,9 +259,9 @@ | ||
| 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. |
| @@ -274,13 +273,10 @@ | ||
| 274 | 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 ); |
| @@ -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 | * |
| @@ -308,9 +303,9 @@ | ||
| 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. |
| @@ -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 |
| @@ -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. |
| @@ -364,9 +359,9 @@ | ||
| 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 | * |
| @@ -378,9 +373,9 @@ | ||
| 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. |