| 1 |
<?php |
| 2 |
/** |
| 3 |
* The abstract class for module definition. |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Loader |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 3.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace ThemeisleSDK\Common; |
| 13 |
|
| 14 |
use ThemeisleSDK\Product; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Abstract_Module. |
| 22 |
* |
| 23 |
* @package ThemeisleSDK\Common |
| 24 |
*/ |
| 25 |
abstract class Abstract_Module { |
| 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 |
'woocommerce-product-addon' => 'woocommerce-product-addon/woocommerce-product-addon.php', |
| 37 |
'visualizer' => 'visualizer/index.php', |
| 38 |
'wp-landing-kit' => 'wp-landing-kit/wp-landing-kit.php', |
| 39 |
'multiple-pages-generator-by-porthas' => 'multiple-pages-generator-by-porthas/porthas-multi-pages-generator.php', |
| 40 |
'sparks-for-woocommerce' => 'sparks-for-woocommerce/sparks-for-woocommerce.php', |
| 41 |
'templates-patterns-collection' => 'templates-patterns-collection/templates-patterns-collection.php', |
| 42 |
'wpcf7-redirect' => 'wpcf7-redirect/wpcf7-redirect.php', |
| 43 |
'wp-full-stripe-free' => 'wp-full-stripe-free/wp-full-stripe.php', |
| 44 |
]; |
| 45 |
|
| 46 |
/** |
| 47 |
* Product which use the module. |
| 48 |
* |
| 49 |
* @var Product $product Product object. |
| 50 |
*/ |
| 51 |
protected $product = null; |
| 52 |
|
| 53 |
/** |
| 54 |
* Can load the module for the selected product. |
| 55 |
* |
| 56 |
* @param Product $product Product data. |
| 57 |
* |
| 58 |
* @return bool Should load module? |
| 59 |
*/ |
| 60 |
abstract public function can_load( $product ); |
| 61 |
|
| 62 |
/** |
| 63 |
* Bootstrap the module. |
| 64 |
* |
| 65 |
* @param Product $product Product object. |
| 66 |
*/ |
| 67 |
abstract public function load( $product ); |
| 68 |
|
| 69 |
/** |
| 70 |
* Check if the product is from partner. |
| 71 |
* |
| 72 |
* @param Product $product Product data. |
| 73 |
* |
| 74 |
* @return bool Is product from partner. |
| 75 |
*/ |
| 76 |
public function is_from_partner( $product ) { |
| 77 |
foreach ( Module_Factory::$domains as $partner_domain ) { |
| 78 |
if ( strpos( $product->get_store_url(), $partner_domain ) !== false ) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return array_key_exists( $product->get_slug(), Module_Factory::$slugs ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Wrapper for wp_remote_get on VIP environments. |
| 88 |
* |
| 89 |
* @param string $url Url to check. |
| 90 |
* @param array $args Option params. |
| 91 |
* |
| 92 |
* @return array|\WP_Error |
| 93 |
*/ |
| 94 |
public function safe_get( $url, $args = array() ) { |
| 95 |
return function_exists( 'vip_safe_wp_remote_get' ) |
| 96 |
? vip_safe_wp_remote_get( $url ) |
| 97 |
: wp_remote_get( //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get, Already used. |
| 98 |
$url, |
| 99 |
$args |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get the SDK base url. |
| 105 |
* |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public function get_sdk_uri() { |
| 109 |
global $themeisle_sdk_max_path; |
| 110 |
|
| 111 |
/** |
| 112 |
* $themeisle_sdk_max_path can point to the theme when the theme version is higher. |
| 113 |
* hence we also need to check that the path does not point to the theme else this will break the URL. |
| 114 |
* References: https://github.com/Codeinwp/neve-pro-addon/issues/2403 |
| 115 |
*/ |
| 116 |
if ( ( $this->product->is_plugin() || $this->product->is_theme() ) && false === strpos( $themeisle_sdk_max_path, get_template_directory() ) ) { |
| 117 |
return plugins_url( '/', $themeisle_sdk_max_path . '/themeisle-sdk/' ); |
| 118 |
}; |
| 119 |
|
| 120 |
return get_template_directory_uri() . '/vendor/codeinwp/themeisle-sdk/'; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Call plugin api |
| 125 |
* |
| 126 |
* @param string $slug plugin slug. |
| 127 |
* |
| 128 |
* @return array|mixed|object |
| 129 |
*/ |
| 130 |
public function call_plugin_api( $slug ) { |
| 131 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 132 |
|
| 133 |
$call_api = get_transient( 'ti_plugin_info_' . $slug ); |
| 134 |
|
| 135 |
if ( false === $call_api ) { |
| 136 |
$call_api = plugins_api( |
| 137 |
'plugin_information', |
| 138 |
array( |
| 139 |
'slug' => $slug, |
| 140 |
'fields' => array( |
| 141 |
'downloaded' => false, |
| 142 |
'rating' => false, |
| 143 |
'description' => false, |
| 144 |
'short_description' => true, |
| 145 |
'donate_link' => false, |
| 146 |
'tags' => false, |
| 147 |
'sections' => true, |
| 148 |
'homepage' => true, |
| 149 |
'added' => false, |
| 150 |
'last_updated' => false, |
| 151 |
'compatibility' => false, |
| 152 |
'tested' => false, |
| 153 |
'requires' => false, |
| 154 |
'downloadlink' => false, |
| 155 |
'icons' => true, |
| 156 |
'banners' => true, |
| 157 |
), |
| 158 |
) |
| 159 |
); |
| 160 |
set_transient( 'ti_plugin_info_' . $slug, $call_api, 30 * MINUTE_IN_SECONDS ); |
| 161 |
} |
| 162 |
|
| 163 |
return $call_api; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get the plugin status. |
| 168 |
* |
| 169 |
* @param string $plugin Plugin slug. |
| 170 |
* |
| 171 |
* @return bool |
| 172 |
*/ |
| 173 |
public function is_plugin_installed( $plugin ) { |
| 174 |
if ( ! isset( $this->plugin_paths[ $plugin ] ) ) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
if ( file_exists( WP_CONTENT_DIR . '/plugins/' . $this->plugin_paths[ $plugin ] ) ) { |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get plugin activation link. |
| 187 |
* |
| 188 |
* @param string $slug The plugin slug. |
| 189 |
* |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
public function get_plugin_activation_link( $slug ) { |
| 193 |
$reference_key = $slug === 'otter-blocks' ? 'reference_key' : 'optimole_reference_key'; |
| 194 |
$plugin = isset( $this->plugin_paths[ $slug ] ) ? $this->plugin_paths[ $slug ] : $slug . '/' . $slug . '.php'; |
| 195 |
|
| 196 |
return add_query_arg( |
| 197 |
array( |
| 198 |
'plugin_status' => 'all', |
| 199 |
'paged' => '1', |
| 200 |
'action' => 'activate', |
| 201 |
$reference_key => $this->product->get_key(), |
| 202 |
'plugin' => rawurlencode( $plugin ), |
| 203 |
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin ), |
| 204 |
), |
| 205 |
admin_url( 'plugins.php' ) |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Checks if a plugin is active. |
| 211 |
* |
| 212 |
* @param string $plugin plugin slug. |
| 213 |
* |
| 214 |
* @return bool |
| 215 |
*/ |
| 216 |
public function is_plugin_active( $plugin ) { |
| 217 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 218 |
|
| 219 |
$plugin = isset( $this->plugin_paths[ $plugin ] ) ? $this->plugin_paths[ $plugin ] : $plugin . '/' . $plugin . '.php'; |
| 220 |
|
| 221 |
return is_plugin_active( $plugin ); |
| 222 |
} |
| 223 |
} |
| 224 |
|