PluginProbe
Code Snippets / 2.14.6
Code Snippets v2.14.6
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.14.6, at php/class-code-snippets.php

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