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

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