PluginProbe
Code Snippets / 3.5.0
Code Snippets v3.5.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.5.0, at php/class-admin.php

333 lines 9.4 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<string, Admin_Menu>
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
58 /**
59 * Allow super admins to control site admin access to
60 * snippet admin menus
61 *
62 * Adds a checkbox to the *Settings > Network Settings*
63 * network admin menu
64 *
65 * @param array<string, string> $menu_items Current mu menu items.
66 *
67 * @return array<string, string> The modified mu menu items
68 *
69 * @since 1.7.1
70 */
71 public function mu_menu_items( array $menu_items ): array {
72 $menu_items['snippets'] = __( 'Snippets', 'code-snippets' );
73 $menu_items['snippets_settings'] = __( 'Snippets &raquo; Settings', 'code-snippets' );
74
75 return $menu_items;
76 }
77
78 /**
79 * Adds a link pointing to the Manage Snippets page
80 *
81 * @param array<string> $links Existing plugin action links.
82 *
83 * @return array<string> Modified plugin action links
84 * @since 2.0.0
85 */
86 public function plugin_settings_link( array $links ): array {
87 $format = '<a href="%1$s" title="%2$s">%3$s</a>';
88
89 array_unshift(
90 $links,
91 sprintf(
92 $format,
93 esc_url( code_snippets()->get_menu_url( 'settings' ) ),
94 esc_html__( 'Change plugin settings', 'code-snippets' ),
95 esc_html__( 'Settings', 'code-snippets' )
96 )
97 );
98
99 array_unshift(
100 $links,
101 sprintf(
102 $format,
103 esc_url( code_snippets()->get_menu_url() ),
104 esc_html__( 'Manage your existing snippets', 'code-snippets' ),
105 esc_html__( 'Snippets', 'code-snippets' )
106 )
107 );
108
109 return $links;
110 }
111
112 /**
113 * Adds extra links related to the plugin
114 *
115 * @param array<string> $links Existing plugin info links.
116 * @param string $file The plugin the links are for.
117 *
118 * @return array<string> The modified plugin info links.
119 * @since 2.0.0
120 */
121 public function plugin_meta_links( array $links, string $file ): array {
122
123 // We only want to affect the Code Snippets plugin listing.
124 if ( plugin_basename( PLUGIN_FILE ) !== $file ) {
125 return $links;
126 }
127
128 $format = '<a href="%1$s" title="%2$s" target="_blank">%3$s</a>';
129
130 /* array_merge appends the links to the end */
131
132 return array_merge(
133 $links,
134 array(
135 sprintf(
136 $format,
137 'https://help.codesnippets.pro/',
138 esc_attr__( 'Find out more about Code Snippets', 'code-snippets' ),
139 esc_html__( 'Docs & FAQ', 'code-snippets' )
140 ),
141 sprintf(
142 $format,
143 'https://help.codesnippets.pro/',
144 esc_attr__( 'Find out how to get support with Code Snippets', 'code-snippets' ),
145 esc_html__( 'Support', 'code-snippets' )
146 ),
147 sprintf(
148 $format,
149 'https://www.facebook.com/groups/codesnippetsplugin/',
150 esc_attr__( 'Join our community on Facebook', 'code-snippets' ),
151 esc_html__( 'FB Community', 'code-snippets' )
152 ),
153 sprintf(
154 '<a href="%1$s" title="%2$s" style="color: #d46f4d;">%3$s</a>',
155 'https://codesnippets.pro/pricing/',
156 esc_attr__( 'Upgrade to Code Snippets Pro', 'code-snippets' ),
157 esc_html__( 'Upgrade to Pro', 'code-snippets' )
158 ),
159 )
160 );
161 }
162
163 /**
164 * Add Code Snippets information to Site Health information.
165 *
166 * @param array<string, array<string, mixed>> $info Current Site Health information.
167 *
168 * @return array<string, array<string, mixed>> Updated Site Health information.
169 * @author sc0ttkclark
170 */
171 public function debug_information( array $info ): array {
172 $fields = array();
173
174 // build the debug information from snippet data.
175 foreach ( get_snippets() as $snippet ) {
176 $values = [ $snippet->scope_name ];
177 $debug = [];
178
179 if ( ! $snippet->active ) {
180 continue;
181 }
182
183 if ( $snippet->name ) {
184 $debug[] = 'name: ' . $snippet->name;
185 }
186
187 $debug[] = 'scope: ' . $snippet->scope;
188
189 if ( $snippet->modified ) {
190 /* translators: %s: formatted last modified date */
191 $values[] = sprintf( __( 'Last modified %s', 'code-snippets' ), $snippet->format_modified( false ) );
192 $debug[] = 'modified: ' . $snippet->modified;
193 }
194
195 if ( $snippet->tags ) {
196 $values[] = $snippet->tags_list;
197 $debug[] = 'tags: [' . $snippet->tags_list . ']';
198 }
199
200 $fields[ 'snippet-' . $snippet->id ] = [
201 'label' => $snippet->display_name,
202 'value' => implode( "\n | ", $values ),
203 'debug' => implode( ', ', $debug ),
204 ];
205 }
206
207 $snippets_info = array(
208 'label' => __( 'Active Snippets', 'code-snippets' ),
209 'show_count' => true,
210 'fields' => $fields,
211 );
212
213 // attempt to insert the new section right after the Inactive Plugins section.
214 $index = array_search( 'wp-plugins-inactive', array_keys( $info ), true );
215
216 if ( false === $index ) {
217 $info['code-snippets'] = $snippets_info;
218 } else {
219 $info = array_merge(
220 array_slice( $info, 0, $index + 1 ),
221 [ 'code-snippets' => $snippets_info ],
222 array_slice( $info, $index + 1 )
223 );
224 }
225
226 return $info;
227 }
228
229 /**
230 * Print any admin notices that have not been dismissed.
231 *
232 * @return void
233 */
234 public function print_notices() {
235 global $current_user;
236
237 $key = 'ignore_code_snippets_survey_message';
238 $dismissed = get_user_meta( $current_user->ID, $key );
239
240 if ( isset( $_GET[ $key ], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $key ) ) {
241 add_user_meta( $current_user->ID, $key, sanitize_key( wp_unslash( $_GET[ $key ] ) ) );
242 return;
243 }
244
245 if ( ! in_array( 'pro', $dismissed, true ) ) {
246 $notice = 'pro';
247 $action_url = 'https://codesnippets.pro/pricing/';
248 $action_label = __( 'Upgrade now', 'code-snippets' );
249 $text = __( '<strong>Code Snippets Pro is here!</strong> Find more about the new features in Pro and our introductory launch offers.', 'code-snippets' );
250
251 } elseif ( ! in_array( 'survey', $dismissed, true ) && ! in_array( 'true', $dismissed, true ) ) {
252 $notice = 'survey';
253 $action_url = 'https://codesnippets.pro/survey/';
254 $action_label = __( 'Take the survey now', 'code-snippets' );
255 $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' );
256 } else {
257 return;
258 }
259
260 printf( '<div class="notice notice-info code-snippets-notice code-snippets-%s-notice is-dismissible"><p>', esc_attr( sanitize_key( $notice ) ) );
261 echo wp_kses( $text, [ 'strong' => [] ] );
262
263 printf(
264 '<a href="%s" class="button secondary" target="_blank" style="margin: auto .5em;">%s</a>',
265 esc_url( $action_url ),
266 esc_html( $action_label )
267 );
268
269 printf(
270 '<a href="%s" class="notice-dismiss"><span class="screen-reader-text">%s</span></a>',
271 esc_url( wp_nonce_url( add_query_arg( $key, $notice ), $key ) ),
272 esc_attr__( 'Dismiss', 'code-snippets' )
273 );
274
275 echo '</p></div>';
276 }
277
278 /**
279 * Render a nav tab for a snippet type.
280 *
281 * @param string $type_name Type identifier.
282 * @param string $label Type label.
283 * @param string $current_type Identifier of currently-selected type.
284 *
285 * @return void
286 */
287 public static function render_snippet_type_tab( string $type_name, string $label, string $current_type = '' ) {
288 if ( $type_name === $current_type ) {
289 printf( '<a class="nav-tab nav-tab-active" data-snippet-type="%s">', esc_attr( $type_name ) );
290
291 } elseif ( Plugin::is_pro_type( $type_name ) ) {
292 printf(
293 '<a class="nav-tab nav-tab-inactive" data-snippet-type="%s" title="%s" href="https://codesnippets.pro/pricing/" target="_blank">',
294 esc_attr( $type_name ),
295 esc_attr__( 'Available in Code Snippets Pro (external link)', 'code-snippets' )
296 );
297
298 } else {
299 $current_url = remove_query_arg( [ 'cloud_select', 'cloud_search' ] );
300
301 printf(
302 '<a class="nav-tab" href="%s" data-snippet-type="%s">',
303 esc_url( add_query_arg( 'type', $type_name, $current_url ) ),
304 esc_attr( $type_name )
305 );
306 }
307
308 echo esc_html( $label );
309
310 switch ( $type_name ) {
311 case 'all':
312 break;
313 case 'cloud':
314 echo '<span class="cloud-badge dashicons dashicons-cloud cloud-icon cloud-synced"></span>';
315 break;
316 case 'cloud_search':
317 echo '<span class="cloud-badge dashicons dashicons-search cloud-icon cloud-downloaded"></span>';
318 break;
319 case 'bundles':
320 echo '<span class="cloud-badge dashicons dashicons-screenoptions cloud-icon cloud-bundle"></span>';
321 break;
322 case 'ai':
323 echo '<span class="cloud-badge ai-icon">', esc_html__( 'AI', 'code-snippets' ), '</span>';
324 break;
325 default:
326 echo '<span class="badge">' . esc_html( $type_name ) . '</span>';
327 break;
328 }
329
330 echo '</a>';
331 }
332 }
333