Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wp Favs integration #690

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,117 @@ function tgmpa( $plugins, $config = array() ) {
}
}

if ( ! function_exists( 'tgmpa_wpfavs_plugins' ) ) {
/**
* Custom function that grabs plugins from https://wpfavs.com
* from given token
*
* @since 2.7.0
*
* @param string $token Wp Fav token key.
*
* @return array
*/
function tgmpa_wpfavs_plugins( $token ) {
if ( empty( $token ) ) {
return array();
}

$cache = json_decode( get_transient( 'tgmpa_wpfavs_plugins' ), true );

if ( empty( $cache ) ) {
// Call the API.
$response = wp_remote_get( 'https://wpfavs.com/api/v1/wpfav/' . $token );

// Make sure there are no errors.
if ( is_wp_error( $response ) ) {
return array();
}

// Decode response.
$response = apply_filters( 'tgmpa_wpfav_api_response', json_decode( wp_remote_retrieve_body( $response ), true ) );

// check for api errors.
if ( isset( $response['error'] ) ) {
return array();
}

$plugins = array();
// create our plugins array.
if ( isset( $response['plugins'] ) ) {
foreach ( $response['plugins'] as $plugin ) {
$plugins[] = array(
'name' => $plugin['name'],
'slug' => $plugin['slug'],
'source' => $plugin['data']['download_link'],
);
}
}
// in case user want to modify default array for example to pass required parameter.
$plugins = apply_filters( 'tgmpa_wpfavs_plugins', $plugins );
// save to cache.
set_transient( 'tgmpa_wpfavs_plugins', wp_json_encode( $plugins ), 1 * DAY_IN_SECONDS );
return $plugins;
}
return $cache;
}
}

if ( ! function_exists( 'tgmpa_wp_plugins' ) ) {
/**
* Custom function that grabs favorites plugins from https://wordpress.org
* from given username
*
* @since 2.7.0
*
* @param string $username Wordpress.org username.
*
* @return array
*/
function tgmpa_wp_plugins( $username ) {
if ( empty( $username ) ) {
return array();
}

$cache = json_decode( get_transient( 'tgmpa_wp_plugins' ), true );

if ( empty( $cache ) ) {

if ( ! function_exists( 'plugins_api' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
}
// Call the API.
$response = plugins_api( 'query_plugins',
array(
'user' => $username,
'per_page' => 100,
)
);

// Make sure there are no errors or there are plugins.
if ( is_wp_error( $response ) || empty( $response->plugins ) ) {
return array();
}

$plugins = array();
// create our plugins array.
foreach ( $response->plugins as $plugin ) {
$plugins[] = array(
'name' => $plugin->name,
'slug' => $plugin->slug,
);
}

// in case user want to modify default array for example to pass required parameter.
$plugins = apply_filters( 'tgmpa_wp_plugins', $plugins );
// save to cache.
set_transient( 'tgmpa_wp_plugins', wp_json_encode( $plugins ), 1 * DAY_IN_SECONDS );
return $plugins;
}
return $cache;
}
}

/**
* WP_List_Table isn't always available. If it isn't available,
* we load it here.
Expand Down
10 changes: 10 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ function my_theme_register_required_plugins() {

);

/**
* Optional you can grab your plugins from one of your Wp Favs Lists ( https://wpfavs.com )
* $plugins = tgmpa_wpfavs_plugins( '3b871fb19229696af32307da785d66e0' );
*/

/**
* Optional you can grab your plugins from your WordPress Favorites
* $plugins = tgmpa_wp_plugins( 'wp-username' );
*/

/*
* Array of configuration settings. Amend each line as needed.
*
Expand Down