| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Extendify\Library; |
| 7 |
|
| 8 |
use Extendify\Config; |
| 9 |
use Extendify\User; |
| 10 |
use Extendify\Library\SiteSettings; |
| 11 |
|
| 12 |
/** |
| 13 |
* This class handles any file loading for the admin area. |
| 14 |
*/ |
| 15 |
class Admin |
| 16 |
{ |
| 17 |
|
| 18 |
/** |
| 19 |
* The instance |
| 20 |
* |
| 21 |
* @var $instance |
| 22 |
*/ |
| 23 |
public static $instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Adds various actions to set up the page |
| 27 |
* |
| 28 |
* @return self|void |
| 29 |
*/ |
| 30 |
public function __construct() |
| 31 |
{ |
| 32 |
if (self::$instance) { |
| 33 |
return self::$instance; |
| 34 |
} |
| 35 |
|
| 36 |
self::$instance = $this; |
| 37 |
$this->loadScripts(); |
| 38 |
|
| 39 |
\add_filter('plugin_action_links_' . EXTENDIFY_PLUGIN_BASENAME, [ $this, 'pluginActionLinks' ]); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Adds action links to the plugin list table |
| 44 |
* |
| 45 |
* @param array $links An array of plugin action links. |
| 46 |
* @return array An array of plugin action links. |
| 47 |
*/ |
| 48 |
public function pluginActionLinks($links) |
| 49 |
{ |
| 50 |
if (defined('EXTENDIFY_SITE_LICENSE')) { |
| 51 |
return $links; |
| 52 |
} |
| 53 |
|
| 54 |
$theme = get_option('template'); |
| 55 |
$label = esc_html__('Upgrade', 'extendify'); |
| 56 |
|
| 57 |
$links['upgrade'] = sprintf( |
| 58 |
'<a href="%1$s" target="_blank"><b>%2$s</b></a>', |
| 59 |
"https://extendify.com/pricing?utm_source=extendify-plugin&utm_medium=wp-dash&utm_campaign=action-link&utm_content=$label&utm_term=$theme", |
| 60 |
$label |
| 61 |
); |
| 62 |
|
| 63 |
return $links; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Adds scripts to the admin |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function loadScripts() |
| 72 |
{ |
| 73 |
\add_action( |
| 74 |
'admin_enqueue_scripts', |
| 75 |
function ($hook) { |
| 76 |
if (!current_user_can(Config::$requiredCapability)) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if (!$this->checkItsGutenbergPost($hook)) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if (!$this->isLibraryEnabled()) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$this->addScopedScriptsAndStyles(); |
| 89 |
} |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Makes sure we are on the correct page |
| 95 |
* |
| 96 |
* @param string $hook - An optional hook provided by WP to identify the page. |
| 97 |
* @return boolean |
| 98 |
*/ |
| 99 |
public function checkItsGutenbergPost($hook = '') |
| 100 |
{ |
| 101 |
// Check for the post type, or on the FSE page. |
| 102 |
$type = isset($GLOBALS['typenow']) ? $GLOBALS['typenow'] : ''; |
| 103 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 104 |
if (!$type && isset($_GET['postType'])) { |
| 105 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 106 |
$type = sanitize_text_field(wp_unslash($_GET['postType'])); |
| 107 |
} |
| 108 |
|
| 109 |
if (\use_block_editor_for_post_type($type)) { |
| 110 |
return $hook && in_array($hook, ['post.php', 'post-new.php', 'appearance_page_gutenberg-edit-site'], true); |
| 111 |
} |
| 112 |
|
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Adds various JS scripts |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public function addScopedScriptsAndStyles() |
| 122 |
{ |
| 123 |
$version = Config::$environment === 'PRODUCTION' ? Config::$version : uniqid(); |
| 124 |
|
| 125 |
\wp_register_script( |
| 126 |
Config::$slug . '-scripts', |
| 127 |
EXTENDIFY_BASE_URL . 'public/build/extendify.js', |
| 128 |
[ |
| 129 |
'wp-i18n', |
| 130 |
'wp-components', |
| 131 |
'wp-element', |
| 132 |
'wp-editor', |
| 133 |
], |
| 134 |
$version, |
| 135 |
true |
| 136 |
); |
| 137 |
\wp_localize_script( |
| 138 |
Config::$slug . '-scripts', |
| 139 |
'extendifyData', |
| 140 |
[ |
| 141 |
'root' => \esc_url_raw(rest_url(Config::$slug . '/' . Config::$apiVersion)), |
| 142 |
'nonce' => \wp_create_nonce('wp_rest'), |
| 143 |
'user' => json_decode(User::data('extendifysdk_user_data'), true), |
| 144 |
'sitesettings' => json_decode(SiteSettings::data()), |
| 145 |
'sdk_partner' => \esc_attr(Config::$sdkPartner), |
| 146 |
'asset_path' => \esc_url(EXTENDIFY_URL . 'public/assets'), |
| 147 |
'standalone' => \esc_attr(Config::$standalone), |
| 148 |
'devbuild' => \esc_attr(Config::$environment === 'DEVELOPMENT'), |
| 149 |
] |
| 150 |
); |
| 151 |
\wp_enqueue_script(Config::$slug . '-scripts'); |
| 152 |
|
| 153 |
\wp_set_script_translations(Config::$slug . '-scripts', 'extendify'); |
| 154 |
|
| 155 |
// Inline the library styles to keep them out of the iframe live preview. |
| 156 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 157 |
$css = file_get_contents(EXTENDIFY_PATH . 'public/build/extendify.css'); |
| 158 |
\wp_register_style(Config::$slug, false, [], $version); |
| 159 |
\wp_enqueue_style(Config::$slug); |
| 160 |
\wp_add_inline_style(Config::$slug, $css); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Check if current user is Admin |
| 165 |
* |
| 166 |
* @return Boolean |
| 167 |
*/ |
| 168 |
private function isAdmin() |
| 169 |
{ |
| 170 |
if (\is_multisite()) { |
| 171 |
return \is_super_admin(); |
| 172 |
} |
| 173 |
|
| 174 |
return in_array('administrator', \wp_get_current_user()->roles, true); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Check if scripts should add |
| 179 |
* |
| 180 |
* @return Boolean |
| 181 |
*/ |
| 182 |
public function isLibraryEnabled() |
| 183 |
{ |
| 184 |
$settings = json_decode(SiteSettings::data()); |
| 185 |
|
| 186 |
// If it's disabled, only show it for admins. |
| 187 |
if (isset($settings->state) && (isset($settings->state->enabled)) && !$settings->state->enabled) { |
| 188 |
return $this->isAdmin(); |
| 189 |
} |
| 190 |
|
| 191 |
return true; |
| 192 |
} |
| 193 |
} |
| 194 |
|