Completed
Push — milestone/2.0 ( debc3a...8685b0 )
by
unknown
03:26
created

Theme_Options_Datastore   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 18.97 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 116
rs 10
wmc 14
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 1 1
A get_storage_array() 0 18 2
A save_key_value_pair() 0 3 1
A save_key_value_pair_with_autoload() 0 9 2
C save() 22 30 7
A delete() 0 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Carbon_Fields\Datastore;
4
5
use \Carbon_Fields\Field\Field;
6
7
/**
8
 * Theme options datastore class.
9
 */
10
class Theme_Options_Datastore extends Key_Value_Datastore {
11
	/**
12
	 * Initialization tasks.
13
	 **/
14
	public function init() {}
15
16
	/**
17
	 * Get a raw database query results array for a field
18
	 *
19
	 * @param Field $field The field to retrieve value for.
20
	 * @param array $storage_key_patterns
21
	 * @return array<stdClass> Array of {key, value} objects
22
	 */
23
	protected function get_storage_array( Field $field, $storage_key_patterns ) {
24
		global $wpdb;
25
26
		$storage_key_comparisons = $this->key_toolset->storage_key_patterns_to_sql( '`option_name`', $storage_key_patterns );
27
28
		$storage_array = $wpdb->get_results( '
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
29
			SELECT `option_name` AS `key`, `option_value` AS `value`
30
			FROM ' . $wpdb->options . '
31
			WHERE ' . $storage_key_comparisons . '
32
			ORDER BY `option_name` ASC
33
		' );
34
35
		if ( empty( $storage_array ) ) {
36
			$storage_array = $this->legacy_storage_service->get_storage_array( $this, $storage_key_patterns );
37
		}
38
39
		return $storage_array;
40
	}
41
42
	/**
43
	 * Save a single key-value pair to the database
44
	 *
45
	 * @param string $key
46
	 * @param string $value
47
	 */
48
	protected function save_key_value_pair( $key, $value ) {
49
		$this->save_key_value_pair_with_autoload( $key, $value, 'no' );
50
	}
51
52
	/**
53
	 * Save a single key-value pair to the database with autoload
54
	 *
55
	 * @param string $key
56
	 * @param string $value
57
	 * @param string $autoload "yes"|"no"
58
	 */
59
	protected function save_key_value_pair_with_autoload( $key, $value, $autoload ) {
60
		$notoptions = wp_cache_get( 'notoptions', 'options' );
61
		$notoptions[ $key ] = '';
62
		wp_cache_set( 'notoptions', $notoptions, 'options' );
63
64
		if ( ! add_option( $key, $value, null, $autoload ) ) {
65
			update_option( $key, $value, $autoload );
66
		}
67
	}
68
69
	/**
70
	 * Save the field value(s)
71
	 *
72
	 * @param Field $field The field to save.
73
	 */
74
	public function save( Field $field ) {
75
		$autoload = $field->get_autoload() ? 'yes': 'no';
76
		$value_set = $field->value()->get_set();
77
		if ( $value_set === null ) {
78
			return;
79
		}
80
81 View Code Duplication
		if ( empty( $value_set ) && $field->value()->keepalive() ) {
82
			$storage_key = $this->key_toolset->get_storage_key(
83
				$field->is_simple_root_field(),
84
				$this->get_full_hierarchy_for_field( $field ),
85
				$this->get_full_hierarchy_index_for_field( $field ),
86
				0,
87
				static::KEEPALIVE_KEY
88
			);
89
			$this->save_key_value_pair_with_autoload( $storage_key, '', $autoload );
90
		}
91 View Code Duplication
		foreach ( $value_set as $value_group_index => $values ) {
92
			foreach ( $values as $value_key => $value ) {
93
				$storage_key = $this->key_toolset->get_storage_key(
94
					$field->is_simple_root_field(),
95
					$this->get_full_hierarchy_for_field( $field ),
96
					$this->get_full_hierarchy_index_for_field( $field ),
97
					$value_group_index,
98
					$value_key
99
				);
100
				$this->save_key_value_pair_with_autoload( $storage_key, $value, $autoload );
101
			}
102
		}
103
	}
104
105
	/**
106
	 * Delete the field value(s)
107
	 *
108
	 * @param Field $field The field to delete.
109
	 */
110
	public function delete( Field $field ) {
111
		global $wpdb;
112
113
		$storage_key_patterns = $this->key_toolset->get_storage_key_deleter_patterns(
114
			$field->is_simple_root_field(),
115
			$this->get_full_hierarchy_for_field( $field ),
116
			$this->get_full_hierarchy_index_for_field( $field )
117
		);
118
		$storage_key_comparisons = $this->key_toolset->storage_key_patterns_to_sql( '`option_name`', $storage_key_patterns );
119
120
		$wpdb->query( '
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
121
			DELETE FROM ' . $wpdb->options . '
122
			WHERE ' . $storage_key_comparisons . '
123
		' );
124
	}
125
}
126