Completed
Push — development ( 381f0e...33ac70 )
by
unknown
05:34
created

Nav_Menu_Datastore::load()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 4
Ratio 17.39 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 3
nop 1
dl 4
loc 23
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Datastore;
4
5
use Carbon_Fields\Field\Field;
6
7
class Nav_Menu_Datastore extends Post_Meta_Datastore {
8
	
9
	public function get_garbage_prefix() {
10
		if ( !$this->get_id() ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
11
			return '';
12
		}
13
		return '_menu-item-' . $this->get_id() . '_';
14
	}
15
16
	public function get_clean_field_name( $field ) {
17
		$name = ( is_object( $field ) && is_subclass_of( $field, 'Carbon_Fields\\Field\\Field' ) ) ? $field->get_name() : $field;
18
		return substr( $name, strlen( $this->get_garbage_prefix() ) );
19
	}
20
21
	public function get_dirty_field_name( $field ) {
22
		$name = ( is_object( $field ) && is_subclass_of( $field, 'Carbon_Fields\\Field\\Field' ) ) ? $field->get_name() : $field;
23
		return $this->get_garbage_prefix() . $name;
24
	}
25
26
	/**
27
	 * Save the field value(s) into the database.
28
	 *
29
	 * @param Field $field The field to save.
30
	 */
31
	public function save( Field $field ) {
32 View Code Duplication
		if ( ! update_metadata( $this->get_meta_type(), $this->get_id(), $this->get_clean_field_name( $field ), $field->get_value() ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
			add_metadata( $this->get_meta_type(), $this->get_id(), $this->get_clean_field_name( $field ), $field->get_value(), true );
34
		}
35
	}
36
37
	/**
38
	 * Load the field value(s) from the database.
39
	 *
40
	 * @param Field $field The field to retrieve value for.
41
	 */
42
	public function load( Field $field ) {
43
		global $wpdb;
44
45
		if ( ! $this->get_id() ) {
46
			return;
47
		}
48
49
		$query = $wpdb->prepare( '
50
			SELECT `meta_value`
51
			FROM ' . $this->get_table_name() . '
52
			WHERE `' . $this->get_table_field_name() . '`= %d
53
			AND `meta_key`= %s
54
			LIMIT 1
55
		', intval( $this->get_id() ), $this->get_clean_field_name( $field ) );
56
		$value = $wpdb->get_col( $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...
57
58 View Code Duplication
		if ( ! is_array( $value ) || count( $value ) < 1 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
			$field->set_value( false );
60
			return;
61
		}
62
63
		$field->set_value( $value[0] );
64
	}
65
66
	/**
67
	 * Delete the field value(s) from the database.
68
	 *
69
	 * @param Field $field The field to delete.
70
	 */
71
	public function delete( Field $field ) {
72
		delete_metadata( $this->get_meta_type(), $this->get_id(), $this->get_clean_field_name( $field ), $field->get_value() );
73
	}
74
75
	/**
76
	 * Load complex field value(s) from the database.
77
	 *
78
	 * @param mixed $field The field to load values for.
79
	 */
80
	public function load_values( $field ) {
81
		global $wpdb;
82
83
		if ( ! $this->get_id() ) {
84
			return;
85
		}
86
87
		$meta_key = $this->get_clean_field_name( $field );
88
89
		$results = $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...
90
			SELECT meta_key AS field_key, meta_value AS field_value FROM ' . $this->get_table_name() . '
91
			WHERE `meta_key` LIKE "' . addslashes( $meta_key ) . '_%" AND `' . $this->get_table_field_name() . '`="' . intval( $this->get_id() ) . '"
92
		', ARRAY_A );
93
94
		foreach ( $results as $index => $result ) {
95
			$result['field_key'] = $this->get_dirty_field_name( $result['field_key'] );
96
			$results[$index] = $result;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
97
		}
98
99
		return $results;
100
	}
101
102
	/**
103
	 * Delete complex field value(s) from the database.
104
	 *
105
	 * @param mixed $field The field to delete values for.
106
	 */
107 View Code Duplication
	public function delete_values( $field ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
		global $wpdb;
109
110
		$group_names = $field->get_group_names();
111
		$field_name = $this->get_clean_field_name( $field );
112
113
		$meta_key_constraint = '`meta_key` LIKE "' . $field_name . implode( '-%" OR `meta_key` LIKE "' . $field_name, $group_names ) . '-%"';
114
115
		return $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...
116
			DELETE FROM ' . $this->get_table_name() . '
117
			WHERE (' . $meta_key_constraint . ') AND `' . $this->get_table_field_name() . '`="' . intval( $this->get_id() ) . '"
118
		' );
119
	}
120
}
121