PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.83
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.83
5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 All 160 releases
wp-data-access / WPDataAccess / Utilities / WPDA_Favourites.php

WPDA_Favourites.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.83, at WPDataAccess/Utilities/WPDA_Favourites.php

144 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
5 *
6 * @package WPDataAccess\Utilities
7 */
8
9 namespace WPDataAccess\Utilities {
10
11 use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist;
12 use WPDataAccess\WPDA;
13
14 /**
15 * Class WPDA_Favourites
16 *
17 * @author Peter Schulz
18 * @since 2.0.13
19 */
20 class WPDA_Favourites {
21
22 const FAVOURITES_OPTION_NAME = 'wpda_favourites';
23
24 /**
25 * Database schema name
26 *
27 * @var string|null
28 */
29 protected $schema_name = null;
30
31 /**
32 * Database table name
33 *
34 * @var string|null
35 */
36 protected $table_name = null;
37
38 /**
39 * WPDA_Favourites constructor
40 *
41 * Checks if the given tables exists.
42 *
43 * @since 1.2.0
44 */
45 public function __construct() {
46 // phpcs:disable WordPress.Security.NonceVerification.Recommended -- already verified
47 if ( isset( $_REQUEST['wpdaschema_name'] ) && isset( $_REQUEST['table_name'] ) ) {
48 $this->schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpdaschema_name'] ) );
49 $this->table_name = sanitize_text_field( wp_unslash( $_REQUEST['table_name'] ) );
50 }
51 // phpcs:enable WordPress.Security.NonceVerification.Recommended
52 }
53
54 /**
55 * Add table to favourites
56 *
57 * @return string '0' = failed; '1' = succeeded
58 *
59 * @since 1.2.0
60 */
61 public function add() {
62 if ( ! $this->check_dictionary() ) {
63 echo '0'; // Failed.
64
65 return;
66 }
67
68 if ( null === $this->table_name ) {
69 echo '0'; // Failed.
70
71 return;
72 }
73
74 $option_value = get_option( self::FAVOURITES_OPTION_NAME );
75
76 if ( false === $option_value ) {
77 $favourites_array = array();
78 } else {
79 $favourites_array = $option_value;
80 }
81
82 if ( ! isset( $favourites_array[ $this->table_name ] ) ) {
83 $favourites_array[ $this->table_name ] = $this->table_name;
84 update_option( self::FAVOURITES_OPTION_NAME, $favourites_array );
85 echo '1';
86
87 return;
88 }
89
90 echo '0'; // Failed.
91 }
92
93 /**
94 * Remove table from favourites
95 *
96 * @return string '0' = failed; '1' = succeeded
97 *
98 * @since 1.2.0
99 */
100 public function rem() {
101 if ( ! $this->check_dictionary() ) {
102 echo '0'; // Failed.
103
104 return;
105 }
106
107 if ( null === $this->table_name ) {
108 echo '0'; // Failed.
109
110 return;
111 }
112 $option_value = get_option( self::FAVOURITES_OPTION_NAME );
113
114 if ( false !== $option_value ) {
115 $favourites_array = $option_value;
116 if ( isset( $favourites_array[ $this->table_name ] ) ) {
117 unset( $favourites_array[ $this->table_name ] );
118 update_option( self::FAVOURITES_OPTION_NAME, $favourites_array );
119 echo '1';
120
121 return;
122 }
123 }
124
125 echo '0'; // Failed.
126 }
127
128 private function check_dictionary() {
129 if ( ! WPDA::schema_exists( $this->schema_name ) ) {
130 return false;
131 }
132
133 $wpda_dictionary_exist = new WPDA_Dictionary_Exist( $this->schema_name, $this->table_name );
134 if ( ! $wpda_dictionary_exist->table_exists( $this->table_name ) ) {
135 return false;
136 }
137
138 return true;
139 }
140
141 }
142
143 }
144