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 +27 -51 2.3.2 → 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,9 +41,9 @@
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 49 protected function _register_post_type(): void {
@@ -49,9 +51,9 @@
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(
@@ -77,9 +79,9 @@
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 87 * @param array<string, mixed> $post Post to insert.
@@ -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 ) { // @phpstan-ignore-line (False-positive in the PHPStan WordPress stubs.)
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,9 +137,9 @@
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 145 * @param array<string, mixed> $post Post.
@@ -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.
@@ -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.
@@ -234,9 +218,9 @@
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 *
@@ -247,9 +231,9 @@
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 *
@@ -260,9 +244,9 @@
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.
@@ -274,13 +258,10 @@
274 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 );
@@ -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 *
@@ -308,9 +288,9 @@
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.
@@ -318,11 +298,9 @@
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 301 public function add_meta_field( int $post_id, string $field, string $value ): bool {
322 - // WP expects a slashed value.
323 - $value = wp_slash( $value );
324 - $success = add_post_meta( $post_id, $field, $value, true ); // true means unique.
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
@@ -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.
@@ -360,13 +336,13 @@
360 336 * @param string $field Name of the post meta field.
361 337 * @return string Value of the meta field.
362 338 */
363 339 public function get_meta_field( int $post_id, string $field ): string {
364 - return get_post_meta( $post_id, $field, true ); // true means single value.
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 *
@@ -374,13 +350,13 @@
374 350 * @param string $field Name of the post meta field.
375 351 * @return bool True on success, false on error.
376 352 */
377 353 public function delete_meta_field( int $post_id, string $field ): bool {
378 - return delete_post_meta( $post_id, $field, true ); // true means single value.
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.