Skip to content

Commit

Permalink
add an API credentials validation to the settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
2ndkauboy committed Jul 4, 2020
1 parent f6d193e commit 0be732c
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 18 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
Requires at least: 5.0
Tested up to: 5.4
Requires PHP: 5.6
Stable tag: 1.0.0
Stable tag: 1.0.1
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.txt

Expand All @@ -16,5 +16,8 @@ In order to be able to use the plugin, you have to get API credentials from Prov

## Changelog

### 1.0.1
* Add an API credentials validation to the settings page

### 1.0.0
* First stable version
6 changes: 3 additions & 3 deletions embeds-for-proven-expert.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
* Plugin Name: Embeds for ProvenExpert
* Plugin URI: https://github.com/2ndkauboy/embeds-for-proven-expert
* Description: Provide multiple Embeds for ProvenExpert rating seals, logos and rating summaries.
* Version: 1.0.0
* Version: 1.0.1
* Author: Bernhard Kau
* Author URI: https://kau-boys.de
* Text Domain: embeds-for-proven-expert
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
*/

define( 'EFPE_VERSION', '1.0.0' );
define( 'EFPE_VERSION', '1.0.1' );
define( 'EFPE_FILE', __FILE__ );
define( 'EFPE_PATH', plugin_dir_path( EFPE_FILE ) );

Expand Down Expand Up @@ -47,7 +47,7 @@ function efpe_pre_init() {
return;
}

// If all checks were succcessful, load the plugin.
// If all checks were successful, load the plugin.
require_once EFPE_PATH . 'src/load.php';
}

Expand Down
4 changes: 2 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<config name="minimum_supported_wp_version" value="5.4"/>
<config name="minimum_supported_wp_version" value="4.7"/>

<rule ref="WordPress">
<!-- As we use PHP 5.4+ now, we can use the short array syntax -->
Expand Down Expand Up @@ -85,7 +85,7 @@
-->

<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="7.2-"/>
<config name="testVersion" value="5.6-"/>
<!-- https://github.com/PHPCompatibility/PHPCompatibilityWP -->
<rule ref="PHPCompatibilityWP"/>

Expand Down
28 changes: 21 additions & 7 deletions src/lib/Helpers/ProvenExpertAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Helper class to access the ProvenExpert API
*
* @see WooCommerce\Admin\WC_Helper_API
* @see WooCommerce/Admin/WC_Helper_API
*
* @package EFPE\Helpers
*/
Expand All @@ -18,10 +18,24 @@ class ProvenExpertAPI {
/**
* Base path for API routes.
*
* @var $api_base
* @var string
*/
public static $api_base;

/**
* The authorization ID.
*
* @var string
*/
public static $api_id = '';

/**
* The authorization key.
*
* @var string
*/
public static $api_key = '';

/**
* Initialize the helper
*/
Expand All @@ -37,6 +51,8 @@ public function init() {
*/
public static function load() {
self::$api_base = apply_filters( 'efpe_helper_api_base', 'https://www.provenexpert.com/api/v1' );
self::$api_id = get_option( 'efpe_api_id' );
self::$api_key = get_option( 'efpe_api_key' );
}

/**
Expand All @@ -48,15 +64,13 @@ public static function load() {
* @return array|WP_Error The response from wp_safe_remote_request()
*/
public static function request( $endpoint, $args = [] ) {
$url = self::url( $endpoint );
$api_id = get_option( 'efpe_api_id' );
$api_key = get_option( 'efpe_api_key' );
$url = self::url( $endpoint );

if ( empty( $api_id ) || empty( $api_key ) ) {
if ( empty( self::$api_id ) || empty( self::$api_key ) ) {
return new WP_Error( 'efpe_authentication', __( 'You need to set up the API credentials in the settings', 'embeds-for-proven-expert' ) );
}

$args['headers']['Authorization'] = 'Basic ' . base64_encode( $api_id . ':' . $api_key ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$args['headers']['Authorization'] = 'Basic ' . base64_encode( self::$api_id . ':' . self::$api_key ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode

/**
* Allow developers to filter the request args passed to wp_safe_remote_request().
Expand Down
9 changes: 5 additions & 4 deletions src/lib/ProvenExpertEmbeds/AbstractProvenExpertEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public function filter_request_args( $args ) {
// Remove the arg, if it's not within the settings of this specific embed.
if ( ! array_key_exists( $key, $this->settings ) ) {
unset( $args[ $key ] );
}
// Check if setting with type "number" are "empty" and remove them, as they otherwise cause issues.
if ( 'number' === $this->settings[ $key ]['type'] && ( ! is_numeric( $arg ) || empty( $arg ) ) ) {
unset( $args[ $key ] );
} else {
// Check if setting with type "number" are "empty" and remove them, as they otherwise cause issues.
if ( 'number' === $this->settings[ $key ]['type'] && ( ! is_numeric( $arg ) || empty( $arg ) ) ) {
unset( $args[ $key ] );
}
}
}

Expand Down
65 changes: 65 additions & 0 deletions src/lib/Settings/ProvenExpertApiCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace EFPE\Settings;

use EFPE\Helpers\ProvenExpertAPI;

/**
* Class ProvenExpertApiCredentials
*/
Expand All @@ -17,6 +19,7 @@ class ProvenExpertApiCredentials {
public function init() {
add_action( 'admin_menu', [ $this, 'add_options_page' ] );
add_action( 'admin_init', [ $this, 'add_settings' ] );
add_filter( 'pre_update_option_efpe_api_key', [ $this, 'validate_credentials' ], 10, 3 );
}

/**
Expand Down Expand Up @@ -125,4 +128,66 @@ public function settings_field( $args ) {
<input type="text" id="<?php echo esc_attr( $args['label_for'] ); ?>>" name="<?php echo esc_attr( $args['label_for'] ); ?>" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>" class="regular-text">
<?php
}

/**
* Validate if the credentials are correct, if not, return the old value so the update is skipped
*
* @param mixed $value The new, unserialized option value.
* @param mixed $old_value The old option value.
* @param string $option Option name.
*
* @return string
*/
public function validate_credentials( $value, $old_value, $option ) {
// Check the credentials by getting the "Logo" embed.
$args = [
'body' => [
'data' => [
'type' => 'logo',
],
],
];

// Set the API credentials with the new values.
ProvenExpertAPI::$api_id = get_option( 'efpe_api_id' );
ProvenExpertAPI::$api_key = $value;

// Try to get a API response with those crendentials.
$request = ProvenExpertAPI::post( '/widget/create', $args );

if ( ! is_wp_error( $request ) ) {
$response_body = json_decode( wp_remote_retrieve_body( $request ), true );

if ( 'error' === $response_body['status'] ) {
if ( isset( $response_body['errors'][0] ) && ( 'authentication failure' === $response_body['errors'][0] || 'wrong credentials' === $response_body['errors'][0] ) ) {
add_settings_error(
'efpe',
esc_attr( 'settings_updated' ),
__( 'The credentials you have entered are wrong!', 'embeds-for-proven-expert' )
);
} else {
add_settings_error(
'efpe',
esc_attr( 'settings_updated' ),
__( 'There was an unknown error validating the credentials!', 'embeds-for-proven-expert' )
);
}
} else {
add_settings_error(
'efpe',
esc_attr( 'settings_updated' ),
__( 'The credentials you have entered have been validated and are correct!', 'embeds-for-proven-expert' ),
'success'
);
}
} else {
add_settings_error(
'efpe',
esc_attr( 'settings_updated' ),
__( 'There was a request error trying to validating the credentials!', 'embeds-for-proven-expert' )
);
}

return $value;
}
}
2 changes: 1 addition & 1 deletion src/lib/Widgets/AbstractWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Abstract widget class
*
* @see WooCommerce\Abstracts\WC_Widget
* @see WooCommerce/Abstracts/WC_Widget
*
* @package EFPE\Widgets
*/
Expand Down

0 comments on commit 0be732c

Please sign in to comment.