Completed
Push — master ( 354d11...90fd5e )
by Dennis
02:13
created

MultisiteLanguageSwitcher.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
Plugin Name: Multisite Language Switcher
5
Plugin URI: http://msls.co/
6
Description: A simple but powerful plugin that will help you to manage the relations of your contents in a multilingual multisite-installation.
7
Version: 1.0.6
8
Author: Dennis Ploetner
9
Author URI: http://lloc.de/
10
*/
11
12
/*
13
Copyright 2013  Dennis Ploetner  (email : [email protected])
14
15
This program is free software; you can redistribute it and/or modify
16
it under the terms of the GNU General Public License, version 2, as
17
published by the Free Software Foundation.
18
19
This program is distributed in the hope that it will be useful,
20
but WITHOUT ANY WARRANTY; without even the implied warranty of
21
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
GNU General Public License for more details.
23
24
You should have received a copy of the GNU General Public License
25
along with this program; if not, write to the Free Software
26
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
27
*/
28
29
/**
30
 * MultisiteLanguageSwitcher
31
 *
32
 * @author Dennis Ploetner <[email protected]>
33
 */
34
if ( ! defined( 'MSLS_PLUGIN_VERSION' ) ) {
35
	define( 'MSLS_PLUGIN_VERSION', '1.0.5' );
36
37
	if ( ! defined( 'MSLS_PLUGIN_PATH' ) ) {
38
		define( 'MSLS_PLUGIN_PATH', plugin_basename( __FILE__ ) );
39
	}
40
	if ( ! defined( 'MSLS_PLUGIN__FILE__' ) ) {
41
		define( 'MSLS_PLUGIN__FILE__', __FILE__ );
42
	}
43
44
	/**
45
	 * The Autoloader does all the magic when it comes to include a file
46
	 * @since 0.9.8
47
	 * @package Msls
48
	 */
49
	class MslsAutoloader {
50
51
		/**
52
		 * Static loader method
53
		 * @param string $class
54
		 */
55
		public static function load( $class ) {
56
			if ( 'Msls' == substr( $class, 0, 4 ) ) {
57
				require_once dirname( __FILE__ ) . '/includes/' . $class . '.php';
58
			}
59
		}
60
61
	}
62
63
	/**
64
	 * The autoload-stack could be inactive so the function will return false
65
	 */
66
	if ( in_array( '__autoload', (array) spl_autoload_functions() ) ) {
67
		spl_autoload_register( '__autoload' );
68
	}
69
	spl_autoload_register( array( 'MslsAutoloader', 'load' ) );
70
71
	/**
72
	 * Interface for classes which are to register in the MslsRegistry-instance
73
	 *
74
	 * get_called_class is just avalable in php >= 5.3 so I defined an interface here
75
	 * @package Msls
76
	 */
77
	interface IMslsRegistryInstance {
78
79
		/**
80
		 * Returnse an instance
81
		 * @return object
82
		 */
83
		public static function instance();
84
85
	}
86
87
	register_uninstall_hook( __FILE__, array( 'MslsPlugin', 'uninstall' ) );
88
89
	add_action( 'plugins_loaded', array( 'MslsPlugin', 'init_i18n_support' ) );
90
91
	if ( function_exists( 'is_multisite' ) && is_multisite() ) {
92
		add_action( 'widgets_init', array( 'MslsPlugin', 'init_widget' ) );
93
		add_filter( 'locale', array( 'MslsPlugin', 'set_admin_language' ) );
94
95
		if ( is_admin() ) {
96
			add_action( 'admin_menu', array( 'MslsPlugin', 'init' ) );
97
			add_action( 'admin_menu', array( 'MslsAdmin', 'init' ) );
98
99
			add_action( 'load-post.php', array( 'MslsMetaBox', 'init' ) );
100
101
			add_action( 'load-post-new.php', array( 'MslsMetaBox', 'init' ) );
102
103
			add_action( 'load-edit.php', array( 'MslsCustomColumn', 'init' ) );
104
			add_action( 'load-edit.php', array( 'MslsCustomFilter', 'init' ) );
105
106
			add_action( 'load-edit-tags.php', array( 'MslsCustomColumnTaxonomy', 'init' ) );
107
			add_action( 'load-edit-tags.php', array( 'MslsPostTag', 'init' ) );
108
109
			if ( filter_has_var( INPUT_POST, 'action' ) ) {
110
				$action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_STRING );
111
112
				if ( 'add-tag' == $action ) {
113
					add_action( 'admin_init', array( 'MslsPostTag', 'init' ) );
114
				}
115
				elseif ( 'inline-save' == $action ) {
116
					add_action( 'admin_init', array( 'MslsCustomColumn', 'init' ) );
117
				}
118
				elseif ( 'inline-save-tax' == $action ) {
119
					add_action( 'admin_init', array( 'MslsCustomColumnTaxonomy', 'init' ) );
120
				}
121
			}
122
123
			add_action( 'wp_ajax_suggest_posts', array( 'MslsMetaBox', 'suggest' ) );
124
125
			add_action( 'wp_ajax_suggest_terms', array( 'MslsPostTag', 'suggest' ) );
126
		}
127
128
		/**
129
		 * Filter for the_content()
130
		 *
131
		 * @package Msls
132
		 * @uses MslsOptions
133
		 * @param string $content
134
		 * @return string
135
		 */
136
		function msls_content_filter( $content ) {
137
			if ( ! is_front_page() && is_singular() ) {
138
				$options = MslsOptions::instance();
139
				if ( $options->is_content_filter() ) {
140
					$content .= msls_filter_string();
141
				}
142
			}
143
			return $content;
144
		}
145
		add_filter( 'the_content', 'msls_content_filter' );
146
147
		/**
148
		 * Create filterstring for msls_content_filter()
149
		 *
150
		 * @package Msls
151
		 * @uses MslsOutput
152
		 * @param string $pref
153
		 * @param string $post
154
		 * @return string
155
		 */
156
		function msls_filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
157
			$obj    = new MslsOutput();
158
			$links  = $obj->get( 1, true, true );
159
			$output = __( 'This post is also available in %s.', 'msls' );
160
161
			if ( has_filter( 'msls_filter_string' ) ) {
162
				/**
163
				 * Overrides the string for the output of the translation hint
164
				 * @since 1.0
165
				 * @param string $output
166
				 * @param array $links
167
				 */
168
				$output = apply_filters( 'msls_filter_string', $output, $links );
169
			}
170
			else {
171
				if ( count( $links ) > 1 ) {
172
					$last   = array_pop( $links );
173
					$output = sprintf(
174
						$output,
175
						sprintf(
176
							__( '%s and %s', 'msls' ),
177
							implode( ', ', $links ),
178
							$last
179
						)
180
					);
181
				}
182
				elseif ( 1 == count( $links ) ) {
183
					$output = sprintf(
184
						$output,
185
						$links[0]
186
					);
187
				}
188
				else {
189
					$output = '';
190
				}
191
			}
192
			return( ! empty( $output ) ? $pref . $output . $post : '' );
193
		}
194
195
		/**
196
		 * Get the output for using the links to the translations in your code
197
		 *
198
		 * @package Msls
199
		 * @param array $arr
200
		 * @return string
201
		 */
202
		function get_the_msls( $arr = array() ) {
203
			$obj = MslsOutput::init()->set_tags( (array) $arr );
204
			return( sprintf( '%s', $obj ) );
205
		}
206
		add_shortcode( 'sc_msls', 'get_the_msls' );
207
208
		/**
209
		 * Output the links to the translations in your template
210
		 *
211
		 * You can call this function directly like that
212
		 *
213
		 *     if ( function_exists ( 'the_msls' ) )
214
		 *         the_msls();
215
		 *
216
		 * or just use it as shortcode [sc_msls]
217
		 *
218
		 * @package Msls
219
 	 	 * @uses get_the_msls
0 ignored issues
show
Tabs must be used to indent lines; spaces are not allowed
Loading history...
220
		 * @param array $arr
221
		 */
222
		function the_msls( $arr = array() ) {
223
			echo get_the_msls( $arr ); // xss ok
224
		}
225
226
		/**
227
		 * Help searchengines to index and to serve the localized version with
228
		 * rel="alternate"-links in the html-header
229
		 */
230
		function msls_head() {
231
			$blogs  = MslsBlogCollection::instance();
232
			$mydata = MslsOptions::create();
233
			foreach ( $blogs->get_objects() as $blog ) {
234
				$language = $blog->get_language();
235
				$url      = $mydata->get_current_link();
236
				$current  = ( $blog->userblog_id == MslsBlogCollection::instance()->get_current_blog_id() );
237
				$title = $blog->get_description();
238
239
				if ( ! $current ) {
240
					switch_to_blog( $blog->userblog_id );
0 ignored issues
show
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...
241
242
					if ( 'MslsOptions' != get_class( $mydata ) && ( is_null( $mydata ) || ! $mydata->has_value( $language ) ) ) {
243
						restore_current_blog();
244
						continue;
245
					}
246
					$url = $mydata->get_permalink( $language );
247
					$title = $blog->get_description();
248
249
					restore_current_blog();
250
				}
251
252
				if ( has_filter( 'msls_head_hreflang' ) ) {
253
					/**
254
					 * Overrides the hreflang value
255
					 * @since 0.9.9
256
					 * @param string $language
257
					 */
258
					$hreflang = (string) apply_filters( 'msls_head_hreflang', $language );
259
				}
260
				else {
261
					$hreflang = $blog->get_alpha2();
262
				}
263
264
				printf(
265
					'<link rel="alternate" hreflang="%s" href="%s" title="%s" />',
266
					$hreflang,
267
					$url,
268
					esc_attr( $title )
269
				);
270
				echo "\n";
271
			}
272
		}
273
		add_action( 'wp_head', 'msls_head' );
274
275
	}
276
	else {
277
278
		/**
279
		 * Prints a message that the Multisite Language Switcher needs an
280
		 * active multisite to work properly.
281
		 */
282
		function plugin_needs_multisite() {
283
			MslsPlugin::message_handler(
284
				__( '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="http://codex.wordpress.org/Create_A_Network">this post</a> if you don\'t know the meaning.', 'msls' )
285
			);
286
		}
287
		add_action( 'admin_notices', 'plugin_needs_multisite' );
288
289
	}
290
}
291