PluginProbe
Extendify / 0.5.0
Extendify v0.5.0
3.2.1 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 All 127 releases
extendify / app / Admin.php

Admin.php in Extendify 0.5.0, at app/Admin.php

178 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 * Admin.
4 */
5
6 namespace Extendify\Library;
7
8 use Extendify\Library\App;
9 use Extendify\Library\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 $theme = get_option('template');
51 $label = esc_html__('Upgrade', 'extendify');
52
53 $links['upgrade'] = sprintf('<a href="%1$s" target="_blank"><b>%2$s</b></a>', "https://extendify.com/pricing?utm_source=extendify-plugin&utm_medium=wp-dash&utm_campaign=action-link&utm_content=$label&utm_term=$theme", $label);
54
55 return $links;
56 }
57
58 /**
59 * Adds scripts to the admin
60 *
61 * @return void
62 */
63 public function loadScripts()
64 {
65 \add_action(
66 'admin_enqueue_scripts',
67 function ($hook) {
68 if (!current_user_can(App::$requiredCapability)) {
69 return;
70 }
71
72 if (!$this->checkItsGutenbergPost($hook)) {
73 return;
74 }
75
76 if (!$this->isLibraryEnabled()) {
77 return;
78 }
79
80 $this->addScopedScriptsAndStyles();
81 }
82 );
83 }
84
85 /**
86 * Makes sure we are on the correct page
87 *
88 * @param string $hook - An optional hook provided by WP to identify the page.
89 * @return boolean
90 */
91 public function checkItsGutenbergPost($hook = '')
92 {
93 if (isset($GLOBALS['typenow']) && \use_block_editor_for_post_type($GLOBALS['typenow'])) {
94 return $hook && in_array($hook, ['post.php', 'post-new.php'], true);
95 }
96
97 return false;
98 }
99
100 /**
101 * Adds various JS scripts
102 *
103 * @return void
104 */
105 public function addScopedScriptsAndStyles()
106 {
107 $version = App::$environment === 'PRODUCTION' ? App::$version : uniqid();
108
109 \wp_register_script(
110 App::$slug . '-scripts',
111 EXTENDIFY_BASE_URL . 'public/build/extendify.js',
112 [
113 'wp-i18n',
114 'wp-components',
115 'wp-element',
116 'wp-editor',
117 ],
118 $version,
119 true
120 );
121 \wp_localize_script(
122 App::$slug . '-scripts',
123 'extendifyData',
124 [
125 'root' => \esc_url_raw(rest_url(APP::$slug . '/' . APP::$apiVersion)),
126 'nonce' => \wp_create_nonce('wp_rest'),
127 'user' => json_decode(User::data('extendifysdk_user_data'), true),
128 'sitesettings' => json_decode(SiteSettings::data()),
129 'sdk_partner' => \esc_attr(APP::$sdkPartner),
130 'asset_path' => \esc_url(EXTENDIFY_URL . 'public/assets'),
131 'standalone' => \esc_attr(APP::$standalone),
132 'devbuild' => \esc_attr(APP::$environment === 'DEVELOPMENT'),
133 ]
134 );
135 \wp_enqueue_script(App::$slug . '-scripts');
136
137 \wp_set_script_translations(App::$slug . '-scripts', 'extendify');
138
139 // Inline the library styles to keep them out of the iframe live preview.
140 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
141 $css = file_get_contents(EXTENDIFY_PATH . 'public/build/extendify.css');
142 \wp_register_style(App::$slug, false, [], $version);
143 \wp_enqueue_style(App::$slug);
144 \wp_add_inline_style(App::$slug, $css);
145 }
146
147 /**
148 * Check if current user is Admin
149 *
150 * @return Boolean
151 */
152 private function isAdmin()
153 {
154 if (\is_multisite()) {
155 return \is_super_admin();
156 }
157
158 return in_array('administrator', \wp_get_current_user()->roles, true);
159 }
160
161 /**
162 * Check if scripts should add
163 *
164 * @return Boolean
165 */
166 public function isLibraryEnabled()
167 {
168 $settings = json_decode(SiteSettings::data());
169
170 // If it's disabled, only show it for admins.
171 if (isset($settings->state) && (isset($settings->state->enabled)) && !$settings->state->enabled) {
172 return $this->isAdmin();
173 }
174
175 return true;
176 }
177 }
178