-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wps-wocuf-pro-update.php
325 lines (266 loc) · 8.96 KB
/
class-wps-wocuf-pro-update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* The update-specific functionality of the plugin.
*
* @link https://wpswings.com/?utm_source=wpswings-official&utm_medium=upsell-pro-backend&utm_campaign=official
* @since 1.0.0
*
* @package woocommerce-one-click-upsell-funnel-pro
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'WPS_Wocuf_Pro_Update' ) ) {
/**
* Update Handler class.
*/
class WPS_Wocuf_Pro_Update {
/**
* Constructer.
*/
public function __construct() {
register_activation_hook( WPS_WOCUF_PRO_BASE_FILE, array( $this, 'wps_check_activation' ) );
add_action( 'wps_wocuf_pro_check_event', array( $this, 'wps_check_update' ) );
add_filter( 'http_request_args', array( $this, 'wps_updates_exclude' ), 5, 2 );
register_deactivation_hook( WPS_WOCUF_PRO_BASE_FILE, array( $this, 'wps_check_deactivation' ) );
$plugin_update = get_option( 'wps_wocuf_plugin_update', 'false' );
if ( 'true' === $plugin_update ) {
// To add view details content in plugin update notice on plugins page.
add_action( 'install_plugins_pre_plugin-information', array( $this, 'wps_wocuf_details' ) );
// To add plugin update notice after plugin update message.
add_action( 'in_plugin_update_message-woocommerce-one-click-upsell-funnel-pro/woocommerce-one-click-upsell-funnel-pro.php', array( $this, 'wps_wocuf_in_plugin_update_notice' ), 10, 2 );
}
}
/**
* Deactivation Checkup Event.
*/
public function wps_check_deactivation() {
wp_clear_scheduled_hook( 'wps_wocuf_pro_check_event' );
}
/**
* Activation Checkup Event.
*/
public function wps_check_activation() {
wp_schedule_event( time(), 'daily', 'wps_wocuf_pro_check_event' );
}
/**
* Update Checkup Event.
*/
public function wps_wocuf_details() {
global $tab;
$secure_nonce = wp_create_nonce( 'wps-upsell-auth-nonce' );
$id_nonce_verified = wp_verify_nonce( $secure_nonce, 'wps-upsell-auth-nonce' );
if ( ! $id_nonce_verified ) {
wp_die( esc_html__( 'Nonce Not verified', 'one-click-upsell-funnel-for-woocommerce-pro' ) );
}
// change $_REQUEST['plugin] to your plugin slug name.
if ( 'plugin-information' === $tab && ! empty( $_REQUEST['plugin'] ) && 'woocommerce-one-click-upsell-funnel-pro' === $_REQUEST['plugin'] ) {
$data = $this->get_plugin_update_data();
if ( is_wp_error( $data ) || empty( $data ) ) {
return;
}
if ( empty( $data['response']['code'] ) || 200 !== $data['response']['code'] ) {
return;
}
if ( ! empty( $data['body'] ) ) {
$all_data = json_decode( $data['body'], true );
if ( ! empty( $all_data ) && is_array( $all_data ) ) {
$this->create_html_data( $all_data );
wp_die();
}
}
}
}
/**
* Update Checkup Event.
*/
public function get_plugin_update_data() {
// replace with your plugin url.
$url = 'https://wpswings.com/pluginupdates/woocommerce-one-click-upsell-funnel-pro/update.php';
$postdata = array(
'action' => 'check_update',
'license_code' => WPS_WOCUF_PRO_LICENSE_KEY,
);
$args = array(
'method' => 'POST',
'body' => $postdata,
);
$data = wp_remote_post( $url, $args );
return $data;
}
/**
* Render HTML content.
*
* @param array $all_data complete banner data.
*/
public function create_html_data( $all_data ) {
?>
<style>
#TB_window{
top : 4% !important;
}
.wps_wocuf_banner > img {
width: 50%;
}
.wps_wocuf_banner > h1 {
margin-top: 0px;
}
.wps_wocuf_banner {
text-align: center;
}
.wps_wocuf_description > h4 {
background-color: #3779B5;
padding: 5px;
color: #ffffff;
border-radius: 5px;
}
.wps_wocuf_changelog_details > h4 {
background-color: #3779B5;
padding: 5px;
color: #ffffff;
border-radius: 5px;
}
</style>
<?php
$plugin_name = ! empty( $all_data['name'] ) ? $all_data['name'] : '';
$plugin_version = ! empty( $all_data['version'] ) ? $all_data['version'] : '';
$plugin_logo = ! empty( $all_data['banners']['logo'] ) ? $all_data['banners']['logo'] : '';
$plugin_description = ! empty( $all_data['sections']['description'] ) ? $all_data['sections']['description'] : '';
$plugin_changelog = ! empty( $all_data['sections']['changelog'] ) ? $all_data['sections']['changelog'] : '';
?>
<div class="wps_wocuf_details_wrapper">
<div class="wps_wocuf_banner">
<h1><?php echo esc_html( $plugin_name ) . ' ' . esc_html( $plugin_version ); ?></h1>
<img src="<?php echo esc_url( $plugin_logo ); ?>">
</div>
<div class="wps_wocuf_description">
<h4><?php esc_html_e( 'Plugin Description', 'one-click-upsell-funnel-for-woocommerce-pro' ); ?></h4>
<span><?php echo wp_kses_post( $plugin_description ); ?></span>
</div>
<div class="wps_wocuf_changelog_details">
<h4><?php esc_html_e( 'Plugin Change Log', 'one-click-upsell-funnel-for-woocommerce-pro' ); ?></h4>
<span><?php echo wp_kses_post( $plugin_changelog ); ?></span>
</div>
</div>
<?php
}
/**
* Render update notice content.
*/
public function wps_wocuf_in_plugin_update_notice() {
$data = $this->get_plugin_update_data();
if ( is_wp_error( $data ) || empty( $data ) ) {
return;
}
if ( empty( $data['response']['code'] ) || 200 !== $data['response']['code'] ) {
return false;
}
if ( isset( $data['body'] ) ) {
$all_data = json_decode( $data['body'], true );
if ( is_array( $all_data ) && ! empty( $all_data['sections']['update_notice'] ) ) {
?>
<style type="text/css">
#woocommerce-one-click-upsell-funnel-pro-update .dummy {
display: none;
}
#wps_wocuf_in_plugin_update_div p:before {
content: none;
}
#wps_wocuf_in_plugin_update_div {
border-top: 1px solid #ffb900;
margin-left: -13px;
padding-left: 20px;
padding-top: 10px;
padding-bottom: 5px;
}
#wps_wocuf_in_plugin_update_div ul {
list-style-type: decimal;
padding-left: 20px;
}
</style>
<?php
echo '</p><div id="wps_wocuf_in_plugin_update_div">' . wp_kses_post( $all_data['sections']['update_notice'] ) . '</div><p class="dummy">';
}
}
}
/**
* Event: update.
*/
public function wps_check_update() {
global $wp_version;
$update_check_wocuf = 'https://wpswings.com/pluginupdates/woocommerce-one-click-upsell-funnel-pro/update.php';
$plugin_folder = plugin_basename( dirname( WPS_WOCUF_PRO_BASE_FILE ) );
$plugin_file = basename( ( WPS_WOCUF_PRO_BASE_FILE ) );
if ( defined( 'WP_INSTALLING' ) ) {
return false;
}
$postdata = array(
'action' => 'check_update',
'license_key' => WPS_WOCUF_PRO_LICENSE_KEY,
);
$args = array(
'method' => 'POST',
'body' => $postdata,
);
$response = wp_remote_post( $update_check_wocuf, $args );
if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
return;
}
if ( empty( $response['response']['code'] ) || 200 !== $response['response']['code'] ) {
update_option( 'wps_wocuf_plugin_update', 'false' );
return false;
}
list($version, $url) = explode( '~', $response['body'] );
if ( $this->wps_plugin_get( 'Version' ) >= $version ) {
update_option( 'wps_wocuf_plugin_update', 'false' );
return false;
}
update_option( 'wps_wocuf_plugin_update', 'true' );
$plugin_transient = get_site_transient( 'update_plugins' );
$a = array(
'slug' => $plugin_folder,
'new_version' => $version,
'url' => $this->wps_plugin_get( 'AuthorURI' ),
'package' => $url,
);
$o = (object) $a;
$plugin_transient->response[ $plugin_folder . '/' . $plugin_file ] = $o;
set_site_transient( 'update_plugins', $plugin_transient );
}
/**
* Exclude update notice content.
*
* @param array $r request.
* @param string $url request url.
*/
public function wps_updates_exclude( $r, $url ) {
if ( 0 !== strpos( $url, 'http://api.wordpress.org/plugins/update-check' ) ) {
return $r;
}
$plugins = unserialize( $r['body']['plugins'] ); //phpcs:ignore
if ( ! empty( $plugins->plugins ) ) {
unset( $plugins->plugins[ plugin_basename( __FILE__ ) ] );
}
if ( ! empty( $plugins->active ) ) {
unset( $plugins->active[ array_search( plugin_basename( __FILE__ ), $plugins->active, true ) ] );
}
$r['body']['plugins'] = serialize( $plugins ); //phpcs:ignore
return $r;
}
/**
* Returns current plugin info.
*
* @param string $i index.
*/
public function wps_plugin_get( $i ) {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_folder = get_plugins( '/' . plugin_basename( dirname( WPS_WOCUF_PRO_BASE_FILE ) ) );
$plugin_file = basename( ( WPS_WOCUF_PRO_BASE_FILE ) );
return $plugin_folder[ $plugin_file ][ $i ];
}
}
new WPS_Wocuf_Pro_Update();
}
?>