PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.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 / Admin / Contextual_Help.php

Contextual_Help.php in Code Snippets 3.10.0, at php/Admin/Contextual_Help.php

206 lines 6.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 WP_Screen;
6 use function Code_Snippets\code_snippets;
7
8 /**
9 * This file holds all the content for the contextual help screens.
10 *
11 * @package Code_Snippets
12 */
13 class Contextual_Help {
14
15 /**
16 * Current screen object
17 *
18 * @see get_current_screen()
19 *
20 * @var WP_Screen
21 */
22 public WP_Screen $screen;
23
24 /**
25 * Name of current screen
26 *
27 * @see get_current_screen()
28 *
29 * @var string
30 */
31 public string $screen_name;
32
33 /**
34 * Class constructor
35 *
36 * @param string $screen_name Name of current screen.
37 */
38 public function __construct( string $screen_name ) {
39 $this->screen_name = $screen_name;
40 }
41
42 /**
43 * Load the contextual help
44 */
45 public function load() {
46 $this->screen = get_current_screen();
47
48 switch ( $this->screen_name ) {
49 case 'manage':
50 $this->load_manage_help();
51 break;
52
53 case 'edit':
54 $this->load_edit_help();
55 break;
56
57 case 'import':
58 $this->load_import_help();
59 break;
60 }
61
62 $this->load_help_sidebar();
63 }
64
65 /**
66 * Load the help sidebar
67 */
68 private function load_help_sidebar() {
69 $sidebar_links = [
70 'https://wordpress.org/plugins/code-snippets' => __( 'About Plugin', 'code-snippets' ),
71 'https://codesnippets.pro/docs/faq/' => __( 'FAQ', 'code-snippets' ),
72 'https://wordpress.org/support/plugin/code-snippets' => __( 'Support Forum', 'code-snippets' ),
73 'https://codesnippets.pro' => __( 'Plugin Website', 'code-snippets' ),
74 ];
75
76 $allowed_html = [
77 'p' => [],
78 'strong' => [],
79 'a' => [ 'href' => [] ],
80 ];
81
82 $contents = sprintf( "<p><strong>%s</strong></p>\n", esc_html__( 'For more information:', 'code-snippets' ) );
83
84 foreach ( $sidebar_links as $url => $label ) {
85 $contents .= "\n" . sprintf( '<p><a href="%s">%s</a></p>', esc_url( $url ), esc_html( $label ) );
86 }
87
88 $this->screen->set_help_sidebar( wp_kses( $contents, $allowed_html ) );
89 }
90
91 /**
92 * Add a help tab to the current screen.
93 *
94 * @param string $id Screen ID.
95 * @param string $title Screen title.
96 * @param string|array<string> $paragraphs List of paragraphs to display as content.
97 *
98 * @return void
99 */
100 private function add_help_tab( string $id, string $title, $paragraphs ) {
101 $this->screen->add_help_tab(
102 array(
103 'title' => $title,
104 'id' => $id,
105 'content' => wp_kses_post(
106 implode(
107 "\n",
108 array_map(
109 function ( $content ) {
110 return '<p>' . $content . '</p>';
111 },
112 is_array( $paragraphs ) ? $paragraphs : [ $paragraphs ]
113 )
114 )
115 ),
116 )
117 );
118 }
119
120 /**
121 * Reusable introduction text
122 *
123 * @return string
124 */
125 private function get_intro_text(): string {
126 return __( 'Snippets are similar to plugins - they both extend and expand the functionality of WordPress. Snippets are more light-weight, just a few lines of code, and do not put as much load on your server. ', 'code-snippets' );
127 }
128
129 /**
130 * Register and handle the help tabs for the manage snippets admin page
131 */
132 private function load_manage_help() {
133 $this->add_help_tab(
134 'overview',
135 __( 'Overview', 'code-snippets' ),
136 $this->get_intro_text() .
137 __( 'Here you can manage your existing snippets and perform tasks on them such as activating, deactivating, deleting and exporting.', 'code-snippets' )
138 );
139
140 $this->add_help_tab(
141 'safe-mode',
142 __( 'Safe Mode', 'code-snippets' ),
143 [
144 __( 'Be sure to check your snippets for errors before you activate them, as a faulty snippet could bring your whole blog down. If your site starts doing strange things, deactivate all your snippets and activate them one at a time.', 'code-snippets' ),
145 __( "If something goes wrong with a snippet, and you can't use WordPress, you can cause all snippets to stop executing by turning on <strong>safe mode</strong>.", 'code-snippets' ),
146 /* translators: %s: URL to Code Snippets Pro Docs */
147 sprintf( __( 'You can find out how to enable safe mode in the <a href="%s">Code Snippets Pro Docs</a>.', 'code-snippets' ), 'https://codesnippets.pro/doc/safe-mode/' ),
148 ]
149 );
150 }
151
152 /**
153 * Register and handle the help tabs for the single snippet admin page
154 */
155 private function load_edit_help() {
156 $this->add_help_tab(
157 'overview',
158 __( 'Overview', 'code-snippets' ),
159 [
160 $this->get_intro_text() .
161 __( 'Here you can add a new snippet, or edit an existing one.', 'code-snippets' ),
162 /* translators: %s: URL to Code Snippets Pro Docs */
163 sprintf( __( "If you're not sure about the types of snippets you can add, take a look at the <a href=\"%s\">Code Snippets Pro Docs</a> for inspiration.", 'code-snippets' ), 'https://codesnippets.pro/docs/adding-snippets/' ),
164 ]
165 );
166
167 $this->add_help_tab(
168 'adding',
169 __( 'Adding Snippets', 'code-snippets' ),
170 [
171 __( 'You need to fill out the name and code fields for your snippet to be added. While the description field will add more information about how your snippet works, what is does and where you found it, it is completely optional.', 'code-snippets' ),
172 __( 'Please be sure to check that your snippet is valid PHP code and will not produce errors before adding it through this page. While doing so will not become active straight away, it will help to minimize the chance of a faulty snippet becoming active on your site.', 'code-snippets' ),
173 ]
174 );
175 }
176
177 /**
178 * Register and handle the help tabs for the import snippets admin page
179 */
180 private function load_import_help() {
181 $manage_url = code_snippets()->get_menu_url( 'manage' );
182
183 $this->add_help_tab(
184 'overview',
185 __( 'Overview', 'code-snippets' ),
186 $this->get_intro_text() .
187 __( 'Here you can load snippets from a code snippets export file into the database alongside existing snippets.', 'code-snippets' )
188 );
189
190 $this->add_help_tab(
191 'import',
192 __( 'Importing', 'code-snippets' ),
193 __( 'You can load your snippets from a code snippets export file using this page.', 'code-snippets' ) .
194 /* translators: %s: URL to Snippets admin menu */
195 sprintf( __( 'Imported snippets will be added to the database along with your existing snippets. Regardless of whether the snippets were active on the previous site, imported snippets are always inactive until activated using the <a href="%s">Manage Snippets</a> page.', 'code-snippets' ), $manage_url )
196 );
197
198 $this->add_help_tab(
199 'export',
200 __( 'Exporting', 'code-snippets' ),
201 /* translators: %s: URL to Manage Snippets admin menu */
202 sprintf( __( 'You can save your snippets to a code snippets export file using the <a href="%s">Manage Snippets</a> page.', 'code-snippets' ), $manage_url )
203 );
204 }
205 }
206