PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.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
mainwp / class / class-mainwp-db-client.php

class-mainwp-db-client.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.1, at class/class-mainwp-db-client.php

1,312 lines 48.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Database Client
4 *
5 * This file handles all interactions with the Client DB.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * Class MainWP_DB_Client
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_DB_Client extends MainWP_DB { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
18
19 // phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared, Generic.Metrics.CyclomaticComplexity -- unprepared SQL ok, accessing the database directly to custom database functions.
20
21 /**
22 * Private static variable to hold the single instance of the class.
23 *
24 * @static
25 *
26 * @var mixed Default null
27 */
28 private static $instance = null;
29
30 /**
31 * Method instance()
32 *
33 * Create public static instance.
34 *
35 * @static
36 * @return MainWP_DB_Client
37 */
38 public static function instance() {
39 if ( null === static::$instance ) {
40 static::$instance = new self();
41 }
42 return static::$instance;
43 }
44
45 /**
46 * Constructor.
47 *
48 * Run each time the class is called.
49 */
50 public function __construct() {
51 parent::__construct();
52 add_filter( 'mainwp_db_install_tables', array( $this, 'hook_db_install_tables' ), 10, 3 );
53 }
54
55 /**
56 * Method hook_db_install_tables()
57 *
58 * Get the query to install db tables.
59 *
60 * @param array $sql input filter.
61 * @param string $currentVersion Current db Version.
62 * @param string $charset_collate charset collate.
63 *
64 * @return array $sql queries.
65 */
66 public function hook_db_install_tables( $sql, $currentVersion, $charset_collate ) {
67
68 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_clients' ) . " (
69 client_id int(11) NOT NULL auto_increment,
70 image varchar(255) NOT NULL default '',
71 name varchar(255) NOT NULL,
72 address_1 varchar(255) NOT NULL default '',
73 address_2 varchar(255) NOT NULL default '',
74 city varchar(100) NOT NULL default '',
75 zip varchar(32) NOT NULL default '',
76 state varchar(200) NOT NULL default '',
77 country varchar(200) NOT NULL default '',
78 note longtext NOT NULL default '',
79 selected_icon_info varchar(64) NOT NULL default '',
80 client_email varchar(191) NOT NULL,
81 client_phone varchar(100) NOT NULL default '',
82 client_facebook varchar(255) NOT NULL default '',
83 client_twitter varchar(255) NOT NULL default '',
84 client_instagram varchar(255) NOT NULL default '',
85 client_linkedin varchar(255) NOT NULL default '',
86 created int(11) NOT NULL DEFAULT 0,
87 `suspended` tinyint(1) NOT NULL DEFAULT 0,
88 primary_contact_id int(11) NOT NULL default 0";
89
90 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.61', '<=' ) ) {
91 $tbl .= ',
92 PRIMARY KEY (client_id)';
93 }
94 $tbl .= ') ' . $charset_collate;
95 $sql[] = $tbl;
96
97 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_clients_fields' ) . ' (
98 field_id int(11) NOT NULL auto_increment,
99 field_name varchar(191) NOT NULL DEFAULT "",
100 field_desc varchar(255) NOT NULL DEFAULT "",
101 client_id int(11) NOT NULL,
102 UNIQUE KEY client_id_field_name (client_id,field_name)';
103
104 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.61', '<=' ) ) {
105 $tbl .= ',
106 PRIMARY KEY (`field_id`) ';
107 }
108
109 $tbl .= ') ' . $charset_collate;
110 $sql[] = $tbl;
111
112 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_clients_field_values' ) . ' (
113 value_id int(11) NOT NULL auto_increment,
114 field_id int(11) NOT NULL,
115 field_value longtext NOT NULL DEFAULT "",
116 value_client_id int(11) NOT NULL,
117 UNIQUE KEY value_client_id_field_id (value_client_id, field_id)';
118
119 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.61', '<=' ) ) {
120 $tbl .= ',
121 PRIMARY KEY (`value_id`) ';
122 }
123
124 $tbl .= ') ' . $charset_collate;
125 $sql[] = $tbl;
126
127 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_clients_contacts' ) . ' (
128 contact_id int(11) NOT NULL auto_increment,
129 contact_client_id int(11) NOT NULL,
130 contact_email varchar(191) NOT NULL,
131 contact_image varchar(255) NOT NULL default "",
132 contact_icon_info varchar(64) NOT NULL default "",
133 contact_name varchar(255) NOT NULL,
134 contact_phone varchar(100) NOT NULL default "",
135 contact_role varchar(255) NOT NULL default "",
136 facebook varchar(255) NOT NULL default "",
137 twitter varchar(255) NOT NULL default "",
138 instagram varchar(255) NOT NULL default "",
139 linkedin varchar(255) NOT NULL default "",
140 UNIQUE KEY contact_email(contact_email)';
141
142 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.69', '<=' ) ) {
143 $tbl .= ',
144 PRIMARY KEY (contact_id)';
145 }
146 $tbl .= ') ' . $charset_collate;
147 $sql[] = $tbl;
148
149 return $sql;
150 }
151
152 /**
153 * Method check_to_updates_reports_data_861()
154 *
155 * Check version less than 8.61 to import reports data.
156 *
157 * @param string $currentVersion Current db Version.
158 *
159 * @return void.
160 */
161 public function check_to_updates_reports_data_861( $currentVersion ) {
162
163 $convert_pro_reports_dt = false;
164 $convert_client_reports_dt = false;
165 if ( is_plugin_active( 'mainwp-pro-reports-extension/mainwp-pro-reports-extension.php' ) ) {
166 $convert_pro_reports_dt = true;
167 } elseif ( is_plugin_active( 'mainwp-client-reports-extension/mainwp-client-reports-extension.php' ) ) {
168 $convert_client_reports_dt = true;
169 }
170
171 if ( ! $convert_pro_reports_dt && ! $convert_client_reports_dt ) {
172 $rslt = $this->query( "SHOW TABLES LIKE '" . $this->table_name( 'pro_reports_token' ) . "'" );
173 if ( static::num_rows( $rslt ) ) {
174 $convert_pro_reports_dt = true;
175 }
176
177 $rslt = $this->query( "SHOW TABLES LIKE '" . $this->table_name( 'client_report_token' ) . "'" );
178 if ( static::num_rows( $rslt ) ) {
179 $convert_client_reports_dt = true;
180 }
181 }
182
183 $this->insert_client_fields_861( $currentVersion );
184
185 if ( $convert_pro_reports_dt ) {
186 $this->pro_reports_check_updates_861( $currentVersion );
187 } elseif ( $convert_client_reports_dt ) {
188 $this->client_reports_check_updates_861( $currentVersion );
189 }
190 }
191
192 /**
193 * Method insert_client_fields_861()
194 *
195 * Check version less than 8.61 to import client reports data.
196 *
197 * @param string $currentVersion Current db Version.
198 *
199 * @return null.
200 */
201 public function insert_client_fields_861( $currentVersion ) {
202
203 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.61', '>' ) ) {
204 return;
205 }
206
207 $default_tokens = array(
208 'client.name' => 'Displays the Client Name',
209 'client.contact.name' => 'Displays the Client Contact Name',
210 'client.contact.address.1' => 'Displays the Client Contact Address 1',
211 'client.contact.address.2' => 'Displays the Client Contact Address 2',
212 'client.city' => 'Displays the Client City',
213 'client.state' => 'Displays the Client State',
214 'client.zip' => 'Displays the Client Zip',
215 'client.phone' => 'Displays the Client Phone',
216 'client.email' => 'Displays the Client Email',
217 'client.note' => 'Displays the Client Note',
218 );
219
220 // create or update default token.
221 foreach ( $default_tokens as $field_name => $field_desc ) {
222 $current = $this->get_client_fields_by( 'field_name', $field_name );
223 if ( $current ) {
224 $this->update_client_field(
225 $current->field_id,
226 array(
227 'field_name' => $field_name,
228 'field_desc' => $field_desc,
229 )
230 );
231 } else {
232 $this->add_client_field(
233 array(
234 'field_name' => $field_name,
235 'field_desc' => $field_desc,
236 'client_id' => 0,
237 )
238 );
239
240 }
241 }
242 }
243
244 /**
245 * Method client_reports_check_updates_861()
246 *
247 * Check version less than 8.61 to import client reports data.
248 *
249 * @param string $currentVersion Current db Version.
250 *
251 * @return null.
252 */
253 public function client_reports_check_updates_861( $currentVersion ) {
254
255 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.61', '>' ) ) {
256 return;
257 }
258
259 $tokens = $this->client_reports_get_tokens();
260 $sites_tokens_values = $this->client_reports_get_site_token_values();
261
262 $this->reports_check_updates_861( $tokens, $sites_tokens_values );
263 }
264
265 /**
266 * Method pro_reports_check_updates_861()
267 *
268 * Check version less than 8.61 to import pro reports data.
269 *
270 * @param string $currentVersion Current db Version.
271 *
272 * @return null.
273 */
274 public function pro_reports_check_updates_861( $currentVersion ) {
275
276 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.61', '>' ) ) {
277 return;
278 }
279
280 $tokens = $this->pro_reports_get_tokens();
281 $sites_tokens_values = $this->pro_reports_get_site_token_values();
282
283 $this->reports_check_updates_861( $tokens, $sites_tokens_values );
284 }
285
286
287 /**
288 * Method reports_check_updates_861()
289 *
290 * Check version less than 8.61 to import reports data.
291 *
292 * @param array $tokens Tokens.
293 * @param array $sites_tokens_values Tokens values.
294 *
295 * @return void.
296 */
297 public function reports_check_updates_861( $tokens, $sites_tokens_values ) { // phpcs:ignore -- NOSONAR complexex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
298
299 $default_client_fields = MainWP_Client_Handler::get_default_client_fields();
300 $default_contact_fields = MainWP_Client_Handler::get_default_contact_fields();
301
302 $compatible_tokens = MainWP_Client_Handler::get_compatible_tokens();
303
304 $ids_to_tokens = array();
305
306 if ( $tokens ) {
307 foreach ( $tokens as $token ) {
308 $tok_name = $token->token_name;
309 $ids_to_tokens[ $token->id ] = array(
310 'token_name' => $tok_name,
311 'token_desc' => $token->token_description,
312 );
313 }
314 }
315
316 $ids_to_tokens_values = array();
317
318 if ( $sites_tokens_values ) {
319 foreach ( $sites_tokens_values as $token ) {
320 if ( ! isset( $ids_to_tokens[ $token->token_id ] ) ) {
321 continue;
322 }
323 if ( empty( $token->token_value ) ) {
324 continue;
325 }
326 $tok_name = $ids_to_tokens[ $token->token_id ]['token_name'];
327
328 $ids_to_tokens_values[ $token->site_id ][ $tok_name ] = array(
329 'token_value' => $token->token_value,
330 'token_desc' => $ids_to_tokens[ $token->token_id ]['token_desc'],
331 );
332 }
333 }
334
335 $clients_to_add = array();
336 $primary_contacts_to_add = array();
337 $custom_tokens_to_add = array();
338
339 foreach ( $ids_to_tokens_values as $site_id => $tokens ) {
340
341 if ( empty( $tokens['client.email'] ) ) {
342 continue;
343 }
344
345 $email = $tokens['client.email']['token_value'];
346
347 if ( ! isset( $clients_to_add[ $email ] ) ) {
348 $clients_to_add[ $email ] = array();
349 }
350
351 if ( ! isset( $clients_to_add[ $email ]['site_ids'] ) ) {
352 $clients_to_add[ $email ]['site_ids'] = array();
353 }
354
355 $clients_to_add[ $email ]['site_ids'][] = $site_id;
356
357 // prepare clients to add.
358 foreach ( $default_client_fields as $field_name => $field ) {
359 if ( ! isset( $tokens[ $field_name ] ) ) {
360 continue;
361 }
362 $clients_to_add[ $email ][ $field['db_field'] ] = $tokens[ $field_name ]['token_value'];
363 }
364
365 $flip_compatible_tokens = array_flip( $compatible_tokens ); // flip: new_token_name => old_token_name.
366 // prepare primary contacts to add.
367 foreach ( $default_contact_fields as $field_name => $field ) {
368
369 // $field_name - new_token_name.
370 // $tok_name - old_token_name.
371 $tok_name = isset( $flip_compatible_tokens[ $field_name ] ) ? $flip_compatible_tokens[ $field_name ] : $field_name;
372 if ( ! isset( $tokens[ $tok_name ] ) ) {
373 continue;
374 }
375
376 $primary_contacts_to_add[ $email ][ $field['db_field'] ] = $tokens[ $tok_name ]['token_value'];
377 }
378
379 foreach ( $tokens as $tok_name => $tok_value_desc ) {
380 if ( isset( $default_client_fields[ $tok_name ] ) || isset( $default_contact_fields[ $tok_name ] ) ) {
381 continue;
382 }
383 if ( 'client.site.url' === $tok_name || 'client.site.name' === $tok_name ) { // avoid those tokens.
384 continue;
385 }
386 if ( isset( $compatible_tokens[ $tok_name ] ) ) {
387 continue;
388 }
389 $custom_tokens_to_add[ $email ][ $tok_name ] = $tok_value_desc;
390 }
391 }
392
393 $emails_to_ids = array();
394 foreach ( $clients_to_add as $email => $client_add ) {
395 $selected_sites = array();
396 if ( isset( $client_add['site_ids'] ) ) {
397 $selected_sites = $client_add['site_ids'];
398 unset( $client_add['site_ids'] );
399 }
400
401 $inserted = $this->update_client( $client_add ); // add new client.
402
403 if ( $inserted ) {
404 $client_id = $inserted->client_id;
405 $this->update_selected_sites_for_client( $client_id, $selected_sites );
406 $emails_to_ids[ $email ] = $client_id;
407 }
408 }
409
410 foreach ( $emails_to_ids as $email => $client_id ) {
411
412 if ( isset( $primary_contacts_to_add[ $email ] ) ) {
413 $contact_add = $primary_contacts_to_add[ $email ];
414 $contact_add['contact_client_id'] = $client_id;
415 $inserted = $this->update_client_contact( $contact_add );
416 if ( $inserted ) {
417 $params = array(
418 'client_id' => $client_id, // update the client.
419 'primary_contact_id' => $inserted->contact_id,
420 );
421 $this->update_client( $params );
422 }
423 }
424
425 if ( isset( $custom_tokens_to_add[ $email ] ) ) {
426 $custom_tokens = $custom_tokens_to_add[ $email ];
427 foreach ( $custom_tokens as $tok_name => $tok_value_desc ) {
428 $field_id = 0;
429 $current = $this->get_client_fields_by( 'field_name', $tok_name ); // get general field by name.
430 if ( $current ) {
431 $field_id = $current->field_id;
432 } else {
433 $field = array(
434 'field_name' => $tok_name,
435 'field_desc' => $tok_value_desc['token_desc'],
436 'client_id' => 0, // it is general tokens.
437 );
438 $new_field = $this->add_client_field( $field );
439 if ( $new_field ) {
440 $field_id = $new_field->field_id;
441 }
442 }
443
444 if ( $field_id ) {
445 $this->update_client_field_value( $field_id, $tok_value_desc['token_value'], $client_id );
446 }
447 }
448 }
449 }
450 }
451
452
453 /**
454 * Method update_client.
455 *
456 * Create or update client.
457 *
458 * @param array $data Client data.
459 * @param bool $throw_out Throw or return error.
460 *
461 * @throws MainWP_Exception On errors.
462 *
463 * @return bool.
464 */
465 public function update_client( $data, $throw_out = false ) {
466 if ( empty( $data ) || ! is_array( $data ) ) {
467 return false;
468 }
469
470 $client_id = isset( $data['client_id'] ) ? (int) $data['client_id'] : 0;
471
472 if ( isset( $data['client_email'] ) && ! empty( $data['client_email'] ) ) {
473 $client_existed = $this->get_wp_client_by( 'client_email', $data['client_email'], ARRAY_A );
474 if ( is_array( $client_existed ) && isset( $client_existed['client_id'] ) && ( empty( $client_id ) || $client_id !== (int) $client_existed['client_id'] ) ) {
475 if ( $throw_out ) {
476 throw new MainWP_Exception( esc_html__( 'Client email exists. Please try again.', 'mainwp' ) );
477 } else {
478 return false;
479 }
480 }
481 }
482
483 if ( ! empty( $client_id ) ) {
484 $this->wpdb->update( $this->table_name( 'wp_clients' ), $data, array( 'client_id' => intval( $client_id ) ) );
485 return $this->get_wp_client_by( 'client_id', $client_id );
486 } elseif ( $this->wpdb->insert( $this->table_name( 'wp_clients' ), $data ) ) {
487 return $this->get_wp_client_by( 'client_id', $this->wpdb->insert_id );
488 }
489 return false;
490 }
491
492
493 /**
494 * Method update_client.
495 *
496 * Create or update client contact.
497 *
498 * @param array $data Contact data.
499 *
500 * @return bool.
501 */
502 public function update_client_contact( $data ) { // phpcs:ignore -- NOSONAR - complex.
503
504 if ( empty( $data ) ) {
505 return false;
506 }
507
508 $contact_id = isset( $data['contact_id'] ) ? (int) $data['contact_id'] : 0;
509
510 $contact_email = isset( $data['contact_email'] ) ? $data['contact_email'] : '';
511 if ( ! empty( $contact_email ) ) {
512 $existed_email = false;
513 // check to valid email.
514 $curret_contact = $this->get_wp_client_contact_by( 'contact_email', $contact_email );
515 if ( $curret_contact && empty( $contact_id ) ) { // add new contact with existed email => failed.
516 $existed_email = true;
517 }
518
519 if ( $curret_contact && ! empty( $contact_id ) && (int) $curret_contact->contact_id !== $contact_id ) { // update contact with existed email => failed.
520 $existed_email = true;
521 }
522
523 if ( $existed_email ) {
524 MainWP_Utility::update_flash_message( 'contact_existed_emails', $contact_email );
525 return false;
526 }
527 }
528
529 if ( ! empty( $contact_id ) ) {
530 $this->wpdb->update( $this->table_name( 'wp_clients_contacts' ), $data, array( 'contact_id' => intval( $contact_id ) ) );
531 return $this->get_wp_client_contact_by( 'contact_id', $contact_id );
532 } elseif ( $this->wpdb->insert( $this->table_name( 'wp_clients_contacts' ), $data ) ) {
533 return $this->get_wp_client_contact_by( 'contact_id', $this->wpdb->insert_id );
534 }
535 return false;
536 }
537
538 /**
539 * Method get_wp_client_contact_by.
540 *
541 * Get client by.
542 *
543 * @param string $by by.
544 * @param mixed $value by value.
545 * @param mixed $obj Format data.
546 * @param array $params other params.
547 *
548 * @return mixed $result results.
549 */
550 public function get_wp_client_contact_by( $by = 'contact_id', $value = null, $obj = OBJECT, $params = array() ) { // phpcs:ignore -- NOSONAR - complex.
551
552 $sql = '';
553
554 if ( 'client_id' === $by && ! empty( $value ) ) {
555 $sql = $this->wpdb->prepare( 'SELECT wc.* FROM ' . $this->table_name( 'wp_clients_contacts' ) . ' wc WHERE wc.contact_client_id=%d ', $value );
556 }
557
558 if ( ! empty( $sql ) ) {
559 if ( OBJECT === $obj ) {
560 $result = $this->wpdb->get_results( $sql, OBJECT );
561 } else {
562 $result = $this->wpdb->get_results( $sql, ARRAY_A );
563 }
564 return $result;
565 }
566
567 if ( 'contact_id' === $by && is_numeric( $value ) ) {
568 $sql = $this->wpdb->prepare( 'SELECT wc.* FROM ' . $this->table_name( 'wp_clients_contacts' ) . ' wc WHERE wc.contact_id=%d ', $value );
569 } elseif ( 'contact_email' === $by && ! empty( $value ) ) {
570 $sql = $this->wpdb->prepare( 'SELECT wc.* FROM ' . $this->table_name( 'wp_clients_contacts' ) . ' wc WHERE wc.contact_email=%s ', $value );
571 }
572
573 $result = null;
574 if ( ! empty( $sql ) ) {
575 if ( OBJECT === $obj ) {
576 $result = $this->wpdb->get_row( $sql, OBJECT );
577 } else {
578 $result = $this->wpdb->get_row( $sql, ARRAY_A );
579 }
580 }
581 return $result;
582 }
583
584
585 /**
586 * Method delete_client.
587 *
588 * Delete client.
589 *
590 * @param int $client_id Client id of the contact.
591 * @param int $contact_id Contact id to delete.
592 *
593 * @return bool Results.
594 */
595 public function delete_client_contact( $client_id, $contact_id ) {
596 $current = $this->get_wp_client_contact_by( 'contact_id', $contact_id );
597 if ( $current && $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_clients_contacts' ) . ' WHERE contact_client_id=%d AND contact_id = %d', $client_id, $contact_id ) ) ) {
598 if ( ! empty( $current->contact_image ) ) {
599 $dirs = MainWP_System_Utility::get_mainwp_dir( 'client-images', true );
600 $base_dir = $dirs[0];
601 $old_file = $base_dir . '/' . $current->contact_image;
602 MainWP_Utility::delete_file( $old_file );
603 }
604 return true;
605 }
606 return false;
607 }
608
609
610 /**
611 * Method delete_client_contacts_by_client_id.
612 *
613 * Delete client contacts by client id.
614 *
615 * @param int $client_id Client id.
616 *
617 * @return bool Results.
618 */
619 public function delete_client_contacts_by_client_id( $client_id ) {
620
621 if ( empty( $client_id ) ) {
622 return false;
623 }
624
625 $client_contacts = $this->get_wp_client_contact_by( 'client_id', $client_id, ARRAY_A );
626 if ( $client_contacts ) {
627 foreach ( $client_contacts as $contact ) {
628 $this->delete_client_contact( $client_id, $contact['contact_id'] );
629 }
630 }
631 return true;
632 }
633
634
635 /**
636 * Method get_wp_client_by.
637 *
638 * Get client by.
639 *
640 * @param string $by by.
641 * @param mixed $value by value.
642 * @param mixed $obj Format data.
643 * @param bool $params Others params.
644 *
645 * @return mixed $result results.
646 */
647 public function get_wp_client_by( $by = 'client_id', $value = null, $obj = OBJECT, $params = array() ) { // phpcs:ignore -- NOSONAR - complexex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
648
649 $with_selected_sites = isset( $params['with_selected_sites'] ) && $params['with_selected_sites'] ? true : false;
650 $with_tags = isset( $params['with_tags'] ) && $params['with_tags'] ? true : false;
651 $group_ids = isset( $params['by_tags'] ) ? $params['by_tags'] : '';
652
653 // valid group ids.
654 if ( is_array( $group_ids ) ) {
655 $group_ids = array_filter(
656 $group_ids,
657 function ( $e ) {
658 if ( 'nogroups' === $e ) {
659 return true;
660 }
661 return ( is_numeric( $e ) && 0 < $e ) ? true : false;
662 }
663 );
664 } else {
665 $group_ids = '';
666 }
667
668 $select_sites = '';
669 $select_tags = '';
670 $where_clients = '';
671
672 $sql = '';
673
674 if ( $with_selected_sites ) {
675 $select_sites .= ',( SELECT GROUP_CONCAT(wp.id SEPARATOR ",") FROM ' . $this->table_name( 'wp' ) . ' wp WHERE wp.client_id = wc.client_id ) as selected_sites ';
676 }
677
678 if ( ! empty( $group_ids ) ) {
679
680 $join_group = '';
681 $where_group = '';
682
683 if ( in_array( 'nogroups', $group_ids ) ) {
684 $join_group = ' LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid ';
685 $group_ids = array_filter(
686 $group_ids,
687 function ( $e ) {
688 return 'nogroups' !== $e;
689 }
690 );
691 if ( ! empty( $group_ids ) ) {
692 $groups = implode( ',', $group_ids );
693 $where_group = ' AND ( wpgroup.groupid IS NULL OR wpgroup.groupid IN (' . $groups . ') ) ';
694 } else {
695 $where_group = ' AND wpgroup.groupid IS NULL ';
696 }
697 } else {
698 $groups = implode( ',', $group_ids );
699 $join_group = ' JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid ';
700 $where_group = ' AND wpgroup.groupid IN (' . $groups . ') ';
701 }
702
703 $sql_tags = ' SELECT wp.client_id FROM ' . $this->table_name( 'wp' ) . ' wp ';
704 $sql_tags .= $join_group;
705 $sql_tags .= ' WHERE 1 ' . $where_group;
706 $result_tags = $this->wpdb->get_results( $sql_tags );
707
708 if ( empty( $result_tags ) ) {
709 return array();
710 } else {
711 $cli_ids = array();
712 foreach ( $result_tags as $item ) {
713 $cli_ids[] = $item->client_id;
714 }
715 $cli_ids = array_values( array_unique( $cli_ids ) );
716 $where_clients = ' AND wc.client_id IN (' . implode( ',', $cli_ids ) . ') ';
717 }
718 }
719
720 if ( $with_tags ) {
721 $select_tags .= ", ( SELECT GROUP_CONCAT(gr.name ORDER BY gr.name SEPARATOR ',') FROM " . $this->table_name( 'wp' ) . ' wp ';
722 $select_tags .= ' LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid ';
723 $select_tags .= ' LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id ';
724 $select_tags .= ' WHERE wp.client_id = wc.client_id ) as wpgroups ';
725
726 $select_tags .= ", ( SELECT GROUP_CONCAT(gr.id ORDER BY gr.id SEPARATOR ',') FROM " . $this->table_name( 'wp' ) . ' wp ';
727 $select_tags .= ' LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid ';
728 $select_tags .= ' LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id ';
729 $select_tags .= ' WHERE wp.client_id = wc.client_id ) as wpgroupids ';
730 }
731
732 $join_contact = ' LEFT JOIN ' . $this->table_name( 'wp_clients_contacts' ) . ' cc ON wc.primary_contact_id = cc.contact_id ';
733
734 if ( 'all' === $by ) {
735 $sql = ' SELECT wc.*, cc.* ' . $select_sites . $select_tags;
736 $sql .= ' FROM ' . $this->table_name( 'wp_clients' ) . ' wc ';
737 $sql .= $join_contact;
738 $sql .= ' WHERE 1 ' . $where_clients;
739 $sql .= ' ORDER BY wc.name';
740 }
741
742 if ( ! empty( $sql ) ) {
743 if ( OBJECT === $obj ) {
744 $result = $this->wpdb->get_results( $sql, OBJECT );
745 } else {
746 $result = $this->wpdb->get_results( $sql, ARRAY_A );
747 }
748 return $result;
749 }
750
751 if ( 'client_id' === $by && is_numeric( $value ) ) {
752 $sql = $this->wpdb->prepare( 'SELECT wc.*, cc.* ' . $select_sites . $select_tags . ' FROM ' . $this->table_name( 'wp_clients' ) . ' wc ' . $join_contact . ' WHERE wc.client_id=%d ', $value );
753 } elseif ( 'client_email' === $by && ! empty( $value ) ) {
754 $sql = $this->wpdb->prepare( 'SELECT wc.*, cc.* ' . $select_sites . $select_tags . ' FROM ' . $this->table_name( 'wp_clients' ) . ' wc ' . $join_contact . ' WHERE wc.client_email=%s ', $value );
755 }
756
757 $result = null;
758 if ( ! empty( $sql ) ) {
759 if ( OBJECT === $obj ) {
760 $result = $this->wpdb->get_row( $sql, OBJECT );
761 } else {
762 $result = $this->wpdb->get_row( $sql, ARRAY_A );
763 }
764 }
765 return $result;
766 }
767
768 /**
769 * Method get_wp_clients.
770 *
771 * Get clients.
772 *
773 * @param array $params params.
774 *
775 * @return mixed result.
776 */
777 public function get_wp_clients( $params = array() ) { // phpcs:ignore -- NOSONAR - complex.
778
779 $custom_field = false;
780 $with_tags = false;
781 $with_contacts = false;
782
783 $where = '';
784 $select_tags = '';
785
786 if ( $params && is_array( $params ) ) {
787 $client = isset( $params['client'] ) ? sanitize_text_field( wp_unslash( $params['client'] ) ) : '';
788 if ( '' !== $client ) {
789 $clients = explode( ';', $client );
790 $clientWhere = '';
791 foreach ( $clients as $clt ) {
792 if ( is_numeric( $clt ) ) {
793 $clientWhere .= intval( $clt ) . ', ';
794 }
795 }
796 $clientWhere = rtrim( $clientWhere, ', ' );
797
798 $where .= ' AND wc.client_id IN (' . $clientWhere . ') ';
799 }
800 $custom_field = $params['custom_fields'] ? true : false;
801
802 $with_tags = isset( $params['with_tags'] ) && $params['with_tags'] ? true : false;
803 $with_contacts = isset( $params['with_contacts'] ) && $params['with_contacts'] ? true : false;
804 }
805
806 if ( $with_tags ) {
807 $select_tags .= ", ( SELECT GROUP_CONCAT(gr.name ORDER BY gr.name SEPARATOR ',') FROM " . $this->table_name( 'wp' ) . ' wp ';
808 $select_tags .= ' LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid ';
809 $select_tags .= ' LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id ';
810 $select_tags .= ' WHERE wp.client_id = wc.client_id ) as wpgroups ';
811
812 $select_tags .= ", ( SELECT GROUP_CONCAT(gr.id ORDER BY gr.id SEPARATOR ',') FROM " . $this->table_name( 'wp' ) . ' wp ';
813 $select_tags .= ' LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid ';
814 $select_tags .= ' LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id ';
815 $select_tags .= ' WHERE wp.client_id = wc.client_id ) as wpgroupids ';
816 }
817
818 $sql = ' SELECT wc.* ';
819 $sql .= ',( SELECT GROUP_CONCAT(wp.id SEPARATOR ",") FROM ' . $this->table_name( 'wp' ) . ' wp WHERE wp.client_id = wc.client_id ) as selected_sites ' . $select_tags;
820 $sql .= ' FROM ' . $this->table_name( 'wp_clients' ) . ' wc ';
821 $sql .= ' WHERE 1 ' . $where . ' ORDER BY wc.name';
822
823 $result = $this->wpdb->get_results( $sql, ARRAY_A );
824
825 $contact_fields = array(
826 'contact_id',
827 'contact_client_id',
828 'contact_email',
829 'contact_name',
830 'contact_phone',
831 'contact_role',
832 'contact_image',
833 'facebook',
834 'twitter',
835 'instagram',
836 'linkedin',
837 );
838
839 $output = array();
840
841 foreach ( $result as $rst ) {
842
843 if ( $custom_field ) {
844 $custom_fields = $this->get_client_fields( true, $rst['client_id'] );
845 if ( $custom_fields ) {
846 foreach ( $custom_fields as $field ) {
847 $rst[ $field->field_name ] = $field->field_value;
848 }
849 }
850 }
851 if ( $with_tags && ! empty( $rst['wpgroupids'] ) && ! empty( $rst['wpgroups'] ) ) {
852 $wpgroupids = explode( ',', $rst['wpgroupids'] );
853 $wpgroups = explode( ',', $rst['wpgroups'] );
854
855 $tags = array();
856 if ( is_array( $wpgroupids ) ) {
857 foreach ( $wpgroupids as $gidx => $groupid ) {
858 if ( $groupid && ! isset( $tags[ $groupid ] ) ) {
859 $tags[ $groupid ] = isset( $wpgroups[ $gidx ] ) ? $wpgroups[ $gidx ] : '';
860 }
861 }
862 }
863
864 $rst['tags'] = $tags;
865 }
866
867 if ( $with_contacts ) {
868 $client_contacts = $this->get_wp_client_contact_by( 'client_id', $rst['client_id'], ARRAY_A );
869
870 $contacts = array();
871
872 if ( is_array( $client_contacts ) ) {
873 foreach ( $client_contacts as $gidx => $contact ) {
874 $contacts[ $contact['contact_id'] ] = MainWP_Utility::map_fields( $contact, $contact_fields, false );
875 }
876 }
877 $rst['contacts'] = $contacts;
878 }
879
880 $output[] = $rst;
881 }
882 return $output;
883 }
884
885 /**
886 * Method update_selected_sites_for_client.
887 *
888 * Update client.
889 *
890 * @param int $client_id client id.
891 * @param array $site_ids site ids.
892 *
893 * @return bool true|false.
894 */
895 public function update_selected_sites_for_client( $client_id, $site_ids ) {
896 if ( empty( $client_id ) ) {
897 return false;
898 }
899
900 $site_ids = array_filter(
901 $site_ids,
902 function ( $e ) {
903 return ( is_numeric( $e ) && 0 < $e ) ? true : false;
904 }
905 );
906
907 if ( is_array( $site_ids ) && ! empty( $site_ids ) ) {
908 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET client_id=0 WHERE client_id=%d AND id NOT IN (' . implode( ',', $site_ids ) . ')', $client_id ) );
909 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET client_id=%d WHERE id IN (' . implode( ',', $site_ids ) . ')', $client_id ) );
910 return true;
911 } else {
912 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET client_id=0 WHERE client_id=%d ', $client_id ) );
913 return true;
914 }
915 }
916
917 /**
918 * Method delete_client.
919 *
920 * Delete client.
921 *
922 * @param int $client_id Client id to delete.
923 * @return bool Results.
924 */
925 public function delete_client( $client_id ) {
926
927 $current = $this->get_wp_client_by( 'client_id', $client_id );
928 if ( $current && $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_clients' ) . ' WHERE client_id=%d ', $client_id ) ) ) {
929 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET client_id=0 WHERE client_id=%d ', $client_id ) );
930 $fields = $this->get_client_fields_by( 'client_id', $client_id );
931 if ( $fields ) {
932 foreach ( $fields as $field ) {
933 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_clients_fields' ) . ' WHERE field_id=%d ', $field->field_id ) );
934 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_clients_field_values' ) . ' WHERE field_id=%d AND value_client_id=%d ', $field->field_id, $client_id ) );
935 }
936 }
937
938 if ( ! empty( $current->image ) ) {
939 $dirs = MainWP_System_Utility::get_mainwp_dir( 'client-images', true );
940 $base_dir = $dirs[0];
941 $old_file = $base_dir . '/' . $current->image;
942 MainWP_Utility::delete_file( $old_file );
943 }
944
945 $this->delete_client_contacts_by_client_id( $client_id );
946
947 /**
948 * Delete client
949 *
950 * Fires after delete a client.
951 *
952 * @param object $current client deleted.
953 *
954 * @since 4.5.1.1
955 */
956 do_action( 'mainwp_client_deleted', $current );
957
958 return true;
959 }
960 return false;
961 }
962
963 /**
964 * Method get_websites_by_client_ids.
965 *
966 * Get websites by client.
967 *
968 * @param int $client_ids Client ids.
969 * @param array $params other params.
970 *
971 * @return mixed $result Results.
972 */
973 public function get_websites_by_client_ids( $client_ids, $params = array() ) {
974
975 if ( empty( $client_ids ) ) {
976 return false;
977 }
978
979 if ( ! is_array( $params ) ) {
980 $params = array();
981 }
982
983 $client = '';
984 if ( is_array( $client_ids ) ) {
985 // valid client ids.
986 $client_ids = array_filter(
987 $client_ids,
988 function ( $e ) {
989 return is_numeric( $e ) && ! empty( $e ) ? true : false; // to valid client ids.
990 }
991 );
992 $client = implode( ';', $client_ids );
993 } else {
994 $client = $client_ids;
995 }
996
997 $params['client'] = $client;
998
999 return MainWP_DB::instance()->get_websites_for_current_user( $params );
1000 }
1001
1002 /**
1003 * Method get_number_sites_of_client.
1004 *
1005 * Get client by.
1006 *
1007 * @param int $client_id Client id.
1008 *
1009 * @return int Number of sites.
1010 */
1011 public function get_number_sites_of_client( $client_id ) {
1012 if ( empty( $client_id ) ) {
1013 return false;
1014 }
1015 return $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT COUNT(wp.id) FROM ' . $this->table_name( 'wp' ) . ' wp WHERE wp.client_id =%d', $client_id ) );
1016 }
1017
1018 /**
1019 * Method count_total_clients.
1020 *
1021 * Count total of Clients.
1022 *
1023 * @return int Total number.
1024 */
1025 public function count_total_clients() {
1026 return $this->wpdb->get_var( 'SELECT COUNT(client_id) FROM ' . $this->table_name( 'wp_clients' ) );
1027 }
1028
1029
1030 /**
1031 * Method add_client_field.
1032 *
1033 * Add client field.
1034 *
1035 * @param mixed $field Client fields data.
1036 *
1037 * @return mixed bool|results.
1038 */
1039 public function add_client_field( $field ) {
1040 if ( ! empty( $field['field_name'] ) && isset( $field['client_id'] ) ) {
1041 if ( $this->get_client_fields_by( 'field_name', $field['field_name'], $field['client_id'] ) ) { // field name existed for the client can not create.
1042 return false;
1043 }
1044 if ( $this->wpdb->insert( $this->table_name( 'wp_clients_fields' ), $field ) ) {
1045 return $this->get_client_fields_by( 'field_id', $this->wpdb->insert_id );
1046 }
1047 }
1048 return false;
1049 }
1050
1051 /**
1052 * Method update_client_field.
1053 *
1054 * Update client field.
1055 *
1056 * @param int $field_id field id.
1057 * @param array $field field data.
1058 *
1059 * @return mixed|false field.
1060 */
1061 public function update_client_field( $field_id, $field ) {
1062 if ( $field_id && ! empty( $field['field_name'] ) ) {
1063 $current = $this->get_client_fields_by( 'field_id', $field_id );
1064 if ( $current && $this->wpdb->update( $this->table_name( 'wp_clients_fields' ), $field, array( 'field_id' => $field_id ) ) ) {
1065 return $this->get_client_fields_by( 'field_id', $current->field_id );
1066 }
1067 }
1068 return false;
1069 }
1070
1071 /**
1072 * Method update_client_field_value.
1073 *
1074 * Update client field value.
1075 *
1076 * @param int $field_id field id.
1077 * @param mixed $value field value.
1078 * @param int $client_id client id.
1079 *
1080 * @return mixed|false field data.
1081 */
1082 public function update_client_field_value( $field_id, $value, $client_id ) {
1083 if ( ! empty( $field_id ) ) {
1084 $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp_clients_field_values' ) . ' WHERE field_id = %d AND value_client_id=%d ', $field_id, $client_id ) );
1085 if ( $row ) {
1086 if ( $row->field_value != $value ) { //phpcs:ignore -- to valid.
1087 $this->wpdb->update( $this->table_name( 'wp_clients_field_values' ), array( 'field_value' => $value ), array( 'value_id' => $row->value_id ) );
1088 }
1089 return true;
1090 } else {
1091 return $this->wpdb->insert(
1092 $this->table_name( 'wp_clients_field_values' ),
1093 array(
1094 'field_id' => $field_id,
1095 'field_value' => $value,
1096 'value_client_id' => $client_id,
1097 )
1098 );
1099 }
1100 }
1101 return false;
1102 }
1103
1104 /**
1105 * Method get_client_fields_by.
1106 *
1107 * Get client field.
1108 *
1109 * @param string $by by.
1110 * @param mixed $value field value.
1111 * @param int $client_id client id.
1112 *
1113 * @return mixed|null $field field data.
1114 */
1115 public function get_client_fields_by( $by = 'field_id', $value = null, $client_id = 0 ) {
1116
1117 if ( empty( $by ) || empty( $value ) ) {
1118 return null;
1119 }
1120
1121 $sql = '';
1122
1123 if ( 'client_id' === $by ) {
1124 $sql = $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp_clients_fields' ) . ' WHERE `client_id`=%d ', $value );
1125 return $this->wpdb->get_results( $sql );
1126 }
1127
1128 if ( 'field_name' === $by ) {
1129 $value = str_replace( array( '[', ']' ), '', $value );
1130 }
1131
1132 if ( 'field_id' === $by ) {
1133 $sql = $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp_clients_fields' ) . ' WHERE `field_id`=%d', $value );
1134 } elseif ( 'field_name' === $by ) {
1135 $sql = $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp_clients_fields' ) . ' WHERE `field_name` = %s AND `client_id`=%d ', $value, $client_id );
1136 }
1137
1138 $field = null;
1139
1140 if ( ! empty( $sql ) ) {
1141 $field = $this->wpdb->get_row( $sql );
1142 }
1143
1144 return $field;
1145 }
1146
1147 /**
1148 * Method get_client_fields()
1149 *
1150 * Get client fields.
1151 *
1152 * @param bool $general Get general fields OR not.
1153 * @param int $client_id Client id.
1154 * @param bool $field_name_index index result with field name.
1155 *
1156 * @return mixed results.
1157 */
1158 public function get_client_fields( $general = true, $client_id = 0, $field_name_index = false ) { // phpcs:ignore -- NOSONAR - complex.
1159 $where = '';
1160
1161 if ( $client_id ) {
1162 if ( true === $general ) {
1163 $where = ' f.client_id = ' . intval( $client_id ) . ' OR ( f.client_id = 0 AND ( v.value_client_id IS NULL OR v.value_client_id = ' . intval( $client_id ) . ') ) ';
1164 } else {
1165 $where = ' f.client_id = ' . intval( $client_id );
1166 }
1167 } elseif ( true === $general ) {
1168 $where = ' f.client_id = 0';
1169 }
1170
1171 if ( '' === $where ) {
1172 return false;
1173 }
1174
1175 $gene_results = array();
1176
1177 if ( $general ) {
1178 // to fix: missing general fields for client without client fields values.
1179 $gene_sql = 'SELECT f.* FROM ' . $this->table_name( 'wp_clients_fields' ) . ' f WHERE f.client_id = 0 ORDER BY f.field_name ';
1180 $gene_results = $this->wpdb->get_results( $gene_sql );
1181 }
1182
1183 if ( $client_id ) { // get fields values of client.
1184 $sql = 'SELECT f.*, v.field_value as field_value FROM ' . $this->table_name( 'wp_clients_fields' ) . ' f LEFT JOIN ' .
1185 $this->table_name( 'wp_clients_field_values' ) . ' v ON f.field_id = v.field_id WHERE ' . $where . ' ORDER BY f.field_name ';
1186 } else {
1187 $sql = 'SELECT f.* FROM ' . $this->table_name( 'wp_clients_fields' ) . ' f WHERE ' . $where . ' ORDER BY f.field_name '; // value_client_id to compatible result.
1188 }
1189
1190 $results = $this->wpdb->get_results( $sql );
1191
1192 $fields_list = array();
1193 if ( $field_name_index ) {
1194 if ( $results ) {
1195 foreach ( $results as $item ) {
1196 $fields_list[ $item->field_name ] = $item;
1197 }
1198 }
1199 if ( $gene_results ) {
1200 foreach ( $gene_results as $gene_item ) {
1201 if ( ! isset( $fields_list[ $gene_item->field_name ] ) ) {
1202 $gene_item->field_value = '';
1203 $fields_list[ $gene_item->field_name ] = $gene_item;
1204 }
1205 }
1206 }
1207 } else {
1208 $check_list = array();
1209 if ( $results ) {
1210 foreach ( $results as $item ) {
1211 $check_list[ $item->field_name ] = true;
1212 $fields_list[] = $item;
1213 }
1214 }
1215 if ( $gene_results ) {
1216 foreach ( $gene_results as $gene_item ) {
1217 if ( ! isset( $check_list[ $gene_item->field_name ] ) ) {
1218 $check_list[ $gene_item->field_name ] = true;
1219 $gene_item->field_value = '';
1220 $fields_list[] = $gene_item;
1221 }
1222 }
1223 }
1224 }
1225 return $fields_list;
1226 }
1227
1228 /**
1229 * Suspend - Unsuspended websites.
1230 *
1231 * @param int $client_id Client id.
1232 * @param int $suspend_val Suspend value: 0 or 1.
1233 *
1234 * @return int|boolean The result.
1235 */
1236 public function suspend_unsuspend_websites_by_client_id( $client_id, $suspend_val ) {
1237 if ( empty( $client_id ) ) {
1238 return false;
1239 }
1240 return $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET suspended=%d WHERE client_id=%d ', $suspend_val, $client_id ) );
1241 }
1242
1243 /**
1244 * Method delete_client_field_by.
1245 *
1246 * Delete client field by.
1247 *
1248 * @param string $by by.
1249 * @param mixed $value field value.
1250 * @param int $client_id client id.
1251 *
1252 * @return bool Deleted or not.
1253 */
1254 public function delete_client_field_by( $by = 'field_id', $value = false, $client_id = false ) {
1255
1256 if ( 'field_id' === $by && $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_clients_fields' ) . ' WHERE field_id=%d AND client_id=%d', $value, $client_id ) ) ) { // delete individual or general client field.
1257 if ( $client_id > 0 ) { // individual field value.
1258 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_clients_field_values' ) . ' WHERE field_id=%d AND value_client_id=%d', $value, $client_id ) ); // delete individual tokens values, for one client.
1259 } else {
1260 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_clients_field_values' ) . ' WHERE field_id=%d', $value ) ); // delete general tokens values, multi clients (value_client_id).
1261 }
1262 return true;
1263 }
1264
1265 return false;
1266 }
1267
1268 /**
1269 * Method pro_reports_get_tokens()
1270 *
1271 * Get pro reports get tokens data.
1272 *
1273 * @return mixed results.
1274 */
1275 public function pro_reports_get_tokens() {
1276 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'pro_reports_token' ) . ' WHERE 1 = 1 ORDER BY type DESC, token_name ASC' );
1277 }
1278
1279 /**
1280 * Method pro_reports_get_site_token_values()
1281 *
1282 * Get pro reports get tokens values data.
1283 *
1284 * @return mixed results.
1285 */
1286 public function pro_reports_get_site_token_values() {
1287 return $this->wpdb->get_results( ' SELECT * FROM ' . $this->table_name( 'pro_reports_site_token' ) . ' WHERE 1 ' );
1288 }
1289
1290 /**
1291 * Method client_reports_get_tokens()
1292 *
1293 * Get client reports get tokens data.
1294 *
1295 * @return mixed results.
1296 */
1297 public function client_reports_get_tokens() {
1298 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'client_report_token' ) . ' WHERE 1 = 1 ORDER BY token_name ASC' );
1299 }
1300
1301 /**
1302 * Method client_reports_get_site_token_values()
1303 *
1304 * Get client reports get tokens values data.
1305 *
1306 * @return mixed results.
1307 */
1308 public function client_reports_get_site_token_values() {
1309 return $this->wpdb->get_results( ' SELECT * FROM ' . $this->table_name( 'client_report_site_token' ) . ' WHERE 1 ' );
1310 }
1311 }
1312