Failed Conditions
Push — develop ( 5d1bfa...8c2bc3 )
by Remco
04:02
created

src/Admin.php (2 issues)

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
		/**
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
	public function admin_init() {
51
		if ( ! \current_user_can( 'manage_options' ) ) {
52
			return;
53
		}
54
55
		$function = array( __CLASS__, 'user_profile' );
56
57
		if ( ! has_action( 'show_user_profile', $function ) ) {
58
			add_action( 'show_user_profile', $function );
59
		}
60
61
		if ( ! has_action( 'edit_user_profile', $function ) ) {
62
			add_action( 'edit_user_profile', $function );
63
		}
64
	}
65
66
	/**
67
	 * Get menu icon URL.
68
	 *
69
	 * @link https://developer.wordpress.org/reference/functions/add_menu_page/
70
	 * @return string
71
	 * @throws \Exception Throws exception when retrieving menu icon fails.
72
	 */
73
	private function get_menu_icon_url() {
74
		/**
75
		 * Icon URL.
76
		 *
77
		 * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
78
		 * This should begin with 'data:image/svg+xml;base64,'.
79
		 *
80
		 * We use a SVG image with default fill color #A0A5AA from the default admin color scheme:
81
		 * https://github.com/WordPress/WordPress/blob/5.2/wp-includes/general-template.php#L4135-L4145
82
		 *
83
		 * The advantage of this is that users with the default admin color scheme do not see the repaint:
84
		 * https://github.com/WordPress/WordPress/blob/5.2/wp-admin/js/svg-painter.js
85
		 *
86
		 * @link https://developer.wordpress.org/reference/functions/add_menu_page/
87
		 */
88
		$file = __DIR__ . '/../images/dist/mollie-wp-admin-fresh-base.svgo-min.svg';
89
90
		if ( ! \is_readable( $file ) ) {
91
			throw new \Exception(
92
				\sprintf(
93
					'Could not read WordPress admin menu icon from file: %s.',
94
					$file
95
				)
96
			);
97
		}
98
99
		$svg = \file_get_contents( $file, true );
100
101
		if ( false === $svg ) {
102
			throw new \Exception(
103
				\sprintf(
104
					'Could not read WordPress admin menu icon from file: %s.',
105
					$file
106
				)
107
			);
108
		}
109
110
		$icon_url = \sprintf(
111
			'data:image/svg+xml;base64,%s',
112
			// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
113
			\base64_encode( $svg )
114
		);
115
116
		return $icon_url;
117
	}
118
119
	/**
120
	 * Admin menu.
121
	 */
122
	public function admin_menu() {
123
		try {
124
			$menu_icon_url = $this->get_menu_icon_url();
125
		} catch ( \Exception $e ) {
126
			// @todo Log.
127
128
			/**
129
			 * If retrieving the menu icon URL fails we will
130
			 * fallback to the WordPress money dashicon.
131
			 *
132
			 * @link https://developer.wordpress.org/resource/dashicons/#money
133
			 */
134
			$menu_icon_url = 'dashicons-money';
135
		}
136
137
		add_menu_page(
138
			__( 'Mollie', 'pronamic_ideal' ),
139
			__( 'Mollie', 'pronamic_ideal' ),
140
			'manage_options',
141
			'pronamic_pay_mollie',
142
			array( $this, 'page_mollie' ),
143
			$menu_icon_url
144
		);
145
146
		add_submenu_page(
147
			'pronamic_pay_mollie',
148
			__( 'Mollie Profiles', 'pronamic_ideal' ),
149
			__( 'Profiles', 'pronamic_ideal' ),
150
			'manage_options',
151
			'pronamic_pay_mollie_profiles',
152
			array( $this, 'page_mollie_profiles' )
153
		);
154
155
		add_submenu_page(
156
			'pronamic_pay_mollie',
157
			__( 'Mollie Customers', 'pronamic_ideal' ),
158
			__( 'Customers', 'pronamic_ideal' ),
159
			'manage_options',
160
			'pronamic_pay_mollie_customers',
161
			array( $this, 'page_mollie_customers' )
162
		);
163
164
		add_submenu_page(
165
			'pronamic_pay_mollie',
166
			__( 'Mollie Payments', 'pronamic_ideal' ),
167
			__( 'Payments', 'pronamic_ideal' ),
168
			'manage_options',
169
			'pronamic_pay_mollie_payments',
170
			array( $this, 'page_mollie_payments' )
171
		);
172
173
		/**
174
		 * Remove menu page.
175
		 *
176
		 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/plugin.php#L1708-L1729
177
		 * @link https://wordpress.stackexchange.com/questions/135692/creating-a-wordpress-admin-page-without-a-menu-for-a-plugin
178
		 * @link https://stackoverflow.com/questions/3902760/how-do-you-add-a-wordpress-admin-page-without-adding-it-to-the-menu
179
		 */
180
		remove_menu_page( 'pronamic_pay_mollie' );
181
	}
182
183
	/**
184
	 * Page Mollie.
185
	 */
186
	public function page_mollie() {
187
		include __DIR__ . '/../views/page-mollie.php';
188
	}
189
190
	/**
191
	 * Page Mollie profiles.
192
	 */
193
	public function page_mollie_profiles() {
194
		if ( filter_has_var( INPUT_GET, 'id' ) ) {
195
			include __DIR__ . '/../views/page-profile.php';
196
197
			return;
198
		}
199
200
		include __DIR__ . '/../views/page-profiles.php';
201
	}
202
203
	/**
204
	 * Page Mollie customers.
205
	 */
206
	public function page_mollie_customers() {
207
		if ( filter_has_var( INPUT_GET, 'id' ) ) {
208
			include __DIR__ . '/../views/page-customer.php';
209
210
			return;
211
		}
212
213
		include __DIR__ . '/../views/page-customers.php';
214
	}
215
216
	/**
217
	 * Page Mollie payments.
218
	 */
219
	public function page_mollie_payments() {
220
		if ( filter_has_var( INPUT_GET, 'id' ) ) {
221
			include __DIR__ . '/../views/page-payment.php';
222
223
			return;
224
		}
225
226
		include __DIR__ . '/../views/page-payments.php';
227
	}
228
229
	/**
230
	 * User profile.
231
	 *
232
	 * @since 1.1.6
233
	 * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600
234
	 * @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...
235
	 * @return void
236
	 */
237
	public static function user_profile( $user ) {
238
		include __DIR__ . '/../views/user-profile.php';
239
	}
240
241
	/**
242
	 * Add payment meta box.
243
	 *
244
	 * @link https://developer.wordpress.org/reference/functions/add_meta_box/
245
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/meta-boxes.php#L1541-L1549
246
	 * @param string   $post_type Post type.
247
	 * @param \WP_Post $post      Post object.
248
	 */
249
	public function add_payment_meta_box( $post_type, $post ) {
250
		if ( 'pronamic_payment' !== $post_type ) {
251
			return;
252
		}
253
254
		$transaction_id = \get_post_meta( $post->ID, '_pronamic_payment_transaction_id', true );
255
256
		if ( 'tr_' !== \substr( $transaction_id, 0, 3 ) ) {
0 ignored issues
show
It seems like $transaction_id can also be of type false; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

256
		if ( 'tr_' !== \substr( /** @scrutinizer ignore-type */ $transaction_id, 0, 3 ) ) {
Loading history...
257
			return;
258
		}
259
260
		\add_meta_box(
261
			'pronamic_pay_mollie_payment',
262
			\__( 'Mollie', 'pronamic_ideal' ),
263
			function( $post ) {
264
				include __DIR__ . '/../views/meta-box-payment.php';
265
			},
266
			$post_type,
267
			'side'
268
		);
269
	}
270
271
	/**
272
	 * Add subscription meta box.
273
	 *
274
	 * @link https://developer.wordpress.org/reference/functions/add_meta_box/
275
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/meta-boxes.php#L1541-L1549
276
	 * @param string   $post_type Post type.
277
	 * @param \WP_Post $post      Post object.
278
	 */
279
	public function add_subscription_meta_box( $post_type, $post ) {
280
		if ( 'pronamic_pay_subscr' !== $post_type ) {
281
			return;
282
		}
283
284
		$mollie_customer_id = \get_post_meta( $post->ID, '_pronamic_subscription_mollie_customer_id', true );
285
286
		if ( empty( $mollie_customer_id ) ) {
287
			return;
288
		}
289
290
		\add_meta_box(
291
			'pronamic_pay_mollie_subscription',
292
			\__( 'Mollie', 'pronamic_ideal' ),
293
			function( $post ) {
294
				include __DIR__ . '/../views/meta-box-subscription.php';
295
			},
296
			$post_type,
297
			'side'
298
		);
299
	}
300
}
301