PluginProbe
Code Snippets / 3.6.6
Code Snippets v3.6.6
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.6, at php/class-plugin.php

367 lines 9.3 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 string $version;
21
22 /**
23 * Filesystem path to the main plugin file
24 *
25 * @var string
26 */
27 public string $file;
28
29 /**
30 * Database class
31 *
32 * @var DB
33 */
34 public DB $db;
35
36 /**
37 * Administration area class
38 *
39 * @var Admin
40 */
41 public Admin $admin;
42
43 /**
44 * Front-end functionality class
45 *
46 * @var Front_End
47 */
48 public Front_End $front_end;
49
50 /**
51 * Class for managing cloud API actions.
52 *
53 * @var Cloud_API
54 */
55 public Cloud_API $cloud_api;
56
57 /**
58 * Class for managing active snippets
59 *
60 * @var Active_Snippets
61 */
62 public Active_Snippets $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->front_end = new Front_End();
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 $welcome = array( 'welcome', 'getting-started', 'code-snippets' );
166
167 if ( in_array( $menu, $edit, true ) ) {
168 return 'edit-snippet';
169 } elseif ( in_array( $menu, $add, true ) ) {
170 return 'add-snippet';
171 } elseif ( in_array( $menu, $import, true ) ) {
172 return 'import-code-snippets';
173 } elseif ( in_array( $menu, $settings, true ) ) {
174 return 'snippets-settings';
175 } elseif ( in_array( $menu, $welcome, true ) ) {
176 return 'code-snippets-welcome';
177 } else {
178 return 'snippets';
179 }
180 }
181
182 /**
183 * Fetch the URL to a snippets admin menu.
184 *
185 * @param string $menu Name of menu to retrieve the URL to.
186 * @param string $context URL scheme to use.
187 *
188 * @return string The menu's URL.
189 */
190 public function get_menu_url( string $menu = '', string $context = 'self' ): string {
191 $slug = $this->get_menu_slug( $menu );
192
193 if ( $this->is_compact_menu() && 'network' !== $context ) {
194 $base_slug = $this->get_menu_slug();
195 $url = 'tools.php?page=' . $base_slug;
196
197 if ( $slug !== $base_slug ) {
198 $url .= '&sub=' . $slug;
199 }
200 } else {
201 $url = 'admin.php?page=' . $slug;
202 }
203
204 if ( 'network' === $context || 'snippets-settings' === $slug ) {
205 return network_admin_url( $url );
206 } elseif ( 'admin' === $context ) {
207 return admin_url( $url );
208 } else {
209 return self_admin_url( $url );
210 }
211 }
212
213 /**
214 * Fetch the admin menu slug for a snippets admin menu.
215 *
216 * @param integer $snippet_id Snippet ID.
217 * @param string $context URL scheme to use.
218 *
219 * @return string The URL to the edit snippet page for that snippet.
220 */
221 public function get_snippet_edit_url( int $snippet_id, string $context = 'self' ): string {
222 return add_query_arg(
223 'id',
224 absint( $snippet_id ),
225 $this->get_menu_url( 'edit', $context )
226 );
227 }
228
229 /**
230 * Allow redirecting to the Code Snippets site.
231 *
232 * @param array<string> $hosts Allowed hosts.
233 *
234 * @return array Modified allowed hosts.
235 */
236 public function allow_code_snippets_redirect( array $hosts ): array {
237 $hosts[] = 'codesnippets.pro';
238 $hosts[] = 'snipco.de';
239 return $hosts;
240 }
241
242 /**
243 * Determine whether the current user can perform actions on snippets.
244 *
245 * @return boolean Whether the current user has the required capability.
246 *
247 * @since 2.8.6
248 */
249 public function current_user_can(): bool {
250 return current_user_can( $this->get_cap() );
251 }
252
253 /**
254 * Retrieve the name of the capability required to manage sub-site snippets.
255 *
256 * @return string
257 */
258 public function get_cap_name(): string {
259 return apply_filters( 'code_snippets_cap', 'manage_options' );
260 }
261
262 /**
263 * Retrieve the name of the capability required to manage network snippets.
264 *
265 * @return string
266 */
267 public function get_network_cap_name(): string {
268 return apply_filters( 'code_snippets_network_cap', 'manage_network_options' );
269 }
270
271 /**
272 * Get the required capability to perform a certain action on snippets.
273 * Does not check if the user has this capability or not.
274 *
275 * If multisite, checks if *Enable Administration Menus: Snippets* is active
276 * under the *Settings > Network Settings* network admin menu
277 *
278 * @return string The capability required to manage snippets.
279 *
280 * @since 2.0
281 */
282 public function get_cap(): string {
283 if ( is_multisite() ) {
284 $menu_perms = get_site_option( 'menu_items', array() );
285
286 // If multisite is enabled and the snippet menu is not activated, restrict snippet operations to super admins only.
287 if ( empty( $menu_perms['snippets'] ) ) {
288 return $this->get_network_cap_name();
289 }
290 }
291
292 return $this->get_cap_name();
293 }
294
295 /**
296 * Inject the safe mode query var into URLs
297 *
298 * @param string $url Original URL.
299 *
300 * @return string Modified URL.
301 */
302 public function add_safe_mode_query_var( string $url ): string {
303 return isset( $_REQUEST['snippets-safe-mode'] ) ?
304 add_query_arg( 'snippets-safe-mode', (bool) $_REQUEST['snippets-safe-mode'], $url ) :
305 $url;
306 }
307
308 /**
309 * Retrieve a list of available snippet types and their labels.
310 *
311 * @return array<string, string> Snippet types.
312 */
313 public static function get_types(): array {
314 return apply_filters(
315 'code_snippets_types',
316 array(
317 'php' => __( 'Functions', 'code-snippets' ),
318 'html' => __( 'Content', 'code-snippets' ),
319 'cloud_search' => __( 'Cloud Search', 'code-snippets' ),
320 'css' => __( 'Styles', 'code-snippets' ),
321 'js' => __( 'Scripts', 'code-snippets' ),
322 'cloud' => __( 'Codevault', 'code-snippets' ),
323 'bundles' => __( 'Bundles', 'code-snippets' ),
324 )
325 );
326 }
327
328 /**
329 * Determine whether a snippet type is Pro-only.
330 *
331 * @param string $type Snippet type name.
332 *
333 * @return bool
334 */
335 public static function is_pro_type( string $type ): bool {
336 return 'css' === $type || 'js' === $type || 'cloud' === $type || 'bundles' === $type;
337 }
338
339 /**
340 * Localise a plugin script to provide the CODE_SNIPPETS object.
341 *
342 * @param string $handle Script handle.
343 *
344 * @return void
345 */
346 public function localize_script( string $handle ) {
347 wp_localize_script(
348 $handle,
349 'CODE_SNIPPETS',
350 [
351 'isLicensed' => false,
352 'restAPI' => [
353 'base' => esc_url_raw( rest_url() ),
354 'snippets' => esc_url_raw( rest_url( Snippets_REST_Controller::get_base_route() ) ),
355 'nonce' => wp_create_nonce( 'wp_rest' ),
356 ],
357 'urls' => [
358 'plugin' => plugins_url( '', PLUGIN_FILE ),
359 'manage' => $this->get_menu_url(),
360 'edit' => $this->get_menu_url( 'edit' ),
361 'addNew' => $this->get_menu_url( 'add' ),
362 ],
363 ]
364 );
365 }
366 }
367