wpml-st-repair-strings-schema.php
2.64 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
<?php
class WPML_ST_Repair_Strings_Schema {
const OPTION_HAS_RUN = 'wpml_st_repair_string_schema_has_run';
/** @var IWPML_St_Upgrade_Command $upgrade_command */
private $upgrade_command;
/** @var WPML_Notices $notices */
private $notices;
/** @var array $args */
private $args;
/** @var string $db_error */
private $db_error;
/** @var array $has_run */
private $has_run = array();
public function __construct( WPML_Notices $notices, array $args, $db_error ) {
$this->notices = $notices;
$this->args = $args;
$this->db_error = $db_error;
}
public function set_command( IWPML_St_Upgrade_Command $upgrade_command ) {
$this->upgrade_command = $upgrade_command;
}
/** @return bool */
public function run() {
$this->has_run = get_option( self::OPTION_HAS_RUN, array() );
if ( ! $this->upgrade_command || array_key_exists( $this->get_command_id(), $this->has_run ) ) {
$this->add_notice();
return false;
}
if ( $this->run_upgrade_command() ) {
return true;
}
$this->add_notice();
return false;
}
/** @return bool */
private function run_upgrade_command() {
if ( ! $this->acquire_lock() ) {
return false;
}
$success = $this->upgrade_command->run();
$this->has_run[ $this->get_command_id() ] = true;
update_option( self::OPTION_HAS_RUN, $this->has_run, false );
$this->release_lock();
return (bool) $success;
}
private function get_command_id() {
return get_class( $this->upgrade_command );
}
/** @return bool */
private function acquire_lock() {
if ( get_transient( WPML_ST_Upgrade::TRANSIENT_UPGRADE_IN_PROGRESS ) ) {
return false;
}
set_transient( WPML_ST_Upgrade::TRANSIENT_UPGRADE_IN_PROGRESS, true, MINUTE_IN_SECONDS );
return true;
}
private function release_lock() {
delete_transient( WPML_ST_Upgrade::TRANSIENT_UPGRADE_IN_PROGRESS );
}
private function add_notice() {
$text = '<p>' . sprintf(
esc_html__( 'We have detected a problem with some tables in the database. Please contact %1$sWPML support%2$s to get this fixed.', 'wpml-string-translation' ),
'<a href="https://wpml.org/forums/forum/english-support/" class="otgs-external-link" rel="noopener" target="_blank">',
'</a>'
) . '</p>';
$text .= '<pre>' . $this->db_error . '</pre>';
if ( $this->upgrade_command ) {
$notice_id = $this->get_command_id();
} else {
$notice_id = 'default';
$text .= '<pre>' . print_r( $this->args, true ) . '</pre>';
}
$notice = $this->notices->create_notice( $notice_id, $text, __CLASS__ );
$notice->set_hideable( true );
$notice->set_css_class_types( array( 'notice-error' ) );
$this->notices->add_notice( $notice );
}
}