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

MultisiteLanguageSwitcher.php (1 issue)

Severity

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