-
-
Save Shelob9/3c16b27e024c88a4fd35c95dfe1d54fd to your computer and use it in GitHub Desktop.
Shortcode to display number of 5-star ratings of a plugin on the WordPress.org Plugin Directory (aka Plugin Repo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_shortcode( 'five_star_reviews', 'matt_get_give_five_stars' ); | |
function matt_get_give_five_stars( $atts ) { | |
$atts = shortcode_atts( array( | |
'slug' => 'caldera-forms', | |
), $atts, 'five_star_reviews' ); | |
$plugin_slug = $atts[ 'slug' ]; | |
// Get any existing copy of our transient data | |
if ( false === ( $cachedresults = get_transient( 'wp-plugin-repo-data-' . $plugin_slug ) ) ) { | |
// It wasn't there, so regenerate the data and save the transient | |
$not_cached = 'Not Cached!'; | |
$cachedresults = matt_call_plugin_repo_api($slug=$plugin_slug); | |
set_transient( 'wp-plugin-repo-data-' . $plugin_slug, $cachedresults, 12 * HOUR_IN_SECONDS ); | |
} else { | |
$not_cached = 'Cached!'; | |
} | |
return '<p><strong>"' . ucwords(str_replace("-"," ",$cachedresults->slug)) . '"</strong> currently has <strong>' . $cachedresults->ratings[5] . '</strong> 5-star ratings. And this response is ' . $not_cached . '</p>'; | |
} | |
function matt_call_plugin_repo_api($slug) { | |
// Require plugin-install.php to query the Repo correctly | |
if( !function_exists( 'plugins_api' ) ) { | |
require_once(ABSPATH . 'wp-admin/includes/plugin-install.php' ); | |
} | |
// Only get the information we really want from the Repo API | |
$call_api = plugins_api( 'plugin_information', | |
array( | |
'slug' => $slug, | |
'fields' => array( | |
//'name' => true, -- required, cannot disable | |
//'slug' => true, -- required, cannot disable | |
//'version' => false, -- required, cannot disable | |
//'author' => false, -- required, cannot disable | |
//'contributors' => false, -- required, cannot disable | |
'requires' => false, | |
'tested' => false, | |
'compatibility' => false, | |
'rating' => true, | |
'num_ratings' => false, | |
'ratings' => true, | |
'downloaded' => false, | |
'last_updated' => false, | |
'added' => false, | |
'homepage' => false, | |
'sections' => false, | |
'tags' => false, | |
'active_installs' => false, | |
'reviews' => false, | |
//'download_link' => false, -- required, cannot disable | |
//'donate_link' => false, -- required, cannot disable | |
) ) ); | |
return $call_api; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment