PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
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 / Cookies / WPDA_Cookies.php

WPDA_Cookies.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.84, at WPDataAccess/Cookies/WPDA_Cookies.php

183 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
4 *
5 * @package WPDataAccess\Cookies
6 */
7
8 namespace WPDataAccess\Cookies {
9
10 use WPDataAccess\WPDA;
11 use WPDataAccess\List_Table\WPDA_List_Table;
12
13 /**
14 * Handle cookies in list tables. Usage is implemented in base class WPDA_List_Table.
15 */
16 class WPDA_Cookies {
17
18 const COOKIE_TIME_EXPIRATION = 365 * 24 * 3600;
19
20 /**
21 * Menu slug or null
22 *
23 * @var null
24 */
25 protected $page = null;
26
27 /**
28 * WP_Data_Access_Admin constructor
29 *
30 * @since 1.0.0
31 */
32 public function __construct() {
33 if ( isset( $_REQUEST['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
34 $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
35 }
36 }
37
38 /**
39 * Handle plugin cookies
40 *
41 * Cookies are use to remember values during navigation: (max 1 hour)
42 * 1) SCHEMA NAME
43 * The schema name is saved as a cookie when changed in the Data Explorer. As long as the user stays within the
44 * page the saved schema is used. When the user moves to another page the value is destroyed. The user gets the
45 * default value on the next visit.
46 * 2) FAVOURITE SELECTION
47 * The favourite selection is saved as cookie when changed in the Data Explorer. As long as the user stays within
48 * the page the saved selection is used. When the user moves to another page the value is destroyed. The user gets
49 * the default value on the next visit.
50 * 3) SEARCH ARGUMENT
51 * Search arguments are saved as cookies per table. As long as the user stays within the same page the saved
52 * search value is used. When the user moves to another page the value is destroyed. This allows users to navigate
53 * between pages without losing the search value.
54 *
55 * @since 1.6.0
56 */
57 public function handle_plugin_cookies() {
58 $panel_cookies = WPDA::get_option( WPDA::OPTION_PLUGIN_PANEL_COOKIES );
59
60 if ( \WP_Data_Access_Admin::PAGE_MAIN === $this->page ) {
61 // Handle Data Explorer cookies (search cookie is handled in next section).
62 // Handle cookie to remember active schema.
63 $cookie_name = \WP_Data_Access_Admin::PAGE_MAIN . '_schema_name';
64 if ( isset( $_REQUEST['wpda_main_db_schema'] ) && '' !== $_REQUEST['wpda_main_db_schema'] ) { // phpcs:ignore WordPress.Security.NonceVerification
65 $requested_db_schema = sanitize_text_field( wp_unslash( $_REQUEST['wpda_main_db_schema'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
66 $this->set_cookie( $cookie_name, $requested_db_schema, time() + self::COOKIE_TIME_EXPIRATION );
67 } else {
68 if ( 'clear' === $panel_cookies ) {
69 // Check referer: clear cookie on new page request.
70 $url = wp_parse_url( wp_get_referer() );
71 if ( isset( $url['query'] ) ) {
72 parse_str( $url['query'], $path );
73 if ( isset( $path['page'] ) ) {
74 $page = $path['page'];
75 if ( $this->page !== $page ) {
76 // New page request: reset cookie.
77 $this->set_cookie( $cookie_name, '', time() - self::COOKIE_TIME_EXPIRATION );
78 }
79 }
80 }
81 }
82 }
83
84 // Handle cookie to remember favourite selection.
85 $cookie_name = $this->page . '_favourites';
86 if ( isset( $_REQUEST['wpda_main_favourites'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
87 $favourites = sanitize_text_field( wp_unslash( $_REQUEST['wpda_main_favourites'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
88 $this->set_cookie( $cookie_name, $favourites, time() + self::COOKIE_TIME_EXPIRATION );
89 } else {
90 if ( 'clear' === $panel_cookies ) {
91 // Check referer: clear cookie on new page request.
92 $url = wp_parse_url( wp_get_referer() );
93 if ( isset( $url['query'] ) ) {
94 parse_str( $url['query'], $path );
95 if ( isset( $path['page'] ) ) {
96 $page = $path['page'];
97 if ( $this->page !== $page ) {
98 // New page request: reset cookie.
99 $this->set_cookie( $cookie_name, '', time() - self::COOKIE_TIME_EXPIRATION );
100 }
101 }
102 }
103 }
104 }
105 }
106
107 // Handle cookie for search value.
108 if ( 'wpda_wpdp_' === substr( $this->page, 0, 10 ) ) {
109 if ( isset( $_REQUEST['child_request'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
110 return; // No search values stored for child tables.
111 }
112 foreach ( $_REQUEST as $key => $val ) { // phpcs:ignore WordPress.Security.NonceVerification
113 if ( strpos( $key, 'WPDA_PARENT_KEY*' ) === 0 ) {
114 return; // No search values stored for child tables.
115 }
116 }
117 $cookie_name = $this->page;
118 } else {
119 $table_name =
120 isset( $_REQUEST['table_name'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
121 sanitize_text_field( wp_unslash( $_REQUEST['table_name'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification
122 WPDA_List_Table::LIST_BASE_TABLE;
123 $cookie_name = $this->page . '_search_' . str_replace( '.', '_', $table_name );
124 }
125
126 $search_item_name = WPDA_List_Table::SEARCH_ITEM_NAME_DEFAULT;
127 if ( isset( $_REQUEST[ $search_item_name ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
128 $search_argument = wp_filter_nohtml_kses( wp_unslash( $_REQUEST[ $search_item_name ] ) ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput
129 $this->set_cookie( $cookie_name, $search_argument, time() + self::COOKIE_TIME_EXPIRATION );
130 } else {
131 if ( 'clear' === $panel_cookies ) {
132 // Check referer: clear cookie on new page request.
133 $url = wp_parse_url( wp_get_referer() );
134 if ( isset( $url['query'] ) ) {
135 parse_str( $url['query'], $path );
136 if ( isset( $path['page'] ) ) {
137 $page = $path['page'];
138 if ( $this->page !== $page ) {
139 // New page request: reset cookie and all cookies for subpages.
140 foreach ( $_COOKIE as $key => $value ) {
141 if ( substr( $key, 0, strlen( $this->page . '_search_' ) ) === $this->page . '_search_' ) {
142 $this->set_cookie( $key, '', time() - self::COOKIE_TIME_EXPIRATION );
143 }
144 }
145 }
146 }
147 }
148 }
149 }
150 }
151
152 /**
153 * Set cookie
154 *
155 * @param string $cookie_name Cookie name.
156 * @param string $cookie_value Cookie value.
157 * @param string $cookie_expires Cookie expiration.
158 * @return void
159 */
160 protected function set_cookie( $cookie_name, $cookie_value, $cookie_expires ) {
161 if ( PHP_VERSION_ID < 70300 ) {
162 setcookie( $cookie_name, $cookie_value, $cookie_expires, '/; samesite=strict' );
163 } else {
164 setcookie(
165 $cookie_name,
166 $cookie_value,
167 array(
168 'expires' => $cookie_expires,
169 'path' => '/',
170 'samesite' => 'strict',
171 )
172 );
173 }
174
175 if ( '' === $cookie_value ) {
176 unset( $_COOKIE[ $cookie_name ] );
177 }
178 }
179
180 }
181
182 }
183