PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.4.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
← All changes | class/class-mainwp-client-handler.php +434 -563 5.2.24.4.1 View file →
@@ -13,621 +13,492 @@
13 13 * @package MainWP\Dashboard
14 14 *
15 15 * @uses \MainWP\Dashboard\MainWP_Client_Handler
16 16 */
17 -class MainWP_Client_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
17 +class MainWP_Client_Handler {
18 18
19 - /**
20 - * Private static variable to hold the single instance of the class.
21 - *
22 - * @static
23 - *
24 - * @var mixed Default null
25 - */
26 - private static $instance = null;
19 + /**
20 + * Private static variable to hold the single instance of the class.
21 + *
22 + * @static
23 + *
24 + * @var mixed Default null
25 + */
26 + private static $instance = null;
27 27
28 - /**
29 - * Method instance()
30 - *
31 - * Create a public static instance.
32 - *
33 - * @static
34 - * @return MainWP_Client_Handler
35 - */
36 - public static function instance() {
37 - if ( null === static::$instance ) {
38 - static::$instance = new self();
39 - }
40 - return static::$instance;
41 - }
28 + /**
29 + * Method instance()
30 + *
31 + * Create a public static instance.
32 + *
33 + * @static
34 + * @return MainWP_Client_Handler
35 + */
36 + public static function instance() {
37 + if ( null == self::$instance ) {
38 + self::$instance = new self();
39 + }
40 + return self::$instance;
41 + }
42 42
43 43
44 - /**
45 - * Get default client fields.
46 - *
47 - * Get default client fields.
48 - */
49 - public static function get_default_client_fields() {
44 + /**
45 + * Get default client fields.
46 + *
47 + * Get default client fields.
48 + */
49 + public static function get_default_client_fields() {
50 50
51 - return array(
52 - 'client.name' => array(
53 - 'title' => esc_html__( 'Client Name (Required)', 'mainwp' ),
54 - 'desc' => esc_html__( 'Displays the Client name', 'mainwp' ),
55 - 'db_field' => 'name',
56 - ),
57 - 'client.email' => array(
58 - 'title' => esc_html__( 'Client email', 'mainwp' ),
59 - 'desc' => esc_html__( 'Displays the client email', 'mainwp' ),
60 - 'db_field' => 'client_email',
61 - ),
62 - 'client.phone' => array(
63 - 'title' => esc_html__( 'Client phone', 'mainwp' ),
64 - 'desc' => esc_html__( 'Displays the client phone', 'mainwp' ),
65 - 'db_field' => 'client_phone',
66 - ),
67 - 'client.facebook' => array(
68 - 'title' => esc_html__( 'Client Facebook', 'mainwp' ),
69 - 'desc' => esc_html__( 'Displays the client Facebook', 'mainwp' ),
70 - 'db_field' => 'client_facebook',
71 - ),
72 - 'client.twitter' => array(
73 - 'title' => esc_html__( 'Client X', 'mainwp' ),
74 - 'desc' => esc_html__( 'Displays the client X', 'mainwp' ),
75 - 'db_field' => 'client_twitter',
76 - ),
77 - 'client.instagram' => array(
78 - 'title' => esc_html__( 'Client Instagram', 'mainwp' ),
79 - 'desc' => esc_html__( 'Displays the client Instagram', 'mainwp' ),
80 - 'db_field' => 'client_instagram',
81 - ),
82 - 'client.linkedin' => array(
83 - 'title' => esc_html__( 'Client LinkedIn', 'mainwp' ),
84 - 'desc' => esc_html__( 'Displays the client LinkedIn', 'mainwp' ),
85 - 'db_field' => 'client_linkedin',
86 - ),
87 - 'client.contact.address.1' => array(
88 - 'title' => esc_html__( 'Client address 1', 'mainwp' ),
89 - 'desc' => esc_html__( 'Displays the client contact address 1', 'mainwp' ),
90 - 'db_field' => 'address_1',
91 - ),
92 - 'client.contact.address.2' => array(
93 - 'title' => esc_html__( 'Client address 2', 'mainwp' ),
94 - 'desc' => esc_html__( 'Displays the client contact address 2', 'mainwp' ),
95 - 'db_field' => 'address_2',
96 - ),
97 - 'client.city' => array(
98 - 'title' => esc_html__( 'Client city', 'mainwp' ),
99 - 'desc' => esc_html__( 'Displays the client city', 'mainwp' ),
100 - 'db_field' => 'city',
101 - ),
102 - 'client.state' => array(
103 - 'title' => esc_html__( 'Client state', 'mainwp' ),
104 - 'desc' => esc_html__( 'Displays the client state', 'mainwp' ),
105 - 'db_field' => 'state',
106 - ),
107 - 'client.zip' => array(
108 - 'title' => esc_html__( 'Client ZIP', 'mainwp' ),
109 - 'desc' => esc_html__( 'Displays the client ZIP code', 'mainwp' ),
110 - 'db_field' => 'zip',
111 - ),
112 - 'client.country' => array(
113 - 'title' => esc_html__( 'Client country', 'mainwp' ),
114 - 'desc' => esc_html__( 'Displays the client country', 'mainwp' ),
115 - 'db_field' => 'country',
116 - ),
117 - 'client.note' => array(
118 - 'title' => esc_html__( 'Client notes', 'mainwp' ),
119 - 'desc' => esc_html__( 'Displays the client note', 'mainwp' ),
120 - 'db_field' => 'note',
121 - ),
122 - 'client.suspended' => array(
123 - 'title' => esc_html__( 'Client status', 'mainwp' ),
124 - 'desc' => esc_html__( 'Displays the client suspended state', 'mainwp' ),
125 - 'db_field' => 'suspended',
126 - ),
127 - 'client.created' => array(
128 - 'title' => esc_html__( 'Added on', 'mainwp' ),
129 - 'desc' => esc_html__( 'Displays the client added on', 'mainwp' ),
130 - 'tooltip' => esc_html__( 'Set the date your client was added to your MainWP Dashboard.', 'mainwp' ),
131 - 'db_field' => 'created',
132 - ),
133 - );
134 - }
51 + return array(
52 + 'client.name' => array(
53 + 'title' => esc_html__( 'Client Name (Required)', 'mainwp' ),
54 + 'desc' => esc_html__( 'Displays the Client name', 'mainwp' ),
55 + 'db_field' => 'name',
56 + ),
57 + 'client.email' => array(
58 + 'title' => esc_html__( 'Client email', 'mainwp' ),
59 + 'desc' => esc_html__( 'Displays the client email', 'mainwp' ),
60 + 'db_field' => 'client_email',
61 + ),
62 + 'client.phone' => array(
63 + 'title' => esc_html__( 'Client phone', 'mainwp' ),
64 + 'desc' => esc_html__( 'Displays the client phone', 'mainwp' ),
65 + 'db_field' => 'client_phone',
66 + ),
67 + 'client.facebook' => array(
68 + 'title' => esc_html__( 'Client Facebook', 'mainwp' ),
69 + 'desc' => esc_html__( 'Displays the client Facebook', 'mainwp' ),
70 + 'db_field' => 'client_facebook',
71 + ),
72 + 'client.twitter' => array(
73 + 'title' => esc_html__( 'Client Twitter', 'mainwp' ),
74 + 'desc' => esc_html__( 'Displays the client Twitter', 'mainwp' ),
75 + 'db_field' => 'client_twitter',
76 + ),
77 + 'client.instagram' => array(
78 + 'title' => esc_html__( 'Client Instagram', 'mainwp' ),
79 + 'desc' => esc_html__( 'Displays the client Instagram', 'mainwp' ),
80 + 'db_field' => 'client_instagram',
81 + ),
82 + 'client.linkedin' => array(
83 + 'title' => esc_html__( 'Client LinkedIn', 'mainwp' ),
84 + 'desc' => esc_html__( 'Displays the client LinkedIn', 'mainwp' ),
85 + 'db_field' => 'client_linkedin',
86 + ),
87 + 'client.contact.address.1' => array(
88 + 'title' => esc_html__( 'Client address 1', 'mainwp' ),
89 + 'desc' => esc_html__( 'Displays the client contact address 1', 'mainwp' ),
90 + 'db_field' => 'address_1',
91 + ),
92 + 'client.contact.address.2' => array(
93 + 'title' => esc_html__( 'Client address 2', 'mainwp' ),
94 + 'desc' => esc_html__( 'Displays the client contact address 2', 'mainwp' ),
95 + 'db_field' => 'address_2',
96 + ),
97 + 'client.city' => array(
98 + 'title' => esc_html__( 'Client city', 'mainwp' ),
99 + 'desc' => esc_html__( 'Displays the client city', 'mainwp' ),
100 + 'db_field' => 'city',
101 + ),
102 + 'client.state' => array(
103 + 'title' => esc_html__( 'Client state', 'mainwp' ),
104 + 'desc' => esc_html__( 'Displays the client state', 'mainwp' ),
105 + 'db_field' => 'state',
106 + ),
107 + 'client.zip' => array(
108 + 'title' => esc_html__( 'Client ZIP', 'mainwp' ),
109 + 'desc' => esc_html__( 'Displays the client ZIP code', 'mainwp' ),
110 + 'db_field' => 'zip',
111 + ),
112 + 'client.country' => array(
113 + 'title' => esc_html__( 'Client country', 'mainwp' ),
114 + 'desc' => esc_html__( 'Displays the client country', 'mainwp' ),
115 + 'db_field' => 'country',
116 + ),
117 + 'client.note' => array(
118 + 'title' => esc_html__( 'Client notes', 'mainwp' ),
119 + 'desc' => esc_html__( 'Displays the client note', 'mainwp' ),
120 + 'db_field' => 'note',
121 + ),
122 + 'client.suspended' => array(
123 + 'title' => esc_html__( 'Client status', 'mainwp' ),
124 + 'desc' => esc_html__( 'Displays the client suspended state', 'mainwp' ),
125 + 'db_field' => 'suspended',
126 + ),
127 + );
128 + }
135 129
136 - /**
137 - * Get default client fields.
138 - *
139 - * Get default client fields.
140 - */
141 - public static function get_mini_default_client_fields() {
142 130
143 - return array(
144 - 'client.name' => array(
145 - 'title' => esc_html__( 'Client Name (Required)', 'mainwp' ),
146 - 'desc' => esc_html__( 'Displays the Client name', 'mainwp' ),
147 - 'db_field' => 'name',
148 - ),
149 - 'client.email' => array(
150 - 'title' => esc_html__( 'Client email', 'mainwp' ),
151 - 'desc' => esc_html__( 'Displays the client email', 'mainwp' ),
152 - 'db_field' => 'client_email',
153 - ),
154 - 'client.phone' => array(
155 - 'title' => esc_html__( 'Client phone', 'mainwp' ),
156 - 'desc' => esc_html__( 'Displays the client phone', 'mainwp' ),
157 - 'db_field' => 'client_phone',
158 - ),
159 - );
160 - }
131 + /**
132 + * Get default contact fields.
133 + *
134 + * Get default contact fields.
135 + */
136 + public static function get_default_contact_fields() {
137 + return array(
138 + 'client.contact.name' => array(
139 + 'title' => esc_html__( 'Contact name (Required)', 'mainwp' ),
140 + 'desc' => esc_html__( 'Displays the client contact name', 'mainwp' ),
141 + 'db_field' => 'contact_name',
142 + ),
143 + 'contact.role' => array(
144 + 'title' => esc_html__( 'Contact role', 'mainwp' ),
145 + 'desc' => esc_html__( 'Displays the contact role', 'mainwp' ),
146 + 'db_field' => 'contact_role',
147 + ),
148 + 'contact.email' => array(
149 + 'title' => esc_html__( 'Contact email (Required)', 'mainwp' ),
150 + 'desc' => esc_html__( 'Displays the contact email', 'mainwp' ),
151 + 'db_field' => 'contact_email',
152 + ),
153 + 'contact.phone' => array(
154 + 'title' => esc_html__( 'Contact phone', 'mainwp' ),
155 + 'desc' => esc_html__( 'Displays the contact phone', 'mainwp' ),
156 + 'db_field' => 'contact_phone',
157 + ),
158 + 'contact.facebook' => array(
159 + 'title' => esc_html__( 'Contact Facebook', 'mainwp' ),
160 + 'desc' => esc_html__( 'Displays the contact Facebook', 'mainwp' ),
161 + 'db_field' => 'facebook',
162 + ),
163 + 'contact.twitter' => array(
164 + 'title' => esc_html__( 'Contact Twitter', 'mainwp' ),
165 + 'desc' => esc_html__( 'Displays the contact Twitter', 'mainwp' ),
166 + 'db_field' => 'twitter',
167 + ),
168 + 'contact.instagram' => array(
169 + 'title' => esc_html__( 'Contact Instagram', 'mainwp' ),
170 + 'desc' => esc_html__( 'Displays the contact Instagram', 'mainwp' ),
171 + 'db_field' => 'instagram',
172 + ),
173 + 'contact.linkedin' => array(
174 + 'title' => esc_html__( 'Contact LinkedIn', 'mainwp' ),
175 + 'desc' => esc_html__( 'Displays the contact LinkedIn', 'mainwp' ),
176 + 'db_field' => 'linkedin',
177 + ),
178 + );
179 + }
161 180
162 181
163 - /**
164 - * Get default contact fields.
165 - *
166 - * Get default contact fields.
167 - */
168 - public static function get_default_contact_fields() {
169 - return array(
170 - 'client.contact.name' => array(
171 - 'title' => esc_html__( 'Contact name (Required)', 'mainwp' ),
172 - 'desc' => esc_html__( 'Displays the client contact name', 'mainwp' ),
173 - 'db_field' => 'contact_name',
174 - ),
175 - 'contact.email' => array(
176 - 'title' => esc_html__( 'Contact email (Required)', 'mainwp' ),
177 - 'desc' => esc_html__( 'Displays the contact email', 'mainwp' ),
178 - 'db_field' => 'contact_email',
179 - ),
180 - 'contact.role' => array(
181 - 'title' => esc_html__( 'Contact role', 'mainwp' ),
182 - 'desc' => esc_html__( 'Displays the contact role', 'mainwp' ),
183 - 'db_field' => 'contact_role',
184 - ),
185 - 'contact.phone' => array(
186 - 'title' => esc_html__( 'Contact phone', 'mainwp' ),
187 - 'desc' => esc_html__( 'Displays the contact phone', 'mainwp' ),
188 - 'db_field' => 'contact_phone',
189 - ),
190 - 'contact.facebook' => array(
191 - 'title' => esc_html__( 'Contact Facebook', 'mainwp' ),
192 - 'desc' => esc_html__( 'Displays the contact Facebook', 'mainwp' ),
193 - 'db_field' => 'facebook',
194 - ),
195 - 'contact.twitter' => array(
196 - 'title' => esc_html__( 'Contact X', 'mainwp' ),
197 - 'desc' => esc_html__( 'Displays the contact X', 'mainwp' ),
198 - 'db_field' => 'twitter',
199 - ),
200 - 'contact.instagram' => array(
201 - 'title' => esc_html__( 'Contact Instagram', 'mainwp' ),
202 - 'desc' => esc_html__( 'Displays the contact Instagram', 'mainwp' ),
203 - 'db_field' => 'instagram',
204 - ),
205 - 'contact.linkedin' => array(
206 - 'title' => esc_html__( 'Contact LinkedIn', 'mainwp' ),
207 - 'desc' => esc_html__( 'Displays the contact LinkedIn', 'mainwp' ),
208 - 'db_field' => 'linkedin',
209 - ),
210 - );
211 - }
182 + /**
183 + * Method get_compatible_tokens().
184 + *
185 + * Get compatible tokens.
186 + */
187 + public static function get_compatible_tokens() {
188 + return array(
189 + 'client.contact.name' => 'client.contact.name',
190 + 'client.email' => 'contact.email',
191 + 'client.phone' => 'contact.phone',
192 + );
193 + }
194 + /**
195 + * Method rest_api_add_client().
196 + *
197 + * Rest API add client.
198 + *
199 + * @param array $data fields array.
200 + * @param bool $edit Is edit.
201 + *
202 + * $data fields.
203 + * 'client_email'.
204 + * 'name'.
205 + * 'client_facebook'.
206 + * 'client_twitter'.
207 + * 'client_instagram'.
208 + * 'client_linkedin'.
209 + * 'address_1'.
210 + * 'address_2'.
211 + * 'city'.
212 + * 'zip'.
213 + * 'state'.
214 + * 'note'.
215 + * 'selected_sites'.
216 + * 'client_id'. - to edit client.
217 + *
218 + * @throws \Exception Error message.
219 + * @return mixed Results.
220 + */
221 + public static function rest_api_add_client( $data, $edit = false ) { // phpcs:ignore -- comlex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
212 222
213 - /**
214 - * Get default contact fields.
215 - *
216 - * Get default contact fields.
217 - */
218 - public static function get_mini_default_contact_fields() {
219 - return array(
220 - 'client.contact.name' => array(
221 - 'title' => esc_html__( 'Contact name (Required)', 'mainwp' ),
222 - 'desc' => esc_html__( 'Displays the client contact name', 'mainwp' ),
223 - 'db_field' => 'contact_name',
224 - ),
225 - 'contact.email' => array(
226 - 'title' => esc_html__( 'Contact email (Required)', 'mainwp' ),
227 - 'desc' => esc_html__( 'Displays the contact email', 'mainwp' ),
228 - 'db_field' => 'contact_email',
229 - ),
230 - 'contact.role' => array(
231 - 'title' => esc_html__( 'Contact role', 'mainwp' ),
232 - 'desc' => esc_html__( 'Displays the contact role', 'mainwp' ),
233 - 'db_field' => 'contact_role',
234 - ),
235 - );
236 - }
223 + $params = array();
237 224
225 + if ( $edit ) {
238 226
239 - /**
240 - * Method get_compatible_tokens().
241 - *
242 - * Get compatible tokens.
243 - */
244 - public static function get_compatible_tokens() {
245 - return array(
246 - 'client.contact.name' => 'client.contact.name',
247 - 'client.email' => 'contact.email',
248 - 'client.phone' => 'contact.phone',
249 - );
250 - }
251 - /**
252 - * Method rest_api_add_client().
253 - *
254 - * Rest API add client.
255 - *
256 - * @param array $data fields array.
257 - * @param bool $edit Is edit.
258 - *
259 - * $data fields.
260 - * 'client_email'.
261 - * 'name'.
262 - * 'client_facebook'.
263 - * 'client_twitter'.
264 - * 'client_instagram'.
265 - * 'client_linkedin'.
266 - * 'address_1'.
267 - * 'address_2'.
268 - * 'city'.
269 - * 'zip'.
270 - * 'state'.
271 - * 'note'.
272 - * 'selected_sites'.
273 - * 'client_id'. - to edit client.
274 - *
275 - * @throws \Exception Error message.
276 - * @return mixed Results.
277 - */
278 - public static function rest_api_add_client( $data, $edit = false ) { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
227 + $client_id = isset( $data['client_id'] ) ? intval( $data['client_id'] ) : 0;
279 228
280 - if ( empty( $data ) || ! is_array( $data ) ) {
281 - return array( 'error' => esc_html__( 'Invalid Client data. Please try again.', 'mainwp' ) );
282 - }
229 + if ( empty( $client_id ) ) { // edit client.
230 + return array(
231 + 'error' => esc_html__( 'Client ID field is required! Please enter a Client ID.', 'mainwp' ),
232 + );
233 + }
283 234
284 - $params = array();
235 + $params['client_id'] = $client_id;
285 236
286 - try {
237 + if ( isset( $data['name'] ) ) {
238 + $params['name'] = sanitize_text_field( wp_unslash( $data['name'] ) );
239 + }
287 240
288 - if ( $edit ) {
241 + if ( isset( $data['address_1'] ) ) {
242 + $params['address_1'] = sanitize_text_field( wp_unslash( $data['address_1'] ) );
243 + }
244 + if ( isset( $data['address_2'] ) ) {
245 + $params['address_2'] = sanitize_text_field( wp_unslash( $data['address_2'] ) );
246 + }
247 + if ( isset( $data['city'] ) ) {
248 + $params['city'] = sanitize_text_field( wp_unslash( $data['city'] ) );
249 + }
250 + if ( isset( $data['zip'] ) ) {
251 + $params['zip'] = sanitize_text_field( wp_unslash( $data['zip'] ) );
252 + }
253 + if ( isset( $data['state'] ) ) {
254 + $params['state'] = sanitize_text_field( wp_unslash( $data['state'] ) );
255 + }
289 256
290 - $client_id = isset( $data['client_id'] ) ? intval( $data['client_id'] ) : 0;
257 + if ( isset( $data['country'] ) ) {
258 + $params['country'] = sanitize_text_field( wp_unslash( $data['country'] ) );
259 + }
291 260
292 - if ( empty( $client_id ) ) { // edit client.
293 - return array(
294 - 'error' => esc_html__( 'Client ID field is required! Please enter a Client ID.', 'mainwp' ),
295 - );
296 - }
261 + if ( isset( $data['note'] ) ) {
262 + $params['note'] = sanitize_text_field( wp_unslash( $data['note'] ) );
263 + }
297 264
298 - $params['client_id'] = $client_id;
265 + if ( isset( $data['client_email'] ) ) {
266 + $params['client_email'] = sanitize_text_field( wp_unslash( $data['client_email'] ) );
267 + }
299 268
300 - if ( isset( $data['name'] ) && ! empty( trim( $data['name'] ) ) ) {
301 - $params['name'] = sanitize_text_field( wp_unslash( $data['name'] ) );
302 - }
269 + if ( isset( $data['client_phone'] ) ) {
270 + $params['client_phone'] = sanitize_text_field( wp_unslash( $data['client_phone'] ) );
271 + }
303 272
304 - if ( isset( $data['address_1'] ) ) {
305 - $params['address_1'] = sanitize_text_field( wp_unslash( $data['address_1'] ) );
306 - }
307 - if ( isset( $data['address_2'] ) ) {
308 - $params['address_2'] = sanitize_text_field( wp_unslash( $data['address_2'] ) );
309 - }
310 - if ( isset( $data['city'] ) ) {
311 - $params['city'] = sanitize_text_field( wp_unslash( $data['city'] ) );
312 - }
313 - if ( isset( $data['zip'] ) ) {
314 - $params['zip'] = sanitize_text_field( wp_unslash( $data['zip'] ) );
315 - }
316 - if ( isset( $data['state'] ) ) {
317 - $params['state'] = sanitize_text_field( wp_unslash( $data['state'] ) );
318 - }
273 + if ( isset( $data['client_facebook'] ) ) {
274 + $params['client_facebook'] = sanitize_text_field( wp_unslash( $data['client_facebook'] ) );
275 + }
319 276
320 - if ( isset( $data['country'] ) ) {
321 - $params['country'] = sanitize_text_field( wp_unslash( $data['country'] ) );
322 - }
277 + if ( isset( $data['client_twitter'] ) ) {
278 + $params['client_twitter'] = sanitize_text_field( wp_unslash( $data['client_twitter'] ) );
279 + }
323 280
324 - if ( isset( $data['note'] ) ) {
325 - $params['note'] = sanitize_text_field( wp_unslash( $data['note'] ) );
326 - }
281 + if ( isset( $data['client_instagram'] ) ) {
282 + $params['client_instagram'] = sanitize_text_field( wp_unslash( $data['client_instagram'] ) );
283 + }
327 284
328 - if ( isset( $data['client_email'] ) ) {
329 - $params['client_email'] = sanitize_text_field( wp_unslash( $data['client_email'] ) );
330 - }
285 + if ( isset( $data['client_linkedin'] ) ) {
286 + $params['client_linkedin'] = sanitize_text_field( wp_unslash( $data['client_linkedin'] ) );
287 + }
331 288
332 - if ( isset( $data['client_phone'] ) ) {
333 - $params['client_phone'] = sanitize_text_field( wp_unslash( $data['client_phone'] ) );
334 - }
289 + try {
290 + MainWP_DB_Client::instance()->update_client( $params, true );
291 + } catch ( \Exception $e ) {
292 + throw $e;
293 + }
335 294
336 - if ( isset( $data['client_facebook'] ) ) {
337 - $params['client_facebook'] = sanitize_text_field( wp_unslash( $data['client_facebook'] ) );
338 - }
295 + if ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) {
296 + $selected_sites = array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) );
297 + MainWP_DB_Client::instance()->update_selected_sites_for_client( $client_id, $selected_sites );
298 + }
339 299
340 - if ( isset( $data['client_twitter'] ) ) {
341 - $params['client_twitter'] = sanitize_text_field( wp_unslash( $data['client_twitter'] ) );
342 - }
300 + if ( isset( $data['custom_fields'] ) && is_array( $data['custom_fields'] ) ) {
301 + $custom_fields = wp_unslash( $data['custom_fields'] );
302 + foreach ( $custom_fields as $field ) {
303 + $fie_name = isset( $field['field_name'] ) ? sanitize_text_field( wp_unslash( $field['field_name'] ) ) : '';
304 + $fie_value = isset( $field['field_value'] ) ? sanitize_text_field( wp_unslash( $field['field_value'] ) ) : '';
305 + $fie_desc = isset( $field['field_desc'] ) ? sanitize_text_field( wp_unslash( $field['field_desc'] ) ) : '';
306 + if ( ! empty( $fie_name ) && ! empty( $fie_value ) ) {
307 + $get_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, 0 );
308 + if ( $get_gen_field ) { // it is general field.
309 + if ( ! empty( $fie_desc ) ) {
310 + MainWP_DB_Client::instance()->update_client_field(
311 + $get_gen_field->field_id,
312 + array(
313 + 'field_desc' => $fie_desc,
314 + )
315 + );
316 + }
317 + MainWP_DB_Client::instance()->update_client_field_value( $get_gen_field->field_id, $fie_value, $client_id ); // add or update general field value for the client.
318 + } else {
319 + $indi_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, $client_id );
320 + if ( $indi_gen_field ) { // it is individual field.
343 321
344 - if ( isset( $data['client_instagram'] ) ) {
345 - $params['client_instagram'] = sanitize_text_field( wp_unslash( $data['client_instagram'] ) );
346 - }
322 + if ( ! empty( $fie_desc ) ) {
323 + MainWP_DB_Client::instance()->update_client_field(
324 + $indi_gen_field->field_id,
325 + array(
326 + 'field_desc' => $fie_desc,
327 + 'client_id' => $client_id,
328 + )
329 + );
330 + }
331 + MainWP_DB_Client::instance()->update_client_field_value( $indi_gen_field->field_id, $fie_value, $client_id ); // add or update individual field value for the client.
332 + }
333 + }
334 + }
335 + }
336 + }
347 337
348 - if ( isset( $data['client_linkedin'] ) ) {
349 - $params['client_linkedin'] = sanitize_text_field( wp_unslash( $data['client_linkedin'] ) );
350 - }
338 + return array(
339 + 'success' => true,
340 + 'clientid' => $client_id,
341 + );
351 342
352 - if ( isset( $data['created'] ) && is_numeric( $data['created'] ) ) {
353 - $params['created'] = intval( $data['created'] );
354 - }
343 + } else {
355 344
356 - MainWP_DB_Client::instance()->update_client( $params, true );
345 + $params['name'] = isset( $data['name'] ) ? sanitize_text_field( wp_unslash( $data['name'] ) ) : '';
357 346
358 - if ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) {
359 - $selected_sites = array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) );
360 - MainWP_DB_Client::instance()->update_selected_sites_for_client( $client_id, $selected_sites );
361 - }
347 + if ( empty( $params['name'] ) ) {
348 + return array(
349 + 'error' => esc_html__( 'Client name field is required! Please enter a Client name.', 'mainwp' ),
350 + );
351 + }
362 352
363 - if ( isset( $data['custom_fields'] ) && is_array( $data['custom_fields'] ) ) {
364 - $custom_fields = wp_unslash( $data['custom_fields'] );
365 - foreach ( $custom_fields as $field ) {
366 - $fie_name = isset( $field['field_name'] ) ? sanitize_text_field( wp_unslash( $field['field_name'] ) ) : '';
367 - $fie_value = isset( $field['field_value'] ) ? sanitize_text_field( wp_unslash( $field['field_value'] ) ) : '';
368 - $fie_desc = isset( $field['field_desc'] ) ? sanitize_text_field( wp_unslash( $field['field_desc'] ) ) : '';
369 - if ( ! empty( $fie_name ) && ! empty( $fie_value ) ) {
370 - $get_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, 0 );
371 - if ( $get_gen_field ) { // it is general field.
372 - if ( ! empty( $fie_desc ) ) {
373 - MainWP_DB_Client::instance()->update_client_field(
374 - $get_gen_field->field_id,
375 - array(
376 - 'field_desc' => $fie_desc,
377 - )
378 - );
379 - }
380 - MainWP_DB_Client::instance()->update_client_field_value( $get_gen_field->field_id, $fie_value, $client_id ); // add or update general field value for the client.
381 - } else {
382 - $indi_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, $client_id );
383 - if ( $indi_gen_field ) { // it is individual field.
353 + $params['address_1'] = isset( $data['address_1'] ) ? sanitize_text_field( wp_unslash( $data['address_1'] ) ) : '';
354 + $params['address_2'] = isset( $data['address_2'] ) ? sanitize_text_field( wp_unslash( $data['address_2'] ) ) : '';
355 + $params['city'] = isset( $data['city'] ) ? sanitize_text_field( wp_unslash( $data['city'] ) ) : '';
356 + $params['zip'] = isset( $data['zip'] ) ? sanitize_text_field( wp_unslash( $data['zip'] ) ) : '';
357 + $params['state'] = isset( $data['state'] ) ? sanitize_text_field( wp_unslash( $data['state'] ) ) : '';
358 + $params['country'] = isset( $data['country'] ) ? sanitize_text_field( wp_unslash( $data['country'] ) ) : '';
359 + $params['note'] = isset( $data['note'] ) ? sanitize_text_field( wp_unslash( $data['note'] ) ) : '';
360 + $params['client_email'] = isset( $data['client_email'] ) ? sanitize_text_field( wp_unslash( $data['client_email'] ) ) : '';
361 + $params['client_phone'] = isset( $data['client_phone'] ) ? sanitize_text_field( wp_unslash( $data['client_phone'] ) ) : '';
362 + $params['client_facebook'] = isset( $data['client_facebook'] ) ? sanitize_text_field( wp_unslash( $data['client_facebook'] ) ) : '';
363 + $params['client_twitter'] = isset( $data['client_twitter'] ) ? sanitize_text_field( wp_unslash( $data['client_twitter'] ) ) : '';
364 + $params['client_instagram'] = isset( $data['client_instagram'] ) ? sanitize_text_field( wp_unslash( $data['client_instagram'] ) ) : '';
365 + $params['client_linkedin'] = isset( $data['client_linkedin'] ) ? sanitize_text_field( wp_unslash( $data['client_linkedin'] ) ) : '';
384 366
385 - if ( ! empty( $fie_desc ) ) {
386 - MainWP_DB_Client::instance()->update_client_field(
387 - $indi_gen_field->field_id,
388 - array(
389 - 'field_desc' => $fie_desc,
390 - 'client_id' => $client_id,
391 - )
392 - );
393 - }
394 - MainWP_DB_Client::instance()->update_client_field_value( $indi_gen_field->field_id, $fie_value, $client_id ); // add or update individual field value for the client.
395 - }
396 - }
397 - }
398 - }
399 - }
367 + $inserted = MainWP_DB_Client::instance()->update_client( $params );
400 368
401 - return array(
402 - 'success' => true,
403 - 'clientid' => $client_id,
404 - );
369 + if ( empty( $inserted ) ) {
370 + return array(
371 + 'error' => esc_html__( 'Undefined error. Please try again.', 'mainwp' ),
372 + );
373 + }
405 374
406 - } else {
375 + $selected_sites = ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) ) : array();
407 376
408 - $params['name'] = isset( $data['name'] ) ? sanitize_text_field( wp_unslash( $data['name'] ) ) : '';
377 + MainWP_DB_Client::instance()->update_selected_sites_for_client( $inserted->client_id, $selected_sites );
409 378
410 - if ( empty( $params['name'] ) ) {
411 - return array(
412 - 'error' => esc_html__( 'Client name field is required! Please enter a Client name.', 'mainwp' ),
413 - );
414 - }
379 + return array(
380 + 'success' => true,
381 + 'clientid' => $inserted->client_id,
382 + );
383 + }
384 + }
415 385
416 - $params['address_1'] = isset( $data['address_1'] ) ? sanitize_text_field( wp_unslash( $data['address_1'] ) ) : '';
417 - $params['address_2'] = isset( $data['address_2'] ) ? sanitize_text_field( wp_unslash( $data['address_2'] ) ) : '';
418 - $params['city'] = isset( $data['city'] ) ? sanitize_text_field( wp_unslash( $data['city'] ) ) : '';
419 - $params['zip'] = isset( $data['zip'] ) ? sanitize_text_field( wp_unslash( $data['zip'] ) ) : '';
420 - $params['state'] = isset( $data['state'] ) ? sanitize_text_field( wp_unslash( $data['state'] ) ) : '';
421 - $params['country'] = isset( $data['country'] ) ? sanitize_text_field( wp_unslash( $data['country'] ) ) : '';
422 - $params['note'] = isset( $data['note'] ) ? sanitize_text_field( wp_unslash( $data['note'] ) ) : '';
423 - $params['client_email'] = isset( $data['client_email'] ) ? sanitize_text_field( wp_unslash( $data['client_email'] ) ) : '';
424 - $params['client_phone'] = isset( $data['client_phone'] ) ? sanitize_text_field( wp_unslash( $data['client_phone'] ) ) : '';
425 - $params['client_facebook'] = isset( $data['client_facebook'] ) ? sanitize_text_field( wp_unslash( $data['client_facebook'] ) ) : '';
426 - $params['client_twitter'] = isset( $data['client_twitter'] ) ? sanitize_text_field( wp_unslash( $data['client_twitter'] ) ) : '';
427 - $params['client_instagram'] = isset( $data['client_instagram'] ) ? sanitize_text_field( wp_unslash( $data['client_instagram'] ) ) : '';
428 - $params['client_linkedin'] = isset( $data['client_linkedin'] ) ? sanitize_text_field( wp_unslash( $data['client_linkedin'] ) ) : '';
429 - $params['created'] = time();
430 386
431 - $inserted = MainWP_DB_Client::instance()->update_client( $params, true );
387 + /**
388 + * Method get_website_client_tokens_data()
389 + *
390 + * Get website client tokens.
391 + *
392 + * @param int $websiteid Website ID.
393 + *
394 + * @return mixed $result Result of tokens.
395 + */
396 + public function get_website_client_tokens_data( $websiteid = false ) { // phpcs:ignore -- comlex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
397 + if ( empty( $websiteid ) ) {
398 + return false;
399 + }
400 + $website = MainWP_DB::instance()->get_website_by_id( $websiteid );
401 + if ( empty( $website ) ) {
402 + return false;
403 + }
432 404
433 - if ( empty( $inserted ) ) {
434 - return array(
435 - 'error' => esc_html__( 'Undefined error. Please try again.', 'mainwp' ),
436 - );
437 - }
405 + $client_tokens = array();
438 406
439 - $selected_sites = ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) ) : array();
407 + $client_tokens['client.site.url'] = $website->url;
408 + $client_tokens['client.site.name'] = $website->name;
440 409
441 - MainWP_DB_Client::instance()->update_selected_sites_for_client( $inserted->client_id, $selected_sites );
410 + // init values.
411 + $default_client_fields = self::get_default_client_fields();
412 + foreach ( $default_client_fields as $tok_name => $field ) {
413 + $client_tokens[ $tok_name ] = ''; // the tokens with empty value will be removed from the report.
414 + }
415 + $contact_fields = self::get_default_contact_fields();
416 + if ( ! empty( $contact_fields ) && is_array( $contact_fields ) ) {
417 + foreach ( $contact_fields as $tok_name => $field ) {
418 + $client_tokens[ $tok_name ] = ''; // the tokens with empty value will be removed from the report.
419 + }
420 + }
442 421
443 - return array(
444 - 'success' => true,
445 - 'clientid' => $inserted->client_id,
446 - );
447 - }
448 - } catch ( \Exception $e ) {
449 - return array( 'error' => $e->getMessage() );
450 - }
451 - }
422 + $custom_tokens_fields = MainWP_DB_Client::instance()->get_client_fields( true, $website->client_id );
423 + if ( ! empty( $custom_tokens_fields ) && is_array( $custom_tokens_fields ) ) {
424 + foreach ( $custom_tokens_fields as $fields ) {
425 + $client_tokens[ $fields->field_name ] = ''; // the tokens with empty value will be removed from the report.
426 + }
427 + }
452 428
429 + if ( empty( $website->client_id ) ) {
430 + return $client_tokens;
431 + }
453 432
454 - /**
455 - * Method get_website_client_tokens_data()
456 - *
457 - * Get website client tokens.
458 - *
459 - * @param int $websiteid Website ID.
460 - *
461 - * @return mixed $result Result of tokens.
462 - */
463 - public function get_website_client_tokens_data( $websiteid = false ) { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
464 - if ( empty( $websiteid ) ) {
465 - return false;
466 - }
467 - $website = MainWP_DB::instance()->get_website_by_id( $websiteid );
468 - if ( empty( $website ) ) {
469 - return false;
470 - }
433 + $clientid = $website->client_id;
471 434
472 - $client_tokens = array();
435 + $client_info = MainWP_DB_Client::instance()->get_wp_client_by( 'client_id', $clientid );
473 436
474 - $client_tokens['client.site.url'] = $website->url;
475 - $client_tokens['client.site.name'] = $website->name;
437 + if ( empty( $client_info ) ) {
438 + return $client_tokens;
439 + }
476 440
477 - // init values.
478 - $default_client_fields = static::get_default_client_fields();
479 - foreach ( $default_client_fields as $tok_name => $field ) {
480 - $client_tokens[ $tok_name ] = ''; // the tokens with empty value will be removed from the report.
481 - }
482 - $contact_fields = static::get_default_contact_fields();
483 - if ( ! empty( $contact_fields ) && is_array( $contact_fields ) ) {
484 - foreach ( $contact_fields as $tok_name => $field ) {
485 - $client_tokens[ $tok_name ] = ''; // the tokens with empty value will be removed from the report.
486 - }
487 - }
441 + if ( ! empty( $custom_tokens_fields ) && is_array( $custom_tokens_fields ) ) {
442 + foreach ( $custom_tokens_fields as $fields ) {
443 + $client_tokens[ $fields->field_name ] = $fields->field_value;
444 + }
445 + }
488 446
489 - $custom_tokens_fields = MainWP_DB_Client::instance()->get_client_fields( true, $website->client_id );
490 - if ( ! empty( $custom_tokens_fields ) && is_array( $custom_tokens_fields ) ) {
491 - foreach ( $custom_tokens_fields as $fields ) {
492 - $client_tokens[ $fields->field_name ] = ''; // the tokens with empty value will be removed from the report.
493 - }
494 - }
447 + foreach ( $default_client_fields as $tok_name => $field ) {
448 + $db_field = $field['db_field'];
449 + if ( property_exists( $client_info, $db_field ) ) {
450 + $client_tokens[ $tok_name ] = $client_info->{$db_field};
451 + }
452 + }
495 453
496 - if ( empty( $website->client_id ) ) {
497 - return $client_tokens;
498 - }
454 + if ( ! empty( $contact_fields ) && is_array( $contact_fields ) ) {
455 + foreach ( $contact_fields as $tok_name => $field ) {
456 + $db_field = isset( $field['db_field'] ) ? $field['db_field'] : '';
457 + $val = '' != $db_field && property_exists( $client_info, $db_field ) ? $client_info->{$db_field} : false;
458 + if ( false !== $val ) {
459 + $client_tokens[ $tok_name ] = $val;
460 + }
461 + }
462 + }
463 + $client_tokens = apply_filters( 'mainwp_clients_website_client_tokens', $client_tokens, $websiteid, $clientid );
464 + return $client_tokens;
465 + }
499 466
500 - $clientid = $website->client_id;
467 + /**
468 + * Method get_favico_url()
469 + *
470 + * Get Child Site favicon URL.
471 + *
472 + * @param string $image_path Client image url path.
473 + *
474 + * @return mixed $full_url Full image URL.
475 + */
476 + public static function get_client_image_url( $image_path ) {
477 + $full_url = '';
478 + if ( ! empty( $image_path ) ) {
479 + $dirs = MainWP_System_Utility::get_mainwp_dir( 'client-images', true );
480 + if ( file_exists( $dirs[0] . $image_path ) ) {
481 + $full_url = $dirs[1] . $image_path;
482 + } else {
483 + $full_url = '';
484 + }
485 + }
486 + return $full_url;
487 + }
501 488
502 - $client_info = MainWP_DB_Client::instance()->get_wp_client_by( 'client_id', $clientid );
503 -
504 - if ( empty( $client_info ) ) {
505 - return $client_tokens;
506 - }
507 -
508 - if ( ! empty( $custom_tokens_fields ) && is_array( $custom_tokens_fields ) ) {
509 - foreach ( $custom_tokens_fields as $fields ) {
510 - $client_tokens[ $fields->field_name ] = $fields->field_value;
511 - }
512 - }
513 -
514 - foreach ( $default_client_fields as $tok_name => $field ) {
515 - $db_field = $field['db_field'];
516 - if ( property_exists( $client_info, $db_field ) ) {
517 - $client_tokens[ $tok_name ] = $client_info->{$db_field};
518 - }
519 - }
520 -
521 - if ( ! empty( $contact_fields ) && is_array( $contact_fields ) ) {
522 - foreach ( $contact_fields as $tok_name => $field ) {
523 - $db_field = isset( $field['db_field'] ) ? $field['db_field'] : '';
524 - $val = '' !== $db_field && property_exists( $client_info, $db_field ) ? $client_info->{$db_field} : false;
525 - if ( false !== $val ) {
526 - $client_tokens[ $tok_name ] = $val;
527 - }
528 - }
529 - }
530 - $client_tokens = apply_filters( 'mainwp_clients_website_client_tokens', $client_tokens, $websiteid, $clientid );
531 - return $client_tokens;
532 - }
533 -
534 - /**
535 - * Method get_client_contact_image()
536 - *
537 - * Get client contact image.
538 - *
539 - * @param array $item client|contact array data.
540 - * @param string $type type of image.
541 - * @param string $what image for what.
542 - */
543 - public static function get_client_contact_image( $item, $type = 'client', $what = 'default' ) {
544 -
545 - if ( empty( $item ) ) {
546 - return '';
547 - }
548 -
549 - $dirs = MainWP_System_Utility::get_mainwp_dir( 'client-images', true );
550 -
551 - if ( 'client' === $type ) {
552 - $image_path = $item['image'];
553 - $icon_info = $item['selected_icon_info'];
554 - } else {
555 - $image_path = $item['contact_image'];
556 - $icon_info = $item['contact_icon_info'];
557 - }
558 -
559 - if ( 'uploaded_icon' === $what ) {
560 - $full_url = '';
561 - if ( ! empty( $image_path ) && file_exists( $dirs[0] . $image_path ) ) {
562 - $full_url = $dirs[1] . $image_path;
563 - }
564 - return $full_url; // return path to uploaded icon.
565 - }
566 -
567 - $img_cls = 'ui mini circular image';
568 - if ( 'card' === $what ) {
569 - $img_cls = 'ui medium circular image';
570 - }
571 -
572 - $icon_wrapper_attr = ' class="' . $img_cls . '" style="height:auto;display:inline-block;" ';
573 - if ( 'display_edit' === $what ) {
574 - $icon_wrapper_attr = ' id="mainwp_add_edit_client_upload_custom_icon" style="width:32px;height:auto;display:inline-block;" class="' . esc_attr( $img_cls ) . '" ';
575 - if ( 'contact' === $type ) {
576 - $icon_wrapper_attr = ' style="width:32px;height:auto;display:inline-block;" class="mainwp_add_edit_contact_upload_custom_icon ' . esc_attr( $img_cls ) . '" ';
577 - }
578 - }
579 -
580 - $img = '';
581 -
582 - if ( ! empty( $image_path ) && file_exists( $dirs[0] . $image_path ) ) {
583 - $full_url = $dirs[1] . $image_path;
584 - $img = '<img ' . $icon_wrapper_attr . ' src="' . esc_attr( $full_url ) . '" alt="' . esc_attr( $item['name'] ) . '">';
585 - } elseif ( ! empty( $icon_info ) ) {
586 - $img = MainWP_Client::get_cust_client_icon( $icon_info, 'display', $what );
587 - }
588 - return $img;
589 - }
590 -
591 -
592 - /**
593 - * Method get_client_image_url()
594 - *
595 - * Get client icon URL.
596 - *
597 - * @param string $image_path Client image url path.
598 - *
599 - * @return mixed $full_url Full image URL.
600 - */
601 - public static function get_client_image_url( $image_path ) {
602 - $full_url = '';
603 - if ( ! empty( $image_path ) ) {
604 - if ( false !== stripos( $image_path, 'assets/images/demo/clients/' ) || false !== stripos( $image_path, 'assets/images/demo/contacts/' ) ) {
605 - $full_url = MAINWP_PLUGIN_URL . $image_path;
606 - } else {
607 - $dirs = MainWP_System_Utility::get_mainwp_dir( 'client-images', true );
608 - if ( file_exists( $dirs[0] . $image_path ) ) {
609 - $full_url = $dirs[1] . $image_path;
610 - } else {
611 - $full_url = '';
612 - }
613 - }
614 - }
615 - return $full_url;
616 - }
617 -
618 - /**
619 - * Method show_notice_existed_contact_emails()
620 - */
621 - public static function show_notice_existed_contact_emails() {
622 - $existed_emails = MainWP_Utility::get_flash_message( 'contact_existed_emails' );
623 - if ( ! empty( $existed_emails ) ) {
624 - $existed_emails = esc_html( $existed_emails );
625 - $existed_emails = str_replace( '|', '<br/>', $existed_emails );
626 - ?>
627 - <div class="ui yellow message">
628 - <?php printf( esc_html__( 'Existed contact emails.%sPlease try again.', 'mainwp' ), '<br/>' . $existed_emails . '</br>' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
629 - </div>
630 - <?php
631 - }
632 - }
489 + /**
490 + * Method show_notice_existed_contact_emails()
491 + */
492 + public static function show_notice_existed_contact_emails() {
493 + $existed_emails = MainWP_Utility::get_flash_message( 'contact_existed_emails' );
494 + if ( ! empty( $existed_emails ) ) {
495 + $existed_emails = esc_html( $existed_emails );
496 + $existed_emails = str_replace( '|', '<br/>', $existed_emails );
497 + ?>
498 + <div class="ui yellow message">
499 + <?php echo sprintf( esc_html__( 'Existed contact emails.%sPlease try again.', 'mainwp' ), '<br/>' . $existed_emails . '</br>' ); ?>
500 + </div>
501 + <?php
502 + }
503 + }
633 504 }