PluginProbe
Code Snippets / 3.2.1
Code Snippets v3.2.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 / build / php / class-plugin.php

class-plugin.php in Code Snippets 3.2.1, at build/php/class-plugin.php

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