← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Featured_plugins.php
+102
-6
3.10.12
→
3.11.14
View file →
| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | */ |
| 13 | 13 | namespace ThemeisleSDK\Modules; |
| 14 | 14 | |
| 15 | 15 | use ThemeisleSDK\Common\Abstract_Module; |
| 16 | +use ThemeisleSDK\Loader; | |
| 16 | 17 | use ThemeisleSDK\Product; |
| 17 | 18 | |
| 18 | 19 | // Exit if accessed directly. |
| 19 | 20 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -32,8 +33,15 @@ | ||
| 32 | 33 | */ |
| 33 | 34 | private $transient_key = 'themeisle_sdk_featured_plugins_'; |
| 34 | 35 | |
| 35 | 36 | /** |
| 37 | + * The current product instance. | |
| 38 | + * | |
| 39 | + * @var Product|null | |
| 40 | + */ | |
| 41 | + protected $product = null; | |
| 42 | + | |
| 43 | + /** | |
| 36 | 44 | * Check if the module can be loaded. |
| 37 | 45 | * |
| 38 | 46 | * @param Product $product Product data. |
| 39 | 47 | * |
| @@ -43,11 +51,9 @@ | ||
| 43 | 51 | if ( $this->is_from_partner( $product ) ) { |
| 44 | 52 | return false; |
| 45 | 53 | } |
| 46 | 54 | |
| 47 | - $slug = $product->get_slug(); | |
| 48 | - // only load for products that contain "pro" in the slug. | |
| 49 | - if ( strpos( $slug, 'pro' ) === false ) { | |
| 55 | + if ( $product->is_wordpress_available() ) { | |
| 50 | 56 | return false; |
| 51 | 57 | } |
| 52 | 58 | |
| 53 | 59 | return ! apply_filters( 'themeisle_sdk_disable_featured_plugins', false ); |
| @@ -60,8 +66,10 @@ | ||
| 60 | 66 | * |
| 61 | 67 | * @return void |
| 62 | 68 | */ |
| 63 | 69 | public function load( $product ) { |
| 70 | + $this->product = $product; | |
| 71 | + | |
| 64 | 72 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 65 | 73 | return; |
| 66 | 74 | } |
| 67 | 75 | |
| @@ -70,12 +78,63 @@ | ||
| 70 | 78 | return; |
| 71 | 79 | } |
| 72 | 80 | add_filter( 'themeisle_sdk_plugin_api_filter_registered', '__return_true' ); |
| 73 | 81 | |
| 74 | - add_filter( 'plugins_api_result', [ $this, 'filter_plugin_api_results' ], 10, 3 ); | |
| 82 | + add_filter( 'plugins_api_result', [ $this, 'filter_plugin_api_results' ], 11, 3 ); | |
| 83 | + | |
| 84 | + // Enqueue inline JS only on plugin-install.php. | |
| 85 | + add_action( 'admin_enqueue_scripts', [ $this, 'maybe_add_inline_js' ] ); | |
| 75 | 86 | } |
| 76 | 87 | |
| 77 | 88 | /** |
| 89 | + * Enqueue inline JavaScript only on plugin-install.php. | |
| 90 | + * | |
| 91 | + * @return void | |
| 92 | + */ | |
| 93 | + public function maybe_add_inline_js() { | |
| 94 | + $screen = get_current_screen(); | |
| 95 | + if ( isset( $screen->base ) && 'plugin-install' === $screen->base ) { | |
| 96 | + add_action( | |
| 97 | + 'admin_footer', | |
| 98 | + function() { | |
| 99 | + $text = esc_html( sprintf( Loader::$labels['promotions']['recommended'], $this->product->get_friendly_name() ) ); | |
| 100 | + | |
| 101 | + echo '<script>(function(){ | |
| 102 | + function onPluginCardFound(card) { | |
| 103 | + var recommendedDiv = document.createElement("div"); | |
| 104 | + Object.assign(recommendedDiv.style, { | |
| 105 | + display: "block", | |
| 106 | + textAlign: "center", | |
| 107 | + padding: "0 12px 12px", | |
| 108 | + background: "#f6f7f7" | |
| 109 | + }); | |
| 110 | + recommendedDiv.innerHTML = "' . esc_html( $text ) . '"; | |
| 111 | + card.appendChild(recommendedDiv); | |
| 112 | + } | |
| 113 | + | |
| 114 | + function checkAndRun() { | |
| 115 | + var card = document.querySelector(".plugin-card-learning-management-system"); | |
| 116 | + if (card && !card.dataset.recommendedAdded) { | |
| 117 | + onPluginCardFound(card); | |
| 118 | + card.dataset.recommendedAdded = "true"; | |
| 119 | + } | |
| 120 | + } | |
| 121 | + | |
| 122 | + var observer = new MutationObserver(function(mutations) { | |
| 123 | + checkAndRun(); | |
| 124 | + }); | |
| 125 | + | |
| 126 | + observer.observe(document.body, { childList: true, subtree: true }); | |
| 127 | + | |
| 128 | + // Initial check in case the card is already present. | |
| 129 | + checkAndRun(); | |
| 130 | + })();</script>'; | |
| 131 | + } | |
| 132 | + ); | |
| 133 | + } | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 78 | 137 | * Filter the plugin API results to include the featured plugins. |
| 79 | 138 | * |
| 80 | 139 | * @param object $res The result object. |
| 81 | 140 | * @param string $action The type of information being requested from the Plugin Install API. |
| @@ -88,8 +147,13 @@ | ||
| 88 | 147 | if ( 'query_plugins' !== $action ) { |
| 89 | 148 | return $res; |
| 90 | 149 | } |
| 91 | 150 | |
| 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 ); | |
| 153 | + return $res; | |
| 154 | + } | |
| 155 | + | |
| 92 | 156 | if ( ! isset( $args->browse ) || $args->browse !== 'featured' ) { |
| 93 | 157 | return $res; |
| 94 | 158 | } |
| 95 | 159 | |
| @@ -102,8 +166,40 @@ | ||
| 102 | 166 | return $res; |
| 103 | 167 | } |
| 104 | 168 | |
| 105 | 169 | /** |
| 170 | + * Prepend the LMS plugin if the search query matches LMS-related terms. | |
| 171 | + * | |
| 172 | + * @param array $plugins The plugins array. | |
| 173 | + * @param object $args The plugin API arguments. | |
| 174 | + * @return array | |
| 175 | + */ | |
| 176 | + private function maybe_prepend_lms_plugin( $plugins, $args ) { | |
| 177 | + $search = isset( $args->search ) ? strtolower( $args->search ) : ''; | |
| 178 | + if ( | |
| 179 | + strpos( $search, 'lms' ) !== false || | |
| 180 | + strpos( $search, 'learn' ) !== false | |
| 181 | + ) { | |
| 182 | + $filter_slugs = apply_filters( 'themeisle_sdk_masteriyo_filter_slugs', [ 'learning-management-system' ] ); | |
| 183 | + $masteriyo = $this->get_plugins_filtered_from_author( $args, $filter_slugs, 'masteriyo' ); | |
| 184 | + | |
| 185 | + if ( ! empty( $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 | + | |
| 195 | + $plugins = array_merge( $masteriyo, $plugins ); | |
| 196 | + } | |
| 197 | + } | |
| 198 | + return $plugins; | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 106 | 202 | * Query plugins by author. |
| 107 | 203 | * |
| 108 | 204 | * @param object $args The arguments for the query. |
| 109 | 205 | * |
| @@ -115,9 +211,9 @@ | ||
| 115 | 211 | $optimole_filter_slugs = apply_filters( 'themeisle_sdk_optimole_filter_slugs', [ 'optimole-wp' ] ); |
| 116 | 212 | $filtered_from_optimole = $this->get_plugins_filtered_from_author( $args, $optimole_filter_slugs, 'Optimole' ); |
| 117 | 213 | $featured = array_merge( $featured, $filtered_from_optimole ); |
| 118 | 214 | |
| 119 | - $themeisle_filter_slugs = apply_filters( 'themeisle_sdk_themeisle_filter_slugs', [ 'otter-blocks' ] ); | |
| 215 | + $themeisle_filter_slugs = apply_filters( 'themeisle_sdk_themeisle_filter_slugs', [ 'otter-blocks', 'wp-cloudflare-page-cache' ] ); | |
| 120 | 216 | $filtered_from_themeisle = $this->get_plugins_filtered_from_author( $args, $themeisle_filter_slugs ); |
| 121 | 217 | $featured = array_merge( $featured, $filtered_from_themeisle ); |
| 122 | 218 | |
| 123 | 219 | return $featured; |
| @@ -131,9 +227,9 @@ | ||
| 131 | 227 | * @param string $author The author to filter. |
| 132 | 228 | * |
| 133 | 229 | * @return array |
| 134 | 230 | */ |
| 135 | - private function get_plugins_filtered_from_author( $args, $filter_slugs = [], $author = 'Themeisle' ) { | |
| 231 | + protected function get_plugins_filtered_from_author( $args, $filter_slugs = [], $author = 'Themeisle' ) { | |
| 136 | 232 | |
| 137 | 233 | $cached = get_transient( $this->transient_key . $author ); |
| 138 | 234 | if ( $cached ) { |
| 139 | 235 | return $cached; |