functions-templating.php ➔ wpbo_get_templates_list()   C
last analyzed

Complexity

Conditions 8
Paths 13

Size

Total Lines 69
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 28
c 2
b 0
f 0
nc 13
nop 0
dl 0
loc 69
rs 6.5437

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
 * BetterOptin Metabox
4
 *
5
 * @package   BetterOptin/Metabox
6
 * @author    ThemeAvenue <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://themeavenue.net
9
 * @copyright 2015 ThemeAvenue
10
 */
11
12
// If this file is called directly, abort.
13
if ( ! defined( 'WPINC' ) ) {
14
	die;
15
}
16
17
/**
18
 * Retrieve the popup templates list.
19
 *
20
 * @since  1.0.0
21
 * @return array List of available templates with the associated screenshot
22
 */
23
function wpbo_get_templates_list() {
24
25
	/* Set the default templates directory */
26
	$directory = array(
27
		'path' => WPBO_PATH . 'templates',
28
		'url'  => WPBO_URL . 'templates'
29
	);
30
31
	/* Allow for extra directories */
32
	$dirs = apply_filters( 'wpbo_templates_dirs', array( $directory ) );
33
	$list = array();
34
35
	foreach( $dirs as $key => $dir ) {
36
37
		$exceptions = array( '.', '..' );
38
39
		if( !isset( $_GET['test_template'] ) )
40
			$exceptions[] = 'template-test.php';
41
42
		/* Get file paths with trailing slashes */
43
		$path = trailingslashit( $dir['path'] );
44
		$url  = trailingslashit( $dir['url'] );
45
46
		/* Scan the content */
47
		$templates = scandir( $path );
48
49
		foreach( $templates as $key => $template ) {
50
51
			$images = array( 'png', 'jpg', 'jpeg', 'gif' ); // Allowed images types
52
53
			/* Don't process the '.' and '..' */
54
			if( in_array( $template, $exceptions ) )
55
				continue;
56
57
			/* Get file extension */
58
			$ext = pathinfo( $path . $template, PATHINFO_EXTENSION );
59
60
			/* Only check the php files */
61
			if( 'php' != $ext )
62
				continue;
63
64
			/* Get template base name */
65
			$tpl = str_replace( ".$ext", '', $template );
66
67
			foreach( $images as $k => $type ) {
68
69
				$imgfile = $tpl . '.' . $type;
70
71
				if( file_exists( $path . $imgfile ) ) {
72
73
					/**
74
					 * @todo need to get image URL
75
					 */
76
					$img = $url . $imgfile;
77
					break;
78
79
				}
80
81
			}
82
83
			/* Add new template to the list */
84
			$list[$tpl] = $img;
0 ignored issues
show
Bug introduced by
The variable $img 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...
85
86
		}
87
	}
88
89
	return $list;
90
91
}
92
93
add_action( 'plugins_loaded', 'wpbo_save_templates' );
94
/**
95
 * Save Customized Templates.
96
 *
97
 * @since  1.0.0
98
 */
99
function wpbo_save_templates() {
100
101
	if ( ! isset( $_GET['wpbo_popup'] ) || ! isset( $_POST['wpbo_nonce'] ) ) {
102
		return;
103
	}
104
105
	$post_id = intval( $_GET['wpbo_popup'] );
106
107
	if ( 'wpbo-popup' != get_post_type( $post_id ) ) {
108
		return;
109
	}
110
111
	if ( ! wp_verify_nonce( $_POST['wpbo_nonce'], 'wpbo_customize_template' ) ) {
112
		return;
113
	}
114
115 View Code Duplication
	if ( isset( $_POST['taed-outerhtml'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
		update_post_meta( $post_id, '_wpbo_template_editor', htmlentities( $_POST['taed-outerhtml'], ENT_COMPAT | ENT_HTML401, 'UTF-8' ) );
117
	}
118
119 View Code Duplication
	if ( isset( $_POST['taed-outerhtmlclean'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
		update_post_meta( $post_id, '_wpbo_template_display', htmlentities( $_POST['taed-outerhtmlclean'], ENT_COMPAT | ENT_HTML401, 'UTF-8' ) );
121
	}
122
123
	/* Read-only redirect */
124
	wp_redirect( add_query_arg( array( 'post_type'  => 'wpbo-popup',
125
	                                   'page'       => 'wpbo-customizer',
126
	                                   'wpbo_popup' => $post_id,
127
	                                   'message'    => 'updated'
128
	), admin_url( 'edit.php' ) ) );
129
130
	exit;
131
132
}
133
134
135
add_action( 'init', 'wpbo_reset_template' );
136
/**
137
 * Reset to default template.
138
 *
139
 * Deletes all customizations from database which
140
 * will result in using the default template file.
141
 *
142
 * @since  1.0.0
143
 */
144
function wpbo_reset_template() {
145
146
	if ( isset( $_GET['wpbo_reset'] ) && isset( $_GET['wpbo_popup'] ) && wp_verify_nonce( $_GET['wpbo_reset'], 'reset_template' ) ) {
147
148
		delete_post_meta( $_GET['wpbo_popup'], '_wpbo_template_editor' );
149
		delete_post_meta( $_GET['wpbo_popup'], '_wpbo_template_display' );
150
151
		wp_redirect( add_query_arg( array(
152
			'wpbo_popup' => $_GET['wpbo_popup'],
153
			'post_type'  => 'wpbo-popup',
154
			'page'       => 'wpbo-customizer'
155
		), admin_url( 'edit.php' ) ) );
156
157
		exit;
158
159
	}
160
161
}