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

Contacts_Action::load_contact()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 0
dl 0
loc 25
rs 9.2088
c 0
b 0
f 0
1
<?php
2
/**
3
 * Gestion des actions des contacts.
4
 *
5
 * @author    Eoxia <[email protected]>
6
 * @copyright (c) 2011-2018 Eoxia <[email protected]>.
7
 *
8
 * @license   AGPLv3 <https://spdx.org/licenses/AGPL-3.0-or-later.html>
9
 *
10
 * @package   WPshop\Classes
11
 *
12
 * @since     2.0.0
13
 */
14
15
namespace wpshop;
16
17
defined( 'ABSPATH' ) || exit;
18
19
/**
20
 * Contact Action Class.
21
 */
22
class Contacts_Action {
23
24
	/**
25
	 * Le constructeur
26
	 *
27
	 * @since 2.0.0
28
	 */
29
	public function __construct() {
30
		add_action( 'wp_ajax_third_party_search_contact', array( $this, 'search_contact' ) );
31
		add_action( 'wp_ajax_third_party_save_contact', array( $this, 'save_and_associate_contact' ) );
32
		add_action( 'wp_ajax_third_party_load_contact', array( $this, 'load_contact' ) );
33
		add_action( 'wp_ajax_third_party_delete_contact', array( $this, 'delete_contact' ) );
34
	}
35
36
	/**
37
	 * Recherches un contact dans la base de donnée de WP.
38
	 *
39
	 * @since 2.0.0
40
	 */
41
	public function search_contact() {
42
		check_ajax_referer( 'search_contact' );
43
44
		$term = ! empty( $_POST['term'] ) ? sanitize_text_field( $_POST['term'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
45
46
		if ( empty( $term ) ) {
47
			wp_send_json_error();
48
		}
49
50
		$contacts = Contact::g()->get( array(
51
			'search'         => '*' . $term . '*',
52
			'search_columns' => array(
53
				'user_login',
54
				'user_nicename',
55
				'user_email',
56
			),
57
		) );
58
59
		ob_start();
60
		foreach ( $contacts as $contact ) :
61
			?>
62
			<li data-id="<?php echo esc_attr( $contact->data['id'] ); ?>" data-result="<?php echo esc_html( $contact->data['firstname'] . ' ' . $contact->data['lastname'] ); ?>" class="autocomplete-result">
63
				<div class="autocomplete-result-container">
64
					<span class="autocomplete-result-title"><?php echo esc_html( $contact->data['firstname'] . ' ' . $contact->data['lastname'] ); ?></span>
65
					<span class="autocomplete-result-subtitle"><?php echo esc_html( $contact->data['email'] ); ?></span>
66
				</div>
67
			</li>
68
			<?php
69
		endforeach;
70
		wp_send_json_success( array(
71
			'view' => ob_get_clean(),
72
		) );
73
	}
74
75
	/**
76
	 * Enregistres et associes un contact au tier
77
	 *
78
	 * @since 2.0.0
79
	 */
80
	public function save_and_associate_contact() {
81
		check_ajax_referer( 'save_and_associate_contact' );
82
83
		$third_party_id = ! empty( $_POST['parent_id'] ) ? (int) $_POST['parent_id'] : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
84
		$contact        = ! empty( $_POST['contact'] ) ? (array) $_POST['contact'] : 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...
85
		$contact['id']  = ! empty( $_POST['contact']['id'] ) ? (int) $_POST['contact']['id'] : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
86
87
		if ( empty( $contact['id'] ) ) {
88
			// Dans le cas ou c'est un contact ajouté depuis la recherche.
89
			$contact['id'] = ! empty( $_POST['contact_id'] ) ? (int) $_POST['contact_id'] : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
90
		}
91
92
		if ( empty( $third_party_id ) ) {
93
			wp_send_json_error();
94
		}
95
96
		$third_party = Third_Party::g()->get( array( 'id' => $third_party_id ), true );
97
98
		if ( empty( $contact['id'] ) ) {
99
			$email                = explode( '@', $contact['email'] );
100
			$contact['login']     = $email[0];
101
			$contact['user_pass'] = wp_generate_password();
102
103
			$contact = apply_filters( 'wps_save_and_associate_contact', $contact, $third_party );
104
			$contact = Contact::g()->update( $contact );
105
		} else {
106
			$contact = Contact::g()->get( array( 'id' => $contact['id'] ), true );
107
		}
108
109
		if ( ! in_array( $contact->data['id'], $third_party->data['contact_ids'] ) ) {
110
			$third_party->data['contact_ids'][] = $contact->data['id'];
111
			$contact->data['third_party']       = $third_party->data['external_id'];
112
113
			$third_party = Third_Party::g()->update( $third_party->data );
114
			$contact     = Contact::g()->update( $contact->data );
115
		}
116
117
		do_action( 'wps_saved_and_associated_contact', $third_party, $contact, empty( $contact->data['id'] ) ? true : false );
118
119
		ob_start();
120
		$contacts = array();
121
		if ( ! empty( $third_party->data['contact_ids'] ) ) {
122
			$contacts = Contact::g()->get( array( 'include' => $third_party->data['contact_ids'] ) );
123
		}
124
		\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'metaboxes/metabox-contacts', array(
125
			'third_party' => $third_party,
126
			'contacts'    => $contacts,
127
		) );
128
		wp_send_json_success( array(
129
			'namespace'        => 'wpshop',
130
			'module'           => 'thirdParties',
131
			'callback_success' => 'associatedContactSuccess',
132
			'view'             => ob_get_clean(),
133
		) );
134
	}
135
136
	/**
137
	 * Charges un contact et appel la vue edit.
138
	 *
139
	 * @since 2.0.0
140
	 */
141
	public function load_contact() {
142
		check_ajax_referer( 'load_contact' );
143
144
		$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...
145
		$contact_id     = ! empty( $_POST['contact_id'] ) ? (int) $_POST['contact_id'] : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
146
147
		if ( empty( $contact_id ) || empty( $third_party_id ) ) {
148
			wp_send_json_error();
149
		}
150
151
		$contact = Contact::g()->get( array( 'id' => $contact_id ), true );
152
153
		ob_start();
154
		\eoxia\View_Util::exec( 'wpshop', 'third-parties', 'metaboxes/metabox-contacts-edit', array(
155
			'third_party_id' => $third_party_id,
156
			'contact'        => $contact,
157
		) );
158
159
		wp_send_json_success( array(
160
			'namespace'        => 'wpshop',
161
			'module'           => 'thirdParties',
162
			'callback_success' => 'loaddedContactSuccess',
163
			'view'             => ob_get_clean(),
164
		) );
165
	}
166
167
	/**
168
	 * Supprimes un contact
169
	 *
170
	 * @since 2.0.0
171
	 */
172
	public function delete_contact() {
173
		check_ajax_referer( 'delete_contact' );
174
175
		$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...
176
		$contact_id     = ! empty( $_POST['contact_id'] ) ? (int) $_POST['contact_id'] : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
177
178
		if ( empty( $contact_id ) || empty( $third_party_id ) ) {
179
			wp_send_json_error();
180
		}
181
182
		$third_party = Third_Party::g()->get( array( 'id' => $third_party_id ), true );
183
184
		$index = array_search( $contact_id, $third_party->data['contact_ids'], true );
185
186
		if ( false !== $index ) {
187
			array_splice( $third_party->data['contact_ids'], $index, 1 );
188
189
			$contact = Contact::g()->get( array( 'id' => $contact_id ), true );
190
191
			$contact->data['third_party_id'] = -1;
192
193
			Third_Party::g()->update( $third_party->data );
194
			Contact::g()->update( $contact->data );
195
196
			do_action( 'wps_deleted_contact', $third_party, $contact );
197
		}
198
199
		wp_send_json_success( array(
200
			'namespace'        => 'wpshop',
201
			'module'           => 'thirdParties',
202
			'callback_success' => 'deletedContactSuccess',
203
		) );
204
	}
205
}
206
207
new Contacts_Action();
208