| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmFormTemplateApi extends FrmFormApi { |
| 7 |
|
| 8 |
protected static $code_option_name = 'frm_free_license_code'; |
| 9 |
|
| 10 |
private static $base_api_url = 'https://formidableforms.com/wp-json/form-templates/v1/'; |
| 11 |
|
| 12 |
protected static $free_license; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var int $new_days |
| 16 |
*/ |
| 17 |
protected $new_days = 30; |
| 18 |
|
| 19 |
/** |
| 20 |
* @since 3.06 |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
protected function set_cache_key() { |
| 25 |
$this->cache_key = 'frm_form_templates_l'; |
| 26 |
|
| 27 |
if ( ! empty( $this->license ) ) { |
| 28 |
$this->cache_key .= md5( $this->license ); |
| 29 |
} else { |
| 30 |
$free_license = $this->get_free_license(); |
| 31 |
if ( $free_license ) { |
| 32 |
$this->cache_key .= md5( $free_license ); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @since 3.06 |
| 39 |
* |
| 40 |
* @return string |
| 41 |
*/ |
| 42 |
protected function api_url() { |
| 43 |
$url = self::$base_api_url . 'list'; |
| 44 |
|
| 45 |
if ( empty( $this->license ) ) { |
| 46 |
$free_license = $this->get_free_license(); |
| 47 |
|
| 48 |
if ( $free_license ) { |
| 49 |
$url .= '?l=' . urlencode( base64_encode( $free_license ) ); |
| 50 |
$url .= '&v=' . FrmAppHelper::plugin_version(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return $url; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @since 3.06 |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
protected function skip_categories() { |
| 63 |
return array(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
public function get_free_license() { |
| 70 |
if ( ! isset( self::$free_license ) ) { |
| 71 |
self::$free_license = get_option( self::$code_option_name ); |
| 72 |
} |
| 73 |
|
| 74 |
return self::$free_license; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Check to make sure the free code is being used. |
| 79 |
* |
| 80 |
* @since 4.09.02 |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public function has_free_access() { |
| 85 |
$free_access = $this->get_free_license(); |
| 86 |
if ( ! $free_access ) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
$templates = $this->get_api_info(); |
| 91 |
$contact_form = 20872734; |
| 92 |
return isset( $templates[ $contact_form ] ) && ! empty( $templates[ $contact_form ]['url'] ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @param string $code the code from the email sent for the API. |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
private static function verify_code( $code ) { |
| 101 |
$base64_code = base64_encode( $code ); |
| 102 |
$api_url = self::$base_api_url . 'code?l=' . urlencode( $base64_code ); |
| 103 |
$response = wp_remote_get( $api_url ); |
| 104 |
|
| 105 |
self::handle_verify_response_errors_if_any( $response ); |
| 106 |
|
| 107 |
$decoded = json_decode( $response['body'] ); |
| 108 |
$successful = ! empty( $decoded->response ); |
| 109 |
|
| 110 |
if ( $successful ) { |
| 111 |
self::on_api_verify_code_success( $code ); |
| 112 |
} else { |
| 113 |
wp_send_json_error( new WP_Error( $decoded->code, $decoded->message ) ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
private static function clear_template_cache_before_getting_free_templates() { |
| 121 |
delete_option( 'frm_form_templates_l' ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @param array $response |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
private static function handle_verify_response_errors_if_any( $response ) { |
| 130 |
if ( is_wp_error( $response ) ) { |
| 131 |
wp_send_json_error( $response ); |
| 132 |
} |
| 133 |
|
| 134 |
if ( ! is_array( $response ) ) { |
| 135 |
wp_send_json_error(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @param string $code The base64 encoded code. |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
private static function on_api_verify_code_success( $code ) { |
| 145 |
self::$free_license = $code; |
| 146 |
update_option( self::$code_option_name, $code, 'no' ); |
| 147 |
|
| 148 |
$data = array(); |
| 149 |
$key = FrmAppHelper::get_param( 'key', '', 'post', 'sanitize_key' ); |
| 150 |
|
| 151 |
// Remove message from Inbox page. |
| 152 |
$message = new FrmInbox(); |
| 153 |
$message->remove( 'free_templates' ); |
| 154 |
|
| 155 |
if ( $key ) { |
| 156 |
self::clear_template_cache_before_getting_free_templates(); |
| 157 |
|
| 158 |
$data['urlByKey'] = array(); |
| 159 |
$api = new self(); |
| 160 |
$templates = $api->get_api_info(); |
| 161 |
|
| 162 |
foreach ( $templates as $template ) { |
| 163 |
if ( ! isset( $template['url'] ) || ! in_array( 'free', $template['categories'], true ) ) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
$data['urlByKey'][ $template['key'] ] = $template['url']; |
| 168 |
} |
| 169 |
|
| 170 |
if ( ! isset( $data['urlByKey'][ $key ] ) ) { |
| 171 |
$error = new WP_Error( 400, 'We were unable to retrieve the template' ); |
| 172 |
wp_send_json_error( $error ); |
| 173 |
} |
| 174 |
|
| 175 |
$data['url'] = $data['urlByKey'][ $key ]; |
| 176 |
}//end if |
| 177 |
|
| 178 |
wp_send_json_success( $data ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* AJAX Hook for signing free users up for a template API key |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public static function signup() { |
| 187 |
$code = FrmAppHelper::get_param( 'code', '', 'post' ); |
| 188 |
|
| 189 |
if ( ! $code ) { |
| 190 |
wp_send_json_error(); |
| 191 |
} |
| 192 |
|
| 193 |
self::verify_code( $code ); |
| 194 |
} |
| 195 |
} |
| 196 |
|