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['s'] = 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
|
|
|
wp_die( $json->encode() ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Init |
70
|
|
|
* |
71
|
|
|
* @codeCoverageIgnore |
72
|
|
|
* |
73
|
|
|
* @return MslsPostTag |
74
|
|
|
*/ |
75
|
|
|
public static function init() { |
76
|
|
|
$options = MslsOptions::instance(); |
77
|
|
|
$collection = MslsBlogCollection::instance(); |
78
|
|
|
|
79
|
|
|
if ( $options->activate_autocomplete ) { |
80
|
|
|
$obj = new static( $options, $collection ); |
81
|
|
|
} |
82
|
|
|
else { |
83
|
|
|
$obj = MslsPostTagClassic::init(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$taxonomy = MslsContentTypes::create()->acl_request(); |
87
|
|
|
if ( '' != $taxonomy ) { |
88
|
|
|
add_action( "{$taxonomy}_add_form_fields", [ $obj, 'add_input' ] ); |
89
|
|
|
add_action( "{$taxonomy}_edit_form_fields", [ $obj, 'edit_input' ] ); |
90
|
|
|
add_action( "edited_{$taxonomy}", [ $obj, 'set' ] ); |
91
|
|
|
add_action( "create_{$taxonomy}", [ $obj, 'set' ] ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $obj; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add the input fields to the add-screen of the taxonomies |
99
|
|
|
* |
100
|
|
|
* @param \StdClass $tag |
101
|
|
|
*/ |
102
|
|
|
public function add_input( $tag ) { |
103
|
|
|
$title_format = '<h3>%s</h3> |
104
|
|
|
<input type="hidden" name="msls_post_type" id="msls_post_type" value="%s"/> |
105
|
|
|
<input type="hidden" name="msls_action" id="msls_action" type="text" value="suggest_terms"/>'; |
106
|
|
|
|
107
|
|
|
$item_format = '<label for="msls_title_%1$s">%2$s</label> |
108
|
|
|
<input type="hidden" id="msls_id_%1$s" name="msls_input_%3$s" value="%4$s"/> |
109
|
|
|
<input class="msls_title" id="msls_title_%1$s" name="msls_title_%1$s" type="text" value="%5$s"/>'; |
110
|
|
|
|
111
|
|
|
echo '<div class="form-field">'; |
112
|
|
|
$this->the_input( $tag, $title_format, $item_format ); |
113
|
|
|
echo '</div>'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Add the input fields to the edit-screen of the taxonomies |
118
|
|
|
* @param \StdClass $tag |
119
|
|
|
*/ |
120
|
|
|
public function edit_input( $tag ) { |
121
|
|
|
$title_format = '<tr> |
122
|
|
|
<th colspan="2"> |
123
|
|
|
<strong>%s</strong> |
124
|
|
|
<input type="hidden" name="msls_post_type" id="msls_post_type" value="%s"/> |
125
|
|
|
<input type="hidden" name="msls_action" id="msls_action" type="text" value="suggest_terms"/> |
126
|
|
|
</th> |
127
|
|
|
</tr>'; |
128
|
|
|
|
129
|
|
|
$item_format = '<tr class="form-field"> |
130
|
|
|
<th scope="row" valign="top"> |
131
|
|
|
<label for="msls_title_%1$s">%2$s</label> |
132
|
|
|
</th> |
133
|
|
|
<td> |
134
|
|
|
<input type="hidden" id="msls_id_%1$s" name="msls_input_%3$s" value="%4$s"/> |
135
|
|
|
<input class="msls_title" id="msls_title_%1$s" name="msls_title_%1$s" type="text" value="%5$s"/> |
136
|
|
|
</td> |
137
|
|
|
</tr>'; |
138
|
|
|
|
139
|
|
|
$this->the_input( $tag, $title_format, $item_format ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Print the input fields |
144
|
|
|
* Returns true if the blogcollection is not empty |
145
|
|
|
* @param \StdClass $tag |
146
|
|
|
* @param string $title_format |
147
|
|
|
* @param string $item_format |
148
|
|
|
* @return boolean |
149
|
|
|
*/ |
150
|
|
|
public function the_input( $tag, $title_format, $item_format ) { |
151
|
|
|
$term_id = ( is_object( $tag ) ? $tag->term_id : 0 ); |
152
|
|
|
$blogs = $this->collection->get(); |
153
|
|
|
if ( $blogs ) { |
|
|
|
|
154
|
|
|
$my_data = MslsOptionsTax::create( $term_id ); |
155
|
|
|
|
156
|
|
|
$this->maybe_set_linked_term( $my_data ); |
157
|
|
|
|
158
|
|
|
$type = MslsContentTypes::create()->get_request(); |
159
|
|
|
|
160
|
|
|
printf( |
161
|
|
|
$title_format, |
162
|
|
|
__( 'Multisite Language Switcher', 'multisite-language-switcher' ), |
163
|
|
|
$type |
164
|
|
|
); |
165
|
|
|
foreach ( $blogs as $blog ) { |
166
|
|
|
switch_to_blog( $blog->userblog_id ); |
|
|
|
|
167
|
|
|
|
168
|
|
|
$language = $blog->get_language(); |
169
|
|
|
$flag_url = $this->options->get_flag_url( $language ); |
170
|
|
|
$icon = MslsAdminIcon::create()->set_language( $language )->set_src( $flag_url ); |
171
|
|
|
|
172
|
|
|
$value = $title = ''; |
173
|
|
|
if ( $my_data->has_value( $language ) ) { |
174
|
|
|
$term = get_term( $my_data->$language, $type ); |
175
|
|
|
if ( is_object( $term ) ) { |
176
|
|
|
$icon->set_href( $my_data->$language ); |
177
|
|
|
$value = $my_data->$language; |
178
|
|
|
$title = $term->name; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
printf( |
183
|
|
|
$item_format, |
184
|
|
|
$blog->userblog_id, |
185
|
|
|
$icon, |
186
|
|
|
$language, |
187
|
|
|
$value, |
188
|
|
|
$title |
189
|
|
|
); |
190
|
|
|
restore_current_blog(); |
191
|
|
|
} |
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set calls the save method if taxonomy is set |
199
|
|
|
* @param int $term_id |
200
|
|
|
* @codeCoverageIgnore |
201
|
|
|
*/ |
202
|
|
|
public function set( $term_id ) { |
203
|
|
|
if ( MslsContentTypes::create()->acl_request() ) { |
204
|
|
|
$this->save( $term_id, MslsOptionsTax::class ); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Sets the selected element in the data from the `$_GET` superglobal, if any. |
210
|
|
|
* |
211
|
|
|
* @param MslsOptionsTax $mydata |
212
|
|
|
* |
213
|
|
|
* @return MslsOptionsTax |
214
|
|
|
*/ |
215
|
|
|
public function maybe_set_linked_term( MslsOptionsTax $mydata ) { |
|
|
|
|
216
|
|
|
if ( ! isset( $_GET['msls_id'], $_GET['msls_lang'] ) ) { |
|
|
|
|
217
|
|
|
return $mydata; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$origin_lang = trim( $_GET['msls_lang'] ); |
|
|
|
|
221
|
|
|
|
222
|
|
|
if ( isset( $mydata->{$origin_lang} ) ) { |
223
|
|
|
return $mydata; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$origin_term_id = (int) $_GET['msls_id']; |
|
|
|
|
227
|
|
|
|
228
|
|
|
$origin_blog_id = $this->collection->get_blog_id( $origin_lang ); |
229
|
|
|
|
230
|
|
|
if ( null === $origin_blog_id ) { |
231
|
|
|
return $mydata; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
switch_to_blog( $origin_blog_id ); |
|
|
|
|
235
|
|
|
$origin_term = get_term( $origin_term_id, $mydata->base ); |
|
|
|
|
236
|
|
|
restore_current_blog(); |
237
|
|
|
|
238
|
|
|
if ( ! $origin_term instanceof \WP_Term ) { |
|
|
|
|
239
|
|
|
return $mydata; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$mydata->{$origin_lang} = $origin_term_id; |
243
|
|
|
|
244
|
|
|
return $mydata; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
} |
248
|
|
|
|