PluginProbe
Code Snippets / 3.10.0-beta.1
Code Snippets v3.10.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 3.10.0-beta.1, at php/Plugin.php

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