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