PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Utils / Database.php

Database.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.0, at includes/Utils/Database.php

139 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Utils;
4
5 use WP_Error;
6
7 class Database {
8 /**
9 * Cache DB Data
10 * @var array
11 */
12 private $cache = [];
13
14 /**
15 * Retrieves theme modification value for the active theme.
16 *
17 * @since 2.5.0
18 *
19 * @param string $key
20 * @param mixed $default
21 *
22 * @return mixed
23 */
24 public function get_theme_mod( $key, $default = false ) {
25 return get_theme_mod( $key, $default );
26 }
27
28 public function get( $key, $default = false ) {
29 if ( empty( $key ) ) {
30 return new WP_Error( 'invalid_key', __( 'Key cannot be empty.', 'betterdocs' ), [ 'status' => 404 ] );
31 }
32
33 if ( ! isset( $this->cache[ $key ] ) ) {
34 $this->cache[ $key ] = get_option( $key, $default );
35 }
36
37 // update the cache when new values are updated(for wpml specifically, other usecase might be available)
38 if ( $this->cache[ $key ] != get_option( $key, $default ) ) {
39 $this->cache[ $key ] = get_option( $key, $default );
40 }
41
42 return $this->cache[ $key ];
43 }
44
45 /**
46 * Summary of save
47 * @param mixed $key
48 * @param mixed $value
49 * @return bool
50 */
51 public function save( $key, $value ) {
52 $_updated = update_option( $key, $value, 'no' );
53
54 if ( $_updated ) {
55 $this->cache[ $key ] = $value;
56 }
57
58 return $_updated;
59 }
60
61 /**
62 * Summary of get_cache
63 * @param string|int $key
64 * @param bool $force
65 * @param string $group
66 * @return bool|mixed
67 */
68 public function get_cache( $key, $force = false, $group = 'betterdocs' ) {
69 return wp_cache_get( $key, $group, $force );
70 }
71
72 /**
73 * Set cache
74 *
75 * @param string $key
76 * @param mixed $value
77 * @param int $expire
78 * @param string $group
79 * @return bool
80 */
81 public function set_cache( $key, $value, $expire = 2, $group = 'betterdocs' ) {
82 $expire = $expire * DAY_IN_SECONDS;
83 return wp_cache_set( $key, $value, $group, $expire );
84 }
85
86 public function flush_cache( $group = 'betterdocs' ) {
87 if ( function_exists( 'wp_cache_flush_group' ) ) {
88 wp_cache_flush_group( $group );
89 } else {
90 // @todo need to do by cache_key
91 }
92 }
93
94 public function set_transient( $transient, $value, $expiration = null ) {
95 $expiration = $expiration == null ? MINUTE_IN_SECONDS : $expiration;
96 return set_transient( $transient, $value, $expiration );
97 }
98
99 public function get_transient( $transient ) {
100 return get_transient( $transient );
101 }
102
103 public function delete_transient( $transient ) {
104 return delete_transient( $transient );
105 }
106
107 /**
108 * Versioned-key incrementor. Reading the current version lets callers build
109 * cache keys like "{namespace}_v{N}_..."; bumping the version makes every
110 * key in that namespace unreachable in one update.
111 *
112 * The canonical value lives in wp_options so invalidation persists across
113 * requests even on sites with no external object cache (where wp_cache_*
114 * is per-request in-memory only). wp_cache_* is still used as a
115 * within-request memoization layer.
116 */
117 public function get_cache_version( $namespace, $group = 'betterdocs' ) {
118 $cache_key = $namespace . '_version';
119 $option_key = 'bd_cache_v_' . $namespace;
120 $v = wp_cache_get( $cache_key, $group );
121 if ( false === $v ) {
122 $v = (int) get_option( $option_key, 1 );
123 if ( $v < 1 ) {
124 $v = 1;
125 }
126 wp_cache_set( $cache_key, $v, $group, 0 );
127 }
128 return (int) $v;
129 }
130
131 public function bump_cache_version( $namespace, $group = 'betterdocs' ) {
132 $cache_key = $namespace . '_version';
133 $option_key = 'bd_cache_v_' . $namespace;
134 $new = (int) get_option( $option_key, 1 ) + 1;
135 update_option( $option_key, $new, false );
136 wp_cache_set( $cache_key, $new, $group, 0 );
137 }
138 }
139