PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.29
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.29
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.29, at WPDataAccess/Utilities/WPDA_Favourites.php

142 lines 2.8 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 if ( isset( $_REQUEST['wpdaschema_name'] ) && isset( $_REQUEST['table_name'] ) ) {
47 $this->schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpdaschema_name'] ) ); // input var okay.
48 $this->table_name = sanitize_text_field( wp_unslash( $_REQUEST['table_name'] ) ); // input var okay.
49 }
50 }
51
52 /**
53 * Add table to favourites
54 *
55 * @return string '0' = failed; '1' = succeeded
56 *
57 * @since 1.2.0
58 */
59 public function add() {
60 if ( ! $this->check_dictionary() ) {
61 echo '0'; // Failed.
62
63 return;
64 }
65
66 if ( null === $this->table_name ) {
67 echo '0'; // Failed.
68
69 return;
70 }
71
72 $option_value = get_option( self::FAVOURITES_OPTION_NAME );
73
74 if ( false === $option_value ) {
75 $favourites_array = array();
76 } else {
77 $favourites_array = $option_value;
78 }
79
80 if ( ! isset( $favourites_array[ $this->table_name ] ) ) {
81 $favourites_array[ $this->table_name ] = $this->table_name;
82 update_option( self::FAVOURITES_OPTION_NAME, $favourites_array );
83 echo '1';
84
85 return;
86 }
87
88 echo '0'; // Failed.
89 }
90
91 /**
92 * Remove table from favourites
93 *
94 * @return string '0' = failed; '1' = succeeded
95 *
96 * @since 1.2.0
97 */
98 public function rem() {
99 if ( ! $this->check_dictionary() ) {
100 echo '0'; // Failed.
101
102 return;
103 }
104
105 if ( null === $this->table_name ) {
106 echo '0'; // Failed.
107
108 return;
109 }
110 $option_value = get_option( self::FAVOURITES_OPTION_NAME );
111
112 if ( false !== $option_value ) {
113 $favourites_array = $option_value;
114 if ( isset( $favourites_array[ $this->table_name ] ) ) {
115 unset( $favourites_array[ $this->table_name ] );
116 update_option( self::FAVOURITES_OPTION_NAME, $favourites_array );
117 echo '1';
118
119 return;
120 }
121 }
122
123 echo '0'; // Failed.
124 }
125
126 private function check_dictionary() {
127 if ( ! WPDA::schema_exists( $this->schema_name ) ) {
128 return false;
129 }
130
131 $wpda_dictionary_exist = new WPDA_Dictionary_Exist( $this->schema_name, $this->table_name );
132 if ( ! $wpda_dictionary_exist->table_exists( $this->table_name ) ) {
133 return false;
134 }
135
136 return true;
137 }
138
139 }
140
141 }
142