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 +59 -54 1.143.0 View file →
@@ -12,8 +12,9 @@
12 12 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13 13
14 14 /**
15 15 * Post Model class
16 + *
16 17 * @package TablePress
17 18 * @subpackage Models
18 19 * @author Tobias Bäthge
19 20 * @since 1.0.0
@@ -23,30 +24,29 @@
23 24 /**
24 25 * Name of the "Custom Post Type" for the tables.
25 26 *
26 27 * @since 1.0.0
27 - * @var string
28 28 */
29 - protected $post_type = 'tablepress_table';
29 + protected string $post_type = 'tablepress_table';
30 30
31 31 /**
32 - * Init the model by registering the Custom Post Type.
32 + * Inits the model by registering the Custom Post Type.
33 33 *
34 34 * @since 1.0.0
35 35 */
36 36 public function __construct() {
37 37 parent::__construct();
38 - $this->_register_post_type(); // we are on WP "init" hook already
38 + $this->_register_post_type(); // We are on WP "init" hook already.
39 39 }
40 40
41 41 /**
42 - * Register the Custom Post Type which the tables use.
42 + * Registers the Custom Post Type which the tables use.
43 43 *
44 44 * @since 1.0.0
45 45 */
46 - protected function _register_post_type() {
46 + protected function _register_post_type(): void {
47 47 /**
48 - * Filter the "Custom Post Type" that TablePress uses for storing tables in the database.
48 + * Filters the "Custom Post Type" that TablePress uses for storing tables in the database.
49 49 *
50 50 * @since 1.0.0
51 51 *
52 52 * @param string $post_type The "Custom Post Type" that TablePress uses.
@@ -65,13 +65,13 @@
65 65 'supports' => array( 'title', 'editor', 'excerpt', 'revisions' ),
66 66 'can_export' => true,
67 67 );
68 68 /**
69 - * Filter the arguments for the registration of the "Custom Post Type" that TablePress uses.
69 + * Filters the arguments for the registration of the "Custom Post Type" that TablePress uses.
70 70 *
71 71 * @since 1.0.0
72 72 *
73 - * @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".
74 74 */
75 75 $post_type_args = apply_filters( 'tablepress_post_type_args', $post_type_args );
76 76 register_post_type( $this->post_type, $post_type_args );
77 77 }
@@ -76,18 +76,18 @@
76 76 register_post_type( $this->post_type, $post_type_args );
77 77 }
78 78
79 79 /**
80 - * 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.
81 81 *
82 82 * @since 1.0.0
83 83 *
84 - * @param array $post Post to insert.
84 + * @param array<string, mixed> $post Post to insert.
85 85 * @return int|WP_Error Post ID of the inserted post on success, WP_Error on error.
86 86 */
87 - public function insert( array $post ) {
87 + public function insert( array $post ) /* : int|WP_Error */ {
88 88 $default_post = array(
89 - 'ID' => false, // false on new insert, but existing post ID on update
89 + 'ID' => false, // false on new insert, but existing post ID on update.
90 90 'comment_status' => 'closed',
91 91 'ping_status' => 'closed',
92 92 'post_category' => false,
93 93 'post_content' => '',
@@ -106,8 +106,9 @@
106 106
107 107 // Remove balanceTags() from sanitize_post(), as it can destroy the JSON when messing with HTML.
108 108 remove_filter( 'content_save_pre', 'balanceTags', 50 );
109 109 remove_filter( 'excerpt_save_pre', 'balanceTags', 50 );
110 +
110 111 /*
111 112 * Remove possible KSES filtering, as it can destroy the JSON when messing with HTML.
112 113 * KSES filtering is done to table cells individually, when saving.
113 114 */
@@ -114,8 +115,9 @@
114 115 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
115 116 if ( $has_kses ) {
116 117 kses_remove_filters();
117 118 }
119 +
118 120 // Remove filter that adds `rel="noopener" to <a> HTML tags, but destroys JSON code. See https://core.trac.wordpress.org/ticket/46316.
119 121 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
120 122 if ( $has_targeted_link_rel_filters ) {
121 123 wp_remove_targeted_link_rel_filters();
@@ -132,22 +134,27 @@
132 134 if ( $has_targeted_link_rel_filters ) {
133 135 wp_init_targeted_link_rel_filters();
134 136 }
135 137
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.
139 + if ( 0 === $post_id ) { // @phpstan-ignore identical.alwaysFalse (False-positive in the PHPStan WordPress stubs.)
140 + return new WP_Error( 'post_insert', '' );
141 + }
142 +
136 143 return $post_id;
137 144 }
138 145
139 146 /**
140 - * 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.
141 148 *
142 149 * @since 1.0.0
143 150 *
144 - * @param array $post Post.
151 + * @param array<string, mixed> $post Post.
145 152 * @return int|WP_Error Post ID of the updated post on success, WP_Error on error.
146 153 */
147 - public function update( array $post ) {
154 + public function update( array $post ) /* : int|WP_Error */ {
148 155 $default_post = array(
149 - 'ID' => false, // false on new insert, but existing post ID on update
156 + 'ID' => false, // false on new insert, but existing post ID on update.
150 157 'comment_status' => 'closed',
151 158 'ping_status' => 'closed',
152 159 'post_category' => false,
153 160 'post_content' => '',
@@ -163,11 +170,12 @@
163 170 $post = array_merge( $default_post, $post );
164 171 // WP expects everything to be slashed.
165 172 $post = wp_slash( $post );
166 173
167 - // Remove balanceTags() from sanitize_post(), as it can destroy the JSON when messing with HTML
174 + // Remove balanceTags() from sanitize_post(), as it can destroy the JSON when messing with HTML.
168 175 remove_filter( 'content_save_pre', 'balanceTags', 50 );
169 176 remove_filter( 'excerpt_save_pre', 'balanceTags', 50 );
177 +
170 178 /*
171 179 * Remove possible KSES filtering, as it can destroy the JSON when messing with HTML.
172 180 * KSES filtering is done to table cells individually, when saving.
173 181 */
@@ -174,8 +182,9 @@
174 182 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
175 183 if ( $has_kses ) {
176 184 kses_remove_filters();
177 185 }
186 +
178 187 // Remove filter that adds `rel="noopener" to <a> HTML tags, but destroys JSON code. See https://core.trac.wordpress.org/ticket/46316.
179 188 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
180 189 if ( $has_targeted_link_rel_filters ) {
181 190 wp_remove_targeted_link_rel_filters();
@@ -196,9 +205,9 @@
196 205 return $post_id;
197 206 }
198 207
199 208 /**
200 - * Get a post from the wp_posts table in the database.
209 + * Gets a post from the wp_posts table in the database.
201 210 *
202 211 * @since 1.0.0
203 212 *
204 213 * @param int $post_id Post ID.
@@ -203,9 +212,9 @@
203 212 *
204 213 * @param int $post_id Post ID.
205 214 * @return WP_Post|false Post on success, false on error.
206 215 */
207 - public function get( $post_id ) {
216 + public function get( int $post_id ) /* : WP_Post|false */ {
208 217 $post = get_post( $post_id );
209 218 if ( is_null( $post ) ) {
210 219 return false;
211 220 }
@@ -212,9 +221,9 @@
212 221 return $post;
213 222 }
214 223
215 224 /**
216 - * 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.
217 226 *
218 227 * @since 1.0.0
219 228 *
220 229 * @param int $post_id Post ID.
@@ -219,14 +228,14 @@
219 228 *
220 229 * @param int $post_id Post ID.
221 230 * @return WP_Post|false|null Post data on success, false or null on failure.
222 231 */
223 - public function delete( $post_id ) {
224 - return wp_delete_post( $post_id, true ); // true means force delete, although for CPTs this is automatic in this function
232 + public function delete( int $post_id ) /* : WP_Post|false|null */ {
233 + return wp_delete_post( $post_id, true ); // true means force delete, although for CPTs this is automatic in this function.
225 234 }
226 235
227 236 /**
228 - * 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.
229 238 * (yet unused)
230 239 *
231 240 * @since 1.0.0
232 241 *
@@ -232,27 +241,27 @@
232 241 *
233 242 * @param int $post_id Post ID.
234 243 * @return WP_Post|false|null Post data on success, false or null on failure.
235 244 */
236 - public function trash( $post_id ) {
245 + public function trash( int $post_id ) /* : WP_Post|false|null */ {
237 246 return wp_trash_post( $post_id );
238 247 }
239 248
240 249 /**
241 - * Restore a post from the trash.
250 + * Restores a post from the trash.
242 251 * (yet unused)
243 252 *
244 253 * @since 1.0.0
245 254 *
246 255 * @param int $post_id Post ID.
247 - * @return WP_Post|false Post on success, false on error.
256 + * @return WP_Post|false|null Post on success, false or null on error.
248 257 */
249 - public function untrash( $post_id ) {
258 + public function untrash( int $post_id ) /* : WP_Post|false */ {
250 259 return wp_untrash_post( $post_id );
251 260 }
252 261
253 262 /**
254 - * Load all posts with one query, to prime the cache.
263 + * Loads all posts with one query, to prime the cache.
255 264 *
256 265 * @since 1.0.0
257 266 *
258 267 * @global wpdb $wpdb WordPress database abstraction object.
@@ -257,26 +266,23 @@
257 266 *
258 267 * @global wpdb $wpdb WordPress database abstraction object.
259 268 * @see get_post()
260 269 *
261 - * @param array $all_post_ids List of Post IDs.
270 + * @param int[] $all_post_ids List of Post IDs.
262 271 * @param bool $update_meta_cache Optional. Whether to update the Post Meta Cache (for table options and visibility).
263 272 */
264 - 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 {
265 274 global $wpdb;
266 275
267 276 // Split post loading, to save memory.
268 - $offset = 0;
269 - $length = 100; // 100 posts at a time
270 - $number_of_posts = count( $all_post_ids );
271 - while ( $offset < $number_of_posts ) {
272 - $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.
273 279 // Don't load posts that are in the cache already.
274 280 $post_ids = _get_non_cached_ids( $post_ids, 'posts' );
275 281 if ( ! empty( $post_ids ) ) {
276 282 $post_ids_list = implode( ',', $post_ids );
277 283 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
278 - $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
279 285 update_post_cache( $posts );
280 286 if ( $update_meta_cache ) {
281 287 // Get all post meta data for all table posts, @see get_post_meta().
282 288 update_meta_cache( 'post', $post_ids );
@@ -281,14 +287,13 @@
281 287 // Get all post meta data for all table posts, @see get_post_meta().
282 288 update_meta_cache( 'post', $post_ids );
283 289 }
284 290 }
285 - $offset += $length; // next array_slice() $offset
286 291 }
287 292 }
288 293
289 294 /**
290 - * 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.
291 296 * (currently for debug only)
292 297 *
293 298 * @since 1.0.0
294 299 *
@@ -293,14 +298,14 @@
293 298 * @since 1.0.0
294 299 *
295 300 * @return int Number of posts.
296 301 */
297 - public function count_posts() {
298 - return array_sum( (array) wp_count_posts( $this->post_type ) ); // original return value is object with the counts for each post_status
302 + public function count_posts(): int {
303 + return array_sum( (array) wp_count_posts( $this->post_type ) ); // Original return value is object with the counts for each post_status.
299 304 }
300 305
301 306 /**
302 - * Add a post meta field to a post.
307 + * Adds a post meta field to a post.
303 308 *
304 309 * @since 1.0.0
305 310 *
306 311 * @param int $post_id ID of the post for which the field shall be added.
@@ -307,12 +312,12 @@
307 312 * @param string $field Name of the post meta field.
308 313 * @param string $value Value of the post meta field (not slashed).
309 314 * @return bool True on success, false on error.
310 315 */
311 - public function add_meta_field( $post_id, $field, $value ) {
316 + public function add_meta_field( int $post_id, string $field, string $value ): bool {
312 317 // WP expects a slashed value.
313 318 $value = wp_slash( $value );
314 - $success = add_post_meta( $post_id, $field, $value, true ); // true means unique
319 + $success = add_post_meta( $post_id, $field, $value, true ); // true means unique.
315 320 // Make sure that $success is a boolean, as add_post_meta() returns an ID or false.
316 321 $success = ( false === $success ) ? false : true;
317 322 return $success;
318 323 }
@@ -317,9 +322,9 @@
317 322 return $success;
318 323 }
319 324
320 325 /**
321 - * Update the value of a post meta field of a post.
326 + * Updatse the value of a post meta field of a post.
322 327 *
323 328 * If the field does not yet exist, it is added.
324 329 *
325 330 * @since 1.0.0
@@ -328,9 +333,9 @@
328 333 * @param string $field Name of the post meta field.
329 334 * @param string $value Value of the post meta field (not slashed).
330 335 * @return bool True on success, false on error.
331 336 */
332 - public function update_meta_field( $post_id, $field, $value ) {
337 + public function update_meta_field( int $post_id, string $field, string $value ): bool {
333 338 $prev_value = (string) get_post_meta( $post_id, $field, true );
334 339 // No need to update, if values are equal (also, update_post_meta() would return false for this).
335 340 if ( $prev_value === $value ) {
336 341 return true;
@@ -341,9 +346,9 @@
341 346 return (bool) update_post_meta( $post_id, $field, $value, $prev_value );
342 347 }
343 348
344 349 /**
345 - * Get the value of a post meta field of a post.
350 + * Gets the value of a post meta field of a post.
346 351 *
347 352 * @since 1.0.0
348 353 *
349 354 * @param int $post_id ID of the post for which the field shall be retrieved.
@@ -349,14 +354,14 @@
349 354 * @param int $post_id ID of the post for which the field shall be retrieved.
350 355 * @param string $field Name of the post meta field.
351 356 * @return string Value of the meta field.
352 357 */
353 - public function get_meta_field( $post_id, $field ) {
354 - return get_post_meta( $post_id, $field, true ); // true means single value
358 + public function get_meta_field( int $post_id, string $field ): string {
359 + return get_post_meta( $post_id, $field, true ); // true means single value.
355 360 }
356 361
357 362 /**
358 - * Delete a post meta field of a post.
363 + * Deletes a post meta field of a post.
359 364 * (yet unused)
360 365 *
361 366 * @since 1.0.0
362 367 *
@@ -363,20 +368,20 @@
363 368 * @param int $post_id ID of the post of which the field shall be deleted.
364 369 * @param string $field Name of the post meta field.
365 370 * @return bool True on success, false on error.
366 371 */
367 - public function delete_meta_field( $post_id, $field ) {
368 - return delete_post_meta( $post_id, $field, true ); // true means single value
372 + public function delete_meta_field( int $post_id, string $field ): bool {
373 + return delete_post_meta( $post_id, $field, true ); // true means single value.
369 374 }
370 375
371 376 /**
372 - * Return the Custom Post Type that TablePress uses.
377 + * Returns the Custom Post Type that TablePress uses.
373 378 *
374 379 * @since 1.5.0
375 380 *
376 381 * @return string The used Custom Post Type.
377 382 */
378 - public function get_post_type() {
383 + public function get_post_type(): string {
379 384 return $this->post_type;
380 385 }
381 386
382 387 } // class TablePress_Post_Model