Completed
Pull Request — master (#111)
by Luca
02:07
created

MetaBox::render()   C

Complexity

Conditions 11
Paths 16

Size

Total Lines 52
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 43
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 52
rs 5.9999

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\MslsRegistryInstance;
10
11
class MetaBox extends MslsRegistryInstance {
12
	protected $data = [];
13
14
	/**
15
	 * Renders the content import metabox.
16
	 */
17
	public function render() {
0 ignored issues
show
Coding Style introduced by
render uses the super-global variable $_GET which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
24
		$input_id        = isset( $_GET['msls_id'] ) ? $_GET['msls_id'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
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' )
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 31 spaces, but found 20.
Loading history...
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' )
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 22 spaces, but found 26.
Loading history...
64
			          . '</p>';
65
		}
66
67
		echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
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 );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
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">
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'add_query_arg'
Loading history...
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: ?>
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
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 ) ) ) ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
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;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$html'
Loading history...
151
		}
152
153
		return $html;
154
	}
155
}