PluginProbe
Code Snippets / 3.4.1
Code Snippets v3.4.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.4.1, at php/class-admin.php

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