-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathMetaBox.php
257 lines (236 loc) · 5.9 KB
/
MetaBox.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace lloc\Msls\ContentImport;
use lloc\Msls\ContentImport\Importers\ImportersFactory;
use lloc\Msls\ContentImport\Importers\Map;
use lloc\Msls\MslsBlogCollection;
use lloc\Msls\MslsFields;
use lloc\Msls\MslsOptionsPost;
use lloc\Msls\MslsPlugin;
use lloc\Msls\MslsRegistryInstance;
use lloc\Msls\MslsRequest;
class MetaBox extends MslsRegistryInstance {
protected $data = array();
/**
* Renders the content import metabox.
*/
public function render() {
$post = get_post();
$mydata = new MslsOptionsPost( $post->ID );
$languages = MslsOptionsPost::instance()->get_available_languages();
$current = MslsBlogCollection::get_blog_language( get_current_blog_id() );
$languages = array_diff_key( $languages, array( $current => $current ) );
$input_lang = MslsRequest::get( MslsFields::FIELD_MSLS_LANG, null );
$input_id = MslsRequest::get( MslsFields::FIELD_MSLS_ID, null );
$has_input = null !== $input_lang && null !== $input_id;
$blogs = msls_blog_collection();
$available = array_filter(
array_map(
function ( $lang ) use ( $mydata ) {
return $mydata->{$lang};
},
array_keys( $languages )
)
);
$has_translation = count( $available ) >= 1;
if ( $has_input || $has_translation ) {
add_thickbox();
/* translators: %s: language name */
$label_template = __( 'Import content from %s', 'multisite-language-switcher' );
$output = '<fieldset>';
$output .= '<legend>'
. esc_html__(
'Warning! This will override and replace all the post content with the content from the source post!',
'multisite-language-switcher'
)
. '</legend>';
foreach ( $languages as $language => $label ) {
$id = $mydata->{$language};
$blog = $blogs->get_blog_id( $language );
$label = sprintf( $label_template, $label );
if ( null === $id && $has_input && $input_lang === $language ) {
$id = $input_id;
$blog = $blogs->get_blog_id( $language );
}
if ( null !== $id ) {
$this->data = array(
'msls_import' => "{$blog}|{$id}",
);
$output .= sprintf(
'<a class="button button-primary thickbox" href="%s" title="%s">%s</a>',
$this->inline_thickbox_url( $this->data ),
$label,
$label
);
}
}
$output .= '</fieldset>';
} else {
$output = '<p>' .
esc_html__(
'No translated versions linked to this post: import content functionality is disabled.',
'multisite-language-switcher'
)
. '</p>';
}
echo $output;
}
protected function inline_thickbox_url( array $data = array() ) {
$args = array_merge(
array(
'modal' => true,
'width' => 770, // meh, just a guess on *most* devices
'height' => 770,
'inlineId' => 'msls-import-dialog-' . str_replace( '|', '-', $data['msls_import'] ),
),
$data
);
return esc_url(
'#TB_inline' . add_query_arg( $args, '' )
);
}
public function print_modal_html() {
echo $this->inline_thickbox_html( true, $this->data );
}
protected function inline_thickbox_html( $echo = true, array $data = array() ) {
if ( ! isset( $data['msls_import'] ) ) {
return '';
}
$slug = str_replace( '|', '-', $data['msls_import'] );
ob_start();
?>
<div style="display: none;" id="msls-import-dialog-
<?php
echo esc_attr( $slug )
?>
">
<h3>
<?php
esc_html_e( 'Select what should be imported and how', 'multisite-language-switcher' )
?>
</h3>
<form action="
<?php
echo add_query_arg( array() )
?>
" method="post">
<?php
wp_nonce_field( MslsPlugin::path(), 'msls_noncename' );
?>
<?php
foreach ( $data as $key => $value ) :
?>
<input type="hidden" name="
<?php
echo esc_attr( $key )
?>
" value="
<?php
echo esc_attr( $value )
?>
">
<?php
endforeach;
?>
<?php
/** @var ImportersFactory $factory */
foreach ( Map::instance()->factories() as $slug => $factory ) :
?>
<?php
$details = $factory->details()
?>
<h4>
<?php
echo esc_html( $details->name )
?>
</h4>
<?php
if ( empty( $details->importers ) ) :
?>
<p>
<?php
esc_html_e(
'No importers available for this type of content.',
'multisite-language-switcher'
)
?>
</p>
<?php
else :
?>
<ul>
<li>
<label>
<input type="radio" name="msls_importers[
<?php
echo esc_attr( $details->slug )
?>
]">
<?php
esc_html_e(
'Off - Do not import this type of content in the destination post.',
'multisite-language-switcher'
)
?>
</label>
</li>
<?php
foreach ( $details->importers as $importer_slug => $importer_info ) :
?>
<li>
<label>
<input type="radio" name="msls_importers[
<?php
echo esc_attr( $details->slug )
?>
]"
value="
<?php
echo esc_attr( $importer_slug )
?>
"
<?php
checked( $details->selected, $importer_slug )
?>
>
<?php
echo( esc_html(
sprintf(
'%s - %s',
$importer_info->name,
$importer_info->description
)
) )
?>
</label>
</li>
<?php
endforeach;
?>
</ul>
<?php
endif;
?>
<?php
endforeach;
?>
<div>
<input
type="submit"
class="button button-primary"
value="
<?php
esc_html_e( 'Import Content', 'multisite-language-switcher' )
?>
"
>
</div>
</form>
</div>
<?php
$html = ob_get_clean();
if ( $echo ) {
echo $html;
}
return $html;
}
}