PluginProbe
Code Snippets / 3.7.0
Code Snippets v3.7.0
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.7.0, at php/class-plugin.php

379 lines 9.7 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 * Class constructor
82 *
83 * @param string $version Current plugin version.
84 * @param string $file Path to main plugin file.
85 */
86 public function __construct( string $version, string $file ) {
87 $this->version = $version;
88 $this->file = $file;
89
90 wp_cache_add_global_groups( CACHE_GROUP );
91
92 add_filter( 'code_snippets/execute_snippets', array( $this, 'disable_snippet_execution' ), 5 );
93
94 if ( isset( $_REQUEST['snippets-safe-mode'] ) ) {
95 add_filter( 'home_url', array( $this, 'add_safe_mode_query_var' ) );
96 add_filter( 'admin_url', array( $this, 'add_safe_mode_query_var' ) );
97 }
98
99 add_action( 'rest_api_init', [ $this, 'init_rest_api' ] );
100 add_action( 'allowed_redirect_hosts', [ $this, 'allow_code_snippets_redirect' ] );
101 }
102
103 /**
104 * Initialise classes and include files
105 */
106 public function load_plugin() {
107 $includes_path = __DIR__;
108
109 // Database operation functions.
110 $this->db = new DB();
111
112 // Snippet operation functions.
113 require_once $includes_path . '/snippet-ops.php';
114 $this->evaluate_content = new Evaluate_Content( $this->db );
115 $this->evaluate_functions = new Evaluate_Functions( $this->db );
116
117 // CodeMirror editor functions.
118 require_once $includes_path . '/editor.php';
119
120 // General Administration functions.
121 if ( is_admin() ) {
122 $this->admin = new Admin();
123 }
124
125 // Settings component.
126 require_once $includes_path . '/settings/settings-fields.php';
127 require_once $includes_path . '/settings/editor-preview.php';
128 require_once $includes_path . '/settings/settings.php';
129
130 // Cloud List Table shared functions.
131 require_once $includes_path . '/cloud/list-table-shared-ops.php';
132
133 $this->front_end = new Front_End();
134 $this->cloud_api = new Cloud_API();
135
136 $upgrade = new Upgrade( $this->version, $this->db );
137 add_action( 'plugins_loaded', array( $upgrade, 'run' ), 0 );
138 $this->licensing = new Licensing();
139 }
140
141 /**
142 * Register custom REST API controllers.
143 *
144 * @return void
145 */
146 public function init_rest_api() {
147 $snippets_controller = new Snippets_REST_Controller();
148 $snippets_controller->register_routes();
149 }
150
151 /**
152 * Disable snippet execution if the necessary query var is set.
153 *
154 * @param bool $execute_snippets Current filter value.
155 *
156 * @return bool New filter value.
157 */
158 public function disable_snippet_execution( bool $execute_snippets ): bool {
159 return ! empty( $_REQUEST['snippets-safe-mode'] ) && $this->current_user_can() ? false : $execute_snippets;
160 }
161
162 /**
163 * Determine whether the menu is full or compact.
164 *
165 * @return bool
166 */
167 public function is_compact_menu(): bool {
168 return ! is_network_admin() && apply_filters( 'code_snippets_compact_menu', false );
169 }
170
171 /**
172 * Fetch the admin menu slug for a menu.
173 *
174 * @param string $menu Name of menu to retrieve the slug for.
175 *
176 * @return string The menu's slug.
177 */
178 public function get_menu_slug( string $menu = '' ): string {
179 $add = array( 'single', 'add', 'add-new', 'add-snippet', 'new-snippet', 'add-new-snippet' );
180 $edit = array( 'edit', 'edit-snippet' );
181 $import = array( 'import', 'import-snippets', 'import-code-snippets' );
182 $settings = array( 'settings', 'snippets-settings' );
183 $cloud = array( 'cloud', 'cloud-snippets' );
184 $welcome = array( 'welcome', 'getting-started', 'code-snippets' );
185
186 if ( in_array( $menu, $edit, true ) ) {
187 return 'edit-snippet';
188 } elseif ( in_array( $menu, $add, true ) ) {
189 return 'add-snippet';
190 } elseif ( in_array( $menu, $import, true ) ) {
191 return 'import-code-snippets';
192 } elseif ( in_array( $menu, $settings, true ) ) {
193 return 'snippets-settings';
194 } elseif ( in_array( $menu, $cloud, true ) ) {
195 return 'snippets&type=cloud';
196 } elseif ( in_array( $menu, $welcome, true ) ) {
197 return 'code-snippets-welcome';
198 } else {
199 return 'snippets';
200 }
201 }
202
203 /**
204 * Fetch the URL to a snippets admin menu.
205 *
206 * @param string $menu Name of menu to retrieve the URL to.
207 * @param string $context URL scheme to use.
208 *
209 * @return string The menu's URL.
210 */
211 public function get_menu_url( string $menu = '', string $context = 'self' ): string {
212 $slug = $this->get_menu_slug( $menu );
213
214 if ( $this->is_compact_menu() && 'network' !== $context ) {
215 $base_slug = $this->get_menu_slug();
216 $url = 'tools.php?page=' . $base_slug;
217
218 if ( $slug !== $base_slug ) {
219 $url .= '&sub=' . $slug;
220 }
221 } else {
222 $url = 'admin.php?page=' . $slug;
223 }
224
225 if ( 'network' === $context ) {
226 return network_admin_url( $url );
227 } elseif ( 'admin' === $context ) {
228 return admin_url( $url );
229 } else {
230 return self_admin_url( $url );
231 }
232 }
233
234 /**
235 * Fetch the admin menu slug for a snippets admin menu.
236 *
237 * @param integer $snippet_id Snippet ID.
238 * @param string $context URL scheme to use.
239 *
240 * @return string The URL to the edit snippet page for that snippet.
241 */
242 public function get_snippet_edit_url( int $snippet_id, string $context = 'self' ): string {
243 return add_query_arg(
244 'id',
245 absint( $snippet_id ),
246 $this->get_menu_url( 'edit', $context )
247 );
248 }
249
250 /**
251 * Allow redirecting to the Code Snippets site.
252 *
253 * @param array<string> $hosts Allowed hosts.
254 *
255 * @return array Modified allowed hosts.
256 */
257 public function allow_code_snippets_redirect( array $hosts ): array {
258 $hosts[] = 'codesnippets.pro';
259 $hosts[] = 'snipco.de';
260 return $hosts;
261 }
262
263 /**
264 * Determine whether the current user can perform actions on snippets.
265 *
266 * @return boolean Whether the current user has the required capability.
267 *
268 * @since 2.8.6
269 */
270 public function current_user_can(): bool {
271 return current_user_can( $this->get_cap() );
272 }
273
274 /**
275 * Retrieve the name of the capability required to manage sub-site snippets.
276 *
277 * @return string
278 */
279 public function get_cap_name(): string {
280 return apply_filters( 'code_snippets_cap', 'manage_options' );
281 }
282
283 /**
284 * Retrieve the name of the capability required to manage network snippets.
285 *
286 * @return string
287 */
288 public function get_network_cap_name(): string {
289 return apply_filters( 'code_snippets_network_cap', 'manage_network_options' );
290 }
291
292 /**
293 * Get the required capability to perform a certain action on snippets.
294 * Does not check if the user has this capability or not.
295 *
296 * If multisite, checks if *Enable Administration Menus: Snippets* is active
297 * under the *Settings > Network Settings* network admin menu
298 *
299 * @return string The capability required to manage snippets.
300 *
301 * @since 2.0
302 */
303 public function get_cap(): string {
304 if ( is_multisite() ) {
305 $menu_perms = get_site_option( 'menu_items', array() );
306
307 // If multisite is enabled and the snippet menu is not activated, restrict snippet operations to super admins only.
308 if ( empty( $menu_perms['snippets'] ) ) {
309 return $this->get_network_cap_name();
310 }
311 }
312
313 return $this->get_cap_name();
314 }
315
316 /**
317 * Inject the safe mode query var into URLs
318 *
319 * @param string $url Original URL.
320 *
321 * @return string Modified URL.
322 */
323 public function add_safe_mode_query_var( string $url ): string {
324 return isset( $_REQUEST['snippets-safe-mode'] ) ?
325 add_query_arg( 'snippets-safe-mode', (bool) $_REQUEST['snippets-safe-mode'], $url ) :
326 $url;
327 }
328
329 /**
330 * Retrieve a list of available snippet types and their labels.
331 *
332 * @return array<string, string> Snippet types.
333 */
334 public static function get_types(): array {
335 return apply_filters(
336 'code_snippets_types',
337 array(
338 'php' => __( 'Functions', 'code-snippets' ),
339 'html' => __( 'Content', 'code-snippets' ),
340 'css' => __( 'Styles', 'code-snippets' ),
341 'js' => __( 'Scripts', 'code-snippets' ),
342 'cloud' => __( 'Codevault', 'code-snippets' ),
343 'cloud_search' => __( 'Cloud Search', 'code-snippets' ),
344 'bundles' => __( 'Bundles', 'code-snippets' ),
345 )
346 );
347 }
348
349 /**
350 * Localise a plugin script to provide the CODE_SNIPPETS object.
351 *
352 * @param string $handle Script handle.
353 *
354 * @return void
355 */
356 public function localize_script( string $handle ) {
357 wp_localize_script(
358 $handle,
359 'CODE_SNIPPETS',
360 [
361 'isLicensed' => $this->licensing->is_licensed(),
362 'isCloudConnected' => Cloud_API::is_cloud_connection_available(),
363 'restAPI' => [
364 'base' => esc_url_raw( rest_url() ),
365 'snippets' => esc_url_raw( rest_url( Snippets_REST_Controller::get_base_route() ) ),
366 'nonce' => wp_create_nonce( 'wp_rest' ),
367 'localToken' => $this->cloud_api->get_local_token(),
368 ],
369 'urls' => [
370 'plugin' => esc_url_raw( plugins_url( '', PLUGIN_FILE ) ),
371 'manage' => esc_url_raw( $this->get_menu_url() ),
372 'edit' => esc_url_raw( $this->get_menu_url( 'edit' ) ),
373 'addNew' => esc_url_raw( $this->get_menu_url( 'add' ) ),
374 ],
375 ]
376 );
377 }
378 }
379