PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.2.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.2.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.2.0, at includes/Utils/Database.php

107 lines 2.4 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