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

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