|
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
|
|
|
* |
|
129
|
|
|
* @return boolean |
|
130
|
|
|
*/ |
|
131
|
|
|
public function verify_nonce() { |
|
132
|
|
|
return ( |
|
133
|
|
|
filter_has_var( INPUT_POST, 'msls_noncename' ) && |
|
134
|
|
|
wp_verify_nonce( filter_input( INPUT_POST, 'msls_noncename' ), MslsPlugin::path() ) |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Delete |
|
140
|
|
|
* |
|
141
|
|
|
* @param int $object_id |
|
142
|
|
|
* |
|
143
|
|
|
* @codeCoverageIgnore |
|
144
|
|
|
*/ |
|
145
|
|
|
public function delete( $object_id ) { |
|
146
|
|
|
$this->save( $object_id, MslsOptionsPost::class ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Save |
|
151
|
|
|
* |
|
152
|
|
|
* @param int $object_id |
|
153
|
|
|
* @param string $class |
|
154
|
|
|
* |
|
155
|
|
|
* @codeCoverageIgnore |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function save( $object_id, $class ) { |
|
158
|
|
|
if ( has_action( 'msls_main_save' ) ) { |
|
159
|
|
|
/** |
|
160
|
|
|
* Calls completely customized save-routine |
|
161
|
|
|
* @since 0.9.9 |
|
162
|
|
|
* |
|
163
|
|
|
* @param int $object_id |
|
164
|
|
|
* @param string Classname |
|
165
|
|
|
*/ |
|
166
|
|
|
do_action( 'msls_main_save', $object_id, $class ); |
|
167
|
|
|
|
|
168
|
|
|
return; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if ( ! $this->collection->has_current_blog() ) { |
|
172
|
|
|
$this->debugger( 'MslsBlogCollection::instance()->has_current_blog returns false.' ); |
|
173
|
|
|
|
|
174
|
|
|
return; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$language = $this->collection->get_current_blog()->get_language(); |
|
178
|
|
|
$msla = new MslsLanguageArray( $this->get_input_array( $object_id ) ); |
|
179
|
|
|
$options = new $class( $object_id ); |
|
180
|
|
|
$temp = $options->get_arr(); |
|
181
|
|
|
|
|
182
|
|
|
if ( 0 != $msla->get_val( $language ) ) { |
|
183
|
|
|
$options->save( $msla->get_arr( $language ) ); |
|
184
|
|
|
} else { |
|
185
|
|
|
$options->delete(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
foreach ( $this->collection->get() as $blog ) { |
|
189
|
|
|
switch_to_blog( $blog->userblog_id ); |
|
190
|
|
|
|
|
191
|
|
|
$language = $blog->get_language(); |
|
192
|
|
|
$larr_id = $msla->get_val( $language ); |
|
193
|
|
|
|
|
194
|
|
|
if ( 0 != $larr_id ) { |
|
195
|
|
|
$options = new $class( $larr_id ); |
|
196
|
|
|
$options->save( $msla->get_arr( $language ) ); |
|
197
|
|
|
} elseif ( isset( $temp[ $language ] ) ) { |
|
198
|
|
|
$options = new $class( $temp[ $language ] ); |
|
199
|
|
|
$options->delete(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
restore_current_blog(); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|