PluginProbe
WP Data Access – App Builder for Tables, Forms, Dashboards, Charts & Maps / 5.5.87
WP Data Access – App Builder for Tables, Forms, Dashboards, Charts & Maps v5.5.87
5.5.87 5.5.85 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 All 162 releases
wp-data-access / WPDataAccess / Cookies / WPDA_Cookies.php

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

127 lines 4.0 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 * Search arguments are saved as cookies per table. As long as the user stays within the same page the saved
43 * search value is used. When the user moves to another page the value is destroyed. This allows users to navigate
44 * between pages without losing the search value.
45 *
46 * @since 1.6.0
47 */
48 public function handle_plugin_cookies() {
49 $panel_cookies = WPDA::get_option( WPDA::OPTION_PLUGIN_PANEL_COOKIES );
50
51 // Handle cookie for search value.
52 if ( 'wpda_wpdp_' === substr( $this->page, 0, 10 ) ) {
53 if ( isset( $_REQUEST['child_request'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
54 return; // No search values stored for child tables.
55 }
56 foreach ( $_REQUEST as $key => $val ) { // phpcs:ignore WordPress.Security.NonceVerification
57 if ( strpos( $key, 'WPDA_PARENT_KEY*' ) === 0 ) {
58 return; // No search values stored for child tables.
59 }
60 }
61 $cookie_name = $this->page;
62 } else {
63 $table_name =
64 isset( $_REQUEST['table_name'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
65 sanitize_text_field( wp_unslash( $_REQUEST['table_name'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification
66 WPDA_List_Table::LIST_BASE_TABLE;
67 $cookie_name = $this->page . '_search_' . str_replace( '.', '_', $table_name );
68 }
69
70 $search_item_name = WPDA_List_Table::SEARCH_ITEM_NAME_DEFAULT;
71 if ( isset( $_REQUEST[ $search_item_name ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
72 $search_argument = wp_filter_nohtml_kses( wp_unslash( $_REQUEST[ $search_item_name ] ) ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput
73 $this->set_cookie( $cookie_name, $search_argument, time() + self::COOKIE_TIME_EXPIRATION );
74 } else {
75 if ( 'clear' === $panel_cookies ) {
76 // Check referer: clear cookie on new page request.
77 $url = wp_parse_url( wp_get_referer() );
78 if ( isset( $url['query'] ) ) {
79 parse_str( $url['query'], $path );
80 if ( isset( $path['page'] ) ) {
81 $page = $path['page'];
82 if ( $this->page !== $page ) {
83 // New page request: reset cookie and all cookies for subpages.
84 foreach ( $_COOKIE as $key => $value ) {
85 if ( substr( $key, 0, strlen( $this->page . '_search_' ) ) === $this->page . '_search_' ) {
86 $this->set_cookie( $key, '', time() - self::COOKIE_TIME_EXPIRATION );
87 }
88 }
89 }
90 }
91 }
92 }
93 }
94 }
95
96 /**
97 * Set cookie
98 *
99 * @param string $cookie_name Cookie name.
100 * @param string $cookie_value Cookie value.
101 * @param string $cookie_expires Cookie expiration.
102 * @return void
103 */
104 protected function set_cookie( $cookie_name, $cookie_value, $cookie_expires ) {
105 if ( PHP_VERSION_ID < 70300 ) {
106 setcookie( $cookie_name, $cookie_value, $cookie_expires, '/; samesite=strict' );
107 } else {
108 setcookie(
109 $cookie_name,
110 $cookie_value,
111 array(
112 'expires' => $cookie_expires,
113 'path' => '/',
114 'samesite' => 'strict',
115 )
116 );
117 }
118
119 if ( '' === $cookie_value ) {
120 unset( $_COOKIE[ $cookie_name ] );
121 }
122 }
123
124 }
125
126 }
127