class-wpml-integrations-requirements.php
11.7 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
use WPML\Core\Twig_Loader_Filesystem;
use WPML\Core\Twig_Environment;
/**
* @author OnTheGo Systems
*/
class WPML_Integrations_Requirements {
const NOTICE_GROUP = 'requirements';
const CORE_REQ_NOTICE_ID = 'core-requirements';
const MISSING_REQ_NOTICE_ID = 'missing-requirements';
const EDITOR_NOTICE_ID = 'enable-translation-editor';
const DOCUMENTATION_LINK = 'https://wpml.org/documentation/translating-your-contents/page-builders/?utm_source=plugin&utm_medium=gui&utm_campaign=wpmlcore';
const DOCUMENTATION_LINK_BLOCK_EDITOR = 'https://wpml.org/?page_id=2909360&utm_source=wpmlplugin&utm_campaign=gutenberg&utm_medium=translation-editor&utm_term=translating-content-created-using-gutenberg-editor';
private $core_issues = array();
private $issues = array();
private $tm_settings;
private $should_create_editor_notice = false;
private $integrations;
private $requirements_scripts;
/** @var SitePress $sitepress */
private $sitepress;
/** @var WPML_Third_Party_Dependencies $third_party_dependencies */
private $third_party_dependencies;
/** @var WPML_Requirements_Notification $requirements_notification */
private $requirements_notification;
/**
* WPML_Integrations_Requirements constructor.
*
* @param SitePress $sitepress
* @param WPML_Third_Party_Dependencies $third_party_dependencies
* @param WPML_Requirements_Notification $requirements_notification
* @param array $integrations
*/
public function __construct(
SitePress $sitepress,
WPML_Third_Party_Dependencies $third_party_dependencies = null,
WPML_Requirements_Notification $requirements_notification = null,
$integrations = null
) {
$this->sitepress = $sitepress;
$this->third_party_dependencies = $third_party_dependencies;
$this->requirements_notification = $requirements_notification;
$this->tm_settings = $this->sitepress->get_setting( 'translation-management' );
$this->integrations = $integrations ? $integrations : $this->get_integrations();
}
public function init_hooks() {
if ( $this->sitepress->get_setting( 'setup_complete' ) ) {
add_action( 'admin_init', array( $this, 'init' ) );
add_action( 'wp_ajax_wpml_set_translation_editor', array( $this, 'set_translation_editor_callback' ) );
}
}
public function init() {
if ( $this->sitepress->get_wp_api()->is_back_end() ) {
$this->update_issues();
$this->update_notices();
if ( $this->core_issues || $this->issues ) {
$requirements_scripts = $this->get_requirements_scripts();
$requirements_scripts->add_plugins_activation_hook();
}
}
}
private function update_notices() {
$wpml_admin_notices = wpml_get_admin_notices();
if ( ! $this->core_issues ) {
$wpml_admin_notices->remove_notice( self::NOTICE_GROUP, self::CORE_REQ_NOTICE_ID );
}
if ( ! $this->issues ) {
$wpml_admin_notices->remove_notice( self::NOTICE_GROUP, self::MISSING_REQ_NOTICE_ID );
}
if ( ! $this->should_create_editor_notice ) {
$wpml_admin_notices->remove_notice( self::NOTICE_GROUP, self::EDITOR_NOTICE_ID );
}
if ( $this->core_issues || $this->issues || $this->should_create_editor_notice ) {
$notice_model = $this->get_notice_model();
$wp_api = $this->sitepress->get_wp_api();
$this->add_core_requirements_notice( $notice_model, $wpml_admin_notices, $wp_api );
$this->add_requirements_notice( $notice_model, $wpml_admin_notices, $wp_api );
$this->add_tm_editor_notice( $notice_model, $wpml_admin_notices, $wp_api );
}
}
private function update_issues() {
$this->core_issues = $this->get_third_party_dependencies()->get_issues( WPML_Integrations::SCOPE_WP_CORE );
$this->issues = $this->get_third_party_dependencies()->get_issues();
$this->update_should_create_editor_notice();
}
private function update_should_create_editor_notice() {
$editor_translation_set =
isset( $this->tm_settings['doc_translation_method'] ) &&
in_array(
(string) $this->tm_settings['doc_translation_method'],
array(
(string) ICL_TM_TMETHOD_EDITOR,
(string) ICL_TM_TMETHOD_ATE,
),
true
);
$requires_tm_editor = false;
foreach ( $this->integrations as $integration_item ) {
if ( in_array( 'wpml-translation-editor', $integration_item['notices-display'], true ) ) {
$requires_tm_editor = true;
break;
}
}
$this->should_create_editor_notice = ! $editor_translation_set && ! $this->issues && $requires_tm_editor;
}
public function set_translation_editor_callback() {
if ( ! $this->is_valid_request() ) {
wp_send_json_error( __( 'This action is not allowed', 'sitepress' ) );
} else {
$wpml_admin_notices = wpml_get_admin_notices();
$this->tm_settings['doc_translation_method'] = 1;
$this->sitepress->set_setting( 'translation-management', $this->tm_settings, true );
$this->sitepress->set_setting( 'doc_translation_method', 1, true );
$wpml_admin_notices->remove_notice( self::NOTICE_GROUP, self::EDITOR_NOTICE_ID );
wp_send_json_success();
}
}
private function is_valid_request() {
$valid_request = true;
if ( ! array_key_exists( 'nonce', $_POST ) ) {
$valid_request = false;
}
if ( $valid_request ) {
$nonce = $_POST['nonce'];
$nonce_is_valid = wp_verify_nonce( $nonce, 'wpml_set_translation_editor' );
if ( ! $nonce_is_valid ) {
$valid_request = false;
}
}
return $valid_request;
}
private function get_integrations() {
$integrations = new WPML_Integrations( $this->sitepress->get_wp_api() );
return $integrations->get_results();
}
/**
* @param string $notice_type
*
* @return array
*/
private function get_integrations_names( $notice_type ) {
$names = array();
foreach ( $this->integrations as $integration ) {
if ( in_array( $notice_type, $integration['notices-display'], true ) &&
! in_array( $integration['name'], $names, true ) ) {
$names[] = $integration['name'];
}
}
return $names;
}
/**
* @return WPML_Requirements_Notification
*/
private function get_notice_model() {
if ( ! $this->requirements_notification ) {
$template_paths = array(
WPML_PLUGIN_PATH . '/templates/warnings/',
);
$twig_loader = new Twig_Loader_Filesystem( $template_paths );
$environment_args = array();
if ( WP_DEBUG ) {
$environment_args['debug'] = true;
}
$twig = new Twig_Environment( $twig_loader, $environment_args );
$twig_service = new WPML_Twig_Template( $twig );
$this->requirements_notification = new WPML_Requirements_Notification( $twig_service );
}
return $this->requirements_notification;
}
/**
* @param WPML_Notice $notice
*/
private function add_actions_to_notice( WPML_Notice $notice ) {
$dismiss_action = new WPML_Notice_Action( __( 'Dismiss', 'sitepress' ), '#', true, false, true, false );
$notice->add_action( $dismiss_action );
if ( $this->has_issues( 'page-builders' ) ) {
$document_action = new WPML_Notice_Action( __( 'Translating content created with page builders', 'sitepress' ), self::DOCUMENTATION_LINK );
$notice->add_action( $document_action );
}
}
private function add_actions_to_core_notice( WPML_Notice $notice ) {
$dismiss_action = new WPML_Notice_Action( __( 'Dismiss', 'sitepress' ), '#', true, false, true, false );
$notice->add_action( $dismiss_action );
if ( $this->has_issues( WPML_Integrations::SCOPE_WP_CORE ) ) {
$document_action = new WPML_Notice_Action( __( 'How to translate Block editor content', 'sitepress' ), self::DOCUMENTATION_LINK_BLOCK_EDITOR );
$document_action->set_link_target( '_blank' );
$notice->add_action( $document_action );
}
}
/**
* @param string $type
*
* @return bool
*/
private function has_issues( $type ) {
$issues = WPML_Integrations::SCOPE_WP_CORE === $type
? $this->core_issues : $this->issues;
if ( array_key_exists( 'causes', $issues ) ) {
foreach ( (array) $issues['causes'] as $cause ) {
if ( $type === $cause['type'] ) {
return true;
}
}
}
return false;
}
/**
* @param WPML_Notice $notice
* @param WPML_WP_API $wp_api
*/
private function add_callbacks( WPML_Notice $notice, WPML_WP_API $wp_api ) {
if ( method_exists( $notice, 'add_display_callback' ) ) {
$notice->add_display_callback( array( $wp_api, 'is_core_page' ) );
$notice->add_display_callback( array( $wp_api, 'is_plugins_page' ) );
$notice->add_display_callback( array( $wp_api, 'is_themes_page' ) );
}
}
/**
* @param WPML_Requirements_Notification $notice_model
* @param WPML_Notices $wpml_admin_notices
* @param WPML_WP_API $wp_api
*/
private function add_core_requirements_notice( WPML_Requirements_Notification $notice_model, WPML_Notices $wpml_admin_notices, WPML_WP_API $wp_api ) {
if ( $this->core_issues ) {
$message = $notice_model->get_core_message( $this->core_issues );
$requirements_notice = new WPML_Notice( self::CORE_REQ_NOTICE_ID, $message, self::NOTICE_GROUP );
$this->add_actions_to_core_notice( $requirements_notice );
$requirements_notice->set_css_class_types( 'warning' );
$wpml_admin_notices->add_notice( $requirements_notice, true );
}
}
/**
* @param WPML_Requirements_Notification $notice_model
* @param WPML_Notices $wpml_admin_notices
* @param WPML_WP_API $wp_api
*/
private function add_requirements_notice( WPML_Requirements_Notification $notice_model, WPML_Notices $wpml_admin_notices, WPML_WP_API $wp_api ) {
if ( $this->issues ) {
$message = $notice_model->get_message( $this->issues, 1 );
$requirements_notice = new WPML_Notice( self::MISSING_REQ_NOTICE_ID, $message, self::NOTICE_GROUP );
$this->add_actions_to_notice( $requirements_notice );
$this->add_callbacks( $requirements_notice, $wp_api );
$wpml_admin_notices->add_notice( $requirements_notice, true );
}
}
/**
* @param WPML_Requirements_Notification $notice_model
* @param WPML_Notices $wpml_admin_notices
* @param WPML_WP_API $wp_api
*/
private function add_tm_editor_notice( WPML_Requirements_Notification $notice_model, WPML_Notices $wpml_admin_notices, WPML_WP_API $wp_api ) {
if ( $this->should_create_editor_notice ) {
$requirements_scripts = $this->get_requirements_scripts();
$requirements_scripts->add_translation_editor_notice_hook();
$integrations_names = $this->get_integrations_names( 'wpml-translation-editor' );
$text = $notice_model->get_settings( $integrations_names );
$notice = new WPML_TM_Editor_Notice( self::EDITOR_NOTICE_ID, $text, self::NOTICE_GROUP );
$notice->set_css_class_types( 'info' );
$enable_action = new WPML_Notice_Action( _x( 'Enable it now', 'Integration requirement notice title for translation editor: enable action', 'sitepress' ), '#', false, false, true );
$enable_action->set_js_callback( 'js-set-translation-editor' );
$notice->add_action( $enable_action );
$this->add_callbacks( $notice, $wp_api );
$this->add_actions_to_notice( $notice );
$wpml_admin_notices->add_notice( $notice );
}
}
/**
* @return WPML_Integrations_Requirements_Scripts
*/
private function get_requirements_scripts() {
if ( ! $this->requirements_scripts ) {
return new WPML_Integrations_Requirements_Scripts();
}
return $this->requirements_scripts;
}
/**
* @return WPML_Third_Party_Dependencies
*/
private function get_third_party_dependencies() {
if ( ! $this->third_party_dependencies ) {
$integrations = new WPML_Integrations( $this->sitepress->get_wp_api() );
$requirements = new WPML_Requirements();
$this->third_party_dependencies = new WPML_Third_Party_Dependencies( $integrations, $requirements );
}
return $this->third_party_dependencies;
}
}