PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 4.0.8
Visualizer – Tables & Charts Manager with Built-in AI Generator v4.0.8
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
← All changes | vendor/codeinwp/themeisle-sdk/src/Modules/Featured_plugins.php +84 -23 4.0.44.0.8 View file →
@@ -148,23 +148,30 @@
148 148 return $res;
149 149 }
150 150
151 151 if ( isset( $args->page ) && 1 === (int) $args->page && isset( $args->search ) && ! empty( $args->search ) ) {
152 - $res->plugins = $this->maybe_prepend_lms_plugin( $res->plugins, $args );
152 + $original_count = count( (array) $res->plugins );
153 + $res->plugins = $this->maybe_prepend_lms_plugin( $res->plugins, $args );
154 +
155 + return $this->adjust_results_count( $res, count( (array) $res->plugins ) - $original_count );
156 + }
157 +
158 + if ( ! isset( $args->browse ) || $args->browse !== 'featured' ) {
153 159 return $res;
154 160 }
155 161
156 - if ( ! isset( $args->browse ) || $args->browse !== 'featured' ) {
162 + // Inject only on the first page so the same plugins are not repeated on every page.
163 + if ( isset( $args->page ) && (int) $args->page > 1 ) {
157 164 return $res;
158 165 }
159 166
160 167 $featured = $this->query_plugins_by_author( $args );
161 168
162 - $plugins = array_merge( $featured, (array) $res->plugins );
163 - $plugins = array_slice( $plugins, 0, $res->info['results'] );
164 - $res->plugins = $plugins;
169 + $original_count = count( (array) $res->plugins );
170 + $plugins = $this->remove_plugins_by_slug( (array) $res->plugins, $this->get_plugin_slugs( $featured ) );
171 + $res->plugins = array_merge( $featured, $plugins );
165 172
166 - return $res;
173 + return $this->adjust_results_count( $res, count( $res->plugins ) - $original_count );
167 174 }
168 175
169 176 /**
170 177 * Prepend the LMS plugin if the search query matches LMS-related terms.
@@ -179,17 +186,10 @@
179 186 $filter_slugs = apply_filters( 'themeisle_sdk_masteriyo_filter_slugs', [ 'learning-management-system' ] );
180 187 $masteriyo = $this->get_plugins_filtered_from_author( $args, $filter_slugs, 'masteriyo' );
181 188
182 189 if ( ! empty( $masteriyo ) ) {
183 - // Remove existing LMS plugin if present to avoid duplicates.
184 - $plugins = array_filter(
185 - $plugins,
186 - function( $plugin ) {
187 - return ( is_object( $plugin ) && isset( $plugin->slug ) && $plugin->slug !== 'learning-management-system' ) ||
188 - ( is_array( $plugin ) && isset( $plugin['slug'] ) && $plugin['slug'] !== 'learning-management-system' );
189 - }
190 - );
191 -
190 + // Remove existing copies of the injected plugins to avoid duplicates.
191 + $plugins = $this->remove_plugins_by_slug( (array) $plugins, $this->get_plugin_slugs( $masteriyo ) );
192 192 $plugins = array_merge( $masteriyo, $plugins );
193 193 }
194 194 }
195 195 return $plugins;
@@ -195,8 +195,64 @@
195 195 return $plugins;
196 196 }
197 197
198 198 /**
199 + * Extract the slugs from a list of plugin entries.
200 + *
201 + * @param array $plugins The plugins list.
202 + *
203 + * @return array
204 + */
205 + private function get_plugin_slugs( $plugins ) {
206 + $slugs = [];
207 + foreach ( $plugins as $plugin ) {
208 + $plugin = (array) $plugin;
209 + if ( isset( $plugin['slug'] ) ) {
210 + $slugs[] = $plugin['slug'];
211 + }
212 + }
213 +
214 + return $slugs;
215 + }
216 +
217 + /**
218 + * Remove the plugins matching the provided slugs from the list.
219 + *
220 + * @param array $plugins The plugins list.
221 + * @param array $slugs The slugs to remove.
222 + *
223 + * @return array
224 + */
225 + private function remove_plugins_by_slug( $plugins, $slugs ) {
226 + return array_values(
227 + array_filter(
228 + $plugins,
229 + function( $plugin ) use ( $slugs ) {
230 + $plugin = (array) $plugin;
231 +
232 + return ! isset( $plugin['slug'] ) || ! in_array( $plugin['slug'], $slugs, true );
233 + }
234 + )
235 + );
236 + }
237 +
238 + /**
239 + * Adjust the reported total results count after injecting plugins.
240 + *
241 + * @param object $res The result object.
242 + * @param int $delta The net number of plugins added to the list.
243 + *
244 + * @return object
245 + */
246 + private function adjust_results_count( $res, $delta ) {
247 + if ( 0 !== $delta && isset( $res->info['results'] ) && is_numeric( $res->info['results'] ) ) {
248 + $res->info['results'] = max( 0, (int) $res->info['results'] + $delta );
249 + }
250 +
251 + return $res;
252 + }
253 +
254 + /**
199 255 * Check if a plugin search query matches LMS-related terms.
200 256 *
201 257 * @param string $search Search query.
202 258 *
@@ -257,10 +313,10 @@
257 313 */
258 314 protected function get_plugins_filtered_from_author( $args, $filter_slugs = [], $author = 'Themeisle' ) {
259 315
260 316 $cached = get_transient( $this->transient_key . $author );
261 - if ( $cached ) {
262 - return $cached;
317 + if ( false !== $cached ) {
318 + return (array) $cached;
263 319 }
264 320
265 321 $new_args = [
266 322 'page' => 1,
@@ -271,17 +327,22 @@
271 327 ];
272 328
273 329 $api = plugins_api( 'query_plugins', $new_args );
274 330 if ( is_wp_error( $api ) ) {
331 + // Cache the failure for a shorter period to avoid hammering the API on every request.
332 + set_transient( $this->transient_key . $author, [], HOUR_IN_SECONDS );
333 +
275 334 return [];
276 335 }
277 336
278 - $filtered = array_filter(
279 - $api->plugins,
280 - function( $plugin ) use ( $filter_slugs ) {
281 - $array_plugin = (array) $plugin;
282 - return in_array( $array_plugin['slug'], $filter_slugs );
283 - }
337 + $filtered = array_values(
338 + array_filter(
339 + $api->plugins,
340 + function( $plugin ) use ( $filter_slugs ) {
341 + $array_plugin = (array) $plugin;
342 + return in_array( $array_plugin['slug'], $filter_slugs );
343 + }
344 + )
284 345 );
285 346
286 347 set_transient( $this->transient_key . $author, $filtered, 12 * HOUR_IN_SECONDS );
287 348