Completed
Push — master ( 667657...1db918 )
by
unknown
02:33
created

option-engine.php ➔ lasso_option_form()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 2
dl 0
loc 68
rs 8.6981
c 0
b 0
f 0

How to fix   Long Method   

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
*	This is used by addons to add cool stuff to the settings modal as an additional tab
4
*
5
*	Example:
6
*	add_filter('lasso_modal_tabs', 'try_tabs');
7
*	function try_tabs( $tabs ){
8
*		$tabs[] = array(
9
*	  		'name' 	=> 'Tab',
10
*	  		'content' => 'mytestcallback',
11
*	  		'options'	=> 'myOptionsCallback'
12
*		);
13
*
14
*		return $tabs;
15
*	}
16
*	function myOptionsCallback(){
17
*
18
*		$options = array(
19
*			array(
20
*				'id'		=> 'title',
21
*				'name' 		=> 'Title',
22
*				'type'		=> 'text',
23
*				'default'	=> 'default',
24
*				'desc'		=> 'Cool'
25
*			),
26
*			array(
27
*				'id'		=> 'another',
28
*				'name' 		=> 'Another',
29
*				'type'		=> 'textarea',
30
*				'default'	=> 'default',
31
*				'desc'		=> 'Awesome'
32
*			)
33
*		);
34
*
35
*		return $options;
36
*
37
*	}
38
*
39
*	@since 0.9.4
40
*/
41
42
/**
43
*	Get an array of addon data for the settings modal
44
*	@since 0.9.4
45
*/
46
function lasso_get_modal_tabs(){
47
48
	$tabs = array();
49
50
	return apply_filters('lasso_modal_tabs', $tabs);
51
52
}
53
54
/**
55
*	Build a side tabs to fit alongside the post settings modal
56
*	This is used by addons to add cool stuff to the settings modal as an additional tab
57
*
58
*	@param $type string tab or content
59
*	@uses lasso_get_modal_tabs()
60
*	@uses lasso_modal_addons_content()
61
*	@since 0.9.4
62
*/
63
function lasso_modal_addons( $type = 'tab' ){
64
65
	$tabs = lasso_get_modal_tabs();
66
	$out = '';
67
68
	if ( $tabs ):
69
70
		if ( 'tab' == $type ) {
71
72
			$out = '<ul class="lasso--modal__tabs">';
73
74
				$out .= '<li class="active-tab" data-addon-name="core">Editus</li>';
75
76
				foreach ( $tabs as $tab ) {
77
78
					if ( isset( $tab ) ) {
79
80
						$out .= lasso_modal_addons_content( $tab, $type );
81
					}
82
				}
83
84
			$out .= '</ul>';
85
86
		} elseif ( 'content' == $type ) {
87
			foreach ( $tabs as $tab ) {
88
89
				if ( isset( $tab ) ) {
90
					$out .= lasso_modal_addons_content( $tab , $type );
91
				}
92
			}
93
94
		}
95
96
	endif;
97
98
	return !empty( $out ) ? $out : false;
99
}
100
101
/**
102
*	Build a post meta options form
103
*
104
*	@param $name string the name of this tab being logged
105
*	@param $options array an array of option fields in the format below
106
*
107
*					array(
108
*						'id'		=> 'title',
109
*						'name' 		=> 'Title',
110
*						'type'		=> 'text',
111
*						'default'	=> 'default',
112
*						'desc'		=> 'My description'
113
*					)
114
*
115
*	@since 0.9.5
116
*	@subpackage lasso_modal_addons_content
117
*/
118
function lasso_option_form( $name = '', $options = array() ){
119
120
	ob_start();
121
122
	if ( empty( $name ) || empty( $options ) || !is_array( $options ) )
123
		return;
124
125
	$nonce = wp_create_nonce('lasso-process-post-meta');
126
	$key   = sprintf('_lasso_%s_settings', $name );
127
128
	$out = sprintf('<form id="lasso--post-form" class="lasso--post-form">' );
129
130
		$out .= lasso_option_fields( $name, $options );
131
		$out .='<div class="form--bottom">';
132
			$out .='<input type="submit" value="'.__( 'Save', 'lasso' ).'">';
133
			$out .='<input type="hidden" name="tab_name" value="'.$key.'">';
134
			$out .='<input type="hidden" name="post_id" value="'.get_the_ID().'">';
135
			$out .='<input type="hidden" name="nonce" value="'.$nonce.'">';
136
			$out .='<input type="hidden" name="action" value="process_meta_update">';
137
		$out .='</div>';
138
139
	$out .= '</form>';
140
	
141
	$out .= "<script>(function( $ ) {
142
					$(document).ready(function(){
143
					   
144
						function imgCustomTabDialog( ){
145
                			var that = this;
146
                
147
                		    // Create the media frame.
148
                		    var lasso_file_frame = wp.media.frames.file_frame = wp.media({
149
                		      	title: 'Select Image',
150
                		      	button: {
151
                		        	text: 'Insert Image',
152
                		      	},
153
                		      	multiple: false  // Set to true to allow multiple files to be selected
154
                		    });
155
                
156
                		    // When an image is selected, run a callback.
157
                		    lasso_file_frame.on( 'select', function() {
158
                		      	var attachment = lasso_file_frame.state().get('selection').first().toJSON();
159
                		        $(that).parent().find('input').val(attachment.url );
160
                		        $(that).parent().find('img').attr('src', attachment.url );
161
                
162
                		    });
163
                
164
                		    // Finally, open the modal
165
                			lasso_file_frame.open();
166
                		};
167
						
168
						function imgCustomTabRemove( ){
169
                		    $(this).parent().find('input').val('');
170
                		    $(this).parent().find('img').attr('src', '' );
171
                		};
172
                		
173
                		jQuery('.editus-customtab-image-control').mousedown(imgCustomTabDialog);
174
						jQuery('.editus-customtab-image-remove').mousedown(imgCustomTabRemove);
175
					});
176
				})( jQuery );
177
	        
178
    		
179
    		</script>";
180
181
	echo $out;
182
183
	return ob_get_clean();
184
185
}
186
187
/**
188
*	Build settings fields for lasso_option_form
189
*
190
*	@param $name string the name of this tab being logged
191
*	@param $options array an array of option fields in the format above
192
*	@since 0.9.5
193
*	@subpackage lasso_modal_addons_content
194
*/
195
function lasso_option_fields( $name = '', $options = array() ){
196
197
	$out 	= '';
198
	$before = '<div class="lasso--postsettings__option">';
199
	$after 	= '</div>';
200
201
	if ( empty( $name ) || empty( $options ) )
202
		return;
203
204
	foreach ( (array) $options as $option ) {
205
206
		$type = isset( $option['type'] ) ? $option['type'] : 'text';
207
208
		switch ( $type ) {
209
			case 'text':
210
				$out .= sprintf('%s%s%s', $before, lasso_option_engine_option( $name, $option,'text' ), $after );
211
				break;
212
			case 'textarea':
213
				$out .= sprintf('%s%s%s', $before, lasso_option_engine_option( $name, $option,'textarea' ), $after );
214
				break;
215
			case 'imgurl':
216
				$out .= sprintf('%s%s%s', $before, lasso_option_engine_option( $name, $option,'imgurl' ), $after );
217
				break;
218
			case 'checkbox':
219
				$out .= sprintf('%s%s%s', $before, lasso_option_engine_option( $name, $option,'checkbox' ), $after );
220
				break;
221
		}
222
223
	}
224
	
225
	
226
227
	return $out;
228
}
229
230
231
/**
232
*	Build settings inputs for settings fields
233
*
234
*	@param $name
235
*	@param $option mixed object
236
*	@param $type string text, textarea, checkbox, color
237
*	@since 5.0
238
*/
239
function lasso_option_engine_option( $name = '', $option = '', $type = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
240
241
	if ( empty( $type ) || empty( $option ) )
242
		return;
243
244
	$id = isset( $option['id'] ) ? $option['id'] : false;
245
	$id = $id ? lasso_clean_string( $id ) : false;
246
247
	$desc = isset( $option['desc'] ) ? $option['desc'] : false;
248
249
	$value = get_post_meta( get_the_id(), $option[ 'id' ], true );
250
251
	switch ( $type ) {
252
		case 'text':
253
			$out = sprintf('<label for="lasso--post-option-%s">%s</label><input id="lasso--post-option-%s" class="editus-custom-field-text" name="%s" type="text" value="%s">',$id, esc_html( $desc ), $id, $id, $value );
254
			break;
255
		case 'textarea':
256
			$out = sprintf('<label for="lasso--post-option-%s">%s</label><textarea id="lasso--post-option-%s" name="%s">%s</textarea>',$id, esc_html( $desc ), $id, $id, $value );
257
			break;
258
		case 'imgurl':
259
			$out = sprintf('<label for="lasso--post-option-%s">%s</label><img src="%s" style="height:80px;"><input id="lasso--post-option-%s" class="editus-custom-field-text" name="%s" type="text" value="%s" style="display:none;"><div title="Replace Image"  class="editus-customtab-image-control" style="float:left;"><i class="lasso-icon-image" style="font-size:20px;padding:5px;"></i></div><div title="Remove Image"  class="editus-customtab-image-remove" style="float:left;"><i class="lasso-icon-bin2" style="font-size:20px;padding:5px;"></i></div>',$id, esc_html( $desc ), $value, $id, $id,  $value );
260
			break;
261
		case 'checkbox':
262
			$out = sprintf('<label for="lasso--post-option-%s" class="checkbox-control checkbox"><input id="lasso--post-option-%s" type="checkbox" name="%s" class="checkbox"><span class="control-indicator"></span>%s',$id, $id, $id ,esc_html( $desc ) );
263
			break;
264
	}
265
266
	return $out;
0 ignored issues
show
Bug introduced by
The variable $out does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
267
268
}
269
270