initial_static_options = array
(
'has_pro_version' =>0,
'show_opts' =>true,
'show_rating_message'=>true,
'display_tabs' =>true,
'required_role' =>'install_plugins',
'default_managed' =>'network', //network, singlesite
);
$this->initial_user_options = array
(
//'seconds_to_read' => 8,
);
$this->shortcodes =[
'wporg_api_pt' =>[
'description'=>__('Displays the data from WP.ORG api', 'api-info-themes-plugins-wp-org'),
'atts'=>[
[ 'type', 'plugins', __('Should be either plugins or themes', 'api-info-themes-plugins-wp-org') ],
[ 'by', 'author', __('You should set either author or tag (At this moment, only author supported)', 'api-info-themes-plugins-wp-org') ],
[ 'by_value', 'wporgusername',__('The value of the chosen by key (If author is chosen, then author username value)', 'api-info-themes-plugins-wp-org') ],
[ 'cache_time', 1440*4, __('Optimal cache time is 5760 minutes [4 days] (to flush this plugin\'s caches now, go to first options page)', 'api-info-themes-plugins-wp-org') ],
[ '___', '___', __('Individual Parameters to be obtained (below)', 'api-info-themes-plugins-wp-org') ],
[ 'name', true, __('', 'api-info-themes-plugins-wp-org') ],
[ 'icon', true, __('', 'api-info-themes-plugins-wp-org') ],
[ 'banner', false, __('', 'api-info-themes-plugins-wp-org') ],
]
]
];
$this->example_properties =
[
'themes'=>
[
// TODO
],
'plugins'=>
[
"name" => 'Plugin Name',
"slug" => 'plugin-slug',
"version" => '2.80',
"author" => 'Example.Com',
"author_profile" => 'https://profiles.wordpress.org/UserName',
"requires" => '4.2',
"tested" => '5.0.3',
"requires_php" => '',
"compatibility" => '',
"rating" => 60,
"ratings" => (Object) [ "5" => 33, "4" => 12, "3" => 4, "2" => 8, "1" => 7 ],
"num_ratings" => 52,
"support_threads" => 23,
"support_threads_resolved" => 15,
"downloaded" => 1711,
"last_updated" => '2019-01-18 4:32pm GMT',
"added" => '2017-02-05',
"homepage" => 'https://example.com/wordpress/',
"sections" => (Object)
[
"description" => '
.....
', "installation" => '....
', "faq" => '...
', "short_description" => 'Plugin adds "parent & hierarchy" functionality to posts.', "download_link" => 'https://downloads.wordpress.org/plugin/plugin-slug.zip', "screenshots" => (Object) [ "1" => (Object) ["src" => 'https://ps.w.org/plugin-slug/assets/screenshot-1.png?rev=1810430', "caption" => 'screenshot' ] ], "tags" => (Object) [ "tag1" => 'tag1', "tag2" => 'tag2'], "versions" => [], "donate_link" => 'https://paypal.me/example', ] ]; $props = $this->example_properties['plugins']; unset($props['name'], $props['icon']); foreach( $props as $key=>$value){ $this->shortcodes['wporg_api_pt']['atts'][] =[ $key, false, "" ]; //__("Show$key", 'api-info-themes-plugins-wp-org')
}
$this->shortcodes['wporg_api_pt']['atts'][] = ['cache_error_message', true, __('It hides the "cache low minutes" error message. But please, only use when you are extremelly sure what you are doing.', 'api-info-themes-plugins-wp-org')];
$this->shortcodes['wporg_api_pt']['atts'][] = ['return_only_data', false, __('By default, function returns visualised output. If you want only data-array, instead of output, then set this to true.', 'api-info-themes-plugins-wp-org')];
$this->hooks_examples = [
"wporg_api_pt_args" => [
'description' =>__('Modify request arguments which is further passed to API FETCHER function', 'api-info-themes-plugins-wp-org'),
'parameters' =>['args'],
'type' =>'filter'
],
"wporg_api_pt_url" => [
'description' =>__('Modify the url to be retrieved', 'api-info-themes-plugins-wp-org'),
'parameters' =>['url','args'],
'type' =>'filter'
],
"wporg_api_pt_response_data" => [
'description' =>__('Modify the response data from API', 'api-info-themes-plugins-wp-org'),
'parameters' =>['response_data','args'],
'type' =>'filter'
],
"wporg_api_pt_table_lines_html" => [
'description' =>__('Modify the table inner lines in output (if output used at all)', 'api-info-themes-plugins-wp-org'),
'parameters' =>['table_lines_html','args'],
'type' =>'filter'
],
"wporg_api_pt_result" => [
'description' =>__('Modify the final result', 'api-info-themes-plugins-wp-org'),
'parameters' =>['result','args'],
'type' =>'filter'
],
];
}
public function __construct_my()
{
//$this->register_stylescript('wp', 'style', 'breadcrumbs_styles', 'assets/style.css');
}
// ============================================================================================================== //
// ============================================================================================================== //
public function get_custom_property($type, $slug, $property){
$result='';
if ($type=="plugins"){
if ($property=="icon"){
$result = "https://ps.w.org/$slug/assets/icon-128x128.png";
// if that doesn't exist, then use (from Javascript) https://s.w.org/plugins/geopattern-icon/$slug.svg
}
if ($property=="banner"){
$result = "https://ps.w.org/$slug/assets/banner-772x250.png";
// if that doesn't exist, then use empty background by javascript
}
}
return $result;
}
// Docs: https://codex.wordpress.org/WordPress.org_API#Plugins
public function fetch_wp_org_api( $type, $args ){
$plugin_base = "https://api.wordpress.org/$type/info/1.1/?action=query_$type";
// ########### RESPONSE has such data: https://pastebin.com/raw/qr8JSK1K
$url = $plugin_base;
foreach($args as $key=>$value){
$url .= "&request[$key]=$value";
}
$url = apply_filters('wporg_api_pt_url', $url, $args );
$response = wp_remote_retrieve_body(wp_remote_get($url));
if($this->is_JSON_string($response)){
$result = json_decode($response, true);
} else {
$result = [];
}
return $result;
}
public function get_pt_data( $type, $args, $cache_time= 86400){
$cache_key = $this->transient_prefix . md5( $cache_time . "_". $type . "_" . json_encode( $args ) );
if ( empty($cache_time) || ! $response = $this->get_transient_CHOSEN($cache_key) )
{
$response = $this->fetch_wp_org_api( $type, $args );
if( ! empty($cache_time) ){
$this->update_transient_CHOSEN( $cache_key, $response, $cache_time );
}
}
return $response;
}
public function wporg_api_pt($atts, $content=false)
{
global $post;
$args = $this->shortcode_atts('wporg_api_pt', $atts);
if( empty($args[$key='type']) || empty($args[$key='by']) || empty($args[$key='by_value']) ){
return __("Required parameter ($key) not set.", 'api-info-themes-plugins-wp-org') ;
}
if( ! in_array($args[$key='type'],[$array='plugins','themes']) || ! in_array($args[$key='by'],[$array='author','themes'])){
return __("Required parameter ($key) not in ". print_r($array,true), 'api-info-themes-plugins-wp-org') ;
}
$args = apply_filters("wporg_api_pt_args", $args);
$type = $args['type'];
$by = $args['by'];
$by_value = $args['by_value'];;
$cache_time = $args['cache_time'];
//these two properties are custom, not included in API at this moment (v1.1, 2019.02)
$icon = $args['icon'];
$banner = $args['banner'];
// I think we can fetch all fields, and only after that, in the end result, filter only the user-chosen fields
$request_args = [$by=>$by_value];
$response_data = $this->get_pt_data( $type, $request_args, $cache_time );
// Prepare for final output
$result= apply_filters("wporg_api_pt_response_data", $response_data, $args );
$pt_array = $result[$type];
$output = '';
$output .= '| '.$key.' | '.$display_value .' |
add_filter("wporg_api_pt_request_args", "myFunc", 10, 2 ); function myFunc($request_args, $args) { .... return $request_args; }
...
add_filter("wporg_api_pt_response_data", "myFunc", 10, 2 ); function myFunc($response_data, $args) { .... return $response_data; }
...
add_filter("wporg_api_pt_table_lines_html", "myFunc", 10, 2 ); function myFunc($table_lines_html, $args) { .... return $table_lines_html; }
...
add_filter("wporg_api_pt_output", "myFunc", 10, 2 ); function myFunc($output, $args) { .... return $output; }