Completed
Push — master ( 9b6d37...f2c5e0 )
by Stephanie
23:24 queued 06:05
created

FrmAddon::deactivate()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.5806
cc 4
eloc 18
nc 7
nop 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	die( 'You are not allowed to call this page directly.' );
5
}
6
7
class FrmAddon {
8
	public $store_url = 'https://formidablepro.com';
9
	public $download_id;
10
	public $plugin_file;
11
	public $plugin_folder;
12
	public $plugin_name;
13
	public $plugin_slug;
14
	public $option_name;
15
	public $version;
16
	public $author = 'Strategy11';
17
	private $license;
18
19
	public function __construct() {
20
21
		if ( empty( $this->plugin_slug ) ) {
22
			$this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) );
23
		}
24
		if ( empty( $this->option_name ) ) {
25
			$this->option_name = 'edd_' . $this->plugin_slug . '_license_';
26
		}
27
28
		$this->plugin_folder = plugin_basename( $this->plugin_file );
29
		$this->license = $this->get_license();
30
31
		add_filter( 'frm_installed_addons', array( &$this, 'insert_installed_addon' ) );
32
		$this->edd_plugin_updater();
33
	}
34
35
	public static function load_hooks() {
36
		add_filter( 'frm_include_addon_page', '__return_true' );
37
		//new static();
38
	}
39
40
	public function insert_installed_addon( $plugins ) {
41
		$plugins[ $this->plugin_slug ] = $this;
42
		return $plugins;
43
	}
44
45
	public static function get_addon( $plugin_slug ) {
46
		$plugins = apply_filters( 'frm_installed_addons', array() );
47
		$plugin = false;
48
		if ( isset( $plugins[ $plugin_slug ] ) ) {
49
			$plugin = $plugins[ $plugin_slug ];
50
		}
51
		return $plugin;
52
	}
53
54
	public function edd_plugin_updater() {
55
56
		$license = $this->license;
57
58
		if ( empty( $license ) ) {
59
			add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'show_license_message' ), 10, 2 );
60
		} else {
61
62
			// setup the updater
63
			$api_data = array(
64
				'version' 	=> $this->version,
65
				'license' 	=> $license,
66
				'author' 	=> $this->author,
67
			);
68
			if ( is_numeric( $this->download_id ) ) {
69
				$api_data['item_id'] = $this->download_id;
70
			}
71
72
			$edd = new FrmEDD_SL_Plugin_Updater( $this->store_url, $this->plugin_file, $api_data );
73
			if ( $this->plugin_folder == 'formidable/formidable.php' ) {
1 ignored issue
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
74
				remove_filter( 'plugins_api', array( $edd, 'plugins_api_filter' ), 10, 3 );
75
			}
76
77
			add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) );
78
		}
79
	}
80
81
	public function get_license() {
82
		return trim( get_option( $this->option_name . 'key' ) );
83
	}
84
85
	public function set_license( $license ) {
86
		update_option( $this->option_name . 'key', $license );
87
	}
88
89
	public function clear_license() {
90
		delete_option( $this->option_name . 'active' );
91
		delete_option( $this->option_name . 'key' );
92
	}
93
94
	public function set_active( $is_active ) {
95
		update_option( $this->option_name . 'active', $is_active );
96
	}
97
98
	public function show_license_message( $file, $plugin ) {
99
		$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
100
		echo '<tr class="plugin-update-tr active"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message">';
101
		echo sprintf( __( 'Your %1$s license key is missing. Please add it on the %2$slicenses page%3$s.', 'formidable' ), $this->plugin_name, '<a href="' . esc_url( admin_url('admin.php?page=formidable-settings&t=licenses_settings' ) ) . '">', '</a>' );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
102
		$id = sanitize_title( $plugin['Name'] );
103
		echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '");if ( d !== null ){ d.className = d.className + " update"; }</script>';
104
		echo '</div></td></tr>';
105
	}
106
107
	public function clear_expired_download( $transient ) {
108
		if ( ! is_object( $transient ) ) {
109
			return $transient;
110
		}
111
112
		if ( $this->is_current_version( $transient ) ) {
113
			//make sure it doesn't show there is an update if plugin is up-to-date
114
			if ( isset( $transient->response[ $this->plugin_folder ] ) ) {
115
				unset( $transient->response[ $this->plugin_folder ] );
116
			}
117
		} else if ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) {
118
			$cache_key = 'edd_plugin_' . md5( sanitize_key( $this->license . $this->version ) . '_get_version' );
119
			$version_info = get_transient( $cache_key );
120
			if ( $version_info !== false ) {
121
				$transient->response[ $this->plugin_folder ] = $version_info;
122
			} else {
123
				if ( ! $this->has_been_cleared() ) {
124
					// if the transient has expired, clear the update and trigger it again
125
					$this->cleared_plugins();
126
					$this->manually_queue_update();
127
				}
128
129
				unset( $transient->response[ $this->plugin_folder ] );
130
			}
131
		}
132
133
		return $transient;
134
	}
135
136
	private function is_current_version( $transient ) {
137
		if ( empty( $transient->checked ) || ! isset( $transient->checked[ $this->plugin_folder ] ) ) {
138
			return false;
139
		}
140
141
		$response = ! isset( $transient->response ) || empty( $transient->response );
142
		if ( $response ) {
143
			return true;
144
		}
145
146
		return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] == $transient->response[ $this->plugin_folder ]->new_version;
147
	}
148
149
	private function has_been_cleared() {
150
		$last_cleared = get_option( 'frm_last_cleared' );
151
		return ( $last_cleared < date( 'Y-m-d H:i:s', strtotime('-5 minutes') ) );
152
	}
153
154
	private function cleared_plugins() {
155
		update_option( 'frm_last_cleared', date('Y-m-d H:i:s') );
156
	}
157
158
	public static function activate() {
159
	 	check_ajax_referer( 'frm_ajax', 'nonce' );
160
161
		if ( ! isset( $_POST['license'] ) || empty( $_POST['license'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
162
			wp_die( __( 'Oops! You forgot to enter your license number.', 'formidable' ) );
163
		}
164
165
		$license = stripslashes( sanitize_text_field( $_POST['license'] ) );
1 ignored issue
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
166
		$plugin_slug = sanitize_text_field( $_POST['plugin'] );
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-validated input variable: $_POST
Loading history...
167
		$this_plugin = self::get_addon( $plugin_slug );
168
		$this_plugin->set_license( $license );
169
170
		$response = array( 'success' => false, 'message' => '' );
171
		try {
172
			$license_data = $this_plugin->send_mothership_request( 'activate_license', $license );
173
174
			// $license_data->license will be either "valid" or "invalid"
175
			$is_valid = 'invalid';
176
			if ( is_array( $license_data ) ) {
177
				if ( $license_data['license'] == 'valid' ) {
178
					$is_valid = $license_data['license'];
179
					$response['message'] = __( 'Your license has been activated. Enjoy!', 'formidable' );
180
					$response['success'] = true;
181
				} else if ( $license_data['license'] == 'invalid' ) {
182
					$response['message'] = __( 'That license is invalid', 'formidable' );
183
				}
184
			} else if ( $license_data == 'expired' ) {
185
				$response['message'] = __( 'That license is expired', 'formidable' );
186
			} else if ( $license_data == 'no_activations_left' ) {
187
				$response['message'] = __( 'That license has been used too many times', 'formidable' );
188
			} else if ( $license_data == 'invalid_item_id' ) {
189
				$response['message'] = __( 'Oops! That is the wrong license number for this plugin.', 'formidable' );
190
			} else if ( $license_data == 'missing' ) {
191
				$response['message'] = __( 'That license is invalid', 'formidable' );
192
			} else {
193
				$response['message'] = FrmAppHelper::kses( $license_data, array( 'a' ) );
194
			}
195
196
			$this_plugin->set_active( $is_valid );
197
		} catch ( Exception $e ) {
198
			$response['message'] = $e->getMessage();
199
		}
200
201
		echo json_encode( $response );
202
		wp_die();
203
	}
204
205
	public static function deactivate() {
206
		check_ajax_referer( 'frm_ajax', 'nonce' );
207
208
		$license = stripslashes( sanitize_text_field( $_POST['license'] ) );
0 ignored issues
show
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
209
		$plugin_slug = sanitize_text_field( $_POST['plugin'] );
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-validated input variable: $_POST
Loading history...
210
		$this_plugin = self::get_addon( $plugin_slug );
211
212
		$response = array( 'success' => false, 'message' => '' );
213
		try {
214
			// $license_data->license will be either "deactivated" or "failed"
215
			$license_data = $this_plugin->send_mothership_request( 'deactivate_license', $license );
216
			if ( is_array( $license_data ) && $license_data['license'] == 'deactivated' ) {
217
				$response['success'] = true;
218
				$response['message'] = __( 'That license was removed successfully', 'formidable' );
219
			} else {
220
				$response['message'] = __( 'There was an error deactivating your license.', 'formidable' );
221
			}
222
		} catch ( Exception $e ) {
223
			$response['message'] = $e->getMessage();
224
		}
225
226
		$this_plugin->clear_license();
227
228
		echo json_encode( $response );
229
		wp_die();
230
	}
231
232
	public function send_mothership_request( $action, $license ) {
233
		$api_params = array(
234
			'edd_action' => $action,
235
			'license'    => $license,
236
			'item_name'  => urlencode( $this->plugin_name ),
237
			'url'        => home_url(),
238
		);
239
		if ( is_numeric( $this->download_id ) ) {
240
			$api_params['item_id'] = absint( $this->download_id );
241
		}
242
243
		$arg_array = array(
244
			'body'      => $api_params,
245
			'timeout'   => 15,
246
			'sslverify' => false,
247
			'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ),
248
		);
249
250
		$resp = wp_remote_post( $this->store_url, $arg_array );
251
		$body = wp_remote_retrieve_body( $resp );
252
253
		$message = __( 'Your License Key was invalid', 'formidable' );
254
		if ( is_wp_error( $resp ) ) {
255
			$message = sprintf( __( 'You had an error communicating with Formidable Pro\'s API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="http://formidablepro.com/knowledgebase/why-cant-i-activate-formidable-pro/" target="_blank">', '</a>');
256
			if ( is_wp_error( $resp ) ) {
257
				$message .= ' '. $resp->get_error_message();
258
			}
259
		} else if ( $body == 'error' || is_wp_error( $body ) ) {
260
			$message = __( 'You had an HTTP error connecting to Formidable Pro\'s API', 'formidable' );
261
		} else {
262
			$json_res = json_decode( $body, true );
263
			if ( null !== $json_res ) {
264
				if ( is_array( $json_res ) && isset( $json_res['error'] ) ) {
265
					$message = $json_res['error'];
266
				} else {
267
					$message = $json_res;
268
				}
269
			} else if ( isset( $resp['response'] ) && isset( $resp['response']['code'] ) ) {
270
				$message = sprintf( __( 'There was a %1$s error: %2$s', 'formidable' ), $resp['response']['code'], $resp['response']['message'] .' '. $resp['body'] );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$resp'
Loading history...
271
			}
272
		}
273
274
		return $message;
275
	}
276
277
    public function manually_queue_update(){
278
        set_site_transient( 'update_plugins', null );
279
    }
280
}
281