Completed
Push — master ( 66a141...284c90 )
by
unknown
02:24
created

helpers.php ➔ lasso_get_supported_theme_title_class()   D

Complexity

Conditions 10
Paths 9

Size

Total Lines 35
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 29
nc 9
nop 0
dl 0
loc 35
rs 4.8196
c 0
b 0
f 0

How to fix   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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 386.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Grab an option from our settings
5
 *
6
 * If we're on multsite we'll grab the site option which is stored in the main blogs site option tables, otherwise
7
 * we'll grab the option which is stored on the single blogs option tables
8
 *
9
 * @param unknown $option  string name of the option
10
 * @param unknown $section string name of the section
11
 * @param unknown $default string/int default option value
12
 * @return the option value
13
 * @since 1.0
14
 */
15
function lasso_editor_get_option( $option, $section, $default = '' ) {
16
17
	if ( empty( $option ) )
18
		return;
19
20
	if ( function_exists( 'is_multisite' ) && is_multisite() ) {
21
22
		$options = get_site_option( $section );
23
24
	} else {
25
26
		$options = get_option( $section );
27
	}
28
29
	if ( isset( $options[$option] ) ) {
30
		return $options[$option];
31
	}
32
33
	return $default;
34
}
35
36
/**
37
 * Check to see if any Lasso galleries exist
38
 *
39
 * @since 1.0
40
 */
41
function lasso_editor_galleries_exist() {
42
43
	$q = new wp_query( array( 'post_type' => 'ai_galleries', 'post_status' => 'publish' ) );
44
45
	if ( $q->have_posts() )
46
		return true;
47
	else
48
		return false;
49
}
50
51
/**
52
 * Return a CSS class of an automatically supported theme
53
 *
54
 * @since 0.8.6
55
 * @return a css class if the theme is supported, false if nothing
56
 */
57
function lasso_get_supported_theme_class() {
58
59
	$name  	= wp_get_theme()->get('Name');
60
	$slug  	= lasso_clean_string( $name );
61
62
	switch ( $slug ) {
63
		case 'aesop-story-theme': // aesop
64
			$out = '.aesop-entry-content';
65
			break;
66
		case 'jorgen': // aesop
67
			$out = '.jorgen-entry-content';
68
			break;
69
		case 'novella': // aesop
70
			$out = '.novella-entry-content';
71
			break;
72
		case 'genji': // aesop
73
			$out = '.genji-entry-content';
74
			break;
75
		case 'kerouac': // aesop
76
			$out = '.kerouac-entry-content';
77
			break;
78
		case 'zealot': // aesop
79
			$out = '.zealot-entry-content';
80
			break;
81
		case 'fable': // aesop
82
			$out = '.fable--entry-content';
83
			break;
84
		case 'canvas': // wootheme..err...Automattic
85
			$out = '.entry';
86
			break;
87
		case 'kleo': // 
88
			$out = '.article-content';
89
			break;
90
		//case 'exposure': // 
91
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
92
		//	break;
93
		//case 'lore': // 
94
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
		//	break;
96
		//case 'worldview': // upthemes
97
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
		//	break;
99
		//case 'genesis': // genesis
100
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
		//	break;
102
		//case 'camera': // array.is
103
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
		//	break;
105
		case 'longform': 
106
			$out = '.entry-content-wrapper';
107
			break;
108
109
	}
110
111
	return apply_filters('lasso_content_class', !empty( $out ) ? $out : false);
112
	//return !empty( $out ) ? $out : false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
113
}
114
115
function lasso_get_supported_theme_title_class() {
116
117
	$name  	= wp_get_theme()->get('Name');
118
	$slug  	= lasso_clean_string( $name );
119
120
	switch ( $slug ) {
121
122
		case 'aesop-story-theme': // aesop
123
			$out = '.aesop-entry-title';
124
			break;
125
		case 'jorgen': // aesop
126
			$out = '.jorgen-entry-title';
127
			break;
128
		case 'genji': // aesop
129
			$out = '.genji-entry-title';
130
			break;
131
		case 'kerouac': // aesop
132
			$out = '.kerouac-entry-title';
133
			break;
134
		case 'zealot': // aesop
135
			$out = '.zealot-entry-title';
136
			break;
137
		case 'fable': // aesop
138
			$out = '.fable--entry-title';
139
			break;
140
		case 'kleo': // 
141
			$out = '.page-title';
142
			break;
143
		case 'longform': // 
144
			$out = '.entry-title';
145
			break;
146
	}
147
148
	return apply_filters('lasso_title_class', !empty( $out ) ? $out : false);
149
}
150
151
//since 0.9.9.6
152
function lasso_get_supported_theme_featured_image_class() {
153
154
	$name  	= wp_get_theme()->get('Name');
155
	$slug  	= lasso_clean_string( $name );
0 ignored issues
show
Unused Code introduced by
$slug is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
156
157
	return apply_filters('lasso_featured_image_class', !empty( $out ) ? $out : false);
0 ignored issues
show
Bug introduced by
The variable $out seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
158
}
159
160
161
/**
162
*	Return a string of classes with items that Lasso will remove when entering the editor
163
*	so that we don't save them as HTML
164
*
165
*	@since 0.8.7
166
*	@return string of comma separated classes
167
*/
168
function lasso_supported_no_save(){
169
170
	return apply_filters('lasso_dont_save', '.lasso--ignore, .sharedaddy, .us_wrapper, .twitter-tweet, .meta, .edit-link, .ssba');
171
}
172
173
/**
174
 * Generic sanitization, useful for sanitization of arrays.
175
 *
176
 * @since 0.9.2
177
 *
178
 * @param array|object|string $data Data to sanatize.
179
 *
180
 * @return array|mixed|object|string|void
181
 */
182
function lasso_sanitize_data( $data ) {
183
	return \lasso\sanatize::do_sanitize( $data );
184
185
}
186
187
/**
188
 *	Return a comma delimited list of categories for a specific post object
189
 *
190
 *	@since 0.9.3
191
 *	@return string of comma delimited category slugs
192
*/
193
function lasso_get_post_objects( $postid = '', $taxonomy = 'category') {
194
195
	if ( empty( $postid ) )
196
		$postid = get_the_ID();
197
198
	$objects = 'category' == $taxonomy ? get_the_category( $postid ) : get_the_tags( $postid );
199
200
	if ( empty( $objects) )
201
		return;
202
203
	$out = '';
204
	foreach( $objects as $object ) {
205
		$out .= $object->slug.', ';
206
	}
207
208
	return rtrim($out, ', ');
209
210
}
211
212
/**
213
 *	Return an array of categories for autocomplete
214
 *
215
 *	@since 0.9.3
216
 *	@return array all categoiries
217
*/
218
function lasso_get_objects( $taxonomy = 'category' ) {
219
220
	$objects = 'category' == $taxonomy ? get_categories(array('hide_empty' => 0)) : get_tags(array('hide_empty' => 0));
221
222
	if ( empty( $objects) )
223
		return;
224
225
	$out = array();
226
	foreach( $objects as $object ) {
227
		$out[] = $object->slug;
228
	}
229
230
	return $out;
231
}
232
233
234
/**
235
 * Get allowed post types for the post chooser modal.
236
 *
237
 *
238
 * @since 0.9.4
239
 */
240
function lasso_post_types_names() {
241
	$post_types = get_post_types( array(
242
		'public' => true,
243
	), 'objects' );
244
	$post_types = array_combine( array_keys( $post_types ), wp_list_pluck( $post_types, 'label' ) );
245
    unset( $post_types[ 'attachment' ] );
246
247
	/**
248
	 * Set which post types are allowed
249
	 *
250
	 * @since 0.9.4
251
	 *
252
	 * @param array $allowed_post_types Array of names (not labels) of allowed post types. Must be registered.
253
	 */
254
	$allowed_post_types = apply_filters( 'lasso_allowed_post_types', array( 'post', 'page') );
255
	foreach( $post_types as $name => $label ) {
256
		if ( ! in_array( $name, $allowed_post_types ) ) {
257
			unset( $post_types[ $name ] );
258
		}
259
	}
260
	return $post_types;
261
}
262
263
264
function lasso_post_types() {
265
	$post_types = get_post_types( array(
266
		'public' => true,
267
	), 'names' );
268
	//$post_types = array_combine( array_keys( $post_types ), wp_list_pluck( $post_types, 'label' ) );
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
269
    unset( $post_types[ 'attachment' ] );
270
271
	/**
272
	 * Set which post types are allowed
273
	 *
274
	 * @since 0.9.4
275
	 *
276
	 * @param array $allowed_post_types Array of names (not labels) of allowed post types. Must be registered.
277
	 */
278
	$allowed_post_types = apply_filters( 'lasso_allowed_post_types', array( 'post', 'page') );
279
	foreach( $post_types as $name => $label ) {
280
		if ( ! in_array( $name, $allowed_post_types ) ) {
281
			unset( $post_types[ $name ] );
282
		}
283
	}
284
	return $post_types;
285
}
286
287
288
289
////////////////////
290
// INTERNAL
291
////////////////////
292
/**
293
*	Used internally as a callback to build a tab or content area for modal addons
294
*
295
*	@param $tab object
296
*	@param $type string tab or content
297
*	@uses lasso_modal_addons()
298
*	@since 0.9.4
299
*/
300
function lasso_modal_addons_content( $tab = '', $type ){
301
302
	$name = lasso_clean_string( $tab['name'] );
303
304
	if ( 'tab' == $type ) {
305
306
		$out = sprintf( '<li data-addon-name="%s">%s</li>', $name, $tab['name'] );
307
308
	} else if ( 'content' == $type ){
309
310
		$content = isset( $tab['content'] ) && is_callable( $tab['content'] ) ? call_user_func( $tab['content'] ) : false;
311
		$options = isset( $tab['options'] ) && is_callable( $tab['options'] ) ? call_user_func( $tab['options'] ) : false;
312
313
		$out = sprintf( '<div class="lasso--modal__content not-visible" data-addon-content="%s">
314
			%s%s
315
			</div>', $name, $content, lasso_option_form( $name, $options ) );
316
317
	}
318
319
	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...
320
}
321
322
/**
323
*	Helper function to clean a string and replace spaces with dash
324
*
325
*	@param $string string content
326
*	@since 0.9.4
327
*
328
* @return void|string
329
*/
330
function lasso_clean_string( $string = '' ) {
331
332
	if ( empty( $string ) )
333
		return;
334
335
	return sanitize_text_field( strtolower( preg_replace('/[\s_]/', '-', $string ) ) );
336
}
337
338
/**
339
 *	Helper function to switch - to _ after having.
340
 *
341
 * This is the evil twin of lasso_clean_string() and may or may not make your data forever unclean.
342
 *
343
 *	@param $string string content
344
 *	@since 0.9.5
345
 *
346
 * @return void|string
347
 */
348
function lasso_unclean_string( $string = '' ) {
349
350
	if ( empty( $string ) ) {
351
		return;
352
	}
353
354
	return sanitize_text_field( strtolower( str_replace( '-', '_', $string ) ) );
355
}
356
357
358
////////////////////
359
// PLUGGABLE
360
////////////////////
361
362
/**
363
 * Check if the user is logged in and has the correctly passed capability
364
 *
365
 * @param unknown $action string a capability such as edit_posts or publish_posts
366
 * @param unknown $postid int the id of the post object to check against
367
 * @since 0.9.9.7 added filter 'lasso_user_can_filter'
368
 */
369
if ( !function_exists( 'lasso_user_can' ) ):
370
	function lasso_user_can( $action = '', $postid = 0 ) {
371
        $result = false;
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
372
		if ( empty( $action ) )
373
			$action = 'edit_posts';
374
375
		if ( empty( $postid ) )
376
			$postid = get_the_ID();
377
378
		if ( is_user_logged_in() && current_user_can( $action, $postid ) ) {
379
			$result =  true;
380
		} else {
381
			$result = false;
382
		}
383
		
384
		return apply_filters( 'lasso_user_can_filter', $result,  $action, $postid);
385
	}
386
endif;
387
388
/**
389
*	Empty state message thats shown when no data available
390
*
391
*	@since 0.9.5
392
*/
393
if ( !function_exists('lasso_editor_empty_results') ):
394
395
	function lasso_editor_empty_results( $type = 'posts' ){
396
397
		if ( 'posts' == $type ) {
398
399
			$string = apply_filters('lasso_empty_state_message', __('No posts to show', 'lasso') );
400
			$icon = 'lasso-icon-file-text2';
401
			$button = false;
402
403
		} elseif ( 'revision' == $type ) {
404
405
			$string = apply_filters('lasso_empty_state_message', __('No revisions found', 'lasso') );
406
			$icon = 'lasso-icon-history';
407
			$button = sprintf('<a href="#" class="lasso--btn-secondary" id="lasso--close-modal">%s</a>', __('Close','lasso') );
408
409
		}
410
411
		return sprintf('<div id="lasso--empty-state" class="lasso--empty-state"><i class="lasso--empty-state-icon lasso-icon %s"></i><p>%s</p>%s</div>', $icon, $string, $button );
0 ignored issues
show
Bug introduced by
The variable $icon 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...
Bug introduced by
The variable $string 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...
Bug introduced by
The variable $button 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...
412
	}
413
414
endif;
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429