OptionPage   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 110
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getMetaData() 0 4 1
A getAllMetaData() 0 17 2
A hasMetaValue() 0 11 1
A updateMetaData() 0 4 1
A deleteMetaData() 0 4 1
A getOptionPageKey() 0 13 3
A getId() 0 4 1
A setId() 0 6 1
A stripOptionId() 0 4 1
1
<?php
2
3
namespace StoutLogic\ACF\Migrations;
4
5
class OptionPage implements HasMetaData
6
{
7
    /**
8
     * @var string
9
     */
10
    private $id;
11
12
    /**
13
     * @param string $id
14
     */
15
    public function __construct($id)
16
    {
17
        $this->setId($id);
18
    }
19
20
    public function getMetaData($key)
21
    {
22
        return get_option($this->getOptionPageKey($key));
23
    }
24
25
    public function getAllMetaData()
26
    {
27
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
28
29
        $query = $wpdb->prepare(
30
            "SELECT option_name, option_value FROM $wpdb->options WHERE option_name REGEXP '%s'",
31
            "_?{$this->getId()}_.+"
32
        );
33
        $results = $wpdb->get_results($query, OBJECT);
34
35
        $options = [];
36
        foreach ($results as $o) {
37
            $options[$this->stripOptionId($o->option_name)] = $o->option_value;
38
        }
39
40
        return $options;
41
    }
42
43
    /**
44
     * @param $metaValue
45
     * @return bool
46
     */
47
    public function hasMetaValue($metaValue)
48
    {
49
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
50
51
        $query = $wpdb->prepare("SELECT option_name, option_value FROM $wpdb->options WHERE option_name REGEXP '%s' and option_value = '%s'",
52
            "_?{$this->getId()}_.+", $metaValue
53
        );
54
        $results = $wpdb->get_results($query);
55
56
        return $results !== [];
57
    }
58
59
    public function updateMetaData($fieldName, $value, $oldValue = '')
60
    {
61
        return update_option($this->getOptionPageKey($fieldName), $value);
62
    }
63
64
    public function deleteMetaData($fieldName)
65
    {
66
        return delete_option($this->getOptionPageKey($fieldName));
67
    }
68
69
    /**
70
     * @param string $key
71
     * @return string
72
     */
73
    public function getOptionPageKey($key)
74
    {
75
        $pre = '';
76
        if ($key[0] === '_') {
77
            $pre = '_';
78
            $key = substr($key, 1);
79
        }
80
        if ($this->getId()) {
81
            return $pre . $this->getId() . '_' . $key;
82
        }
83
84
        return $pre . $key;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getId()
91
    {
92
        return $this->id;
93
    }
94
95
    /**
96
     * @param string $id
97
     * @return $this
98
     */
99
    public function setId($id)
100
    {
101
        $this->id = $id;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param string $fieldName
108
     * @return string
109
     */
110
    private function stripOptionId($fieldName)
111
    {
112
        return str_replace($this->getId(), '', $fieldName);
113
    }
114
}