PluginProbe
Code Snippets / 3.8.1
Code Snippets v3.8.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-contextual-help.php

class-contextual-help.php in Code Snippets 3.8.1, at php/class-contextual-help.php

205 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;
4
5 use WP_Screen;
6
7 /**
8 * This file holds all the content for the contextual help screens.
9 *
10 * @package Code_Snippets
11 */
12 class Contextual_Help {
13
14 /**
15 * Current screen object
16 *
17 * @see get_current_screen()
18 *
19 * @var WP_Screen
20 */
21 public WP_Screen $screen;
22
23 /**
24 * Name of current screen
25 *
26 * @see get_current_screen()
27 *
28 * @var string
29 */
30 public string $screen_name;
31
32 /**
33 * Class constructor
34 *
35 * @param string $screen_name Name of current screen.
36 */
37 public function __construct( string $screen_name ) {
38 $this->screen_name = $screen_name;
39 }
40
41 /**
42 * Load the contextual help
43 */
44 public function load() {
45 $this->screen = get_current_screen();
46
47 switch ( $this->screen_name ) {
48 case 'manage':
49 $this->load_manage_help();
50 break;
51
52 case 'edit':
53 $this->load_edit_help();
54 break;
55
56 case 'import':
57 $this->load_import_help();
58 break;
59 }
60
61 $this->load_help_sidebar();
62 }
63
64 /**
65 * Load the help sidebar
66 */
67 private function load_help_sidebar() {
68 $sidebar_links = [
69 'https://wordpress.org/plugins/code-snippets' => __( 'About Plugin', 'code-snippets' ),
70 'https://help.codesnippets.pro/collection/3-faq' => __( 'FAQ', 'code-snippets' ),
71 'https://wordpress.org/support/plugin/code-snippets' => __( 'Support Forum', 'code-snippets' ),
72 'https://codesnippets.pro' => __( 'Plugin Website', 'code-snippets' ),
73 ];
74
75 $kses = [
76 'p' => [],
77 'strong' => [],
78 'a' => [ 'href' => [] ],
79 ];
80
81 $contents = sprintf( "<p><strong>%s</strong></p>\n", esc_html__( 'For more information:', 'code-snippets' ) );
82
83 foreach ( $sidebar_links as $url => $label ) {
84 $contents .= "\n" . sprintf( '<p><a href="%s">%s</a></p>', esc_url( $url ), esc_html( $label ) );
85 }
86
87 $this->screen->set_help_sidebar( wp_kses( $contents, $kses ) );
88 }
89
90 /**
91 * Add a help tab to the current screen.
92 *
93 * @param string $id Screen ID.
94 * @param string $title Screen title.
95 * @param string|array<string> $paragraphs List of paragraphs to display as content.
96 *
97 * @return void
98 */
99 private function add_help_tab( string $id, string $title, $paragraphs ) {
100 $this->screen->add_help_tab(
101 array(
102 'title' => $title,
103 'id' => $id,
104 'content' => wp_kses_post(
105 implode(
106 "\n",
107 array_map(
108 function ( $content ) {
109 return '<p>' . $content . '</p>';
110 },
111 is_array( $paragraphs ) ? $paragraphs : [ $paragraphs ]
112 )
113 )
114 ),
115 )
116 );
117 }
118
119 /**
120 * Reusable introduction text
121 *
122 * @return string
123 */
124 private function get_intro_text(): string {
125 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' );
126 }
127
128 /**
129 * Register and handle the help tabs for the manage snippets admin page
130 */
131 private function load_manage_help() {
132 $this->add_help_tab(
133 'overview',
134 __( 'Overview', 'code-snippets' ),
135 $this->get_intro_text() .
136 __( 'Here you can manage your existing snippets and perform tasks on them such as activating, deactivating, deleting and exporting.', 'code-snippets' )
137 );
138
139 $this->add_help_tab(
140 'safe-mode',
141 __( 'Safe Mode', 'code-snippets' ),
142 [
143 __( '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' ),
144 __( "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' ),
145 /* translators: %s: URL to Code Snippets Pro Docs */
146 sprintf( __( 'You can find out how to enable safe mode in the <a href="%s">Code Snippets Pro Docs</a>.', 'code-snippets' ), 'https://help.codesnippets.pro/article/12-safe-mode' )
147 ]
148 );
149 }
150
151 /**
152 * Register and handle the help tabs for the single snippet admin page
153 */
154 private function load_edit_help() {
155 $this->add_help_tab(
156 'overview',
157 __( 'Overview', 'code-snippets' ),
158 [
159 $this->get_intro_text() .
160 __( 'Here you can add a new snippet, or edit an existing one.', 'code-snippets' ),
161 /* translators: %s: URL to Code Snippets Pro Docs */
162 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://help.codesnippets.pro/collection/2-adding-snippets' ),
163 ]
164 );
165
166 $this->add_help_tab(
167 'adding',
168 __( 'Adding Snippets', 'code-snippets' ),
169 [
170 __( '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' ),
171 __( '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' ),
172 ]
173 );
174 }
175
176 /**
177 * Register and handle the help tabs for the import snippets admin page
178 */
179 private function load_import_help() {
180 $manage_url = code_snippets()->get_menu_url( 'manage' );
181
182 $this->add_help_tab(
183 'overview',
184 __( 'Overview', 'code-snippets' ),
185 $this->get_intro_text() .
186 __( 'Here you can load snippets from a code snippets export file into the database alongside existing snippets.', 'code-snippets' )
187 );
188
189 $this->add_help_tab(
190 'import',
191 __( 'Importing', 'code-snippets' ),
192 __( 'You can load your snippets from a code snippets export file using this page.', 'code-snippets' ) .
193 /* translators: %s: URL to Snippets admin menu */
194 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 )
195 );
196
197 $this->add_help_tab(
198 'export',
199 __( 'Exporting', 'code-snippets' ),
200 /* translators: %s: URL to Manage Snippets admin menu */
201 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 )
202 );
203 }
204 }
205