1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace lloc\Msls\ContentImport\LogWriters; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use lloc\Msls\ContentImport\ImportCoordinates; |
7
|
|
|
use lloc\Msls\MslsRegistryInstance; |
8
|
|
|
|
9
|
|
|
class AdminNoticeLogger extends MslsRegistryInstance implements LogWriter { |
10
|
|
|
protected $transient = 'msls_last_import_log'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var ImportCoordinates |
14
|
|
|
*/ |
15
|
|
|
protected $import_coordinates; |
16
|
|
|
|
17
|
|
|
public function write( array $data ) { |
18
|
|
|
$message = '<h3>' . esc_html__( 'Multisite Language Switcher last import report', 'multisite-language-switcher' ) . '</h3>'; |
19
|
|
|
$message .= '<b>' . sprintf( |
20
|
|
|
esc_html__( 'From post %d on site %d to post %d on site %d', 'multisite-language-switcher' ), |
|
|
|
|
21
|
|
|
$this->import_coordinates->source_post_id, |
|
|
|
|
22
|
|
|
$this->import_coordinates->source_blog_id, |
|
|
|
|
23
|
|
|
$this->import_coordinates->dest_post_id, |
|
|
|
|
24
|
|
|
$this->import_coordinates->dest_blog_id |
|
|
|
|
25
|
|
|
) . '</b>'; |
|
|
|
|
26
|
|
|
if ( ! empty( $data['info'] ) ) { |
27
|
|
|
$section_title = esc_html__( 'General information', 'multisite-language-switcher' ); |
28
|
|
|
$entries = $data['info']; |
29
|
|
|
$message .= $this->get_section_html( $section_title, $entries ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ( ! empty( $data['success'] ) ) { |
33
|
|
|
$section_title = esc_html__( 'Details', 'multisite-language-switcher' ); |
34
|
|
|
$success_data = $data['success']; |
35
|
|
|
$success_entries = []; |
36
|
|
|
|
37
|
|
|
if ( isset( $success_data['post-field']['added'] ) ) { |
38
|
|
|
$success_entries[] = esc_html__( 'The following post fields have been set: ', 'multisite-language-switcher' ) . |
39
|
|
|
'<code>' . implode( '</code>, <code>', array_keys( $success_data['post-field']['added'] ) ) . '</code>.'; |
40
|
|
|
} |
41
|
|
|
if ( isset( $success_data['meta']['added'] ) ) { |
42
|
|
|
$success_entries[] = esc_html__( 'The following post meta have been set: ', 'multisite-language-switcher' ) . |
43
|
|
|
'<code>' . implode( '</code>, <code>', array_keys( $success_data['meta']['added'] ) ) . '</code>.'; |
44
|
|
|
} |
45
|
|
|
if ( isset( $success_data['term']['added'] ) ) { |
46
|
|
|
$success_entries[] = esc_html__( 'Terms have been assigned to the post for the following taxonomies: ', 'multisite-language-switcher' ) . |
47
|
|
|
'<code>' . implode( '</code>, <code>', array_keys( $success_data['term']['added'] ) ) . '</code>.'; |
48
|
|
|
} |
49
|
|
|
if ( isset( $success_data['post-thumbnail']['set'] ) ) { |
50
|
|
|
$success_entries[] = esc_html__( 'The post thumbnail has been set.', 'multisite-language-switcher' ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$message .= $this->get_section_html( $section_title, $success_entries, false ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ( ! empty( $data['error'] ) ) { |
57
|
|
|
$section_title = esc_html__( 'Errors:', 'multisite-language-switcher' ); |
58
|
|
|
$error_data = $data['error']; |
59
|
|
|
$error_entries = []; |
60
|
|
|
if ( isset( $error_data['term']['added'] ) || isset( $error_data['term']['created'] ) ) { |
61
|
|
|
$taxonomies = isset( $error_data['term']['added'] ) ? array_keys( $error_data['term']['added'] ) : []; |
62
|
|
|
$taxonomies = isset( $error_data['term']['created'] ) ? array_merge( $taxonomies, array_keys( $error_data['term']['created'] ) ) : $taxonomies; |
63
|
|
|
$error_entries[] = esc_html__( 'There were issues creating or assigning terms for the following taxonomies: ', 'multisite-language-switcher' ) . |
64
|
|
|
'<code>' . implode( '</code>, <code>', $taxonomies ) . '</code>.'; |
65
|
|
|
} |
66
|
|
|
if ( isset( $error_data['post-thumbnail']['set'] ) || isset( $error_data['post-thumbnail']['created'] ) ) { |
67
|
|
|
$error_entries[] = esc_html__( 'The post thumbnail could not be created or set.', 'multisite-language-switcher' ); |
68
|
|
|
} |
69
|
|
|
$message .= $this->get_section_html( $section_title, $error_entries, false ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$html = '<div class="notice notice-success is-dismissible"><p>' . $message . '</p></div>'; |
73
|
|
|
|
74
|
|
|
switch_to_blog( $this->import_coordinates->dest_blog_id ); |
|
|
|
|
75
|
|
|
|
76
|
|
|
set_transient( $this->transient, $html, HOUR_IN_SECONDS ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function get_section_html( $section_title, $entries, $escape_entries = true ) { |
80
|
|
|
$html = '<h3>' . $section_title . '</h3>'; |
81
|
|
|
$html .= '<ul>'; |
82
|
|
|
foreach ( $entries as $entry ) { |
83
|
|
|
if ( $escape_entries ) { |
84
|
|
|
$html .= '<li>' . esc_html( $entry ) . '</li>'; |
85
|
|
|
} else { |
86
|
|
|
$html .= '<li>' . $entry . '</li>'; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
$html .= '</ul>'; |
90
|
|
|
|
91
|
|
|
return $html; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function show_last_log($echo = true) { |
95
|
|
|
if ( ! ( $html = get_transient( $this->transient ) ) ) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ( $echo ) { |
100
|
|
|
echo $html; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// we've shown it, no reason to keep it |
104
|
|
|
delete_transient( $this->transient ); |
105
|
|
|
|
106
|
|
|
return $html; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function set_import_coordinates( $import_coordinates ) { |
110
|
|
|
$this->import_coordinates = $import_coordinates; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the name of the transient where the logger will store the output HTML. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function get_transient() { |
119
|
|
|
return $this->transient; |
120
|
|
|
} |
121
|
|
|
} |