Test Failed
Push — develop ( 5f6097...889b97 )
by Remco
04:40
created

Admin::page_mollie_profiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
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 Profiles', 'pronamic_ideal' ),
129
			__( 'Profiles', 'pronamic_ideal' ),
130
			'manage_options',
131
			'pronamic_pay_mollie_profiles',
132
			array( $this, 'page_mollie_profiles' )
133
		);
134
135
		add_submenu_page(
136
			'pronamic_pay_mollie',
137
			__( 'Mollie Customers', 'pronamic_ideal' ),
138
			__( 'Customers', 'pronamic_ideal' ),
139
			'manage_options',
140
			'pronamic_pay_mollie_customers',
141
			array( $this, 'page_mollie_customers' )
142
		);
143
	}
144
145
	/**
146
	 * Page Mollie.
147
	 */
148
	public function page_mollie() {
149
150
	}
151
152
	/**
153
	 * Page Mollie profiles.
154
	 */
155
	public function page_mollie_profiles() {
156
		if ( filter_has_var( INPUT_GET, 'id' ) ) {
157
			include __DIR__ . '/../views/page-profile.php';
158
159
			return;
160
		}
161
162
		include __DIR__ . '/../views/page-profiles.php';
163
	}
164
165
	/**
166
	 * Page Mollie customers.
167
	 */
168
	public function page_mollie_customers() {
169
		if ( filter_has_var( INPUT_GET, 'id' ) ) {
170
			include __DIR__ . '/../views/page-customer.php';
171
172
			return;
173
		}
174
175
		include __DIR__ . '/../views/page-customers.php';
176
	}
177
178
	/**
179
	 * User profile.
180
	 *
181
	 * @since 1.1.6
182
	 * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600
183
	 * @param WP_User $user WordPress user.
0 ignored issues
show
Bug introduced by
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...
184
	 * @return void
185
	 */
186
	public static function user_profile( $user ) {
187
		include __DIR__ . '/../views/html-admin-user-profile.php';
188
	}
189
}
190