Passed
Push — master ( 8c8e21...c4fc51 )
by Fernando
10:52
created

WETU_Importer::set_variables()   D

Complexity

Conditions 18
Paths 74

Size

Total Lines 39
Code Lines 26

Duplication

Lines 6
Ratio 15.38 %

Importance

Changes 0
Metric Value
dl 6
loc 39
rs 4.947
c 0
b 0
f 0
cc 18
eloc 26
nc 74
nop 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
2
/**
3
 * @package   WETU_Importer
4
 * @author    LightSpeed
5
 * @license   GPL-2.0+
6
 * @link      
7
 * @copyright 2016 LightSpeed
8
 **/
9
10
class WETU_Importer {
11
	
12
	/**
13
	 * Holds class instance
14
	 *
15
	 * @since 1.0.0
16
	 *
17
	 * @var      object|Module_Template
18
	 */
19
	protected static $instance = null;
20
21
	/**
22
	 * The slug for this plugin
23
	 *
24
	 * @since 0.0.1
25
	 *
26
	 * @var      string
27
	 */
28
	public $plugin_slug = 'wetu-importer';
29
30
	/**
31
	 * The options for the plugin
32
	 *
33
	 * @since 0.0.1
34
	 *
35
	 * @var      string
36
	 */
37
	public $options = false;
38
39
	/**
40
	 * The url to import images from WETU
41
	 *
42
	 * @since 0.0.1
43
	 *
44
	 * @var      string
45
	 */
46
	public $import_scaling_url = false;		
47
48
	/**
49
	 * scale the images on import or not
50
	 *
51
	 * @since 0.0.1
52
	 *
53
	 * @var      boolean
54
	 */
55
	public $scale_images = false;
56
57
	/**
58
	 * The WETU API Key
59
	 */
60
	public $api_key = false;
61
62
	/**
63
	 * The WETU API Username
64
	 */
65
	public $api_username = false;
66
67
	/**
68
	 * The WETU API Password
69
	 */
70
	public $api_password = false;
71
	
72
	/**
73
	 * Initialize the plugin by setting localization, filters, and administration functions.
74
	 *
75
	 * @since 1.0.0
76
	 *
77
	 * @access private
78
	 */
79
	public function __construct() {
80
81
		add_action( 'admin_init', array( $this, 'compatible_version_check' ) );
82
83
		// Don't run anything else in the plugin, if we're on an incompatible PHP version
84
		if ( ! self::compatible_version() ) {
85
			return;
86
		}
87
88
		$this->set_variables();
89
90
		add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
91
	}
92
	
93
	/**
94
	 * On plugin activation
95
	 *
96
	 * @since 1.0.0
97
	 */
98
	public static function register_activation_hook() {
99
		self::compatible_version_check_on_activation();
100
	}
101
	
102
	/**
103
	 * Check if the PHP version is compatible.
104
	 *
105
	 * @since 1.0.0
106
	 */
107
	public static function compatible_version() {
108
		if ( version_compare( PHP_VERSION, '5.6', '<' ) ) {
109
			return false;
110
		}
111
112
		return true;
113
	}
114
	
115
	/**
116
	 * The backup sanity check, in case the plugin is activated in a weird way,
117
	 * or the versions change after activation.
118
	 *
119
	 * @since 1.0.0
120
	 */
121
	public function compatible_version_check() {
122
		if ( ! self::compatible_version() ) {
123
			if ( is_plugin_active( plugin_basename( WETU_IMPORTER_CORE ) ) ) {
124
				deactivate_plugins( plugin_basename( WETU_IMPORTER_CORE ) );
125
				add_action( 'admin_notices', array( $this, 'compatible_version_notice' ) );
126
				
127
				if ( isset( $_GET['activate'] ) ) {
128
					unset( $_GET['activate'] );
129
				}
130
			}
131
		}
132
	}
133
	
134
	/**
135
	 * Display the notice related with the older version from PHP.
136
	 *
137
	 * @since 1.0.0
138
	 */
139
	public function compatible_version_notice() {
140
		$class = 'notice notice-error';
141
		$message = esc_html__( 'Wetu Importer Plugin requires PHP 5.6 or higher.', 'wetu-importer' );
142
		printf( '<div class="%1$s"><p>%2$s</p></div>', esc_html( $class ), esc_html( $message ) );
143
	}
144
	
145
	/**
146
	 * The primary sanity check, automatically disable the plugin on activation if it doesn't
147
	 * meet minimum requirements.
148
	 *
149
	 * @since 1.0.0
150
	 */
151
	public static function compatible_version_check_on_activation() {
152
		if ( ! self::compatible_version() ) {
153
			deactivate_plugins( plugin_basename( WETU_IMPORTER_CORE ) );
154
			wp_die( esc_html__( 'Wetu Importer Plugin requires PHP 5.6 or higher.', 'wetu-importer' ) );
155
		}
156
	}
157
158
	/**
159
	 * Sets the variables used throughout the plugin.
160
	 */
161
	public function set_variables() {
162
		$temp_options = get_option('_lsx-to_settings',false);
163
164
		if(isset($temp_options[$this->plugin_slug])) {
165
			$this->options = $temp_options[$this->plugin_slug];
166
167
			$this->api_key = false;
168
			$this->api_username = false;
169
			$this->api_password = false;
170
			if (false !== $temp_options) {
171
				if (isset($temp_options['api']['wetu_api_key']) && '' !== $temp_options['api']['wetu_api_key']) {
172
					$this->api_key = $temp_options['api']['wetu_api_key'];
173
				}
174
				if (isset($temp_options['api']['wetu_api_username']) && '' !== $temp_options['api']['wetu_api_username']) {
175
					$this->api_username = $temp_options['api']['wetu_api_username'];
176
				}
177
				if (isset($temp_options['api']['wetu_api_password']) && '' !== $temp_options['api']['wetu_api_password']) {
178
					$this->api_password = $temp_options['api']['wetu_api_password'];
179
				}
180
181
				if (isset($temp_options[$this->plugin_slug]) && !empty($temp_options[$this->plugin_slug]) && isset($this->options['image_scaling'])) {
182
					$this->scale_images = true;
183
					$width = '800';
184
					if (isset($this->options['width']) && '' !== $this->options['width']) {
185
						$width = $this->options['width'];
186
					}
187
					$height = '600';
188 View Code Duplication
					if (isset($this->options['height']) && '' !== $this->options['height']) {
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...
189
						$height = $this->options['height'];
190
					}
191
					$cropping = 'raw';
192 View Code Duplication
					if (isset($this->options['cropping']) && '' !== $this->options['cropping']) {
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...
193
						$cropping = $this->options['cropping'];
194
					}
195
					$this->image_scaling_url = 'https://wetu.com/ImageHandler/' . $cropping . $width . 'x' . $height . '/';
0 ignored issues
show
Bug introduced by
The property image_scaling_url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
196
				}
197
			}
198
		}
199
	}
200
	
201
	/**
202
	 * Load the plugin text domain for translation.
203
	 *
204
	 * @since 1.0.0
205
	 */
206
	public function load_plugin_textdomain() {
207
		load_plugin_textdomain( 'wetu-importer', FALSE, basename( WETU_IMPORTER_PATH ) . '/languages');
208
	}
209
}
210
$wetu_importer = new WETU_Importer();
211