|
<?php |
|
/** |
|
* Plugin Name: Malformed meta demo |
|
* Plugin Version: 0.1.0 |
|
*/ |
|
|
|
namespace Malformed_Meta_Demo; |
|
|
|
const META_KEY = 'example_post_meta'; |
|
const OTHER_META_KEY = 'example_other_meta'; |
|
|
|
function bootstrap() : void { |
|
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_script' ); |
|
add_action( 'init', __NAMESPACE__ . '\\register_meta' ); |
|
register_activation_hook( __FILE__, __NAMESPACE__ . '\\activate_plugin' ); |
|
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\\deactivate_plugin' ); |
|
} |
|
bootstrap(); |
|
|
|
function enqueue_script() : void { |
|
wp_enqueue_script( |
|
'malformed-meta', |
|
trailingslashit( plugin_dir_url( __FILE__ ) ) . 'panel.js', |
|
[ |
|
'wp-edit-post', |
|
'wp-data', |
|
'wp-element', |
|
'wp-plugins', |
|
'wp-core-data', |
|
], |
|
false, |
|
true |
|
); |
|
} |
|
|
|
function register_meta() : void { |
|
register_post_meta( 'post', META_KEY, [ |
|
'single' => true, |
|
'show_in_rest' => true, |
|
] ); |
|
register_post_meta( 'post', OTHER_META_KEY, [ |
|
'single' => false, |
|
'show_in_rest' => true, |
|
] ); |
|
} |
|
|
|
// ====================================================== |
|
// SET UP DATA ON THE LATEST POST TO REPLICATE THE ISSUE. |
|
// ====================================================== |
|
|
|
function get_latest_post_id_and_hope_that_nothing_has_been_published_since_plugin_activation() : int { |
|
$latest_posts = wp_get_recent_posts( [ |
|
'numberposts' => 1, |
|
'post_type' => 'post', |
|
'post_status' => 'publish', |
|
] ); |
|
if ( is_array( $latest_posts ) && count( $latest_posts ) > 0 ) { |
|
return $latest_posts[0]['ID']; |
|
} |
|
return 0; |
|
} |
|
|
|
function activate_plugin() : void { |
|
global $wpdb; |
|
|
|
$post_id = get_latest_post_id_and_hope_that_nothing_has_been_published_since_plugin_activation(); |
|
|
|
$wpdb->query( $wpdb->prepare( |
|
'insert into wp_postmeta (post_id, meta_key, meta_value) values (%d, "%s", 333)', |
|
$post_id, |
|
META_KEY |
|
) ); |
|
$wpdb->query( $wpdb->prepare( |
|
'insert into wp_postmeta (post_id, meta_key, meta_value) values (%d, "%s", 333)', |
|
$post_id, |
|
META_KEY |
|
) ); |
|
$wpdb->query( $wpdb->prepare( |
|
'insert into wp_postmeta (post_id, meta_key, meta_value) values (%d, "%s", 333)', |
|
$post_id, |
|
META_KEY |
|
) ); |
|
wp_cache_flush(); |
|
} |
|
|
|
function deactivate_plugin() : void { |
|
global $wpdb; |
|
$wpdb->query( 'delete from wp_postmeta where meta_key="%s"', META_KEY ); |
|
$wpdb->query( 'delete from wp_postmeta where meta_key="%s"', OTHER_META_KEY ); |
|
wp_cache_flush(); |
|
} |