PluginProbe
Code Snippets / 3.9.1
Code Snippets v3.9.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-admin.php

class-admin.php in Code Snippets 3.9.1, at php/class-admin.php

424 lines 12.0 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 use DateTimeImmutable;
6 use DateTimeZone;
7 use Exception;
8
9 /**
10 * Functions specific to the administration interface
11 *
12 * @package Code_Snippets
13 */
14 class Admin {
15
16 /**
17 * Admin_Menu class instances
18 *
19 * @var array<string, Admin_Menu>
20 */
21 public array $menus = array();
22
23 /**
24 * Welcome_API class instance.
25 *
26 * @var Welcome_API
27 */
28 public Welcome_API $welcome_api;
29
30 /**
31 * Class constructor
32 */
33 public function __construct() {
34 if ( is_admin() ) {
35 $this->welcome_api = new Welcome_API();
36 $this->run();
37 }
38 }
39
40 /**
41 * Initialise classes
42 */
43 public function load_classes() {
44 $this->menus['manage'] = new Manage_Menu();
45 $this->menus['edit'] = new Edit_Menu();
46 $this->menus['import'] = new Import_Menu();
47
48 if ( is_network_admin() === Settings\are_settings_unified() ) {
49 $this->menus['settings'] = new Settings_Menu();
50 }
51
52 $this->menus['welcome'] = new Welcome_Menu( $this->welcome_api );
53
54 foreach ( $this->menus as $menu ) {
55 $menu->run();
56 }
57 }
58
59 /**
60 * Register action and filter hooks
61 */
62 public function run() {
63 add_action( 'init', array( $this, 'load_classes' ), 11 );
64
65 add_filter( 'mu_menu_items', array( $this, 'mu_menu_items' ) );
66 add_filter( 'manage_sites_action_links', array( $this, 'add_sites_row_action' ), 10, 2 );
67 add_filter( 'plugin_action_links_' . plugin_basename( PLUGIN_FILE ), array( $this, 'plugin_action_links' ), 10, 2 );
68 add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
69 add_filter( 'debug_information', array( $this, 'debug_information' ) );
70 add_action( 'code_snippets/admin/manage', array( $this, 'print_notices' ) );
71 }
72
73 /**
74 * Allow super admins to control site admin access to
75 * snippet admin menus
76 *
77 * Adds a checkbox to the *Settings > Network Settings*
78 * network admin menu
79 *
80 * @param array<string, string> $menu_items Current mu menu items.
81 *
82 * @return array<string, string> The modified mu menu items
83 *
84 * @since 1.7.1
85 */
86 public function mu_menu_items( array $menu_items ): array {
87 $menu_items['snippets'] = __( 'Snippets', 'code-snippets' );
88 $menu_items['snippets_settings'] = __( 'Snippets &raquo; Settings', 'code-snippets' );
89
90 return $menu_items;
91 }
92
93 /**
94 * Add a "Snippets" row action to the Network Sites table.
95 *
96 * @param array<string, string> $actions Existing row actions.
97 * @param int $site_id Current site ID.
98 *
99 * @return array<string, string>
100 */
101 public function add_sites_row_action( array $actions, int $site_id ): array {
102 if ( ! is_multisite() || ! current_user_can( code_snippets()->get_network_cap_name() ) ) {
103 return $actions;
104 }
105
106 $menu_slug = code_snippets()->get_menu_slug();
107 $actions['code_snippets'] = sprintf(
108 '<a href="%s">%s</a>',
109 esc_url( get_admin_url( $site_id, 'admin.php?page=' . $menu_slug ) ),
110 esc_html__( 'Snippets', 'code-snippets' )
111 );
112
113 return $actions;
114 }
115
116 /**
117 * Modify the action links for this plugin.
118 *
119 * @param array<string> $actions Existing plugin action links.
120 * @param string $plugin_file The plugin the links are for.
121 *
122 * @return array<string> Modified plugin action links.
123 * @since 2.0.0
124 */
125 public function plugin_action_links( array $actions, string $plugin_file ): array {
126 if ( plugin_basename( PLUGIN_FILE ) !== $plugin_file ) {
127 return $actions;
128 }
129
130 $format = '<a href="%1$s" title="%2$s">%3$s</a>';
131
132 $actions = array_merge(
133 [
134 sprintf(
135 $format,
136 esc_url( code_snippets()->get_menu_url( 'settings' ) ),
137 esc_attr__( 'Change plugin settings', 'code-snippets' ),
138 esc_html__( 'Settings', 'code-snippets' )
139 ),
140 sprintf(
141 $format,
142 esc_url( code_snippets()->get_menu_url() ),
143 esc_attr__( 'Manage your existing snippets', 'code-snippets' ),
144 esc_html__( 'Snippets', 'code-snippets' )
145 ),
146 ],
147 $actions
148 );
149
150 if ( ! code_snippets()->licensing->is_licensed() ) {
151 $actions[] = sprintf(
152 '<a href="%1$s" title="%2$s" style="color: #d46f4d; font-weight: bold;" target="_blank">%3$s</a>',
153 'https://snipco.de/JE2i',
154 esc_attr__( 'Upgrade to Code Snippets Pro', 'code-snippets' ),
155 esc_attr__( 'Upgrade to Pro', 'code-snippets' )
156 );
157 }
158 return $actions;
159 }
160
161 /**
162 * Adds extra links related to the plugin
163 *
164 * @param array<string> $plugin_meta Existing plugin info links.
165 * @param string $plugin_file The plugin the links are for.
166 *
167 * @return array<string> Modified plugin info links.
168 * @since 2.0.0
169 */
170 public function plugin_row_meta( array $plugin_meta, string $plugin_file ): array {
171 if ( plugin_basename( PLUGIN_FILE ) !== $plugin_file ) {
172 return $plugin_meta;
173 }
174
175 $format = '<a href="%1$s" title="%2$s" target="_blank">%3$s</a>';
176
177 return array_merge(
178 $plugin_meta,
179 array(
180 sprintf(
181 $format,
182 'https://help.codesnippets.pro/',
183 esc_attr__( 'Find out how to get support with Code Snippets', 'code-snippets' ),
184 esc_html__( 'Docs and Support', 'code-snippets' )
185 ),
186 sprintf(
187 $format,
188 'https://www.facebook.com/groups/codesnippetsplugin/',
189 esc_attr__( 'Join our community on Facebook', 'code-snippets' ),
190 esc_html__( 'Community', 'code-snippets' )
191 ),
192 )
193 );
194 }
195
196 /**
197 * Add Code Snippets information to Site Health information.
198 *
199 * @param array<string, array<string, mixed>> $info Current Site Health information.
200 *
201 * @return array<string, array<string, mixed>> Updated Site Health information.
202 * @author sc0ttkclark
203 */
204 public function debug_information( array $info ): array {
205 $fields = array();
206
207 // build the debug information from snippet data.
208 foreach ( get_snippets() as $snippet ) {
209 $values = [ $snippet->scope_name ];
210 $debug = [];
211
212 if ( ! $snippet->active ) {
213 continue;
214 }
215
216 if ( $snippet->name ) {
217 $debug[] = 'name: ' . $snippet->name;
218 }
219
220 $debug[] = 'scope: ' . $snippet->scope;
221
222 if ( $snippet->modified ) {
223 /* translators: %s: formatted last modified date */
224 $values[] = sprintf( __( 'Last modified %s', 'code-snippets' ), $snippet->format_modified( false ) );
225 $debug[] = 'modified: ' . $snippet->modified;
226 }
227
228 if ( $snippet->tags ) {
229 $values[] = $snippet->tags_list;
230 $debug[] = 'tags: [' . $snippet->tags_list . ']';
231 }
232
233 $fields[ 'snippet-' . $snippet->id ] = [
234 'label' => $snippet->display_name,
235 'value' => implode( "\n | ", $values ),
236 'debug' => implode( ', ', $debug ),
237 ];
238 }
239
240 $snippets_info = array(
241 'label' => __( 'Active Snippets', 'code-snippets' ),
242 'show_count' => true,
243 'fields' => $fields,
244 );
245
246 // attempt to insert the new section right after the Inactive Plugins section.
247 $index = array_search( 'wp-plugins-inactive', array_keys( $info ), true );
248
249 if ( false === $index ) {
250 $info['code-snippets'] = $snippets_info;
251 } else {
252 $info = array_merge(
253 array_slice( $info, 0, $index + 1 ),
254 [ 'code-snippets' => $snippets_info ],
255 array_slice( $info, $index + 1 )
256 );
257 }
258
259 return $info;
260 }
261
262 /**
263 * Print any admin notices that have not been dismissed.
264 *
265 * @return void
266 */
267 public function print_notices() {
268 global $current_user;
269
270 if ( apply_filters( 'code_snippets/hide_welcome_banner', false ) ) {
271 return;
272 }
273
274 $meta_key = 'ignore_code_snippets_survey_message';
275 $dismissed = get_user_meta( $current_user->ID, $meta_key, false );
276
277 if ( isset( $_GET[ $meta_key ], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $meta_key ) ) {
278 add_user_meta( $current_user->ID, $meta_key, sanitize_key( wp_unslash( $_GET[ $meta_key ] ) ) );
279 return;
280 }
281
282 $welcome = $this->welcome_api->get_banner();
283
284 try {
285 $now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
286 } catch ( Exception $e ) {
287 $now = $welcome['start_datetime'];
288 }
289
290 if ( ! empty( $welcome['key'] ) && ! in_array( $welcome['key'], $dismissed, true ) &&
291 ( empty( $welcome['start_datetime'] ) || $now >= $welcome['start_datetime'] ) &&
292 ( empty( $welcome['end_datetime'] ) || $now <= $welcome['end_datetime'] ) ) {
293 $notice = $welcome['key'];
294
295 $text = $welcome['text_free'];
296 $action_url = $welcome['action_url_free'];
297 $action_label = $welcome['action_label_free'];
298
299 } elseif ( ! in_array( 'survey', $dismissed, true ) && ! in_array( 'true', $dismissed, true ) ) {
300 $notice = 'survey';
301 $action_url = 'https://codesnippets.pro/survey/';
302 $action_label = __( 'Take the survey now', 'code-snippets' );
303 $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' );
304 } else {
305 return;
306 }
307
308 printf(
309 '<div class="notice notice-info code-snippets-notice code-snippets-%s-notice is-dismissible"><p>',
310 esc_attr( sanitize_key( $notice ) )
311 );
312
313 echo wp_kses_post( $text );
314
315 printf(
316 '<a href="%s" class="button button-secondary" target="_blank" style="margin-block: auto; margin-inline: .5em;">%s</a>',
317 esc_url( $action_url ),
318 esc_html( $action_label )
319 );
320
321 printf(
322 '<a href="%s" class="notice-dismiss"><span class="screen-reader-text">%s</span></a>',
323 esc_url( wp_nonce_url( add_query_arg( $meta_key, $notice ), $meta_key ) ),
324 esc_html__( 'Dismiss', 'code-snippets' )
325 );
326
327 echo '</p></div>';
328 }
329
330 /**
331 * Render a badge for a snippet type in the nav tabs.
332 *
333 * @param string $type_name Identifier of the snippet type.
334 */
335 private static function render_snippet_tab_badge( string $type_name ) {
336 if ( 'all' !== $type_name ) {
337 printf( '<span class="badge %s-badge">', esc_attr( $type_name ) );
338
339 switch ( $type_name ) {
340 case 'cloud':
341 echo '<span class="dashicons dashicons-cloud"></span>';
342 break;
343 case 'cloud_search':
344 echo '<span class="dashicons dashicons-search"></span>';
345 break;
346 case 'bundles':
347 echo '<span class="dashicons dashicons-screenoptions"></span>';
348 break;
349 case 'ai':
350 echo '<span class="ai-icon">', esc_html__( 'AI', 'code-snippets' ), '</span>';
351 break;
352 case 'cond':
353 echo '<span class="dashicons dashicons-randomize"></span>';
354 break;
355 default:
356 echo esc_html( $type_name );
357 break;
358 }
359
360 echo '</span>';
361 }
362 }
363
364 /**
365 * Render a nav tab for a snippet type.
366 *
367 * @param array{string, string} $type_labels Associative array of snippet type identifiers and their labels.
368 * @param string $current_type Identifier of currently-selected type.
369 *
370 * @return void
371 */
372 public static function render_snippet_type_tabs( array $type_labels, string $current_type = '' ) {
373 $is_licensed = code_snippets()->licensing->is_licensed();
374 $pro_types = [ 'css', 'js', 'cond', 'cloud', 'bundles' ];
375 $cloud_tabs = [ 'cloud', 'bundles' ];
376
377 foreach ( $type_labels as $type_name => $label ) {
378 if ( ! $is_licensed && in_array( $type_name, $pro_types, true ) ) {
379 continue;
380 }
381
382 if ( $type_name === $current_type ) {
383 printf( '<a class="nav-tab nav-tab-active %s-tab">', esc_attr( $type_name ) );
384 } else {
385 $current_url = remove_query_arg( [ 'cloud_select', 'cloud_search' ] );
386 $nav_tab_inactive = in_array( $type_name, $cloud_tabs, true ) && ! code_snippets()->cloud_api->is_cloud_key_verified();
387
388 printf(
389 '<a class="%s %s-tab" href="%s">',
390 $nav_tab_inactive ? 'nav-tab nav-tab-inactive' : 'nav-tab',
391 esc_attr( $type_name ),
392 esc_url( add_query_arg( 'type', $type_name, $current_url ) )
393 );
394 }
395
396 printf(
397 '<span class="%s">%s</span>',
398 esc_attr( 'all' === $type_name ? 'all-snippets-label' : 'snippet-label' ),
399 esc_html( $label )
400 );
401
402 self::render_snippet_tab_badge( $type_name );
403 echo '</a>';
404 }
405
406 foreach ( $type_labels as $type_name => $label ) {
407 if ( $is_licensed || ! in_array( $type_name, $pro_types, true ) ) {
408 continue;
409 }
410
411 printf(
412 '<a class="nav-tab nav-tab-inactive %s-tab" href="%s" target="_blank" aria-label="%s">%s',
413 esc_attr( $type_name ),
414 esc_url( 'https://codesnippets.pro/pricing/' ),
415 esc_attr__( 'Find more about Pro (opens in external tab)', 'code-snippets' ),
416 esc_html( $label )
417 );
418
419 self::render_snippet_tab_badge( $type_name );
420 echo '</a>';
421 }
422 }
423 }
424