| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Extendify\Library; |
| 7 |
|
| 8 |
use Extendify\PartnerData; |
| 9 |
use Extendify\Config; |
| 10 |
use Extendify\User; |
| 11 |
|
| 12 |
/** |
| 13 |
* This class handles any file loading for the admin area. |
| 14 |
*/ |
| 15 |
class Admin |
| 16 |
{ |
| 17 |
/** |
| 18 |
* The instance |
| 19 |
* |
| 20 |
* @var $instance |
| 21 |
*/ |
| 22 |
public static $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Adds various actions to set up the page |
| 26 |
* |
| 27 |
* @return self|void |
| 28 |
*/ |
| 29 |
public function __construct() |
| 30 |
{ |
| 31 |
if (self::$instance) { |
| 32 |
return self::$instance; |
| 33 |
} |
| 34 |
|
| 35 |
self::$instance = $this; |
| 36 |
$this->loadScripts(); |
| 37 |
|
| 38 |
// Check if legacy classes are present. |
| 39 |
if (!get_option('extendify_has_legacy_classes', false)) { |
| 40 |
// TODO: Remove the ! in the following if statement once patterns and templates no longer use utility classes. |
| 41 |
if (!version_compare(get_option('extendify_first_installed_version', '0.0.0'), '1.7.0', '<')) { |
| 42 |
if (!$this->patternWasImported()) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
$siteSettings = json_decode(get_option('extendifysdk_sitesettings', '{ "state": {} }')); |
| 47 |
if (!isset($siteSettings->state->activateLegacyClasses) || $siteSettings->state->activateLegacyClasses === false) { |
| 48 |
$siteSettings->state->activateLegacyClasses = true; |
| 49 |
update_option('extendifysdk_sitesettings', wp_json_encode($siteSettings)); |
| 50 |
} |
| 51 |
|
| 52 |
update_option('extendify_has_legacy_classes', true); |
| 53 |
} |
| 54 |
}//end if |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Adds scripts to the admin |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function loadScripts() |
| 63 |
{ |
| 64 |
\add_action( |
| 65 |
'admin_enqueue_scripts', |
| 66 |
function ($hook) { |
| 67 |
if (!current_user_can(Config::$requiredCapability)) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$this->maybeAddDeactivationScript(); |
| 72 |
|
| 73 |
if (!$this->checkItsGutenbergPost($hook)) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if (!$this->isLibraryEnabled()) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$this->addScopedScriptsAndStyles(); |
| 82 |
} |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Checks if an Extendify pattern exists in any post type |
| 88 |
* |
| 89 |
* @return boolean |
| 90 |
*/ |
| 91 |
public function patternWasImported() |
| 92 |
{ |
| 93 |
// check if Launch has been completed to avoid database search. |
| 94 |
if (Config::$launchCompleted) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
// This is set when a user imports a pattern from the library. |
| 99 |
$wasImported = get_option('extendify_pattern_was_imported', null); |
| 100 |
if ($wasImported !== null) { |
| 101 |
return $wasImported; |
| 102 |
} |
| 103 |
|
| 104 |
// We only check this once (for bw compatibility). |
| 105 |
$wpdb = $GLOBALS['wpdb']; |
| 106 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 107 |
$patternExists = $wpdb->get_var( |
| 108 |
$wpdb->prepare( |
| 109 |
"SELECT COUNT(*) FROM $wpdb->posts WHERE post_content LIKE %s AND post_type != 'revision'", |
| 110 |
'%extUtilities%' |
| 111 |
) |
| 112 |
) !== '0'; |
| 113 |
|
| 114 |
update_option('extendify_pattern_was_imported', $patternExists); |
| 115 |
return $patternExists; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Makes sure we are on the correct page |
| 120 |
* |
| 121 |
* @param string $hook - An optional hook provided by WP to identify the page. |
| 122 |
* @return boolean |
| 123 |
*/ |
| 124 |
public function checkItsGutenbergPost($hook = '') |
| 125 |
{ |
| 126 |
// Check for the post type, or on the FSE page. |
| 127 |
$type = isset($GLOBALS['typenow']) ? $GLOBALS['typenow'] : ''; |
| 128 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 129 |
if (!$type && isset($_GET['postType'])) { |
| 130 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 131 |
$type = sanitize_text_field(wp_unslash($_GET['postType'])); |
| 132 |
} |
| 133 |
|
| 134 |
if (\use_block_editor_for_post_type($type)) { |
| 135 |
return $hook && in_array($hook, ['post.php', 'post-new.php'], true); |
| 136 |
} |
| 137 |
|
| 138 |
// Temporarily disable the library on the site editor page until the issues with 6.3 are fixed. |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Adds various JS scripts |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function addScopedScriptsAndStyles() |
| 148 |
{ |
| 149 |
$user = json_decode(User::data('extendifysdk_user_data'), true); |
| 150 |
$openOnNewPage = isset($user['state']['openOnNewPage']) ? $user['state']['openOnNewPage'] : Config::$launchCompleted; |
| 151 |
$version = Config::$environment === 'PRODUCTION' ? Config::$version : uniqid(); |
| 152 |
$scriptAssetPath = EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify.php']; |
| 153 |
$fallback = [ |
| 154 |
'dependencies' => [], |
| 155 |
'version' => $version, |
| 156 |
]; |
| 157 |
$scriptAsset = file_exists($scriptAssetPath) ? require $scriptAssetPath : $fallback; |
| 158 |
foreach ($scriptAsset['dependencies'] as $style) { |
| 159 |
wp_enqueue_style($style); |
| 160 |
} |
| 161 |
|
| 162 |
\wp_register_script( |
| 163 |
Config::$slug . '-scripts', |
| 164 |
EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify.js'], |
| 165 |
$scriptAsset['dependencies'], |
| 166 |
$scriptAsset['version'], |
| 167 |
true |
| 168 |
); |
| 169 |
|
| 170 |
\wp_localize_script( |
| 171 |
Config::$slug . '-scripts', |
| 172 |
'extendifyData', |
| 173 |
array_merge([ |
| 174 |
'root' => \esc_url_raw(rest_url(Config::$slug . '/' . Config::$apiVersion)), |
| 175 |
'nonce' => \wp_create_nonce('wp_rest'), |
| 176 |
'partnerLogo' => PartnerData::$logo, |
| 177 |
'partnerName' => PartnerData::$name, |
| 178 |
'user' => $user, |
| 179 |
'openOnNewPage' => $openOnNewPage, |
| 180 |
'sitesettings' => json_decode(SiteSettings::data()), |
| 181 |
'sdk_partner' => \esc_attr(PartnerData::$id), |
| 182 |
'asset_path' => \esc_url(EXTENDIFY_URL . 'public/assets'), |
| 183 |
'devbuild' => \esc_attr(Config::$environment === 'DEVELOPMENT'), |
| 184 |
'siteId' => \get_option('extendify_site_id', ''), |
| 185 |
'hasLegacyClasses' => (bool) \get_option('extendify_has_legacy_classes', false), |
| 186 |
]) |
| 187 |
); |
| 188 |
|
| 189 |
\wp_enqueue_script(Config::$slug . '-scripts'); |
| 190 |
|
| 191 |
\wp_set_script_translations(Config::$slug . '-scripts', 'extendify'); |
| 192 |
|
| 193 |
// Inline the library styles to keep them out of the iframe live preview. |
| 194 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 195 |
$css = file_get_contents(EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify.css']); |
| 196 |
\wp_register_style(Config::$slug, false, [], Config::$version); |
| 197 |
\wp_enqueue_style(Config::$slug); |
| 198 |
\wp_add_inline_style(Config::$slug, $css); |
| 199 |
|
| 200 |
$cssColorVars = PartnerData::cssVariableMapping(); |
| 201 |
$cssString = implode('; ', array_map(function ($k, $v) { |
| 202 |
return "$k: $v"; |
| 203 |
}, array_keys($cssColorVars), $cssColorVars)); |
| 204 |
wp_add_inline_style(Config::$slug, "body { $cssString; }"); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Adds deactivation prompt JS script |
| 209 |
* |
| 210 |
* @return void |
| 211 |
*/ |
| 212 |
public function maybeAddDeactivationScript() |
| 213 |
{ |
| 214 |
$screen = get_current_screen(); |
| 215 |
if (!isset($screen->id) || $screen->id !== 'plugins') { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
if (version_compare(get_bloginfo('version'), '6.2', '<')) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
if (!$this->patternWasImported()) { |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
// phpcs:disable Squiz.PHP.CommentedOutCode |
| 228 |
// TODO: Uncomment this when the CSS utility setting is implemented. |
| 229 |
|
| 230 |
/* |
| 231 |
$hasLegacyClasses = json_decode(\get_option('extendifysdk_sitesettings', null)); |
| 232 |
if (!isset($hasLegacyClasses->state->activateLegacyClasses) || $hasLegacyClasses->state->activateLegacyClasses === false) { |
| 233 |
return; |
| 234 |
} |
| 235 |
*/ |
| 236 |
// phpcs:enable |
| 237 |
|
| 238 |
$version = Config::$environment === 'PRODUCTION' ? Config::$version : uniqid(); |
| 239 |
$scriptAssetPath = EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-deactivate.php']; |
| 240 |
$fallback = [ |
| 241 |
'dependencies' => [], |
| 242 |
'version' => $version, |
| 243 |
]; |
| 244 |
$scriptAsset = file_exists($scriptAssetPath) ? require $scriptAssetPath : $fallback; |
| 245 |
foreach ($scriptAsset['dependencies'] as $style) { |
| 246 |
wp_enqueue_style($style); |
| 247 |
} |
| 248 |
|
| 249 |
\wp_register_script( |
| 250 |
Config::$slug . '-deactivate-scripts', |
| 251 |
EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify-deactivate.js'], |
| 252 |
$scriptAsset['dependencies'], |
| 253 |
$scriptAsset['version'], |
| 254 |
true |
| 255 |
); |
| 256 |
|
| 257 |
\wp_localize_script( |
| 258 |
Config::$slug . '-deactivate-scripts', |
| 259 |
'extendifyData', |
| 260 |
array_merge([ |
| 261 |
'root' => \esc_url_raw(rest_url(Config::$slug . '/' . Config::$apiVersion)), |
| 262 |
'nonce' => \wp_create_nonce('wp_rest'), |
| 263 |
'partnerLogo' => PartnerData::$logo, |
| 264 |
'partnerName' => PartnerData::$name, |
| 265 |
'adminUrl' => \esc_url_raw(\admin_url()), |
| 266 |
]) |
| 267 |
); |
| 268 |
|
| 269 |
\wp_enqueue_script(Config::$slug . '-deactivate-scripts'); |
| 270 |
|
| 271 |
\wp_set_script_translations(Config::$slug . '-deactivate-scripts', 'extendify'); |
| 272 |
|
| 273 |
\wp_enqueue_style(Config::$slug, EXTENDIFY_BASE_URL . '/public/build/' . Config::$assetManifest['extendify.css'], [], Config::$version); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Check if current user is Admin |
| 278 |
* |
| 279 |
* @return Boolean |
| 280 |
*/ |
| 281 |
private function isAdmin() |
| 282 |
{ |
| 283 |
if (\is_multisite()) { |
| 284 |
return \is_super_admin(); |
| 285 |
} |
| 286 |
|
| 287 |
return in_array('administrator', \wp_get_current_user()->roles, true); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Check if scripts should add based on user setting. |
| 292 |
* |
| 293 |
* @return Boolean |
| 294 |
*/ |
| 295 |
public function isLibraryEnabled() |
| 296 |
{ |
| 297 |
// TODO: For now just always show. |
| 298 |
return true; |
| 299 |
} |
| 300 |
} |
| 301 |
|