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

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

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