PluginProbe
Code Snippets / 3.10.2
Code Snippets v3.10.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 3.0.1 All 64 releases
code-snippets / php / Plugin.php

Plugin.php in Code Snippets 3.10.2, at php/Plugin.php

389 lines 10.6 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\Controller\Cloud_Search_Controller;
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\Model\Basic_Cloud_Connection;
17 use Code_Snippets\REST_API\Cloud\Cloud_Snippets_REST_Controller;
18 use Code_Snippets\REST_API\Import\File_Import_REST_Controller;
19 use Code_Snippets\REST_API\Import\Plugins_Import_REST_Controller;
20 use Code_Snippets\REST_API\Snippets\Preferences_REST_Controller;
21 use Code_Snippets\REST_API\Snippets\Recently_Active_REST_Controller;
22 use Code_Snippets\REST_API\Snippets\Snippets_REST_Controller;
23 use function Code_Snippets\Settings\are_settings_unified;
24
25 /**
26 * The main plugin class
27 *
28 * @package Code_Snippets
29 */
30 class Plugin {
31
32 /**
33 * Current plugin version number
34 *
35 * @deprecated Use the PLUGIN_VERSION constant instead.
36 *
37 * @var string
38 */
39 public string $version = PLUGIN_VERSION;
40
41 /**
42 * Filesystem path to the main plugin file
43 *
44 * @deprecated Use the PLUGIN_FILE constant instead.
45 *
46 * @var string
47 */
48 public string $file = PLUGIN_FILE;
49
50 /**
51 * Database class
52 *
53 * @var DB
54 */
55 public DB $db;
56
57 /**
58 * Class for managing shortcodes.
59 *
60 * @var Shortcodes
61 */
62 public Shortcodes $shortcodes;
63
64 /**
65 * Administration area class
66 *
67 * @var Bootstrap_Admin
68 */
69 public Bootstrap_Admin $admin;
70
71 /**
72 * Handles licensing and plugin updates.
73 *
74 * @var Licensing
75 */
76 public Licensing $licensing;
77
78 /**
79 * Handles snippet handler registration.
80 *
81 * @var Flat_Files\Handler_Registry
82 */
83 public Flat_Files\Handler_Registry $snippet_handler_registry;
84
85 /**
86 * Instance of connection to Code Snippets Cloud.
87 *
88 * @var Basic_Cloud_Connection
89 */
90 public Basic_Cloud_Connection $cloud_connection;
91
92 /**
93 * Class constructor
94 */
95 public function __construct() {
96 wp_cache_add_global_groups( CACHE_GROUP );
97 add_action( 'allowed_redirect_hosts', [ $this, 'allow_code_snippets_redirect' ] );
98 }
99
100 /**
101 * Load the plugin utilities.
102 */
103 private function load_utilities() {
104 require_once __DIR__ . '/Utils/editor.php';
105 require_once __DIR__ . '/Utils/options.php';
106 require_once __DIR__ . '/Settings/settings.php';
107 require_once __DIR__ . '/snippet-ops.php';
108 }
109
110 /**
111 * Initialise the plugin and load all necessary classes.
112 */
113 public function load_plugin() {
114 $this->load_utilities();
115
116 $this->db = new DB();
117 $this->licensing = new Licensing();
118
119 $this->cloud_connection = new Basic_Cloud_Connection();
120
121 new Evaluate_Content( $this->db );
122 new Evaluate_Functions( $this->db );
123
124 $this->shortcodes = new Shortcodes();
125
126 if ( is_admin() ) {
127 $this->admin = new Bootstrap_Admin();
128 new Promotion_Manager();
129 }
130
131 new Shortcodes();
132 new MCE_Plugin();
133 new Upgrader( PLUGIN_VERSION, $this->db );
134 new Admin_Bar();
135
136 if ( is_admin() ) {
137 new Promotion_Manager();
138 }
139
140 $this->init_snippet_files();
141
142 add_action( 'rest_api_init', [ $this, 'init_rest_api' ], 1 );
143 }
144
145 /**
146 * Load REST API controller classes when serving REST API requests.
147 *
148 * @return void
149 */
150 public function init_rest_api() {
151 $cloud_search = new Cloud_Search_Controller( $this->cloud_connection );
152
153 new Snippets_REST_Controller();
154 new Recently_Active_REST_Controller();
155 new Preferences_REST_Controller();
156 new Plugins_Import_REST_Controller();
157 new File_Import_REST_Controller();
158
159 new Cloud_Snippets_REST_Controller( $cloud_search );
160 }
161
162 /**
163 * Initialises the snippet files component.
164 *
165 * @return void
166 */
167 private function init_snippet_files() {
168 $this->snippet_handler_registry = new Flat_Files\Handler_Registry(
169 [
170 'php' => new Flat_Files\Handlers\Functions_Snippet_Handler(),
171 'html' => new Flat_Files\Handlers\Content_Snippet_Handler(),
172 ]
173 );
174
175 $fs = new Flat_Files\WP_Filesystem_Adapter();
176 $config_repo = new Flat_Files\Flat_File_Config_Repository( $fs );
177
178 $snippet_files = new Flat_Files\Snippet_Files( $this->snippet_handler_registry, $fs, $config_repo );
179 $snippet_files->register_hooks();
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 = [ 'single', 'add', 'add-new', 'add-snippet', 'new-snippet', 'add-new-snippet' ];
200 $edit = [ 'edit', 'edit-snippet' ];
201 $import = [ 'import', 'import-snippets', 'import-code-snippets' ];
202 $settings = [ 'settings', 'snippets-settings' ];
203 $cloud = [ 'cloud', 'cloud-snippets' ];
204 $welcome = [ '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&subpage=cloud-community';
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 ( 'snippets-settings' === $slug && are_settings_unified() ) {
246 return network_admin_url( $url );
247 }
248
249 switch ( $context ) {
250 case 'network':
251 return network_admin_url( $url );
252
253 case 'admin':
254 return admin_url( $url );
255
256 default:
257 return self_admin_url( $url );
258 }
259 }
260
261 /**
262 * Allow redirecting to the Code Snippets site.
263 *
264 * @param string[] $hosts Allowed hosts.
265 *
266 * @return array Modified allowed hosts.
267 */
268 public function allow_code_snippets_redirect( array $hosts ): array {
269 $hosts[] = 'codesnippets.pro';
270 $hosts[] = 'snipco.de';
271 return $hosts;
272 }
273
274 /**
275 * Determine whether the current user can perform actions on snippets.
276 *
277 * @return bool Whether the current user has the required capability.
278 *
279 * @since 2.8.6
280 */
281 public function current_user_can(): bool {
282 return current_user_can( $this->get_cap() );
283 }
284
285 /**
286 * Retrieve the name of the capability required to manage sub-site snippets.
287 *
288 * @return string
289 */
290 public function get_cap_name(): string {
291 return apply_filters( 'code_snippets_cap', 'manage_options' );
292 }
293
294 /**
295 * Retrieve the name of the capability required to manage network snippets.
296 *
297 * @return string
298 */
299 public function get_network_cap_name(): string {
300 return apply_filters( 'code_snippets_network_cap', 'manage_network_options' );
301 }
302
303 /**
304 * Determine if a subsite user menu is enabled via *Network Settings > Enable administration menus*.
305 *
306 * @return bool
307 */
308 public function is_subsite_menu_enabled(): bool {
309 if ( ! is_multisite() ) {
310 return true;
311 }
312
313 $menu_perms = get_site_option( 'menu_items', array() );
314 return ! empty( $menu_perms['snippets'] );
315 }
316
317 /**
318 * Determine if the current user should have the network snippets capability.
319 *
320 * @return bool
321 */
322 public function user_can_manage_network_snippets(): bool {
323 return is_super_admin() || current_user_can( $this->get_network_cap_name() );
324 }
325
326 /**
327 * Get the required capability to perform a certain action on snippets.
328 * Does not check if the user has this capability or not.
329 *
330 * If multisite, adjusts the capability based on whether the user is viewing
331 * the network dashboard or a subsite and whether the menu is enabled for subsites.
332 *
333 * @return string The capability required to manage snippets.
334 *
335 * @since 2.0
336 */
337 public function get_cap(): string {
338 return is_multisite() && ( is_network_admin() || ! $this->is_subsite_menu_enabled() )
339 ? $this->get_network_cap_name()
340 : $this->get_cap_name();
341 }
342
343 /**
344 * Localise a plugin script to provide the CODE_SNIPPETS object.
345 *
346 * @param string $handle Script handle.
347 *
348 * @return void
349 */
350 public function localize_script( string $handle ) {
351 wp_localize_script(
352 $handle,
353 'CODE_SNIPPETS',
354 [
355 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
356 'isLicensed' => $this->licensing->is_licensed(),
357 'isCloudConnected' => $this->cloud_connection->is_authenticated(),
358 'hideUpsell' => Settings\get_setting( 'general', 'hide_upgrade_menu' ),
359 'snippetView' => Preferences_REST_Controller::get_snippet_view(),
360 'restAPI' => [
361 'base' => esc_url_raw( rest_url() ),
362 'snippets' => esc_url_raw( rest_url( Snippets_REST_Controller::get_base_route() ) ),
363 'recentlyActive' => esc_url_raw( rest_url( Recently_Active_REST_Controller::get_base_route() ) ),
364 'preferences' => esc_url_raw( rest_url( Preferences_REST_Controller::get_base_route() ) ),
365 'importPlugins' => esc_url_raw( rest_url( Plugins_Import_REST_Controller::get_base_route() ) ),
366 'importFiles' => esc_url_raw( rest_url( File_Import_REST_Controller::get_base_route() ) ),
367 'nonce' => wp_create_nonce( 'wp_rest' ),
368 'cloud' => [
369 'token' => $this->cloud_connection->get_local_token(),
370 'snippets' => esc_url_raw( rest_url( Cloud_Snippets_REST_Controller::get_base_route() ) ),
371 ],
372 ],
373 'urls' => [
374 'plugin' => esc_url_raw( plugins_url( '', PLUGIN_FILE ) ),
375 'manage' => esc_url_raw( $this->get_menu_url() ),
376 'edit' => esc_url_raw( $this->get_menu_url( 'edit' ) ),
377 'addNew' => esc_url_raw( $this->get_menu_url( 'add' ) ),
378 'welcome' => esc_url_raw( $this->get_menu_url( 'welcome' ) ),
379 'import' => esc_url_raw( $this->get_menu_url( 'import' ) ),
380 'cloud' => esc_url_raw( $this->cloud_connection->get_base_url() ),
381 'settings' => ! are_settings_unified() || is_super_admin()
382 ? esc_url_raw( $this->get_menu_url( 'settings' ) )
383 : null,
384 ],
385 ]
386 );
387 }
388 }
389