PluginProbe
Code Snippets / 4.0.0-beta.2
Code Snippets v4.0.0-beta.2
4.0.0-beta.2 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 All 65 releases
code-snippets / php / Admin / Bootstrap_Admin.php

Bootstrap_Admin.php in Code Snippets 4.0.0-beta.2, at php/Admin/Bootstrap_Admin.php

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