diff --git a/class-tgm-plugin-activation.php b/class-tgm-plugin-activation.php index 1aa2e3a0..da2383ad 100755 --- a/class-tgm-plugin-activation.php +++ b/class-tgm-plugin-activation.php @@ -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. diff --git a/example.php b/example.php index c10fbb04..be15b183 100755 --- a/example.php +++ b/example.php @@ -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. *