Loader.php
3.71 KB
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
<?php
namespace WPML\TM\ATE\ClonedSites;
use WPML\Core\WP\App\Resources;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\LIB\WP\User;
use WPML\TM\ATE\ClonedSites\Endpoints\GetCredits as ClonedSitesGetCredits;
use WPML\TM\ATE\ClonedSites\Endpoints\Copy;
use WPML\TM\ATE\ClonedSites\Endpoints\Move;
use WPML\TM\ATE\ClonedSites\Endpoints\CopyWithCredits;
use WPML\TM\ATE\ClonedSites\Lock;
use WPML\TM\Templates\Notices\AteLocked;
use WPML\LIB\WP\Hooks;
use WPML\UIPage;
use function WPML\Container\make;
use function WPML\FP\invoke;
use function WPML\FP\spreadArgs;
class Loader implements \IWPML_Backend_Action, \IWPML_DIC_Action {
/** @var Lock */
private $lock;
public function __construct( Lock $lock ) {
$this->lock = $lock;
}
public function add_hooks() {
$displayPlaceholder = function () {
if ( $this->shouldRender() ) {
echo '<div id="wpml-tm-ate-lock-notice"></div>';
}
};
Hooks::onAction( 'admin_notices' )
->then( $displayPlaceholder );
Hooks::onFilter( 'wpml_tm_dashboard_notices' )
->then( spreadArgs( Lst::append( $displayPlaceholder ) ) );
Hooks::onAction( 'admin_enqueue_scripts' )
->then( function () {
if ( $this->shouldRender() ) {
$fn = Resources::enqueueApp( 'ate/clonedSites' );
$fn( $this->getData() );
}
} );
}
public function getData() {
$lockData = $this->lock->getLockData();
$urlCurrentlyRegisteredInAMS = Obj::prop( 'urlCurrentlyRegisteredInAMS', $lockData );
$urlUsedToMakeRequest = Obj::prop( 'urlUsedToMakeRequest', $lockData );
return [
'name' => 'ate_cloned_sites',
'data' => [
'hasRightToHandle' => User::isAdministrator(),
'endpoints' => [
'move' => Move::class,
'copy' => Copy::class,
'copyWithCredits' => CopyWithCredits::class,
'clonedSitesGetCredits' => ClonedSitesGetCredits::class,
],
'urls' => [
'toolsOnOldSite' => $urlCurrentlyRegisteredInAMS . '/wp-admin/' . UIPage::getTMATE() . '&widget_action=open_sites&force_code=1',
],
'settings' => [
'allowedModes' => apply_filters( 'wpml_ate_locked_allow_site_move_copy', [
'move' => true,
'copy' => true
] ),
'urlCurrentlyRegisteredInAMS' => $urlCurrentlyRegisteredInAMS,
'urlUsedToMakeRequest' => Obj::prop( 'urlUsedToMakeRequest', $lockData ),
'isOriginalSiteMovedToAnotherURL' => $lockData['identicalUrlBeforeMovement'],
],
],
];
}
private function shouldRender() {
return Lock::isLocked() && $this->shouldDisplayOnCurrentPage();
}
private function shouldDisplayOnCurrentPage() {
return $this->shouldDisplayOnScreen( [ 'dashboard' ] ) || $this->shouldDisplayOnPage( $this->getPages() );
}
/**
* @param array $screens
*
* @return bool
*/
private function shouldDisplayOnScreen( array $screens ) {
$currentScreen = \get_current_screen();
return $currentScreen instanceof \WP_Screen
&& in_array( $currentScreen->id, $screens );
}
/**
* @param array $pages
*
* @return bool
*/
private function shouldDisplayOnPage( array $pages ) {
return isset( $_GET['page'] ) && in_array( $_GET['page'], $pages );
}
/**
* @return array|string[]
*/
private function getPages() {
$pages = [
WPML_PLUGIN_FOLDER . '/menu/languages.php',
WPML_PLUGIN_FOLDER . '/menu/theme-localization.php',
WPML_PLUGIN_FOLDER . '/menu/settings.php',
WPML_PLUGIN_FOLDER . '/menu/support.php',
WPML_TM_FOLDER . '/menu/settings',
WPML_TM_FOLDER . '/menu/main.php',
WPML_TM_FOLDER . '/menu/translations-queue.php',
WPML_TM_FOLDER . '/menu/string-translation.php',
WPML_TM_FOLDER . '/menu/settings.php',
];
return $pages;
}
}