PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
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.1, at php/Admin/Bootstrap_Admin.php

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