class-controller.php
5.16 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
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
<?php
/**
* BLC admin modal for legacy screens.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\App\Admin_Notices\Legacy
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\App\Admin_Modals\Legacy;
// Abort if called directly.
defined( 'WPINC' ) || die;
use WPMUDEV_BLC\Core\Controllers\Admin_Modals;
use WPMUDEV_BLC\App\Options\Settings\Model as Settings;
use WPMUDEV_BLC\Core\Utils\Utilities;
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
use WPMUDEV_BLC\Core\Traits\Enqueue;
/**
* Class Controller
*
* @package WPMUDEV_BLC\App\Admin_Pages\Dashboard
*/
class Controller extends Base {
/**
* Use the Enqueue Trait.
*
* @since 2.0.0
*/
use Enqueue;
/**
* The unique id that can be used by react.
*
* @var int $unique_id
*
* @since 2.0.0
*/
public static $unique_id = null;
/**
* The admin pages the notice will be visible at.
*
* @var array $admin_pages
*
* @since 2.0.0
*/
protected $admin_pages = array();
/**
* Init function.
*
* @since 2.0.0
*/
public function init() {
add_action( 'current_screen', array( $this, 'boot' ), 11 );
}
/**
* Boots modal parts.
*/
public function boot() {
if ( $this->can_boot() ) {
add_filter( 'admin_body_class', array( $this, 'admin_body_classes' ), 999 );
add_action( 'admin_footer', array( $this, 'output' ) );
$this->unique_id = Utilities::get_unique_id();
$this->prepare_props();
$this->prepare_scripts();
}
}
/**
* Checks if admin page actions/scripts should load in current screen.
*
* @return boolean Checks if admin page actions/scripts should load. Useful for enqueuing scripts.
* @since 2.0.0
*/
public function can_boot() {
/*
We use the Utilities::$value_provider array variable.
This variable can hold values that can be used from different classes which should help avoid checking
same conditions multiple times.
In this case we are using `boot_admin_legacy_pages` key which is also used in
WPMUDEV_BLC\App\Admin_Notices\Legacy\Controller class.
*/
if ( ! isset( Utilities::$value_provider['boot_admin_legacy_pages'] ) ) {
Utilities::$value_provider['boot_admin_legacy_pages'] = Utilities::is_admin_screen( $this->admin_pages );
}
$legacy_option = Settings::instance()->get( 'use_legacy_blc_version' );
return Utilities::$value_provider['boot_admin_legacy_pages'] && empty( $legacy_option );
}
/**
* Prepares the properties of the Admin Page.
*
* @return void
* @since 2.0.0
*/
public function prepare_props() {
/*
* Set the admin pages the notice will be visible at.
*/
$this->admin_pages = array(
'view-broken-links',
'link-checker-settings',
);
}
/**
* Admin Menu Callback.
*
* @return void The callback function of the Admin Menu Page.
* @since 2.0.0
*/
public function output() {
View::instance()->render( array( 'unique_id' => $this->unique_id ) );
}
/**
* Register css files for admin page.
*
* @return array
*/
public function set_admin_styles() {
return array(
'blc_sui' => array(
'src' => $this->styles_dir . 'shared-ui-' . BLC_SHARED_UI_VERSION_NUMBER . '.min.css',
'ver' => WPMUDEV_BLC_SCIPTS_VERSION,
),
'blc_legacy_modal' => array(
'src' => $this->styles_dir . 'legacy-modal.min.css',
'ver' => WPMUDEV_BLC_SCIPTS_VERSION,
),
);
}
/**
* Adds the blc legacy class to body classes for styling purposes.
*
* @param string $classes The body classes.
* @return string Returns the body classes.
* @since 2.0.0
*/
public function admin_body_classes( string $classes = '' ) {
return $classes . ' blc-show-legacy-popup';
}
/**
* Register scripts for the admin page.
*
* @return array Register scripts for the admin page.
* @since 2.0.0
*/
public function set_admin_scripts() {
$script_data = include WPMUDEV_BLC_DIR . 'assets/js/activation-popup/main.asset.php';
$dependencies = $script_data['dependencies'] ?? array(
'react',
'wp-element',
'wp-i18n',
'wp-is-shallow-equal',
'wp-polyfill',
);
$version = $script_data['version'] ?? WPMUDEV_BLC_SCIPTS_VERSION;
return array(
'blc_legacy_popup' => array(
'src' => $this->scripts_dir . 'legacy-modal/main.js',
'deps' => $dependencies,
'ver' => $version,
'in_footer' => true,
'localize' => array(
'blc_legacy_popup' => array(
'data' => array(
'rest_url' => esc_url_raw( rest_url() ),
'settings_endpoint' => '/wpmudev_blc/v1/settings',
'unique_id' => $this->unique_id,
'nonce' => wp_create_nonce( 'wp_rest' ),
'site_connected' => false,
'show_legacy_link' => boolval( Settings::instance()->get( 'use_legacy_blc_version' ) ),
'legacy_blc_url' => admin_url( 'admin.php?page=view-broken-links' ),
'blc_url' => admin_url( 'admin.php?page=blc_dash' ),
),
'labels' => array(
'error_messages' => array(
'general' => __( 'Something went wrong here.', 'broken-link-checker' ),
),
),
),
),
'translate' => true,
),
// END OF blc_activation_popup.
);
}
}