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