PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.3
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-cache.php

class-mainwp-cache.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.3, at class/class-mainwp-cache.php

145 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Search Cache Handler
4 *
5 * Handles all search content.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * Class MainWP_Cache
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_Cache { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
18
19 /**
20 * Method init_session()
21 *
22 * Start session.
23 */
24 public static function init_session() {
25 if ( PHP_SESSION_NONE === session_status() ) {
26 session_start();
27 }
28 }
29
30 /**
31 * Method init_session()
32 *
33 * Start session.
34 */
35 public static function is_session_disable() {
36 return ! function_exists( 'session_start' ) || ! function_exists( 'session_status' ) || PHP_SESSION_DISABLED === session_status();
37 }
38
39 /**
40 * Method init_cache()
41 *
42 * Set session variables.
43 *
44 * @param mixed $page Page information.
45 */
46 public static function init_cache( $page ) {
47 $_SESSION[ 'MainWP' . $page . 'Search' ] = '';
48 $_SESSION[ 'MainWP' . $page . 'SearchContext' ] = '';
49 $_SESSION[ 'MainWP' . $page . 'SearchResult' ] = '';
50 }
51
52 /**
53 * Method add_context()
54 *
55 * Set time & search context.
56 *
57 * @param mixed $page Page information.
58 * @param array $context Search context.
59 */
60 public static function add_context( $page, $context ) {
61 if ( ! is_array( $context ) ) {
62 $context = array();
63 }
64
65 $context['time'] = time();
66 $_SESSION[ 'MainWP' . $page . 'SearchContext' ] = $context;
67 }
68
69 /**
70 * Method add_body()
71 *
72 * Set search body session variable.
73 *
74 * @param string $page Page information..
75 * @param mixed $body Search body.
76 */
77 public static function add_body( $page, $body ) {
78 $_SESSION[ 'MainWP' . $page . 'Search' ] .= $body;
79 }
80
81 /**
82 * Method get_cached_context()
83 *
84 * Grab any cached searches.
85 *
86 * @param mixed $page Page information.
87 *
88 * @return array $cachedSearch Cached search array.
89 */
90 public static function get_cached_context( $page ) {
91 $cachedSearch = ( isset( $_SESSION[ 'MainWP' . $page . 'SearchContext' ] ) && is_array( $_SESSION[ 'MainWP' . $page . 'SearchContext' ] ) ? $_SESSION[ 'MainWP' . $page . 'SearchContext' ] : null ); //phpcs:ignore -- ok.
92
93 if ( null !== $cachedSearch && ( ( time() - ( 2 * 60 * 60 ) ) > $cachedSearch['time'] ) ) {
94 unset( $_SESSION[ 'MainWP' . $page . 'SearchContext' ] );
95 unset( $_SESSION[ 'MainWP' . $page . 'Search' ] );
96 unset( $_SESSION[ 'MainWP' . $page . 'SearchResult' ] );
97 $cachedSearch = null;
98 }
99
100 if ( null !== $cachedSearch && isset( $cachedSearch['status'] ) ) {
101 $cachedSearch['status'] = explode( ',', $cachedSearch['status'] );
102 }
103
104 return $cachedSearch;
105 }
106
107 /**
108 * Method echo_body()
109 *
110 * Grab & echo cached search body.
111 *
112 * @param mixed $page Page information.
113 */
114 public static function echo_body( $page ) {
115 if ( isset( $_SESSION[ 'MainWP' . $page . 'Search' ] ) ) {
116 echo $_SESSION[ 'MainWP' . $page . 'Search' ]; // phpcs:ignore WordPress.Security.EscapeOutput,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
117 }
118 }
119
120 /**
121 * Method add_result()
122 *
123 * Grab search results & store them in session.
124 *
125 * @param mixed $page Page information.
126 * @param mixed $result Search results.
127 */
128 public static function add_result( $page, $result ) {
129 $_SESSION[ 'MainWP' . $page . 'SearchResult' ] = $result;
130 }
131
132 /**
133 * Method get_cached_result()
134 *
135 * Grab cached search results.
136 *
137 * @param mixed $page Page information.
138 */
139 public static function get_cached_result( $page ) {
140 if ( isset( $_SESSION[ 'MainWP' . $page . 'SearchResult' ] ) ) {
141 return $_SESSION[ 'MainWP' . $page . 'SearchResult' ]; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.NonceVerification.Recommended
142 }
143 }
144 }
145