-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwordpress-diffy.php
289 lines (257 loc) · 8.16 KB
/
wordpress-diffy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* The Diffy Plugin.
*
* Diffy is visual regression testing platform.
*
* @package Diffy Visual Regression Testing
* @subpackage Main
* @since 1.0.0
*/
/**
* Plugin Name: Diffy
* Plugin URI: https://diffy.website/wordpress
* Description: Diffy allows you to see if any of your updates caused visual changes on the site.
* Author: Diffy
* Author URI: https://diffy.website/
* Version: 0.9.6
* License: GPLv2 or later (license.txt)
*/
/**
* Autoload helper classes.
*/
$diffy_autoload_file = plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
if ( is_readable( $diffy_autoload_file ) ) {
require $diffy_autoload_file;
}
elseif ( ! class_exists( 'Diffy\Diffy' ) ) { // Still checking since might be site-level autoload R.
add_action( 'admin_init', 'diffy_missing_autoload', 1 );
return;
}
// Load admin pages.
require_once dirname( __FILE__ ) . '/admin/class-wordpress-diffy-admin.php';
$admin = new Diffy_Admin('wordpress-diffy', '0.9.0');
/**
* Throw an error if the Composer autoload is missing and self-deactivate plugin.
*
* @return void
*/
function diffy_missing_autoload() {
if ( is_admin() ) {
add_action( 'admin_notices', 'diffy_missing_autoload_notice' );
diffy_self_deactivate();
}
}
/**
* Returns the notice in case of missing Composer autoload.
*/
function diffy_missing_autoload_notice() {
$message = esc_html__( 'The %1$s plugin installation is incomplete. Misses autoload files. Please refer to %2$sinstallation instructions%3$s.', 'wordpress-diffy' );
$message = sprintf( $message, 'Diffy', '<a href="https://github.com/DiffyWebsite/wordpress-diffy#installation">', '</a>' );
echo '<div class="error"><p>' . esc_html__( 'Activation failed:', 'wordpress-diffy' ) . ' ' . strip_tags( $message, '<a>' ) . '</p></div>';
}
/**
* The method will deactivate the plugin, but only once, done by the static $is_deactivated.
*/
function diffy_self_deactivate() {
static $is_deactivated;
if ( $is_deactivated === null ) {
$is_deactivated = true;
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
/**
* Runs when we start upgrade process. Create first set of screenshots.
*/
function diffy_create_first_screenshots($options) {
// Upgrade was run without Diffy coverage.
if (!isset($_POST['testing'])) {
return $options;
}
add_settings_error(
'diffy',
'diffy',
__( 'Diffy started first set of screenshots. Waiting until completed and run updates.' ),
'success'
);
$diffy_api_key = get_option('diffy_api_key');
$diffy_project_id = get_option('diffy_project_id');
if (empty($diffy_api_key) || empty($diffy_project_id)) {
return $options;
}
try {
\Diffy\Diffy::setApiKey($diffy_api_key);
}
catch (\Exception $exception) {
return $options;
}
$screenshot_id = 0;
try {
$screenshot_id = \Diffy\Screenshot::create($diffy_project_id, 'production');
update_option('diffy_first_screenshot_id', $screenshot_id);
// Assume that screenshots should be created in 20 mins max.
for ($i = 0; $i < 240; $i++) {
sleep(5);
$screenshot = \Diffy\Screenshot::retrieve($screenshot_id);
if ($screenshot->isCompleted()) {
break;
}
}
}
catch (\Exception $e) {
return $options;
}
return $options;
}
add_filter( 'upgrader_package_options', 'diffy_create_first_screenshots' );
/**
* Runs after updates completed. Create second set of screenshots and diff.
*/
function diffy_create_second_screenshots_diff() {
$first_screenshot_id = get_option('diffy_first_screenshot_id');
if (empty($first_screenshot_id)) {
return;
}
$diffy_project_id = get_option('diffy_project_id');
try {
$second_screenshot_id = \Diffy\Screenshot::create($diffy_project_id, 'production');
update_option('diffy_second_screenshot_id', $second_screenshot_id);
$diff_id = \Diffy\Diff::create($diffy_project_id, $first_screenshot_id, $second_screenshot_id);
update_option('diffy_diff_id', $diff_id);
update_option('diffy_first_screenshot_id', '');
}
catch (\Exception $e) {
return;
}
}
add_action( 'upgrader_process_complete', 'diffy_create_second_screenshots_diff' );
/**
* Add js to the plugins listing page.
*/
function diffy_plugins_page_add_javascript($plugins) {
wp_enqueue_script(
'diffy-plugins-page',
plugin_dir_url(__FILE__) . 'js/plugins-scripts.js',
array('jquery'),
NULL,
TRUE
);
// set variables for script
wp_localize_script(
'diffy-plugins-page',
'settings',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
)
);
return $plugins;
}
add_filter( 'all_plugins', 'diffy_plugins_page_add_javascript' );
/**
* Ajax requests to check the status of first set of screenshots.
*/
function diffy_ajax_first_screenshots_callback() {
$diffy_api_key = get_option('diffy_api_key');
$diffy_project_id = get_option('diffy_project_id');
if (empty($diffy_api_key) || empty($diffy_project_id)) {
wp_send_json_error([
'error_message' => 'Diffy API Key and project ID are empty',
]);
}
try {
\Diffy\Diffy::setApiKey($diffy_api_key);
}
catch (\Exception $exception) {
wp_send_json_error([
'error_message' => 'Could not set Diffy API Key',
]);
}
$first_screenshot_id = get_option('diffy_first_screenshot_id');
if (!empty($first_screenshot_id)) {
try {
$screenshot = \Diffy\Screenshot::retrieve($first_screenshot_id);
if (!$screenshot->isCompleted()) {
wp_send_json_success( [
'status' => 'progress',
'message' => sprintf('Creating first set of screenshots. %s.', ucfirst($screenshot->getEstimate())),
] );
}
else {
wp_send_json_success( [
'status' => 'completed',
'message' => 'Completed creating first set of screenshots. Wordpress plugin update in progress.',
] );
}
}
catch (\Exception $exception) {
wp_send_json_error([
'error_message' => sprintf('Could not retrieve Screenshot ID %d', $first_screenshot_id)
]);
}
}
}
add_action( 'wp_ajax_diffy_first_screenshots', 'diffy_ajax_first_screenshots_callback');
/**
* Ajax requests to check the status of second set of screenshots and the diff.
*/
function diffy_ajax_second_screenshots_callback() {
$diffy_api_key = get_option('diffy_api_key');
$diffy_project_id = get_option('diffy_project_id');
if (empty($diffy_api_key) || empty($diffy_project_id)) {
wp_send_json_error([
'error_message' => 'Diffy API Key and project ID are empty',
]);
}
try {
\Diffy\Diffy::setApiKey($diffy_api_key);
}
catch (\Exception $exception) {
wp_send_json_error([
'error_message' => 'Could not set Diffy API Key',
]);
}
$second_screenshot_id = get_option('diffy_second_screenshot_id');
if (!empty($second_screenshot_id)) {
try {
$screenshot = \Diffy\Screenshot::retrieve($second_screenshot_id);
if (!$screenshot->isCompleted()) {
wp_send_json_success( [
'status' => 'progress',
'message' => sprintf('Creating second set of screenshots. %s.', ucfirst($screenshot->getEstimate())),
] );
}
update_option('diffy_second_screenshot_id', 0);
}
catch (\Exception $exception) {
wp_send_json_error([
'error_message' => sprintf('Could not retrieve Screenshot ID %d', $second_screenshot_id)
]);
}
}
$diff_id = get_option('diffy_diff_id');
if (!empty($diff_id)) {
try {
$diff = \Diffy\Diff::retrieve($diff_id);
if (!$diff->isCompleted()) {
wp_send_json_success( [
'status' => 'progress',
'message' => sprintf('Second set completed. Comparing screenshots. %s.', ucfirst($diff->getEstimate())),
] );
}
update_option('diffy_diff_id', 0);
wp_send_json_success( [
'status' => 'completed',
'message' => sprintf('Comparison completed. %s.', $diff->getReadableResult()),
] );
}
catch (\Exception $exception) {
wp_send_json_error([
'error_message' => sprintf('Could not retrieve Diff ID %d', $diff_id)
]);
}
}
}
add_action( 'wp_ajax_diffy_second_screenshots', 'diffy_ajax_second_screenshots_callback');