1 | <?php |
||
2 | /** |
||
3 | * Mollie admin. |
||
4 | * |
||
5 | * @author Pronamic <[email protected]> |
||
6 | * @copyright 2005-2022 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-2022 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 initialize Mollie admin. |
||
26 | */ |
||
27 | public function __construct() { |
||
28 | /** |
||
29 | * Initialize. |
||
30 | */ |
||
31 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
32 | |||
33 | /** |
||
34 | * Menu. |
||
35 | * |
||
36 | * @link https://metabox.io/create-hidden-admin-page/ |
||
37 | */ |
||
38 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
||
39 | |||
40 | /** |
||
41 | * Meta boxes. |
||
42 | */ |
||
43 | add_action( 'add_meta_boxes', array( $this, 'add_payment_meta_box' ), 10, 2 ); |
||
44 | add_action( 'add_meta_boxes', array( $this, 'add_subscription_meta_box' ), 10, 2 ); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Admin init. |
||
49 | * |
||
50 | * @return void |
||
51 | */ |
||
52 | public function admin_init() { |
||
53 | if ( ! \current_user_can( 'manage_options' ) ) { |
||
54 | return; |
||
55 | } |
||
56 | |||
57 | $function = array( __CLASS__, 'user_profile' ); |
||
58 | |||
59 | if ( ! has_action( 'show_user_profile', $function ) ) { |
||
60 | add_action( 'show_user_profile', $function ); |
||
61 | } |
||
62 | |||
63 | if ( ! has_action( 'edit_user_profile', $function ) ) { |
||
64 | add_action( 'edit_user_profile', $function ); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Get menu icon URL. |
||
70 | * |
||
71 | * @link https://developer.wordpress.org/reference/functions/add_menu_page/ |
||
72 | * @return string |
||
73 | * @throws \Exception Throws exception when retrieving menu icon fails. |
||
74 | */ |
||
75 | private function get_menu_icon_url() { |
||
76 | /** |
||
77 | * Icon URL. |
||
78 | * |
||
79 | * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. |
||
80 | * This should begin with 'data:image/svg+xml;base64,'. |
||
81 | * |
||
82 | * We use a SVG image with default fill color #A0A5AA from the default admin color scheme: |
||
83 | * https://github.com/WordPress/WordPress/blob/5.2/wp-includes/general-template.php#L4135-L4145 |
||
84 | * |
||
85 | * The advantage of this is that users with the default admin color scheme do not see the repaint: |
||
86 | * https://github.com/WordPress/WordPress/blob/5.2/wp-admin/js/svg-painter.js |
||
87 | * |
||
88 | * @link https://developer.wordpress.org/reference/functions/add_menu_page/ |
||
89 | */ |
||
90 | $file = __DIR__ . '/../images/dist/mollie-wp-admin-fresh-base.svgo-min.svg'; |
||
91 | |||
92 | if ( ! \is_readable( $file ) ) { |
||
93 | throw new \Exception( |
||
94 | \sprintf( |
||
95 | 'Could not read WordPress admin menu icon from file: %s.', |
||
96 | $file |
||
97 | ) |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | $svg = \file_get_contents( $file, true ); |
||
102 | |||
103 | if ( false === $svg ) { |
||
104 | throw new \Exception( |
||
105 | \sprintf( |
||
106 | 'Could not read WordPress admin menu icon from file: %s.', |
||
107 | $file |
||
108 | ) |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | $icon_url = \sprintf( |
||
113 | 'data:image/svg+xml;base64,%s', |
||
114 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
||
115 | \base64_encode( $svg ) |
||
116 | ); |
||
117 | |||
118 | return $icon_url; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Admin menu. |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | public function admin_menu() { |
||
127 | try { |
||
128 | $menu_icon_url = $this->get_menu_icon_url(); |
||
129 | } catch ( \Exception $e ) { |
||
130 | // @todo Log. |
||
131 | |||
132 | /** |
||
133 | * If retrieving the menu icon URL fails we will |
||
134 | * fallback to the WordPress money dashicon. |
||
135 | * |
||
136 | * @link https://developer.wordpress.org/resource/dashicons/#money |
||
137 | */ |
||
138 | $menu_icon_url = 'dashicons-money'; |
||
139 | } |
||
140 | |||
141 | add_menu_page( |
||
142 | __( 'Mollie', 'pronamic_ideal' ), |
||
143 | __( 'Mollie', 'pronamic_ideal' ), |
||
144 | 'manage_options', |
||
145 | 'pronamic_pay_mollie', |
||
146 | array( $this, 'page_mollie' ), |
||
147 | $menu_icon_url |
||
148 | ); |
||
149 | |||
150 | add_submenu_page( |
||
151 | 'pronamic_pay_mollie', |
||
152 | __( 'Mollie Profiles', 'pronamic_ideal' ), |
||
153 | __( 'Profiles', 'pronamic_ideal' ), |
||
154 | 'manage_options', |
||
155 | 'pronamic_pay_mollie_profiles', |
||
156 | array( $this, 'page_mollie_profiles' ) |
||
157 | ); |
||
158 | |||
159 | add_submenu_page( |
||
160 | 'pronamic_pay_mollie', |
||
161 | __( 'Mollie Customers', 'pronamic_ideal' ), |
||
162 | __( 'Customers', 'pronamic_ideal' ), |
||
163 | 'manage_options', |
||
164 | 'pronamic_pay_mollie_customers', |
||
165 | array( $this, 'page_mollie_customers' ) |
||
166 | ); |
||
167 | |||
168 | add_submenu_page( |
||
169 | 'pronamic_pay_mollie', |
||
170 | __( 'Mollie Payments', 'pronamic_ideal' ), |
||
171 | __( 'Payments', 'pronamic_ideal' ), |
||
172 | 'manage_options', |
||
173 | 'pronamic_pay_mollie_payments', |
||
174 | array( $this, 'page_mollie_payments' ) |
||
175 | ); |
||
176 | |||
177 | /** |
||
178 | * Remove menu page. |
||
179 | * |
||
180 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/plugin.php#L1708-L1729 |
||
181 | * @link https://wordpress.stackexchange.com/questions/135692/creating-a-wordpress-admin-page-without-a-menu-for-a-plugin |
||
182 | * @link https://stackoverflow.com/questions/3902760/how-do-you-add-a-wordpress-admin-page-without-adding-it-to-the-menu |
||
183 | */ |
||
184 | remove_menu_page( 'pronamic_pay_mollie' ); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Page Mollie. |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | public function page_mollie() { |
||
193 | include __DIR__ . '/../views/page-mollie.php'; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Page Mollie profiles. |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | public function page_mollie_profiles() { |
||
202 | if ( filter_has_var( INPUT_GET, 'id' ) ) { |
||
203 | include __DIR__ . '/../views/page-profile.php'; |
||
204 | |||
205 | return; |
||
206 | } |
||
207 | |||
208 | include __DIR__ . '/../views/page-profiles.php'; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Page Mollie customers. |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | public function page_mollie_customers() { |
||
217 | if ( filter_has_var( INPUT_GET, 'id' ) ) { |
||
218 | include __DIR__ . '/../views/page-customer.php'; |
||
219 | |||
220 | return; |
||
221 | } |
||
222 | |||
223 | include __DIR__ . '/../views/page-customers.php'; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Page Mollie payments. |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | public function page_mollie_payments() { |
||
232 | if ( filter_has_var( INPUT_GET, 'id' ) ) { |
||
233 | include __DIR__ . '/../views/page-payment.php'; |
||
234 | |||
235 | return; |
||
236 | } |
||
237 | |||
238 | include __DIR__ . '/../views/page-payments.php'; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * User profile. |
||
243 | * |
||
244 | * @since 1.1.6 |
||
245 | * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600 |
||
246 | * @param \WP_User $user WordPress user. |
||
247 | * @return void |
||
248 | */ |
||
249 | public static function user_profile( $user ) { |
||
250 | include __DIR__ . '/../views/user-profile.php'; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Add payment meta box. |
||
255 | * |
||
256 | * @link https://developer.wordpress.org/reference/functions/add_meta_box/ |
||
257 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/meta-boxes.php#L1541-L1549 |
||
258 | * @param string $post_type Post type. |
||
259 | * @param \WP_Post $post Post object. |
||
260 | * @return void |
||
261 | */ |
||
262 | public function add_payment_meta_box( $post_type, $post ) { |
||
263 | if ( 'pronamic_payment' !== $post_type ) { |
||
264 | return; |
||
265 | } |
||
266 | |||
267 | $transaction_id = \get_post_meta( $post->ID, '_pronamic_payment_transaction_id', true ); |
||
268 | |||
269 | if ( 'tr_' !== \substr( $transaction_id, 0, 3 ) ) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
270 | return; |
||
271 | } |
||
272 | |||
273 | \add_meta_box( |
||
274 | 'pronamic_pay_mollie_payment', |
||
275 | \__( 'Mollie', 'pronamic_ideal' ), |
||
276 | function( $post ) { |
||
277 | include __DIR__ . '/../views/meta-box-payment.php'; |
||
278 | }, |
||
279 | $post_type, |
||
280 | 'side' |
||
281 | ); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Add subscription meta box. |
||
286 | * |
||
287 | * @link https://developer.wordpress.org/reference/functions/add_meta_box/ |
||
288 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/meta-boxes.php#L1541-L1549 |
||
289 | * @param string $post_type Post type. |
||
290 | * @param \WP_Post $post Post object. |
||
291 | * @return void |
||
292 | */ |
||
293 | public function add_subscription_meta_box( $post_type, $post ) { |
||
294 | if ( 'pronamic_pay_subscr' !== $post_type ) { |
||
295 | return; |
||
296 | } |
||
297 | |||
298 | // Get subscription. |
||
299 | $subscription = \get_pronamic_subscription( $post->ID ); |
||
300 | |||
301 | if ( null === $subscription ) { |
||
302 | return; |
||
303 | } |
||
304 | |||
305 | // Get Mollie customer ID. |
||
306 | $mollie_customer_id = $subscription->get_meta( 'mollie_customer_id' ); |
||
307 | |||
308 | if ( empty( $mollie_customer_id ) ) { |
||
309 | return; |
||
310 | } |
||
311 | |||
312 | // Add meta box. |
||
313 | \add_meta_box( |
||
314 | 'pronamic_pay_mollie_subscription', |
||
315 | \__( 'Mollie', 'pronamic_ideal' ), |
||
316 | function( $post ) { |
||
317 | include __DIR__ . '/../views/meta-box-subscription.php'; |
||
318 | }, |
||
319 | $post_type, |
||
320 | 'side' |
||
321 | ); |
||
322 | } |
||
323 | } |
||
324 |