| 1 |
<?php |
| 2 |
/** |
| 3 |
* File responsible for showing plugins inside the featured tab. |
| 4 |
* |
| 5 |
* This is used to display information about limited events, such as Black Friday. |
| 6 |
* |
| 7 |
* @package ThemeIsleSDK |
| 8 |
* @subpackage Modules |
| 9 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 10 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 11 |
* @since 3.3.0 |
| 12 |
*/ |
| 13 |
namespace ThemeisleSDK\Modules; |
| 14 |
|
| 15 |
use ThemeisleSDK\Common\Abstract_Module; |
| 16 |
use ThemeisleSDK\Product; |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Featured_Plugins module for the ThemeIsle SDK. |
| 25 |
*/ |
| 26 |
class Featured_Plugins extends Abstract_Module { |
| 27 |
|
| 28 |
/** |
| 29 |
* The transient key prefix. |
| 30 |
* |
| 31 |
* @var string $transient_key |
| 32 |
*/ |
| 33 |
private $transient_key = 'themeisle_sdk_featured_plugins_'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Check if the module can be loaded. |
| 37 |
* |
| 38 |
* @param Product $product Product data. |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function can_load( $product ) { |
| 43 |
if ( $this->is_from_partner( $product ) ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
if ( $product->is_wordpress_available() ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
return ! apply_filters( 'themeisle_sdk_disable_featured_plugins', false ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Load the module for the selected product. |
| 56 |
* |
| 57 |
* @param Product $product Product data. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function load( $product ) { |
| 62 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// bail if we already registered a filter for the plugin API. |
| 67 |
if ( apply_filters( 'themeisle_sdk_plugin_api_filter_registered', false ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
add_filter( 'themeisle_sdk_plugin_api_filter_registered', '__return_true' ); |
| 71 |
|
| 72 |
add_filter( 'plugins_api_result', [ $this, 'filter_plugin_api_results' ], 10, 3 ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Filter the plugin API results to include the featured plugins. |
| 77 |
* |
| 78 |
* @param object $res The result object. |
| 79 |
* @param string $action The type of information being requested from the Plugin Install API. |
| 80 |
* @param object $args Plugin API arguments. |
| 81 |
* |
| 82 |
* @return object |
| 83 |
*/ |
| 84 |
public function filter_plugin_api_results( $res, $action, $args ) { |
| 85 |
|
| 86 |
if ( 'query_plugins' !== $action ) { |
| 87 |
return $res; |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! isset( $args->browse ) || $args->browse !== 'featured' ) { |
| 91 |
return $res; |
| 92 |
} |
| 93 |
|
| 94 |
$featured = $this->query_plugins_by_author( $args ); |
| 95 |
|
| 96 |
$plugins = array_merge( $featured, (array) $res->plugins ); |
| 97 |
$plugins = array_slice( $plugins, 0, $res->info['results'] ); |
| 98 |
$res->plugins = $plugins; |
| 99 |
|
| 100 |
return $res; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Query plugins by author. |
| 105 |
* |
| 106 |
* @param object $args The arguments for the query. |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
private function query_plugins_by_author( $args ) { |
| 111 |
$featured = []; |
| 112 |
|
| 113 |
$optimole_filter_slugs = apply_filters( 'themeisle_sdk_optimole_filter_slugs', [ 'optimole-wp' ] ); |
| 114 |
$filtered_from_optimole = $this->get_plugins_filtered_from_author( $args, $optimole_filter_slugs, 'Optimole' ); |
| 115 |
$featured = array_merge( $featured, $filtered_from_optimole ); |
| 116 |
|
| 117 |
$themeisle_filter_slugs = apply_filters( 'themeisle_sdk_themeisle_filter_slugs', [ 'otter-blocks' ] ); |
| 118 |
$filtered_from_themeisle = $this->get_plugins_filtered_from_author( $args, $themeisle_filter_slugs ); |
| 119 |
$featured = array_merge( $featured, $filtered_from_themeisle ); |
| 120 |
|
| 121 |
return $featured; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get plugins filtered from an author. |
| 126 |
* |
| 127 |
* @param object $args The arguments for the query. |
| 128 |
* @param array $filter_slugs The slugs to filter. |
| 129 |
* @param string $author The author to filter. |
| 130 |
* |
| 131 |
* @return array |
| 132 |
*/ |
| 133 |
private function get_plugins_filtered_from_author( $args, $filter_slugs = [], $author = 'Themeisle' ) { |
| 134 |
|
| 135 |
$cached = get_transient( $this->transient_key . $author ); |
| 136 |
if ( $cached ) { |
| 137 |
return $cached; |
| 138 |
} |
| 139 |
|
| 140 |
$new_args = [ |
| 141 |
'page' => 1, |
| 142 |
'per_page' => 36, |
| 143 |
'locale' => get_user_locale(), |
| 144 |
'author' => $author, |
| 145 |
'wp_version' => isset( $args->wp_version ) ? $args->wp_version : get_bloginfo( 'version' ), |
| 146 |
]; |
| 147 |
|
| 148 |
$api = plugins_api( 'query_plugins', $new_args ); |
| 149 |
if ( is_wp_error( $api ) ) { |
| 150 |
return []; |
| 151 |
} |
| 152 |
|
| 153 |
$filtered = array_filter( |
| 154 |
$api->plugins, |
| 155 |
function( $plugin ) use ( $filter_slugs ) { |
| 156 |
$array_plugin = (array) $plugin; |
| 157 |
return in_array( $array_plugin['slug'], $filter_slugs ); |
| 158 |
} |
| 159 |
); |
| 160 |
|
| 161 |
set_transient( $this->transient_key . $author, $filtered, 12 * HOUR_IN_SECONDS ); |
| 162 |
|
| 163 |
return $filtered; |
| 164 |
} |
| 165 |
} |
| 166 |
|