williamfridh / pattern_friend

WordPress plugin that extends the default Gutenberg
GNU General Public License v2.0
0 stars 0 forks source link

Review - Generic function/class/define/namespace/option names #26

Closed williamfridh closed 1 month ago

williamfridh commented 2 months ago

All plugins must have unique function names, namespaces, defines, class and option names. This prevents your plugin from conflicting with other plugins or themes. We need you to update your plugin to use more unique and distinct names.

A good way to do this is with a prefix. For example, if your plugin is called "Easy Custom Post Types" then you could use names like these: function ecpt_save_post() class ECPT_Admin{} namespace ECPT; update_option( 'ecpt_settings', $settings ); define( 'ECPT_LICENSE', true ); global $ecpt_options;

Don't try to use two (2) or three (3) letter prefixes anymore. We host nearly 100-thousand plugins on WordPress.org alone. There are tens of thousands more outside our servers. Believe us, you’re going to run into conflicts.

You also need to avoid the use of _ (double underscores), wp , or _ (single underscore) as a prefix. Those are reserved for WordPress itself. You can use them inside your classes, but not as stand-alone function.

Please remember, if you're using _n() or __() for translation, that's fine. We're only talking about functions you've created for your plugin, not the core functions from WordPress. In fact, those core features are why you need to not use those prefixes in your own plugin! You don't want to break WordPress for your users.

Related to this, using if (!function_exists('NAME')) { around all your functions and classes sounds like a great idea until you realize the fatal flaw. If something else has a function with the same name and their code loads first, your plugin will break. Using if-exists should be reserved for shared libraries only.

Remember: Good prefix names are unique and distinct to your plugin. This will help you and the next person in debugging, as well as prevent conflicts.

Analysis result:

This plugin is using the prefix "patternfriend" for 1 element(s).

This plugin is using the prefix "pf" for 6 element(s).

The prefix "pf" is too short, we require prefixes at least over 4 characters.

pattern_friend/pattern_friend.php:271 add_option('pf_mobile_max_threshold', $this->default_settings['deviceThresholds']['mobileMaxThreshold']); pattern_friend/pattern_friend.php:272 add_option('pf_tablet_max_threshold', $this->default_settings['deviceThresholds']['tabletMaxThreshold']); pattern_friend/pattern_friend.php:273 add_option('pf_header_sticky', $this->default_settings['headerFooter']['headerSticky']); pattern_friend/Routes.php:112 update_option('pf_mobile_max_threshold', $pf_mobile_max_threshold); pattern_friend/Routes.php:113 update_option('pf_tablet_max_threshold', $pf_tablet_max_threshold); pattern_friend/Routes.php:169 update_option('pf_header_sticky', $pf_header_sticky);

Looks like there are elements not using common prefixes.

pattern_friend/pattern_friend.php:240 set_transient('plugin_activated', true, 5); pattern_friend/pattern_friend.php:121 $screen_id_options;

williamfridh commented 1 month ago

Implemented and tested. Seems to be working!