Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

Third_Party_Action::metabox_invoices()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * Gestion des actions des tiers.
4
 *
5
 * Ajoutes une page "Tiers" dans le menu de WordPress.
6
 *
7
 * @author    Eoxia <[email protected]>
8
 * @copyright (c) 2011-2018 Eoxia <[email protected]>.
9
 *
10
 * @license   AGPLv3 <https://spdx.org/licenses/AGPL-3.0-or-later.html>
11
 *
12
 * @package   WPshop\Classes
13
 *
14
 * @since     2.0.0
15
 */
16
17
namespace wpshop;
18
19
defined( 'ABSPATH' ) || exit;
20
21
/**
22
 * Third Party Action Class.
23
 */
24
class Third_Party_Action {
25
26
	/**
27
	 * Définition des metabox sur la page.
28
	 *
29
	 * @since 2.0.0
30
	 *
31
	 * @var array
32
	 */
33
	public $metaboxes = null;
34
35
	/**
36
	 * Constructeur.
37
	 *
38
	 * @since 2.0.0
39
	 */
40
	public function __construct() {
41
		add_action( 'admin_menu', array( $this, 'callback_admin_menu' ) );
42
43
		add_action( 'load-wpshop_page_wps-third-party', array( $this, 'callback_load' ) );
44
45
		add_action( 'wp_ajax_third_party_load_title_edit', array( $this, 'load_title_edit' ) );
46
		add_action( 'admin_post_third_party_save_title', array( $this, 'save_third' ) );
47
		add_action( 'wp_ajax_third_party_save_title', array( $this, 'save_third' ) );
48
49
		add_action( 'wp_ajax_third_party_load_address', array( $this, 'load_billing_address' ) );
50
		add_action( 'wp_ajax_third_party_save_address', array( $this, 'save_billing_address' ) );
51
52
		$this->metaboxes = array(
53
			'wps-third-party-billing'  => array(
54
				'title'    => __( 'Billing address', 'wpshop' ),
55
				'callback' => array( $this, 'metabox_billing_address' ),
56
			),
57
			'wps-third-party-contacts' => array(
58
				'title'    => __( 'Contacts', 'wpshop' ),
59
				'callback' => array( $this, 'metabox_contacts' ),
60
			),
61
			'wps-third-party-orders'   => array(
62
				'title'    => __( 'Orders', 'wpshop' ),
63
				'callback' => array( $this, 'metabox_orders' ),
64
			),
65
			'wps-third-party-invoices' => array(
66
				'title'    => __( 'Invoices', 'wpshop' ),
67
				'callback' => array( $this, 'metabox_invoices' ),
68
			),
69
		);
70
	}
71
72
	/**
73
	 * Initialise la page "Third Parties".
74
	 *
75
	 * @since 2.0.0
76
	 */
77
	public function callback_admin_menu() {
78
		add_submenu_page(
79
			'wps-order',
80
			__( 'Third Parties', 'wpshop' ),
81
			__( 'Third Parties', 'wpshop' ),
82
			'manage_options',
83
			'wps-third-party',
84
			array( $this, 'callback_add_menu_page' )
85
		);
86
	}
87
88
	/**
89
	 * Gestion JS des metabox
90
	 *
91
	 * @since 2.0.0
92
	 */
93
	public function callback_load() {
94
		wp_enqueue_script( 'common' );
95
		wp_enqueue_script( 'wp-lists' );
96
		wp_enqueue_script( 'postbox' );
97
	}
98
99
	/**
100
	 * Appel la vue "main" du module "Third Party".
101
	 *
102
	 * @since 2.0.0
103
	 */
104
	public function callback_add_menu_page() {
105
		if ( isset( $_GET['id'] ) ) {
106
			$third_party  = Third_Party::g()->get( array( 'id' => $_GET['id'] ), true );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
107
			$args_metabox = array(
108
				'third_party' => $third_party,
109
				'id'          => $_GET['id'],
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
110
			);
111
112
			if ( ! empty( $this->metaboxes ) ) {
113
				foreach ( $this->metaboxes as $key => $metabox ) {
114
					add_meta_box(
115
						$key,
116
						$metabox['title'],
117
						$metabox['callback'],
118
						'wps-third-party',
119
						'normal',
120
						'default',
121
						$args_metabox
122
					);
123
				}
124
			}
125
126
			\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'single', array( 'third_party' => $third_party ) );
127
		} else {
128
			$args = array(
129
				'post_type'      => 'wps-third-party',
130
				'posts_per_page' => -1,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set posts_per_page to -1 ever.
Loading history...
131
			);
132
133
			if ( ! empty( $_GET['s'] ) ) {
134
				$args['s'] = $_GET['s'];
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
135
			}
136
137
			$count = count( get_posts( $args ) );
138
139
			$number_page  = ceil( $count / 25 );
140
			$current_page = isset( $_GET['current_page'] ) ? $_GET['current_page'] : 1;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
141
142
			$base_url = admin_url( 'admin.php?page=wps-third-party' );
143
144
			$begin_url = $base_url . '&current_page=1';
145
			$end_url   = $base_url . '&current_page=' . $number_page;
146
147
			$prev_url = $base_url . '&current_page=' . ( $current_page - 1 );
148
			$next_url = $base_url . '&current_page=' . ( $current_page + 1 );
149
150
			if ( ! empty( $_GET['s'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
151
				$begin_url .= '&s=' . $_GET['s'];
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
152
				$end_url   .= '&s=' . $_GET['s'];
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
153
				$prev_url  .= '&s=' . $_GET['s'];
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
154
				$next_url  .= '&s=' . $_GET['s'];
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
155
			}
156
157
			\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'main', array(
158
				'number_page'  => $number_page,
159
				'current_page' => $current_page,
160
				'count'        => $count,
161
				'begin_url'    => $begin_url,
162
				'end_url'      => $end_url,
163
				'prev_url'     => $prev_url,
164
				'next_url'     => $next_url,
165
			) );
166
		}
167
	}
168
169
	/**
170
	 * Appel la vue de la metabox des adresses.
171
	 *
172
	 * @param  WP_Post $post          Le post actuel.
173
	 * @param  array   $callback_args Les paramètres envoyées dans le add_meta_box.
174
	 *
175
	 * @since 2.0.0
176
	 */
177
	public function metabox_billing_address( $post, $callback_args ) {
178
		\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'metaboxes/metabox-billing-address', array(
179
			'third_party' => $callback_args['args']['third_party'],
180
		) );
181
	}
182
183
	/**
184
	 * Appel la vue de la metabox des contacts.
185
	 *
186
	 * @param  WP_Post $post          Le post actuel.
187
	 * @param  array   $callback_args Les paramètres envoyées dans le add_meta_box.
188
	 *
189
	 * @since 2.0.0
190
	 */
191
	public function metabox_contacts( $post, $callback_args ) {
192
		$contacts = array();
193
194
		if ( ! empty( $callback_args['args']['third_party']->data['contact_ids'] ) ) {
195
			$contacts = Contact::g()->get( array( 'include' => $callback_args['args']['third_party']->data['contact_ids'] ) );
196
		}
197
		\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'metaboxes/metabox-contacts', array(
198
			'third_party' => $callback_args['args']['third_party'],
199
			'contacts'    => $contacts,
200
		) );
201
	}
202
203
	/**
204
	 * Appel la vue de la metabox des commandes.
205
	 *
206
	 * @param  WP_Post $post          Le post actuel.
207
	 * @param  array   $callback_args Les paramètres envoyées dans le add_meta_box.
208
	 *
209
	 * @since 2.0.0
210
	 */
211 View Code Duplication
	public function metabox_orders( $post, $callback_args ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
		$orders = Doli_Order::g()->get( array( 'post_parent' => $callback_args['args']['id'] ) );
213
214
		if ( ! empty( $orders ) ) {
215
			foreach ( $orders as &$order ) {
216
				$order->data['invoice'] = Doli_Invoice::g()->get( array( 'post_parent' => $order->data['id'] ), true );
217
			}
218
		}
219
220
		\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'metaboxes/metabox-orders', array(
221
			'orders' => $orders,
222
		) );
223
	}
224
225
	/**
226
	 * Appel la vue de la metabox des factures.
227
	 *
228
	 * @param  WP_Post $post          Le post actuel.
229
	 * @param  array   $callback_args Les paramètres envoyées dans le add_meta_box.
230
	 *
231
	 * @since 2.0.0
232
	 */
233 View Code Duplication
	public function metabox_invoices( $post, $callback_args ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
235
		$invoices = Doli_Invoice::g()->get( array( 'author__in' => $callback_args['args']['third_party']->data['contact_ids'] ) );
236
237
		if ( ! empty( $invoices ) ) {
238
			foreach ( $invoices as &$invoice ) {
239
				$invoice->data['order'] = Doli_Order::g()->get( array( 'id' => $invoice->data['parent_id'] ), true );
240
			}
241
		}
242
243
		\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'metaboxes/metabox-invoices', array(
244
			'invoices' => $invoices,
245
		) );
246
	}
247
248
	/**
249
	 * Renvoies la vue d'édition d'un titre.
250
	 *
251
	 * @since 2.0.0
252
	 */
253
	public function load_title_edit() {
254
		check_ajax_referer( 'load_title_edit' );
255
256
		$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : -1;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
257
258
		if ( -1 == $post_id ) {
259
			exit;
260
		}
261
262
		$third_party = Third_Party::g()->get( array( 'id' => $post_id ), true );
263
264
		ob_start();
265
		\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'single-title-edit', array(
266
			'third_party' => $third_party,
267
		) );
268
		wp_send_json_success( array(
269
			'namespace'        => 'wpshop',
270
			'module'           => 'thirdParties',
271
			'callback_success' => 'loaddedTitleEdit',
272
			'view'             => ob_get_clean(),
273
		) );
274
	}
275
276
	/**
277
	 * Enregistres le titre du tier.
278
	 *
279
	 * @since 2.0.0
280
	 */
281
	public function save_third() {
282
		check_ajax_referer( 'save_third' );
283
284
		$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : -1;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
285
		$title   = ! empty( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
286
287
		if ( -1 == $post_id ) {
288
			exit;
289
		}
290
291
		$third_party = Third_Party::g()->get( array( 'id' => $post_id ), true );
292
293
		$third_party->data['id'] = $post_id;
294
295
		if ( empty( $post_id ) ) {
296
			$third_party->data['status'] = 'publish';
297
		}
298
		$third_party->data['title'] = $title;
299
300
		$third_party = Third_Party::g()->update( $third_party->data );
301
302
		$external_id = do_action( 'wps_saved_third_party', $third_party->data );
303
304
		$third_party->data['external_id'] = $external_id;
305
		$third_party                      = Third_Party::g()->update( $third_party->data );
306
307
		if ( wp_doing_ajax() ) {
308
			ob_start();
309
			\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'single-title', array(
310
				'third_party' => $third_party,
311
			) );
312
			wp_send_json_success( array(
313
				'namespace'        => 'wpshop',
314
				'module'           => 'thirdParties',
315
				'callback_success' => 'savedThird',
316
				'view'             => ob_get_clean(),
317
			) );
318
		} else {
319
			wp_redirect( admin_url( 'admin.php?page=wps-third-party&id=' . $third_party->data['id'] ) );
320
			exit;
321
		}
322
	}
323
324
	/**
325
	 * Charges la vue pour éditer l'adresse du tier.
326
	 *
327
	 * @since 2.0.0
328
	 */
329
	public function load_billing_address() {
330
		check_ajax_referer( 'load_billing_address' );
331
332
		$third_party_id = ! empty( $_POST['third_party_id'] ) ? (int) $_POST['third_party_id'] : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
333
334
		if ( empty( $third_party_id ) ) {
335
			wp_send_json_error();
336
		}
337
338
		$third_party = Third_Party::g()->get( array( 'id' => $third_party_id ), true );
339
340
		ob_start();
341
		\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'metaboxes/metabox-billing-address-edit', array(
342
			'third_party' => $third_party,
343
		) );
344
		wp_send_json_success( array(
345
			'namespace'        => 'wpshop',
346
			'module'           => 'thirdParties',
347
			'callback_success' => 'loaddedBillingAddressSuccess',
348
			'view'             => ob_get_clean(),
349
		) );
350
	}
351
352
	/**
353
	 * Met à jour l'adresse du tier
354
	 *
355
	 * @todo: Merger avec save third
356
	 * @since 2.0.0
357
	 */
358
	public function save_billing_address() {
359
		check_ajax_referer( 'save_billing_address' );
360
361
		$third_party_id   = ! empty( $_POST['third_party_id'] ) ? (int) $_POST['third_party_id'] : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
362
		$third_party_form = ! empty( $_POST['third_party'] ) ? (array) $_POST['third_party'] : array();
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
363
364
		if ( empty( $third_party_id ) ) {
365
			wp_send_json_error();
366
		}
367
368
		$third_party = Third_Party::g()->get( array( 'id' => $third_party_id ), true );
369
370
		$third_party->data['title']   = $third_party_form['title'];
371
		$third_party->data['address'] = $third_party_form['address'];
372
		$third_party->data['zip']     = $third_party_form['zip'];
373
		$third_party->data['town']    = $third_party_form['town'];
374
		$third_party->data['phone']   = $third_party_form['phone'];
375
376
		$third_party = Third_Party::g()->update( $third_party->data );
377
378
		do_action( 'wps_saved_billing_address', $third_party );
379
380
		ob_start();
381
		\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'metaboxes/metabox-billing-address', array(
382
			'third_party' => $third_party,
383
		) );
384
		wp_send_json_success( array(
385
			'namespace'        => 'wpshop',
386
			'module'           => 'thirdParties',
387
			'callback_success' => 'savedBillingAddressSuccess',
388
			'view'             => ob_get_clean(),
389
		) );
390
	}
391
}
392
393
new Third_Party_Action();
394