PluginProbe
Auto Alt Text / trunk
Auto Alt Text vtrunk
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
auto-alt-text / src / App / Infrastructure / Repositories / WordPressConfigRepository.php

WordPressConfigRepository.php in Auto Alt Text trunk, at src/App/Infrastructure/Repositories/WordPressConfigRepository.php

83 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AATXT\App\Infrastructure\Repositories;
4
5 /**
6 * WordPress Config Repository
7 *
8 * Concrete implementation of ConfigRepositoryInterface using WordPress options API.
9 * This class wraps WordPress functions for configuration management,
10 * providing a clean abstraction layer.
11 */
12 final class WordPressConfigRepository implements ConfigRepositoryInterface
13 {
14 /**
15 * Get a configuration value
16 *
17 * Wraps WordPress get_option() function.
18 *
19 * @param string $key The configuration key
20 * @param mixed $default Default value if key doesn't exist
21 * @return mixed The configuration value or default
22 */
23 public function get(string $key, $default = null)
24 {
25 $value = get_option($key, $default);
26
27 // WordPress returns false when option doesn't exist
28 // Return the default value in this case
29 if ($value === false && $default !== null) {
30 return $default;
31 }
32
33 return $value;
34 }
35
36 /**
37 * Set a configuration value
38 *
39 * Wraps WordPress update_option() function.
40 * Creates the option if it doesn't exist, updates if it does.
41 *
42 * @param string $key The configuration key
43 * @param mixed $value The value to store
44 * @return bool True on success, false on failure
45 */
46 public function set(string $key, $value): bool
47 {
48 return update_option($key, $value);
49 }
50
51 /**
52 * Delete a configuration value
53 *
54 * Wraps WordPress delete_option() function.
55 *
56 * @param string $key The configuration key to delete
57 * @return bool True on success, false on failure
58 */
59 public function delete(string $key): bool
60 {
61 return delete_option($key);
62 }
63
64 /**
65 * Check if a configuration key exists
66 *
67 * WordPress doesn't have a direct has_option() function,
68 * so we use get_option() with a unique default value to check existence.
69 *
70 * @param string $key The configuration key to check
71 * @return bool True if key exists, false otherwise
72 */
73 public function has(string $key): bool
74 {
75 // Use a unique object as default to distinguish between
76 // "option doesn't exist" and "option exists with false value"
77 $default = new \stdClass();
78 $value = get_option($key, $default);
79
80 return $value !== $default;
81 }
82 }
83