MetaBox::render()   C
last analyzed

Complexity

Conditions 11
Paths 16

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 16
nop 0
dl 0
loc 52
rs 6.9006
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\MslsPlugin;
10
use lloc\Msls\MslsRegistryInstance;
11
12
class MetaBox extends MslsRegistryInstance {
13
	protected $data = [];
14
15
	/**
16
	 * Renders the content import metabox.
17
	 */
18
	public function render() {
19
		$post            = get_post();
20
		$mydata          = new MslsOptionsPost( $post->ID );
21
		$languages       = MslsOptionsPost::instance()->get_available_languages();
22
		$current         = MslsBlogCollection::get_blog_language( get_current_blog_id() );
23
		$languages       = array_diff_key( $languages, array( $current => $current ) );
24
		$input_lang      = isset( $_GET['msls_lang'] ) ? $_GET['msls_lang'] : null;
25
		$input_id        = isset( $_GET['msls_id'] ) ? $_GET['msls_id'] : null;
26
		$has_input       = null !== $input_lang && null !== $input_id;
27
		$blogs           = MslsBlogCollection::instance();
28
		$available       = array_filter( array_map( function ( $lang ) use ( $mydata ) {
29
			return $mydata->{$lang};
30
		}, array_keys( $languages ) ) );
31
		$has_translation = count( $available ) >= 1;
32
33
		if ( $has_input || $has_translation ) {
34
			add_thickbox();
35
			$label_template = __( 'Import content from %s', 'multisite-language-switcher' );
36
			$output         = '<fieldset>';
37
			$output         .= '<legend>'
38
			                   . esc_html__( 'Warning! This will override and replace all the post content with the content from the source post!',
39
					'multisite-language-switcher' )
40
			                   . '</legend>';
41
			foreach ( $languages as $language => $label ) {
42
				$id    = $mydata->{$language};
43
				$blog  = $blogs->get_blog_id( $language );
44
				$label = sprintf( $label_template, $label );
45
				if ( null === $id && $has_input && $input_lang === $language ) {
46
					$id   = $input_id;
47
					$blog = $blogs->get_blog_id( $language );
48
				}
49
				if ( null !== $id ) {
50
					$this->data = [
51
						'msls_import'  => "{$blog}|{$id}",
52
					];
53
					$output .= sprintf( '<a class="button button-primary thickbox" href="%s" title="%s">%s</a>',
54
						$this->inline_thickbox_url( $this->data ),
55
						$label,
56
						$label
57
					);
58
				}
59
			}
60
			$output .= '</fieldset>';
61
		} else {
62
			$output = '<p>' .
63
			          esc_html__( 'No translated versions linked to this post: import content functionality is disabled.',
64
				          'multisite-language-switcher' )
65
			          . '</p>';
66
		}
67
68
		echo $output;
69
	}
70
71
	protected function inline_thickbox_url( array $data = [] ) {
72
		$args = array_merge( [
73
			'modal'    => true,
74
			'width'    => 770, // meh, just a guess on *most* devices
75
			'height'   => 770,
76
			'inlineId' => 'msls-import-dialog-' . str_replace( '|', '-', $data['msls_import'] ),
77
		], $data );
78
79
		return esc_url(
80
			'#TB_inline' . add_query_arg( $args, '' )
81
		);
82
	}
83
84
	public function print_modal_html() {
85
		echo $this->inline_thickbox_html( true, $this->data );
86
	}
87
88
	protected function inline_thickbox_html( $echo = true, array $data = [] ) {
89
		if ( ! isset( $data['msls_import'] ) ) {
90
			return '';
91
		}
92
93
		$slug = str_replace( '|', '-', $data['msls_import'] );
94
95
		ob_start();
96
		?>
97
        <div style="display: none;" id="msls-import-dialog-<?php echo esc_attr( $slug ) ?>">
98
            <h3><?php esc_html_e( 'Select what should be imported and how', 'multisite-language-switcher' ) ?></h3>
99
100
            <form action="<?php echo add_query_arg( [] ) ?>" method="post">
101
102
				<?php wp_nonce_field( MslsPlugin::path(), 'msls_noncename' ); ?>
103
104
				<?php foreach ( $data as $key => $value ) : ?>
105
                    <input type="hidden" name="<?php echo esc_attr( $key ) ?>" value="<?php echo esc_attr( $value ) ?>">
106
				<?php endforeach; ?>
107
108
				<?php /** @var ImportersFactory $factory */
109
				foreach ( Map::instance()->factories() as $slug => $factory ) : ?>
110
					<?php $details = $factory->details() ?>
111
                    <h4><?php echo esc_html( $details->name ) ?></h4>
112
					<?php if ( empty( $details->importers ) ) : ?>
113
                        <p><?php esc_html_e( 'No importers available for this type of content.', 'multisite-language-switcher' ) ?></p>
114
					<?php else: ?>
115
                        <ul>
116
                            <li>
117
                                <label>
118
                                    <input type="radio" name="msls_importers[<?php echo esc_attr( $details->slug ) ?>]">
119
									<?php esc_html_e( 'Off - Do not import this type of content in the destination post.', 'multisite-language-switcher' ) ?>
120
                                </label>
121
                            </li>
122
							<?php foreach ( $details->importers as $importer_slug => $importer_info ) : ?>
123
                                <li>
124
                                    <label>
125
                                        <input type="radio" name="msls_importers[<?php echo esc_attr( $details->slug ) ?>]"
126
                                               value="<?php echo esc_attr( $importer_slug ) ?>"
127
											<?php checked( $details->selected, $importer_slug ) ?>
128
                                        >
129
										<?php echo( esc_html( sprintf( '%s -  %s', $importer_info->name, $importer_info->description ) ) ) ?>
130
                                    </label>
131
                                </li>
132
							<?php endforeach; ?>
133
                        </ul>
134
					<?php endif; ?>
135
				<?php endforeach; ?>
136
137
                <div>
138
                    <input
139
                            type="submit"
140
                            class="button button-primary"
141
                            value="<?php esc_html_e( 'Import Content', 'multisite-language-switcher' ) ?>"
142
                    >
143
                </div>
144
            </form>
145
        </div>
146
147
		<?php
148
		$html = ob_get_clean();
149
150
		if ( $echo ) {
151
			echo $html;
152
		}
153
154
		return $html;
155
	}
156
}
157