PluginProbe
Code Snippets / 3.5.0
Code Snippets v3.5.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.5.0, at php/class-plugin.php

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