Completed
Branch FET/event-question-group-refac... (8c9768)
by
unknown
27:04 queued 17:53
created

JsonWpOptionManager::populateFromDb()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\options;
4
5
use stdClass;
6
7
/**
8
 * Class JsonWpOptionManager
9
 *
10
 * Takes care of loading and saving objects that implement JsonWpOptioinserializableInterface to the WP Options table.
11
 *
12
 * @package     Event Espresso
13
 * @author         Mike Nelson
14
 * @since         4.9.80.p
15
 *
16
 */
17
class JsonWpOptionManager
18
{
19
    /**
20
     * Updates the object with what's in the DB (specifically, the wp_options table). If nothing is in the DB, leaves
21
     * the object alone and returns false.
22
     * @since 4.9.80.p
23
     * @param JsonWpOptionSerializableInterface $obj
24
     * @return bool
25
     */
26
    public function populateFromDb(JsonWpOptionSerializableInterface $obj)
27
    {
28
        $option = get_option($obj->getWpOptionName());
29
        if ($option) {
30
            $json = json_decode($option);
31
            if (is_array($json) || $json instanceof stdClass) {
32
                return $obj->fromJsonSerializedData($json);
33
            }
34
        }
35
        return false;
36
    }
37
38
    /**
39
     * Saves the object's data to the wp_options table for later use.
40
     * @since 4.9.80.p
41
     * @param JsonWpOptionSerializableInterface $obj
42
     * @return bool
43
     */
44
    public function saveToDb(JsonWpOptionSerializableInterface $obj)
45
    {
46
        return update_option(
47
            $obj->getWpOptionName(),
48
            wp_json_encode($obj->toJsonSerializableData()),
49
            false
50
        );
51
    }
52
}
53
// End of file JsonWpOptionManager.php
54
// Location: EventEspresso\core\services\options/JsonWpOptionManager.php
55