1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace lloc\Msls\ContentImport; |
4
|
|
|
|
5
|
|
|
use lloc\Msls\ContentImport\Importers\ImportersFactory; |
6
|
|
|
use lloc\Msls\ContentImport\Importers\Map; |
7
|
|
|
use lloc\Msls\MslsBlogCollection; |
8
|
|
|
use lloc\Msls\MslsOptionsPost; |
9
|
|
|
use lloc\Msls\MslsRegistryInstance; |
10
|
|
|
|
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() { |
84
|
|
|
echo $this->inline_thickbox_html( true, $this->data ); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function inline_thickbox_html( $echo = true, array $data = [] ) { |
88
|
|
|
if ( ! isset( $data['msls_import'] ) ) { |
89
|
|
|
return ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$slug = str_replace( '|', '-', $data['msls_import'] ); |
93
|
|
|
|
94
|
|
|
ob_start(); |
95
|
|
|
?> |
96
|
|
|
<div style="display: none;" id="msls-import-dialog-<?php echo esc_attr( $slug ) ?>"> |
97
|
|
|
<h3><?php esc_html_e( 'Select what should be imported and how', 'multisite-language-switcher' ) ?></h3> |
98
|
|
|
|
99
|
|
|
<form action="<?php echo add_query_arg( [] ) ?>" method="post"> |
|
|
|
|
100
|
|
|
|
101
|
|
|
<?php wp_nonce_field( MSLS_PLUGIN_PATH, 'msls_noncename' ); ?> |
102
|
|
|
|
103
|
|
|
<?php foreach ( $data as $key => $value ) : ?> |
104
|
|
|
<input type="hidden" name="<?php echo esc_attr( $key ) ?>" value="<?php echo esc_attr( $value ) ?>"> |
105
|
|
|
<?php endforeach; ?> |
106
|
|
|
|
107
|
|
|
<?php /** @var ImportersFactory $factory */ |
108
|
|
|
foreach ( Map::instance()->factories() as $slug => $factory ) : ?> |
109
|
|
|
<?php $details = $factory->details() ?> |
110
|
|
|
<h4><?php echo esc_html( $details->name ) ?></h4> |
111
|
|
|
<?php if ( empty( $details->importers ) ) : ?> |
112
|
|
|
<p><?php esc_html_e( 'No importers available for this type of content.', 'multisite-language-switcher' ) ?></p> |
113
|
|
|
<?php else: ?> |
|
|
|
|
114
|
|
|
<ul> |
115
|
|
|
<li> |
116
|
|
|
<label> |
117
|
|
|
<input type="radio" name="msls_importers[<?php echo esc_attr( $details->slug ) ?>]"> |
118
|
|
|
<?php esc_html_e( 'Off - Do not import this type of content in the destination post.', 'multisite-language-switcher' ) ?> |
119
|
|
|
</label> |
120
|
|
|
</li> |
121
|
|
|
<?php foreach ( $details->importers as $importer_slug => $importer_info ) : ?> |
122
|
|
|
<li> |
123
|
|
|
<label> |
124
|
|
|
<input type="radio" name="msls_importers[<?php echo esc_attr( $details->slug ) ?>]" |
125
|
|
|
value="<?php echo esc_attr( $importer_slug ) ?>" |
126
|
|
|
<?php checked( $details->selected, $importer_slug ) ?> |
127
|
|
|
> |
128
|
|
|
<?php echo( esc_html( sprintf( '%s - %s', $importer_info->name, $importer_info->description ) ) ) ?> |
|
|
|
|
129
|
|
|
</label> |
130
|
|
|
</li> |
131
|
|
|
<?php endforeach; ?> |
132
|
|
|
</ul> |
133
|
|
|
<?php endif; ?> |
134
|
|
|
<?php endforeach; ?> |
135
|
|
|
|
136
|
|
|
<div> |
137
|
|
|
<input |
138
|
|
|
type="submit" |
139
|
|
|
class="button-primary" |
140
|
|
|
value="<?php esc_html_e( 'Import Content', 'multisite-language-switcher' ) ?>" |
141
|
|
|
> |
142
|
|
|
</div> |
143
|
|
|
</form> |
144
|
|
|
</div> |
145
|
|
|
|
146
|
|
|
<?php |
147
|
|
|
$html = ob_get_clean(); |
148
|
|
|
|
149
|
|
|
if ( $echo ) { |
150
|
|
|
echo $html; |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $html; |
154
|
|
|
} |
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: