| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | class FrmFormTemplateApi extends FrmFormApi { |
| 7 | 7 | |
| 8 | - protected static $code_option_name = 'frm_free_license_code'; | |
| 8 | + protected static $code_option_name = 'frm_free_license_code'; | |
| 9 | 9 | |
| 10 | 10 | private static $base_api_url = 'https://formidableforms.com/wp-json/form-templates/v1/'; |
| 11 | 11 | |
| 12 | 12 | protected static $free_license; |
| @@ -11,21 +11,9 @@ | ||
| 11 | 11 | |
| 12 | 12 | protected static $free_license; |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | - * @var int | |
| 16 | - */ | |
| 17 | - protected $new_days = 30; | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * @var string | |
| 21 | - */ | |
| 22 | - protected $cache_timeout = '+12 hours'; | |
| 23 | - | |
| 24 | - /** | |
| 25 | 15 | * @since 3.06 |
| 26 | - * | |
| 27 | - * @return void | |
| 28 | 16 | */ |
| 29 | 17 | protected function set_cache_key() { |
| 30 | 18 | $this->cache_key = 'frm_form_templates_l'; |
| 31 | 19 | |
| @@ -31,22 +19,28 @@ | ||
| 31 | 19 | |
| 32 | 20 | if ( ! empty( $this->license ) ) { |
| 33 | 21 | $this->cache_key .= md5( $this->license ); |
| 34 | 22 | } else { |
| 35 | - $this->cache_key .= '01fd72122f4a90f15684915c729d4368'; | |
| 23 | + $free_license = $this->get_free_license(); | |
| 24 | + if ( $free_license ) { | |
| 25 | + $this->cache_key .= md5( $free_license ); | |
| 26 | + } | |
| 36 | 27 | } |
| 37 | 28 | } |
| 38 | 29 | |
| 39 | 30 | /** |
| 40 | 31 | * @since 3.06 |
| 41 | - * | |
| 42 | - * @return string | |
| 43 | 32 | */ |
| 44 | 33 | protected function api_url() { |
| 45 | 34 | $url = self::$base_api_url . 'list'; |
| 46 | 35 | |
| 47 | 36 | if ( empty( $this->license ) ) { |
| 48 | - $url .= '?l=RlJFRVRFTVBMQVRFUyEhIQ%3D%3D&v=' . FrmAppHelper::plugin_version(); | |
| 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 | + } | |
| 49 | 43 | } |
| 50 | 44 | |
| 51 | 45 | return $url; |
| 52 | 46 | } |
| @@ -52,10 +46,8 @@ | ||
| 52 | 46 | } |
| 53 | 47 | |
| 54 | 48 | /** |
| 55 | 49 | * @since 3.06 |
| 56 | - * | |
| 57 | - * @return string[] | |
| 58 | 50 | */ |
| 59 | 51 | protected function skip_categories() { |
| 60 | 52 | return array(); |
| 61 | 53 | } |
| @@ -60,54 +52,120 @@ | ||
| 60 | 52 | return array(); |
| 61 | 53 | } |
| 62 | 54 | |
| 63 | 55 | /** |
| 64 | - * Set the free license code option value. | |
| 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. | |
| 65 | 68 | * |
| 66 | - * @since 6.25 | |
| 67 | - * | |
| 68 | - * @param string $code The license code to set. | |
| 69 | + * @since 4.09.02 | |
| 69 | 70 | */ |
| 70 | - public static function set_free_license_code( $code ) { | |
| 71 | - update_option( self::$code_option_name, $code, 'no' ); | |
| 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'] ); | |
| 72 | 80 | } |
| 73 | 81 | |
| 74 | 82 | /** |
| 75 | - * Get the free license code option value. | |
| 76 | - * | |
| 77 | - * @since 6.25 | |
| 78 | - * | |
| 79 | - * @return false|string The license code, or false if not set. | |
| 83 | + * @param string $code the code from the email sent for the API | |
| 80 | 84 | */ |
| 81 | - public static function get_free_license_code() { | |
| 82 | - return get_option( self::$code_option_name ); | |
| 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 | + } | |
| 83 | 100 | } |
| 84 | 101 | |
| 102 | + private static function clear_template_cache_before_getting_free_templates() { | |
| 103 | + delete_option( 'frm_form_templates_l' ); | |
| 104 | + } | |
| 105 | + | |
| 85 | 106 | /** |
| 86 | - * AJAX Hook for signing free users up for a template API key | |
| 87 | - * | |
| 88 | - * @return void | |
| 107 | + * @param array $response | |
| 89 | 108 | */ |
| 90 | - public static function signup() { | |
| 91 | - _deprecated_function( __METHOD__, '6.20' ); | |
| 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 | + } | |
| 92 | 117 | } |
| 93 | 118 | |
| 94 | 119 | /** |
| 95 | - * @return string | |
| 120 | + * @param string $code the base64 encoded code | |
| 96 | 121 | */ |
| 97 | - public function get_free_license() { | |
| 98 | - _deprecated_function( __METHOD__, '6.20' ); | |
| 99 | - return ''; | |
| 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 ); | |
| 100 | 157 | } |
| 101 | 158 | |
| 102 | 159 | /** |
| 103 | - * Check to make sure the free code is being used. | |
| 104 | - * | |
| 105 | - * @since 4.09.02 | |
| 106 | - * | |
| 107 | - * @return bool | |
| 160 | + * AJAX Hook for signing free users up for a template API key | |
| 108 | 161 | */ |
| 109 | - public function has_free_access() { | |
| 110 | - _deprecated_function( __METHOD__, '6.20' ); | |
| 111 | - return true; | |
| 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 ); | |
| 112 | 170 | } |
| 113 | 171 | } |