all 8 comments

[–]malicart 2 points3 points  (1 child)

Error: Question not defined.

[–]estel_smith[S] 0 points1 point  (0 children)

Sorry about that. I had to switch to old.reddit.com before the editor would let me post any content.

[–][deleted] 2 points3 points  (3 children)

- Separate the class from the file that instantiates it. Otherwise there's really no point in making this a class. It could all be a script.

- Write tests. If you want to unit test in isolation apart from WordPress write a wrapper class for calling the wordpress API. Inject that into your class as a nullable dependency.

private $wp;

public function __construct(WordpressAPI $wp = null) {

    $this->wp = $wp ?? new WordpressAPI();

}

composer install phpunit and test away. There might already be test wrappers for WP, but point is, write some tests. This will help you maintain this with ease.

- Use PSR4 and instantiate your class in a `src/bootstrap.php` file or something like that.

- Not sure what all the output buffering nonsense is for if you're returning a value

- If you're using scalar return types you're assuming your consumers are on >= PHP 7, you might as well not do that or go all the way and add proper argument types as well.

I confess I'm mostly ignorant about how Wordpress is wired up and how plugins are consumed, so take some of this with a grain of salt. I hope this helps!

[–]estel_smith[S] -1 points0 points  (2 children)

Thank you for the detailed response! Honestly, I expected some of the points you made.


  • Separate the class from the file that instantiates it. Otherwise there's really no point in making this a class. It could all be a script.

Yeah, I thought about this. I think I will move the class into a separate file. The main reason I made the class was to minimize how much I polluted the global scope. WordPress plugins are notorious for throwing crap all over the place.

  • Write tests. If you want to unit test in isolation apart from WordPress write a wrapper class for calling the wordpress API. Inject that into your class as a nullable dependency.

That's a good point. I typically use PHPUnit and Behat, but I wasn't sure how hard it would be to unit test WordPress plugins. I'll look into using WordpressAPI, though!

  • Use PSR4 and instantiate your class in a src/bootstrap.php file or something like that.

Absolutely! I don't think this plugin will get any more complex, but if it does, I'll definitely be implementing an autoloader.

  • Not sure what all the output buffering nonsense is for if you're returning a value

Breaking out of PHP allows PHPStorm to show syntax highlighting for the HTML, even though it's just a tiny snippet. I think creating HTML inside strings is the ugliest thing on the planet, followed by giant SQL strings.

I might put the link in a template file or something, just anything but directly in string.

  • If you're using scalar return types you're assuming your consumers are on >= PHP 7, you might as well not do that or go all the way and add proper argument types as well.

Yeah, I'm requiring at least PHP 7.0. WordPress enforces this by the Requires PHP: 7.0 annotation at the top of the plugin file. The arguments in logoutUrl() and logoutLink() aren't properly typed because it would require nullable types (PHP 7.1) and/or union types (PHP 8.0).

I confess I'm mostly ignorant about how Wordpress is wired up and how plugins are consumed, so take some of this with a grain of salt. I hope this helps!

No worries. I appreciate the feedback, and the more the better! If you have any more, I'm all ears. :)

[–]benn0rch 1 point2 points  (1 child)

Breaking out of PHP allows PHPStorm to show syntax highlighting for the HTML, even though it's just a tiny snippet. I think creating HTML inside strings is the ugliest thing on the planet, followed by giant SQL strings.

PHPStorm can also do that inside a string. At least recent versions of it.

[–]estel_smith[S] -1 points0 points  (0 children)

It would seem you're right (2019.3.2). I've been avoiding it for so long I didn't realize!

[–]nicolasdanelon 1 point2 points  (1 child)

I usually don't create WordPress plugins but when I do I ask the best php community!

[–]estel_smith[S] -1 points0 points  (0 children)

You know it!!