PluginProbe
Code Snippets / 3.7.0
Code Snippets v3.7.0
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.7.0, at php/class-admin.php

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