← All changes
|
vendor/codeinwp/themeisle-sdk/src/Common/Abstract_module.php
+224
-3
3.1.0
→
trunk
View file →
| @@ -23,8 +23,40 @@ | ||
| 23 | 23 | * @package ThemeisleSDK\Common |
| 24 | 24 | */ |
| 25 | 25 | abstract class Abstract_Module { |
| 26 | 26 | /** |
| 27 | + * Plugin paths. | |
| 28 | + * | |
| 29 | + * @var string[] $plugin_paths Plugin paths. | |
| 30 | + */ | |
| 31 | + private $plugin_paths = [ | |
| 32 | + 'otter-blocks' => 'otter-blocks/otter-blocks.php', | |
| 33 | + 'optimole-wp' => 'optimole-wp/optimole-wp.php', | |
| 34 | + 'tweet-old-post' => 'tweet-old-post/tweet-old-post.php', | |
| 35 | + 'feedzy-rss-feeds' => 'feedzy-rss-feeds/feedzy-rss-feed.php', | |
| 36 | + 'insert-php' => 'insert-php/insert_php.php', | |
| 37 | + 'woocommerce-product-addon' => 'woocommerce-product-addon/woocommerce-product-addon.php', | |
| 38 | + 'visualizer' => 'visualizer/index.php', | |
| 39 | + 'wp-landing-kit' => 'wp-landing-kit/wp-landing-kit.php', | |
| 40 | + 'multiple-pages-generator-by-porthas' => 'multiple-pages-generator-by-porthas/porthas-multi-pages-generator.php', | |
| 41 | + 'sparks-for-woocommerce' => 'sparks-for-woocommerce/sparks-for-woocommerce.php', | |
| 42 | + 'templates-patterns-collection' => 'templates-patterns-collection/templates-patterns-collection.php', | |
| 43 | + 'wpcf7-redirect' => 'wpcf7-redirect/wpcf7-redirect.php', | |
| 44 | + 'anti-spam' => 'anti-spam/anti-spam.php', | |
| 45 | + 'wp-full-stripe-free' => 'wp-full-stripe-free/wp-full-stripe.php', | |
| 46 | + 'learning-management-system' => 'learning-management-system/lms.php', | |
| 47 | + 'wp-cloudflare-page-cache' => 'wp-cloudflare-page-cache/wp-cloudflare-super-page-cache.php', | |
| 48 | + 'easy-mcp-ai' => 'easy-mcp-ai/easy-mcp-ai.php', | |
| 49 | + ]; | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Runtime-resolved plugin paths. | |
| 53 | + * | |
| 54 | + * @var string[] $resolved_plugin_paths Plugin main file path by slug. | |
| 55 | + */ | |
| 56 | + private $resolved_plugin_paths = []; | |
| 57 | + | |
| 58 | + /** | |
| 27 | 59 | * Product which use the module. |
| 28 | 60 | * |
| 29 | 61 | * @var Product $product Product object. |
| 30 | 62 | */ |
| @@ -36,9 +68,9 @@ | ||
| 36 | 68 | * @param Product $product Product data. |
| 37 | 69 | * |
| 38 | 70 | * @return bool Should load module? |
| 39 | 71 | */ |
| 40 | - public abstract function can_load( $product ); | |
| 72 | + abstract public function can_load( $product ); | |
| 41 | 73 | |
| 42 | 74 | /** |
| 43 | 75 | * Bootstrap the module. |
| 44 | 76 | * |
| @@ -43,9 +75,9 @@ | ||
| 43 | 75 | * Bootstrap the module. |
| 44 | 76 | * |
| 45 | 77 | * @param Product $product Product object. |
| 46 | 78 | */ |
| 47 | - public abstract function load( $product ); | |
| 79 | + abstract public function load( $product ); | |
| 48 | 80 | |
| 49 | 81 | /** |
| 50 | 82 | * Check if the product is from partner. |
| 51 | 83 | * |
| @@ -53,9 +85,8 @@ | ||
| 53 | 85 | * |
| 54 | 86 | * @return bool Is product from partner. |
| 55 | 87 | */ |
| 56 | 88 | public function is_from_partner( $product ) { |
| 57 | - | |
| 58 | 89 | foreach ( Module_Factory::$domains as $partner_domain ) { |
| 59 | 90 | if ( strpos( $product->get_store_url(), $partner_domain ) !== false ) { |
| 60 | 91 | return true; |
| 61 | 92 | } |
| @@ -61,6 +92,196 @@ | ||
| 61 | 92 | } |
| 62 | 93 | } |
| 63 | 94 | |
| 64 | 95 | return array_key_exists( $product->get_slug(), Module_Factory::$slugs ); |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Wrapper for wp_remote_get on VIP environments. | |
| 100 | + * | |
| 101 | + * @param string $url Url to check. | |
| 102 | + * @param array $args Option params. | |
| 103 | + * | |
| 104 | + * @return array|\WP_Error | |
| 105 | + */ | |
| 106 | + public function safe_get( $url, $args = array() ) { | |
| 107 | + return function_exists( 'vip_safe_wp_remote_get' ) | |
| 108 | + ? vip_safe_wp_remote_get( $url ) | |
| 109 | + : wp_remote_get( //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get, Already used. | |
| 110 | + $url, | |
| 111 | + $args | |
| 112 | + ); | |
| 113 | + } | |
| 114 | + | |
| 115 | + /** | |
| 116 | + * Get the SDK base url. | |
| 117 | + * | |
| 118 | + * @return string | |
| 119 | + */ | |
| 120 | + public function get_sdk_uri() { | |
| 121 | + global $themeisle_sdk_max_path; | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * $themeisle_sdk_max_path can point to the theme when the theme version is higher. | |
| 125 | + * hence we also need to check that the path does not point to the theme else this will break the URL. | |
| 126 | + * References: https://github.com/Codeinwp/neve-pro-addon/issues/2403 | |
| 127 | + */ | |
| 128 | + if ( ( $this->product->is_plugin() || $this->product->is_theme() ) && false === strpos( $themeisle_sdk_max_path, get_template_directory() ) ) { | |
| 129 | + return plugins_url( '/', $themeisle_sdk_max_path . '/themeisle-sdk/' ); | |
| 130 | + }; | |
| 131 | + | |
| 132 | + return get_template_directory_uri() . '/vendor/codeinwp/themeisle-sdk/'; | |
| 133 | + } | |
| 134 | + | |
| 135 | + /** | |
| 136 | + * Call plugin api | |
| 137 | + * | |
| 138 | + * @param string $slug plugin slug. | |
| 139 | + * | |
| 140 | + * @return array|mixed|object | |
| 141 | + */ | |
| 142 | + public function call_plugin_api( $slug ) { | |
| 143 | + include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | |
| 144 | + | |
| 145 | + $call_api = get_transient( 'ti_plugin_info_' . $slug ); | |
| 146 | + | |
| 147 | + if ( false === $call_api ) { | |
| 148 | + $call_api = plugins_api( | |
| 149 | + 'plugin_information', | |
| 150 | + array( | |
| 151 | + 'slug' => $slug, | |
| 152 | + 'fields' => array( | |
| 153 | + 'downloaded' => false, | |
| 154 | + 'rating' => true, | |
| 155 | + 'ratings' => true, | |
| 156 | + 'description' => false, | |
| 157 | + 'short_description' => true, | |
| 158 | + 'donate_link' => false, | |
| 159 | + 'tags' => false, | |
| 160 | + 'sections' => true, | |
| 161 | + 'homepage' => true, | |
| 162 | + 'added' => false, | |
| 163 | + 'last_updated' => false, | |
| 164 | + 'compatibility' => false, | |
| 165 | + 'tested' => false, | |
| 166 | + 'requires' => false, | |
| 167 | + 'downloadlink' => false, | |
| 168 | + 'icons' => true, | |
| 169 | + 'banners' => true, | |
| 170 | + ), | |
| 171 | + ) | |
| 172 | + ); | |
| 173 | + set_transient( 'ti_plugin_info_' . $slug, $call_api, 30 * MINUTE_IN_SECONDS ); | |
| 174 | + } | |
| 175 | + | |
| 176 | + return $call_api; | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * Get the plugin status. | |
| 181 | + * | |
| 182 | + * @param string $plugin Plugin slug. | |
| 183 | + * | |
| 184 | + * @return bool | |
| 185 | + */ | |
| 186 | + public function is_plugin_installed( $plugin ) { | |
| 187 | + $plugin_file = $this->resolve_plugin_main_file( $plugin ); | |
| 188 | + | |
| 189 | + if ( file_exists( WP_CONTENT_DIR . '/plugins/' . $plugin_file ) ) { | |
| 190 | + return true; | |
| 191 | + } | |
| 192 | + | |
| 193 | + return false; | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Get plugin activation link. | |
| 198 | + * | |
| 199 | + * @param string $slug The plugin slug. | |
| 200 | + * | |
| 201 | + * @return string | |
| 202 | + */ | |
| 203 | + public function get_plugin_activation_link( $slug ) { | |
| 204 | + $reference_key = $slug === 'otter-blocks' ? 'reference_key' : 'optimole_reference_key'; | |
| 205 | + $plugin = $this->resolve_plugin_main_file( $slug ); | |
| 206 | + | |
| 207 | + return add_query_arg( | |
| 208 | + array( | |
| 209 | + 'plugin_status' => 'all', | |
| 210 | + 'paged' => '1', | |
| 211 | + 'action' => 'activate', | |
| 212 | + $reference_key => $this->product->get_key(), | |
| 213 | + 'plugin' => rawurlencode( $plugin ), | |
| 214 | + '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin ), | |
| 215 | + ), | |
| 216 | + admin_url( 'plugins.php' ) | |
| 217 | + ); | |
| 218 | + } | |
| 219 | + | |
| 220 | + /** | |
| 221 | + * Checks if a plugin is active. | |
| 222 | + * | |
| 223 | + * @param string $plugin plugin slug. | |
| 224 | + * | |
| 225 | + * @return bool | |
| 226 | + */ | |
| 227 | + public function is_plugin_active( $plugin ) { | |
| 228 | + include_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 229 | + | |
| 230 | + $plugin = $this->resolve_plugin_main_file( $plugin ); | |
| 231 | + | |
| 232 | + return is_plugin_active( $plugin ); | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 236 | + * Resolve a plugin's main file path by slug. | |
| 237 | + * | |
| 238 | + * @param string $slug Plugin slug. | |
| 239 | + * | |
| 240 | + * @return string | |
| 241 | + */ | |
| 242 | + private function resolve_plugin_main_file( $slug ) { | |
| 243 | + if ( isset( $this->resolved_plugin_paths[ $slug ] ) ) { | |
| 244 | + return $this->resolved_plugin_paths[ $slug ]; | |
| 245 | + } | |
| 246 | + | |
| 247 | + if ( isset( $this->plugin_paths[ $slug ] ) ) { | |
| 248 | + $this->resolved_plugin_paths[ $slug ] = $this->plugin_paths[ $slug ]; | |
| 249 | + return $this->resolved_plugin_paths[ $slug ]; | |
| 250 | + } | |
| 251 | + | |
| 252 | + $default_plugin_file = $slug . '/' . $slug . '.php'; | |
| 253 | + | |
| 254 | + if ( file_exists( WP_CONTENT_DIR . '/plugins/' . $default_plugin_file ) ) { | |
| 255 | + $this->resolved_plugin_paths[ $slug ] = $default_plugin_file; | |
| 256 | + return $this->resolved_plugin_paths[ $slug ]; | |
| 257 | + } | |
| 258 | + | |
| 259 | + include_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 260 | + | |
| 261 | + if ( function_exists( 'get_plugins' ) ) { | |
| 262 | + $plugins = get_plugins( '/' . $slug ); | |
| 263 | + | |
| 264 | + if ( ! empty( $plugins ) ) { | |
| 265 | + $plugin_files = array_keys( $plugins ); | |
| 266 | + $plugin_file = $slug . '/' . reset( $plugin_files ); | |
| 267 | + | |
| 268 | + $this->resolved_plugin_paths[ $slug ] = $plugin_file; | |
| 269 | + | |
| 270 | + return $this->resolved_plugin_paths[ $slug ]; | |
| 271 | + } | |
| 272 | + } | |
| 273 | + | |
| 274 | + $this->resolved_plugin_paths[ $slug ] = $default_plugin_file; | |
| 275 | + | |
| 276 | + return $this->resolved_plugin_paths[ $slug ]; | |
| 277 | + } | |
| 278 | + | |
| 279 | + /** | |
| 280 | + * Get the current date. | |
| 281 | + * | |
| 282 | + * @return \DateTime The date time. | |
| 283 | + */ | |
| 284 | + public function get_current_date() { | |
| 285 | + return apply_filters( 'themeisle_sdk_current_date', new \DateTime( 'now' ) ); | |
| 65 | 286 | } |
| 66 | 287 | } |