Completed
Push — master ( 48e484...cedfdf )
by
unknown
03:10
created

option-engine.php ➔ lasso_option_engine_option()   D

Complexity

Conditions 9
Paths 33

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 9
eloc 18
c 6
b 0
f 0
nc 33
nop 3
dl 0
loc 27
rs 4.909
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">Lasso</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
88
89
			foreach ( $tabs as $tab ) {
90
91
				if ( isset( $tab ) ) {
92
					$out .= lasso_modal_addons_content( $tab , $type );
93
				}
94
			}
95
96
		}
97
98
	endif;
99
100
	return !empty( $out ) ? $out : false;
101
}
102
103
/**
104
*	Build a post meta options form
105
*
106
*	@param $name string the name of this tab being logged
107
*	@param $options array an array of option fields in the format below
108
*
109
*					array(
110
*						'id'		=> 'title',
111
*						'name' 		=> 'Title',
112
*						'type'		=> 'text',
113
*						'default'	=> 'default',
114
*						'desc'		=> 'My description'
115
*					)
116
*
117
*	@since 0.9.5
118
*	@subpackage lasso_modal_addons_content
119
*/
120
function lasso_option_form( $name = '', $options = array() ){
121
122
	ob_start();
123
124
	if ( empty( $name ) || empty( $options ) || !is_array( $options ) )
125
		return;
126
127
	$nonce = wp_create_nonce('lasso-process-post-meta');
128
	$key   = sprintf('_lasso_%s_settings', $name );
129
130
	$out = sprintf('<form id="lasso--post-form" class="lasso--post-form">' );
131
132
		$out .= lasso_option_fields( $name, $options );
133
		$out .='<div class="form--bottom">';
134
			$out .='<input type="submit" value="Save">';
135
			$out .='<input type="hidden" name="tab_name" value="'.$key.'">';
136
			$out .='<input type="hidden" name="post_id" value="'.get_the_ID().'">';
137
			$out .='<input type="hidden" name="nonce" value="'.$nonce.'">';
138
			$out .='<input type="hidden" name="action" value="process_meta_update">';
139
		$out .='</div>';
140
141
	$out .= '</form>';
142
143
	echo $out;
144
145
	return ob_get_clean();
146
147
}
148
149
/**
150
*	Build settings fields for lasso_option_form
151
*
152
*	@param $name string the name of this tab being logged
153
*	@param $options array an array of option fields in the format above
154
*	@since 0.9.5
155
*	@subpackage lasso_modal_addons_content
156
*/
157
function lasso_option_fields( $name = '', $options = array() ){
158
159
	$out 	= '';
160
	$before = '<div class="lasso--postsettings__option">';
161
	$after 	= '</div>';
162
163
	if ( empty( $name ) || empty( $options ) )
164
		return;
165
166
	foreach ( (array) $options as $option ) {
167
168
		$type = isset( $option['type'] ) ? $option['type'] : 'text';
169
170
		switch ( $type ) {
171
			case 'text':
172
				$out .= sprintf('%s%s%s', $before, lasso_option_engine_option( $name, $option,'text' ), $after );
173
				break;
174
			case 'textarea':
175
				$out .= sprintf('%s%s%s', $before, lasso_option_engine_option( $name, $option,'textarea' ), $after );
176
				break;
177
			case 'checkbox':
178
				$out .= sprintf('%s%s%s', $before, lasso_option_engine_option( $name, $option,'checkbox' ), $after );
179
				break;
180
		}
181
182
	}
183
184
	return $out;
185
}
186
187
188
/**
189
*	Build settings inputs for settings fields
190
*
191
*	@param $name
192
*	@param $option mixed object
193
*	@param $type string text, textarea, checkbox, color
194
*	@since 5.0
195
*/
196
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...
197
198
	if ( empty( $type ) || empty( $option ) )
199
		return;
200
201
	$id = isset( $option['id'] ) ? $option['id'] : false;
202
	$id = $id ? lasso_clean_string( $id ) : false;
203
204
	$desc = isset( $option['desc'] ) ? $option['desc'] : false;
205
206
	$value = get_post_meta( get_the_id(), $option[ 'id' ], true );
207
208
	switch ( $type ) {
209
		case 'text':
210
			$out = sprintf('<label for="lasso--post-option-%s">%s</label><input id="lasso--post-option-%s" name="%s" type="text" value="%s">',$id, esc_html( $desc ), $id, $id, $value );
211
			break;
212
		case 'textarea':
213
			$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 );
214
			break;
215
		case 'checkbox':
216
			$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 ) );
217
			break;
218
	}
219
220
	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...
221
222
}
223
224