PluginProbe
Code Snippets / 2.13.0
Code Snippets v2.13.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-code-snippets.php

class-code-snippets.php in Code Snippets 2.13.0, at php/class-code-snippets.php

271 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The main plugin class
5 *
6 * @package Code_Snippets
7 */
8 class Code_Snippets {
9
10 /**
11 * The current plugin version
12 * @var string
13 */
14 public $version;
15
16 /**
17 * Filesystem path to the main plugin file
18 * @var string
19 */
20 public $file;
21
22 /**
23 * @var Code_Snippets_DB
24 */
25 public $db;
26
27 /**
28 * @var Code_Snippets_Admin
29 */
30 public $admin;
31
32 /**
33 * @var Code_Snippets_Shortcode
34 */
35 public $shortcode;
36
37 /**
38 * Class constructor
39 *
40 * @param string $version The current plugin version
41 * @param string $file The main plugin file
42 */
43 function __construct( $version, $file ) {
44 $this->version = $version;
45 $this->file = $file;
46
47 add_action( 'init', array( $this, 'load_textdomain' ), 9 );
48
49 add_filter( 'code_snippets/execute_snippets', array( $this, 'disable_snippet_execution' ), 5 );
50
51 if ( isset( $_REQUEST['snippets-safe-mode'] ) ) {
52 add_filter( 'home_url', array( $this, 'add_safe_mode_query_var' ) );
53 add_filter( 'admin_url', array( $this, 'add_safe_mode_query_var' ) );
54 }
55 }
56
57 function load_plugin() {
58 $includes_path = dirname( __FILE__ );
59
60 /* Database operation functions */
61 $this->db = new Code_Snippets_DB();
62
63 /* Snippet operation functions */
64 require_once $includes_path . '/snippet-ops.php';
65 require_once $includes_path . '/import-export.php';
66
67 /* CodeMirror editor functions */
68 require_once $includes_path . '/editor.php';
69
70 /* Backwards compatibility functions */
71 require_once $includes_path . '/functions.php';
72
73 /* General Administration functions */
74 if ( is_admin() ) {
75 $this->admin = new Code_Snippets_Admin();
76 }
77
78 /* Settings component */
79 require_once $includes_path . '/settings/settings-fields.php';
80 require_once $includes_path . '/settings/editor-preview.php';
81 require_once $includes_path . '/settings/render-fields.php';
82 require_once $includes_path . '/settings/settings.php';
83
84 $this->shortcode = new Code_Snippets_Shortcode();
85
86 $upgrade = new Code_Snippets_Upgrade( $this->db );
87 add_action( 'plugins_loaded', array( $upgrade, 'run' ), 0 );
88 }
89
90 public function disable_snippet_execution( $execute_snippets ) {
91
92 if ( isset( $_GET['snippets-safe-mode'] ) && $_GET['snippets-safe-mode'] && $this->current_user_can() ) {
93 return false;
94 }
95
96 return $execute_snippets;
97 }
98
99 /**
100 * Fetch the admin menu slug for a snippets menu
101 *
102 * @param string $menu The menu to retrieve the slug for
103 *
104 * @return string The menu's slug
105 */
106 public function get_menu_slug( $menu = '' ) {
107 $add = array( 'single', 'add', 'add-new', 'add-snippet', 'new-snippet', 'add-new-snippet' );
108 $edit = array( 'edit', 'edit-snippet' );
109 $import = array( 'import', 'import-snippets' );
110 $settings = array( 'settings', 'snippets-settings' );
111
112 if ( in_array( $menu, $edit ) ) {
113 return 'edit-snippet';
114 } elseif ( in_array( $menu, $add ) ) {
115 return 'add-snippet';
116 } elseif ( in_array( $menu, $import ) ) {
117 return 'import-snippets';
118 } elseif ( in_array( $menu, $settings ) ) {
119 return 'snippets-settings';
120 } else {
121 return 'snippets';
122 }
123 }
124
125 /**
126 * Fetch the URL to a snippets admin menu
127 *
128 * @param string $menu The menu to retrieve the URL to
129 * @param string $context The URL scheme to use
130 *
131 * @return string The menu's URL
132 */
133 public function get_menu_url( $menu = '', $context = 'self' ) {
134 $slug = $this->get_menu_slug( $menu );
135
136 if ( $this->admin->is_compact_menu() && 'network' !== $context ) {
137 $base_slug = $this->get_menu_slug();
138 $url = 'tools.php?page=' . $base_slug;
139
140 if ( $slug !== $base_slug ) {
141 $url .= '&sub=' . $slug;
142 }
143
144 } else {
145 $url = 'admin.php?page=' . $slug;
146 }
147
148 if ( 'network' === $context ) {
149 return network_admin_url( $url );
150 } elseif ( 'admin' === $context ) {
151 return admin_url( $url );
152 } else {
153 return self_admin_url( $url );
154 }
155 }
156
157 /**
158 * Fetch the admin menu hook for a snippets menu
159 *
160 * @param string $menu The menu to retrieve the hook for
161 *
162 * @return string The menu's hook
163 */
164 public function get_menu_hook( $menu = '' ) {
165 $slug = $this->get_menu_slug( $menu );
166
167 return get_plugin_page_hookname( $slug, 'snippets' );
168 }
169
170 /**
171 * Fetch the admin menu slug for a snippets menu
172 *
173 * @param int $snippet_id The snippet
174 * @param string $context The URL scheme to use
175 *
176 * @return string The URL to the edit snippet page for that snippet
177 */
178 public function get_snippet_edit_url( $snippet_id, $context = 'self' ) {
179 return add_query_arg(
180 'id', absint( $snippet_id ),
181 $this->get_menu_url( 'edit', $context )
182 );
183 }
184
185 /**
186 * Determine whether the current user can perform actions on snippets.
187 *
188 * @since 2.8.6
189 * @return boolean Whether the current user has the required capability
190 */
191 public function current_user_can() {
192 return current_user_can( $this->get_cap() );
193 }
194
195 /**
196 * Retrieve the name of the capability required to manage sub-site snippets
197 *
198 * @return string
199 */
200 public function get_cap_name() {
201 return apply_filters( 'code_snippets_cap', 'manage_options' );
202 }
203
204 /**
205 * Retrieve the name of the capability required to manage network snippets
206 *
207 * @return string
208 */
209 public function get_network_cap_name() {
210 return apply_filters( 'code_snippets_network_cap', 'manage_network_options' );
211 }
212
213 /**
214 * Get the required capability to perform a certain action on snippets.
215 * Does not check if the user has this capability or not.
216 *
217 * If multisite, checks if *Enable Administration Menus: Snippets* is active
218 * under the *Settings > Network Settings* network admin menu
219 *
220 * @since 2.0
221 * @return string The capability required to manage snippets
222 */
223 public function get_cap() {
224
225 if ( is_multisite() ) {
226 $menu_perms = get_site_option( 'menu_items', array() );
227
228 /* If multisite is enabled and the snippet menu is not activated,
229 restrict snippet operations to super admins only */
230 if ( empty( $menu_perms['snippets'] ) ) {
231 return $this->get_network_cap_name();
232 }
233 }
234
235 return $this->get_cap_name();
236 }
237
238 /**
239 * Load up the localization file if we're using WordPress in a different language.
240 *
241 * If you wish to contribute a language file to be included in the Code Snippets package,
242 * please see create an issue on GitHub: https://github.com/sheabunge/code-snippets/issues
243 */
244 function load_textdomain() {
245 $domain = 'code-snippets';
246 $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
247
248 // wp-content/languages/code-snippets/code-snippets-[locale].mo
249 load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . "$domain/$domain-$locale.mo" );
250
251 // wp-content/plugins/code-snippets/languages/code-snippets-[locale].mo
252 load_plugin_textdomain( $domain, false, dirname( plugin_basename( $this->file ) ) . '/languages' );
253 }
254
255 /**
256 * Inject the safe mode query var into URLs
257 *
258 * @param string $url A URL
259 *
260 * @return string
261 */
262 function add_safe_mode_query_var( $url ) {
263
264 if ( isset( $_REQUEST['snippets-safe-mode'] ) ) {
265 return add_query_arg( 'snippets-safe-mode', $_REQUEST['snippets-safe-mode'], $url );
266 }
267
268 return $url;
269 }
270 }
271