|
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 ) { |
|
|
|
|
|
|
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 ); |
|
|
|
|
|
|
241
|
|
|
restore_current_blog(); |
|
242
|
|
|
|
|
243
|
|
|
if ( ! $origin_term instanceof \WP_Term ) { |
|
|
|
|
|
|
244
|
|
|
return $mydata; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
$mydata->{$origin_lang} = $origin_term_id; |
|
248
|
|
|
|
|
249
|
|
|
return $mydata; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
} |
|
253
|
|
|
|
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.