Admin   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 170
rs 10
c 1
b 0
f 0
wmc 25
lcom 1
cbo 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A initializes() 0 4 1
A hooks() 0 5 1
A wpdfi_enqueue_scripts() 0 3 1
A setting_menu() 0 3 1
A render_layout() 0 9 3
A _get_tabs() 0 6 1
A _get_current_tab() 0 3 3
A _get_default_tab() 0 3 1
A _get_options() 0 3 1
A _get_layout_name() 0 3 2
A _update_options() 0 3 1
C update_settings() 0 25 7
A get_option() 0 4 2
1
<?php
2
namespace WPDFI;
3
/**
4
 * This class handle all admin stuffs of this plugin
5
 *
6
 * @author Duc Bui Quang <[email protected]>
7
 * @since 1.0.0
8
 */
9
10
use WPDFI\Traits\Singleton;
11
12
final class Admin
13
{
14
	use Singleton;
15
16
	/**
17
	 * @traitDoc
18
	 */
19
	public function initializes() 
20
	{	
21
		$this->hooks();
22
	}
23
24
	/**
25
	 * All WordPress hooks come here
26
	 *
27
	 * @since 1.0.0
28
	 * @return void
29
	 */
30
	public function hooks() {
31
		\add_action( 'admin_enqueue_scripts', [$this, 'wpdfi_enqueue_scripts']);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
32
		\add_action( 'admin_menu', [$this, 'setting_menu'] );
33
		\add_action( 'init', [$this, 'update_settings']);	
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
34
	}
35
36
	/**
37
	 * Enqueue styles and scripts
38
	 *
39
	 * @since 1.0.0
40
	 * @return void
41
	 */
42
	public function wpdfi_enqueue_scripts() {
43
		\wp_enqueue_media();
44
	}
45
46
    /**
47
     * Add new setting menu
48
     *
49
     * @since 1.0.0
50
     * @return void
51
     */
52
	public function setting_menu() {
53
		\add_options_page( 'WPDFI', 'WPDFI', 'manage_options', 'wpdfi-settings.php', [$this, 'render_layout']);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
54
	}
55
56
	/**
57
	 * Render layout for wpdfi setting page
58
	 *
59
	 * @since 1.0.0
60
	 * @return void
61
	 */
62
	public function render_layout() {
0 ignored issues
show
Coding Style introduced by
render_layout uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
63
		global $pagenow;
64
		// Exit if this is not options and WP Default Thumbnail settings page
65
		if($pagenow != 'options-general.php' or $_GET['page'] != 'wpdfi-settings.php') return;
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_GET
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
66
		
67
		echo \wpdfi()->layout->get_admin_layout(
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '\'
Loading history...
68
			$this->_get_tabs(), $this->_get_current_tab(), $this->_get_options(), $this->_get_layout_name()
69
		);
70
	}
71
	
72
	/**
73
	 * Retrieve tabs content
74
	 *
75
	 * @since 1.0.0
76
	 * @return array
77
	 */
78
	private function _get_tabs() {
79
		return [ 
80
			'dfis'		=> 'DFIs',
81
			'options'		=> 'Options'
82
		];
83
	}
84
	
85
	/**
86
	 * Get the current Admin tab
87
	 *
88
	 * @since 1.0.0
89
	 * @return string
90
	 */
91
	private function _get_current_tab() {
0 ignored issues
show
Coding Style introduced by
_get_current_tab uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
92
		return (isset($_GET['tab']) and $_GET['tab']) ? $_GET['tab'] : $this->_get_default_tab();
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
93
	}
94
	
95
	/**
96
	 * Retrieve default tab
97
	 *
98
	 * @since 1.0.0
99
	 * @return string
100
	 */
101
	private function _get_default_tab() {
102
		return 'dfis';
103
	}
104
105
	/**
106
	 * Get main setting options
107
	 *
108
	 * @since 1.0.0
109
	 * @return mixed
110
	 */
111
	private function _get_options() {
112
		return \get_option('wpdfi-settings');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
113
	}
114
115
	/**
116
	 * Get the name of current layout, it will be default or exist, depending on settings option
117
	 *
118
	 * @since 1.0.0
119
	 * @return string
120
	 */
121
	private function _get_layout_name() {
122
		return ($this->get_option($this->_get_current_tab())) ? 'exist' : 'default';
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
123
	}
124
	
125
	/**
126
	 * Update main setting options
127
	 *
128
	 * @param array $options
129
	 * @since 1.0.0
130
	 * @return boolean
131
	 */
132
	private function _update_options($options) {
133
		return \update_option('wpdfi-settings', $options);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
134
	}
135
136
	/**
137
	 * Update wpdfi settings option
138
	 *
139
	 * @since 1.0.0
140
	 * @return void
141
	 */
142
	public function update_settings() {
0 ignored issues
show
Coding Style introduced by
update_settings uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
update_settings uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
143
		if(isset($_GET['page']) and $_GET['page'] == 'wpdfi-settings.php' and isset($_POST['_wpnonce'])) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
144
            $nonce = $_POST['_wpnonce'];
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
145
            if ( ! \wp_verify_nonce( $nonce, 'wpdfi-settings-page' ) ) {
146
                // This nonce is not valid.
147
                return;
148
            } else {
149
            	// Dont need to store wp_nonce value
150
                unset($_POST['_wpnonce']);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
151
                unset($_POST['_wp_http_referer']);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
152
                
153
                $options = $this->_get_options();
154
                foreach($_POST as $key => $value) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
155
                	$options[$key] = $value;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
156
                }
157
158
                if($this->_update_options($options)) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
159
                	\wpdfi()->admin_notice->add('Settings Saved.', 'success');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
160
                } else {
161
                	\wpdfi()->admin_notice->add('Your options are still the same.', 'warning');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
162
                }
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
163
                
164
            }   
165
        }
166
	}	
167
168
	/**
169
	 * Get single setting option.
170
	 *
171
	 * @param string $option_key
172
	 * @param string $option_detail
173
	 * @since 1.0.0
174
	 * @return mixed(string/array)
0 ignored issues
show
Documentation introduced by
The doc-type mixed(string/array) could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
175
	 */
176
	public function get_option($option_key, $option_detail = null) {
177
		if($option_detail) return $this->_get_options()[$option_key][$option_detail];
0 ignored issues
show
Bug Best Practice introduced by
The expression $option_detail of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
introduced by
Space after opening control structure is required
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
178
		return $this->_get_options()[$option_key];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
179
	}
180
181
}