PluginProbe
Code Snippets / 3.2.1
Code Snippets v3.2.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 / build / php / class-admin.php

class-admin.php in Code Snippets 3.2.1, at build/php/class-admin.php

345 lines 9.6 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 * Functions specific to the administration interface
7 *
8 * @package Code_Snippets
9 */
10 class Admin {
11
12 /**
13 * Admin_Menu class instances
14 *
15 * @var array
16 */
17 public $menus = array();
18
19 /**
20 * Class constructor
21 */
22 public function __construct() {
23 if ( is_admin() ) {
24 $this->run();
25 }
26 }
27
28 /**
29 * Initialise classes
30 */
31 public function load_classes() {
32 $this->menus['manage'] = new Manage_Menu();
33 $this->menus['edit'] = new Edit_Menu();
34 $this->menus['import'] = new Import_Menu();
35
36 if ( is_network_admin() === Settings\are_settings_unified() ) {
37 $this->menus['settings'] = new Settings_Menu();
38 }
39
40 foreach ( $this->menus as $menu ) {
41 $menu->run();
42 }
43 }
44
45 /**
46 * Register action and filter hooks
47 */
48 public function run() {
49 add_action( 'init', array( $this, 'load_classes' ), 11 );
50
51 add_filter( 'mu_menu_items', array( $this, 'mu_menu_items' ) );
52 add_filter( 'plugin_action_links_' . plugin_basename( PLUGIN_FILE ), array( $this, 'plugin_settings_link' ) );
53 add_filter( 'plugin_row_meta', array( $this, 'plugin_meta_links' ), 10, 2 );
54 add_filter( 'debug_information', array( $this, 'debug_information' ) );
55 add_action( 'code_snippets/admin/manage', array( $this, 'print_notices' ) );
56
57 if ( ! empty( $_POST['save_snippet'] ) ) {
58 add_action( 'code_snippets/allow_execute_snippet', array( $this, 'prevent_exec_on_save' ), 10, 3 );
59 }
60 }
61
62 /**
63 * Allow super admins to control site admin access to
64 * snippet admin menus
65 *
66 * Adds a checkbox to the *Settings > Network Settings*
67 * network admin menu
68 *
69 * @param array $menu_items Current mu menu items.
70 *
71 * @return array The modified mu menu items
72 *
73 * @since 1.7.1
74 */
75 public function mu_menu_items( $menu_items ) {
76 $menu_items['snippets'] = __( 'Snippets', 'code-snippets' );
77 $menu_items['snippets_settings'] = __( 'Snippets &raquo; Settings', 'code-snippets' );
78
79 return $menu_items;
80 }
81
82 /**
83 * Prevent the snippet currently being saved from being executed
84 * so that it is not run twice (once normally, once when validated)
85 *
86 * @param bool $exec Whether the snippet will be executed.
87 * @param int $exec_id ID of the snippet being executed.
88 * @param string $table_name Name of the database table the snippet is stored in.
89 *
90 * @return bool Whether the snippet will be executed.
91 */
92 public function prevent_exec_on_save( $exec, $exec_id, $table_name ) {
93
94 if ( ! isset( $_POST['save_snippet'], $_POST['snippet_id'] ) ) {
95 return $exec;
96 }
97
98 if ( code_snippets()->db->get_table_name() !== $table_name ) {
99 return $exec;
100 }
101
102 $id = intval( $_POST['snippet_id'] );
103
104 if ( $id === $exec_id ) {
105 return false;
106 }
107
108 return $exec;
109 }
110
111 /**
112 * Adds a link pointing to the Manage Snippets page
113 *
114 * @param array $links Existing plugin action links.
115 *
116 * @return array Modified plugin action links
117 * @since 2.0.0
118 */
119 public function plugin_settings_link( $links ) {
120 $format = '<a href="%1$s" title="%2$s">%3$s</a>';
121
122 array_unshift(
123 $links,
124 sprintf(
125 $format,
126 esc_url( code_snippets()->get_menu_url( 'settings' ) ),
127 esc_html__( 'Change plugin settings', 'code-snippets' ),
128 esc_html__( 'Settings', 'code-snippets' )
129 )
130 );
131
132 array_unshift(
133 $links,
134 sprintf(
135 $format,
136 esc_url( code_snippets()->get_menu_url() ),
137 esc_html__( 'Manage your existing snippets', 'code-snippets' ),
138 esc_html__( 'Snippets', 'code-snippets' )
139 )
140 );
141
142 return $links;
143 }
144
145 /**
146 * Adds extra links related to the plugin
147 *
148 * @param array $links Existing plugin info links.
149 * @param string $file The plugin the links are for.
150 *
151 * @return array The modified plugin info links.
152 * @since 2.0.0
153 */
154 public function plugin_meta_links( $links, $file ) {
155
156 /* We only want to affect the Code Snippets plugin listing */
157 if ( plugin_basename( PLUGIN_FILE ) !== $file ) {
158 return $links;
159 }
160
161 $format = '<a href="%1$s" title="%2$s" target="_blank">%3$s</a>';
162
163 /* array_merge appends the links to the end */
164
165 return array_merge(
166 $links,
167 array(
168 sprintf(
169 $format,
170 'https://codesnippets.pro/about/',
171 esc_attr__( 'Find out more about Code Snippets', 'code-snippets' ),
172 esc_html__( 'About', 'code-snippets' )
173 ),
174 sprintf(
175 $format,
176 'https://help.codesnippets.pro/',
177 esc_attr__( 'Find out how to get support with Code Snippets', 'code-snippets' ),
178 esc_html__( 'Support', 'code-snippets' )
179 ),
180 sprintf(
181 $format,
182 'https://www.facebook.com/groups/codesnippetsplugin/',
183 esc_attr__( 'Join our community on Facebook', 'code-snippets' ),
184 esc_html__( 'FB Community', 'code-snippets' )
185 ),
186 sprintf(
187 '<a href="%1$s" title="%2$s" style="color: #d46f4d;">%3$s</a>',
188 'https://codesnippets.pro/pricing/',
189 esc_attr__( 'Upgrade to Code Snippets Pro', 'code-snippets' ),
190 esc_html__( 'Upgrade to Pro', 'code-snippets' )
191 ),
192 )
193 );
194 }
195
196 /**
197 * Add Code Snippets information to Site Health information.
198 *
199 * @param array $info The Site Health information.
200 *
201 * @return array The updated Site Health information.
202 * @author sc0ttkclark
203 */
204 public function debug_information( $info ) {
205 $fields = array();
206
207 // fetch all active snippets.
208 $args = array(
209 'active_only' => true,
210 'limit' => 100,
211 );
212 $snippet_objects = get_snippets( array(), null, $args );
213
214 // build the debug information from snippet data.
215 foreach ( $snippet_objects as $snippet ) {
216 $values = [ $snippet->scope_name ];
217 $debug = [];
218
219 if ( $snippet->name ) {
220 $debug[] = 'name: ' . $snippet->name;
221 }
222
223 $debug[] = 'scope: ' . $snippet->scope;
224
225 if ( $snippet->modified ) {
226 /* translators: %s: formatted last modified date */
227 $values[] = sprintf( __( 'Last modified %s', 'code-snippets' ), $snippet->format_modified( false ) );
228 $debug[] = 'modified: ' . $snippet->modified;
229 }
230
231 if ( $snippet->tags ) {
232 $values[] = $snippet->tags_list;
233 $debug[] = 'tags: [' . $snippet->tags_list . ']';
234 }
235
236 $fields[ 'snippet-' . $snippet->id ] = [
237 'label' => $snippet->display_name,
238 'value' => implode( "\n | ", $values ),
239 'debug' => implode( ', ', $debug ),
240 ];
241 }
242
243 $snippets_info = array(
244 'label' => __( 'Active Snippets', 'code-snippets' ),
245 'show_count' => true,
246 'fields' => $fields,
247 );
248
249 // attempt to insert the new section right after the Inactive Plugins section.
250 $index = array_search( 'wp-plugins-inactive', array_keys( $info ), true );
251
252 if ( false === $index ) {
253 $info['code-snippets'] = $snippets_info;
254 } else {
255 $info = array_merge(
256 array_slice( $info, 0, $index + 1 ),
257 [ 'code-snippets' => $snippets_info ],
258 array_slice( $info, $index + 1 )
259 );
260 }
261
262 return $info;
263 }
264
265 /**
266 * Print any admin notices that have not been dismissed.
267 *
268 * @return void
269 */
270 public function print_notices() {
271 global $current_user;
272
273 $key = 'ignore_code_snippets_survey_message';
274 $dismissed = get_user_meta( $current_user->ID, $key, false );
275
276 if ( isset( $_GET[ $key ], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $key ) ) {
277 add_user_meta( $current_user->ID, $key, sanitize_key( wp_unslash( $_GET[ $key ] ) ), false );
278 return;
279 }
280
281 if ( ! in_array( 'pro', $dismissed, true ) ) {
282 $notice = 'pro';
283 $action_url = 'https://codesnippets.pro/pricing/';
284 $action_label = __( 'Upgrade now', 'code-snippets' );
285 $text = __( '<strong>Code Snippets Pro is here!</strong> Find more about the new features in Pro and our introductory launch offers.', 'code-snippets' );
286
287 } elseif ( ! in_array( 'survey', $dismissed, true ) && ! in_array( 'true', $dismissed, true ) ) {
288 $notice = 'survey';
289 $action_url = 'https://codesnippets.pro/survey/';
290 $action_label = __( 'Take the survey now', 'code-snippets' );
291 $text = __( "<strong>Have feedback on Code Snippets?</strong> Please take the time to answer a short survey on how you use this plugin and what you'd like to see changed or added in the future.", 'code-snippets' );
292 } else {
293 return;
294 }
295
296 printf( '<div class="notice notice-info code-snippets-notice code-snippets-%s-notice is-dismissible"><p>', esc_attr( sanitize_key( $notice ) ) );
297 echo wp_kses( $text, [ 'strong' => [] ] );
298
299 printf(
300 '<a href="%s" class="button secondary" target="_blank" style="margin: auto .5em;">%s</a>',
301 esc_url( $action_url ),
302 esc_html( $action_label )
303 );
304
305 printf(
306 '<a href="%s" class="notice-dismiss"><span class="screen-reader-text">%s</span></a>',
307 esc_url( wp_nonce_url( add_query_arg( $key, $notice ), $key ) ),
308 esc_attr__( 'Dismiss', 'code-snippets' )
309 );
310
311 echo '</p></div>';
312 }
313
314 /**
315 * Render a nav tab for a snippet type.
316 *
317 * @param string $type_name Type identifier.
318 * @param string $label Type label.
319 * @param string $current_type Identifier of currently-selected type.
320 *
321 * @return void
322 */
323 public static function render_snippet_type_tab( $type_name, $label, $current_type = '' ) {
324 if ( $type_name === $current_type ) {
325 printf( '<a class="nav-tab nav-tab-active" data-type="%s">', esc_attr( $type_name ) );
326
327 } elseif ( Plugin::is_pro_type( $type_name ) ) {
328 printf(
329 '<a class="nav-tab nav-tab-inactive" data-type="%s" title="%s" href="https://codesnippets.pro/pricing/" target="_blank">',
330 esc_attr( $type_name ),
331 esc_attr__( 'Available in Code Snippets Pro (external link)', 'code-snippets' )
332 );
333
334 } else {
335 printf(
336 '<a class="nav-tab" href="%s" data-type="%s">',
337 esc_url( add_query_arg( 'type', $type_name ) ),
338 esc_attr( $type_name )
339 );
340 }
341
342 echo esc_html( $label ), 'all' === $type_name ? '' : ' <span class="badge">' . esc_html( $type_name ) . '</span>', '</a>';
343 }
344 }
345