Loader.php
245 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The main loader class for ThemeGrill SDK |
| 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; |
| 13 | |
| 14 | use ThemeGrillSDK\Common\ModuleFactory; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Singleton loader for ThemeGrill SDK. |
| 22 | */ |
| 23 | final class Loader { |
| 24 | /** |
| 25 | * Singleton instance. |
| 26 | * |
| 27 | * @var Loader instance The singleton instance |
| 28 | */ |
| 29 | private static $instance; |
| 30 | /** |
| 31 | * Current loader version. |
| 32 | * |
| 33 | * @var string $version The class version. |
| 34 | */ |
| 35 | private static $version = '1.0.4'; |
| 36 | /** |
| 37 | * Holds registered products. |
| 38 | * |
| 39 | * @var array<Product> The products which use the SDK. |
| 40 | */ |
| 41 | private static $products = []; |
| 42 | /** |
| 43 | * Holds available modules to load. |
| 44 | * |
| 45 | * @var array The modules which SDK will be using. |
| 46 | */ |
| 47 | private static $available_modules = [ |
| 48 | 'script_loader', |
| 49 | 'rollback', |
| 50 | 'uninstall_feedback', |
| 51 | 'logger', |
| 52 | 'notification', |
| 53 | 'announcements', |
| 54 | 'licenser', |
| 55 | 'updater', |
| 56 | ]; |
| 57 | /** |
| 58 | * Holds the labels for the modules. |
| 59 | * |
| 60 | * @var array The labels for the modules. |
| 61 | */ |
| 62 | public static $labels = [ |
| 63 | 'announcements' => [ |
| 64 | 'notice_link_label' => 'See the Offer', |
| 65 | 'max_savings' => 'Our biggest sale of the year: <strong>%s OFF everything!</strong> Don\'t miss this limited-time offer.', |
| 66 | 'black_friday' => 'Black Friday Sale', |
| 67 | 'time_left' => '%s left', |
| 68 | ], |
| 69 | 'uninstall' => [ |
| 70 | 'heading_plugin' => 'What\'s wrong?', |
| 71 | 'heading_theme' => 'What does not work for you in {theme}?', |
| 72 | 'submit' => 'Submit', |
| 73 | 'cta_info' => 'What info do we collect?', |
| 74 | 'button_submit' => 'Submit & Deactivate', |
| 75 | 'button_cancel' => 'Skip & Deactivate', |
| 76 | 'disclosure' => [ |
| 77 | 'title' => 'Below is a detailed view of all data that ThemeGrill will receive if you fill in this survey. No email address or IP addresses are transmitted after you submit the survey.', |
| 78 | 'version' => '%s %s version %s %s %s %s', |
| 79 | 'website' => '%sCurrent website:%s %s %s %s', |
| 80 | 'usage' => '%sUsage time:%s %s %s%s', |
| 81 | 'reason' => '%s Uninstall reason %s %s Selected reason from the above survey %s ', |
| 82 | ], |
| 83 | |
| 84 | 'options' => [ |
| 85 | 'id3' => [ |
| 86 | 'title' => 'I found a better plugin', |
| 87 | 'placeholder' => 'What\'s the plugin\'s name?', |
| 88 | ], |
| 89 | 'id4' => [ |
| 90 | 'title' => 'I could not get the plugin to work', |
| 91 | 'placeholder' => 'What problem are you experiencing?', |
| 92 | ], |
| 93 | 'id5' => [ |
| 94 | 'title' => 'I no longer need the plugin', |
| 95 | 'placeholder' => 'If you could improve one thing about our product, what would it be?', |
| 96 | ], |
| 97 | 'id6' => [ |
| 98 | 'title' => 'It\'s a temporary deactivation. I\'m just debugging an issue.', |
| 99 | 'placeholder' => 'What problem are you experiencing?', |
| 100 | ], |
| 101 | 'id7' => [ |
| 102 | 'title' => 'I don\'t know how to make it look like demo', |
| 103 | ], |
| 104 | 'id8' => [ |
| 105 | |
| 106 | 'placeholder' => 'What option is missing?', |
| 107 | 'title' => 'It lacks options', |
| 108 | ], |
| 109 | 'id9' => [ |
| 110 | 'title' => 'Is not working with a plugin that I need', |
| 111 | 'placeholder' => 'What is the name of the plugin', |
| 112 | ], |
| 113 | 'id10' => [ |
| 114 | 'title' => 'I want to try a new design, I don\'t like {theme} style', |
| 115 | ], |
| 116 | 'id999' => [ |
| 117 | 'title' => 'Other', |
| 118 | 'placeholder' => 'What can we do better?', |
| 119 | ], |
| 120 | ], |
| 121 | ], |
| 122 | 'rollback' => [ |
| 123 | 'cta' => 'Rollback to v%s', |
| 124 | ], |
| 125 | 'logger' => [ |
| 126 | 'notice' => 'Do you enjoy <b>{product}</b>? Become a contributor by opting in to our anonymous data tracking. We guarantee no sensitive data is collected.', |
| 127 | 'cta_y' => 'Sure, I would love to help.', |
| 128 | 'cta_n' => 'No, thanks.', |
| 129 | ], |
| 130 | ]; |
| 131 | |
| 132 | /** |
| 133 | * Initialize the sdk logic. |
| 134 | */ |
| 135 | public static function init() { |
| 136 | /** |
| 137 | * This filter can be used to localize the labels inside each product. |
| 138 | */ |
| 139 | self::$labels = apply_filters( 'themegrill_sdk_labels', self::$labels ); |
| 140 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Loader ) ) { |
| 141 | self::$instance = new Loader(); |
| 142 | $modules = array_merge( self::$available_modules, apply_filters( 'themegrill_sdk_modules', [] ) ); |
| 143 | foreach ( $modules as $key => $module ) { |
| 144 | if ( ! class_exists( 'ThemeGrillSDK\\Modules\\' . str_replace( ' ', '', ucwords( str_replace( '_', ' ', $module ) ) ) ) ) { |
| 145 | unset( $modules[ $key ] ); |
| 146 | } |
| 147 | } |
| 148 | self::$available_modules = $modules; |
| 149 | |
| 150 | add_action( 'themegrill_sdk_first_activation', array( __CLASS__, 'activate' ) ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get cache token used in API requests. |
| 156 | * |
| 157 | * @return string Cache token. |
| 158 | */ |
| 159 | public static function get_cache_token() { |
| 160 | $cache_token = get_transient( 'themegrill_sdk_cache_token' ); |
| 161 | if ( false === $cache_token ) { |
| 162 | $cache_token = wp_generate_password( 6, false ); |
| 163 | set_transient( 'themegrill_sdk_cache_token', $cache_token, WEEK_IN_SECONDS ); |
| 164 | } |
| 165 | |
| 166 | return $cache_token; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Clear cache token. |
| 171 | */ |
| 172 | public static function clear_cache_token() { |
| 173 | delete_transient( 'themegrill_sdk_cache_token' ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Register product into SDK. |
| 178 | * |
| 179 | * @param string $base_file The product base file. |
| 180 | * |
| 181 | * @return Loader The singleton object. |
| 182 | */ |
| 183 | public static function add_product( $base_file ) { |
| 184 | if ( ! is_file( $base_file ) ) { |
| 185 | return self::$instance; |
| 186 | } |
| 187 | $product = new Product( $base_file ); |
| 188 | |
| 189 | ModuleFactory::attach( $product, self::get_modules() ); |
| 190 | |
| 191 | self::$products[ $product->get_slug() ] = $product; |
| 192 | |
| 193 | return self::$instance; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Activate the product routine. |
| 198 | * |
| 199 | * @param string $file The base file of the product. |
| 200 | * |
| 201 | * @return void |
| 202 | */ |
| 203 | public static function activate( $file ) { |
| 204 | |
| 205 | $dirname = trailingslashit( dirname( ( $file ) ) ); |
| 206 | if ( ! file_exists( $dirname . '_reference.php' ) ) { |
| 207 | return; |
| 208 | } |
| 209 | $reference_data = require_once $dirname . '_reference.php'; |
| 210 | if ( ! is_array( $reference_data ) || |
| 211 | ! isset( $reference_data['key'] ) || |
| 212 | ! isset( $reference_data['value'] ) || |
| 213 | ! preg_match( '/^[a-zA-Z0-9_]+_reference_key$/', $reference_data['key'] ) ) { |
| 214 | return; |
| 215 | } |
| 216 | add_option( $reference_data['key'], sanitize_key( $reference_data['value'] ) ); |
| 217 | } |
| 218 | /** |
| 219 | * Get all registered modules by the SDK. |
| 220 | * |
| 221 | * @return array Modules available. |
| 222 | */ |
| 223 | public static function get_modules() { |
| 224 | return self::$available_modules; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Get all products using the SDK. |
| 229 | * |
| 230 | * @return array<Product> Products available. |
| 231 | */ |
| 232 | public static function get_products() { |
| 233 | return self::$products; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Get the version of the SDK. |
| 238 | * |
| 239 | * @return string The version. |
| 240 | */ |
| 241 | public static function get_version() { |
| 242 | return self::$version; |
| 243 | } |
| 244 | } |
| 245 |