Completed
Push — master ( 524ea0...3594e3 )
by Stephanie
03:55
created

FrmAddon::load_hooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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_name;
12
	public $plugin_slug;
13
	public $option_name;
14
	public $version;
15
	public $author = 'Strategy11';
16
17
	public function __construct() {
18
19
		if ( empty( $this->plugin_slug ) ) {
20
			$this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) );
21
		}
22
		if ( empty( $this->option_name ) ) {
23
			$this->option_name = 'edd_' . $this->plugin_slug . '_license_';
24
		}
25
26
		add_filter( 'frm_installed_addons', array( &$this, 'insert_installed_addon' ) );
27
		$this->edd_plugin_updater();
28
	}
29
30
	public static function load_hooks() {
31
		add_filter( 'frm_include_addon_page', '__return_true' );
32
		//new static();
33
	}
34
35
	public function insert_installed_addon( $plugins ) {
36
		$plugins[ $this->plugin_slug ] = $this;
37
		return $plugins;
38
	}
39
40
	public static function get_addon( $plugin_slug ) {
41
		$plugins = apply_filters( 'frm_installed_addons', array() );
42
		$plugin = false;
43
		if ( isset( $plugins[ $plugin_slug ] ) ) {
44
			$plugin = $plugins[ $plugin_slug ];
45
		}
46
		return $plugin;
47
	}
48
49
	public function edd_plugin_updater() {
50
51
		// retrieve our license key from the DB
52
		$license = trim( get_option( $this->option_name . 'key' ) );
53
54
		if ( empty( $license ) ) {
55
			add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'show_license_message' ), 10, 2 );
56
		} else {
57
			if ( ! class_exists('EDD_SL_Plugin_Updater') ) {
58
				include( dirname( __FILE__ ) . '/EDD_SL_Plugin_Updater.php' );
59
			}
60
61
			// setup the updater
62
			$api_data = array(
63
				'version' 	=> $this->version,
64
				'license' 	=> $license,
65
				'author' 	=> $this->author,
66
			);
67
			if ( is_numeric( $this->download_id ) ) {
68
				$api_data['item_id'] = $this->download_id;
69
			}
70
71
			new EDD_SL_Plugin_Updater( $this->store_url, $this->plugin_file, $api_data );
72
		}
73
	}
74
75
	public function show_license_message( $file, $plugin ) {
76
		$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
77
		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">';
78
		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...
79
		$id = sanitize_title( $plugin['Name'] );
80
		echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '");if ( d !== null ){ d.className = d.className + " update"; }</script>';
81
		echo '</div></td></tr>';
82
	}
83
84
	public static function activate() {
85
	 	check_ajax_referer( 'frm_ajax', 'nonce' );
86
87
		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...
88
			wp_die( __( 'Oops! You forgot to enter your license number.', 'formidable' ) );
89
		}
90
91
		$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...
92
		$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...
93
		$this_plugin = self::get_addon( $plugin_slug );
94
		update_option( $this_plugin->option_name . 'key', $license );
95
96
		$response = array( 'success' => false, 'message' => '' );
97
		try {
98
			$license_data = $this_plugin->send_mothership_request( 'activate_license', $license );
99
100
			// $license_data->license will be either "valid" or "invalid"
101
			$is_valid = 'invalid';
102
			if ( is_array( $license_data ) ) {
103
				if ( $license_data['license'] == 'valid' ) {
1 ignored issue
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
104
					$is_valid = $license_data['license'];
105
					$response['message'] = __( 'Enjoy!', 'formidable' );
106
					$response['success'] = true;
107
				} else if ( $license_data['license'] == 'invalid' ) {
1 ignored issue
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
108
					$response['message'] = __( 'That license is invalid', 'formidable' );
109
				}
110
			} else if ( $license_data == 'expired' ) {
1 ignored issue
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
111
				$response['message'] = __( 'That license is expired', 'formidable' );
112
			} else if ( $license_data == 'no_activations_left' ) {
1 ignored issue
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
113
				$response['message'] = __( 'That license has been used too many times', 'formidable' );
114
			} else if ( $license_data == 'invalid_item_id' ) {
1 ignored issue
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
115
				$response['message'] = __( 'Opps! That is the wrong license number for this plugin.', 'formidable' );
116
			} else if ( $license_data == 'missing' ) {
1 ignored issue
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
117
				$response['message'] = __( 'That license is invalid', 'formidable' );
118
			} else {
119
				$response['message'] = FrmAppHelper::kses( $license_data, array('a') );
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
120
			}
121
122
			update_option( $this_plugin->option_name . 'active', $is_valid );
123
		} catch ( Exception $e ) {
124
			$response['message'] = $e->getMessage();
125
		}
126
127
		echo json_encode( $response );
128
		wp_die();
129
	}
130
131
	public static function deactivate() {
132
		check_ajax_referer( 'frm_ajax', 'nonce' );
133
134
		$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...
135
		$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...
136
		$this_plugin = self::get_addon( $plugin_slug );
137
138
		$response = array( 'success' => false, 'message' => '' );
139
		try {
140
			// $license_data->license will be either "deactivated" or "failed"
141
			$license_data = $this_plugin->send_mothership_request( 'deactivate_license', $license );
142
			if ( is_array( $license_data ) && $license_data['license'] == 'deactivated' ) {
143
				$response['success'] = true;
144
				$response['message'] = __( 'That license was removed successfully', 'helpdesk' );
145
			} else {
146
				$response['message'] = __( 'There was an error deactivating your license.', 'formidable' );
147
			}
148
		} catch ( Exception $e ) {
149
			$response['message'] = $e->getMessage();
150
		}
151
152
		delete_option( $this_plugin->option_name . 'active' );
153
		delete_option( $this_plugin->option_name . 'key' );
154
155
		echo json_encode( $response );
156
		wp_die();
157
	}
158
159
	public function send_mothership_request( $action, $license ) {
160
		$api_params = array(
161
			'edd_action' => $action,
162
			'license'    => $license,
163
			'item_name'  => urlencode( $this->plugin_name ),
164
			'url'        => home_url(),
165
		);
166
		if ( is_numeric( $this->download_id ) ) {
167
			$api_params['item_id'] = absint( $this->download_id );
168
		}
169
170
		$arg_array = array(
171
			'body'      => $api_params,
172
			'timeout'   => 15,
173
			'sslverify' => false,
174
			'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ),
175
		);
176
177
		$resp = wp_remote_post( $this->store_url, $arg_array );
178
		$body = wp_remote_retrieve_body( $resp );
179
180
		$message = __( 'Your License Key was invalid', 'formidable' );
181
		if ( is_wp_error( $resp ) ) {
182
			$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>');
183
			if ( is_wp_error( $resp ) ) {
184
				$message .= ' '. $resp->get_error_message();
185
			}
186
		} else if ( $body == 'error' || is_wp_error( $body ) ) {
187
			$message = __( 'You had an HTTP error connecting to Formidable Pro\'s API', 'formidable' );
188
		} else {
189
			$json_res = json_decode( $body, true );
190
			if ( null !== $json_res ) {
191
				if ( is_array( $json_res ) && isset( $json_res['error'] ) ) {
192
					$message = $json_res['error'];
193
				} else {
194
					$message = $json_res;
195
				}
196
			} else if ( isset( $resp['response'] ) && isset( $resp['response']['code'] ) ) {
197
				$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...
198
			}
199
		}
200
201
		return $message;
202
	}
203
}
204