PluginProbe
Code Snippets / 3.10.2
Code Snippets v3.10.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 3.0.1 All 64 releases
code-snippets / php / Admin / Bootstrap_Admin.php

Bootstrap_Admin.php in Code Snippets 3.10.2, at php/Admin/Bootstrap_Admin.php

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