class-controller.php
6.12 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
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
<?php
/**
* BLC admin modal for legacy screens.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.1
*
* @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\Local;
// Abort if called directly.
defined( 'WPINC' ) || die;
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;
use WPMUDEV_BLC\Core\Traits\Dashboard_API;
/**
* Class Controller
*
* @package WPMUDEV_BLC\App\Admin_Modals\Local
*/
class Controller extends Base {
/**
* Use the Enqueue and Dashboard_API Traits.
*
* @since 2.1
*/
use Enqueue, Dashboard_API;
/**
* The unique id that can be used by react.
*
* @since 2.1
* @var int $unique_id
*
*/
public static $unique_id = null;
/**
* The admin pages the notice will be visible at.
*
* @since 2.1
* @var array $admin_pages
*
*/
protected $admin_pages = array();
/**
* Init function.
*
* @since 2.1
*/
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' ) );
self::$unique_id = Utilities::get_unique_id();
View::instance()::$unique_id = self::$unique_id;
$this->prepare_props();
$this->prepare_scripts();
}
}
/**
* Checks if admin page actions/scripts should load in current screen.
*
* @since 2.1
* @return boolean Checks if admin page actions/scripts should load. Useful for enqueuing scripts.
*/
public function can_boot() {
/*
* Show modal only when Local BLC is active.
*/
//if ( boolval( self::site_connected() ) || empty( boolval( Settings::instance()->get( 'use_legacy_blc_version' ) ) ) ) {
if ( ! current_user_can( 'manage_options' ) || empty( Settings::instance()->get( 'use_legacy_blc_version' ) ) ) {
return false;
}
/*
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_local_page` key which is also used in
WPMUDEV_BLC\App\Admin_Notices\Legacy\Controller class.
*/
if ( ! isset( Utilities::$value_provider['boot_admin_local_page'] ) ) {
Utilities::$value_provider['boot_admin_local_page'] = Utilities::is_admin_screen( $this->admin_pages() );
}
$legacy_option = Settings::instance()->get( 'use_legacy_blc_version' );
return Utilities::$value_provider['boot_admin_local_page'] && ! empty( $legacy_option );
}
protected function admin_pages() {
return array(
'blc_local',
);
}
/**
* Prepares the properties of the Admin Page.
*
* @since 2.1
* @return void
*/
public function prepare_props() {
}
/**
* Register scripts for the admin page.
*
* @since 2.1
* @return array Register scripts for the admin page.
*/
public function set_admin_scripts() {
$script_data = include WPMUDEV_BLC_DIR . 'assets/js/local-modal/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;
$signup_url = add_query_arg(
array(
'utm_source' => 'blc',
'utm_medium' => 'plugin',
'utm_campaign' => 'blc_plugin_onboarding',
),
Utilities::signup_url()
);
return array(
'blc_local_modal' => array(
'src' => $this->scripts_dir . 'local-modal/main.js',
'deps' => $dependencies,
'ver' => $version,
'in_footer' => true,
'localize' => array(
'blc_local_modal' => array(
'data' => array(
'rest_url' => esc_url_raw( rest_url() ),
'settings_endpoint' => '/wpmudev_blc/v1/settings',
'unique_id' => self::$unique_id,
'nonce' => wp_create_nonce( 'wp_rest' ),
'legacy_active' => boolval( Settings::instance()->get( 'use_legacy_blc_version' ) ),
'local_blc_url' => admin_url( 'admin.php?page=blc_local' ),
'cloud_blc_url' => admin_url( 'admin.php?page=blc_dash' ),
'assets_url' => WPMUDEV_BLC_ASSETS_URL,
'site_connected' => (bool) self::site_connected(),
'expired_membership' => boolval( Utilities::membership_expired() ),
'hub_signup_url' => $signup_url,
'dash_installed' => boolval( Utilities::dash_plugin_installed() ),
'dash_active' => boolval( Utilities::dash_plugin_active() ),
),
'labels' => array(
'error_messages' => array(
'general' => __( 'Something went wrong here.', 'broken-link-checker' ),
),
),
),
),
'translate' => true,
),
// END OF blc_activation_popup.
);
}
/**
* Register css files for admin page.
*
* @return array
*/
public function set_admin_styles() {
return array(
'blc_legacy_modal' => array(
'src' => $this->styles_dir . 'local-modal.min.css',
'ver' => $this->scripts_version(),
),
);
}
protected function scripts_version() {
static $scripts_version = null;
if ( is_null( $scripts_version ) ) {
$script_data = include WPMUDEV_BLC_DIR . 'assets/js/local-modal/main.asset.php';
$scripts_version = $script_data['version'] ?? WPMUDEV_BLC_SCIPTS_VERSION;
}
return $scripts_version;
}
/**
* Admin Menu Callback.
*
* @since 2.1
* @return void The callback function of the Admin Menu Page.
*/
public function output() {
View::instance()->render( array( 'unique_id' => self::$unique_id ) );
}
/**
* Adds the blc legacy class to body classes for styling purposes.
*
* @param string $classes The body classes.
*
* @since 2.1
* @return string Returns the body classes.
*/
public function admin_body_classes( string $classes = '' ) {
return $classes . ' blc-show-local-modal';
}
}