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
|
|
|
* Initialize the plugin by setting localization, filters, and administration functions. |
64
|
|
|
* |
65
|
|
|
* @since 1.0.0 |
66
|
|
|
* |
67
|
|
|
* @access private |
68
|
|
|
*/ |
69
|
|
|
public function __construct() { |
70
|
|
|
|
71
|
|
|
add_action( 'admin_init', array( $this, 'compatible_version_check' ) ); |
72
|
|
|
|
73
|
|
|
// Don't run anything else in the plugin, if we're on an incompatible PHP version |
74
|
|
|
if ( ! self::compatible_version() ) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->set_variables(); |
79
|
|
|
|
80
|
|
|
add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* On plugin activation |
85
|
|
|
* |
86
|
|
|
* @since 1.0.0 |
87
|
|
|
*/ |
88
|
|
|
public static function register_activation_hook() { |
89
|
|
|
self::compatible_version_check_on_activation(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check if the PHP version is compatible. |
94
|
|
|
* |
95
|
|
|
* @since 1.0.0 |
96
|
|
|
*/ |
97
|
|
|
public static function compatible_version() { |
98
|
|
|
if ( version_compare( PHP_VERSION, '5.6', '<' ) ) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* The backup sanity check, in case the plugin is activated in a weird way, |
107
|
|
|
* or the versions change after activation. |
108
|
|
|
* |
109
|
|
|
* @since 1.0.0 |
110
|
|
|
*/ |
111
|
|
|
public function compatible_version_check() { |
112
|
|
|
if ( ! self::compatible_version() ) { |
113
|
|
|
if ( is_plugin_active( plugin_basename( WETU_IMPORTER_CORE ) ) ) { |
114
|
|
|
deactivate_plugins( plugin_basename( WETU_IMPORTER_CORE ) ); |
115
|
|
|
add_action( 'admin_notices', array( $this, 'compatible_version_notice' ) ); |
116
|
|
|
|
117
|
|
|
if ( isset( $_GET['activate'] ) ) { |
118
|
|
|
unset( $_GET['activate'] ); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Display the notice related with the older version from PHP. |
126
|
|
|
* |
127
|
|
|
* @since 1.0.0 |
128
|
|
|
*/ |
129
|
|
|
public function compatible_version_notice() { |
130
|
|
|
$class = 'notice notice-error'; |
131
|
|
|
$message = esc_html__( 'Wetu Importer Plugin requires PHP 5.6 or higher.', 'wetu-importer' ); |
132
|
|
|
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_html( $class ), esc_html( $message ) ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* The primary sanity check, automatically disable the plugin on activation if it doesn't |
137
|
|
|
* meet minimum requirements. |
138
|
|
|
* |
139
|
|
|
* @since 1.0.0 |
140
|
|
|
*/ |
141
|
|
|
public static function compatible_version_check_on_activation() { |
142
|
|
|
if ( ! self::compatible_version() ) { |
143
|
|
|
deactivate_plugins( plugin_basename( WETU_IMPORTER_CORE ) ); |
144
|
|
|
wp_die( esc_html__( 'Wetu Importer Plugin requires PHP 5.6 or higher.', 'wetu-importer' ) ); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Sets the variables used throughout the plugin. |
150
|
|
|
*/ |
151
|
|
|
public function set_variables() { |
152
|
|
|
$temp_options = get_option('_lsx-to_settings',false); |
153
|
|
|
|
154
|
|
|
if(isset($temp_options[$this->plugin_slug])) { |
155
|
|
|
$this->options = $temp_options[$this->plugin_slug]; |
156
|
|
|
|
157
|
|
|
$this->api_key = false; |
158
|
|
|
if (false !== $temp_options) { |
159
|
|
|
if (isset($temp_options['api']['wetu_api_key']) && '' !== $temp_options['api']['wetu_api_key']) { |
160
|
|
|
$this->api_key = $temp_options['api']['wetu_api_key']; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (isset($temp_options[$this->plugin_slug]) && !empty($temp_options[$this->plugin_slug]) && isset($this->options['image_scaling'])) { |
164
|
|
|
$this->scale_images = true; |
165
|
|
|
$width = '800'; |
166
|
|
|
if (isset($this->options['width']) && '' !== $this->options['width']) { |
167
|
|
|
$width = $this->options['width']; |
168
|
|
|
} |
169
|
|
|
$height = '600'; |
170
|
|
View Code Duplication |
if (isset($this->options['height']) && '' !== $this->options['height']) { |
|
|
|
|
171
|
|
|
$height = $this->options['height']; |
172
|
|
|
} |
173
|
|
|
$cropping = 'raw'; |
174
|
|
View Code Duplication |
if (isset($this->options['cropping']) && '' !== $this->options['cropping']) { |
|
|
|
|
175
|
|
|
$cropping = $this->options['cropping']; |
176
|
|
|
} |
177
|
|
|
$this->image_scaling_url = 'https://wetu.com/ImageHandler/' . $cropping . $width . 'x' . $height . '/'; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Load the plugin text domain for translation. |
185
|
|
|
* |
186
|
|
|
* @since 1.0.0 |
187
|
|
|
*/ |
188
|
|
|
public function load_plugin_textdomain() { |
189
|
|
|
load_plugin_textdomain( 'wetu-importer', FALSE, basename( WETU_IMPORTER_PATH ) . '/languages'); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
$wetu_importer = new WETU_Importer(); |
193
|
|
|
|
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.