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

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