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 / class-plugin.php

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

434 lines 11.1 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\Cloud\Cloud_API;
6 use Code_Snippets\REST_API\Snippets_REST_Controller;
7 use Evaluation\Evaluate_Content;
8 use Evaluation\Evaluate_Functions;
9
10 /**
11 * The main plugin class
12 *
13 * @package Code_Snippets
14 */
15 class Plugin {
16
17 /**
18 * Current plugin version number
19 *
20 * @var string
21 */
22 public string $version;
23
24 /**
25 * Filesystem path to the main plugin file
26 *
27 * @var string
28 */
29 public string $file;
30
31 /**
32 * Database class
33 *
34 * @var DB
35 */
36 public DB $db;
37
38 /**
39 * Class for evaluating function snippets.
40 *
41 * @var Evaluate_Functions
42 */
43 public Evaluate_Functions $evaluate_functions;
44
45 /**
46 * Class for evaluating content snippets.
47 *
48 * @var Evaluate_Content
49 */
50 public Evaluate_Content $evaluate_content;
51
52 /**
53 * Administration area class
54 *
55 * @var Admin
56 */
57 public Admin $admin;
58
59 /**
60 * Front-end functionality class
61 *
62 * @var Front_End
63 */
64 public Front_End $front_end;
65
66 /**
67 * Class for managing cloud API actions.
68 *
69 * @var Cloud_API
70 */
71 public Cloud_API $cloud_api;
72
73 /**
74 * Handles licensing and plugin updates.
75 *
76 * @var Licensing
77 */
78 public Licensing $licensing;
79
80 /**
81 * Handles snippet handler registration.
82 *
83 * @var Snippet_Handler_Registry
84 */
85 public Snippet_Handler_Registry $snippet_handler_registry;
86
87 /**
88 * Class constructor
89 *
90 * @param string $version Current plugin version.
91 * @param string $file Path to main plugin file.
92 */
93 public function __construct( string $version, string $file ) {
94 $this->version = $version;
95 $this->file = $file;
96
97 wp_cache_add_global_groups( CACHE_GROUP );
98
99 add_filter( 'code_snippets/execute_snippets', array( $this, 'disable_snippet_execution' ), 5 );
100
101 if ( isset( $_REQUEST['snippets-safe-mode'] ) ) {
102 add_filter( 'home_url', array( $this, 'add_safe_mode_query_var' ) );
103 add_filter( 'admin_url', array( $this, 'add_safe_mode_query_var' ) );
104 }
105
106 add_action( 'rest_api_init', [ $this, 'init_rest_api' ] );
107 add_action( 'allowed_redirect_hosts', [ $this, 'allow_code_snippets_redirect' ] );
108 }
109
110 /**
111 * Initialise classes and include files
112 */
113 public function load_plugin() {
114 $includes_path = __DIR__;
115
116 // Database operation functions.
117 $this->db = new DB();
118
119 // Snippet operation functions.
120 require_once $includes_path . '/snippet-ops.php';
121 $this->evaluate_content = new Evaluate_Content( $this->db );
122 $this->evaluate_functions = new Evaluate_Functions( $this->db );
123
124 // CodeMirror editor functions.
125 require_once $includes_path . '/editor.php';
126
127 // General Administration functions.
128 if ( is_admin() ) {
129 $this->admin = new Admin();
130 }
131
132 // Settings component.
133 require_once $includes_path . '/settings/settings-fields.php';
134 require_once $includes_path . '/settings/editor-preview.php';
135 require_once $includes_path . '/settings/class-version-switch.php';
136 require_once $includes_path . '/settings/settings.php';
137
138 // Cloud List Table shared functions.
139 require_once $includes_path . '/cloud/list-table-shared-ops.php';
140
141 // Snippet files.
142 $this->snippet_handler_registry = new Snippet_Handler_Registry( [
143 'php' => new Php_Snippet_Handler(),
144 'html' => new Html_Snippet_Handler(),
145 ] );
146
147 $fs = new WordPress_File_System_Adapter();
148
149 $config_repo = new Snippet_Config_Repository( $fs );
150
151 ( new Snippet_Files( $this->snippet_handler_registry, $fs, $config_repo ) )->register_hooks();
152
153 $this->front_end = new Front_End();
154 $this->cloud_api = new Cloud_API();
155
156 $upgrade = new Upgrade( $this->version, $this->db );
157 add_action( 'plugins_loaded', array( $upgrade, 'run' ), 0 );
158 $this->licensing = new Licensing();
159
160 // Importers.
161 new Plugins_Import_Manager();
162 new Files_Import_Manager();
163 }
164
165 /**
166 * Register custom REST API controllers.
167 *
168 * @return void
169 */
170 public function init_rest_api() {
171 $snippets_controller = new Snippets_REST_Controller();
172 $snippets_controller->register_routes();
173 }
174
175 /**
176 * Disable snippet execution if the necessary query var is set.
177 *
178 * @param bool $execute_snippets Current filter value.
179 *
180 * @return bool New filter value.
181 */
182 public function disable_snippet_execution( bool $execute_snippets ): bool {
183 return ! empty( $_REQUEST['snippets-safe-mode'] ) && $this->current_user_can() ? false : $execute_snippets;
184 }
185
186 /**
187 * Determine whether the menu is full or compact.
188 *
189 * @return bool
190 */
191 public function is_compact_menu(): bool {
192 return ! is_network_admin() && apply_filters( 'code_snippets_compact_menu', false );
193 }
194
195 /**
196 * Fetch the admin menu slug for a menu.
197 *
198 * @param string $menu Name of menu to retrieve the slug for.
199 *
200 * @return string The menu's slug.
201 */
202 public function get_menu_slug( string $menu = '' ): string {
203 $add = array( 'single', 'add', 'add-new', 'add-snippet', 'new-snippet', 'add-new-snippet' );
204 $edit = array( 'edit', 'edit-snippet' );
205 $import = array( 'import', 'import-snippets', 'import-code-snippets' );
206 $settings = array( 'settings', 'snippets-settings' );
207 $cloud = array( 'cloud', 'cloud-snippets' );
208 $welcome = array( 'welcome', 'getting-started', 'code-snippets' );
209
210 if ( in_array( $menu, $edit, true ) ) {
211 return 'edit-snippet';
212 } elseif ( in_array( $menu, $add, true ) ) {
213 return 'add-snippet';
214 } elseif ( in_array( $menu, $import, true ) ) {
215 return 'import-code-snippets';
216 } elseif ( in_array( $menu, $settings, true ) ) {
217 return 'snippets-settings';
218 } elseif ( in_array( $menu, $cloud, true ) ) {
219 return 'snippets&type=cloud';
220 } elseif ( in_array( $menu, $welcome, true ) ) {
221 return 'code-snippets-welcome';
222 } else {
223 return 'snippets';
224 }
225 }
226
227 /**
228 * Fetch the URL to a snippets admin menu.
229 *
230 * @param string $menu Name of menu to retrieve the URL to.
231 * @param string $context URL scheme to use.
232 *
233 * @return string The menu's URL.
234 */
235 public function get_menu_url( string $menu = '', string $context = 'self' ): string {
236 $slug = $this->get_menu_slug( $menu );
237
238 if ( $this->is_compact_menu() && 'network' !== $context ) {
239 $base_slug = $this->get_menu_slug();
240 $url = 'tools.php?page=' . $base_slug;
241
242 if ( $slug !== $base_slug ) {
243 $url .= '&sub=' . $slug;
244 }
245 } else {
246 $url = 'admin.php?page=' . $slug;
247 }
248
249 if ( 'network' === $context ) {
250 return network_admin_url( $url );
251 } elseif ( 'admin' === $context ) {
252 return admin_url( $url );
253 } else {
254 return self_admin_url( $url );
255 }
256 }
257
258 /**
259 * Fetch the admin menu slug for a snippets admin menu.
260 *
261 * @param integer $snippet_id Snippet ID.
262 * @param string $context URL scheme to use.
263 *
264 * @return string The URL to the edit snippet page for that snippet.
265 */
266 public function get_snippet_edit_url( int $snippet_id, string $context = 'self' ): string {
267 return add_query_arg(
268 'id',
269 absint( $snippet_id ),
270 $this->get_menu_url( 'edit', $context )
271 );
272 }
273
274 /**
275 * Allow redirecting to the Code Snippets site.
276 *
277 * @param array<string> $hosts Allowed hosts.
278 *
279 * @return array Modified allowed hosts.
280 */
281 public function allow_code_snippets_redirect( array $hosts ): array {
282 $hosts[] = 'codesnippets.pro';
283 $hosts[] = 'snipco.de';
284 return $hosts;
285 }
286
287 /**
288 * Determine whether the current user can perform actions on snippets.
289 *
290 * @return boolean Whether the current user has the required capability.
291 *
292 * @since 2.8.6
293 */
294 public function current_user_can(): bool {
295 return current_user_can( $this->get_cap() );
296 }
297
298 /**
299 * Retrieve the name of the capability required to manage sub-site snippets.
300 *
301 * @return string
302 */
303 public function get_cap_name(): string {
304 return apply_filters( 'code_snippets_cap', 'manage_options' );
305 }
306
307 /**
308 * Retrieve the name of the capability required to manage network snippets.
309 *
310 * @return string
311 */
312 public function get_network_cap_name(): string {
313 return apply_filters( 'code_snippets_network_cap', 'manage_network_options' );
314 }
315
316 /**
317 * Determine if a subsite user menu is enabled via *Network Settings > Enable administration menus*.
318 *
319 * @return bool
320 */
321 public function is_subsite_menu_enabled(): bool {
322 if ( ! is_multisite() ) {
323 return true;
324 }
325
326 $menu_perms = get_site_option( 'menu_items', array() );
327 return ! empty( $menu_perms['snippets'] );
328 }
329
330 /**
331 * Determine if the current user should have the network snippets capability.
332 *
333 * @return bool
334 */
335 public function user_can_manage_network_snippets(): bool {
336 return is_super_admin() || current_user_can( $this->get_network_cap_name() );
337 }
338
339 /**
340 * Determine whether the current request originates in the network admin.
341 *
342 * @return bool
343 */
344 public function is_network_context(): bool {
345 return is_network_admin();
346 }
347
348 /**
349 * Get the required capability to perform a certain action on snippets.
350 * Does not check if the user has this capability or not.
351 *
352 * If multisite, adjusts the capability based on whether the user is viewing
353 * the network dashboard or a subsite and whether the menu is enabled for subsites.
354 *
355 * @return string The capability required to manage snippets.
356 *
357 * @since 2.0
358 */
359 public function get_cap(): string {
360 if ( is_multisite() && $this->is_network_context() ) {
361 return $this->get_network_cap_name();
362 }
363
364 if ( is_multisite() && ! $this->is_subsite_menu_enabled() ) {
365 return $this->get_network_cap_name();
366 }
367
368 return $this->get_cap_name();
369 }
370
371 /**
372 * Inject the safe mode query var into URLs
373 *
374 * @param string $url Original URL.
375 *
376 * @return string Modified URL.
377 */
378 public function add_safe_mode_query_var( string $url ): string {
379 return isset( $_REQUEST['snippets-safe-mode'] ) ?
380 add_query_arg( 'snippets-safe-mode', (bool) $_REQUEST['snippets-safe-mode'], $url ) :
381 $url;
382 }
383
384 /**
385 * Retrieve a list of available snippet types and their labels.
386 *
387 * @return array<string, string> Snippet types.
388 */
389 public static function get_types(): array {
390 return apply_filters(
391 'code_snippets_types',
392 array(
393 'php' => __( 'Functions', 'code-snippets' ),
394 'html' => __( 'Content', 'code-snippets' ),
395 'css' => __( 'Styles', 'code-snippets' ),
396 'js' => __( 'Scripts', 'code-snippets' ),
397 'cloud' => __( 'Codevault', 'code-snippets' ),
398 'cloud_search' => __( 'Cloud Search', 'code-snippets' ),
399 'bundles' => __( 'Bundles', 'code-snippets' ),
400 )
401 );
402 }
403
404 /**
405 * Localise a plugin script to provide the CODE_SNIPPETS object.
406 *
407 * @param string $handle Script handle.
408 *
409 * @return void
410 */
411 public function localize_script( string $handle ) {
412 wp_localize_script(
413 $handle,
414 'CODE_SNIPPETS',
415 [
416 'isLicensed' => $this->licensing->is_licensed(),
417 'isCloudConnected' => Cloud_API::is_cloud_connection_available(),
418 'restAPI' => [
419 'base' => esc_url_raw( rest_url() ),
420 'snippets' => esc_url_raw( rest_url( Snippets_REST_Controller::get_base_route() ) ),
421 'nonce' => wp_create_nonce( 'wp_rest' ),
422 'localToken' => $this->cloud_api->get_local_token(),
423 ],
424 'urls' => [
425 'plugin' => esc_url_raw( plugins_url( '', PLUGIN_FILE ) ),
426 'manage' => esc_url_raw( $this->get_menu_url() ),
427 'edit' => esc_url_raw( $this->get_menu_url( 'edit' ) ),
428 'addNew' => esc_url_raw( $this->get_menu_url( 'add' ) ),
429 ],
430 ]
431 );
432 }
433 }
434