forked from lloc/Multisite-Language-Switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMslsPlugin.php
More file actions
256 lines (219 loc) · 7.05 KB
/
Copy pathMslsPlugin.php
File metadata and controls
256 lines (219 loc) · 7.05 KB
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
<?php declare( strict_types=1 );
namespace lloc\Msls;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
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;
}
/**
* @codeCoverageIgnore
*/
public static function init(): void {
$obj = new self( msls_options() );
register_activation_hook( self::file(), array( __CLASS__, '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( 'rest_api_init', array( MslsRestApi::class, 'init' ) );
add_action( 'init', array( MslsAdminBar::class, 'init' ) );
add_action( 'init', array( MslsBlock::class, 'init' ) );
add_action( 'init', array( MslsShortCode::class, 'init' ) );
add_action( 'init', array( MslsContentFilter::class, 'init' ) );
add_action( 'widgets_init', array( MslsWidget::class, 'init' ) );
add_action( 'wp_head', array( __CLASS__, 'print_alternate_links' ) );
add_filter( 'msls_get_output', 'msls_output' );
\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 ) ) {
switch ( MslsRequest::get_var( MslsFields::FIELD_ACTION ) ) {
case 'add-tag':
add_action( 'admin_init', array( MslsPostTag::class, 'init' ) );
break;
case 'inline-save':
add_action( 'admin_init', array( MslsCustomColumn::class, 'init' ) );
break;
case 'inline-save-tax':
add_action( 'admin_init', array( MslsCustomColumnTaxonomy::class, 'init' ) );
break;
}
}
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 );
}
);
}
}
public static function print_alternate_links(): void {
echo msls_output()->get_alternate_links(), PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Loads styles and some js if needed
*/
public function custom_enqueue(): void {
if ( ! is_admin_bar_showing() ) {
return;
}
$ver = defined( 'MSLS_PLUGIN_VERSION' ) ? constant( 'MSLS_PLUGIN_VERSION' ) : false;
$folder = defined( 'SCRIPT_DEBUG' ) && constant( 'SCRIPT_DEBUG' ) ? 'src' : 'assets/js';
wp_enqueue_style( 'msls-styles', self::plugins_url( 'assets/css/msls.css' ), array(), $ver );
wp_enqueue_style( 'msls-flags', self::plugins_url( 'assets/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, array( 'in_footer' => true ) );
}
if ( $this->options->activate_quick_create ) {
wp_enqueue_script( 'msls-quick-create', self::plugins_url( "$folder/msls-quick-create.js" ), array( 'jquery', 'wp-api-fetch' ), $ver, array( 'in_footer' => true ) );
}
}
/**
* 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' ) : '';
}
/**
* 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 ) ) {
echo wp_kses_post(
sprintf(
'<div id="msls-warning" class="%s"><p>%s</p></div>',
esc_attr( $css_class ),
$message
)
);
return true;
}
return false;
}
/**
* Activate plugin
*/
public static function activate(): void {
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 void
*/
public static function uninstall(): void {
/**
* 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__ );
$blog_ids = ( new BlogsInNetworkQuery( $sql_cache ) )();
foreach ( $blog_ids as $new_blog_id ) {
switch_to_blog( $new_blog_id );
self::cleanup();
restore_current_blog();
}
}
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;
}
}