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