Completed
Push — master ( 09b4d3...bc573d )
by Dennis
05:04
created

MslsSelect.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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 33 and the first side effect is on line 107.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
Plugin Name: MslsSelect
5
Plugin URI: https://github.com/lloc/MslsSelect
6
Description: Transforms the output of the Multisite Language Switcher to an HTML select
7
Version: 1.2
8
Author: Dennis Ploetner
9
Author URI: http://lloc.de/
10
*/
11
12
/*
13
Copyright 2014  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
 * MslsSelect Class
31
 * @package mslsselect
32
 */
33
class MslsSelect {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
34
35
	const VERSION = '1.2';
36
37
	/**
38
	 * Init
39
	 *
40
	 * @return MslsSelect
41
	 */
42
	public function init() {
43
		$options = get_option( 'msls' );
44
		if ( empty( $options['output_current_blog'] ) ) {
45
			$options['output_current_blog'] = 1;
46
			update_option( 'msls', $options );
47
		}
48
49
		if ( ! is_admin() ) {
50
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
51
			add_filter( 'msls_output_get_tags', array( $this, 'get_tags' ) );
52
			add_filter( 'msls_output_get', array( $this, 'output_get' ), 10, 3 );
53
		}
54
55
		return $this;
56
	}
57
58
	/**
59
	 * Factory
60
	 *
61
	 * @return MslsSelect
62
	 */
63
	public static function create() {
64
		$obj = new self;
65
66
		add_action( 'plugins_loaded', array( $obj, 'init' ) );
67
68
		return $obj;
69
	}
70
71
	/**
72
	 * Enqueue scripts action
73
	 */
74
	public function enqueue_scripts() {
75
		wp_enqueue_script( 'mslsselect', plugins_url( '/js/mslsselect.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
76
	}
77
78
	/**
79
	 * Filter for the 'msls_output_get'-hook
80
	 *
81
	 * @param string $url
82
	 * @param StdClass $link
83
	 * @param bool $current
84
	 *
85
	 * @return string
86
	 */
87
	public function output_get( $url, $link, $current ) {
88
		return sprintf( '<option value="%s"%s>%s</option>', $url, ( $current ? ' selected="selected"' : '' ), $link->txt );
89
	}
90
91
	/**
92
	 * Filter for the 'msls_output_get_tags'-hook
93
	 *
94
	 * @return array
95
	 */
96
	public function get_tags() {
97
		return array(
98
			'before_item'   => '',
99
			'after_item'    => '',
100
			'before_output' => '<select class="msls_languages">',
101
			'after_output'  => '</select>',
102
		);
103
	}
104
105
}
106
107
MslsSelect::create();
108