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

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