string WP_Post->ID If Classic Editor Plugin
-
Background: I am developing my first plugins to create my first WP (or any) CMS website. I replaced my computer’s storage device and updated to the newest WordPress, version 5.2.3, which comes with the new blocks editor. During my previous development I had the classic/TinyMCE editor as the default core editor. My plugins make widgets and manage metadata with their own meta boxes. Therefore, they do not entirely work with the block editor API. I want to finish these plugins and make a website with the classic/Tiny editor before seriously studying and applying the Blocks Editor Handbook.
Problem: There is a display callback passed as the third argument of WP function add_meta_box(). The sole argument passed to that callback, I call it class method ‘function html( $post ) {…}’, is a post value. Value $post is always an instance of WP_Post, as desired. If I DO NOT have the Classic Editor plugin, version 1.5, active, the block editor is active and the value of $post->ID is an int, e.g. int(11), as desired. If Classic Editor plugin 1.5 is active, the value of $post->ID is a string, e.g. string “11”. I used echo and var_dump to demonstrate that at the start of method html().
Evaluation Effort: The Classic Editor plugin 1.5 implementation is one php file and one very small JS file. I could not find anything wrong by visual inspection of those files and the core WP files (not all of them, of course). I can’t locate the plumbing that feeds the meta box display callback, e.g. my method html().
Concerns:
(1) I think that post ids should always be ints in the PHP world. True? I see core code that casts to (int). I I am supposing I should just cast to int in my callback method. In core code, wouldn’t it make more sense to use assert( is_int($post_id), … ) for post IDs that should not be unscrubbed user data from the client side but ‘inside’. Detect the problem early and trace less?(2) The other approach would be to accept post ID regardless of its type and adapt to it. Meaning I could pass it as is to WP functions. I also need to compare post IDs from my metadata apples to apples (or start using loosing typing, which I don’t like to do).
(3) An aside, it seems (saw somewhere in WP core) that the value int(0) is NOT a valid post ID for an actual post (it’s falsy and tested that way, not my favorite semantic approach). Is it true that int zero is definitively understood to be no actual post? If so, is it documented? Importantly to me as an implementation detail, can zero be used as a post ID in the MySQL/MariaDB database (i.e. table wp_posts, column ID and table wp_postmeta, column post_id ). In my database they are the type bigint(20) unsigned. Maybe the auto_increment attribute prevents zero from being used?
I consider PHP zero to be a falsy and loose-typing hazard to avoid. I’m not a seasoned programmer, and I don’t presume what others should do.
Thanks for reading, kind person.
- The topic ‘string WP_Post->ID If Classic Editor Plugin’ is closed to new replies.