-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathMslsPlugin.php
349 lines (297 loc) · 8.82 KB
/
MslsPlugin.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
namespace lloc\Msls;
use lloc\Msls\Query\BlogsInNetworkQuery;
use lloc\Msls\Query\CleanupOptionsQuery;
/**
* Provides functionalities for general hooks and activation/deactivation
*
* @package Msls
*/
class MslsPlugin {
/**
* Injected MslsOptions object
*
* @var MslsOptions
*/
protected $options;
/**
* MslsPlugin constructor.
*
* @param MslsOptions $options
*/
public function __construct( MslsOptions $options ) {
$this->options = $options;
}
/**
* Factory
*
* @codeCoverageIgnore
*
* @return MslsPlugin
*/
public static function init() {
$obj = new self( msls_options() );
add_action( 'plugins_loaded', array( $obj, 'init_i18n_support' ) );
register_activation_hook( self::file(), array( $obj, 'activate' ) );
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
add_action( 'admin_enqueue_scripts', array( $obj, 'custom_enqueue' ) );
add_action( 'wp_enqueue_scripts', array( $obj, 'custom_enqueue' ) );
add_action( 'init', array( MslsAdminBar::class, 'init' ) );
add_action( 'init', array( MslsBlock::class, 'init' ) );
add_action( 'init', array( MslsShortCode::class, 'init' ) );
add_action( 'widgets_init', array( MslsWidget::class, 'init' ) );
add_action( 'wp_head', array( __CLASS__, 'print_alternate_links' ) );
add_filter( 'msls_get_output', array( __CLASS__, 'get_output' ) );
add_filter( 'the_content', array( $obj, 'content_filter' ) );
\lloc\Msls\ContentImport\Service::instance()->register();
if ( is_admin() ) {
add_action( 'admin_menu', array( MslsAdmin::class, 'init' ) );
add_action( 'load-post.php', array( MslsMetaBox::class, 'init' ) );
add_action( 'load-post-new.php', array( MslsMetaBox::class, 'init' ) );
add_action( 'load-edit.php', array( MslsCustomColumn::class, 'init' ) );
add_action( 'load-edit.php', array( MslsCustomFilter::class, 'init' ) );
add_action( 'load-edit-tags.php', array( MslsCustomColumnTaxonomy::class, 'init' ) );
add_action( 'load-edit-tags.php', array( MslsPostTag::class, 'init' ) );
add_action( 'load-term.php', array( MslsPostTag::class, 'init' ) );
if ( MslsRequest::has_var( MslsFields::FIELD_ACTION ) ) {
$action = MslsRequest::has_var( MslsFields::FIELD_ACTION );
if ( 'add-tag' === $action ) {
add_action( 'admin_init', array( MslsPostTag::class, 'init' ) );
} elseif ( 'inline-save' === $action ) {
add_action( 'admin_init', array( MslsCustomColumn::class, 'init' ) );
} elseif ( 'inline-save-tax' === $action ) {
add_action( 'admin_init', array( MslsCustomColumnTaxonomy::class, 'init' ) );
}
}
add_action( 'wp_ajax_suggest_posts', array( MslsMetaBox::class, 'suggest' ) );
add_action( 'wp_ajax_suggest_terms', array( MslsPostTag::class, 'suggest' ) );
}
} else {
add_action(
'admin_notices',
function () {
/* translators: %s: URL to the WordPress Codex. */
$format = __(
'The Multisite Language Switcher needs the activation of the multisite-feature for working properly. Please read <a onclick="window.open(this.href); return false;" href="%s">this post</a> if you don\'t know the meaning.',
'multisite-language-switcher'
);
$message = sprintf(
$format,
esc_url( 'https://developer.wordpress.org/advanced-administration/multisite/create-network/' )
);
self::message_handler( $message );
}
);
}
return $obj;
}
/**
* Gets MslsOutput object
*
* @return MslsOutput
*/
public static function get_output() {
static $obj = null;
if ( is_null( $obj ) ) {
$obj = MslsOutput::init();
}
return $obj;
}
/**
* Callback for action wp_head
*/
public static function print_alternate_links() {
echo self::get_output()->get_alternate_links(), PHP_EOL;
}
/**
* Filter for the_content()
*
* @param string $content
*
* @return string
*/
public function content_filter( string $content ) {
if ( ! is_front_page() && is_singular() && $this->options->is_content_filter() ) {
$content .= $this->filter_string();
}
return $content;
}
/**
* Create filter-string for msls_content_filter()
*
* @param string $pref
* @param string $post
*
* @return string
*/
public function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
$obj = MslsOutput::init();
$links = $obj->get( 1, true, true );
/* translators: %s: list of languages */
$output = __( 'This post is also available in %s.', 'multisite-language-switcher' );
if ( has_filter( 'msls_filter_string' ) ) {
/**
* Overrides the string for the output of the translation hint
*
* @param string $output
* @param array $links
*
* @since 1.0
*/
$output = apply_filters( 'msls_filter_string', $output, $links );
} elseif ( count( $links ) > 1 ) {
$last = array_pop( $links );
/* translators: %1$s: list of languages separated by a comma, %2$s: last language */
$format = __( '%1$s and %2$s', 'multisite-language-switcher' );
$output = sprintf(
$output,
sprintf(
$format,
implode( ', ', $links ),
$last
)
);
} elseif ( 1 == count( $links ) ) {
$output = sprintf(
$output,
$links[0]
);
}
return ! empty( $output ) ? $pref . $output . $post : '';
}
/**
* Loads styles and some js if needed
*
* The method returns true if the autocomplete-option is activated, false otherwise.
*
* @return boolean
*/
public function custom_enqueue() {
if ( ! is_admin_bar_showing() ) {
return false;
}
$ver = defined( 'MSLS_PLUGIN_VERSION' ) ? constant( 'MSLS_PLUGIN_VERSION' ) : false;
$folder = defined( 'SCRIPT_DEBUG' ) && constant( 'SCRIPT_DEBUG' ) ? 'src' : 'js';
wp_enqueue_style( 'msls-styles', self::plugins_url( 'css/msls.css' ), array(), $ver );
wp_enqueue_style( 'msls-flags', self::plugins_url( 'css-flags/css/flag-icon.min.css' ), array(), $ver );
if ( $this->options->activate_autocomplete ) {
wp_enqueue_script( 'msls-autocomplete', self::plugins_url( "$folder/msls.js" ), array( 'jquery-ui-autocomplete' ), $ver );
return true;
}
return false;
}
/**
* Wrapper for plugins_url
*
* @param string $path
*
* @return string
*/
public static function plugins_url( string $path ): string {
return plugins_url( $path, self::file() );
}
/**
* Wrapper for plugin_dir_path
*
* @param string $path
*
* @return string
*/
public static function plugin_dir_path( string $path ): string {
return plugin_dir_path( self::file() ) . $path;
}
/**
* @param string $path
*
* @return string
*/
public static function dirname( string $path ): string {
return dirname( self::path() ) . $path;
}
/**
* @return string
*/
public static function file(): string {
return defined( 'MSLS_PLUGIN__FILE__' ) ? constant( 'MSLS_PLUGIN__FILE__' ) : '';
}
/**
* @return string
*/
public static function path(): string {
return defined( 'MSLS_PLUGIN_PATH' ) ? constant( 'MSLS_PLUGIN_PATH' ) : '';
}
/**
* Load textdomain
*
* The method should be executed always on init because we have some
* translatable string in the frontend too.
*
* @return boolean
*/
public function init_i18n_support() {
return load_plugin_textdomain( 'multisite-language-switcher', false, self::dirname( '/languages/' ) );
}
/**
* Message handler
*
* Prints a message box to the screen.
*
* @param string $message
* @param string $css_class
*
* @return boolean
*/
public static function message_handler( $message, $css_class = 'error' ) {
if ( ! empty( $message ) ) {
printf( '<div id="msls-warning" class="%s"><p>%s</p></div>', $css_class, $message );
return true;
}
return false;
}
/**
* Activate plugin
*/
public static function activate() {
register_uninstall_hook( self::file(), array( __CLASS__, 'uninstall' ) );
}
/**
* Uninstall plugin
*
* The plugin data in all blogs of the current network will be
* deleted after the uninstall procedure.
*
* @return boolean
*/
public static function uninstall() {
/**
* We want to be sure that the user has not deactivated the
* multisite because we need to use switch_to_blog and
* restore_current_blog
*/
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
$blogs = ( new BlogsInNetworkQuery( $sql_cache ) )();
foreach ( $blogs as $blog ) {
switch_to_blog( $blog->blog_id );
self::cleanup();
restore_current_blog();
}
}
return self::cleanup();
}
/**
* Cleanup the options
*
* Removes all values of the current blogs which are stored in the
* options-table and returns true if it was successful.
*
* @return boolean
*/
public static function cleanup() {
if ( delete_option( 'msls' ) ) {
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
return ( new CleanupOptionsQuery( $sql_cache ) )();
}
return false;
}
}