← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Featured_plugins.php
+27
-115
4.0.8
→
3.11.14
View file →
| @@ -148,12 +148,10 @@ | ||
| 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 | - $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 ); | |
| 152 | + $res->plugins = $this->maybe_prepend_lms_plugin( $res->plugins, $args ); | |
| 153 | + return $res; | |
| 156 | 154 | } |
| 157 | 155 | |
| 158 | 156 | if ( ! isset( $args->browse ) || $args->browse !== 'featured' ) { |
| 159 | 157 | return $res; |
| @@ -158,20 +156,15 @@ | ||
| 158 | 156 | if ( ! isset( $args->browse ) || $args->browse !== 'featured' ) { |
| 159 | 157 | return $res; |
| 160 | 158 | } |
| 161 | 159 | |
| 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 ) { | |
| 164 | - return $res; | |
| 165 | - } | |
| 166 | - | |
| 167 | 160 | $featured = $this->query_plugins_by_author( $args ); |
| 168 | 161 | |
| 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 ); | |
| 162 | + $plugins = array_merge( $featured, (array) $res->plugins ); | |
| 163 | + $plugins = array_slice( $plugins, 0, $res->info['results'] ); | |
| 164 | + $res->plugins = $plugins; | |
| 172 | 165 | |
| 173 | - return $this->adjust_results_count( $res, count( $res->plugins ) - $original_count ); | |
| 166 | + return $res; | |
| 174 | 167 | } |
| 175 | 168 | |
| 176 | 169 | /** |
| 177 | 170 | * Prepend the LMS plugin if the search query matches LMS-related terms. |
| @@ -181,15 +174,25 @@ | ||
| 181 | 174 | * @return array |
| 182 | 175 | */ |
| 183 | 176 | private function maybe_prepend_lms_plugin( $plugins, $args ) { |
| 184 | 177 | $search = isset( $args->search ) ? strtolower( $args->search ) : ''; |
| 185 | - if ( $this->matches_lms_search_keywords( $search ) ) { | |
| 178 | + if ( | |
| 179 | + strpos( $search, 'lms' ) !== false || | |
| 180 | + strpos( $search, 'learn' ) !== false | |
| 181 | + ) { | |
| 186 | 182 | $filter_slugs = apply_filters( 'themeisle_sdk_masteriyo_filter_slugs', [ 'learning-management-system' ] ); |
| 187 | 183 | $masteriyo = $this->get_plugins_filtered_from_author( $args, $filter_slugs, 'masteriyo' ); |
| 188 | 184 | |
| 189 | 185 | if ( ! empty( $masteriyo ) ) { |
| 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 ) ); | |
| 186 | + // Remove existing LMS plugin if present to avoid duplicates. | |
| 187 | + $plugins = array_filter( | |
| 188 | + $plugins, | |
| 189 | + function( $plugin ) { | |
| 190 | + return ( is_object( $plugin ) && isset( $plugin->slug ) && $plugin->slug !== 'learning-management-system' ) || | |
| 191 | + ( is_array( $plugin ) && isset( $plugin['slug'] ) && $plugin['slug'] !== 'learning-management-system' ); | |
| 192 | + } | |
| 193 | + ); | |
| 194 | + | |
| 192 | 195 | $plugins = array_merge( $masteriyo, $plugins ); |
| 193 | 196 | } |
| 194 | 197 | } |
| 195 | 198 | return $plugins; |
| @@ -195,94 +198,8 @@ | ||
| 195 | 198 | return $plugins; |
| 196 | 199 | } |
| 197 | 200 | |
| 198 | 201 | /** |
| 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 | - /** | |
| 255 | - * Check if a plugin search query matches LMS-related terms. | |
| 256 | - * | |
| 257 | - * @param string $search Search query. | |
| 258 | - * | |
| 259 | - * @return bool True if the search query matches LMS-related terms. | |
| 260 | - */ | |
| 261 | - private function matches_lms_search_keywords( $search ) { | |
| 262 | - $lms_keywords = array( | |
| 263 | - 'lms', | |
| 264 | - 'learn', | |
| 265 | - 'course', | |
| 266 | - 'courses', | |
| 267 | - 'learning', | |
| 268 | - 'academy', | |
| 269 | - 'training', | |
| 270 | - 'student', | |
| 271 | - 'students', | |
| 272 | - 'quiz', | |
| 273 | - ); | |
| 274 | - | |
| 275 | - foreach ( $lms_keywords as $keyword ) { | |
| 276 | - if ( preg_match( '/(^|[^a-z0-9])' . preg_quote( $keyword, '/' ) . '([^a-z0-9]|$)/', $search ) === 1 ) { | |
| 277 | - return true; | |
| 278 | - } | |
| 279 | - } | |
| 280 | - | |
| 281 | - return false; | |
| 282 | - } | |
| 283 | - | |
| 284 | - /** | |
| 285 | 202 | * Query plugins by author. |
| 286 | 203 | * |
| 287 | 204 | * @param object $args The arguments for the query. |
| 288 | 205 | * |
| @@ -313,10 +230,10 @@ | ||
| 313 | 230 | */ |
| 314 | 231 | protected function get_plugins_filtered_from_author( $args, $filter_slugs = [], $author = 'Themeisle' ) { |
| 315 | 232 | |
| 316 | 233 | $cached = get_transient( $this->transient_key . $author ); |
| 317 | - if ( false !== $cached ) { | |
| 318 | - return (array) $cached; | |
| 234 | + if ( $cached ) { | |
| 235 | + return $cached; | |
| 319 | 236 | } |
| 320 | 237 | |
| 321 | 238 | $new_args = [ |
| 322 | 239 | 'page' => 1, |
| @@ -327,22 +244,17 @@ | ||
| 327 | 244 | ]; |
| 328 | 245 | |
| 329 | 246 | $api = plugins_api( 'query_plugins', $new_args ); |
| 330 | 247 | 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 | - | |
| 334 | 248 | return []; |
| 335 | 249 | } |
| 336 | 250 | |
| 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 | - ) | |
| 251 | + $filtered = array_filter( | |
| 252 | + $api->plugins, | |
| 253 | + function( $plugin ) use ( $filter_slugs ) { | |
| 254 | + $array_plugin = (array) $plugin; | |
| 255 | + return in_array( $array_plugin['slug'], $filter_slugs ); | |
| 256 | + } | |
| 345 | 257 | ); |
| 346 | 258 | |
| 347 | 259 | set_transient( $this->transient_key . $author, $filtered, 12 * HOUR_IN_SECONDS ); |
| 348 | 260 | |