PluginProbe
Extendify / 2.2.0
Extendify v2.2.0
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / app / Library / Admin.php

Admin.php in Extendify 2.2.0, at app/Library/Admin.php

153 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Admin.
5 */
6
7 namespace Extendify\Library;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\Config;
12 use Extendify\PartnerData;
13
14 /**
15 * This class handles any file loading for the admin area.
16 */
17
18 class Admin
19 {
20 /**
21 * Adds various actions to set up the page
22 *
23 * @return void
24 */
25 public function __construct()
26 {
27 \add_action('rest_api_init', [$this, 'registerUserMeta']);
28 \add_action('admin_enqueue_scripts', [$this, 'loadScripts']);
29 }
30
31 /**
32 * Adds scripts to the admin
33 *
34 * @param string $hook - An optional hook provided by WP to identify the page.
35 * @return void
36 */
37 public function loadScripts($hook)
38 {
39 if (!$this->isGutenbergEditor($hook)) {
40 return;
41 }
42
43 $this->addScopedScriptsAndStyles();
44 }
45
46 /**
47 * Makes sure we are on the correct page
48 *
49 * @param string $hook - An optional hook provided by WP to identify the page.
50 * @return boolean
51 */
52 public function isGutenbergEditor($hook = '')
53 {
54 // Check for the post type, or on the FSE page.
55 $type = isset($GLOBALS['typenow']) ? $GLOBALS['typenow'] : '';
56 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
57 if (!$type && isset($_GET['postType'])) {
58 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
59 $type = sanitize_text_field(wp_unslash($_GET['postType']));
60 }
61
62 if (\use_block_editor_for_post_type($type)) {
63 return $hook && in_array($hook, ['post.php', 'post-new.php'], true);
64 }
65
66 // Temporarily disable the library on the site editor page until the issues with 6.3 are fixed.
67 return false;
68 }
69
70 /**
71 * Adds various scripts and styles
72 *
73 * @return void
74 */
75 public function addScopedScriptsAndStyles()
76 {
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' => ['siteType' => \get_option('extendify_siteType', new \stdClass())],
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 }
96
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'],
102 true
103 );
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'
111 );
112 \wp_set_script_translations(
113 Config::$slug . 'library-scripts',
114 'extendify-local',
115 EXTENDIFY_PATH . 'languages/js'
116 );
117
118 // Inline the library styles to keep them out of the iframe live preview.
119 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
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);
124 \wp_enqueue_style(Config::$slug);
125 \wp_add_inline_style(Config::$slug, $css);
126 }
127
128 /**
129 * Adds user meta to the user profile
130 *
131 * @return void
132 */
133 public function registerUserMeta()
134 {
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 );
151 }
152 }
153