Completed
Pull Request — master (#109)
by Luca
04:24
created

MslsPlugin   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 172
rs 10
c 0
b 0
f 0
wmc 24
lcom 0
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 23 4
A init_widget() 0 7 2
A init_i18n_support() 0 7 1
B set_admin_language() 0 9 5
A message_handler() 0 11 2
B uninstall() 0 25 4
A cleanup() 0 11 2
A get_superglobals() 0 16 4
1
<?php
2
/**
3
 * MslsPlugin
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
/**
9
 * Provides functionalities for general hooks and activation/deactivation
10
 * @package Msls
11
 */
12
class MslsPlugin {
13
14
	/**
15
	 * Loads styles and some js if needed
16
	 * The methiod returns true if JS is loaded or false if not
17
 	 * @return boolean
18
	 */
19
	public static function init() {
20
		$postfix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG
21
			? ''
22
			: '.min';
23
24
		wp_enqueue_style(
25
			'msls-styles',
26
			plugins_url( 'css/msls.css', MSLS_PLUGIN__FILE__ ),
27
			array(),
28
			MSLS_PLUGIN_VERSION
29
		);
30
31
		if ( MslsOptions::instance()->activate_autocomplete ) {
32
			wp_enqueue_script(
33
				'msls-autocomplete',
34
				plugins_url( "js/msls{$postfix}.js", MSLS_PLUGIN__FILE__ ),
35
				array( 'jquery-ui-autocomplete' ),
36
				MSLS_PLUGIN_VERSION
37
			);
38
			return true;
39
		}
40
		return false;
41
	}
42
43
	/**
44
	 * Register widget
45
	 *
46
	 * The widget will only be registered if the current blog is not
47
	 * excluded in the configuration of the plugin.
48
	 * @return boolean
49
	 */
50
	public static function init_widget() {
51
		if ( ! MslsOptions::instance()->is_excluded() ) {
52
			register_widget( 'MslsWidget' );
53
			return true;
54
		}
55
		return false;
56
	}
57
58
	/**
59
	 * Load textdomain
60
	 *
61
	 * The method will be executed allways on init because we have some
62
	 * translatable string in the frontend too.
63
	 * @return boolean
64
	 */
65
	public static function init_i18n_support() {
66
		return load_plugin_textdomain(
67
			'multisite-language-switcher',
68
			false,
69
			dirname( MSLS_PLUGIN_PATH ) . '/languages/'
70
		);
71
	}
72
73
	/**
74
	 * Set the admin language
75
	 * Callback for 'locale' hook
76
	 * @param string $locale
77
	 * @return string
78
	 */
79
	public static function set_admin_language( $locale ) {
80
		if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
81
			$code = MslsOptions::instance()->admin_language;
82
			if ( ! empty( $code ) ) {
83
				return $code;
84
			}
85
		}
86
		return $locale;
87
	}
88
89
	/**
90
	 * Message handler
91
	 *
92
	 * Prints a message box to the screen.
93
	 * @param string $message
94
	 * @param string $css_class
95
	 * @return boolean
96
	 */
97
	public static function message_handler( $message, $css_class = 'error' ) {
98
		if ( ! empty( $message ) ) {
99
			printf(
100
				'<div id="msls-warning" class="%s"><p>%s</p></div>',
101
				$css_class,
102
				$message
103
			);
104
			return true;
105
		}
106
		return false;
107
	}
108
109
	/**
110
	 * Uninstall plugin
111
	 *
112
	 * The plugin data in all blogs of the current network will be
113
	 * deleted after the uninstall procedure.
114
	 * @return boolean
115
	 */
116
	public static function uninstall() {
117
		/**
118
		 * We want to be sure that the user has not deactivated the
119
		 * multisite because we need to use switch_to_blog and
120
		 * restore_current_blog
121
		 */
122
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
123
			$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ );
124
125
			$blogs = $cache->get_results(
126
				$cache->prepare(
127
					"SELECT blog_id FROM {$cache->blogs} WHERE blog_id != %d AND site_id = %d",
128
					$cache->blogid,
129
					$cache->siteid
130
				)
131
			);
132
133
			foreach ( $blogs as $blog ) {
134
				switch_to_blog( $blog->blog_id );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
135
				self::cleanup();
136
				restore_current_blog();
137
			}
138
		}
139
		return self::cleanup();
140
	}
141
142
	/**
143
	 * Cleanup the options
144
	 *
145
	 * Removes all values of the current blogs which are stored in the
146
	 * options-table and returns true if it was successful.
147
	 * @return boolean
148
	 */
149
	public static function cleanup() {
150
		if ( delete_option( 'msls' ) ) {
151
			$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ );
152
			$sql   = $cache->prepare(
153
				"DELETE FROM {$cache->options} WHERE option_name LIKE %s",
154
				'msls_%'
155
			);
156
			return (bool) $cache->query( $sql );
157
		}
158
		return false;
159
	}
160
161
	/**
162
	 * Get specific vars from $_POST and $_GET in a safe way
163
	 * @param array $list
164
	 * @return array
165
	 */
166
	public static function get_superglobals( array $list ) {
167
		$arr = array();
168
169
		foreach ( $list as $var ) {
170
			if ( filter_has_var( INPUT_POST, $var ) ) {
171
				$arr[ $var ] = filter_input( INPUT_POST, $var );
172
			}
173
			elseif ( filter_has_var( INPUT_GET, $var ) ) {
174
				$arr[ $var ] = filter_input( INPUT_GET, $var );
175
			} else {
176
				$arr[ $var ] = '';
177
			}
178
		}
179
180
		return $arr;
181
	}
182
183
}
184