Completed
Push — master ( ffda11...ab397f )
by Stephanie
06:42
created

FrmAddon::set_license()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
			new FrmEDD_SL_Plugin_Updater( $this->store_url, $this->plugin_file, $api_data );
73
74
			add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) );
75
		}
76
	}
77
78
	public function get_license() {
79
		return trim( get_option( $this->option_name . 'key' ) );
80
	}
81
82
	public function set_license( $license ) {
83
		update_option( $this->option_name . 'key', $license );
84
	}
85
86
	public function clear_license() {
87
		delete_option( $this->option_name . 'active' );
88
		delete_option( $this->option_name . 'key' );
89
	}
90
91
	public function set_active( $is_active ) {
92
		update_option( $this->option_name . 'active', $is_active );
93
	}
94
95
	public function show_license_message( $file, $plugin ) {
96
		$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
97
		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">';
98
		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...
99
		$id = sanitize_title( $plugin['Name'] );
100
		echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '");if ( d !== null ){ d.className = d.className + " update"; }</script>';
101
		echo '</div></td></tr>';
102
	}
103
104
	public function clear_expired_download( $transient ) {
105
		if ( ! is_object($transient) ) {
106
			return $transient;
107
		}
108
109
		if ( $this->is_current_version( $transient ) ) {
110
			//make sure it doesn't show there is an update if plugin is up-to-date
111
			if ( isset( $transient->response[ $this->plugin_folder ] ) ) {
112
				unset( $transient->response[ $this->plugin_folder ] );
113
			}
114
		} else if ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) {
115
			$cache_key = 'edd_plugin_' . md5( sanitize_key( $this->license . $this->version ) . '_get_version' );
116
			$version_info = get_transient( $cache_key );
117
			if ( $version_info !== false ) {
118
				$transient->response[ $this->plugin_folder ] = $version_info;
119
			} else if ( ! $this->has_been_cleared() ) {
120
				// if the transient has expired, clear the update and trigger it again
121
				$this->cleared_plugins();
122
				$this->manually_queue_update();
123
				unset( $transient->response[ $this->plugin_folder ] );
124
			}
125
		}
126
127
		return $transient;
128
	}
129
130
	private function is_current_version( $transient ) {
131
		if ( empty( $transient->checked ) || ! isset( $transient->checked[ $this->plugin_folder ] ) ) {
132
			return false;
133
		}
134
135
		$response = ! isset( $transient->response ) || empty( $transient->response );
136
		if ( $response ) {
137
			return true;
138
		}
139
140
		return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] == $transient->response[ $this->plugin_folder ]->new_version;
141
	}
142
143
	private function has_been_cleared() {
144
		global $frm_vars;
145
		return isset( $frm_vars['cleared'] ) ? $frm_vars['cleared'] : false;
146
	}
147
148
	private function cleared_plugins() {
149
		global $frm_vars;
150
		$frm_vars['cleared'] = true;
151
	}
152
153
	public static function activate() {
154
	 	check_ajax_referer( 'frm_ajax', 'nonce' );
155
156
		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...
157
			wp_die( __( 'Oops! You forgot to enter your license number.', 'formidable' ) );
158
		}
159
160
		$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...
161
		$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...
162
		$this_plugin = self::get_addon( $plugin_slug );
163
		$this_plugin->set_license( $license );
164
165
		$response = array( 'success' => false, 'message' => '' );
166
		try {
167
			$license_data = $this_plugin->send_mothership_request( 'activate_license', $license );
168
169
			// $license_data->license will be either "valid" or "invalid"
170
			$is_valid = 'invalid';
171
			if ( is_array( $license_data ) ) {
172
				if ( $license_data['license'] == 'valid' ) {
173
					$is_valid = $license_data['license'];
174
					$response['message'] = __( 'Your license has been activated. Enjoy!', 'formidable' );
175
					$response['success'] = true;
176
				} else if ( $license_data['license'] == 'invalid' ) {
177
					$response['message'] = __( 'That license is invalid', 'formidable' );
178
				}
179
			} else if ( $license_data == 'expired' ) {
180
				$response['message'] = __( 'That license is expired', 'formidable' );
181
			} else if ( $license_data == 'no_activations_left' ) {
182
				$response['message'] = __( 'That license has been used too many times', 'formidable' );
183
			} else if ( $license_data == 'invalid_item_id' ) {
184
				$response['message'] = __( 'Oops! That is the wrong license number for this plugin.', 'formidable' );
185
			} else if ( $license_data == 'missing' ) {
186
				$response['message'] = __( 'That license is invalid', 'formidable' );
187
			} else {
188
				$response['message'] = FrmAppHelper::kses( $license_data, array( 'a' ) );
189
			}
190
191
			$this_plugin->set_active( $is_valid );
192
		} catch ( Exception $e ) {
193
			$response['message'] = $e->getMessage();
194
		}
195
196
		echo json_encode( $response );
197
		wp_die();
198
	}
199
200
	public static function deactivate() {
201
		check_ajax_referer( 'frm_ajax', 'nonce' );
202
203
		$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...
204
		$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...
205
		$this_plugin = self::get_addon( $plugin_slug );
206
207
		$response = array( 'success' => false, 'message' => '' );
208
		try {
209
			// $license_data->license will be either "deactivated" or "failed"
210
			$license_data = $this_plugin->send_mothership_request( 'deactivate_license', $license );
211
			if ( is_array( $license_data ) && $license_data['license'] == 'deactivated' ) {
212
				$response['success'] = true;
213
				$response['message'] = __( 'That license was removed successfully', 'helpdesk' );
214
			} else {
215
				$response['message'] = __( 'There was an error deactivating your license.', 'formidable' );
216
			}
217
		} catch ( Exception $e ) {
218
			$response['message'] = $e->getMessage();
219
		}
220
221
		$this_plugin->clear_license();
222
223
		echo json_encode( $response );
224
		wp_die();
225
	}
226
227
	public function send_mothership_request( $action, $license ) {
228
		$api_params = array(
229
			'edd_action' => $action,
230
			'license'    => $license,
231
			'item_name'  => urlencode( $this->plugin_name ),
232
			'url'        => home_url(),
233
		);
234
		if ( is_numeric( $this->download_id ) ) {
235
			$api_params['item_id'] = absint( $this->download_id );
236
		}
237
238
		$arg_array = array(
239
			'body'      => $api_params,
240
			'timeout'   => 15,
241
			'sslverify' => false,
242
			'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ),
243
		);
244
245
		$resp = wp_remote_post( $this->store_url, $arg_array );
246
		$body = wp_remote_retrieve_body( $resp );
247
248
		$message = __( 'Your License Key was invalid', 'formidable' );
249
		if ( is_wp_error( $resp ) ) {
250
			$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>');
251
			if ( is_wp_error( $resp ) ) {
252
				$message .= ' '. $resp->get_error_message();
253
			}
254
		} else if ( $body == 'error' || is_wp_error( $body ) ) {
255
			$message = __( 'You had an HTTP error connecting to Formidable Pro\'s API', 'formidable' );
256
		} else {
257
			$json_res = json_decode( $body, true );
258
			if ( null !== $json_res ) {
259
				if ( is_array( $json_res ) && isset( $json_res['error'] ) ) {
260
					$message = $json_res['error'];
261
				} else {
262
					$message = $json_res;
263
				}
264
			} else if ( isset( $resp['response'] ) && isset( $resp['response']['code'] ) ) {
265
				$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...
266
			}
267
		}
268
269
		return $message;
270
	}
271
272
    public function manually_queue_update(){
273
        set_site_transient( 'update_plugins', null );
274
    }
275
}
276