| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post Model |
| 4 |
* |
| 5 |
* @package TablePress |
| 6 |
* @subpackage Models |
| 7 |
* @author Tobias Bäthge |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Prohibit direct script loading. |
| 12 |
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 13 |
|
| 14 |
/** |
| 15 |
* Post Model class |
| 16 |
* |
| 17 |
* @package TablePress |
| 18 |
* @subpackage Models |
| 19 |
* @author Tobias Bäthge |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class TablePress_Post_Model extends TablePress_Model { |
| 23 |
|
| 24 |
/** |
| 25 |
* Name of the "Custom Post Type" for the tables. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
protected string $post_type = 'tablepress_table'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Inits the model by registering the Custom Post Type. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
parent::__construct(); |
| 38 |
$this->_register_post_type(); // We are on WP "init" hook already. |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Registers the Custom Post Type which the tables use. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
*/ |
| 46 |
protected function _register_post_type(): void { |
| 47 |
/** |
| 48 |
* Filters the "Custom Post Type" that TablePress uses for storing tables in the database. |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
* |
| 52 |
* @param string $post_type The "Custom Post Type" that TablePress uses. |
| 53 |
*/ |
| 54 |
$this->post_type = apply_filters( 'tablepress_post_type', $this->post_type ); |
| 55 |
$post_type_args = array( |
| 56 |
'labels' => array( |
| 57 |
'name' => 'TablePress Tables', |
| 58 |
), |
| 59 |
'public' => false, |
| 60 |
'show_ui' => false, |
| 61 |
'query_var' => false, |
| 62 |
'rewrite' => false, |
| 63 |
'capability_type' => 'tablepress_table', // This ensures, that WP's regular CPT UI respects our capabilities. |
| 64 |
'map_meta_cap' => false, // Integrated WP mapping does not fit our needs, therefore use our own in a filter. |
| 65 |
'supports' => array( 'title', 'editor', 'excerpt', 'revisions' ), |
| 66 |
'can_export' => true, |
| 67 |
); |
| 68 |
/** |
| 69 |
* Filters the arguments for the registration of the "Custom Post Type" that TablePress uses. |
| 70 |
* |
| 71 |
* @since 1.0.0 |
| 72 |
* |
| 73 |
* @param array<string, mixed> $post_type_args Arguments for the registration of the TablePress "Custom Post Type". |
| 74 |
*/ |
| 75 |
$post_type_args = apply_filters( 'tablepress_post_type_args', $post_type_args ); |
| 76 |
register_post_type( $this->post_type, $post_type_args ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Inserts a post with the correct Custom Post Type and default values in the the wp_posts table in the database. |
| 81 |
* |
| 82 |
* @since 1.0.0 |
| 83 |
* |
| 84 |
* @param array<string, mixed> $post Post to insert. |
| 85 |
* @return int|WP_Error Post ID of the inserted post on success, WP_Error on error. |
| 86 |
*/ |
| 87 |
public function insert( array $post ) /* : int|WP_Error */ { |
| 88 |
$default_post = array( |
| 89 |
'ID' => false, // false on new insert, but existing post ID on update. |
| 90 |
'comment_status' => 'closed', |
| 91 |
'ping_status' => 'closed', |
| 92 |
'post_category' => false, |
| 93 |
'post_content' => '', |
| 94 |
'post_excerpt' => '', |
| 95 |
'post_parent' => 0, |
| 96 |
'post_password' => '', |
| 97 |
'post_status' => 'publish', |
| 98 |
'post_title' => '', |
| 99 |
'post_type' => $this->post_type, |
| 100 |
'tags_input' => '', |
| 101 |
'to_ping' => '', |
| 102 |
); |
| 103 |
$post = array_merge( $default_post, $post ); |
| 104 |
// WP expects everything to be slashed. |
| 105 |
$post = wp_slash( $post ); |
| 106 |
|
| 107 |
// Remove balanceTags() from sanitize_post(), as it can destroy the JSON when messing with HTML. |
| 108 |
remove_filter( 'content_save_pre', 'balanceTags', 50 ); |
| 109 |
remove_filter( 'excerpt_save_pre', 'balanceTags', 50 ); |
| 110 |
|
| 111 |
/* |
| 112 |
* Remove possible KSES filtering, as it can destroy the JSON when messing with HTML. |
| 113 |
* KSES filtering is done to table cells individually, when saving. |
| 114 |
*/ |
| 115 |
$has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) ); |
| 116 |
if ( $has_kses ) { |
| 117 |
kses_remove_filters(); |
| 118 |
} |
| 119 |
|
| 120 |
// Remove filter that adds `rel="noopener" to <a> HTML tags, but destroys JSON code. See https://core.trac.wordpress.org/ticket/46316. |
| 121 |
$has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) ); |
| 122 |
if ( $has_targeted_link_rel_filters ) { |
| 123 |
wp_remove_targeted_link_rel_filters(); // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_remove_targeted_link_rel_filtersFound |
| 124 |
} |
| 125 |
|
| 126 |
$post_id = wp_insert_post( $post, true ); |
| 127 |
|
| 128 |
// Restore removed content filters. |
| 129 |
add_filter( 'content_save_pre', 'balanceTags', 50 ); |
| 130 |
add_filter( 'excerpt_save_pre', 'balanceTags', 50 ); |
| 131 |
if ( $has_kses ) { |
| 132 |
kses_init_filters(); |
| 133 |
} |
| 134 |
if ( $has_targeted_link_rel_filters ) { |
| 135 |
wp_init_targeted_link_rel_filters(); // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_init_targeted_link_rel_filtersFound |
| 136 |
} |
| 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 |
|
| 143 |
return $post_id; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Updates an existing post with the correct Custom Post Type and default values in the the wp_posts table in the database. |
| 148 |
* |
| 149 |
* @since 1.0.0 |
| 150 |
* |
| 151 |
* @param array<string, mixed> $post Post. |
| 152 |
* @return int|WP_Error Post ID of the updated post on success, WP_Error on error. |
| 153 |
*/ |
| 154 |
public function update( array $post ) /* : int|WP_Error */ { |
| 155 |
$default_post = array( |
| 156 |
'ID' => false, // false on new insert, but existing post ID on update. |
| 157 |
'comment_status' => 'closed', |
| 158 |
'ping_status' => 'closed', |
| 159 |
'post_category' => false, |
| 160 |
'post_content' => '', |
| 161 |
'post_excerpt' => '', |
| 162 |
'post_parent' => 0, |
| 163 |
'post_password' => '', |
| 164 |
'post_status' => 'publish', |
| 165 |
'post_title' => '', |
| 166 |
'post_type' => $this->post_type, |
| 167 |
'tags_input' => '', |
| 168 |
'to_ping' => '', |
| 169 |
); |
| 170 |
$post = array_merge( $default_post, $post ); |
| 171 |
// WP expects everything to be slashed. |
| 172 |
$post = wp_slash( $post ); |
| 173 |
|
| 174 |
// Remove balanceTags() from sanitize_post(), as it can destroy the JSON when messing with HTML. |
| 175 |
remove_filter( 'content_save_pre', 'balanceTags', 50 ); |
| 176 |
remove_filter( 'excerpt_save_pre', 'balanceTags', 50 ); |
| 177 |
|
| 178 |
/* |
| 179 |
* Remove possible KSES filtering, as it can destroy the JSON when messing with HTML. |
| 180 |
* KSES filtering is done to table cells individually, when saving. |
| 181 |
*/ |
| 182 |
$has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) ); |
| 183 |
if ( $has_kses ) { |
| 184 |
kses_remove_filters(); |
| 185 |
} |
| 186 |
|
| 187 |
// Remove filter that adds `rel="noopener" to <a> HTML tags, but destroys JSON code. See https://core.trac.wordpress.org/ticket/46316. |
| 188 |
$has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) ); |
| 189 |
if ( $has_targeted_link_rel_filters ) { |
| 190 |
wp_remove_targeted_link_rel_filters(); // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_remove_targeted_link_rel_filtersFound |
| 191 |
} |
| 192 |
|
| 193 |
$post_id = wp_update_post( $post, true ); |
| 194 |
|
| 195 |
// Restore removed content filters. |
| 196 |
add_filter( 'content_save_pre', 'balanceTags', 50 ); |
| 197 |
add_filter( 'excerpt_save_pre', 'balanceTags', 50 ); |
| 198 |
if ( $has_kses ) { |
| 199 |
kses_init_filters(); |
| 200 |
} |
| 201 |
if ( $has_targeted_link_rel_filters ) { |
| 202 |
wp_init_targeted_link_rel_filters(); // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_init_targeted_link_rel_filtersFound |
| 203 |
} |
| 204 |
|
| 205 |
return $post_id; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Gets a post from the wp_posts table in the database. |
| 210 |
* |
| 211 |
* @since 1.0.0 |
| 212 |
* |
| 213 |
* @param int $post_id Post ID. |
| 214 |
* @return WP_Post|false Post on success, false on error. |
| 215 |
*/ |
| 216 |
public function get( int $post_id ) /* : WP_Post|false */ { |
| 217 |
$post = get_post( $post_id ); |
| 218 |
if ( is_null( $post ) ) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
return $post; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Deletes a post (and all revisions) from the wp_posts table in the database. |
| 226 |
* |
| 227 |
* @since 1.0.0 |
| 228 |
* |
| 229 |
* @param int $post_id Post ID. |
| 230 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
| 231 |
*/ |
| 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. |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Moves a post to the trash (if trash is globally enabled), instead of directly deleting the post. |
| 238 |
* (yet unused) |
| 239 |
* |
| 240 |
* @since 1.0.0 |
| 241 |
* |
| 242 |
* @param int $post_id Post ID. |
| 243 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
| 244 |
*/ |
| 245 |
public function trash( int $post_id ) /* : WP_Post|false|null */ { |
| 246 |
return wp_trash_post( $post_id ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Restores a post from the trash. |
| 251 |
* (yet unused) |
| 252 |
* |
| 253 |
* @since 1.0.0 |
| 254 |
* |
| 255 |
* @param int $post_id Post ID. |
| 256 |
* @return WP_Post|false|null Post on success, false or null on error. |
| 257 |
*/ |
| 258 |
public function untrash( int $post_id ) /* : WP_Post|false */ { |
| 259 |
return wp_untrash_post( $post_id ); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Loads all posts with one query, to prime the cache. |
| 264 |
* |
| 265 |
* @since 1.0.0 |
| 266 |
* |
| 267 |
* @global wpdb $wpdb WordPress database abstraction object. |
| 268 |
* @see get_post() |
| 269 |
* |
| 270 |
* @param int[] $all_post_ids List of Post IDs. |
| 271 |
* @param bool $update_meta_cache Optional. Whether to update the Post Meta Cache (for table options and visibility). |
| 272 |
*/ |
| 273 |
public function load_posts( array $all_post_ids, bool $update_meta_cache = true ): void { |
| 274 |
global $wpdb; |
| 275 |
|
| 276 |
// Split post loading, to save memory. |
| 277 |
while ( ! empty( $all_post_ids ) ) { |
| 278 |
$post_ids = array_splice( $all_post_ids, 0, 100 ); // Extract 100 posts at a time. |
| 279 |
// Don't load posts that are in the cache already. |
| 280 |
$post_ids = _get_non_cached_ids( $post_ids, 'posts' ); |
| 281 |
if ( ! empty( $post_ids ) ) { |
| 282 |
$post_ids_list = implode( ',', $post_ids ); |
| 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 |
| 285 |
update_post_cache( $posts ); |
| 286 |
if ( $update_meta_cache ) { |
| 287 |
// Get all post meta data for all table posts, @see get_post_meta(). |
| 288 |
update_meta_cache( 'post', $post_ids ); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Counts the number of posts with the model's CPT in the wp_posts table in the database. |
| 296 |
* (currently for debug only) |
| 297 |
* |
| 298 |
* @since 1.0.0 |
| 299 |
* |
| 300 |
* @return int Number of posts. |
| 301 |
*/ |
| 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. |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Adds a post meta field to a post. |
| 308 |
* |
| 309 |
* @since 1.0.0 |
| 310 |
* |
| 311 |
* @param int $post_id ID of the post for which the field shall be added. |
| 312 |
* @param string $field Name of the post meta field. |
| 313 |
* @param string $value Value of the post meta field (not slashed). |
| 314 |
* @return bool True on success, false on error. |
| 315 |
*/ |
| 316 |
public function add_meta_field( int $post_id, string $field, string $value ): bool { |
| 317 |
// WP expects a slashed value. |
| 318 |
$value = wp_slash( $value ); |
| 319 |
$success = add_post_meta( $post_id, $field, $value, true ); // true means unique. |
| 320 |
// Make sure that $success is a boolean, as add_post_meta() returns an ID or false. |
| 321 |
$success = ( false === $success ) ? false : true; |
| 322 |
return $success; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Updatse the value of a post meta field of a post. |
| 327 |
* |
| 328 |
* If the field does not yet exist, it is added. |
| 329 |
* |
| 330 |
* @since 1.0.0 |
| 331 |
* |
| 332 |
* @param int $post_id ID of the post for which the field shall be updated. |
| 333 |
* @param string $field Name of the post meta field. |
| 334 |
* @param string $value Value of the post meta field (not slashed). |
| 335 |
* @return bool True on success, false on error. |
| 336 |
*/ |
| 337 |
public function update_meta_field( int $post_id, string $field, string $value ): bool { |
| 338 |
$prev_value = (string) get_post_meta( $post_id, $field, true ); |
| 339 |
// No need to update, if values are equal (also, update_post_meta() would return false for this). |
| 340 |
if ( $prev_value === $value ) { |
| 341 |
return true; |
| 342 |
} |
| 343 |
|
| 344 |
// WP expects a slashed value. |
| 345 |
$value = wp_slash( $value ); |
| 346 |
return (bool) update_post_meta( $post_id, $field, $value, $prev_value ); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Gets the value of a post meta field of a post. |
| 351 |
* |
| 352 |
* @since 1.0.0 |
| 353 |
* |
| 354 |
* @param int $post_id ID of the post for which the field shall be retrieved. |
| 355 |
* @param string $field Name of the post meta field. |
| 356 |
* @return string Value of the meta field. |
| 357 |
*/ |
| 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. |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Deletes a post meta field of a post. |
| 364 |
* (yet unused) |
| 365 |
* |
| 366 |
* @since 1.0.0 |
| 367 |
* |
| 368 |
* @param int $post_id ID of the post of which the field shall be deleted. |
| 369 |
* @param string $field Name of the post meta field. |
| 370 |
* @return bool True on success, false on error. |
| 371 |
*/ |
| 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. |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Returns the Custom Post Type that TablePress uses. |
| 378 |
* |
| 379 |
* @since 1.5.0 |
| 380 |
* |
| 381 |
* @return string The used Custom Post Type. |
| 382 |
*/ |
| 383 |
public function get_post_type(): string { |
| 384 |
return $this->post_type; |
| 385 |
} |
| 386 |
|
| 387 |
} // class TablePress_Post_Model |
| 388 |
|