1 | <?php |
||
16 | class MslsMetaBox extends MslsMain { |
||
17 | |||
18 | /** |
||
19 | * Suggest |
||
20 | * |
||
21 | * Echo a JSON-ified array of posts of the given post-type and |
||
22 | * the requested search-term and then die silently |
||
23 | */ |
||
24 | public static function suggest() { |
||
54 | |||
55 | /** |
||
56 | * @param MslsJson $json |
||
57 | * @param array $args |
||
58 | * |
||
59 | * @return mixed |
||
60 | */ |
||
61 | public static function get_suggested_fields( $json, $args ) { |
||
96 | |||
97 | /** |
||
98 | * Init |
||
99 | * |
||
100 | * @codeCoverageIgnore |
||
101 | * |
||
102 | * @return MslsMetaBox |
||
103 | */ |
||
104 | public static function init() { |
||
117 | |||
118 | /** |
||
119 | * Add |
||
120 | */ |
||
121 | public function add() { |
||
160 | |||
161 | /** |
||
162 | * Render the classic select-box |
||
163 | * @uses selected |
||
164 | */ |
||
165 | public function render_select() { |
||
166 | $blogs = $this->collection->get(); |
||
167 | if ( $blogs ) { |
||
168 | global $post; |
||
169 | |||
170 | $type = get_post_type( $post->ID ); |
||
171 | $mydata = new MslsOptionsPost( $post->ID ); |
||
172 | |||
173 | $this->maybe_set_linked_post( $mydata ); |
||
174 | |||
175 | $temp = $post; |
||
176 | |||
177 | wp_nonce_field( MslsPlugin::path(), 'msls_noncename' ); |
||
178 | |||
179 | $lis = ''; |
||
180 | |||
181 | foreach ( $blogs as $blog ) { |
||
182 | switch_to_blog( $blog->userblog_id ); |
||
183 | |||
184 | $language = $blog->get_language(); |
||
185 | $icon = MslsAdminIcon::create() |
||
186 | ->set_language( $language ) |
||
187 | ->set_icon_type( 'flag' ); |
||
188 | |||
189 | if ( $mydata->has_value( $language ) ) { |
||
190 | $icon->set_href( $mydata->$language ); |
||
191 | } |
||
192 | |||
193 | $selects = ''; |
||
194 | $p_object = get_post_type_object( $type ); |
||
195 | |||
196 | if ( $p_object->hierarchical ) { |
||
197 | $args = [ |
||
198 | 'post_type' => $type, |
||
199 | 'selected' => $mydata->$language, |
||
200 | 'name' => 'msls_input_' . $language, |
||
201 | 'show_option_none' => ' ', |
||
202 | 'option_none_value' => 0, |
||
203 | 'sort_column' => 'menu_order, post_title', |
||
204 | 'echo' => 0, |
||
205 | ]; |
||
206 | /** |
||
207 | * Overrides the args for wp_dropdown_pages when using the HTML select in the MetaBox |
||
208 | * |
||
209 | * @param array $args |
||
210 | * |
||
211 | * @since 1.0.5 |
||
212 | * |
||
213 | */ |
||
214 | $args = (array) apply_filters( 'msls_meta_box_render_select_hierarchical', $args ); |
||
215 | |||
216 | $selects .= wp_dropdown_pages( $args ); |
||
217 | } else { |
||
218 | $selects .= sprintf( |
||
219 | '<select name="msls_input_%s"><option value="0"></option>%s</select>', |
||
220 | $language, |
||
221 | $this->render_options( $type, $mydata->$language ) |
||
222 | ); |
||
223 | } |
||
224 | |||
225 | $lis .= sprintf( |
||
226 | '<li><label for="msls_input_%s">%s</label>%s</li>', |
||
227 | $language, |
||
228 | $icon, |
||
229 | $selects |
||
230 | ); |
||
231 | |||
232 | restore_current_blog(); |
||
233 | } |
||
234 | |||
235 | printf( |
||
236 | '<ul>%s</ul><input type="submit" class="button-secondary" value="%s"/>', |
||
237 | $lis, |
||
238 | __( 'Update', 'multisite-language-switcher' ) |
||
239 | ); |
||
240 | |||
241 | $post = $temp; |
||
242 | } else { |
||
243 | printf( |
||
244 | '<p>%s</p>', |
||
245 | __( 'You should define at least another blog in a different language in order to have some benefit from this plugin!', 'multisite-language-switcher' ) |
||
246 | ); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param string $type |
||
252 | * @param string $msls_id |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | public function render_options( $type, $msls_id ) { |
||
257 | $options = []; |
||
258 | |||
259 | $my_query = new \WP_Query( [ |
||
260 | 'post_type' => $type, |
||
261 | 'post_status' => get_post_stati( [ 'internal' => '' ] ), |
||
262 | 'orderby' => 'title', |
||
263 | 'order' => 'ASC', |
||
264 | 'posts_per_page' => - 1, |
||
265 | 'fields' => 'ids', |
||
266 | ] ); |
||
267 | |||
268 | if ( $my_query->have_posts() ) { |
||
269 | foreach ( $my_query->posts as $post_id ) { |
||
270 | $options[] = $this->render_option( $post_id, $msls_id ); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | return implode( PHP_EOL, $options ); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @param string $post_id |
||
279 | * @param string $msls_id |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | public function render_option( $post_id, $msls_id ) { |
||
284 | return sprintf( '<option value="%s" %s>%s</option>', $post_id, selected( $post_id, $msls_id, false ), get_the_title( $post_id ) ); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Render the suggest input-field |
||
289 | * |
||
290 | * @param bool $echo Whether the metabox markup should be echoed to the page or not. |
||
291 | */ |
||
292 | public function render_input( $echo = true ) { |
||
293 | $blogs = $this->collection->get(); |
||
294 | |||
295 | if ( $blogs ) { |
||
296 | global $post; |
||
297 | |||
298 | $post_type = get_post_type( $post->ID ); |
||
299 | $my_data = new MslsOptionsPost( $post->ID ); |
||
300 | |||
301 | $this->maybe_set_linked_post( $my_data ); |
||
302 | |||
303 | $temp = $post; |
||
304 | $items = ''; |
||
305 | |||
306 | wp_nonce_field( MslsPlugin::path(), 'msls_noncename' ); |
||
307 | |||
308 | foreach ( $blogs as $blog ) { |
||
309 | switch_to_blog( $blog->userblog_id ); |
||
310 | |||
311 | $language = $blog->get_language(); |
||
312 | $icon = MslsAdminIcon::create() |
||
313 | ->set_language( $language ) |
||
314 | ->set_icon_type( 'flag' ); |
||
315 | |||
316 | $value = $title = ''; |
||
317 | |||
318 | if ( $my_data->has_value( $language ) ) { |
||
319 | $icon->set_href( $my_data->$language ); |
||
320 | $value = $my_data->$language; |
||
321 | $title = get_the_title( $value ); |
||
322 | } |
||
323 | |||
324 | $items .= sprintf( |
||
325 | '<li> |
||
326 | <label for="msls_title_%1$s">%2$s</label> |
||
327 | <input type="hidden" id="msls_id_%1$s" name="msls_input_%3$s" value="%4$s"/> |
||
328 | <input class="msls_title" id="msls_title_%1$s" name="msls_title_%1$s" type="text" value="%5$s"/> |
||
329 | </li>', |
||
330 | $blog->userblog_id, |
||
331 | $icon, |
||
332 | $language, |
||
333 | $value, |
||
334 | $title |
||
335 | ); |
||
336 | |||
337 | restore_current_blog(); |
||
338 | } |
||
339 | |||
340 | $input_button = sprintf( |
||
341 | '<input type="submit" class="button-secondary clear" value="%s"/>', |
||
342 | __( 'Update', 'multisite-language-switcher' ) |
||
343 | ); |
||
344 | |||
345 | /** |
||
346 | * Returns the input button, return an empty string if you'ld like to hide the button |
||
347 | * |
||
348 | * @param string $input_button |
||
349 | * |
||
350 | * @since 1.0.2 |
||
351 | * |
||
352 | */ |
||
353 | $input_button = ( string ) apply_filters( 'msls_meta_box_render_input_button', $input_button ); |
||
354 | |||
355 | printf( |
||
356 | '<ul>%s</ul> |
||
357 | <input type="hidden" name="msls_post_type" id="msls_post_type" value="%s"/> |
||
358 | <input type="hidden" name="msls_action" id="msls_action" value="suggest_posts"/> |
||
359 | %s', |
||
360 | $items, |
||
361 | $post_type, |
||
362 | $input_button |
||
363 | ); |
||
364 | |||
365 | $post = $temp; |
||
366 | } else { |
||
367 | printf( |
||
368 | '<p>%s</p>', |
||
369 | __( 'You should define at least another blog in a different language in order to have some benefit from this plugin!', 'multisite-language-switcher' ) |
||
370 | ); |
||
371 | } |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Set |
||
376 | * |
||
377 | * @param int $post_id |
||
378 | */ |
||
379 | public function set( $post_id ) { |
||
396 | |||
397 | /** |
||
398 | * Sets the selected element in the data from the `$_GET` superglobal, if any. |
||
399 | * |
||
400 | * @param MslsOptionsPost $mydata |
||
401 | * |
||
402 | * @return MslsOptionsPost |
||
403 | */ |
||
404 | public function maybe_set_linked_post( MslsOptionsPost $mydata ) { |
||
435 | } |
||
436 |
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.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.