PluginProbe
Code Snippets / 3.9.1
Code Snippets v3.9.1
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 3.0.1 All 64 releases
code-snippets / php / class-plugin.php

class-plugin.php in Code Snippets 3.9.1, at php/class-plugin.php

426 lines 10.9 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
161 /**
162 * Register custom REST API controllers.
163 *
164 * @return void
165 */
166 public function init_rest_api() {
167 $snippets_controller = new Snippets_REST_Controller();
168 $snippets_controller->register_routes();
169 }
170
171 /**
172 * Disable snippet execution if the necessary query var is set.
173 *
174 * @param bool $execute_snippets Current filter value.
175 *
176 * @return bool New filter value.
177 */
178 public function disable_snippet_execution( bool $execute_snippets ): bool {
179 return ! empty( $_REQUEST['snippets-safe-mode'] ) && $this->current_user_can() ? false : $execute_snippets;
180 }
181
182 /**
183 * Determine whether the menu is full or compact.
184 *
185 * @return bool
186 */
187 public function is_compact_menu(): bool {
188 return ! is_network_admin() && apply_filters( 'code_snippets_compact_menu', false );
189 }
190
191 /**
192 * Fetch the admin menu slug for a menu.
193 *
194 * @param string $menu Name of menu to retrieve the slug for.
195 *
196 * @return string The menu's slug.
197 */
198 public function get_menu_slug( string $menu = '' ): string {
199 $add = array( 'single', 'add', 'add-new', 'add-snippet', 'new-snippet', 'add-new-snippet' );
200 $edit = array( 'edit', 'edit-snippet' );
201 $import = array( 'import', 'import-snippets', 'import-code-snippets' );
202 $settings = array( 'settings', 'snippets-settings' );
203 $cloud = array( 'cloud', 'cloud-snippets' );
204 $welcome = array( 'welcome', 'getting-started', 'code-snippets' );
205
206 if ( in_array( $menu, $edit, true ) ) {
207 return 'edit-snippet';
208 } elseif ( in_array( $menu, $add, true ) ) {
209 return 'add-snippet';
210 } elseif ( in_array( $menu, $import, true ) ) {
211 return 'import-code-snippets';
212 } elseif ( in_array( $menu, $settings, true ) ) {
213 return 'snippets-settings';
214 } elseif ( in_array( $menu, $cloud, true ) ) {
215 return 'snippets&type=cloud';
216 } elseif ( in_array( $menu, $welcome, true ) ) {
217 return 'code-snippets-welcome';
218 } else {
219 return 'snippets';
220 }
221 }
222
223 /**
224 * Fetch the URL to a snippets admin menu.
225 *
226 * @param string $menu Name of menu to retrieve the URL to.
227 * @param string $context URL scheme to use.
228 *
229 * @return string The menu's URL.
230 */
231 public function get_menu_url( string $menu = '', string $context = 'self' ): string {
232 $slug = $this->get_menu_slug( $menu );
233
234 if ( $this->is_compact_menu() && 'network' !== $context ) {
235 $base_slug = $this->get_menu_slug();
236 $url = 'tools.php?page=' . $base_slug;
237
238 if ( $slug !== $base_slug ) {
239 $url .= '&sub=' . $slug;
240 }
241 } else {
242 $url = 'admin.php?page=' . $slug;
243 }
244
245 if ( 'network' === $context ) {
246 return network_admin_url( $url );
247 } elseif ( 'admin' === $context ) {
248 return admin_url( $url );
249 } else {
250 return self_admin_url( $url );
251 }
252 }
253
254 /**
255 * Fetch the admin menu slug for a snippets admin menu.
256 *
257 * @param integer $snippet_id Snippet ID.
258 * @param string $context URL scheme to use.
259 *
260 * @return string The URL to the edit snippet page for that snippet.
261 */
262 public function get_snippet_edit_url( int $snippet_id, string $context = 'self' ): string {
263 return add_query_arg(
264 'id',
265 absint( $snippet_id ),
266 $this->get_menu_url( 'edit', $context )
267 );
268 }
269
270 /**
271 * Allow redirecting to the Code Snippets site.
272 *
273 * @param array<string> $hosts Allowed hosts.
274 *
275 * @return array Modified allowed hosts.
276 */
277 public function allow_code_snippets_redirect( array $hosts ): array {
278 $hosts[] = 'codesnippets.pro';
279 $hosts[] = 'snipco.de';
280 return $hosts;
281 }
282
283 /**
284 * Determine whether the current user can perform actions on snippets.
285 *
286 * @return boolean Whether the current user has the required capability.
287 *
288 * @since 2.8.6
289 */
290 public function current_user_can(): bool {
291 return current_user_can( $this->get_cap() );
292 }
293
294 /**
295 * Retrieve the name of the capability required to manage sub-site snippets.
296 *
297 * @return string
298 */
299 public function get_cap_name(): string {
300 return apply_filters( 'code_snippets_cap', 'manage_options' );
301 }
302
303 /**
304 * Retrieve the name of the capability required to manage network snippets.
305 *
306 * @return string
307 */
308 public function get_network_cap_name(): string {
309 return apply_filters( 'code_snippets_network_cap', 'manage_network_options' );
310 }
311
312 /**
313 * Determine if a subsite user menu is enabled via *Network Settings > Enable administration menus*.
314 *
315 * @return bool
316 */
317 public function is_subsite_menu_enabled(): bool {
318 if ( ! is_multisite() ) {
319 return true;
320 }
321
322 $menu_perms = get_site_option( 'menu_items', array() );
323 return ! empty( $menu_perms['snippets'] );
324 }
325
326 /**
327 * Determine if the current user should have the network snippets capability.
328 *
329 * @return bool
330 */
331 public function user_can_manage_network_snippets(): bool {
332 return is_super_admin() || current_user_can( $this->get_network_cap_name() );
333 }
334
335 /**
336 * Determine whether the current request originates in the network admin.
337 *
338 * @return bool
339 */
340 public function is_network_context(): bool {
341 return is_network_admin();
342 }
343
344 /**
345 * Get the required capability to perform a certain action on snippets.
346 * Does not check if the user has this capability or not.
347 *
348 * If multisite, adjusts the capability based on whether the user is viewing
349 * the network dashboard or a subsite and whether the menu is enabled for subsites.
350 *
351 * @return string The capability required to manage snippets.
352 *
353 * @since 2.0
354 */
355 public function get_cap(): string {
356 if ( is_multisite() && $this->is_network_context() ) {
357 return $this->get_network_cap_name();
358 }
359
360 return $this->get_cap_name();
361 }
362
363 /**
364 * Inject the safe mode query var into URLs
365 *
366 * @param string $url Original URL.
367 *
368 * @return string Modified URL.
369 */
370 public function add_safe_mode_query_var( string $url ): string {
371 return isset( $_REQUEST['snippets-safe-mode'] ) ?
372 add_query_arg( 'snippets-safe-mode', (bool) $_REQUEST['snippets-safe-mode'], $url ) :
373 $url;
374 }
375
376 /**
377 * Retrieve a list of available snippet types and their labels.
378 *
379 * @return array<string, string> Snippet types.
380 */
381 public static function get_types(): array {
382 return apply_filters(
383 'code_snippets_types',
384 array(
385 'php' => __( 'Functions', 'code-snippets' ),
386 'html' => __( 'Content', 'code-snippets' ),
387 'css' => __( 'Styles', 'code-snippets' ),
388 'js' => __( 'Scripts', 'code-snippets' ),
389 'cloud' => __( 'Codevault', 'code-snippets' ),
390 'cloud_search' => __( 'Cloud Search', 'code-snippets' ),
391 'bundles' => __( 'Bundles', 'code-snippets' ),
392 )
393 );
394 }
395
396 /**
397 * Localise a plugin script to provide the CODE_SNIPPETS object.
398 *
399 * @param string $handle Script handle.
400 *
401 * @return void
402 */
403 public function localize_script( string $handle ) {
404 wp_localize_script(
405 $handle,
406 'CODE_SNIPPETS',
407 [
408 'isLicensed' => $this->licensing->is_licensed(),
409 'isCloudConnected' => Cloud_API::is_cloud_connection_available(),
410 'restAPI' => [
411 'base' => esc_url_raw( rest_url() ),
412 'snippets' => esc_url_raw( rest_url( Snippets_REST_Controller::get_base_route() ) ),
413 'nonce' => wp_create_nonce( 'wp_rest' ),
414 'localToken' => $this->cloud_api->get_local_token(),
415 ],
416 'urls' => [
417 'plugin' => esc_url_raw( plugins_url( '', PLUGIN_FILE ) ),
418 'manage' => esc_url_raw( $this->get_menu_url() ),
419 'edit' => esc_url_raw( $this->get_menu_url( 'edit' ) ),
420 'addNew' => esc_url_raw( $this->get_menu_url( 'add' ) ),
421 ],
422 ]
423 );
424 }
425 }
426