Completed
Push — milestone/2_0/react-ui ( ba7ccf...2bd855 )
by
unknown
04:51
created

Term_Meta_Container::show_on_taxonomy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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