I gave Meta AI a break, because I had to do most of the refactoring and debugging by hand. I lost track of time, but I’m guessing it was 4 or 5 hours work, because I had a typo that screwed me up for at least an hour.
The code for this post is here, and it appears to work: https://github.com/johnkawakami/wp-plugin-llm-experiment/tree/meta2
What I should have done, was pass the code through the AI, and ask it to find the bug. When I finally did that, after suffering all kinds of debugging work, it found the bug immediately. I would have saved so much time!
The refactoring has my folders looking like this:
├── README.md
├── plugin.php
├── LICENSE
├── includes
│ ├── ReCaptcha.php
│ └── Correction
│ ├── CorrectionPostType.php
│ ├── CorrectionPlugin.php
│ ├── CorrectionLinkShortcode.php
│ └── CorrectionFormShortcode.php
├── composer.json
└── admin
├── ReCaptchaAdmin.php
├── Correction
│ └── CorrectionAdmin.php
└── AbstractAdmin.php
The longest file is CorrectionAdmin.php, at 140 lines.
The Composer script to clean up and prettify the code takes care of most of the wonky code that Meta (and Gemini) produced.
Handler Registration Order
Initially, everything stopped loading correctly. The problem was pretty simple:
I was registering an action on ‘init’ within an action triggered by ‘admin_init’.
So the problem was, the action was never called, because ‘init’ had already fired.
Here’s some code to show what was happening:
add_action('admin_init', function () {
add_action('init', 'foo');
});
I just didn’t see it, because the inner action was hidden away in the CorrectionAdmin class.
The fix was simple: make sure the registration happens within an ‘init’ action.
Since there isn’t any nice way to enforce this, I opted to use an abstract Admin class to enforce the definition of a register() method, and documented the fact it should be called within an ‘init’ action.
The entire plugin is loaded within and ‘init’ action, so, I found that you can either make the call to register() immediately, or add it as an action on ‘init’:
$correction_admin = new CorrectionAdmin();
// call it immediately
$correction_admin->register();
// or add the call to 'init'
add_action( 'init', [$correction_admin, 'register'] );
Meta Barfing
When I asked it to debug my code, it did so, but it also didn’t stop writing gibberish afterward. There’s some problem in the machine.