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