1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MslsMain |
4
|
|
|
* @author Dennis Ploetner <[email protected]> |
5
|
|
|
* @since 0.9.8 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace lloc\Msls; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Abstraction for the hook classes |
12
|
|
|
* |
13
|
|
|
* @package Msls |
14
|
|
|
*/ |
15
|
|
|
class MslsMain { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Instance of options |
19
|
|
|
* |
20
|
|
|
* @var MslsOptions |
21
|
|
|
*/ |
22
|
|
|
protected $options; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Collection of blog objects |
26
|
|
|
* |
27
|
|
|
* @var MslsBlogCollection |
28
|
|
|
*/ |
29
|
|
|
protected $collection; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param MslsOptions $options |
35
|
|
|
* @param MslsBlogCollection $collection |
36
|
|
|
*/ |
37
|
|
|
public function __construct( MslsOptions $options, MslsBlogCollection $collection ) { |
38
|
|
|
$this->options = $options; |
39
|
|
|
$this->collection = $collection; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Factory |
44
|
|
|
* |
45
|
|
|
* @codeCoverageIgnore |
46
|
|
|
* |
47
|
|
|
* @return static |
48
|
|
|
*/ |
49
|
|
|
public static function init() { |
50
|
|
|
$options = MslsOptions::instance(); |
51
|
|
|
$collection = MslsBlogCollection::instance(); |
52
|
|
|
|
53
|
|
|
return new static( $options, $collection ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Prints a message in the error log if WP_DEBUG is true |
58
|
|
|
* |
59
|
|
|
* @param mixed $message |
60
|
|
|
*/ |
61
|
|
|
public function debugger( $message ) { |
62
|
|
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) { |
|
|
|
|
63
|
|
|
if ( is_array( $message ) || is_object( $message ) ) { |
64
|
|
|
$message = print_r( $message, true ); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
error_log( 'MSLS Debug: ' . $message ); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the input array |
72
|
|
|
* |
73
|
|
|
* @param int $object_id |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function get_input_array( $object_id ) { |
78
|
|
|
$arr = []; |
79
|
|
|
|
80
|
|
|
$current_blog = $this->collection->get_current_blog(); |
81
|
|
|
if ( ! is_null( $current_blog ) ) { |
82
|
|
|
$arr[ $current_blog->get_language() ] = (int) $object_id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$input_post = filter_input_array( INPUT_POST ); |
86
|
|
|
if ( ! is_array( $input_post ) ) { |
87
|
|
|
return $arr; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
foreach ( $input_post as $k => $v ) { |
91
|
|
|
list ( $key, $value ) = $this->get_input_value( $k, $v ); |
92
|
|
|
if ( $value ) { |
93
|
|
|
$arr[ $key ] = $value; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $arr; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Prepare input key/value-pair |
102
|
|
|
* @param $key |
103
|
|
|
* @param $value |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
protected function get_input_value( $key, $value ) { |
108
|
|
|
if ( false === strpos( $key, 'msls_input_' ) || empty( $value ) ) { |
109
|
|
|
return [ '', 0 ]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return [ substr( $key, 11 ), intval( $value ) ]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Checks if the current input comes from the autosave-functionality |
117
|
|
|
* |
118
|
|
|
* @param int $post_id |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function is_autosave( $post_id ) { |
123
|
|
|
return ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Checks for the nonce in the INPUT_POST |
128
|
|
|
* @return boolean |
129
|
|
|
*/ |
130
|
|
|
public function verify_nonce() { |
131
|
|
|
return ( |
132
|
|
|
filter_has_var( INPUT_POST, 'msls_noncename' ) && |
133
|
|
|
wp_verify_nonce( filter_input( INPUT_POST, 'msls_noncename' ), MSLS_PLUGIN_PATH ) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Delete |
139
|
|
|
* |
140
|
|
|
* @param int $object_id |
141
|
|
|
* |
142
|
|
|
* @codeCoverageIgnore |
143
|
|
|
*/ |
144
|
|
|
public function delete( $object_id ) { |
145
|
|
|
$this->save( $object_id, MslsOptionsPost::class ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Save |
150
|
|
|
* |
151
|
|
|
* @param int $object_id |
152
|
|
|
* @param string $class |
153
|
|
|
* |
154
|
|
|
* @codeCoverageIgnore |
155
|
|
|
*/ |
156
|
|
|
protected function save( $object_id, $class ) { |
157
|
|
|
if ( has_action( 'msls_main_save' ) ) { |
158
|
|
|
/** |
159
|
|
|
* Calls completely customized save-routine |
160
|
|
|
* @since 0.9.9 |
161
|
|
|
* |
162
|
|
|
* @param int $object_id |
163
|
|
|
* @param string Classname |
164
|
|
|
*/ |
165
|
|
|
do_action( 'msls_main_save', $object_id, $class ); |
166
|
|
|
|
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if ( ! $this->collection->has_current_blog() ) { |
171
|
|
|
$this->debugger( 'MslsBlogCollection::instance()->has_current_blog returns false.' ); |
172
|
|
|
|
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$language = $this->collection->get_current_blog()->get_language(); |
177
|
|
|
$msla = new MslsLanguageArray( $this->get_input_array( $object_id ) ); |
178
|
|
|
$options = new $class( $object_id ); |
179
|
|
|
$temp = $options->get_arr(); |
180
|
|
|
|
181
|
|
|
if ( 0 != $msla->get_val( $language ) ) { |
182
|
|
|
$options->save( $msla->get_arr( $language ) ); |
183
|
|
|
} else { |
184
|
|
|
$options->delete(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
foreach ( $this->collection->get() as $blog ) { |
188
|
|
|
switch_to_blog( $blog->userblog_id ); |
|
|
|
|
189
|
|
|
|
190
|
|
|
$language = $blog->get_language(); |
191
|
|
|
$larr_id = $msla->get_val( $language ); |
192
|
|
|
|
193
|
|
|
if ( 0 != $larr_id ) { |
194
|
|
|
$options = new $class( $larr_id ); |
195
|
|
|
$options->save( $msla->get_arr( $language ) ); |
196
|
|
|
} elseif ( isset( $temp[ $language ] ) ) { |
197
|
|
|
$options = new $class( $temp[ $language ] ); |
198
|
|
|
$options->delete(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
restore_current_blog(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
} |
206
|
|
|
|