MslsPostTag::add_input()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsPostTag
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Post Tag
12
 * @package Msls
13
 */
14
class MslsPostTag extends MslsMain {
15
16
	/**
17
	 * Suggest
18
	 *
19
	 * Echo a JSON-ified array of posts of the given post-type and
20
	 * the requested search-term and then die silently
21
	 */
22
	public static function suggest() {
23
		$json = new MslsJson();
24
25
		if ( filter_has_var( INPUT_POST, 'blog_id' ) ) {
26
			switch_to_blog(
27
				filter_input( INPUT_POST, 'blog_id', FILTER_SANITIZE_NUMBER_INT )
28
			);
29
30
			$args = array(
31
				'orderby'    => 'name',
32
				'order'      => 'ASC',
33
				'number'     => 10,
34
				'hide_empty' => 0,
35
			);
36
37
			if ( filter_has_var( INPUT_POST, 's' ) ) {
38
				$args['search'] = sanitize_text_field(
39
					filter_input( INPUT_POST, 's' )
40
				);
41
			}
42
43
			/**
44
			 * Overrides the query-args for the suggest fields
45
			 * @since 0.9.9
46
			 * @param array $args
47
			 */
48
			$args = (array) apply_filters( 'msls_post_tag_suggest_args', $args );
49
50
			foreach ( get_terms( sanitize_text_field( filter_input( INPUT_POST, 'post_type' ) ), $args ) as $term ) {
51
52
				/**
53
				 * Manipulates the term object before using it
54
				 * @since 0.9.9
55
				 * @param \StdClass $term
56
				 */
57
				$term = apply_filters( 'msls_post_tag_suggest_term', $term );
58
59
				if ( is_object( $term ) ) {
60
					$json->add( $term->term_id, $term->name );
61
				}
62
			}
63
			restore_current_blog();
64
		}
65
66
		wp_die( $json->encode() );
67
	}
68
69
	/**
70
	 * Init
71
	 *
72
	 * @codeCoverageIgnore
73
	 *
74
	 * @return MslsPostTag
75
	 */
76
	public static function init() {
77
		$options    = MslsOptions::instance();
78
		$collection = MslsBlogCollection::instance();
79
80
		if ( $options->activate_autocomplete	) {
81
			$obj = new static( $options, $collection );
82
		}
83
		else {
84
			$obj = new MslsPostTagClassic( $options, $collection );
85
		}
86
87
		$taxonomy = MslsContentTypes::create()->acl_request();
88
		if ( '' != $taxonomy ) {
89
			add_action( "{$taxonomy}_add_form_fields",  [ $obj, 'add_input' ] );
90
			add_action( "{$taxonomy}_edit_form_fields", [ $obj, 'edit_input' ] );
91
			add_action( "edited_{$taxonomy}", [ $obj, 'set' ] );
92
			add_action( "create_{$taxonomy}", [ $obj, 'set' ] );
93
		}
94
95
		return $obj;
96
	}
97
98
	/**
99
	 * Add the input fields to the add-screen of the taxonomies
100
	 *
101
	 * @param \StdClass $tag
102
	 */
103
	public function add_input( $tag ) {
104
		$title_format = '<h3>%s</h3>
105
			<input type="hidden" name="msls_post_type" id="msls_post_type" value="%s"/>
106
			<input type="hidden" name="msls_action" id="msls_action" type="text" value="suggest_terms"/>';
107
108
		$item_format = '<label for="msls_title_%1$s">%2$s</label>
109
			<input type="hidden" id="msls_id_%1$s" name="msls_input_%3$s" value="%4$s"/>
110
			<input class="msls_title" id="msls_title_%1$s" name="msls_title_%1$s" type="text" value="%5$s"/>';
111
112
		echo '<div class="form-field">';
113
		$this->the_input( $tag, $title_format, $item_format );
114
		echo '</div>';
115
	}
116
117
	/**
118
	 * Add the input fields to the edit-screen of the taxonomies
119
	 * @param \StdClass $tag
120
	 */
121
	public function edit_input( $tag ) {
122
		$title_format = '<tr>
123
			<th colspan="2">
124
			<strong>%s</strong>
125
			<input type="hidden" name="msls_post_type" id="msls_post_type" value="%s"/>
126
			<input type="hidden" name="msls_action" id="msls_action" type="text" value="suggest_terms"/>
127
			</th>
128
			</tr>';
129
130
		$item_format = '<tr class="form-field">
131
			<th scope="row" valign="top">
132
			<label for="msls_title_%1$s">%2$s</label>
133
			</th>
134
			<td>
135
			<input type="hidden" id="msls_id_%1$s" name="msls_input_%3$s" value="%4$s"/>
136
			<input class="msls_title" id="msls_title_%1$s" name="msls_title_%1$s" type="text" value="%5$s"/>
137
			</td>
138
			</tr>';
139
140
		$this->the_input( $tag, $title_format, $item_format );
141
	}
142
143
	/**
144
	 * Print the input fields
145
	 * Returns true if the blogcollection is not empty
146
	 * @param \StdClass $tag
147
	 * @param string $title_format
148
	 * @param string $item_format
149
	 * @return boolean
150
	 */
151
	public function the_input( $tag, $title_format, $item_format ) {
152
		$term_id = ( is_object( $tag ) ? $tag->term_id : 0 );
153
		$blogs   = $this->collection->get();
154
		if ( $blogs ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $blogs of type lloc\Msls\MslsBlog[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
155
			$my_data = MslsOptionsTax::create( $term_id );
156
157
			$this->maybe_set_linked_term( $my_data );
158
159
			$type    = MslsContentTypes::create()->get_request();
160
161
			printf(
162
				$title_format,
163
				apply_filters( 
164
					'msls_term_select_title',
165
					__( 'Multisite Language Switcher', 'multisite-language-switcher' )
166
				),
167
				$type
168
			);
169
			foreach ( $blogs as $blog ) {
170
				switch_to_blog( $blog->userblog_id );
171
172
				$language = $blog->get_language();
173
				$icon     = MslsAdminIcon::create()
174
					->set_language( $language )
175
					->set_icon_type( 'flag' );
176
177
				$value = $title = '';
178
				if ( $my_data->has_value( $language ) ) {
179
					$term = get_term( $my_data->$language, $type );
180
					if ( is_object( $term ) ) {
181
						$icon->set_href( $my_data->$language );
182
						$value = $my_data->$language;
183
						$title = $term->name;
184
					}
185
				}
186
187
				printf(
188
					$item_format,
189
					$blog->userblog_id,
190
					$icon,
191
					$language,
192
					$value,
193
					$title
194
				);
195
				restore_current_blog();
196
			}
197
			return true;
198
		}
199
		return false;
200
	}
201
202
	/**
203
	 * Set calls the save method if taxonomy is set
204
	 * @param int $term_id
205
 	 * @codeCoverageIgnore
206
	 */
207
	public function set( $term_id ) {
208
		if ( MslsContentTypes::create()->acl_request() ) {
209
			$this->save( $term_id, MslsOptionsTax::class );
210
		}
211
	}
212
213
	/**
214
	 * Sets the selected element in the data from the `$_GET` superglobal, if any.
215
	 *
216
	 * @param MslsOptionsTax $mydata
217
	 *
218
	 * @return MslsOptionsTax
219
	 */
220
	public function maybe_set_linked_term( MslsOptionsTax $mydata ) {
221
		if ( ! isset( $_GET['msls_id'], $_GET['msls_lang'] ) ) {
222
			return $mydata;
223
		}
224
225
		$origin_lang = trim( $_GET['msls_lang'] );
226
227
		if ( isset( $mydata->{$origin_lang} ) ) {
228
			return $mydata;
229
		}
230
231
		$origin_term_id = (int) $_GET['msls_id'];
232
233
		$origin_blog_id = $this->collection->get_blog_id( $origin_lang );
234
235
		if ( null === $origin_blog_id ) {
236
			return $mydata;
237
		}
238
239
		switch_to_blog( $origin_blog_id );
240
		$origin_term = get_term( $origin_term_id, $mydata->base );
0 ignored issues
show
Documentation introduced by
The property base does not exist on object<lloc\Msls\MslsOptionsTax>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
241
		restore_current_blog();
242
243
		if ( ! $origin_term instanceof \WP_Term ) {
0 ignored issues
show
Bug introduced by
The class WP_Term does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
244
			return $mydata;
245
		}
246
247
		$mydata->{$origin_lang} = $origin_term_id;
248
249
		return $mydata;
250
	}
251
252
}
253