| @@ -24,9 +24,8 @@ | ||
| 24 | 24 | /** |
| 25 | 25 | * Name of the "Custom Post Type" for the tables. |
| 26 | 26 | * |
| 27 | 27 | * @since 1.0.0 |
| 28 | - * @var lowercase-string&non-empty-string | |
| 29 | 28 | */ |
| 30 | 29 | protected string $post_type = 'tablepress_table'; |
| 31 | 30 | |
| 32 | 31 | /** |
| @@ -49,9 +48,9 @@ | ||
| 49 | 48 | * Filters the "Custom Post Type" that TablePress uses for storing tables in the database. |
| 50 | 49 | * |
| 51 | 50 | * @since 1.0.0 |
| 52 | 51 | * |
| 53 | - * @param lowercase-string&non-empty-string $post_type The "Custom Post Type" that TablePress uses. | |
| 52 | + * @param string $post_type The "Custom Post Type" that TablePress uses. | |
| 54 | 53 | */ |
| 55 | 54 | $this->post_type = apply_filters( 'tablepress_post_type', $this->post_type ); |
| 56 | 55 | $post_type_args = array( |
| 57 | 56 | 'labels' => array( |
| @@ -77,9 +76,9 @@ | ||
| 77 | 76 | register_post_type( $this->post_type, $post_type_args ); |
| 78 | 77 | } |
| 79 | 78 | |
| 80 | 79 | /** |
| 81 | - * Inserts a post with the correct Custom Post Type and default values in 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. |
| @@ -117,8 +116,14 @@ | ||
| 117 | 116 | if ( $has_kses ) { |
| 118 | 117 | kses_remove_filters(); |
| 119 | 118 | } |
| 120 | 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(); | |
| 124 | + } | |
| 125 | + | |
| 121 | 126 | $post_id = wp_insert_post( $post, true ); |
| 122 | 127 | |
| 123 | 128 | // Restore removed content filters. |
| 124 | 129 | add_filter( 'content_save_pre', 'balanceTags', 50 ); |
| @@ -125,8 +130,11 @@ | ||
| 125 | 130 | add_filter( 'excerpt_save_pre', 'balanceTags', 50 ); |
| 126 | 131 | if ( $has_kses ) { |
| 127 | 132 | kses_init_filters(); |
| 128 | 133 | } |
| 134 | + if ( $has_targeted_link_rel_filters ) { | |
| 135 | + wp_init_targeted_link_rel_filters(); | |
| 136 | + } | |
| 129 | 137 | |
| 130 | 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. |
| 131 | 139 | if ( 0 === $post_id ) { // @phpstan-ignore identical.alwaysFalse (False-positive in the PHPStan WordPress stubs.) |
| 132 | 140 | return new WP_Error( 'post_insert', '' ); |
| @@ -135,9 +143,9 @@ | ||
| 135 | 143 | return $post_id; |
| 136 | 144 | } |
| 137 | 145 | |
| 138 | 146 | /** |
| 139 | - * Updates an existing post with the correct Custom Post Type and default values in 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. | |
| 140 | 148 | * |
| 141 | 149 | * @since 1.0.0 |
| 142 | 150 | * |
| 143 | 151 | * @param array<string, mixed> $post Post. |
| @@ -175,8 +183,14 @@ | ||
| 175 | 183 | if ( $has_kses ) { |
| 176 | 184 | kses_remove_filters(); |
| 177 | 185 | } |
| 178 | 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(); | |
| 191 | + } | |
| 192 | + | |
| 179 | 193 | $post_id = wp_update_post( $post, true ); |
| 180 | 194 | |
| 181 | 195 | // Restore removed content filters. |
| 182 | 196 | add_filter( 'content_save_pre', 'balanceTags', 50 ); |
| @@ -183,8 +197,11 @@ | ||
| 183 | 197 | add_filter( 'excerpt_save_pre', 'balanceTags', 50 ); |
| 184 | 198 | if ( $has_kses ) { |
| 185 | 199 | kses_init_filters(); |
| 186 | 200 | } |
| 201 | + if ( $has_targeted_link_rel_filters ) { | |
| 202 | + wp_init_targeted_link_rel_filters(); | |
| 203 | + } | |
| 187 | 204 | |
| 188 | 205 | return $post_id; |
| 189 | 206 | } |
| 190 | 207 | |
| @@ -296,9 +313,11 @@ | ||
| 296 | 313 | * @param string $value Value of the post meta field (not slashed). |
| 297 | 314 | * @return bool True on success, false on error. |
| 298 | 315 | */ |
| 299 | 316 | public function add_meta_field( int $post_id, string $field, string $value ): bool { |
| 300 | - $success = add_post_meta( $post_id, wp_slash( $field ), wp_slash( $value ), true ); // WP expects slashed values, `true` means unique. | |
| 317 | + // WP expects a slashed value. | |
| 318 | + $value = wp_slash( $value ); | |
| 319 | + $success = add_post_meta( $post_id, $field, $value, true ); // true means unique. | |
| 301 | 320 | // Make sure that $success is a boolean, as add_post_meta() returns an ID or false. |
| 302 | 321 | $success = ( false === $success ) ? false : true; |
| 303 | 322 | return $success; |
| 304 | 323 | } |
| @@ -321,9 +340,11 @@ | ||
| 321 | 340 | if ( $prev_value === $value ) { |
| 322 | 341 | return true; |
| 323 | 342 | } |
| 324 | 343 | |
| 325 | - return (bool) update_post_meta( $post_id, wp_slash( $field ), wp_slash( $value ), $prev_value ); // WP expects slashed values. | |
| 344 | + // WP expects a slashed value. | |
| 345 | + $value = wp_slash( $value ); | |
| 346 | + return (bool) update_post_meta( $post_id, $field, $value, $prev_value ); | |
| 326 | 347 | } |
| 327 | 348 | |
| 328 | 349 | /** |
| 329 | 350 | * Gets the value of a post meta field of a post. |
| @@ -334,9 +355,9 @@ | ||
| 334 | 355 | * @param string $field Name of the post meta field. |
| 335 | 356 | * @return string Value of the meta field. |
| 336 | 357 | */ |
| 337 | 358 | public function get_meta_field( int $post_id, string $field ): string { |
| 338 | - return get_post_meta( $post_id, $field, true ); // `true` means single value. | |
| 359 | + return get_post_meta( $post_id, $field, true ); // true means single value. | |
| 339 | 360 | } |
| 340 | 361 | |
| 341 | 362 | /** |
| 342 | 363 | * Deletes a post meta field of a post. |
| @@ -348,9 +369,9 @@ | ||
| 348 | 369 | * @param string $field Name of the post meta field. |
| 349 | 370 | * @return bool True on success, false on error. |
| 350 | 371 | */ |
| 351 | 372 | public function delete_meta_field( int $post_id, string $field ): bool { |
| 352 | - return delete_post_meta( $post_id, wp_slash( $field ) ); // WP expects a slashed value. | |
| 373 | + return delete_post_meta( $post_id, $field, true ); // true means single value. | |
| 353 | 374 | } |
| 354 | 375 | |
| 355 | 376 | /** |
| 356 | 377 | * Returns the Custom Post Type that TablePress uses. |