Skip to content

Commit

Permalink
More verbose messaging during the updates process.
Browse files Browse the repository at this point in the history
  • Loading branch information
ygerasimov committed Sep 21, 2020
1 parent c6893a9 commit 62c1678
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 24 deletions.
73 changes: 71 additions & 2 deletions admin/class-wordpress-diffy-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ public function addPluginAdminMenu() {
public function registerAndBuildFields() {
// Two settings API Key and Project ID.
register_setting('reading', 'diffy_api_key');
register_setting('reading', 'diffy_project_id');
register_setting('other', 'diffy_last_screenshot_id');
register_setting('reading', 'diffy_project_id', [$this, 'diffy_validate_project_id_callback']);
register_setting('other', 'diffy_first_screenshot_id');
register_setting('other', 'diffy_second_screenshot_id');
register_setting('other', 'diffy_diff_id');

// Introduction section.
add_settings_section(
Expand Down Expand Up @@ -222,4 +224,71 @@ public function displayPluginAdminDashboard() {
require_once 'wordpress-diffy-settings-form.php';
}

/**
* Validate if settings are saved correctly.
*/
public function diffy_validate_project_id_callback($project_id) {
$diffy_api_key = get_option('diffy_api_key');

if (empty($diffy_api_key)) {
add_settings_error(
'diffy',
'diffy',
__( 'Please add API Key' ),
'error'
);
return $project_id;
}

if (empty($project_id)) {
add_settings_error(
'diffy',
'diffy',
__( 'Please add Project ID' ),
'error'
);
return $project_id;
}

try {
\Diffy\Diffy::setApiKey($diffy_api_key);
}
catch (\Exception $exception) {
add_settings_error(
'diffy',
'diffy',
sprintf( __( 'Invalid API Key %s' ), $diffy_api_key),
'error'
);
return $project_id;
}

try {
$project = \Diffy\Project::get($project_id);
}
catch (\Exception $exception) {

}

if (empty($project)) {
add_settings_error(
'diffy',
'diffy',
sprintf( __( 'Invalid Project ID %s' ), $project_id),
'error'
);
return $project_id;
}

add_settings_error(
'diffy',
'diffy',
/* translators: %d: Number of requests. */
__( 'We have verified your API Key and Project ID. You are good to go.' ),
'success'
);

return $project_id;
}

}
48 changes: 48 additions & 0 deletions js/plugins-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
( function( $ ) {

/**
* Call backend to check the job status and display its status to the user.
*
* @param string jobType
*/
showDiffyStatus = function(jobType) {
var showDiffyStatusInterval = setInterval(function() {

var data = {
'action': jobType
};
$.post(settings.ajaxurl, data, function (response) {
var data = response.data;

if (response.success) {
if (response.data.status == 'completed') {
clearInterval(showDiffyStatusInterval);
}
$('.diffy-message').html(response.data.message);
} else {

}
})
.fail(function () {
console.log("Diffy ajax error");
});
}, 5000);
};

// Triggered right before ajax request is sent to update plugin.
$( document ).on('wp-plugin-updating', function(){
$('.update-message').after('<div class="notice inline notice-alt notice-info"><p class="diffy-message">Diffy creates first set of screenshots (before update).</p></div>');

showDiffyStatus('diffy_first_screenshots');
});

// Triggered after plugin was successfully updated.
$( document ).on('wp-plugin-update-success', function(){
$('.diffy-message').html('Updates completed. Now Diffy creates second set of screenshots and comparison.');

showDiffyStatus('diffy_second_screenshots');
});



})( jQuery );
10 changes: 5 additions & 5 deletions js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
$('#diffy-register').click(function() {
$('.diffy-register-error').remove();

$('#diffy-register').html('Registering a user and creating project...');
$('#diffy-register').attr('disabled', true);
$('.diffy-register-wrapper').after('<div class="updating-message"><p>Registering a user and creating project...</p></div>');
$('.diffy-register-wrapper').hide();

var data = {
'action' : 'diffy_register'
Expand All @@ -18,17 +18,17 @@
'<p><strong>Diffy account & project created.</strong> It is created by using your email address "' + data.email + '" and password "' + data.password + '". Please note these credentials. But no worries you can always reset the password if you need to.</p></div>';
$('.diffy-register-wrapper').html(success_message);


$('input[name=diffy_api_key]').val(data.diffy_api_key);
$('input[name=diffy_project_id]').val(data.diffy_project_id);
}
else {
var error_message = '<div id="setting-error-settings_updated" class="diffy-register-error notice notice-error settings-error is-dismissible"> \n' +
'<p><strong>' + data.error_message + '.</strong> Please go to <a target="_blank" href="https://app.diffy.website">Diffy app</a> and retrieve API Key and set up the project manually.</p></div>';
$('.diffy-register-wrapper').append(error_message);

$('#diffy-register').html('One click register');
$('#diffy-register').attr('disabled', false);
}
$('.updating-message').remove();
$('.diffy-register-wrapper').show();
})
.fail(function() {
console.log( "Ajax error" );
Expand Down
6 changes: 3 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
=== Diffy ===
=== Diffy Visual Regression Testing ===
Contributors: ygerasimov
Tags: visual regression testing, updates verification, automated testing
Requires at least: 4.8
Tested up to: 5.5.0
Tested up to: 5.5.1
Requires PHP: 7.0
Stable tag: 0.9.1
Stable tag: 0.9.3
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Development: https://github.com/DiffyWebsite/wordpress-diffy
Expand Down
25 changes: 24 additions & 1 deletion vendor/diffywebsite/diffy-php/src/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static function create(int $projectId, int $screenshotId1, int $screensho
* Load full info on Diff.
*
* @param int $diffId
* @return mixed
* @return \Diffy\Diff
*/
public static function retrieve(int $diffId)
{
Expand Down Expand Up @@ -111,6 +111,29 @@ public function isCompleted()
return in_array($this->data['state'], [self::COMPLETED, self::ZIPFILE, self::COMPLETED_HOOK_EXECUTED, self::WITHOUT_ZIP]);
}

/**
* How long it will take to complete the diff.
*
* @return string
*/
public function getEstimate()
{
return 'under 1 minute';
}

/**
* How long it will take to complete the diff.
*
* @return string
*/
public function getReadableResult()
{
if ($this->data['result'] == 0) {
return 'No changes found';
}
return sprintf('%d% pages changed. <a target="_blank" href="%s">See the report</a>', $this->data['result'], $this->data['diffSharedUrl']);
}

/**
* Check if Diff is failed.
*
Expand Down
7 changes: 6 additions & 1 deletion vendor/diffywebsite/diffy-php/src/Screenshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function setBaselineSet(int $projectId, int $screenshotId)
* Load full info on Screenshot.
*
* @param \Diffy\int $screenshotId
* @return mixed
* @return \Diffy\Screenshot
*/
public static function retrieve(int $screenshotId)
{
Expand Down Expand Up @@ -106,6 +106,11 @@ public function isCompleted()
return in_array($this->data['state'], [self::COMPLETED, self::COMPLETED_HOOK_EXECUTED, self::ZIPFILE]);
}

public function getEstimate()
{
return $this->data['status']['estimate'];
}


/**
* Create Screenshot from browserstack.
Expand Down
Loading

0 comments on commit 62c1678

Please sign in to comment.