Completed
Push — milestone/2_0/react-ui ( 6ff089...f420b0 )
by
unknown
05:40
created

Term_Meta_Container::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\Datastore\Datastore;
6
7
/**
8
 * Term meta container class.
9
 */
10
class Term_Meta_Container extends Container {
11
	
12
	protected $term_id;
13
14
	public $settings = array();
15
16
	/**
17
	 * {@inheritDoc}
18
	 */
19 View Code Duplication
	public function __construct( $unique_id, $title, $type, $condition_collection, $condition_translator ) {
20
		parent::__construct( $unique_id, $title, $type, $condition_collection, $condition_translator );
21
22
		if ( ! $this->get_datastore() ) {
23
			$this->set_datastore( Datastore::make( 'term_meta' ), $this->has_default_datastore() );
24
		}
25
	}
26
27
	/**
28
	 * Bind attach() and save() to the appropriate WordPress actions.
29
	 */
30
	public function init() {
31
		add_action( 'admin_init', array( $this, '_attach' ) );
32
33
		$taxonomies = $this->get_taxonomy_visibility();
34
35
		foreach ( $taxonomies as $taxonomy ) {
36
			add_action( 'edited_' . $taxonomy, array( $this, '_save' ), 10, 2 );
37
			add_action( 'created_' . $taxonomy, array( $this, '_save' ), 10, 2 );
38
		}
39
	}
40
41
	/**
42
	 * Checks whether the current save request is valid
43
	 *
44
	 * @return bool
45
	 */
46 View Code Duplication
	public function is_valid_save() {
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...
47
		if ( ! $this->verified_nonce_in_request() ) {
48
			return false;
49
		}
50
51
		$params = func_get_args();
52
		return $this->is_valid_attach_for_object( $params[0] );
53
	}
54
55
	/**
56
	 * Perform save operation after successful is_valid_save() check.
57
	 * The call is propagated to all fields in the container.
58
	 *
59
	 * @param int $term_id ID of the term against which save() is ran
60
	 */
61 View Code Duplication
	public function save( $term_id = null ) {
62
		$this->set_term_id( $term_id );
63
64
		foreach ( $this->fields as $field ) {
65
			$field->set_value_from_input( stripslashes_deep( $_POST ) );
66
			$field->save();
67
		}
68
69
		do_action( 'carbon_fields_term_meta_container_saved', $term_id, $this );
70
	}
71
72
	/**
73
	 * Get environment array for page request (in admin)
74
	 *
75
	 * @return array
76
	 */
77
	protected function get_environment_for_request() {
78
		$input = stripslashes_deep( $_GET );
79
		$request_term_id = isset( $input['tag_ID'] ) ? intval( $input['tag_ID'] ) : 0;
80
		$request_taxonomy = isset( $input['taxonomy'] ) ? $input['taxonomy'] : '';
81
82
		$term = get_term( $request_term_id );
83
		$term = ( $term && ! is_wp_error( $term ) ) ? $term : null;
84
		$environment = array(
85
			'term_id' => $term ? intval( $term->term_id ) : 0,
86
			'term' => $term,
87
			'taxonomy' => $term ? $term->taxonomy : $request_taxonomy,
88
		);
89
		return $environment;
90
	}
91
92
	/**
93
	 * Perform checks whether the container should be attached during the current request
94
	 *
95
	 * @return bool True if the container is allowed to be attached
96
	 */
97
	public function is_valid_attach_for_request() {
98
		global $pagenow;
99
100
		if ( $pagenow !== 'edit-tags.php' && $pagenow !== 'term.php' ) {
101
			return false;
102
		}
103
104
		return $this->static_conditions_pass();
105
	}
106
107
	/**
108
	 * Get environment array for object id
109
	 *
110
	 * @return array
111
	 */
112
	protected function get_environment_for_object( $object_id ) {
113
		$term = get_term( intval( $object_id ) );
114
		$environment = array(
115
			'term_id' => intval( $term->term_id ),
116
			'term' => $term,
117
			'taxonomy' => $term->taxonomy,
118
		);
119
		return $environment;
120
	}
121
122
	/**
123
	 * Check container attachment rules against object id
124
	 *
125
	 * @param int $object_id
126
	 * @return bool
127
	 */
128
	public function is_valid_attach_for_object( $object_id = null ) {
129
		$term = get_term( $object_id );
130
		$term = ( $term && ! is_wp_error( $term ) ) ? $term : null;
131
132
		if ( ! $term ) {
133
			return false;
134
		}
135
136
		return $this->all_conditions_pass( intval( $term->term_id ) );
137
	}
138
139
	/**
140
	 * Add term meta for each of the container taxonomies
141
	 */
142
	public function attach() {
143
		$taxonomies = $this->get_taxonomy_visibility();
144
145
		foreach ( $taxonomies as $taxonomy ) {
146
			add_action( $taxonomy . '_edit_form_fields', array( $this, 'render' ), 10, 2 );
147
			add_action( $taxonomy . '_add_form_fields', array( $this, 'render' ), 10, 2 );
148
		}
149
	}
150
151
	/**
152
	 * Output the container markup
153
	 */
154
	public function render( $term = null ) {
155
		if ( is_object( $term ) ) {
156
			$this->set_term_id( $term->term_id );
157
		}
158
159
		include \Carbon_Fields\DIR . '/templates/Container/term_meta.php';
160
	}
161
162
	/**
163
	 * Set the term ID the container will operate with.
164
	 *
165
	 * @param int $term_id
166
	 */
167 View Code Duplication
	protected function set_term_id( $term_id ) {
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...
168
		$this->term_id = $term_id;
169
		$this->get_datastore()->set_object_id( $term_id );
170
171
		foreach ( $this->fields as $field ) {
172
			$datastore = $field->get_datastore();
173
			if ( $datastore->get_object_id() === 0 ) {
174
				$datastore->set_object_id( $term_id );
175
			}
176
		}
177
	}
178
179
	/**
180
	 * Get array of taxonomies this container can appear on conditionally
181
	 * 
182
	 * @return array<string>
183
	 */
184 View Code Duplication
	public function get_taxonomy_visibility() {
185
		$all_taxonomies = get_taxonomies();
186
		$filtered_collection = $this->condition_collection->filter( array( 'term_taxonomy' ) );
187
188
		$shown_on = array();
189
		foreach ( $all_taxonomies as $taxonomy ) {
190
			$environment = array(
191
				'taxonomy' => $taxonomy,
192
			);
193
			if ( $filtered_collection->is_fulfilled( $environment ) ) {
194
				$shown_on[] = $taxonomy;
195
			}
196
		}
197
		return $shown_on;
198
	}
199
	
200
	/**
201
	 * Show the container only on terms from the specified taxonomies.
202
	 *
203
	 * @deprecated
204
	 * @param string|array $taxonomies
205
	 * @return object $this
206
	 */
207
	public function show_on_taxonomy( $taxonomies ) {
208
		$taxonomies = is_array( $taxonomies ) ? $taxonomies : array( $taxonomies );
209
		$this->where( 'term_taxonomy', 'IN', $taxonomies );
210
		return $this;
211
	}
212
213
	/**
214
	 * Show the container only on particular term level.
215
	 *
216
	 * @deprecated
217
	 * @param int $term_level
218
	 * @return object $this
219
	 */
220
	public function show_on_level( $term_level ) {
221
		$this->where( 'term_level', '=', intval( $term_level ) );
222
		return $this;
223
	}
224
}
225