PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.11.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.11.3
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
visualizer / vendor / codeinwp / themeisle-sdk / src / Modules / Featured_plugins.php

Featured_plugins.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.11.3, at vendor/codeinwp/themeisle-sdk/src/Modules/Featured_plugins.php

168 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $slug = $product->get_slug();
48 // only load for products that contain "pro" in the slug.
49 if ( strpos( $slug, 'pro' ) === false ) {
50 return false;
51 }
52
53 return ! apply_filters( 'themeisle_sdk_disable_featured_plugins', false );
54 }
55
56 /**
57 * Load the module for the selected product.
58 *
59 * @param Product $product Product data.
60 *
61 * @return void
62 */
63 public function load( $product ) {
64 if ( ! current_user_can( 'install_plugins' ) ) {
65 return;
66 }
67
68 // bail if we already registered a filter for the plugin API.
69 if ( apply_filters( 'themeisle_sdk_plugin_api_filter_registered', false ) ) {
70 return;
71 }
72 add_filter( 'themeisle_sdk_plugin_api_filter_registered', '__return_true' );
73
74 add_filter( 'plugins_api_result', [ $this, 'filter_plugin_api_results' ], 10, 3 );
75 }
76
77 /**
78 * Filter the plugin API results to include the featured plugins.
79 *
80 * @param object $res The result object.
81 * @param string $action The type of information being requested from the Plugin Install API.
82 * @param object $args Plugin API arguments.
83 *
84 * @return object
85 */
86 public function filter_plugin_api_results( $res, $action, $args ) {
87
88 if ( 'query_plugins' !== $action ) {
89 return $res;
90 }
91
92 if ( ! isset( $args->browse ) || $args->browse !== 'featured' ) {
93 return $res;
94 }
95
96 $featured = $this->query_plugins_by_author( $args );
97
98 $plugins = array_merge( $featured, (array) $res->plugins );
99 $plugins = array_slice( $plugins, 0, $res->info['results'] );
100 $res->plugins = $plugins;
101
102 return $res;
103 }
104
105 /**
106 * Query plugins by author.
107 *
108 * @param object $args The arguments for the query.
109 *
110 * @return array
111 */
112 private function query_plugins_by_author( $args ) {
113 $featured = [];
114
115 $optimole_filter_slugs = apply_filters( 'themeisle_sdk_optimole_filter_slugs', [ 'optimole-wp' ] );
116 $filtered_from_optimole = $this->get_plugins_filtered_from_author( $args, $optimole_filter_slugs, 'Optimole' );
117 $featured = array_merge( $featured, $filtered_from_optimole );
118
119 $themeisle_filter_slugs = apply_filters( 'themeisle_sdk_themeisle_filter_slugs', [ 'otter-blocks' ] );
120 $filtered_from_themeisle = $this->get_plugins_filtered_from_author( $args, $themeisle_filter_slugs );
121 $featured = array_merge( $featured, $filtered_from_themeisle );
122
123 return $featured;
124 }
125
126 /**
127 * Get plugins filtered from an author.
128 *
129 * @param object $args The arguments for the query.
130 * @param array $filter_slugs The slugs to filter.
131 * @param string $author The author to filter.
132 *
133 * @return array
134 */
135 private function get_plugins_filtered_from_author( $args, $filter_slugs = [], $author = 'Themeisle' ) {
136
137 $cached = get_transient( $this->transient_key . $author );
138 if ( $cached ) {
139 return $cached;
140 }
141
142 $new_args = [
143 'page' => 1,
144 'per_page' => 36,
145 'locale' => get_user_locale(),
146 'author' => $author,
147 'wp_version' => isset( $args->wp_version ) ? $args->wp_version : get_bloginfo( 'version' ),
148 ];
149
150 $api = plugins_api( 'query_plugins', $new_args );
151 if ( is_wp_error( $api ) ) {
152 return [];
153 }
154
155 $filtered = array_filter(
156 $api->plugins,
157 function( $plugin ) use ( $filter_slugs ) {
158 $array_plugin = (array) $plugin;
159 return in_array( $array_plugin['slug'], $filter_slugs );
160 }
161 );
162
163 set_transient( $this->transient_key . $author, $filtered, 12 * HOUR_IN_SECONDS );
164
165 return $filtered;
166 }
167 }
168