PluginProbe
Code Snippets / 2.10.0
Code Snippets v2.10.0
4.0.0-beta.2 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 All 65 releases
code-snippets / php / class-code-snippets.php

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

248 lines 6.7 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
61 /* Upgrade function */
62 require_once $includes_path . '/upgrade.php';
63
64 /* CodeMirror editor functions */
65 require_once $includes_path . '/editor.php';
66
67 /* Backwards compatability functions */
68 require_once $includes_path . '/functions.php';
69
70 /* General Administration functions */
71 if ( is_admin() ) {
72 $this->admin = new Code_Snippets_Admin();
73 }
74
75 /* Settings component */
76 require_once $includes_path . '/settings/settings-fields.php';
77 require_once $includes_path . '/settings/editor-preview.php';
78 require_once $includes_path . '/settings/render-fields.php';
79 require_once $includes_path . '/settings/settings.php';
80
81 $this->shortcode = new Code_Snippets_Shortcode();
82 }
83
84 public function disable_snippet_execution( $execute_snippets ) {
85
86 /* Bail early if safe mode is active */
87 if ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) {
88 return false;
89 }
90
91 if ( isset( $_GET['snippets-safe-mode'] ) && $_GET['snippets-safe-mode'] && $this->current_user_can() ) {
92 return false;
93 }
94
95 return $execute_snippets;
96 }
97
98 /**
99 * Fetch the admin menu slug for a snippets menu
100 * @param string $menu The menu to retrieve the slug for
101 * @return string The menu's slug
102 */
103 public function get_menu_slug( $menu = '' ) {
104 $add = array( 'single', 'add', 'add-new', 'add-snippet', 'new-snippet', 'add-new-snippet' );
105 $edit = array( 'edit', 'edit-snippet' );
106 $import = array( 'import', 'import-snippets' );
107 $settings = array( 'settings', 'snippets-settings' );
108
109 if ( in_array( $menu, $edit ) ) {
110 return 'edit-snippet';
111 } elseif ( in_array( $menu, $add ) ) {
112 return 'add-snippet';
113 } elseif ( in_array( $menu, $import ) ) {
114 return 'import-snippets';
115 } elseif ( in_array( $menu, $settings ) ) {
116 return 'snippets-settings';
117 } else {
118 return 'snippets';
119 }
120 }
121
122 /**
123 * Fetch the URL to a snippets admin menu
124 * @param string $menu The menu to retrieve the URL to
125 * @param string $context The URL scheme to use
126 * @return string The menu's URL
127 */
128 public function get_menu_url( $menu = '', $context = 'self' ) {
129 $slug = $this->get_menu_slug( $menu );
130 $url = 'admin.php?page=' . $slug;
131
132 if ( 'network' === $context ) {
133 return network_admin_url( $url );
134 } elseif ( 'admin' === $context ) {
135 return admin_url( $url );
136 } else {
137 return self_admin_url( $url );
138 }
139 }
140
141 /**
142 * Fetch the admin menu hook for a snippets menu
143 * @param string $menu The menu to retrieve the hook for
144 * @return string The menu's hook
145 */
146 public function get_menu_hook( $menu = '' ) {
147 $slug = $this->get_menu_slug( $menu );
148 return get_plugin_page_hookname( $slug, 'snippets' );
149 }
150
151 /**
152 * Fetch the admin menu slug for a snippets menu
153 * @param int $snippet_id The snippet
154 * @param string $context The URL scheme to use
155 * @return string The URL to the edit snippet page for that snippet
156 */
157 public function get_snippet_edit_url( $snippet_id, $context = 'self' ) {
158 return add_query_arg(
159 'id', absint( $snippet_id ),
160 $this->get_menu_url( 'edit', $context )
161 );
162 }
163
164 /**
165 * Determine whether the current user can perform actions on snippets.
166 *
167 * @since 2.8.6
168 * @return boolean Whether the current user has the required capability
169 */
170 public function current_user_can() {
171 return current_user_can( $this->get_cap() );
172 }
173
174 /**
175 * Retrieve the name of the capability required to manage sub-site snippets
176 *
177 * @return string
178 */
179 public function get_cap_name() {
180 return apply_filters( 'code_snippets_cap', 'manage_options' );
181 }
182
183 /**
184 * Retrieve the name of the capability required to manage network snippets
185 *
186 * @return string
187 */
188 public function get_network_cap_name() {
189 return apply_filters( 'code_snippets_network_cap', 'manage_network_options' );
190 }
191
192 /**
193 * Get the required capability to perform a certain action on snippets.
194 * Does not check if the user has this capability or not.
195 *
196 * If multisite, checks if *Enable Administration Menus: Snippets* is active
197 * under the *Settings > Network Settings* network admin menu
198 *
199 * @since 2.0
200 * @return string The capability required to manage snippets
201 */
202 public function get_cap() {
203
204 if ( is_multisite() ) {
205 $menu_perms = get_site_option( 'menu_items', array() );
206
207 /* If multisite is enabled and the snippet menu is not activated,
208 restrict snippet operations to super admins only */
209 if ( empty( $menu_perms['snippets'] ) ) {
210 return $this->get_network_cap_name();
211 }
212 }
213
214 return $this->get_cap_name();
215 }
216
217 /**
218 * Load up the localization file if we're using WordPress in a different language.
219 *
220 * If you wish to contribute a language file to be included in the Code Snippets package,
221 * please see create an issue on GitHub: https://github.com/sheabunge/code-snippets/issues
222 */
223 function load_textdomain() {
224 $domain = 'code-snippets';
225 $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
226
227 // wp-content/languages/code-snippets/code-snippets-[locale].mo
228 load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . "$domain/$domain-$locale.mo" );
229
230 // wp-content/plugins/code-snippets/languages/code-snippets-[locale].mo
231 load_plugin_textdomain( $domain, false, dirname( plugin_basename( $this->file ) ) . '/languages' );
232 }
233
234 /**
235 * Inject the safe mode query var into URLs
236 *
237 * @param string $url A URL
238 *
239 * @return string
240 */
241 function add_safe_mode_query_var( $url ) {
242 if ( isset( $_REQUEST['snippets-safe-mode'] ) ) {
243 return add_query_arg( 'snippets-safe-mode', $_REQUEST['snippets-safe-mode'], $url );
244 }
245 return $url;
246 }
247 }
248