Completed
Branch fix-wp-gql-for-plain-urls (809337)
by
unknown
55:28 queued 46:00
created

WordPressOption::loadOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\database;
4
5
/**
6
 * Class WordPressOption
7
 * This may seem like paving a cow path, but if you look around,
8
 * you'll see that a lot of the logic in this class is duplicated all over the place.
9
 * This class can be configured upon construction (or later)
10
 * and only requires calls to loadOption() and updateOption($value) afterwards.
11
 *
12
 * @author  Brent Christensen
13
 * @package EventEspresso\core\services\database
14
 * @since   $VID:$
15
 */
16
abstract class WordPressOption
17
{
18
    const NOT_SET_YET    = 'wordpress-option-value-not-yet-set';
19
20
    /**
21
     * WordPress makes it difficult to determine if an option was successfully saved or not
22
     * which is sometimes really important to know if the information you are saving is critical.
23
     * The following options allow us to have a better chance of knowing when an update actually failed
24
     * or when it just didn't update because the value hasn't changed, but everything is safe.
25
     */
26
    const UPDATE_SUCCESS = 1;
27
28
    const UPDATE_NONE    = 0;
29
30
    const UPDATE_ERROR   = -1;
31
32
    /**
33
     * @var boolean
34
     */
35
    private $autoload;
36
37
    /**
38
     * @var mixed
39
     */
40
    private $default_value;
41
42
    /**
43
     * @var string
44
     */
45
    private $option_name;
46
47
    /**
48
     * @var mixed
49
     */
50
    private $value = WordPressOption::NOT_SET_YET;
51
52
53
    /**
54
     * WordPressOption constructor.
55
     *
56
     * @param bool   $autoload
57
     * @param mixed  $default_value
58
     * @param string $option_name
59
     */
60
    public function __construct(string $option_name, $default_value, bool $autoload = false)
61
    {
62
        $this->setAutoload($autoload);
63
        $this->setDefaultValue($default_value);
64
        $this->setOptionName($option_name);
65
    }
66
67
68
    /**
69
     * @param bool|string $autoload
70
     */
71
    public function setAutoload($autoload): void
72
    {
73
        $this->autoload = filter_var($autoload, FILTER_VALIDATE_BOOLEAN);
74
    }
75
76
77
    /**
78
     * @param mixed $default_value
79
     */
80
    public function setDefaultValue($default_value): void
81
    {
82
        $this->default_value = $default_value;
83
    }
84
85
86
    /**
87
     * @param string $option_name
88
     */
89
    public function setOptionName(string $option_name): void
90
    {
91
        $this->option_name = sanitize_key($option_name);
92
    }
93
94
95
    /**
96
     * @return string
97
     */
98
    public function getOptionName(): string
99
    {
100
        return $this->option_name;
101
    }
102
103
104
    /**
105
     * @return false|mixed|void
106
     */
107
    public function loadOption()
108
    {
109
        if ($this->value === WordPressOption::NOT_SET_YET) {
110
            $this->value = get_option($this->option_name, $this->default_value);
111
        }
112
        return $this->value;
113
    }
114
115
116
    /**
117
     * @param $value
118
     * @return int
119
     */
120
    public function updateOption($value) : int
121
    {
122
        // don't update if value has not changed since last update
123
        if ($value === $this->value) {
124
            return WordPressOption::UPDATE_NONE;
125
        }
126
        // because the options for updating differ when adding an option for the first time
127
        // we use the WordPressOption::NOT_SET_YET to determine if things already exist in the db
128
        if (get_option($this->option_name, WordPressOption::NOT_SET_YET) === WordPressOption::NOT_SET_YET) {
129
            $updated = add_option($this->option_name, $value, '', $this->autoload());
130
        } else {
131
            $updated = update_option($this->option_name, $value);
132
        }
133
        if ($updated) {
134
            $this->value = $value;
135
            return WordPressOption::UPDATE_SUCCESS;
136
        }
137
        return WordPressOption::UPDATE_ERROR;
138
    }
139
140
141
    /**
142
     * @return string
143
     */
144
    private function autoload() : string
145
    {
146
        return $this->autoload ? 'yes' : 'no';
147
    }
148
}
149