PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / Plugin.php

Plugin.php in Code Snippets 4.0.0-beta.1, at php/Plugin.php

372 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets;
4
5 use Code_Snippets\Admin\Bootstrap_Admin;
6 use Code_Snippets\Client\Cloud_API;
7 use Code_Snippets\Core\DB;
8 use Code_Snippets\Core\Licensing;
9 use Code_Snippets\Core\Upgrader;
10 use Code_Snippets\Integration\Admin_Bar;
11 use Code_Snippets\Integration\Classic_Editor\MCE_Plugin;
12 use Code_Snippets\Integration\Evaluate_Content;
13 use Code_Snippets\Integration\Evaluate_Functions;
14 use Code_Snippets\Integration\Promotions\Promotion_Manager;
15 use Code_Snippets\Integration\Shortcodes;
16 use Code_Snippets\REST_API\Cloud\Cloud_Snippets_REST_Controller;
17 use Code_Snippets\REST_API\Import\File_Import_REST_Controller;
18 use Code_Snippets\REST_API\Import\Plugins_Import_REST_Controller;
19 use Code_Snippets\REST_API\Snippets\Recently_Active_REST_Controller;
20 use Code_Snippets\REST_API\Snippets\Snippets_REST_Controller;
21
22 /**
23 * The main plugin class
24 *
25 * @package Code_Snippets
26 */
27 class Plugin {
28
29 /**
30 * Current plugin version number
31 *
32 * @deprecated Use the PLUGIN_VERSION constant instead.
33 *
34 * @var string
35 */
36 public string $version = PLUGIN_VERSION;
37
38 /**
39 * Filesystem path to the main plugin file
40 *
41 * @deprecated Use the PLUGIN_FILE constant instead.
42 *
43 * @var string
44 */
45 public string $file = PLUGIN_FILE;
46
47 /**
48 * Database class
49 *
50 * @var DB
51 */
52 public DB $db;
53
54 /**
55 * Class for evaluating function snippets.
56 *
57 * @var Evaluate_Functions
58 */
59 public Evaluate_Functions $evaluate_functions;
60
61 /**
62 * Class for evaluating content snippets.
63 *
64 * @var Evaluate_Content
65 */
66 public Evaluate_Content $evaluate_content;
67
68 /**
69 * Administration area class
70 *
71 * @var Bootstrap_Admin
72 */
73 public Bootstrap_Admin $admin;
74
75 /**
76 * Class for managing cloud API actions.
77 *
78 * @var Cloud_API
79 */
80 public Cloud_API $cloud_api;
81
82 /**
83 * Handles licensing and plugin updates.
84 *
85 * @var Licensing
86 */
87 public Licensing $licensing;
88
89 /**
90 * Handles snippet handler registration.
91 *
92 * @var Flat_Files\Handler_Registry
93 */
94 public Flat_Files\Handler_Registry $snippet_handler_registry;
95
96 /**
97 * Class constructor
98 */
99 public function __construct() {
100 wp_cache_add_global_groups( CACHE_GROUP );
101 add_action( 'allowed_redirect_hosts', [ $this, 'allow_code_snippets_redirect' ] );
102 }
103
104 /**
105 * Load the plugin utilities.
106 */
107 private function load_utilities() {
108 require_once __DIR__ . '/snippet-ops.php';
109 require_once __DIR__ . '/Utils/editor.php';
110 require_once __DIR__ . '/Utils/options.php';
111 require_once __DIR__ . '/Settings/settings.php';
112 }
113
114 /**
115 * Initialise the plugin and load all necessary classes.
116 */
117 public function load_plugin() {
118 $this->load_utilities();
119
120 $this->db = new DB();
121 $this->cloud_api = new Cloud_API();
122 $this->licensing = new Licensing();
123 $this->evaluate_content = new Evaluate_Content( $this->db );
124 $this->evaluate_functions = new Evaluate_Functions( $this->db );
125
126 if ( is_admin() ) {
127 $this->admin = new Bootstrap_Admin();
128 }
129
130 new Shortcodes();
131 new MCE_Plugin();
132 new Upgrader( PLUGIN_VERSION, $this->db );
133 new Admin_Bar();
134
135 if ( is_admin() ) {
136 new Promotion_Manager();
137 }
138
139 $this->init_snippet_files();
140
141 add_action( 'rest_api_init', [ $this, 'init_rest_api' ], 1 );
142 }
143
144 /**
145 * Load REST API controller classes when serving REST API requests.
146 *
147 * @return void
148 */
149 public function init_rest_api() {
150 new Snippets_REST_Controller();
151 new Recently_Active_REST_Controller();
152 new Plugins_Import_REST_Controller();
153 new File_Import_REST_Controller();
154 new Cloud_Snippets_REST_Controller( $this->cloud_api );
155 }
156
157 /**
158 * Initialises the snippet files component.
159 *
160 * @return void
161 */
162 private function init_snippet_files() {
163 $this->snippet_handler_registry = new Flat_Files\Handler_Registry(
164 [
165 'php' => new Flat_Files\Handlers\Functions_Snippet_Handler(),
166 'html' => new Flat_Files\Handlers\Content_Snippet_Handler(),
167 ]
168 );
169
170 $fs = new Flat_Files\WP_Filesystem_Adapter();
171 $config_repo = new Flat_Files\Flat_File_Config_Repository( $fs );
172
173 $snippet_files = new Flat_Files\Snippet_Files( $this->snippet_handler_registry, $fs, $config_repo );
174 $snippet_files->register_hooks();
175 }
176
177 /**
178 * Determine whether the menu is full or compact.
179 *
180 * @return bool
181 */
182 public function is_compact_menu(): bool {
183 return ! is_network_admin() && apply_filters( 'code_snippets_compact_menu', false );
184 }
185
186 /**
187 * Fetch the admin menu slug for a menu.
188 *
189 * @param string $menu Name of menu to retrieve the slug for.
190 *
191 * @return string The menu's slug.
192 */
193 public function get_menu_slug( string $menu = '' ): string {
194 $add = [ 'single', 'add', 'add-new', 'add-snippet', 'new-snippet', 'add-new-snippet' ];
195 $edit = [ 'edit', 'edit-snippet' ];
196 $import = [ 'import', 'import-snippets', 'import-code-snippets' ];
197 $settings = [ 'settings', 'snippets-settings' ];
198 $cloud = [ 'cloud', 'cloud-snippets' ];
199 $welcome = [ 'welcome', 'getting-started', 'code-snippets' ];
200
201 if ( in_array( $menu, $edit, true ) ) {
202 return 'edit-snippet';
203 } elseif ( in_array( $menu, $add, true ) ) {
204 return 'add-snippet';
205 } elseif ( in_array( $menu, $import, true ) ) {
206 return 'import-code-snippets';
207 } elseif ( in_array( $menu, $settings, true ) ) {
208 return 'snippets-settings';
209 } elseif ( in_array( $menu, $cloud, true ) ) {
210 return 'snippets&subpage=cloud-community';
211 } elseif ( in_array( $menu, $welcome, true ) ) {
212 return 'code-snippets-welcome';
213 } else {
214 return 'snippets';
215 }
216 }
217
218 /**
219 * Fetch the URL to a snippets admin menu.
220 *
221 * @param string $menu Name of menu to retrieve the URL to.
222 * @param string $context URL scheme to use.
223 *
224 * @return string The menu's URL.
225 */
226 public function get_menu_url( string $menu = '', string $context = 'self' ): string {
227 $slug = $this->get_menu_slug( $menu );
228
229 if ( $this->is_compact_menu() && 'network' !== $context ) {
230 $base_slug = $this->get_menu_slug();
231 $url = 'tools.php?page=' . $base_slug;
232
233 if ( $slug !== $base_slug ) {
234 $url .= '&sub=' . $slug;
235 }
236 } else {
237 $url = 'admin.php?page=' . $slug;
238 }
239
240 if ( 'network' === $context ) {
241 return network_admin_url( $url );
242 } elseif ( 'admin' === $context ) {
243 return admin_url( $url );
244 } else {
245 return self_admin_url( $url );
246 }
247 }
248
249 /**
250 * Allow redirecting to the Code Snippets site.
251 *
252 * @param array<string> $hosts Allowed hosts.
253 *
254 * @return array Modified allowed hosts.
255 */
256 public function allow_code_snippets_redirect( array $hosts ): array {
257 $hosts[] = 'codesnippets.pro';
258 $hosts[] = 'snipco.de';
259 return $hosts;
260 }
261
262 /**
263 * Determine whether the current user can perform actions on snippets.
264 *
265 * @return bool Whether the current user has the required capability.
266 *
267 * @since 2.8.6
268 */
269 public function current_user_can(): bool {
270 return current_user_can( $this->get_cap() );
271 }
272
273 /**
274 * Retrieve the name of the capability required to manage sub-site snippets.
275 *
276 * @return string
277 */
278 public function get_cap_name(): string {
279 return apply_filters( 'code_snippets_cap', 'manage_options' );
280 }
281
282 /**
283 * Retrieve the name of the capability required to manage network snippets.
284 *
285 * @return string
286 */
287 public function get_network_cap_name(): string {
288 return apply_filters( 'code_snippets_network_cap', 'manage_network_options' );
289 }
290
291 /**
292 * Determine if a subsite user menu is enabled via *Network Settings > Enable administration menus*.
293 *
294 * @return bool
295 */
296 public function is_subsite_menu_enabled(): bool {
297 if ( ! is_multisite() ) {
298 return true;
299 }
300
301 $menu_perms = get_site_option( 'menu_items', array() );
302 return ! empty( $menu_perms['snippets'] );
303 }
304
305 /**
306 * Determine if the current user should have the network snippets capability.
307 *
308 * @return bool
309 */
310 public function user_can_manage_network_snippets(): bool {
311 return is_super_admin() || current_user_can( $this->get_network_cap_name() );
312 }
313
314 /**
315 * Get the required capability to perform a certain action on snippets.
316 * Does not check if the user has this capability or not.
317 *
318 * If multisite, adjusts the capability based on whether the user is viewing
319 * the network dashboard or a subsite and whether the menu is enabled for subsites.
320 *
321 * @return string The capability required to manage snippets.
322 *
323 * @since 2.0
324 */
325 public function get_cap(): string {
326 return is_multisite() && ( is_network_admin() || ! $this->is_subsite_menu_enabled() )
327 ? $this->get_network_cap_name()
328 : $this->get_cap_name();
329 }
330
331 /**
332 * Localise a plugin script to provide the CODE_SNIPPETS object.
333 *
334 * @param string $handle Script handle.
335 *
336 * @return void
337 */
338 public function localize_script( string $handle ) {
339 wp_localize_script(
340 $handle,
341 'CODE_SNIPPETS',
342 [
343 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
344 'isLicensed' => $this->licensing->is_licensed(),
345 'isCloudConnected' => $this->cloud_api->is_cloud_connection_available(),
346 'hideUpsell' => Settings\get_setting( 'general', 'hide_upgrade_menu' ),
347 'restAPI' => [
348 'base' => esc_url_raw( rest_url() ),
349 'snippets' => esc_url_raw( rest_url( Snippets_REST_Controller::get_base_route() ) ),
350 'recentlyActive' => esc_url_raw( rest_url( Recently_Active_REST_Controller::get_base_route() ) ),
351 'importPlugins' => esc_url_raw( rest_url( Plugins_Import_REST_Controller::get_base_route() ) ),
352 'importFiles' => esc_url_raw( rest_url( File_Import_REST_Controller::get_base_route() ) ),
353 'nonce' => wp_create_nonce( 'wp_rest' ),
354 'cloud' => [
355 'token' => $this->cloud_api->get_local_token(),
356 'snippets' => esc_url_raw( rest_url( Cloud_Snippets_REST_Controller::get_base_route() ) ),
357 ],
358 ],
359 'urls' => [
360 'plugin' => esc_url_raw( plugins_url( '', PLUGIN_FILE ) ),
361 'manage' => esc_url_raw( $this->get_menu_url() ),
362 'edit' => esc_url_raw( $this->get_menu_url( 'edit' ) ),
363 'addNew' => esc_url_raw( $this->get_menu_url( 'add' ) ),
364 'welcome' => esc_url_raw( $this->get_menu_url( 'welcome' ) ),
365 'settings' => esc_url_raw( $this->get_menu_url( 'settings' ) ),
366 'cloud' => esc_url_raw( $this->cloud_api->get_cloud_url() ),
367 ],
368 ]
369 );
370 }
371 }
372