data_store = $data_store; $this->api_connector = $api_connector; } protected function get_nonce_name() { return self::ACTION_NAME; } public function handle() { if ( ! $this->validate() ) { wp_send_json_error( __( 'Request must contain either an array of values to update, or a key and value to update individually.', 'gravitysmtp' ), 400 ); } $settings = filter_input( INPUT_POST, self::PARAM_SETTINGS, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); if ( ! empty( $settings ) ) { $this->handle_bulk_settings( $settings ); } else { $this->handle_individual_setting(); } } protected function handle_bulk_settings( $settings ) { $this->data_store->save_all( $settings ); $data = $settings; if ( isset( $settings[ self::PARAM_LICENSE_KEY ] ) ) { $data = array_merge( $data, $this->handle_license_key( $settings[ self::PARAM_LICENSE_KEY ] ) ); } wp_send_json_success( $data ); } protected function handle_individual_setting() { $key = htmlspecialchars( filter_input( INPUT_POST, self::PARAM_SETTING_KEY ) ); $value = isset( $_POST[ self::PARAM_SETTING_VALUE ] ) ? $_POST[ self::PARAM_SETTING_VALUE ] : null; if ( is_array( $value ) ) { $value = filter_input( INPUT_POST, self::PARAM_SETTING_VALUE, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); } else { $value = htmlspecialchars( $value ); } switch ( $key ) { case self::PARAM_LICENSE_KEY: $data = $this->handle_license_key_setting( $key, $value ); break; default: $this->data_store->save( $key, $value ); $data = array( $key => $value ); break; } do_action( 'gravitysmtp_save_plugin_setting', $key, $value ); wp_send_json_success( $data ); } protected function handle_license_key_setting( $key, $value ) { $this->data_store->save( $key, $value ); $data = array( $key => $value ); return array_merge( $data, $this->handle_license_key( $value ) ); } protected function handle_license_key( $license_key ) { $key_is_empty = empty( $license_key ); if ( $key_is_empty ) { return array( 'license_is_valid' => null ); } $license_info = $this->api_connector->check_license( $license_key ); return array( 'license_is_valid' => License_Statuses::VALID_KEY === $license_info->get_status() ); } protected function validate() { if ( ! parent::validate() ) { return false; } if ( ! isset( $_REQUEST[ self::PARAM_SETTINGS ] ) && ( ! isset( $_REQUEST[ self::PARAM_SETTING_KEY ] ) || ! isset( $_REQUEST[ self::PARAM_SETTING_VALUE ] ) ) ) { return false; } return true; } }