Completed
Push — master ( 81af79...161bc3 )
by Dennis
01:55
created

MslsMenu.php (8 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 34 and the first side effect is on line 170.

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: MslsMenu
5
Plugin URI: https://github.com/lloc/MslsMenu
6
Description: Adds the Multisite Language Switcher to the primary-nav-menu
7
Version: 1.3.1
8
Author: Dennis Ploetner
9
Author URI: http://lloc.de/
10
Text Domain: mslsmenu
11
*/
12
13
/*
14
Copyright 2014  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
 * MslsMenu Class
32
 * @package mslsmenu
33
 */
34
class MslsMenu {
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...
35
36
	/**
37
	 * MslsMenu constructor.
38
	 */
39
	public function __construct() {
40
		add_filter( 'wp_nav_menu_items', array( $this, 'nav_item' ), 10, 2 );
41
		add_action( 'msls_admin_register', array( $this, 'admin_register' ) );
42
	}
43
44
	/**
45
	 * Plugin init
46
	 */
47
	public static function init() {
48
		load_plugin_textdomain( 'mslsmenu', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
49
50
		return new self;
51
	}
52
53
	/**
54
	 * Callback for wp_nav_menu_items
55
	 *
56
	 * @param string $items
57
	 * @param StdClass $args
58
	 *
59
	 * @return string
60
	 */
61
	function nav_item( $items, $args ) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
		if ( function_exists( 'the_msls' ) ) {
63
			$options   = MslsOptions::instance();
64
			$locations = (array) $options->mslsmenu_theme_location;
65
66
			if ( in_array( $args->theme_location, $locations ) ) {
67
				$mslsmenu = '';
68
69
				$obj = new MslsOutput;
70
				foreach ( $obj->get( (int) $options->mslsmenu_display ) as $item ) {
71
					$mslsmenu .= $options->mslsmenu_before_item . $item . $options->mslsmenu_after_item;
72
				}
73
74
				$items .= $options->mslsmenu_before_output . $mslsmenu . $options->mslsmenu_after_output;
75
			}
76
		}
77
78
		return $items;
79
	}
80
81
	/**
82
	 * Callback for msls_admin_register
83
	 *
84
	 * @param string $page
85
	 */
86
	function admin_register( $page ) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
87
		add_settings_section( 'mslsmenu_section', __( 'Menu Settings', 'mslsmenu' ), null, $page );
88
89
		$args = array( 'msls_admin' => new MslsAdmin() );
90
91
		$callback = array( $this, 'theme_location' );
92
		add_settings_field( 'mslsmenu_theme_location', __( 'Theme Location', 'mslsmenu' ), $callback, $page, 'mslsmenu_section', $args );
93
94
		$callback = array( $this, 'display' );
95
		add_settings_field( 'mslsmenu_display', __( 'Display', 'mslsmenu' ), $callback, $page, 'mslsmenu_section', $args );
96
97
		$callback = array( $this, 'input' );
98
		$fields   = array(
99
			'mslsmenu_before_output' => __( 'Text/HTML before the list', 'mslsmenu' ),
100
			'mslsmenu_after_output'  => __( 'Text/HTML after the list', 'mslsmenu' ),
101
			'mslsmenu_before_item'   => __( 'Text/HTML before each item', 'mslsmenu' ),
102
			'mslsmenu_after_item'    => __( 'Text/HTML after each item', 'mslsmenu' ),
103
		);
104
		foreach ( $fields as $id => $label ) {
105
			$args['mslsmenu_input'] = $id;
106
			add_settings_field( $id, $label, $callback, $page, 'mslsmenu_section', $args );
107
		}
108
	}
109
110
	/**
111
	 * Callback for mslsmenu_theme_location
112
	 *
113
	 * @param array $args
114
	 */
115
	function theme_location( $args ) {
0 ignored issues
show
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
		$locations = array();
117
		foreach ( get_nav_menu_locations() as $key => $value ) {
118
			$locations[ $key ] = $key;
119
		}
120
121
		$selected = (array) MslsOptions::instance()->mslsmenu_theme_location;
122
123
		$options = array(
124
			sprintf(
125
				'<option value="" %s>%s</option>',
126
				selected( true, ( in_array( '', $selected ) ), false ),
127
				__( '-- empty --', 'mslsmenu' )
128
			)
129
		);
130
		foreach ( $locations as $value => $description ) {
131
			$options[] = sprintf(
132
				'<option value="%s" %s>%s</option>',
133
				$value,
134
				selected( true, ( in_array( $value, $selected ) ), false ),
135
				$description
136
			);
137
		}
138
139
		printf(
140
			'<select id="%1$s" name="msls[%1$s][]" multiple="multiple">%2$s</select>',
141
			'mslsmenu_theme_location',
142
			implode( '', $options )
143
		);
144
	}
145
146
	/**
147
	 * Callback for mslsmenu_display
148
	 *
149
	 * @param array $args
150
	 */
151
	function display( $args ) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
		echo $args['msls_admin']->render_select(
153
			'mslsmenu_display',
154
			MslsLink::get_types_description(),
155
			MslsOptions::instance()->mslsmenu_display
156
		);
157
	}
158
159
	/**
160
	 * Callback for mslsmenu text-inputs
161
	 *
162
	 * @param array $args
163
	 */
164
	function input( $args ) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
165
		echo $args['msls_admin']->render_input( $args['mslsmenu_input'] );
166
	}
167
168
}
169
170
MslsMenu::init();
171