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 +66 -60 1.123.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,9 +115,10 @@
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 }
118 - // Remove filter that adds `rel="noopener noreferrer" to <a> HTML tags, but destroys JSON code. See https://core.trac.wordpress.org/ticket/46316.
119 +
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();
122 124 }
@@ -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,9 +182,10 @@
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 }
178 - // Remove filter that adds `rel="noopener noreferrer" to <a> HTML tags, but destroys JSON code. See https://core.trac.wordpress.org/ticket/46316.
186 +
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();
182 191 }
@@ -196,16 +205,16 @@
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.
205 - * @return WP_Post|bool Post on success, false on error.
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,47 +221,47 @@
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.
221 - * @return mixed|bool Post on success, false on error.
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 *
233 242 * @param int $post_id Post ID.
234 - * @return mixed|bool Post on success, false on error.
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 array|bool 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,25 +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 - $posts = $wpdb->get_results( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID IN ({$post_ids_list})" );
283 + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
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
278 285 update_post_cache( $posts );
279 286 if ( $update_meta_cache ) {
280 287 // Get all post meta data for all table posts, @see get_post_meta().
281 288 update_meta_cache( 'post', $post_ids );
@@ -280,14 +287,13 @@
280 287 // Get all post meta data for all table posts, @see get_post_meta().
281 288 update_meta_cache( 'post', $post_ids );
282 289 }
283 290 }
284 - $offset += $length; // next array_slice() $offset
285 291 }
286 292 }
287 293
288 294 /**
289 - * 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.
290 296 * (currently for debug only)
291 297 *
292 298 * @since 1.0.0
293 299 *
@@ -292,14 +298,14 @@
292 298 * @since 1.0.0
293 299 *
294 300 * @return int Number of posts.
295 301 */
296 - public function count_posts() {
297 - 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.
298 304 }
299 305
300 306 /**
301 - * Add a post meta field to a post.
307 + * Adds a post meta field to a post.
302 308 *
303 309 * @since 1.0.0
304 310 *
305 311 * @param int $post_id ID of the post for which the field shall be added.
@@ -306,12 +312,12 @@
306 312 * @param string $field Name of the post meta field.
307 313 * @param string $value Value of the post meta field (not slashed).
308 314 * @return bool True on success, false on error.
309 315 */
310 - public function add_meta_field( $post_id, $field, $value ) {
316 + public function add_meta_field( int $post_id, string $field, string $value ): bool {
311 317 // WP expects a slashed value.
312 318 $value = wp_slash( $value );
313 - $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.
314 320 // Make sure that $success is a boolean, as add_post_meta() returns an ID or false.
315 321 $success = ( false === $success ) ? false : true;
316 322 return $success;
317 323 }
@@ -316,9 +322,9 @@
316 322 return $success;
317 323 }
318 324
319 325 /**
320 - * Update the value of a post meta field of a post.
326 + * Updatse the value of a post meta field of a post.
321 327 *
322 328 * If the field does not yet exist, it is added.
323 329 *
324 330 * @since 1.0.0
@@ -327,9 +333,9 @@
327 333 * @param string $field Name of the post meta field.
328 334 * @param string $value Value of the post meta field (not slashed).
329 335 * @return bool True on success, false on error.
330 336 */
331 - public function update_meta_field( $post_id, $field, $value ) {
337 + public function update_meta_field( int $post_id, string $field, string $value ): bool {
332 338 $prev_value = (string) get_post_meta( $post_id, $field, true );
333 339 // No need to update, if values are equal (also, update_post_meta() would return false for this).
334 340 if ( $prev_value === $value ) {
335 341 return true;
@@ -336,13 +342,13 @@
336 342 }
337 343
338 344 // WP expects a slashed value.
339 345 $value = wp_slash( $value );
340 - return update_post_meta( $post_id, $field, $value, $prev_value );
346 + return (bool) update_post_meta( $post_id, $field, $value, $prev_value );
341 347 }
342 348
343 349 /**
344 - * Get the value of a post meta field of a post.
350 + * Gets the value of a post meta field of a post.
345 351 *
346 352 * @since 1.0.0
347 353 *
348 354 * @param int $post_id ID of the post for which the field shall be retrieved.
@@ -348,14 +354,14 @@
348 354 * @param int $post_id ID of the post for which the field shall be retrieved.
349 355 * @param string $field Name of the post meta field.
350 356 * @return string Value of the meta field.
351 357 */
352 - public function get_meta_field( $post_id, $field ) {
353 - 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.
354 360 }
355 361
356 362 /**
357 - * Delete a post meta field of a post.
363 + * Deletes a post meta field of a post.
358 364 * (yet unused)
359 365 *
360 366 * @since 1.0.0
361 367 *
@@ -362,20 +368,20 @@
362 368 * @param int $post_id ID of the post of which the field shall be deleted.
363 369 * @param string $field Name of the post meta field.
364 370 * @return bool True on success, false on error.
365 371 */
366 - public function delete_meta_field( $post_id, $field ) {
367 - 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.
368 374 }
369 375
370 376 /**
371 - * Return the Custom Post Type that TablePress uses.
377 + * Returns the Custom Post Type that TablePress uses.
372 378 *
373 379 * @since 1.5.0
374 380 *
375 381 * @return string The used Custom Post Type.
376 382 */
377 - public function get_post_type() {
383 + public function get_post_type(): string {
378 384 return $this->post_type;
379 385 }
380 386
381 387 } // class TablePress_Post_Model