1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11: function xu_add_option( $name, $value ) {
12: $name = xu_get_option_name( $name );
13:
14: if ( empty( $name ) ) {
15: return false;
16: }
17:
18: if ( xu_get_option( $name ) !== false ) {
19: return false;
20: }
21:
22: $post_id = wp_insert_post( [
23: 'post_type' => 'xu_large_option',
24: 'post_name' => $name,
25: 'post_title' => $name
26: ] );
27:
28: if ( is_wp_error( $post_id ) ) {
29: return false;
30: }
31:
32: if ( ! update_post_meta( $post_id, 'option_value', $value ) ) {
33: return false;
34: }
35:
36: wp_cache_set( $name, $value, 'xu_large_option' );
37:
38: return true;
39: }
40:
41: 42: 43: 44: 45: 46: 47:
48: function xu_delete_option( $name ) {
49: $name = xu_get_option_name( $name );
50:
51: if ( empty( $name ) ) {
52: return false;
53: }
54:
55: if ( ! ( $post_id = xu_get_option_post_id( $name ) ) ) {
56: return false;
57: }
58:
59: wp_cache_delete( $name, 'xu_large_option' );
60:
61: return is_object( wp_delete_post( $post_id, true ) );
62: }
63:
64: 65: 66: 67: 68: 69: 70: 71:
72: function xu_get_option( $name, $default = false ) {
73: $name = xu_get_option_name( $name );
74:
75: if ( empty( $name ) ) {
76: return $default;
77: }
78:
79: if ( $value = wp_cache_get( $name, __FUNCTION__ ) ) {
80: return $value;
81: }
82:
83: if ( ! ( $post_id = xu_get_option_post_id( $name ) ) ) {
84: return $default;
85: }
86:
87: if ( $value = get_post_meta( $post_id, 'option_value', true ) ) {
88: return $value;
89: }
90:
91: return $default;
92: }
93:
94: 95: 96: 97: 98: 99: 100:
101: function xu_get_option_name( $name ) {
102: if ( ! is_string( $name ) ) {
103: return;
104: }
105:
106: return sanitize_title( trim( $name ) );
107: }
108:
109: 110: 111: 112: 113: 114: 115:
116: function xu_get_option_post_id( $name ) {
117: $name = xu_get_option_name( $name );
118:
119: if ( empty( $name ) ) {
120: return 0;
121: }
122:
123: $query = new WP_Query( [
124: 'fields' => 'ids',
125: 'name' => $name,
126: 'post_type' => 'xu_large_option',
127: 'posts_per_page' => 1
128: ] );
129:
130: $posts = $query->get_posts();
131:
132: if ( count( $posts ) ) {
133: return $posts[0];
134: }
135:
136: return 0;
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146:
147: function xu_update_option( $name, $value ) {
148: $name = xu_get_option_name( $name );
149:
150: if ( empty( $name ) ) {
151: return false;
152: }
153:
154: $old = xu_get_option( $name );
155:
156: if ( $old === $value ) {
157: return false;
158: }
159:
160: if ( $old === false ) {
161: return xu_add_option( $name, $value );
162: }
163:
164: if ( ! ( $post_id = xu_get_option_post_id( $name ) ) ) {
165: return $default;
166: }
167:
168: if ( ! update_post_meta( $post_id, 'option_value', $value ) ) {
169: return false;
170: }
171:
172: wp_cache_replace( $name, $value, 'xu_large_option' );
173:
174: return true;
175: }
176:
177: 178: 179:
180: function xu_register_large_option_post_type() {
181: register_post_type( 'xu_large_option', [
182: 'can_export' => true,
183: 'capability_type' => 'xu_large_option',
184: 'exclude_from_search' => true,
185: 'has_archive' => false,
186: 'public' => false,
187: 'publicly_queryable' => false,
188: 'rewrite' => false,
189: 'query_var' => false,
190: 'show_ui' => false,
191: 'show_in_admin_bar' => false,
192: 'show_in_menu' => false,
193: 'show_in_nav_menus' => false,
194: ] );
195: }
196: