PluginProbe
Pronamic Client / trunk
Pronamic Client vtrunk
2.4.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 All 54 releases
pronamic-client / classes / Admin.php

Admin.php in Pronamic Client trunk, at classes/Admin.php

411 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 Pronamic\WordPress\PronamicClient;
4
5 class Admin {
6 /**
7 * Instance of this class.
8 *
9 * @since 1.1.0
10 *
11 * @var self
12 */
13 protected static $instance = null;
14
15 /**
16 * Plugin
17 *
18 * @var Plugin
19 */
20 private $plugin;
21
22 /**
23 * Constructs and initialize admin
24 */
25 private function __construct( Plugin $plugin ) {
26 $this->plugin = $plugin;
27
28 // Includes
29 foreach ( glob( $this->plugin->dir_path . 'admin/includes/*.php' ) as $filename ) {
30 require_once $filename;
31 }
32
33 // Actions
34 add_action( 'admin_init', [ $this, 'admin_init' ] );
35
36 add_action( 'admin_menu', [ $this, 'admin_menu' ] );
37
38 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
39
40 add_action( 'wp_dashboard_setup', [ $this, 'dashboard_setup' ] );
41 }
42
43 /**
44 * Admin initialize
45 */
46 public function admin_init() {
47 // Maybe update
48 global $pronamic_client_db_version;
49
50 if ( get_option( 'pronamic_client_db_version' ) !== $pronamic_client_db_version ) {
51 $this->upgrade();
52
53 update_option( 'pronamic_client_db_version', $pronamic_client_db_version );
54 }
55
56 // Test email.
57 if ( filter_has_var( INPUT_POST, 'pronamic_client_send_test_email' ) ) {
58 $this->maybe_send_test_email();
59 }
60 }
61
62 /**
63 * Maybe send test email.
64 *
65 * @link https://developer.wordpress.org/reference/functions/wp_nonce_field/
66 * @link https://developer.wordpress.org/reference/functions/wp_mail/
67 * @link https://plugins.trac.wordpress.org/browser/check-email/trunk/check-email.php#L73
68 */
69 private function maybe_send_test_email() {
70 if ( ! \current_user_can( 'pronamic_client' ) ) {
71 return;
72 }
73
74 if ( ! array_key_exists( 'pronamic_client_send_test_email_nonce', $_POST ) ) {
75 return;
76 }
77
78 $nonce = \sanitize_text_field( \wp_unslash( $_POST['pronamic_client_send_test_email_nonce'] ) );
79
80 if ( ! wp_verify_nonce( $nonce, 'pronamic_client_send_test_email' ) ) {
81 return;
82 }
83
84 $email_data = filter_input( INPUT_POST, 'pronamic_client_test_email', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY );
85
86 if ( null === $email_data ) {
87 return;
88 }
89
90 $email = (object) filter_var_array(
91 $email_data,
92 [
93 'from' => \FILTER_VALIDATE_EMAIL,
94 'to' => \FILTER_VALIDATE_EMAIL,
95 'subject' => \FILTER_UNSAFE_RAW,
96 'message' => \FILTER_UNSAFE_RAW,
97 'headers' => \FILTER_UNSAFE_RAW,
98 ]
99 );
100
101 if ( empty( $email->to ) ) {
102 return;
103 }
104
105 $email->headers = \explode( "\r\n", $email->headers );
106
107 if ( $email->from ) {
108 $email->headers[] = 'From: ' . $email->from;
109 }
110
111 // Extend message with date.
112 $email->message .= "\r\n";
113 $email->message .= "\r\n";
114
115 $email->message .= sprintf(
116 /* translators: %s: sent date */
117 __( 'Sent: %s', 'pronamic-client' ),
118 \gmdate( 'r' )
119 );
120
121 $result = wp_mail( $email->to, $email->subject, $email->message, $email->headers );
122
123 /**
124 * Redirect.
125 *
126 * @link https://developer.wordpress.org/reference/functions/admin_url/
127 * @link https://developer.wordpress.org/reference/functions/wp_get_referer/
128 * @link https://developer.wordpress.org/reference/functions/wp_safe_redirect/
129 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/misc.php#L1204-L1230
130 * @link https://developer.wordpress.org/reference/functions/wp_removable_query_args/
131 */
132 $location = admin_url( 'admin.php' );
133
134 $location = add_query_arg(
135 [
136 'page' => 'pronamic_client_email',
137 'message' => 'pronamic_client_test_email_sent_' . ( $result ? 'yes' : 'no' ),
138 ],
139 $location
140 );
141
142 wp_safe_redirect( $location );
143
144 exit;
145 }
146
147 /**
148 * Get menu icon URL.
149 *
150 * @link https://developer.wordpress.org/reference/functions/add_menu_page/
151 * @return string
152 * @throws \Exception Throws exception when retrieving menu icon fails.
153 */
154 private function get_menu_icon_url() {
155 /**
156 * Icon URL.
157 *
158 * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
159 * This should begin with 'data:image/svg+xml;base64,'.
160 *
161 * We use a SVG image with default fill color #A0A5AA from the default admin color scheme:
162 * https://github.com/WordPress/WordPress/blob/5.2/wp-includes/general-template.php#L4135-L4145
163 *
164 * The advantage of this is that users with the default admin color scheme do not see the repaint:
165 * https://github.com/WordPress/WordPress/blob/5.2/wp-admin/js/svg-painter.js
166 *
167 * @link https://developer.wordpress.org/reference/functions/add_menu_page/
168 */
169 $file = __DIR__ . '/../images/pronamic-icon-wp-admin-fresh-base.svgo-min.svg';
170
171 if ( ! \is_readable( $file ) ) {
172 throw new \Exception(
173 \sprintf(
174 'Could not read WordPress admin menu icon from file: %s.',
175 \esc_html( $file )
176 )
177 );
178 }
179
180 $svg = \file_get_contents( $file, true );
181
182 if ( false === $svg ) {
183 throw new \Exception(
184 \sprintf(
185 'Could not read WordPress admin menu icon from file: %s.',
186 \esc_html( $file )
187 )
188 );
189 }
190
191 $icon_url = \sprintf(
192 'data:image/svg+xml;base64,%s',
193 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
194 \base64_encode( $svg )
195 );
196
197 return $icon_url;
198 }
199
200 /**
201 * Admin menu
202 */
203 public function admin_menu() {
204 add_menu_page(
205 __( 'Pronamic', 'pronamic-client' ), // page title
206 __( 'Pronamic', 'pronamic-client' ), // menu title
207 'pronamic_client', // capability
208 'pronamic_client', // menu slug
209 [ $this, 'page_dashboard' ], // function
210 $this->get_menu_icon_url() // icon URL
211 // 0 // position
212 );
213
214 // @see wp-admin/menu.php
215 add_submenu_page(
216 'pronamic_client', // parent slug
217 __( 'Pronamic Checklist', 'pronamic-client' ), // page title
218 __( 'Checklist', 'pronamic-client' ), // menu title
219 'pronamic_client', // capability
220 'pronamic_client_checklist', // menu slug
221 [ $this, 'page_checklist' ] // function
222 );
223
224 add_submenu_page(
225 'pronamic_client', // parent slug
226 __( 'Pronamic Extensions', 'pronamic-client' ), // page title
227 __( 'Extensions', 'pronamic-client' ), // menu title
228 'pronamic_client', // capability
229 'pronamic_client_extensions', // menu slug
230 [ $this, 'page_extensions' ] // function
231 );
232
233 add_submenu_page(
234 'pronamic_client', // parent slug
235 __( 'Email', 'pronamic-client' ), // page title
236 __( 'Email', 'pronamic-client' ), // menu title
237 'pronamic_client', // capability
238 'pronamic_client_email', // menu slug
239 [ $this, 'page_email' ] // function
240 );
241
242 add_submenu_page(
243 'pronamic_client', // parent slug
244 __( 'Settings', 'pronamic-client' ), // page title
245 __( 'Settings', 'pronamic-client' ), // menu title
246 'pronamic_client', // capability
247 'pronamic_client_settings', // menu slug
248 [ $this, 'page_settings' ] // function
249 );
250 }
251
252 /**
253 * Admin enqueue scripts
254 *
255 * @param string $hook
256 */
257 public function admin_enqueue_scripts( $hook ) {
258 wp_register_script( 'proanmic-client-media', plugins_url( 'admin/js/media.js', $this->plugin->file ) );
259
260 wp_localize_script(
261 'proanmic-client-media',
262 'pronamicClientMedia',
263 [
264 'browseText' => __( 'Browse…', 'pronamic-client' ),
265 ]
266 );
267
268 wp_register_style( 'proanmic-client-admin', plugins_url( 'admin/css/admin.css', $this->plugin->file ) );
269
270 $enqueue = strpos( $hook, 'pronamic_client' ) !== false;
271
272 if ( $enqueue ) {
273 // Styles
274 wp_enqueue_style( 'proanmic-client-admin' );
275 }
276 }
277
278 /**
279 * Page options
280 */
281 public function page_options() {
282 $this->plugin->display( 'admin/page-options.php' );
283 }
284
285 /**
286 * Dashboard setup
287 */
288 public function dashboard_setup() {
289 wp_add_dashboard_widget(
290 'pronamic_client',
291 __( 'Pronamic', 'pronamic-client' ),
292 'pronamic_client_dashboard'
293 );
294 }
295
296 /**
297 * Input text.
298 *
299 * @param array $args Arguments.
300 */
301 public static function input_text( $args ) {
302 $defaults = [
303 'type' => 'text',
304 'classes' => 'regular-text',
305 'description' => '',
306 ];
307
308 $args = wp_parse_args( $args, $defaults );
309
310 $name = $args['label_for'];
311 $value = get_option( $name );
312
313 $atts = [
314 'name' => $name,
315 'id' => $name,
316 'type' => $args['type'],
317 'class' => $args['classes'],
318 'value' => $value,
319 ];
320
321 $html = '';
322
323 foreach ( $atts as $key => $value ) {
324 $html .= sprintf( '%s="%s" ', $key, esc_attr( $value ) );
325 }
326
327 $html = trim( $html );
328
329 printf(
330 '<input %s />',
331 // @codingStandardsIgnoreStart
332 $html
333 // @codingStandardsIgnoreEn
334 );
335
336 if ( ! empty( $args['description'] ) ) {
337 printf(
338 '<p class="description">%s</p>',
339 wp_kses(
340 $args['description'],
341 array(
342 'br' => array(),
343 'code' => array()
344 )
345 )
346 );
347 }
348 }
349
350 /**
351 * Page index
352 */
353 public function page_dashboard() {
354 $this->plugin->display( 'admin/dashboard.php' );
355 }
356
357 /**
358 * Page checklist
359 */
360 public function page_checklist() {
361 $this->plugin->display( 'admin/checklist.php' );
362 }
363
364 /**
365 * Page extensions
366 */
367 public function page_extensions() {
368 $this->plugin->display( 'admin/extensions.php' );
369 }
370
371 /**
372 * Page email
373 */
374 public function page_email() {
375 $this->plugin->display( 'admin/email.php' );
376 }
377
378 /**
379 * Page settings
380 */
381 public function page_settings() {
382 $this->plugin->display( 'admin/settings.php' );
383 }
384
385 /**
386 * Upgrade
387 */
388 public function upgrade() {
389 global $wp_roles;
390
391 $wp_roles->add_cap( 'administrator', 'pronamic_client' );
392 $wp_roles->add_cap( 'editor', 'pronamic_client' );
393 }
394
395 /**
396 * Return an instance of this class.
397 *
398 * @since 1.1.0
399 *
400 * @return object A single instance of this class.
401 */
402 public static function get_instance( Plugin $plugin ) {
403 // If the single instance hasn't been set, set it now.
404 if ( null === self::$instance ) {
405 self::$instance = new self( $plugin );
406 }
407
408 return self::$instance;
409 }
410 }
411