Term_Meta_Container::show_on_level()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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