Loader.php
565 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The main loader class for ThemeIsle SDK |
| 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 1.0.0 |
| 10 | */ |
| 11 | |
| 12 | namespace ThemeisleSDK; |
| 13 | |
| 14 | use ThemeisleSDK\Common\Module_Factory; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; |
| 18 | } |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * Singleton loader for ThemeIsle SDK. |
| 23 | */ |
| 24 | final class Loader { |
| 25 | /** |
| 26 | * Singleton instance. |
| 27 | * |
| 28 | * @var Loader instance The singleton instance |
| 29 | */ |
| 30 | private static $instance; |
| 31 | /** |
| 32 | * Current loader version. |
| 33 | * |
| 34 | * @var string $version The class version. |
| 35 | */ |
| 36 | private static $version = '2.0.0'; |
| 37 | /** |
| 38 | * Holds registered products. |
| 39 | * |
| 40 | * @var array<Product> The products which use the SDK. |
| 41 | */ |
| 42 | private static $products = []; |
| 43 | /** |
| 44 | * Holds available modules to load. |
| 45 | * |
| 46 | * @var array The modules which SDK will be using. |
| 47 | */ |
| 48 | private static $available_modules = [ |
| 49 | 'script_loader', |
| 50 | 'dashboard_widget', |
| 51 | 'rollback', |
| 52 | 'uninstall_feedback', |
| 53 | 'licenser', |
| 54 | 'logger', |
| 55 | 'translate', |
| 56 | 'translations', |
| 57 | 'review', |
| 58 | 'recommendation', |
| 59 | 'notification', |
| 60 | 'promotions', |
| 61 | 'welcome', |
| 62 | 'compatibilities', |
| 63 | 'about_us', |
| 64 | 'announcements', |
| 65 | 'featured_plugins', |
| 66 | 'float_widget', |
| 67 | 'migrator', |
| 68 | ]; |
| 69 | /** |
| 70 | * Holds the labels for the modules. |
| 71 | * |
| 72 | * @var array The labels for the modules. |
| 73 | */ |
| 74 | public static $labels = [ |
| 75 | 'announcements' => [ |
| 76 | 'notice_link_label' => 'See the deals', |
| 77 | 'max_savings' => 'Best WordPress Black Friday deals of %s — themes, plugins, hosting. Curated by the Themeisle team.', |
| 78 | 'black_friday' => 'Black Friday Sale', |
| 79 | 'time_left' => '%s left', |
| 80 | 'plugin_meta_message' => 'Black Friday Sale - 60% OFF', |
| 81 | ], |
| 82 | 'compatibilities' => [ |
| 83 | 'notice' => '%s requires a newer version of %s. Please %supdate%s %s %s to the latest version.', |
| 84 | 'notice2' => '%s update requires a newer version of %s. Please %supdate%s %s %s.', |
| 85 | 'notice_theme' => '%1$sWarning:%2$s This theme has not been tested with your current version of %1$s%3$s%2$s. Please update %3$s plugin.', |
| 86 | 'notice_plugin' => '%1$sWarning:%2$s This plugin has not been tested with your current version of %1$s%3$s%2$s. Please update %3$s %4$s.', |
| 87 | 'theme' => 'theme', |
| 88 | 'plugin' => 'plugin', |
| 89 | ], |
| 90 | 'dashboard_widget' => [ |
| 91 | 'title' => 'WordPress Guides/Tutorials', |
| 92 | 'popular' => 'Popular %s', |
| 93 | 'install' => 'Install', |
| 94 | 'powered' => 'Powered by %s', |
| 95 | ], |
| 96 | 'licenser' => [ |
| 97 | 'activate' => 'Activate', |
| 98 | 'invalid_msg' => 'Invalid license.', |
| 99 | 'error_notice' => 'ERROR: Failed to connect to the license service. Please try again later. Reason: %s', |
| 100 | 'error_notice2' => 'ERROR: Failed to validate license. Please try again in one minute.', |
| 101 | 'error_invalid' => 'ERROR: Invalid license provided.', |
| 102 | 'update_license' => 'Updating this theme will lose any customizations you have made. Cancel to stop, OK to update.', |
| 103 | 'invalid_msg' => 'Invalid license.', |
| 104 | 'already_active' => 'License is already active.', |
| 105 | 'notice_update' => '%1$s is available. %2$sCheck out what\'s%3$s new or %4$supdate now%3$s.', |
| 106 | 'not_active' => 'License not active.', |
| 107 | 'deactivate' => 'Deactivate', |
| 108 | 'renew_cta' => 'Renew license to update', |
| 109 | 'autoactivate_notice' => '%s has been successfully activated using %s license !', |
| 110 | 'valid' => 'Valid', |
| 111 | 'invalid' => 'Invalid', |
| 112 | 'notice' => 'Enter your license from %s purchase history in order to get %s updates', |
| 113 | 'expired' => '%s license expired', |
| 114 | 'expired_date' => 'Expired on %s', |
| 115 | 'expired_notice' => 'Your current setup continues working, but premium features are disabled and you\'re no longer receive updates - including critical patches - or support.', |
| 116 | |
| 117 | 'inactive' => 'In order to benefit from updates and support for %s, please add your license code from your %spurchase history%s and validate it %shere%s.', |
| 118 | 'no_activations' => 'No more activations left for %s. You need to upgrade your plan in order to use %s on more websites. If you need assistance, please get in touch with %s staff.', |
| 119 | 'renew_license' => 'Renew License', |
| 120 | 'learn_more' => 'Learn More', |
| 121 | ], |
| 122 | 'promotions' => [ |
| 123 | 'recommended' => 'Recommended by %s', |
| 124 | 'installActivate' => 'Install & Activate', |
| 125 | 'preview' => 'Preview', |
| 126 | 'installing' => 'Installing', |
| 127 | 'activating' => 'Activating', |
| 128 | 'connecting' => 'Connecting to API', |
| 129 | 'learnmore' => 'Learn More', |
| 130 | 'activate' => 'Activate', |
| 131 | 'all_set' => 'Awesome! You are all set!', |
| 132 | 'woo' => [ |
| 133 | 'title' => 'More extensions from Themeisle', |
| 134 | 'title2' => 'Recommended extensions', |
| 135 | 'cta_install' => 'Install', |
| 136 | 'learn_more' => 'Learn More', |
| 137 | 'dismiss' => 'Dismiss this suggestion', |
| 138 | 'ppom_title' => 'Product Add-Ons', |
| 139 | 'ppom_desc' => 'Add extra custom fields & add-ons on your product pages, like sizes, colors & more.', |
| 140 | 'spark_title1' => 'Wishlist', |
| 141 | 'spark_title2' => 'Multi-Announcement Bars', |
| 142 | 'spark_title3' => 'Advanced Product Review', |
| 143 | 'spark_desc1' => 'Loyalize your customers by allowing them to save their favorite products.', |
| 144 | 'spark_desc2' => 'Add a top notification bar on your website to highlight the latest products, offers, or upcoming events.', |
| 145 | 'spark_desc3' => 'Enable an advanced review section, enlarging the basic review options with lots of capabilities.', |
| 146 | ], |
| 147 | |
| 148 | 'feedzy' => [ |
| 149 | 'import_desc' => 'Schedule automatic content imports from any RSS feed directly to your site. %sBuilt by %s%s', |
| 150 | 'install_now' => 'Install Now', |
| 151 | 'by' => 'by %s', |
| 152 | 'editor_recommends' => '%s recommends %sFeedzy%s to display entries from any RSS feed with more advanced styling and filtering options.', |
| 153 | ], |
| 154 | 'optimole' => [ |
| 155 | 'installOptimole' => 'Install Optimole', |
| 156 | 'gotodash' => 'Go to Optimole dashboard', |
| 157 | 'dismisscta' => 'Dismiss this notice.', |
| 158 | 'message1' => 'Increase this page speed and SEO ranking by optimizing images with Optimole.', |
| 159 | 'message3' => 'Save your server space by storing images to Optimole and deliver them optimized from 400 locations around the globe. Unlimited images, Unlimited traffic.', |
| 160 | 'message4' => 'This image looks to be too large and would affect your site speed, we recommend you to install Optimole to optimize your images.', |
| 161 | 'message2' => 'Leverage Optimole\'s full integration with Elementor to automatically lazyload, resize, compress to AVIF/WebP and deliver from 400 locations around the globe!', |
| 162 | ], |
| 163 | 'redirectionCF7' => [ |
| 164 | 'gotodash' => 'Go to Contact Forms', |
| 165 | 'dismisscta' => 'Dismiss this notice.', |
| 166 | 'gst' => 'Get Started Free', |
| 167 | 'message' => 'Add URL redirects, spam protection, execute JavaScript after submissions, and more with the Redirection for CF7 free plugin.', |
| 168 | ], |
| 169 | 'hyve' => [ |
| 170 | 'gotodash' => 'Go to Hyve Dashboard', |
| 171 | 'install' => 'Install Hyve', |
| 172 | 'dismisscta' => 'Dismiss this notice.', |
| 173 | 'message' => 'Hyve is an AI-powered chatbot that turns your WordPress content into interactive conversations, helping you efficiently handle user inquiries.', |
| 174 | ], |
| 175 | 'wp_full_pay' => [ |
| 176 | 'gotodash' => 'Go to WP Full Pay Settings', |
| 177 | 'install' => 'Install WP Full Pay', |
| 178 | 'dismisscta' => 'Dismiss this notice.', |
| 179 | 'message' => 'Enhance your donation page with WP Full Pay—create custom Stripe forms for one-time and recurring donations, manage transactions easily, and boost support with a seamless setup.', |
| 180 | ], |
| 181 | 'masteriyo' => [ |
| 182 | 'gotodash' => 'Go to Masteriyo Dashboard', |
| 183 | 'install' => 'Install Masteriyo', |
| 184 | 'dismisscta' => 'Dismiss this notice.', |
| 185 | 'message' => 'Transform your site into a learning hub with Masteriyo LMS. Build engaging courses with intuitive tools, track student progress effortlessly, and grow your education business with powerful marketing features and seamless payment integration.', |
| 186 | ], |
| 187 | ], |
| 188 | 'welcome' => [ |
| 189 | 'ctan' => 'No, thanks.', |
| 190 | 'ctay' => 'Upgrade Now!', |
| 191 | 'message' => '<p>You\'ve been using <b>{product}</b> for 7 days now and we appreciate your loyalty! We also want to make sure you\'re getting the most out of our product. That\'s why we\'re offering you a special deal - upgrade to <b>{pro_product}</b> in the next 5 days and receive a discount of <b>up to 30%</b>. <a href="{cta_link}" target="_blank">Upgrade now</a> and unlock all the amazing features of <b>{pro_product}</b>!</p>', |
| 192 | ], |
| 193 | 'uninstall' => [ |
| 194 | 'heading_plugin' => 'What\'s wrong?', |
| 195 | 'heading_theme' => 'What does not work for you in {theme}?', |
| 196 | 'submit' => 'Submit', |
| 197 | 'cta_info' => 'What info do we collect?', |
| 198 | 'button_submit' => 'Submit & Deactivate', |
| 199 | 'button_cancel' => 'Skip & Deactivate', |
| 200 | 'disclosure' => [ |
| 201 | 'title' => 'Below is a detailed view of all data that Themeisle will receive if you fill in this survey. No email address or IP addresses are transmitted after you submit the survey.', |
| 202 | 'version' => '%s %s version %s %s %s %s', |
| 203 | 'website' => '%sCurrent website:%s %s %s %s', |
| 204 | 'usage' => '%sUsage time:%s %s %s%s', |
| 205 | 'reason' => '%s Uninstall reason %s %s Selected reason from the above survey %s ', |
| 206 | ], |
| 207 | |
| 208 | 'options' => [ |
| 209 | 'id3' => [ |
| 210 | 'title' => 'I found a better plugin', |
| 211 | 'placeholder' => 'What\'s the plugin\'s name?', |
| 212 | ], |
| 213 | 'id4' => [ |
| 214 | |
| 215 | 'title' => 'I could not get the plugin to work', |
| 216 | 'placeholder' => 'What problem are you experiencing?', |
| 217 | ], |
| 218 | 'id5' => [ |
| 219 | |
| 220 | 'title' => 'I no longer need the plugin', |
| 221 | 'placeholder' => 'If you could improve one thing about our product, what would it be?', |
| 222 | ], |
| 223 | 'id6' => [ |
| 224 | 'title' => 'It\'s a temporary deactivation. I\'m just debugging an issue.', |
| 225 | 'placeholder' => 'What problem are you experiencing?', |
| 226 | ], |
| 227 | 'id7' => [ |
| 228 | 'title' => 'I don\'t know how to make it look like demo', |
| 229 | ], |
| 230 | 'id8' => [ |
| 231 | |
| 232 | 'placeholder' => 'What option is missing?', |
| 233 | 'title' => 'It lacks options', |
| 234 | ], |
| 235 | 'id9' => [ |
| 236 | 'title' => 'Is not working with a plugin that I need', |
| 237 | 'placeholder' => 'What is the name of the plugin', |
| 238 | ], |
| 239 | 'id10' => [ |
| 240 | 'title' => 'I want to try a new design, I don\'t like {theme} style', |
| 241 | ], |
| 242 | 'id999' => [ |
| 243 | 'title' => 'Other', |
| 244 | 'placeholder' => 'What can we do better?', |
| 245 | ], |
| 246 | ], |
| 247 | ], |
| 248 | 'review' => [ |
| 249 | 'notice' => '<p>Hey, it\'s great to see you have <b>{product}</b> active for a few days now. How is everything going? If you can spare a few moments to rate it on WordPress.org it would help us a lot (and boost my motivation). Cheers! <br/> <br/>~ {developer}, developer of {product}</p>', |
| 250 | 'ctay' => 'Ok, I will gladly help.', |
| 251 | 'ctan' => 'No, thanks.', |
| 252 | |
| 253 | ], |
| 254 | 'rollback' => [ |
| 255 | 'cta' => 'Rollback to v%s', |
| 256 | ], |
| 257 | 'logger' => [ |
| 258 | 'notice' => 'Help improve <b>{product}</b> by sharing anonymous usage data about your setup. No personal data collected.', |
| 259 | 'cta_y' => 'Count me in', |
| 260 | 'cta_n' => 'No thanks', |
| 261 | ], |
| 262 | 'about_us' => [ |
| 263 | 'title' => 'About Us', |
| 264 | 'heroHeader' => 'Our Story', |
| 265 | 'heroTextFirst' => 'Themeisle was founded in 2012 by a group of passionate developers who wanted to create beautiful and functional WordPress themes and plugins. Since then, we have grown into a team of over 20 dedicated professionals who are committed to delivering the best possible products to our customers.', |
| 266 | 'heroTextSecond' => 'At Themeisle, we offer a wide range of WordPress themes and plugins that are designed to meet the needs of both beginners and advanced users. Our products are feature-rich, easy to use, and are designed to help you create beautiful and functional websites.', |
| 267 | 'teamImageCaption' => 'Our team in WCEU2022 in Portugal', |
| 268 | 'newsHeading' => 'Stay connected for news & updates!', |
| 269 | 'emailPlaceholder' => 'Your email address', |
| 270 | 'signMeUp' => 'Sign me up', |
| 271 | 'services' => [ |
| 272 | 'ariaLabel' => 'Themeisle services', |
| 273 | 'trustpilotLabel' => 'Rated excellent on Trustpilot', |
| 274 | 'trustpilotRated' => 'Rated', |
| 275 | 'trustpilotOn' => 'on', |
| 276 | 'trustpilotBrand' => 'Trustpilot', |
| 277 | 'heading' => 'Expert WordPress services from the Themeisle team', |
| 278 | 'description' => 'Done for you by the same people who build your plugins and themes.', |
| 279 | 'cta' => 'Explore all services', |
| 280 | 'items' => [ |
| 281 | 'websiteDesign' => [ |
| 282 | 'title' => 'Website Design', |
| 283 | 'subtitle' => 'Built for your business', |
| 284 | ], |
| 285 | 'support' => [ |
| 286 | 'title' => 'Support', |
| 287 | 'subtitle' => 'On-demand expert help', |
| 288 | ], |
| 289 | 'speed' => [ |
| 290 | 'title' => 'Speed Optimization', |
| 291 | 'subtitle' => 'Core Web Vitals boost', |
| 292 | ], |
| 293 | 'seo' => [ |
| 294 | 'title' => 'SEO Foundation', |
| 295 | 'subtitle' => 'Rank & get found', |
| 296 | ], |
| 297 | 'maintenance' => [ |
| 298 | 'title' => 'Maintenance', |
| 299 | 'subtitle' => 'Updates, backups, security', |
| 300 | ], |
| 301 | 'hackedSite' => [ |
| 302 | 'title' => 'Hacked Site Repair', |
| 303 | 'subtitle' => 'Malware removed fast', |
| 304 | ], |
| 305 | ], |
| 306 | ], |
| 307 | 'installNow' => 'Install Now', |
| 308 | 'activate' => 'Activate', |
| 309 | 'learnMore' => 'Learn More', |
| 310 | 'installed' => 'Installed', |
| 311 | 'notInstalled' => 'Not Installed', |
| 312 | 'active' => 'Active', |
| 313 | 'others' => [ |
| 314 | 'optimole_desc' => 'Optimole is an image optimization service that automatically optimizes your images and serves them to your visitors via a global CDN, making your website lighter, faster and helping you reduce your bandwidth usage.', |
| 315 | 'neve_desc' => 'A fast, lightweight, customizable WordPress theme offering responsive design, speed, and flexibility for various website types.', |
| 316 | 'landingkit_desc' => 'Turn WordPress into a landing page powerhouse with Landing Kit, map domains to pages or any other published resource.', |
| 317 | 'sparks_desc' => 'Extend your store functionality with 8 ultra-performant features like product comparisons, variation swatches, wishlist, and more.', |
| 318 | 'tpc_desc' => 'Design, save, and revisit your templates anytime with your personal vault on Templates Cloud.', |
| 319 | ], |
| 320 | 'otter-page' => [ |
| 321 | 'heading' => 'Build innovative layouts with Otter Blocks and Gutenberg', |
| 322 | 'text' => 'Otter is a lightweight, dynamic collection of page building blocks and templates for the WordPress block editor.', |
| 323 | 'buttons' => [ |
| 324 | 'install_otter_free' => "Install Otter - It's free!", |
| 325 | 'install_now' => 'Install Now', |
| 326 | 'learn_more' => 'Learn More', |
| 327 | ], |
| 328 | 'features' => [ |
| 329 | 'advancedTitle' => 'Advanced Features', |
| 330 | 'advancedDesc' => 'Add features such as Custom CSS, Animations & Visibility Conditions to all blocks.', |
| 331 | 'fastTitle' => 'Lightweight and Fast', |
| 332 | 'fastDesc' => 'Otter enhances WordPress site building experience without impacting site speed.', |
| 333 | 'mobileTitle' => 'Mobile-Friendly', |
| 334 | 'mobileDesc' => 'Each block can be tweaked to provide a consistent experience across all devices.', |
| 335 | ], |
| 336 | 'details' => [ |
| 337 | 's1Title' => 'A Better Page Building Experience', |
| 338 | 's1Text' => 'Otter can be used to build everything from a personal blog to an e-commerce site without losing the personal touch. Otter’s ease of use transforms basic blocks into expressive layouts in seconds.', |
| 339 | 's2Title' => 'A New Collection of Patterns', |
| 340 | 's2Text' => 'A New Patterns Library, containing a range of different elements in a variety of styles to help you build great pages. All of your website’s most important areas are covered: headers, testimonials, pricing tables, sections and more.', |
| 341 | 's3Title' => 'Advanced Blocks', |
| 342 | 's3Text' => 'Enhance your website’s design with powerful blocks, like the Add to Cart, Business Hours, Review Comparison, and dozens of WooCommerce blocks.', |
| 343 | ], |
| 344 | 'testimonials' => [ |
| 345 | 'heading' => 'Trusted by more than 300K website owners', |
| 346 | 'users' => [ |
| 347 | 'user_1' => 'Loved the collection of blocks. If you want to create nice Gutenberg Pages, this plugin will be very handy and useful.', |
| 348 | 'user_2' => 'I am very satisfied with Otter – a fantastic collection of blocks. And the plugin is perfectly integrated with Gutenberg and complete enough for my needs.', |
| 349 | 'user_3' => 'Otter Blocks work really well and I like the customization options. Easy to use and format to fit in with my site theme – and I’ve not encountered any compatibility or speed issues.', |
| 350 | ], |
| 351 | ], |
| 352 | ], |
| 353 | ], |
| 354 | 'float_widget' => [ |
| 355 | 'button' => 'Toggle Help Widget for %s', |
| 356 | 'panel' => [ |
| 357 | 'greeting' => 'Thank you for using %s', |
| 358 | 'title' => 'How can we help you?', |
| 359 | 'close' => 'Close Toggle Help Widget', |
| 360 | ], |
| 361 | 'links' => [ |
| 362 | 'documentation' => 'Documentation', |
| 363 | 'support' => 'Get Support', |
| 364 | 'wizard' => 'Run Setup Wizard', |
| 365 | 'upgrade' => 'Upgrade to Pro', |
| 366 | 'feature_request' => 'Suggest a Feature', |
| 367 | 'rate' => 'Rate Us', |
| 368 | ], |
| 369 | ], |
| 370 | ]; |
| 371 | |
| 372 | /** |
| 373 | * Initialize the sdk logic. |
| 374 | */ |
| 375 | public static function init() { |
| 376 | self::localize_labels(); |
| 377 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Loader ) ) { |
| 378 | self::$instance = new Loader(); |
| 379 | $modules = array_merge( self::$available_modules, apply_filters( 'themeisle_sdk_modules', [] ) ); |
| 380 | foreach ( $modules as $key => $module ) { |
| 381 | if ( ! class_exists( 'ThemeisleSDK\\Modules\\' . ucwords( $module, '_' ) ) ) { |
| 382 | unset( $modules[ $key ] ); |
| 383 | } |
| 384 | } |
| 385 | self::$available_modules = $modules; |
| 386 | |
| 387 | add_action( 'themeisle_sdk_first_activation', array( __CLASS__, 'activate' ) ); |
| 388 | |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Localize the labels. |
| 394 | */ |
| 395 | public static function localize_labels() { |
| 396 | $originals = self::$labels; |
| 397 | $all_translations = []; |
| 398 | |
| 399 | global $wp_filter; |
| 400 | if ( isset( $wp_filter['themeisle_sdk_labels'] ) ) { |
| 401 | foreach ( $wp_filter['themeisle_sdk_labels']->callbacks as $priority => $hooks ) { |
| 402 | foreach ( $hooks as $hook ) { |
| 403 | // Each callback gets fresh originals, not previous callback's output |
| 404 | $result = call_user_func( $hook['function'], $originals ); |
| 405 | $all_translations[] = $result; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // Remove the filter so it doesn't run again via apply_filters |
| 410 | remove_all_filters( 'themeisle_sdk_labels' ); |
| 411 | } |
| 412 | |
| 413 | // Merge all results, first real translation wins |
| 414 | self::$labels = self::merge_all_translations( $originals, $all_translations ); |
| 415 | } |
| 416 | /** |
| 417 | * Merge all translations. |
| 418 | * |
| 419 | * @param array $originals The original labels. |
| 420 | * @param array $all_translations The all translations. |
| 421 | * |
| 422 | * @return array The merged labels. |
| 423 | */ |
| 424 | private static function merge_all_translations( $originals, $all_translations ) { |
| 425 | $result = $originals; |
| 426 | |
| 427 | foreach ( $all_translations as $translations ) { |
| 428 | $result = self::merge_if_translated( $result, $translations, $originals ); |
| 429 | } |
| 430 | |
| 431 | return $result; |
| 432 | } |
| 433 | /** |
| 434 | * Merge if translated. |
| 435 | * |
| 436 | * @param array $current The current labels. |
| 437 | * @param array $new The new labels. |
| 438 | * @param array $originals The original labels. |
| 439 | * @return array The merged labels. |
| 440 | */ |
| 441 | private static function merge_if_translated( $current, $new, $originals ) { |
| 442 | foreach ( $new as $key => $value ) { |
| 443 | if ( ! isset( $originals[ $key ] ) ) { |
| 444 | // New key, accept it |
| 445 | if ( ! isset( $current[ $key ] ) ) { |
| 446 | $current[ $key ] = $value; |
| 447 | } |
| 448 | continue; |
| 449 | } |
| 450 | |
| 451 | if ( is_array( $value ) && is_array( $originals[ $key ] ) ) { |
| 452 | $current[ $key ] = self::merge_if_translated( |
| 453 | $current[ $key ], |
| 454 | $value, |
| 455 | $originals[ $key ] |
| 456 | ); |
| 457 | } else { |
| 458 | // Only accept if: |
| 459 | // 1. New value is actually translated (differs from original) |
| 460 | // 2. Current value is NOT already translated |
| 461 | $is_new_translated = ( $value !== $originals[ $key ] ); |
| 462 | $is_current_untranslated = ( $current[ $key ] === $originals[ $key ] ); |
| 463 | |
| 464 | if ( $is_new_translated && $is_current_untranslated ) { |
| 465 | $current[ $key ] = $value; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return $current; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Get cache token used in API requests. |
| 475 | * |
| 476 | * @return string Cache token. |
| 477 | */ |
| 478 | public static function get_cache_token() { |
| 479 | $cache_token = get_transient( 'themeisle_sdk_cache_token' ); |
| 480 | if ( false === $cache_token ) { |
| 481 | $cache_token = wp_generate_password( 6, false ); |
| 482 | set_transient( $cache_token, WEEK_IN_SECONDS ); |
| 483 | } |
| 484 | |
| 485 | return $cache_token; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Clear cache token. |
| 490 | */ |
| 491 | public static function clear_cache_token() { |
| 492 | delete_transient( 'themeisle_sdk_cache_token' ); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Register product into SDK. |
| 497 | * |
| 498 | * @param string $base_file The product base file. |
| 499 | * |
| 500 | * @return Loader The singleton object. |
| 501 | */ |
| 502 | public static function add_product( $base_file ) { |
| 503 | |
| 504 | if ( ! is_file( $base_file ) ) { |
| 505 | return self::$instance; |
| 506 | } |
| 507 | $product = new Product( $base_file ); |
| 508 | |
| 509 | Module_Factory::attach( $product, self::get_modules() ); |
| 510 | |
| 511 | self::$products[ $product->get_slug() ] = $product; |
| 512 | |
| 513 | return self::$instance; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Activate the product routine. |
| 518 | * |
| 519 | * @param string $file The base file of the product. |
| 520 | * |
| 521 | * @return void |
| 522 | */ |
| 523 | public static function activate( $file ) { |
| 524 | |
| 525 | $dirname = trailingslashit( dirname( ( $file ) ) ); |
| 526 | if ( ! file_exists( $dirname . '_reference.php' ) ) { |
| 527 | return; |
| 528 | } |
| 529 | $reference_data = require_once $dirname . '_reference.php'; |
| 530 | if ( ! is_array( $reference_data ) || |
| 531 | ! isset( $reference_data['key'] ) || |
| 532 | ! isset( $reference_data['value'] ) || |
| 533 | ! preg_match( '/^[a-zA-Z0-9_]+_reference_key$/', $reference_data['key'] ) ) { |
| 534 | return; |
| 535 | } |
| 536 | add_option( $reference_data['key'], sanitize_key( $reference_data['value'] ) ); |
| 537 | } |
| 538 | /** |
| 539 | * Get all registered modules by the SDK. |
| 540 | * |
| 541 | * @return array Modules available. |
| 542 | */ |
| 543 | public static function get_modules() { |
| 544 | return self::$available_modules; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Get all products using the SDK. |
| 549 | * |
| 550 | * @return array<Product> Products available. |
| 551 | */ |
| 552 | public static function get_products() { |
| 553 | return self::$products; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Get the version of the SDK. |
| 558 | * |
| 559 | * @return string The version. |
| 560 | */ |
| 561 | public static function get_version() { |
| 562 | return self::$version; |
| 563 | } |
| 564 | } |
| 565 |