-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathAdminNoticeLogger.php
150 lines (129 loc) · 4.85 KB
/
AdminNoticeLogger.php
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
<?php
namespace lloc\Msls\ContentImport\LogWriters;
use lloc\Msls\ContentImport\ImportCoordinates;
use lloc\Msls\MslsRegistryInstance;
class AdminNoticeLogger extends MslsRegistryInstance implements LogWriter {
protected $transient = 'msls_last_import_log';
/**
* @var ImportCoordinates
*/
protected $import_coordinates;
public function write( array $data ) {
/* translators: %1$d: source post ID, %2$d: source blog ID, %3$d: destination post ID, %4$d: destination blog ID */
$format = esc_html__( 'From post %1$d on site %2$d to post %3$d on site %4$d', 'multisite-language-switcher' );
$message = '<h3>' . esc_html__( 'Multisite Language Switcher last import report', 'multisite-language-switcher' ) . '</h3>';
$message .= '<b>' . sprintf(
$format,
$this->import_coordinates->source_post_id,
$this->import_coordinates->source_blog_id,
$this->import_coordinates->dest_post_id,
$this->import_coordinates->dest_blog_id
) . '</b>';
if ( ! empty( $data['info'] ) ) {
$section_title = esc_html__( 'General information', 'multisite-language-switcher' );
$entries = $data['info'];
$message .= $this->get_section_html( $section_title, $entries );
}
if ( ! empty( $data['success'] ) ) {
$section_title = esc_html__( 'Details', 'multisite-language-switcher' );
$success_data = $data['success'];
$success_entries = array();
if ( isset( $success_data['post-field']['added'] ) ) {
$success_entries[] = esc_html__(
'The following post fields have been set: ',
'multisite-language-switcher'
) .
'<code>' . implode(
'</code>, <code>',
array_keys( $success_data['post-field']['added'] )
) . '</code>.';
}
if ( isset( $success_data['meta']['added'] ) ) {
$success_entries[] = esc_html__(
'The following post meta have been set: ',
'multisite-language-switcher'
) .
'<code>' . implode(
'</code>, <code>',
array_keys( $success_data['meta']['added'] )
) . '</code>.';
}
if ( isset( $success_data['term']['added'] ) ) {
$success_entries[] = esc_html__(
'Terms have been assigned to the post for the following taxonomies: ',
'multisite-language-switcher'
) .
'<code>' . implode(
'</code>, <code>',
array_keys( $success_data['term']['added'] )
) . '</code>.';
}
if ( isset( $success_data['post-thumbnail']['set'] ) ) {
$success_entries[] = esc_html__( 'The post thumbnail has been set.', 'multisite-language-switcher' );
}
$message .= $this->get_section_html( $section_title, $success_entries, false );
}
if ( ! empty( $data['error'] ) ) {
$section_title = esc_html__( 'Errors:', 'multisite-language-switcher' );
$error_data = $data['error'];
$error_entries = array();
if ( isset( $error_data['term']['added'] ) || isset( $error_data['term']['created'] ) ) {
$taxonomies = isset( $error_data['term']['added'] ) ? array_keys( $error_data['term']['added'] ) : array();
$taxonomies = isset( $error_data['term']['created'] ) ? array_merge(
$taxonomies,
array_keys( $error_data['term']['created'] )
) : $taxonomies;
$error_entries[] = esc_html__(
'There were issues creating or assigning terms for the following taxonomies: ',
'multisite-language-switcher'
) .
'<code>' . implode( '</code>, <code>', $taxonomies ) . '</code>.';
}
if ( isset( $error_data['post-thumbnail']['set'] ) || isset( $error_data['post-thumbnail']['created'] ) ) {
$error_entries[] = esc_html__(
'The post thumbnail could not be created or set.',
'multisite-language-switcher'
);
}
$message .= $this->get_section_html( $section_title, $error_entries, false );
}
$html = '<div class="notice notice-success is-dismissible"><p>' . $message . '</p></div>';
switch_to_blog( $this->import_coordinates->dest_blog_id );
set_transient( $this->transient, $html, HOUR_IN_SECONDS );
}
protected function get_section_html( $section_title, $entries, $escape_entries = true ) {
$html = '<h3>' . $section_title . '</h3>';
$html .= '<ul>';
foreach ( $entries as $entry ) {
if ( $escape_entries ) {
$html .= '<li>' . esc_html( $entry ) . '</li>';
} else {
$html .= '<li>' . $entry . '</li>';
}
}
$html .= '</ul>';
return $html;
}
public function show_last_log( $echo = true ) {
if ( ! ( $html = get_transient( $this->transient ) ) ) {
return;
}
if ( $echo ) {
echo $html;
}
// we've shown it, no reason to keep it
delete_transient( $this->transient );
return $html;
}
public function set_import_coordinates( $import_coordinates ) {
$this->import_coordinates = $import_coordinates;
}
/**
* Returns the name of the transient where the logger will store the output HTML.
*
* @return string
*/
public function get_transient() {
return $this->transient;
}
}