| 1 |
var $wildcards_select = jQuery('.select-wildcards').select2({ |
| 2 |
tags: true, |
| 3 |
tokenSeparators: ['/'], |
| 4 |
createTag: function (params) { |
| 5 |
var term = jQuery.trim(params.term) |
| 6 |
|
| 7 |
if (term === '') { |
| 8 |
return null |
| 9 |
} |
| 10 |
|
| 11 |
let tags = [ |
| 12 |
'%site_id%', |
| 13 |
'%site_url%', |
| 14 |
'%site_url_host%', |
| 15 |
'%site_url_path%', |
| 16 |
'%date_year/date_month%', |
| 17 |
] |
| 18 |
|
| 19 |
// Remove special chars from tags |
| 20 |
if (!/^[a-zA-Z0-9_\-.]+$/.test(term) && jQuery.inArray(term, tags) == -1) { |
| 21 |
term = term.replace(/[^a-zA-Z0-9_\-.]/g, '') |
| 22 |
} |
| 23 |
|
| 24 |
return { |
| 25 |
id: term, |
| 26 |
text: term, |
| 27 |
} |
| 28 |
}, |
| 29 |
templateSelection: function (state) { |
| 30 |
// Add slash at the en of the tag |
| 31 |
return state.text |
| 32 |
}, |
| 33 |
insertTag: function (data, tag) { |
| 34 |
// Insert the tag at the end of the results |
| 35 |
data.push(tag) |
| 36 |
}, |
| 37 |
}) |
| 38 |
|
| 39 |
jQuery('.select-wildcards').on('select2:select', function (evt) { |
| 40 |
var element = evt.params.data.element |
| 41 |
var $element = jQuery(element) |
| 42 |
let $element_value = $element.value |
| 43 |
|
| 44 |
$element.detach() |
| 45 |
jQuery(this).append($element) |
| 46 |
|
| 47 |
/** |
| 48 |
* add slash after tag |
| 49 |
*/ |
| 50 |
if ($element_value !== '/') { |
| 51 |
// Create the DOM option that is pre-selected by default |
| 52 |
var newState = new Option('/', '/', true, true) |
| 53 |
jQuery(newState).prop('disabled', true) |
| 54 |
// Append it to the select |
| 55 |
jQuery(this).append(newState) |
| 56 |
} |
| 57 |
|
| 58 |
jQuery(this).trigger('change') |
| 59 |
prepare_preview_url() |
| 60 |
}) |
| 61 |
|
| 62 |
jQuery('.select-wildcards').on('select2:unselect', function (evt) { |
| 63 |
if (evt.params.data.id === '/') { |
| 64 |
var element = evt.params.data.element |
| 65 |
var $element = jQuery(element) |
| 66 |
$element.detach() |
| 67 |
} |
| 68 |
prepare_preview_url() |
| 69 |
}) |
| 70 |
|
| 71 |
function prepare_preview_url() { |
| 72 |
let root_dir = '' |
| 73 |
let selected_wildcards = jQuery('.select-wildcards').val() |
| 74 |
|
| 75 |
if (selected_wildcards !== null) { |
| 76 |
root_dir = selected_wildcards.join('/') |
| 77 |
} |
| 78 |
|
| 79 |
jQuery('#sm_root_dir').val(root_dir) |
| 80 |
jQuery('#sm_root_dir').trigger('change') |
| 81 |
} |
| 82 |
|
| 83 |
function replace_wildcard_to_the_end(wildcards, remove_slashes = false) { |
| 84 |
if (remove_slashes) { |
| 85 |
jQuery('.select-wildcards > option').each(function () { |
| 86 |
if (this.value == '/') { |
| 87 |
jQuery(this).detach() |
| 88 |
} |
| 89 |
}) |
| 90 |
} |
| 91 |
wildcards.forEach(function (wildcard) { |
| 92 |
wildcard_exist = false |
| 93 |
jQuery('.select-wildcards > option').each(function () { |
| 94 |
if (this.value === wildcard && this.value !== '/') { |
| 95 |
jQuery(this).detach() |
| 96 |
jQuery('.select-wildcards').append(jQuery(this)) |
| 97 |
wildcard_exist = true |
| 98 |
} |
| 99 |
}) |
| 100 |
if (!wildcard_exist) { |
| 101 |
var newState = new Option('/', '/', true, true) |
| 102 |
jQuery(newState).prop('disabled', true) |
| 103 |
// Append it to the select |
| 104 |
jQuery('.select-wildcards').append(newState) |
| 105 |
} |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
// Application |
| 110 |
var wpStatelessApp = angular |
| 111 |
.module('wpStatelessApp', ['ngSanitize']) |
| 112 |
|
| 113 |
// Controllers |
| 114 |
.controller('wpStatelessSettings', function ($scope, $filter) { |
| 115 |
$scope.backup = {} |
| 116 |
$scope.sm = wp_stateless_settings || {} |
| 117 |
$scope.sm.readonly = $scope.sm.readonly || {} |
| 118 |
|
| 119 |
if ($scope.sm.network_admin) { |
| 120 |
$scope.sm.hashify_file_name = 'true' |
| 121 |
$scope.sm.readonly.hashify_file_name = true |
| 122 |
} |
| 123 |
|
| 124 |
$scope.$watch('sm.mode', function (value) { |
| 125 |
if ( |
| 126 |
(value == 'stateless' || value == 'ephemeral') && |
| 127 |
$scope.sm.readonly.hashify_file_name != 'constant' |
| 128 |
) { |
| 129 |
$scope.backup.hashify_file_name = $scope.sm.hashify_file_name |
| 130 |
$scope.sm.hashify_file_name = 'true' |
| 131 |
// $scope.apply(); |
| 132 |
} else { |
| 133 |
if ($scope.backup.hashify_file_name) { |
| 134 |
$scope.sm.hashify_file_name = $scope.backup.hashify_file_name |
| 135 |
// $scope.apply(); |
| 136 |
} |
| 137 |
} |
| 138 |
}) |
| 139 |
|
| 140 |
$scope.$watch('sm.bucket_folder_type', function (value) { |
| 141 |
if (value == 'single-site') { |
| 142 |
replace_wildcard_to_the_end(['/', '%date_year/date_month%', '/'], true) |
| 143 |
$wildcards_select |
| 144 |
.val(['/', '%date_year/date_month%', '/']) |
| 145 |
.trigger('change') |
| 146 |
} else if (value == 'multi-site') { |
| 147 |
//changins wildcards position |
| 148 |
replace_wildcard_to_the_end( |
| 149 |
['/', 'sites', '/', '%site_id%', '/', '%date_year/date_month%', '/'], |
| 150 |
true |
| 151 |
) |
| 152 |
$wildcards_select |
| 153 |
.val([ |
| 154 |
'/', |
| 155 |
'sites', |
| 156 |
'/', |
| 157 |
'%site_id%', |
| 158 |
'/', |
| 159 |
'%date_year/date_month%', |
| 160 |
'/', |
| 161 |
]) |
| 162 |
.trigger('change') |
| 163 |
} else if (value == '' && $scope.sm.network_admin) { |
| 164 |
$wildcards_select.val(null).trigger('change') |
| 165 |
} |
| 166 |
prepare_preview_url() |
| 167 |
}) |
| 168 |
|
| 169 |
$scope.$watch('sm.root_dir', function (value) { |
| 170 |
if (value == '%date_year/date_month%') { |
| 171 |
$scope.sm.bucket_folder_type = 'single-site' |
| 172 |
} else if (value == 'sites/%site_id%/%date_year/date_month%') { |
| 173 |
$scope.sm.bucket_folder_type = 'multi-site' |
| 174 |
} else if (value == '' && $scope.sm.network_admin) { |
| 175 |
$scope.sm.bucket_folder_type = '' |
| 176 |
} else { |
| 177 |
$scope.sm.bucket_folder_type = 'custom' |
| 178 |
} |
| 179 |
|
| 180 |
setTimeout(function () { |
| 181 |
jQuery('#permalink_structure').trigger('change') |
| 182 |
}, 1) |
| 183 |
}) |
| 184 |
|
| 185 |
var readonlyTag = $scope.sm.readonly.root_dir || false |
| 186 |
if (readonlyTag) { |
| 187 |
jQuery('.available-structure-tags .button') |
| 188 |
.off('click') |
| 189 |
.css('opacity', '.5') |
| 190 |
} |
| 191 |
$scope.tagClicked = function () { |
| 192 |
if (readonlyTag) return false |
| 193 |
$scope.sm.bucket_folder_type = 'custom' |
| 194 |
setTimeout(function () { |
| 195 |
jQuery('#permalink_structure').trigger('change') |
| 196 |
}, 1) |
| 197 |
} |
| 198 |
|
| 199 |
$scope.sm.showNotice = function (option) { |
| 200 |
if ($scope.sm.readonly && $scope.sm.readonly[option]) { |
| 201 |
var slug = $scope.sm.readonly[option] |
| 202 |
return $scope.sm.strings[slug] |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
$scope.sm.generatePreviewUrl = function () { |
| 207 |
$scope.sm.is_custom_domain = false |
| 208 |
var host = 'https://storage.googleapis.com/' |
| 209 |
var hash = |
| 210 |
$scope.sm.hashify_file_name == 'true' |
| 211 |
? Date.now().toString(36) + '-' |
| 212 |
: '' |
| 213 |
var is_ssl = $scope.sm.custom_domain.indexOf('https://') |
| 214 |
var custom_domain = $scope.sm.custom_domain.toString() |
| 215 |
var root_dir = $scope.sm.root_dir ? $scope.sm.root_dir : '' |
| 216 |
|
| 217 |
jQuery.each($scope.sm.wildcards, function (index, item) { |
| 218 |
var reg = new RegExp(index, 'g') |
| 219 |
root_dir = root_dir.replace(reg, item[0]) |
| 220 |
}) |
| 221 |
let tags = [ |
| 222 |
'%date_year%', |
| 223 |
'%date_month%', |
| 224 |
'%site_id%', |
| 225 |
'%site_url%', |
| 226 |
'%site_url_host%', |
| 227 |
'%site_url_path%', |
| 228 |
] |
| 229 |
let value_splitted = root_dir.split('/') |
| 230 |
for (let i = 0; i < value_splitted.length; i++) { |
| 231 |
if ( |
| 232 |
!/^[a-zA-Z0-9_\-.]+$/.test(value_splitted[i]) && |
| 233 |
value_splitted[i] != '' && |
| 234 |
jQuery.inArray(value_splitted[i], tags) == -1 |
| 235 |
) { |
| 236 |
value_splitted[i] = value_splitted[i].replace(/[^a-zA-Z0-9_\-.]/g, '') |
| 237 |
} |
| 238 |
} |
| 239 |
root_dir = value_splitted.join('/') |
| 240 |
root_dir = root_dir.replace(/(\/+)/g, '/') |
| 241 |
root_dir = root_dir.replace(/^\//, '') |
| 242 |
root_dir = root_dir.replace(/\/$/, '') |
| 243 |
if (root_dir) { |
| 244 |
root_dir = root_dir + '/' |
| 245 |
} |
| 246 |
|
| 247 |
custom_domain = custom_domain.replace(/\/+$/, '') // removing trailing slashes |
| 248 |
custom_domain = custom_domain.replace(/https?:\/\//, '') // removing http:// or https:// from the beginning. |
| 249 |
host += $scope.sm.bucket ? $scope.sm.bucket : '{bucket-name}' |
| 250 |
|
| 251 |
if ( |
| 252 |
custom_domain !== 'storage.googleapis.com' && |
| 253 |
$scope.sm.bucket && |
| 254 |
custom_domain && |
| 255 |
(is_ssl === 0 || custom_domain == $scope.sm.bucket) |
| 256 |
) { |
| 257 |
$scope.sm.is_custom_domain = true |
| 258 |
$scope.sm.is_ssl = is_ssl === 0 ? true : false |
| 259 |
host = is_ssl === 0 ? 'https://' : 'http://' // bucketname will be host |
| 260 |
host += custom_domain |
| 261 |
} |
| 262 |
|
| 263 |
$scope.sm.preview_url = |
| 264 |
host + '/' + root_dir + hash + 'your-image-name.jpeg' |
| 265 |
} |
| 266 |
|
| 267 |
$scope.sm.generatePreviewUrl() |
| 268 |
}) |
| 269 |
.controller('wpStatelessCompatibility', function ($scope, $filter) { |
| 270 |
$scope.modules = wp_stateless_compatibility || {} |
| 271 |
}) |
| 272 |
.controller('wpStatelessProcessing', function ($scope, $http) { |
| 273 |
/** |
| 274 |
* General Errors |
| 275 |
*/ |
| 276 |
$scope.errors = [] |
| 277 |
|
| 278 |
/** |
| 279 |
* Global level flag for the ability to run any of the processes |
| 280 |
*/ |
| 281 |
$scope.can_run = true |
| 282 |
|
| 283 |
/** |
| 284 |
* Block UI to prevent unwanted quick action sequences |
| 285 |
*/ |
| 286 |
$scope.blockUI = function () { |
| 287 |
$scope.can_run = false |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Unblock UI to allow further actions |
| 292 |
*/ |
| 293 |
$scope.unblockUI = function () { |
| 294 |
$scope.can_run = true |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Count percantage |
| 299 |
* @param {*} part |
| 300 |
* @param {*} base |
| 301 |
*/ |
| 302 |
$scope.percentage = function (part, base) { |
| 303 |
return parseInt((100 / base) * part) + '%' |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Processes Model |
| 308 |
*/ |
| 309 |
$scope.processes = { |
| 310 |
isLoading: false, |
| 311 |
classes: [], |
| 312 |
load: function () { |
| 313 |
var that = this |
| 314 |
that.isLoading = true |
| 315 |
$http({ |
| 316 |
method: 'GET', |
| 317 |
url: window.wpApiSettings.root + 'wp-stateless/v1/sync/getProcesses', |
| 318 |
headers: { |
| 319 |
'x-wps-auth': window.wp_stateless_configs.REST_API_TOKEN, |
| 320 |
}, |
| 321 |
}) |
| 322 |
.then(function (response) { |
| 323 |
that.classes = [] |
| 324 |
if (response && response.data) { |
| 325 |
var processes = response.data.data || {} |
| 326 |
for (var i in processes) { |
| 327 |
var sync = new ProcessingClass(processes[i]) |
| 328 |
if (sync.is_running) { |
| 329 |
sync.startPolling() |
| 330 |
} else { |
| 331 |
sync.stopPolling() |
| 332 |
} |
| 333 |
that.classes.push(sync) |
| 334 |
} |
| 335 |
} else { |
| 336 |
$scope.errors.push(window.stateless_l10n.something_went_wrong) |
| 337 |
} |
| 338 |
that.isLoading = false |
| 339 |
}) |
| 340 |
.catch(function (error) { |
| 341 |
$scope.errors.push( |
| 342 |
error.data.message || window.stateless_l10n.something_went_wrong |
| 343 |
) |
| 344 |
that.isLoading = false |
| 345 |
}) |
| 346 |
}, |
| 347 |
} |
| 348 |
|
| 349 |
// initialize stuff |
| 350 |
$scope.init = function () { |
| 351 |
ProcessingClass.prototype.$http = $http |
| 352 |
ProcessingClass.prototype.$scope = $scope |
| 353 |
$scope.processes.load() |
| 354 |
} |
| 355 |
|
| 356 |
// watch for any running process |
| 357 |
$scope.$watch( |
| 358 |
function (scope) { |
| 359 |
if (!scope.processes.classes.length) return false |
| 360 |
return Boolean( |
| 361 |
scope.processes.classes.find(function (process) { |
| 362 |
return process.is_running |
| 363 |
}) |
| 364 |
) |
| 365 |
}, |
| 366 |
function (is_running) { |
| 367 |
// Disable the ability to save settings if there are running processes |
| 368 |
jQuery('#save-settings,#save-compatibility').attr( |
| 369 |
'disabled', |
| 370 |
is_running |
| 371 |
) |
| 372 |
|
| 373 |
// maybe remove nag |
| 374 |
if (is_running) |
| 375 |
jQuery('#stateless-notice-processing-in-progress').show() |
| 376 |
else jQuery('#stateless-notice-processing-in-progress').hide() |
| 377 |
} |
| 378 |
) |
| 379 |
}) |
| 380 |
.controller('noJSWarning', function ($scope, $filter) { |
| 381 |
$scope.jsLoaded = true |
| 382 |
}) |
| 383 |
|
| 384 |
wpStatelessApp.filter('trust', [ |
| 385 |
'$sce', |
| 386 |
function ($sce) { |
| 387 |
return function (htmlCode) { |
| 388 |
return $sce.trustAsHtml(htmlCode) |
| 389 |
} |
| 390 |
}, |
| 391 |
]) |
| 392 |
|
| 393 |
/** |
| 394 |
* ProcessingClass |
| 395 |
* |
| 396 |
* @param {*} data |
| 397 |
*/ |
| 398 |
function ProcessingClass(data) { |
| 399 |
this.id = false |
| 400 |
this.total_items = 0 |
| 401 |
this.queued_items = 0 |
| 402 |
this.processed_items = 0 |
| 403 |
this.is_running = false |
| 404 |
this.limit = 0 |
| 405 |
this.order = 'desc' |
| 406 |
|
| 407 |
// Build an instance |
| 408 |
Object.assign(this, data) |
| 409 |
|
| 410 |
this.interval = null |
| 411 |
|
| 412 |
/** |
| 413 |
* Run sync |
| 414 |
*/ |
| 415 |
this.run = function () { |
| 416 |
var that = this |
| 417 |
this.is_running = true |
| 418 |
this.is_stopping = false |
| 419 |
this.$scope.blockUI() |
| 420 |
this.$http({ |
| 421 |
method: 'POST', |
| 422 |
url: window.wpApiSettings.root + 'wp-stateless/v1/sync/run', |
| 423 |
headers: { |
| 424 |
'x-wps-auth': window.wp_stateless_configs.REST_API_TOKEN, |
| 425 |
}, |
| 426 |
data: { |
| 427 |
id: this.id, |
| 428 |
limit: this.limit, |
| 429 |
order: this.order, |
| 430 |
}, |
| 431 |
}) |
| 432 |
.then(function (response) { |
| 433 |
if (response.data && response.data.ok) { |
| 434 |
that.startPolling() |
| 435 |
} else { |
| 436 |
that.$scope.errors.push( |
| 437 |
response.data.message || window.stateless_l10n.something_went_wrong |
| 438 |
) |
| 439 |
} |
| 440 |
}) |
| 441 |
.catch(function (error) { |
| 442 |
that.stopPolling() |
| 443 |
that.$scope.errors.push( |
| 444 |
error.data.message || window.stateless_l10n.something_went_wrong |
| 445 |
) |
| 446 |
}) |
| 447 |
.finally(function () { |
| 448 |
that.$scope.unblockUI() |
| 449 |
}) |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* If process can be ran |
| 454 |
*/ |
| 455 |
this.canRun = function () { |
| 456 |
return this.total_items > 0 && !this.is_running && this.$scope.can_run |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* If proccess can be stopped |
| 461 |
*/ |
| 462 |
this.canStop = function () { |
| 463 |
return this.is_running && !this.is_stopping |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Get Progress bar possible total |
| 468 |
*/ |
| 469 |
this.getProgressTotal = function () { |
| 470 |
if (this.limit > 0 && this.limit <= this.total_items) { |
| 471 |
return this.limit |
| 472 |
} |
| 473 |
return this.total_items |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Get Total queued count |
| 478 |
*/ |
| 479 |
this.getQueuedTotal = function () { |
| 480 |
return this.queued_items > this.total_items |
| 481 |
? this.total_items |
| 482 |
: this.queued_items |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Get processed total |
| 487 |
*/ |
| 488 |
this.getProcessedTotal = function () { |
| 489 |
return this.processed_items > this.getQueuedTotal() |
| 490 |
? this.getQueuedTotal() |
| 491 |
: this.processed_items |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Stop the process |
| 496 |
*/ |
| 497 |
this.stop = function () { |
| 498 |
var that = this |
| 499 |
this.is_stopping = true |
| 500 |
this.$http({ |
| 501 |
method: 'POST', |
| 502 |
url: window.wpApiSettings.root + 'wp-stateless/v1/sync/stop', |
| 503 |
headers: { |
| 504 |
'x-wps-auth': window.wp_stateless_configs.REST_API_TOKEN, |
| 505 |
}, |
| 506 |
data: { |
| 507 |
id: this.id, |
| 508 |
}, |
| 509 |
}) |
| 510 |
.then(function (response) { |
| 511 |
if (response.data && response.data.ok) { |
| 512 |
} |
| 513 |
}) |
| 514 |
.catch(function (error) { |
| 515 |
that.$scope.errors.push( |
| 516 |
error.data.message || window.stateless_l10n.something_went_wrong |
| 517 |
) |
| 518 |
}) |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Start polling for changes |
| 523 |
*/ |
| 524 |
this.startPolling = function () { |
| 525 |
var that = this |
| 526 |
this.stopPolling() |
| 527 |
this.interval = setInterval(function () { |
| 528 |
that.refresh() |
| 529 |
}, 5000) |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* Stop polling for changes |
| 534 |
*/ |
| 535 |
this.stopPolling = function () { |
| 536 |
clearInterval(this.interval) |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* Refresh the process data |
| 541 |
*/ |
| 542 |
this.refresh = function () { |
| 543 |
var that = this |
| 544 |
this.$http({ |
| 545 |
method: 'GET', |
| 546 |
url: |
| 547 |
window.wpApiSettings.root + |
| 548 |
'wp-stateless/v1/sync/getProcess/' + |
| 549 |
String(window.btoa(this.id)).replace(/=+/, ''), |
| 550 |
headers: { |
| 551 |
'x-wps-auth': window.wp_stateless_configs.REST_API_TOKEN, |
| 552 |
}, |
| 553 |
}) |
| 554 |
.then(function (response) { |
| 555 |
if ( |
| 556 |
response && |
| 557 |
response.data && |
| 558 |
response.data.ok && |
| 559 |
response.data.data |
| 560 |
) { |
| 561 |
if (!response.data.data.is_running) { |
| 562 |
that.stopPolling() |
| 563 |
that.queued_items = that.getProgressTotal() |
| 564 |
that.processed_items = that.getProgressTotal() |
| 565 |
setTimeout(function () { |
| 566 |
Object.assign(that, response.data.data) |
| 567 |
that.$scope.$apply() |
| 568 |
}, 3000) |
| 569 |
} else { |
| 570 |
Object.assign(that, response.data.data) |
| 571 |
} |
| 572 |
} |
| 573 |
}) |
| 574 |
.catch(function (error) { |
| 575 |
that.stopPolling() |
| 576 |
that.$scope.errors.push( |
| 577 |
error.data.message || window.stateless_l10n.something_went_wrong |
| 578 |
) |
| 579 |
}) |
| 580 |
} |
| 581 |
} |
| 582 |
|