开发者 | KestutisIT |
---|---|
更新时间 | 2021年4月27日 02:00 |
捐献地址: | 去捐款 |
PHP版本: | 5.4 及以上 |
WordPress版本: | 5.7 |
版权: | MIT License |
版权网址: | 版权信息 |
WordPress - Envato Market
plugins code, that is the only one built by Envato, and has so complicated and unclear code, that you never get how it works (see example below).
When I tried to create plugin's [Check for Update]
and [Validate Purchase Code]
feature-buttons in the plugin myself, and I saw the code of the WordPress - Envato Market
plugin, I was shocked how badly it is written and how you should not to code.
For example - you would like to give an error message, if Envato user token is empty, which is a required string, i.e. - pAA0aBCdeFGhiJKlmNOpqRStuVWxyZ44
. If you like K.I.S.S., PSR-2, D.R.Y., clean code coding standards and paradigms, you'd probably just have these five lines of code, so that every developer would get it:
$token = get_user_meta(get_current_user_id(), 'envato_token', TRUE); if($token == "") { return new \WP_Error('api_token_error', __('An API token is required.', 'envato-toolkit')); }
Now lets see how the same task traceback looks like in WordPress - Envato Market
plugin:
[Api.php -> request(..)]
Check if the token is empty:if ( empty( $token ) ) { return new WP_Error( 'api_token_error', __( 'An API token is required.', 'envato-market' ) ); }
1. [Api.php -> request(..)]
Parse it from another string:
$token = trim( str_replace( 'Bearer', '', $args['headers']['Authorization'] ) );
1. [Api.php -> request(..)]
Parse it one more time - this time from arguments array:
public function request( $url, $args = array() ) { $defaults = array( 'timeout' => 20, ); $args = wp_parse_args( $args, $defaults ); }
1. [Api.php -> download(..)]
Transfer the token variable one more time - this time via params:
class Envato_Market_API { public function download( $id, $args = array() ) { $url = 'https://api.envato.com/v2/market/buyer/download?item_id=' . $id . '&shorten_url=true'; return $this->request( $url, $args ); } }
1. [admin.php -> maybe_deferred_download(..)]
Pass it again - this time get it to args array from another method call:
function maybe_deferred_download( $options ) { $args = $this->set_bearer_args(); $options['package'] = envato_market()->api()->download( $vars['item_id'], $args ); return $options; }
1. [admin.php -> set_bearer_args(..)]
Wrap the token into multi-dimensional string array:
$args = array( 'headers' => array( 'Authorization' => 'Bearer ' . $token, ), );
1. [admin.php -> set_bearer_args(..)]
Pass the wrapped token one more time - this time get it from get_option:
foreach ( envato_market()->get_option( 'items', array() ) as $item ) { if ( $item['id'] === $id ) { $token = $item['token']; break; } }
1. [admin.php -> get_option(..)]
So what's in this get_option
? - Correct, another call to another method - get_options()
:
public function get_option( $name, $default = '' ) { $options = self::get_options(); $name = self::sanitize_key( $name ); return isset( $options[ $name ] ) ? $options[ $name ] : $default; }
1. [admin.php -> get_options()]
Finally, after almost 10 steps in the tree, we are finally getting the original
WordPress method call, but now I'm getting confused again - what is that option_name
variable here:
public function get_options() { return get_option( $this->option_name, array() ); }
1. [envato-market.php -> init_globals()]
Here is it is - the option name
key name is... Oh wait...
No it is not here it. It is equals to another variable, who is is put
in another clean-up function - look like I'm keep seeing this for the 2 time in the tree - the sanitization of sanitization:
$this->option_name = self::sanitize_key( $this->slug );
1. [envato-market.php -> init_globals()]
So the option name
key name is the name of $this->slug
.
Now lets see what is the value of $this->slug
:
$this->slug = 'envato-market';
So it takes eleven (!) steps to understand one variable. And the whole code of that plugin is like that. The example above was the headache I had, until I realized that I must write a new Envato API Management Toolkit, instead of trying to use what Envato is giving, because otherwise I won't get anything working ever.
And, I believe, that many other developers had the same issue when tried to create update check feature for their plugins or themes.
So instead of using that library for myself, I decided that I want to help all these developers to save their time, and I'm sharing this code with you. I'm releasing it under MIT license, which allows you to use this code in your plugin without any restrictions for both - free and commercial use.
Plus - I'm giving a promise to you, that this plugin is and will always be 100% free, without any ads, 'Subscribe', 'Follow us', 'Check our page', 'Get Pro Version' or similar links.
If you created in hi-quality code a valuable additional functionality to the library and you want to share it with everyone - I'm open here to support your efforts, and add your code to the plugin's library, so that we all together make this plugin better for authors - the better is the plugin, the better plugins authors will make for their customers. The better quality products we will have on the internet, the happier people will be all over the world.
Finally - the code is poetry - the better is the plugin, the happier is the world.
The pseudo-code of example output of the plugin is this:
`
Details about you:
List of all different plugins you bought:
$plugin): ?>
, Licenses:
Code: <?=$license['purchase_code'];?>,
License: <?=$license['license'];?>,
Purchased: <?=$license['license_purchase_date'];?> <?=$license['license_purchase_time'];?>,
Expires: <?=$license['support_expiration_date'];?> <?=$license['support_expiration_time'];?>,
Support Status: <?=$license['support_active'];?>
List of all different themes you bought:
$theme): ?>
, Licenses:
Code: <?=$license['purchase_code'];?>,
License: <?=$license['license'];?>,
Purchased: <?=$license['license_purchase_date'];?> <?=$license['license_purchase_time'];?>,
Expires: <?=$license['support_expiration_date'];?> <?=$license['support_expiration_time'];?>,
Status: <?=$license['support_active'] == 1 ? "Supported" : "Support Expired";?>
Your summary:
Your location is <?=$authorCity;?>, <?=$authorCountry;?>.
You've sold your items <?=$authorSales;?> times and you have <?=$authorFollowers;?> followers on Envato.
EnvatoToolkit
(or toolkit-for-envato
) to the /wp-content/plugins/
directory.EnvatoToolkit
and enter your Envato Username, Envato API Key and Envato Private Token.This plugin uses both - Envato Edge API and Envato Market API to retrieve required data automatically, without any need of server-in-the-middle. So there is no need to save your head revision number or last version on your server, it will get that that automatically from Envato via it's API.