Test Failed
Push — develop ( 21667b...724bc2 )
by Remco
05:38 queued 13s
created

src/Admin.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Mollie admin.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
/**
14
 * Title: Mollie admin
15
 * Description:
16
 * Copyright: 2005-2020 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 2.0.9
21
 * @since   1.0.0
22
 */
23
class Admin {
24
	/**
25
	 * Construct and intialize Mollie admin.
26
	 */
27
	public function __construct() {
28
		$function = array( __CLASS__, 'user_profile' );
29
30
		if ( ! has_action( 'show_user_profile', $function ) ) {
31
			add_action( 'show_user_profile', $function );
32
		}
33
34
		if ( ! has_action( 'edit_user_profile', $function ) ) {
35
			add_action( 'edit_user_profile', $function );
36
		}
37
38
		/**
39
		 * Menu.
40
		 *
41
		 * @link https://metabox.io/create-hidden-admin-page/
42
		 */
43
		add_action( 'admin_menu', array( $this, 'admin_menu' ) );
44
	}
45
46
	/**
47
	 * Get menu icon URL.
48
	 *
49
	 * @link https://developer.wordpress.org/reference/functions/add_menu_page/
50
	 * @return string
51
	 * @throws \Exception Throws exception when retrieving menu icon fails.
52
	 */
53
	private function get_menu_icon_url() {
54
		/**
55
		 * Icon URL.
56
		 *
57
		 * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
58
		 * This should begin with 'data:image/svg+xml;base64,'.
59
		 *
60
		 * We use a SVG image with default fill color #A0A5AA from the default admin color scheme:
61
		 * https://github.com/WordPress/WordPress/blob/5.2/wp-includes/general-template.php#L4135-L4145
62
		 *
63
		 * The advantage of this is that users with the default admin color scheme do not see the repaint:
64
		 * https://github.com/WordPress/WordPress/blob/5.2/wp-admin/js/svg-painter.js
65
		 *
66
		 * @link https://developer.wordpress.org/reference/functions/add_menu_page/
67
		 */
68
		$file = __DIR__ . '/../images/dist/mollie-wp-admin-fresh-base.svgo-min.svg';
69
70
		if ( ! \is_readable( $file ) ) {
71
			throw new \Exception(
72
				\sprintf(
73
					'Could not read WordPress admin menu icon from file: %s.',
74
					$file
75
				)
76
			);
77
		}
78
79
		$svg = \file_get_contents( $file, true );
80
81
		if ( false === $svg ) {
82
			throw new \Exception(
83
				\sprintf(
84
					'Could not read WordPress admin menu icon from file: %s.',
85
					$file
86
				)
87
			);
88
		}
89
90
		$icon_url = \sprintf(
91
			'data:image/svg+xml;base64,%s',
92
			// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
93
			\base64_encode( $svg )
94
		);
95
96
		return $icon_url;
97
	}
98
99
	/**
100
	 * Admin menu.
101
	 */
102
	public function admin_menu() {
103
		try {
104
			$menu_icon_url = $this->get_menu_icon_url();
105
		} catch ( \Exception $e ) {
106
			// @todo Log.
107
108
			/**
109
			 * If retrieving the menu icon URL fails we will
110
			 * fallback to the WordPress money dashicon.
111
			 *
112
			 * @link https://developer.wordpress.org/resource/dashicons/#money
113
			 */
114
			$menu_icon_url = 'dashicons-money';
115
		}
116
117
		add_menu_page(
118
			__( 'Mollie', 'pronamic_ideal' ),
119
			__( 'Mollie', 'pronamic_ideal' ),
120
			'manage_options',
121
			'pronamic_pay_mollie',
122
			array( $this, 'page_mollie' ),
123
			$menu_icon_url
124
		);
125
126
		add_submenu_page(
127
			'pronamic_pay_mollie',
128
			__( 'Mollie Customers', 'pronamic_ideal' ),
129
			__( 'Customers', 'pronamic_ideal' ),
130
			'manage_options',
131
			'pronamic_pay_mollie_customers',
132
			array( $this, 'page_mollie_customers' )
133
		);
134
	}
135
136
	/**
137
	 * Page Mollie.
138
	 */
139
	public function page_mollie() {
140
141
	}
142
143
	/**
144
	 * Page Mollie customers.
145
	 */
146
	public function page_mollie_customers() {
147
		if ( filter_has_var( INPUT_GET, 'id' ) ) {
148
			include __DIR__ . '/../views/page-customer.php';
149
150
			return;
151
		}
152
153
		include __DIR__ . '/../views/page-customers.php';
154
	}
155
156
	/**
157
	 * User profile.
158
	 *
159
	 * @since 1.1.6
160
	 * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600
161
	 * @param WP_User $user WordPress user.
0 ignored issues
show
The type Pronamic\WordPress\Pay\Gateways\Mollie\WP_User was not found. Did you mean WP_User? If so, make sure to prefix the type with \.
Loading history...
162
	 * @return void
163
	 */
164
	public static function user_profile( $user ) {
165
		include __DIR__ . '/../views/html-admin-user-profile.php';
166
	}
167
}
168