PluginProbe
Code Snippets / 3.1.1
Code Snippets v3.1.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.1.1, at php/class-plugin.php

322 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_action( 'init', array( $this, 'load_textdomain' ), 9 );
74
75 add_filter( 'code_snippets/execute_snippets', array( $this, 'disable_snippet_execution' ), 5 );
76
77 if ( isset( $_REQUEST['snippets-safe-mode'] ) ) {
78 add_filter( 'home_url', array( $this, 'add_safe_mode_query_var' ) );
79 add_filter( 'admin_url', array( $this, 'add_safe_mode_query_var' ) );
80 }
81 }
82
83 /**
84 * Initialise classes and include files
85 */
86 public function load_plugin() {
87 $includes_path = __DIR__;
88
89 /* Database operation functions */
90 $this->db = new DB();
91
92 /* Snippet operation functions */
93 require_once $includes_path . '/snippet-ops.php';
94
95 /* CodeMirror editor functions */
96 require_once $includes_path . '/editor.php';
97
98 /* General Administration functions */
99 if ( is_admin() ) {
100 $this->admin = new Admin();
101 }
102
103 /* Settings component */
104 require_once $includes_path . '/settings/settings-fields.php';
105 require_once $includes_path . '/settings/editor-preview.php';
106 require_once $includes_path . '/settings/settings.php';
107
108 $this->rest_api = new REST_API();
109 $this->active_snippets = new Active_Snippets();
110 $this->frontend = new Frontend();
111
112 $upgrade = new Upgrade( $this->version, $this->db );
113 add_action( 'plugins_loaded', array( $upgrade, 'run' ), 0 );
114 }
115
116 /**
117 * Disable snippet execution if the necessary query var is set
118 *
119 * @param bool $execute_snippets Current filter value.
120 *
121 * @return bool New filter value.
122 */
123 public function disable_snippet_execution( $execute_snippets ) {
124 return ! empty( $_REQUEST['snippets-safe-mode'] ) && $this->current_user_can() ? false : $execute_snippets;
125 }
126
127 /**
128 * Determine whether the menu is full or compact
129 *
130 * @return bool
131 */
132 public function is_compact_menu() {
133 return ! is_network_admin() && apply_filters( 'code_snippets_compact_menu', false );
134 }
135
136 /**
137 * Fetch the admin menu slug for a snippets menu.
138 *
139 * @param string $menu Name of menu to retrieve the slug for.
140 *
141 * @return string The menu's slug.
142 */
143 public function get_menu_slug( $menu = '' ) {
144 $add = array( 'single', 'add', 'add-new', 'add-snippet', 'new-snippet', 'add-new-snippet' );
145 $edit = array( 'edit', 'edit-snippet' );
146 $import = array( 'import', 'import-snippets', 'import-code-snippets' );
147 $settings = array( 'settings', 'snippets-settings' );
148
149 if ( in_array( $menu, $edit, true ) ) {
150 return 'edit-snippet';
151 } elseif ( in_array( $menu, $add, true ) ) {
152 return 'add-snippet';
153 } elseif ( in_array( $menu, $import, true ) ) {
154 return 'import-code-snippets';
155 } elseif ( in_array( $menu, $settings, true ) ) {
156 return 'snippets-settings';
157 } else {
158 return 'snippets';
159 }
160 }
161
162 /**
163 * Fetch the URL to a snippets admin menu.
164 *
165 * @param string $menu Name of menu to retrieve the URL to.
166 * @param string $context URL scheme to use.
167 *
168 * @return string The menu's URL.
169 */
170 public function get_menu_url( $menu = '', $context = 'self' ) {
171 $slug = $this->get_menu_slug( $menu );
172
173 if ( $this->is_compact_menu() && 'network' !== $context ) {
174 $base_slug = $this->get_menu_slug();
175 $url = 'tools.php?page=' . $base_slug;
176
177 if ( $slug !== $base_slug ) {
178 $url .= '&sub=' . $slug;
179 }
180 } else {
181 $url = 'admin.php?page=' . $slug;
182 }
183
184 if ( 'network' === $context || 'snippets-settings' === $slug ) {
185 return network_admin_url( $url );
186 } elseif ( 'admin' === $context ) {
187 return admin_url( $url );
188 } else {
189 return self_admin_url( $url );
190 }
191 }
192
193 /**
194 * Fetch the admin menu slug for a snippets menu
195 *
196 * @param int $snippet_id Snippet ID.
197 * @param string $context URL scheme to use.
198 *
199 * @return string The URL to the edit snippet page for that snippet.
200 */
201 public function get_snippet_edit_url( $snippet_id, $context = 'self' ) {
202 return add_query_arg(
203 'id',
204 absint( $snippet_id ),
205 $this->get_menu_url( 'edit', $context )
206 );
207 }
208
209 /**
210 * Determine whether the current user can perform actions on snippets.
211 *
212 * @return boolean Whether the current user has the required capability
213 * @since 2.8.6
214 */
215 public function current_user_can() {
216 return current_user_can( $this->get_cap() );
217 }
218
219 /**
220 * Retrieve the name of the capability required to manage sub-site snippets
221 *
222 * @return string
223 */
224 public function get_cap_name() {
225 return apply_filters( 'code_snippets_cap', 'manage_options' );
226 }
227
228 /**
229 * Retrieve the name of the capability required to manage network snippets
230 *
231 * @return string
232 */
233 public function get_network_cap_name() {
234 return apply_filters( 'code_snippets_network_cap', 'manage_network_options' );
235 }
236
237 /**
238 * Get the required capability to perform a certain action on snippets.
239 * Does not check if the user has this capability or not.
240 *
241 * If multisite, checks if *Enable Administration Menus: Snippets* is active
242 * under the *Settings > Network Settings* network admin menu
243 *
244 * @return string The capability required to manage snippets
245 * @since 2.0
246 */
247 public function get_cap() {
248
249 if ( is_multisite() ) {
250 $menu_perms = get_site_option( 'menu_items', array() );
251
252 /* If multisite is enabled and the snippet menu is not activated, restrict snippet operations to super admins only */
253 if ( empty( $menu_perms['snippets'] ) ) {
254 return $this->get_network_cap_name();
255 }
256 }
257
258 return $this->get_cap_name();
259 }
260
261 /**
262 * Load up the localization file if we're using WordPress in a different language.
263 *
264 * If you wish to contribute a language file to be included in the Code Snippets package,
265 * please see create an issue on GitHub: https://github.com/sheabunge/code-snippets/issues
266 */
267 public function load_textdomain() {
268 $domain = 'code-snippets';
269 $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
270
271 // wp-content/languages/code-snippets/code-snippets-[locale].mo.
272 load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . "$domain/$domain-$locale.mo" );
273
274 // wp-content/plugins/code-snippets/languages/code-snippets-[locale].mo.
275 load_plugin_textdomain( $domain, false, dirname( plugin_basename( $this->file ) ) . '/languages' );
276 }
277
278 /**
279 * Inject the safe mode query var into URLs
280 *
281 * @param string $url Original URL.
282 *
283 * @return string Modified URL.
284 */
285 public function add_safe_mode_query_var( $url ) {
286
287 if ( isset( $_REQUEST['snippets-safe-mode'] ) ) {
288 return add_query_arg( 'snippets-safe-mode', (bool) $_REQUEST['snippets-safe-mode'], $url );
289 }
290
291 return $url;
292 }
293
294 /**
295 * Retrieve a list of available snippet types and their labels.
296 *
297 * @return array
298 */
299 public function get_types() {
300 return array(
301 'php' => __( 'Functions', 'code-snippets' ),
302 'html' => __( 'Content', 'code-snippets' ),
303 );
304 }
305
306 /**
307 * Retrieve the description for a particular snippet type.
308 *
309 * @param string $type Snippet type name.
310 *
311 * @return string
312 */
313 public function get_type_description( $type ) {
314 $descriptions = array(
315 'php' => __( 'Function snippets are run on your site as if there were in a plugin or theme functions.php file.', 'code-snippets' ),
316 'html' => __( 'Content snippets are bits of reusable PHP and HTML content that can be inserted into posts and pages.', 'code-snippets' ),
317 );
318
319 return isset( $descriptions[ $type ] ) ? $descriptions[ $type ] : '';
320 }
321 }
322