@@ -3,191 +3,191 @@ |
||
| 3 | 3 | namespace Wordlift\Configuration; |
| 4 | 4 | |
| 5 | 5 | class Config { |
| 6 | - /** |
|
| 7 | - * @var \Wordlift_Admin_Setup |
|
| 8 | - */ |
|
| 9 | - private $admin_setup; |
|
| 10 | - /** |
|
| 11 | - * @var \Wordlift_Key_Validation_Service |
|
| 12 | - */ |
|
| 13 | - private $key_validation_service; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * Config constructor. |
|
| 17 | - * |
|
| 18 | - * @param $admin_setup \Wordlift_Admin_Setup |
|
| 19 | - * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
| 20 | - */ |
|
| 21 | - public function __construct( $admin_setup, $key_validation_service ) { |
|
| 22 | - |
|
| 23 | - $this->admin_setup = $admin_setup; |
|
| 24 | - $this->key_validation_service = $key_validation_service; |
|
| 25 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 26 | - |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Check if the key is valid and also not bound to any domain. |
|
| 31 | - * |
|
| 32 | - * @param $key string |
|
| 33 | - * |
|
| 34 | - * @return bool |
|
| 35 | - */ |
|
| 36 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 37 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * we need to check if the key is not associated with any account |
|
| 41 | - * before setting it, we should check if the url is null. |
|
| 42 | - */ |
|
| 43 | - if ( is_wp_error( $account_info ) |
|
| 44 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 45 | - return false; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $account_info_json = $account_info['body']; |
|
| 49 | - |
|
| 50 | - $account_info_data = json_decode( $account_info_json, true ); |
|
| 51 | - |
|
| 52 | - if ( ! $account_info_data ) { |
|
| 53 | - // Invalid json returned by api. |
|
| 54 | - return false; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 58 | - |
|
| 59 | - if ( null === $account_info_data['url'] ) { |
|
| 60 | - return true; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - // Check if the key belongs to same site. |
|
| 64 | - if ( untrailingslashit( $account_info_data['url'] ) !== $site_url ) { |
|
| 65 | - // key already associated with another account. |
|
| 66 | - return false; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - // Return true if the key domain and site domain are the same. |
|
| 70 | - return true; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function config() { |
|
| 74 | - |
|
| 75 | - // Perform validation check for all the parameters. |
|
| 76 | - $required_fields = array( |
|
| 77 | - 'diagnostic', |
|
| 78 | - 'vocabulary', |
|
| 79 | - // Don't ask for language from webapp. |
|
| 80 | - // 'language', |
|
| 81 | - 'country', |
|
| 82 | - 'publisherName', |
|
| 83 | - 'publisher', |
|
| 84 | - 'license', |
|
| 85 | - ); |
|
| 86 | - |
|
| 87 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 88 | - |
|
| 89 | - // validate all the fields before processing |
|
| 90 | - foreach ( $required_fields as $field ) { |
|
| 91 | - if ( ! array_key_exists( $field, $_POST ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 92 | - /* translators: %s: field name */ |
|
| 93 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 94 | - |
|
| 95 | - return; |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $key = isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 100 | - |
|
| 101 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 102 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 103 | - |
|
| 104 | - // exit if not valid. |
|
| 105 | - return; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // check if key is already configured, if yes then dont save settings. |
|
| 109 | - if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
| 110 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 111 | - |
|
| 112 | - // key already configured |
|
| 113 | - return; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 117 | - |
|
| 118 | - // This prevents invalid key issue with automatic installation in sites which have redis cache enabled. |
|
| 119 | - wp_cache_flush(); |
|
| 120 | - |
|
| 121 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - private function get_params() { |
|
| 129 | - |
|
| 130 | - $attachment_id = $this->may_be_get_attachment_id(); |
|
| 131 | - |
|
| 132 | - $params = array( |
|
| 133 | - 'key' => isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 134 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['vocabulary'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 135 | - 'wl-country-code' => isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['country'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 136 | - 'name' => isset( $_POST['publisherName'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisherName'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 137 | - 'user_type' => isset( $_POST['publisher'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisher'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 138 | - 'logo' => $attachment_id, |
|
| 139 | - ); |
|
| 140 | - |
|
| 141 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 142 | - if ( $diagnostic ) { |
|
| 143 | - $params['share-diagnostic'] = 'on'; |
|
| 144 | - } |
|
| 6 | + /** |
|
| 7 | + * @var \Wordlift_Admin_Setup |
|
| 8 | + */ |
|
| 9 | + private $admin_setup; |
|
| 10 | + /** |
|
| 11 | + * @var \Wordlift_Key_Validation_Service |
|
| 12 | + */ |
|
| 13 | + private $key_validation_service; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * Config constructor. |
|
| 17 | + * |
|
| 18 | + * @param $admin_setup \Wordlift_Admin_Setup |
|
| 19 | + * @param $key_validation_service \Wordlift_Key_Validation_Service |
|
| 20 | + */ |
|
| 21 | + public function __construct( $admin_setup, $key_validation_service ) { |
|
| 22 | + |
|
| 23 | + $this->admin_setup = $admin_setup; |
|
| 24 | + $this->key_validation_service = $key_validation_service; |
|
| 25 | + add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 26 | + |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Check if the key is valid and also not bound to any domain. |
|
| 31 | + * |
|
| 32 | + * @param $key string |
|
| 33 | + * |
|
| 34 | + * @return bool |
|
| 35 | + */ |
|
| 36 | + private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 37 | + $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * we need to check if the key is not associated with any account |
|
| 41 | + * before setting it, we should check if the url is null. |
|
| 42 | + */ |
|
| 43 | + if ( is_wp_error( $account_info ) |
|
| 44 | + || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 45 | + return false; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $account_info_json = $account_info['body']; |
|
| 49 | + |
|
| 50 | + $account_info_data = json_decode( $account_info_json, true ); |
|
| 51 | + |
|
| 52 | + if ( ! $account_info_data ) { |
|
| 53 | + // Invalid json returned by api. |
|
| 54 | + return false; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 58 | + |
|
| 59 | + if ( null === $account_info_data['url'] ) { |
|
| 60 | + return true; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + // Check if the key belongs to same site. |
|
| 64 | + if ( untrailingslashit( $account_info_data['url'] ) !== $site_url ) { |
|
| 65 | + // key already associated with another account. |
|
| 66 | + return false; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + // Return true if the key domain and site domain are the same. |
|
| 70 | + return true; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function config() { |
|
| 74 | + |
|
| 75 | + // Perform validation check for all the parameters. |
|
| 76 | + $required_fields = array( |
|
| 77 | + 'diagnostic', |
|
| 78 | + 'vocabulary', |
|
| 79 | + // Don't ask for language from webapp. |
|
| 80 | + // 'language', |
|
| 81 | + 'country', |
|
| 82 | + 'publisherName', |
|
| 83 | + 'publisher', |
|
| 84 | + 'license', |
|
| 85 | + ); |
|
| 86 | + |
|
| 87 | + header( 'Access-Control-Allow-Origin: *' ); |
|
| 88 | + |
|
| 89 | + // validate all the fields before processing |
|
| 90 | + foreach ( $required_fields as $field ) { |
|
| 91 | + if ( ! array_key_exists( $field, $_POST ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 92 | + /* translators: %s: field name */ |
|
| 93 | + wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 94 | + |
|
| 95 | + return; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $key = isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 100 | + |
|
| 101 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 102 | + wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 103 | + |
|
| 104 | + // exit if not valid. |
|
| 105 | + return; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // check if key is already configured, if yes then dont save settings. |
|
| 109 | + if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
| 110 | + wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 111 | + |
|
| 112 | + // key already configured |
|
| 113 | + return; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 117 | + |
|
| 118 | + // This prevents invalid key issue with automatic installation in sites which have redis cache enabled. |
|
| 119 | + wp_cache_flush(); |
|
| 120 | + |
|
| 121 | + wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + private function get_params() { |
|
| 129 | + |
|
| 130 | + $attachment_id = $this->may_be_get_attachment_id(); |
|
| 131 | + |
|
| 132 | + $params = array( |
|
| 133 | + 'key' => isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 134 | + 'vocabulary' => isset( $_POST['vocabulary'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['vocabulary'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 135 | + 'wl-country-code' => isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['country'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 136 | + 'name' => isset( $_POST['publisherName'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisherName'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 137 | + 'user_type' => isset( $_POST['publisher'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisher'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 138 | + 'logo' => $attachment_id, |
|
| 139 | + ); |
|
| 140 | + |
|
| 141 | + $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 142 | + if ( $diagnostic ) { |
|
| 143 | + $params['share-diagnostic'] = 'on'; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - return $params; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @return int | bool |
|
| 151 | - */ |
|
| 152 | - private function may_be_get_attachment_id() { |
|
| 153 | - |
|
| 154 | - // if image or image extension not posted then return false. |
|
| 155 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 160 | - $image_string = sanitize_text_field( wp_unslash( (string) $_POST['image'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 161 | - $image_ext = sanitize_text_field( wp_unslash( (string) $_POST['imageExtension'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 162 | - |
|
| 163 | - if ( ! in_array( $image_ext, $allowed_extensions, true ) ) { |
|
| 164 | - return false; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
| 168 | - $image_decoded_string = base64_decode( $image_string ); |
|
| 169 | - |
|
| 170 | - $upload_dir = wp_upload_dir(); |
|
| 171 | - |
|
| 172 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . '.' . $image_ext; |
|
| 173 | - |
|
| 174 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
| 175 | - file_put_contents( $file_path, $image_decoded_string ); |
|
| 176 | - |
|
| 177 | - $attachment_id = wp_insert_attachment( |
|
| 178 | - array( |
|
| 179 | - 'post_status' => 'inherit', |
|
| 180 | - 'post_mime_type' => "image/$image_ext", |
|
| 181 | - ), |
|
| 182 | - $file_path |
|
| 183 | - ); |
|
| 146 | + return $params; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @return int | bool |
|
| 151 | + */ |
|
| 152 | + private function may_be_get_attachment_id() { |
|
| 153 | + |
|
| 154 | + // if image or image extension not posted then return false. |
|
| 155 | + if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 160 | + $image_string = sanitize_text_field( wp_unslash( (string) $_POST['image'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 161 | + $image_ext = sanitize_text_field( wp_unslash( (string) $_POST['imageExtension'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 162 | + |
|
| 163 | + if ( ! in_array( $image_ext, $allowed_extensions, true ) ) { |
|
| 164 | + return false; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
| 168 | + $image_decoded_string = base64_decode( $image_string ); |
|
| 169 | + |
|
| 170 | + $upload_dir = wp_upload_dir(); |
|
| 171 | + |
|
| 172 | + $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . '.' . $image_ext; |
|
| 173 | + |
|
| 174 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
| 175 | + file_put_contents( $file_path, $image_decoded_string ); |
|
| 176 | + |
|
| 177 | + $attachment_id = wp_insert_attachment( |
|
| 178 | + array( |
|
| 179 | + 'post_status' => 'inherit', |
|
| 180 | + 'post_mime_type' => "image/$image_ext", |
|
| 181 | + ), |
|
| 182 | + $file_path |
|
| 183 | + ); |
|
| 184 | 184 | |
| 185 | - // Generate the metadata for the attachment, and update the database record. |
|
| 186 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 187 | - // Update the attachment metadata. |
|
| 188 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 185 | + // Generate the metadata for the attachment, and update the database record. |
|
| 186 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 187 | + // Update the attachment metadata. |
|
| 188 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 189 | 189 | |
| 190 | - return $attachment_id; |
|
| 191 | - } |
|
| 190 | + return $attachment_id; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | } |
@@ -18,11 +18,11 @@ discard block |
||
| 18 | 18 | * @param $admin_setup \Wordlift_Admin_Setup |
| 19 | 19 | * @param $key_validation_service \Wordlift_Key_Validation_Service |
| 20 | 20 | */ |
| 21 | - public function __construct( $admin_setup, $key_validation_service ) { |
|
| 21 | + public function __construct($admin_setup, $key_validation_service) { |
|
| 22 | 22 | |
| 23 | 23 | $this->admin_setup = $admin_setup; |
| 24 | 24 | $this->key_validation_service = $key_validation_service; |
| 25 | - add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) ); |
|
| 25 | + add_action('wp_ajax_nopriv_wl_config_plugin', array($this, 'config')); |
|
| 26 | 26 | |
| 27 | 27 | } |
| 28 | 28 | |
@@ -33,35 +33,35 @@ discard block |
||
| 33 | 33 | * |
| 34 | 34 | * @return bool |
| 35 | 35 | */ |
| 36 | - private function is_key_valid_and_not_bound_to_any_domain( $key ) { |
|
| 37 | - $account_info = $this->key_validation_service->get_account_info( $key ); |
|
| 36 | + private function is_key_valid_and_not_bound_to_any_domain($key) { |
|
| 37 | + $account_info = $this->key_validation_service->get_account_info($key); |
|
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * we need to check if the key is not associated with any account |
| 41 | 41 | * before setting it, we should check if the url is null. |
| 42 | 42 | */ |
| 43 | - if ( is_wp_error( $account_info ) |
|
| 44 | - || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
|
| 43 | + if (is_wp_error($account_info) |
|
| 44 | + || wp_remote_retrieve_response_code($account_info) !== 200) { |
|
| 45 | 45 | return false; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | $account_info_json = $account_info['body']; |
| 49 | 49 | |
| 50 | - $account_info_data = json_decode( $account_info_json, true ); |
|
| 50 | + $account_info_data = json_decode($account_info_json, true); |
|
| 51 | 51 | |
| 52 | - if ( ! $account_info_data ) { |
|
| 52 | + if ( ! $account_info_data) { |
|
| 53 | 53 | // Invalid json returned by api. |
| 54 | 54 | return false; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) ); |
|
| 57 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit(get_option('home'))); |
|
| 58 | 58 | |
| 59 | - if ( null === $account_info_data['url'] ) { |
|
| 59 | + if (null === $account_info_data['url']) { |
|
| 60 | 60 | return true; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | // Check if the key belongs to same site. |
| 64 | - if ( untrailingslashit( $account_info_data['url'] ) !== $site_url ) { |
|
| 64 | + if (untrailingslashit($account_info_data['url']) !== $site_url) { |
|
| 65 | 65 | // key already associated with another account. |
| 66 | 66 | return false; |
| 67 | 67 | } |
@@ -84,41 +84,41 @@ discard block |
||
| 84 | 84 | 'license', |
| 85 | 85 | ); |
| 86 | 86 | |
| 87 | - header( 'Access-Control-Allow-Origin: *' ); |
|
| 87 | + header('Access-Control-Allow-Origin: *'); |
|
| 88 | 88 | |
| 89 | 89 | // validate all the fields before processing |
| 90 | - foreach ( $required_fields as $field ) { |
|
| 91 | - if ( ! array_key_exists( $field, $_POST ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 90 | + foreach ($required_fields as $field) { |
|
| 91 | + if ( ! array_key_exists($field, $_POST)) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 92 | 92 | /* translators: %s: field name */ |
| 93 | - wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 ); |
|
| 93 | + wp_send_json_error(sprintf(__('Field %s is required', 'wordlift'), $field), 422); |
|
| 94 | 94 | |
| 95 | 95 | return; |
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - $key = isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 99 | + $key = isset($_POST['license']) ? sanitize_text_field(wp_unslash((string) $_POST['license'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 100 | 100 | |
| 101 | - if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) { |
|
| 102 | - wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 ); |
|
| 101 | + if ( ! $this->is_key_valid_and_not_bound_to_any_domain($key)) { |
|
| 102 | + wp_send_json_error(__('Key is not valid or associated with other domain', 'wordlift'), 403); |
|
| 103 | 103 | |
| 104 | 104 | // exit if not valid. |
| 105 | 105 | return; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // check if key is already configured, if yes then dont save settings. |
| 109 | - if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) { |
|
| 110 | - wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 ); |
|
| 109 | + if (\Wordlift_Configuration_Service::get_instance()->get_key()) { |
|
| 110 | + wp_send_json_error(__('Key already configured.', 'wordlift'), 403); |
|
| 111 | 111 | |
| 112 | 112 | // key already configured |
| 113 | 113 | return; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - $this->admin_setup->save_configuration( $this->get_params() ); |
|
| 116 | + $this->admin_setup->save_configuration($this->get_params()); |
|
| 117 | 117 | |
| 118 | 118 | // This prevents invalid key issue with automatic installation in sites which have redis cache enabled. |
| 119 | 119 | wp_cache_flush(); |
| 120 | 120 | |
| 121 | - wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) ); |
|
| 121 | + wp_send_json_success(__('Configuration Saved', 'wordlift')); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | /** |
@@ -130,16 +130,16 @@ discard block |
||
| 130 | 130 | $attachment_id = $this->may_be_get_attachment_id(); |
| 131 | 131 | |
| 132 | 132 | $params = array( |
| 133 | - 'key' => isset( $_POST['license'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['license'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 134 | - 'vocabulary' => isset( $_POST['vocabulary'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['vocabulary'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 135 | - 'wl-country-code' => isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['country'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 136 | - 'name' => isset( $_POST['publisherName'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisherName'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 137 | - 'user_type' => isset( $_POST['publisher'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['publisher'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 133 | + 'key' => isset($_POST['license']) ? sanitize_text_field(wp_unslash((string) $_POST['license'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 134 | + 'vocabulary' => isset($_POST['vocabulary']) ? sanitize_text_field(wp_unslash((string) $_POST['vocabulary'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 135 | + 'wl-country-code' => isset($_POST['country']) ? sanitize_text_field(wp_unslash((string) $_POST['country'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 136 | + 'name' => isset($_POST['publisherName']) ? sanitize_text_field(wp_unslash((string) $_POST['publisherName'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 137 | + 'user_type' => isset($_POST['publisher']) ? sanitize_text_field(wp_unslash((string) $_POST['publisher'])) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 138 | 138 | 'logo' => $attachment_id, |
| 139 | 139 | ); |
| 140 | 140 | |
| 141 | - $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 142 | - if ( $diagnostic ) { |
|
| 141 | + $diagnostic = isset($_POST['diagnostic']) ? (bool) $_POST['diagnostic'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 142 | + if ($diagnostic) { |
|
| 143 | 143 | $params['share-diagnostic'] = 'on'; |
| 144 | 144 | } |
| 145 | 145 | |
@@ -152,27 +152,27 @@ discard block |
||
| 152 | 152 | private function may_be_get_attachment_id() { |
| 153 | 153 | |
| 154 | 154 | // if image or image extension not posted then return false. |
| 155 | - if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 155 | + if ( ! isset($_POST['image']) || ! isset($_POST['imageExtension'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 156 | 156 | return false; |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - $allowed_extensions = array( 'png', 'jpeg', 'jpg' ); |
|
| 160 | - $image_string = sanitize_text_field( wp_unslash( (string) $_POST['image'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 161 | - $image_ext = sanitize_text_field( wp_unslash( (string) $_POST['imageExtension'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 159 | + $allowed_extensions = array('png', 'jpeg', 'jpg'); |
|
| 160 | + $image_string = sanitize_text_field(wp_unslash((string) $_POST['image'])); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 161 | + $image_ext = sanitize_text_field(wp_unslash((string) $_POST['imageExtension'])); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 162 | 162 | |
| 163 | - if ( ! in_array( $image_ext, $allowed_extensions, true ) ) { |
|
| 163 | + if ( ! in_array($image_ext, $allowed_extensions, true)) { |
|
| 164 | 164 | return false; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 168 | - $image_decoded_string = base64_decode( $image_string ); |
|
| 168 | + $image_decoded_string = base64_decode($image_string); |
|
| 169 | 169 | |
| 170 | 170 | $upload_dir = wp_upload_dir(); |
| 171 | 171 | |
| 172 | - $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . '.' . $image_ext; |
|
| 172 | + $file_path = $upload_dir['path'].DIRECTORY_SEPARATOR.md5($image_string).'.'.$image_ext; |
|
| 173 | 173 | |
| 174 | 174 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
| 175 | - file_put_contents( $file_path, $image_decoded_string ); |
|
| 175 | + file_put_contents($file_path, $image_decoded_string); |
|
| 176 | 176 | |
| 177 | 177 | $attachment_id = wp_insert_attachment( |
| 178 | 178 | array( |
@@ -183,9 +183,9 @@ discard block |
||
| 183 | 183 | ); |
| 184 | 184 | |
| 185 | 185 | // Generate the metadata for the attachment, and update the database record. |
| 186 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); |
|
| 186 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path); |
|
| 187 | 187 | // Update the attachment metadata. |
| 188 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 188 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 189 | 189 | |
| 190 | 190 | return $attachment_id; |
| 191 | 191 | } |