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

102 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 return $this->cache[ $key ];
38 }
39
40 /**
41 * Summary of save
42 * @param mixed $key
43 * @param mixed $value
44 * @return bool
45 */
46 public function save( $key, $value ) {
47 $_updated = update_option( $key, $value, 'no' );
48
49 if( $_updated ) {
50 $this->cache[ $key ] = $value;
51 }
52
53 return $_updated;
54 }
55
56 /**
57 * Summary of get_cache
58 * @param string|int $key
59 * @param bool $force
60 * @param string $group
61 * @return bool|mixed
62 */
63 public function get_cache( $key, $force = false, $group = 'betterdocs' ) {
64 return wp_cache_get( $key, $group, $force );
65 }
66
67 /**
68 * Set cache
69 *
70 * @param string $key
71 * @param mixed $value
72 * @param int $expire
73 * @param string $group
74 * @return bool
75 */
76 public function set_cache( $key, $value, $expire = 2, $group = 'betterdocs' ) {
77 $expire = $expire * DAY_IN_SECONDS;
78 return wp_cache_set( $key, $value, $group, $expire );
79 }
80
81 public function flush_cache( $group = 'betterdocs' ){
82 if( function_exists('wp_cache_flush_group') ) {
83 wp_cache_flush_group( $group );
84 } else {
85 // @todo need to do by cache_key
86 }
87 }
88
89 public function set_transient( $transient, $value, $expiration = null ) {
90 $expiration = $expiration == null ? MINUTE_IN_SECONDS : $expiration;
91 return set_transient( $transient, $value, $expiration );
92 }
93
94 public function get_transient( $transient ) {
95 return get_transient( $transient );
96 }
97
98 public function delete_transient( $transient ) {
99 return delete_transient( $transient );
100 }
101 }
102