1 | <?php |
||
11 | class MetaBox extends MslsRegistryInstance { |
||
12 | protected $data = []; |
||
13 | |||
14 | /** |
||
15 | * Renders the content import metabox. |
||
16 | */ |
||
17 | public function render() { |
||
|
|||
18 | $post = get_post(); |
||
19 | $mydata = new MslsOptionsPost( $post->ID ); |
||
20 | $languages = MslsOptionsPost::instance()->get_available_languages(); |
||
21 | $current = MslsBlogCollection::get_blog_language( get_current_blog_id() ); |
||
22 | $languages = array_diff_key( $languages, array( $current => $current ) ); |
||
23 | $input_lang = isset( $_GET['msls_lang'] ) ? $_GET['msls_lang'] : null; |
||
24 | $input_id = isset( $_GET['msls_id'] ) ? $_GET['msls_id'] : null; |
||
25 | $has_input = null !== $input_lang && null !== $input_id; |
||
26 | $blogs = MslsBlogCollection::instance(); |
||
27 | $available = array_filter( array_map( function ( $lang ) use ( $mydata ) { |
||
28 | return $mydata->{$lang}; |
||
29 | }, array_keys( $languages ) ) ); |
||
30 | $has_translation = count( $available ) >= 1; |
||
31 | |||
32 | if ( $has_input || $has_translation ) { |
||
33 | add_thickbox(); |
||
34 | $label_template = __( 'Import content from %s', 'multisite-language-switcher' ); |
||
35 | $output = '<fieldset>'; |
||
36 | $output .= '<legend>' |
||
37 | . esc_html__( 'Warning! This will override and replace all the post content with the content from the source post!', |
||
38 | 'multisite-language-switcher' ) |
||
39 | . '</legend>'; |
||
40 | foreach ( $languages as $language => $label ) { |
||
41 | $id = $mydata->{$language}; |
||
42 | $blog = $blogs->get_blog_id( $language ); |
||
43 | $label = sprintf( $label_template, $label ); |
||
44 | if ( null === $id && $has_input && $input_lang === $language ) { |
||
45 | $id = $input_id; |
||
46 | $blog = $blogs->get_blog_id( $language ); |
||
47 | } |
||
48 | if ( null !== $id ) { |
||
49 | $this->data = [ |
||
50 | 'msls_import' => "{$blog}|{$id}", |
||
51 | ]; |
||
52 | $output .= sprintf( '<a class="button-primary thickbox" href="%s" title="%s">%s</a>', |
||
53 | $this->inline_thickbox_url( $this->data ), |
||
54 | $label, |
||
55 | $label |
||
56 | ); |
||
57 | } |
||
58 | } |
||
59 | $output .= '</fieldset>'; |
||
60 | } else { |
||
61 | $output = '<p>' . |
||
62 | esc_html__( 'No translated versions linked to this post: import content functionality is disabled.', |
||
63 | 'multisite-language-switcher' ) |
||
64 | . '</p>'; |
||
65 | } |
||
66 | |||
67 | echo $output; |
||
68 | } |
||
69 | |||
70 | protected function inline_thickbox_url( array $data = [] ) { |
||
71 | $args = array_merge( [ |
||
72 | 'modal' => true, |
||
73 | 'width' => 770, // meh, just a guess on *most* devices |
||
74 | 'height' => 770, |
||
75 | 'inlineId' => 'msls-import-dialog-' . str_replace( '|', '-', $data['msls_import'] ), |
||
76 | ], $data ); |
||
77 | |||
78 | return esc_url( |
||
79 | '#TB_inline' . add_query_arg( $args, '' ) |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | public function print_modal_html() { |
||
86 | |||
87 | protected function inline_thickbox_html( $echo = true, array $data = [] ) { |
||
155 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: