| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Bootstrap the application |
| 5 |
*/ |
| 6 |
|
| 7 |
defined('ABSPATH') || die('No direct access.'); |
| 8 |
|
| 9 |
use Extendify\AdminPageRouter; |
| 10 |
use Extendify\Affiliate; |
| 11 |
use Extendify\Assist\Admin as AssistAdmin; |
| 12 |
use Extendify\Config; |
| 13 |
use Extendify\Draft\Admin as DraftAdmin; |
| 14 |
use Extendify\HelpCenter\Admin as HelpCenterAdmin; |
| 15 |
use Extendify\Insights; |
| 16 |
use Extendify\Launch\Admin as LaunchAdmin; |
| 17 |
use Extendify\Library\Admin as LibraryAdmin; |
| 18 |
use Extendify\PageCreator\Admin as PageCreatorAdmin; |
| 19 |
use Extendify\Library\Frontend as LibraryFrontend; |
| 20 |
use Extendify\PartnerData; |
| 21 |
use Extendify\Shared\Admin as SharedAdmin; |
| 22 |
use Extendify\Shared\DataProvider\ResourceData; |
| 23 |
use Extendify\Shared\Services\Import\ImagesImporter; |
| 24 |
use Extendify\Shared\Services\VersionMigrator; |
| 25 |
use Extendify\Recommendations\Admin as RecommendationsAdmin; |
| 26 |
|
| 27 |
if (!defined('EXTENDIFY_REQUIRED_CAPABILITY')) { |
| 28 |
define('EXTENDIFY_REQUIRED_CAPABILITY', 'manage_options'); |
| 29 |
} |
| 30 |
|
| 31 |
if (!defined('EXTENDIFY_PATH')) { |
| 32 |
define('EXTENDIFY_PATH', \plugin_dir_path(__FILE__)); |
| 33 |
} |
| 34 |
|
| 35 |
if (!defined('EXTENDIFY_URL')) { |
| 36 |
define('EXTENDIFY_URL', \plugin_dir_url(__FILE__)); |
| 37 |
} |
| 38 |
|
| 39 |
if (!defined('EXTENDIFY_PLUGIN_BASENAME')) { |
| 40 |
define('EXTENDIFY_PLUGIN_BASENAME', \plugin_basename(__DIR__ . '/extendify.php')); |
| 41 |
} |
| 42 |
|
| 43 |
if (is_readable(EXTENDIFY_PATH . 'vendor/autoload.php')) { |
| 44 |
require EXTENDIFY_PATH . 'vendor/autoload.php'; |
| 45 |
} |
| 46 |
|
| 47 |
if (!defined('EXTENDIFY_IS_THEME_EXTENDABLE')) { |
| 48 |
define('EXTENDIFY_IS_THEME_EXTENDABLE', get_option('stylesheet') === 'extendable'); |
| 49 |
} |
| 50 |
|
| 51 |
// This file should have no dependencies and always load. |
| 52 |
new LibraryFrontend(); |
| 53 |
// This file hooks into an external task and should always load. |
| 54 |
new Insights(); |
| 55 |
// This class set up the image import check scheduler. |
| 56 |
new ImagesImporter(); |
| 57 |
|
| 58 |
// Run various database updates depending on the plugin version. |
| 59 |
new VersionMigrator(); |
| 60 |
|
| 61 |
// This class will fetch and cache partner data to be used |
| 62 |
// throughout every class below. If opt in. |
| 63 |
new PartnerData(); |
| 64 |
|
| 65 |
// Set up scheduled cache (if opt-in and active). |
| 66 |
if (!PartnerData::setting('deactivated')) { |
| 67 |
ResourceData::scheduleCache(); |
| 68 |
} |
| 69 |
|
| 70 |
if (current_user_can(EXTENDIFY_REQUIRED_CAPABILITY)) { |
| 71 |
// The config class will collect information about the |
| 72 |
// partner and plugin, so it's easier to access. |
| 73 |
new Config(); |
| 74 |
if (!defined('EXTENDIFY_DEVMODE')) { |
| 75 |
define('EXTENDIFY_DEVMODE', Config::$environment === 'DEVELOPMENT'); |
| 76 |
} |
| 77 |
|
| 78 |
// This is a global "loader" class that loads in any assets that are shared everywhere. |
| 79 |
new SharedAdmin(); |
| 80 |
// This class will handle loading library assets. |
| 81 |
new LibraryAdmin(); |
| 82 |
|
| 83 |
// Only load these if the partner ID is set. These are all opt-in features. |
| 84 |
if ((Config::$partnerId || constant('EXTENDIFY_DEVMODE')) && !PartnerData::setting('deactivated')) { |
| 85 |
// This class handles the admin pages required for the plugin. |
| 86 |
new AdminPageRouter(); |
| 87 |
|
| 88 |
// This class will handle loading page creator assets. |
| 89 |
if (PartnerData::setting('showAIPageCreation') || constant('EXTENDIFY_DEVMODE')) { |
| 90 |
new PageCreatorAdmin(); |
| 91 |
} |
| 92 |
|
| 93 |
// This class will update links based on the partner's specifications. |
| 94 |
new Affiliate(); |
| 95 |
// The remaining classes handle loading assets for each individual products. |
| 96 |
// They are essentially asset loading classes. |
| 97 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 98 |
if (isset($_GET['page']) && $_GET['page'] === 'extendify-assist') { |
| 99 |
// Load only on Assist. |
| 100 |
new AssistAdmin(); |
| 101 |
} |
| 102 |
|
| 103 |
// Don't load on Launch. |
| 104 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 105 |
if (!isset($_GET['page']) || $_GET['page'] !== 'extendify-launch') { |
| 106 |
new HelpCenterAdmin(); |
| 107 |
if (PartnerData::setting('showProductRecommendations') || constant('EXTENDIFY_DEVMODE')) { |
| 108 |
new RecommendationsAdmin(); |
| 109 |
} |
| 110 |
|
| 111 |
if (PartnerData::setting('showDraft') || constant('EXTENDIFY_DEVMODE')) { |
| 112 |
new DraftAdmin(); |
| 113 |
} |
| 114 |
} else { |
| 115 |
new LaunchAdmin(); |
| 116 |
} |
| 117 |
}//end if |
| 118 |
|
| 119 |
// This loads in all the REST API routes used by the plugin. |
| 120 |
require EXTENDIFY_PATH . 'routes/api.php'; |
| 121 |
}//end if |
| 122 |
|
| 123 |
// This file is used to update the plugin and removed before w.org release. |
| 124 |
if (is_readable(EXTENDIFY_PATH . '/updater.php')) { |
| 125 |
require EXTENDIFY_PATH . 'updater.php'; |
| 126 |
} |
| 127 |
|
| 128 |
add_action('init', function () { |
| 129 |
load_plugin_textdomain('extendify-local', false, dirname(plugin_basename(__FILE__)) . '/languages/php'); |
| 130 |
}); |
| 131 |
|
| 132 |
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter |
| 133 |
add_filter('load_script_translation_file', function ($file, $handle, $domain) { |
| 134 |
if ($domain !== 'extendify-local') { |
| 135 |
return $file; |
| 136 |
} |
| 137 |
|
| 138 |
return extendifyResolveFallbackLocaleFile($file); |
| 139 |
}, 10, 3); |
| 140 |
|
| 141 |
add_filter('load_textdomain_mofile', function ($mofile, $domain) { |
| 142 |
if ($domain !== 'extendify-local') { |
| 143 |
return $mofile; |
| 144 |
} |
| 145 |
|
| 146 |
return extendifyResolveFallbackLocaleFile($mofile); |
| 147 |
}, 10, 2); |
| 148 |
|
| 149 |
/** |
| 150 |
* Resolves a fallback translation file path if the requested locale is unsupported. |
| 151 |
* |
| 152 |
* This function checks if the current file path includes an unsupported locale |
| 153 |
* and replaces it with a supported fallback (e.g. es_AR → es_ES), |
| 154 |
* as long as the fallback file exists on disk. |
| 155 |
* |
| 156 |
* @param string $filepath The original .mo or .json translation file path. |
| 157 |
* @return string The fallback file path if it exists, or the original one. |
| 158 |
*/ |
| 159 |
|
| 160 |
function extendifyResolveFallbackLocaleFile($filepath) |
| 161 |
{ |
| 162 |
if (str_contains($filepath, '-es_') && !file_exists($filepath)) { |
| 163 |
$fallbackFile = preg_replace('/-es_[A-Z]{2}/', '-es_ES', $filepath); |
| 164 |
if ($fallbackFile && file_exists($fallbackFile)) { |
| 165 |
return $fallbackFile; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return $filepath; |
| 170 |
} |
| 171 |
|
| 172 |
// To cover legacy conflicts. |
| 173 |
// phpcs:ignore |
| 174 |
class ExtendifySdk |
| 175 |
{ |
| 176 |
} |
| 177 |
|