| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /** |
| 3 | 4 | * Admin. |
| 4 | 5 | */ |
| 5 | 6 | |
| @@ -4,91 +5,43 @@ | ||
| 4 | 5 | */ |
| 5 | 6 | |
| 6 | 7 | namespace Extendify\Library; |
| 7 | 8 | |
| 9 | +defined('ABSPATH') || die('No direct access.'); | |
| 10 | + | |
| 8 | 11 | use Extendify\Config; |
| 9 | -use Extendify\User; | |
| 10 | -use Extendify\Library\SiteSettings; | |
| 12 | +use Extendify\PartnerData; | |
| 11 | 13 | |
| 12 | 14 | /** |
| 13 | 15 | * This class handles any file loading for the admin area. |
| 14 | 16 | */ |
| 17 | + | |
| 15 | 18 | class Admin |
| 16 | 19 | { |
| 17 | - | |
| 18 | 20 | /** |
| 19 | - * The instance | |
| 20 | - * | |
| 21 | - * @var $instance | |
| 22 | - */ | |
| 23 | - public static $instance = null; | |
| 24 | - | |
| 25 | - /** | |
| 26 | 21 | * Adds various actions to set up the page |
| 27 | 22 | * |
| 28 | - * @return self|void | |
| 23 | + * @return void | |
| 29 | 24 | */ |
| 30 | 25 | public function __construct() |
| 31 | 26 | { |
| 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' ]); | |
| 27 | + \add_action('rest_api_init', [$this, 'registerUserMeta']); | |
| 28 | + \add_action('admin_enqueue_scripts', [$this, 'loadScripts']); | |
| 40 | 29 | } |
| 41 | 30 | |
| 42 | 31 | /** |
| 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 | 32 | * Adds scripts to the admin |
| 68 | 33 | * |
| 34 | + * @param string $hook - An optional hook provided by WP to identify the page. | |
| 69 | 35 | * @return void |
| 70 | 36 | */ |
| 71 | - public function loadScripts() | |
| 37 | + public function loadScripts($hook) | |
| 72 | 38 | { |
| 73 | - \add_action( | |
| 74 | - 'admin_enqueue_scripts', | |
| 75 | - function ($hook) { | |
| 76 | - if (!current_user_can(Config::$requiredCapability)) { | |
| 77 | - return; | |
| 78 | - } | |
| 39 | + if (!$this->isGutenbergEditor($hook)) { | |
| 40 | + return; | |
| 41 | + } | |
| 79 | 42 | |
| 80 | - if (!$this->checkItsGutenbergPost($hook)) { | |
| 81 | - return; | |
| 82 | - } | |
| 83 | - | |
| 84 | - if (!$this->isLibraryEnabled()) { | |
| 85 | - return; | |
| 86 | - } | |
| 87 | - | |
| 88 | - $this->addScopedScriptsAndStyles(); | |
| 89 | - } | |
| 90 | - ); | |
| 43 | + $this->addScopedScriptsAndStyles(); | |
| 91 | 44 | } |
| 92 | 45 | |
| 93 | 46 | /** |
| 94 | 47 | * Makes sure we are on the correct page |
| @@ -95,9 +48,9 @@ | ||
| 95 | 48 | * |
| 96 | 49 | * @param string $hook - An optional hook provided by WP to identify the page. |
| 97 | 50 | * @return boolean |
| 98 | 51 | */ |
| 99 | - public function checkItsGutenbergPost($hook = '') | |
| 52 | + public function isGutenbergEditor($hook = '') | |
| 100 | 53 | { |
| 101 | 54 | // Check for the post type, or on the FSE page. |
| 102 | 55 | $type = isset($GLOBALS['typenow']) ? $GLOBALS['typenow'] : ''; |
| 103 | 56 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| @@ -106,88 +59,94 @@ | ||
| 106 | 59 | $type = sanitize_text_field(wp_unslash($_GET['postType'])); |
| 107 | 60 | } |
| 108 | 61 | |
| 109 | 62 | 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); | |
| 63 | + return $hook && in_array($hook, ['post.php', 'post-new.php'], true); | |
| 111 | 64 | } |
| 112 | 65 | |
| 66 | + // Temporarily disable the library on the site editor page until the issues with 6.3 are fixed. | |
| 113 | 67 | return false; |
| 114 | 68 | } |
| 115 | 69 | |
| 116 | 70 | /** |
| 117 | - * Adds various JS scripts | |
| 71 | + * Adds various scripts and styles | |
| 118 | 72 | * |
| 119 | 73 | * @return void |
| 120 | 74 | */ |
| 121 | 75 | public function addScopedScriptsAndStyles() |
| 122 | 76 | { |
| 123 | - $version = Config::$environment === 'PRODUCTION' ? Config::$version : uniqid(); | |
| 77 | + $userInfo = \get_user_option('extendify_library_user'); | |
| 78 | + $userInfo = $userInfo ? json_decode($userInfo, true) : [ | |
| 79 | + 'state' => ['openOnNewPage' => Config::$partnerId && ! PartnerData::setting('disableLibraryAutoOpen')], | |
| 80 | + 'version' => 0, | |
| 81 | + ]; | |
| 82 | + $siteInfo = \get_option('extendify_library_site_data', [ | |
| 83 | + 'state' => [], | |
| 84 | + 'version' => 0, | |
| 85 | + ]); | |
| 86 | + $version = constant('EXTENDIFY_DEVMODE') ? uniqid() : Config::$version; | |
| 87 | + $scriptAssetPath = EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-library.php']; | |
| 88 | + $fallback = [ | |
| 89 | + 'dependencies' => [], | |
| 90 | + 'version' => $version, | |
| 91 | + ]; | |
| 92 | + $scriptAsset = file_exists($scriptAssetPath) ? require $scriptAssetPath : $fallback; | |
| 93 | + foreach ($scriptAsset['dependencies'] as $style) { | |
| 94 | + \wp_enqueue_style($style); | |
| 95 | + } | |
| 124 | 96 | |
| 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, | |
| 97 | + \wp_enqueue_script( | |
| 98 | + Config::$slug . 'library-scripts', | |
| 99 | + EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify-library.js'], | |
| 100 | + array_merge([Config::$slug . '-shared-scripts'], $scriptAsset['dependencies']), | |
| 101 | + $scriptAsset['version'], | |
| 135 | 102 | true |
| 136 | 103 | ); |
| 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 | - ] | |
| 104 | + \wp_add_inline_script( | |
| 105 | + Config::$slug . 'library-scripts', | |
| 106 | + 'window.extLibraryData = ' . \wp_json_encode([ | |
| 107 | + 'userInfo' => \wp_json_encode($userInfo), | |
| 108 | + 'siteInfo' => \wp_json_encode($siteInfo), | |
| 109 | + ]), | |
| 110 | + 'before' | |
| 150 | 111 | ); |
| 151 | - \wp_enqueue_script(Config::$slug . '-scripts'); | |
| 112 | + \wp_set_script_translations( | |
| 113 | + Config::$slug . 'library-scripts', | |
| 114 | + 'extendify-local', | |
| 115 | + EXTENDIFY_PATH . 'languages/js' | |
| 116 | + ); | |
| 152 | 117 | |
| 153 | - \wp_set_script_translations(Config::$slug . '-scripts', 'extendify'); | |
| 154 | - | |
| 155 | 118 | // Inline the library styles to keep them out of the iframe live preview. |
| 156 | 119 | // 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); | |
| 120 | + $css = file_get_contents( | |
| 121 | + EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-library.css'] | |
| 122 | + ); | |
| 123 | + \wp_register_style(Config::$slug, false, [], Config::$version); | |
| 159 | 124 | \wp_enqueue_style(Config::$slug); |
| 160 | 125 | \wp_add_inline_style(Config::$slug, $css); |
| 161 | 126 | } |
| 162 | 127 | |
| 163 | 128 | /** |
| 164 | - * Check if current user is Admin | |
| 129 | + * Adds user meta to the user profile | |
| 165 | 130 | * |
| 166 | - * @return Boolean | |
| 131 | + * @return void | |
| 167 | 132 | */ |
| 168 | - private function isAdmin() | |
| 133 | + public function registerUserMeta() | |
| 169 | 134 | { |
| 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; | |
| 135 | + register_rest_field( | |
| 136 | + 'user', | |
| 137 | + 'extendify_library_user', | |
| 138 | + [ | |
| 139 | + 'get_callback' => function ($user) { | |
| 140 | + return \get_user_option('extendify_library_user', $user['id']); | |
| 141 | + }, | |
| 142 | + 'update_callback' => function ($value, $user) { | |
| 143 | + return \update_user_option($user->ID, 'extendify_library_user', $value); | |
| 144 | + }, | |
| 145 | + 'schema' => [ | |
| 146 | + 'description' => __('Extendify Library User Settings', 'extendify-local'), | |
| 147 | + 'type' => 'string', | |
| 148 | + ], | |
| 149 | + ] | |
| 150 | + ); | |
| 192 | 151 | } |
| 193 | 152 | } |