GMB_Shortcode_Generator::select()   C
last analyzed

Complexity

Conditions 12
Paths 96

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 96
nop 1
dl 0
loc 71
rs 6.206
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
/**
4
 * GMB_Shortcode_Generator class.
5
 *
6
 * @description: Adds a TinyMCE button that's clickable
7
 *
8
 * @since      2.0
9
 */
10
class GMB_Shortcode_Generator extends Google_Maps_Builder_Core_Shortcode_Generator {
11
12
	/**
13
	 * Constructor
14
	 */
15
	public function __construct() {
16
		parent::__construct();
17
		add_action( 'gmb_after_shortcode_form', array( $this, 'form_upsell') );
18
		add_action( 'gmb_shortcode_iframe_style', array( $this, 'upsell_css' ) );
19
	}
20
21
	
22
23
	/**
24
	 * Renders an HTML Dropdown
25
	 *
26
	 * @since 2.0
27
	 *
28
	 * @param array $args
29
	 *
30
	 * @return string
31
	 */
32
	public static function select( $args = array() ) {
33
34
		$defaults = array(
35
			'options'          => array(),
36
			'name'             => null,
37
			'class'            => '',
38
			'id'               => '',
39
			'selected'         => 0,
40
			'chosen'           => false,
41
			'placeholder'      => null,
42
			'multiple'         => false,
43
			'show_option_all'  => _x( 'All', 'all dropdown items', 'google-maps-builder' ),
44
			'show_option_none' => _x( 'None', 'no dropdown items', 'google-maps-builder' )
45
		);
46
47
		$args = wp_parse_args( $args, $defaults );
48
49
		if ( $args['multiple'] ) {
50
			$multiple = ' MULTIPLE';
51
		} else {
52
			$multiple = '';
53
		}
54
55
		if ( $args['chosen'] ) {
56
			$args['class'] .= 'gmb-select-chosen';
57
		}
58
59
		if ( $args['placeholder'] ) {
60
			$placeholder = $args['placeholder'];
61
		} else {
62
			$placeholder = '';
63
		}
64
65
		$output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( sanitize_key( str_replace( '-', '_', $args['id'] ) ) ) . '" class="gmb-select ' . esc_attr( $args['class'] ) . '"' . $multiple . ' data-placeholder="' . $placeholder . '">';
66
67
		if ( $args['show_option_all'] ) {
68
			if ( $args['multiple'] ) {
69
				$selected = selected( true, in_array( 0, $args['selected'] ), false );
70
			} else {
71
				$selected = selected( $args['selected'], 0, false );
72
			}
73
			$output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
74
		}
75
76
		if ( ! empty( $args['options'] ) ) {
77
78
			if ( $args['show_option_none'] ) {
79
				if ( $args['multiple'] ) {
80
					$selected = selected( true, in_array( - 1, $args['selected'] ), false );
81
				} else {
82
					$selected = selected( $args['selected'], - 1, false );
83
				}
84
				$output .= '<option value="-1"' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
85
			}
86
87
			foreach ( $args['options'] as $key => $option ) {
88
89
				if ( $args['multiple'] && is_array( $args['selected'] ) ) {
90
					$selected = selected( true, in_array( $key, $args['selected'] ), false );
91
				} else {
92
					$selected = selected( $args['selected'], $key, false );
93
				}
94
95
				$output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
96
			}
97
		}
98
99
		$output .= '</select>';
100
101
		return $output;
102
	}
103
104
	/**
105
	 * Add upsell markup to shortcode form
106
	 *
107
	 * @since 2.1.0
108
	 *
109
	 * @uses "gmb_after_shortcode_form"
110
	 */
111
	public function form_upsell(){?>
112
		<a href="https://wordimpress.com/plugins/maps-builder-pro?utm_source=MBF&utm_medium=BANNER&utm_content=SHORTCODE&utm_campaign=MBF%20Shortcode" class="button button-small shortcode-upsell" target="_blank">
113
			<?php _e( 'Go Pro', 'google-maps-builder' ); ?>
114
			<span class="dashicons dashicons-external"></span>
115
		</a>
116
	<?php
117
118
	}
119
120
	/**
121
	 * Add extra css for upsell
122
	 *
123
	 * @since 2.1.0
124
	 *
125
	 * @uses "gmb_shortcode_iframe_style" action
126
	 */
127
	public function upsell_css(){ ?>
128
		.shortcode-upsell {
129
			position: absolute;
130
			bottom: 10px;
131
			right: 10px;
132
			padding: 5px 10px !important;
133
			font-size: 13px !important;
134
		}
135
136
		.shortcode-upsell span.dashicons {
137
			font-size: 12px;
138
			height: 14px;
139
			position: relative;
140
			top: 3px;
141
			opacity: 0.8;
142
			width: 12px;
143
		}
144
	<?php
145
146
	}
147
148
}
149