Completed
Push — try/scrutinizer-wp-coding-stan... ( dcaef3...dfddad )
by
unknown
02:30
created

Term_Meta_Container::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\Datastore\Meta_Datastore;
6
use Carbon_Fields\Datastore\Term_Meta_Datastore;
7
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
8
9
/**
10
 * Term meta container class.
11
 */
12
class Term_Meta_Container extends Container {
13
	protected $term_id;
14
15
	public $settings = array(
16
		'taxonomy' => array( 'category' ),
17
		'show_on_level' => false,
18
	);
19
20
	/**
21
	 * Create a new term meta fields container
22
	 *
23
	 * @param string $title Unique title of the container
24
	 **/
25
	public function __construct( $title ) {
26
		parent::__construct( $title );
27
28
		if ( ! $this->get_datastore() ) {
29
			$this->set_datastore( new Term_Meta_Datastore() );
30
		}
31
	}
32
33
	/**
34
	 * Bind attach() and save() to the appropriate WordPress actions.
35
	 **/
36
	public function init() {
37
		// force taxonomy to be array
38
		if ( ! is_array( $this->settings['taxonomy'] ) ) {
39
			$this->settings['taxonomy'] = array( $this->settings['taxonomy'] );
40
		}
41
42
		add_action( 'admin_init', array( $this, '_attach' ) );
43
44 View Code Duplication
		foreach ( $this->settings['taxonomy'] as $taxonomy ) {
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...
45
			add_action( 'edited_' . $taxonomy, array( $this, '_save' ), 10, 2 );
46
			add_action( 'created_' . $taxonomy, array( $this, '_save' ), 10, 2 );
47
		}
48
	}
49
50
	/**
51
	 * Perform save operation after successful is_valid_save() check.
52
	 * The call is propagated to all fields in the container.
53
	 *
54
	 * @param int $term_id ID of the term against which save() is ran
55
	 **/
56
	public function save( $term_id ) {
57
		$this->set_term_id( $term_id );
58
59
		foreach ( $this->fields as $field ) {
60
			$field->set_value_from_input();
61
			$field->save();
62
		}
63
64
		do_action( 'carbon_after_save_term_meta', $term_id );
65
	}
66
67
	/**
68
	 * Perform checks whether the container should be attached during the current request
69
	 *
70
	 * @return bool True if the container is allowed to be attached
71
	 **/
72
	public function is_valid_attach() {
73
		if ( isset( $_GET['taxonomy'] ) && in_array( $_GET['taxonomy'], $this->settings['taxonomy'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
74
			return true;
75
		}
76
77
		return false;
78
	}
79
80
	/**
81
	 * Perform checks whether the current save() request is valid.
82
	 *
83
	 * @param int $term_id ID of the term against which save() is ran
84
	 * @return bool
85
	 **/
86
	public function is_valid_save( $term_id = null ) {
87 View Code Duplication
		if ( ! isset( $_REQUEST[ $this->get_nonce_name() ] ) || ! wp_verify_nonce( $_REQUEST[ $this->get_nonce_name() ], $this->get_nonce_name() ) ) { // Input var okay.
1 ignored issue
show
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
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...
88
			return false;
89
		} else if ( $term_id < 1 ) {
90
			return false;
91
		}
92
93
		return true;
94
	}
95
96
	/**
97
	 * Add term meta for each of the container taxonomies
98
	 **/
99
	public function attach() {
100
		foreach ( $this->settings['taxonomy'] as $taxonomy ) {
101
			add_action( $taxonomy . '_edit_form_fields', array( $this, 'render' ), 10, 2 );
102
			add_action( $taxonomy . '_add_form_fields', array( $this, 'render' ), 10, 2 );
103
		}
104
	}
105
106
	/**
107
	 * Revert the result of attach()
108
	 *
109
	 **/
110
	public function detach() {
111
		parent::detach();
112
113
		remove_action( 'admin_init', array( $this, '_attach' ) );
114
115 View Code Duplication
		foreach ( $this->settings['taxonomy'] as $taxonomy ) {
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...
116
			remove_action( 'edited_' . $taxonomy, array( $this, '_save' ), 10 );
117
			remove_action( 'created_' . $taxonomy, array( $this, '_save' ), 10 );
118
		}
119
120
		// unregister field names
121
		foreach ( $this->fields as $field ) {
122
			$this->drop_unique_field_name( $field->get_name() );
123
		}
124
	}
125
126
	/**
127
	 * Output the container markup
128
	 **/
129
	public function render( $term = null ) {
130
		if ( is_object( $term ) ) {
131
			$this->set_term_id( $term->term_id );
132
		}
133
134
		include \Carbon_Fields\DIR . '/templates/Container/term_meta.php';
135
	}
136
137
	/**
138
	 * Set the term ID the container will operate with.
139
	 *
140
	 * @param int $term_id
141
	 **/
142
	public function set_term_id( $term_id ) {
143
		$this->term_id = $term_id;
144
		$this->store->set_id( $term_id );
145
	}
146
147
	/**
148
	 * Show the container only on terms from the specified taxonomies.
149
	 *
150
	 * @param string|array $taxonomies
151
	 * @return object $this
152
	 **/
153
	public function show_on_taxonomy( $taxonomies ) {
154
		$taxonomies = (array) $taxonomies;
155
156
		$this->settings['taxonomy'] = $taxonomies;
157
158
		return $this;
159
	}
160
161
	/**
162
	 * Show the container only on particular term level.
163
	 *
164
	 * @param int $term_level
165
	 * @return object $this
166
	 */
167
	public function show_on_level( $term_level ) {
168
		$this->settings['show_on_level'] = $term_level;
169
		return $this;
170
	}
171
}
172