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

MslsMenu.php (1 issue)

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 35 and the first side effect is on line 171.

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
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
if ( ! class_exists( 'MslsMenu' ) ) {
31
	/**
32
	 * MslsMenu Class
33
	 * @package mslsmenu
34
	 */
35
	class MslsMenu {
36
37
		/**
38
		 * MslsMenu constructor.
39
		 */
40
		public function __construct() {
41
			add_filter( 'wp_nav_menu_items', array( $this, 'nav_item' ), 10, 2 );
42
			add_action( 'msls_admin_register', array( $this, 'admin_register' ) );
43
		}
44
45
		/**
46
		 * Plugin init
47
		 */
48
		public static function init() {
49
			load_plugin_textdomain( 'mslsmenu', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
50
51
			return new self;
52
		}
53
54
		/**
55
		 * Callback for wp_nav_menu_items
56
		 *
57
		 * @param string $items
58
		 * @param StdClass $args
59
		 *
60
		 * @return string
61
		 */
62
		function nav_item( $items, $args ) {
63
			if ( function_exists( 'the_msls' ) ) {
64
				$options   = MslsOptions::instance();
65
				$locations = (array) $options->mslsmenu_theme_location;
66
67
				if ( in_array( $args->theme_location, $locations ) ) {
68
					$mslsmenu = '';
69
70
					$obj = new MslsOutput;
71
					foreach ( $obj->get( (int) $options->mslsmenu_display ) as $item ) {
72
						$mslsmenu .= $options->mslsmenu_before_item . $item . $options->mslsmenu_after_item;
73
					}
74
75
					$items .= $options->mslsmenu_before_output . $mslsmenu . $options->mslsmenu_after_output;
76
				}
77
			}
78
79
			return $items;
80
		}
81
82
		/**
83
		 * Callback for msls_admin_register
84
		 *
85
		 * @param string $page
86
		 */
87
		function admin_register( $page ) {
88
			add_settings_section( 'mslsmenu_section', __( 'Menu Settings', 'mslsmenu' ), null, $page );
89
90
			$args = array( 'msls_admin' => new MslsAdmin() );
91
92
			$callback = array( $this, 'theme_location' );
93
			add_settings_field( 'mslsmenu_theme_location', __( 'Theme Location', 'mslsmenu' ), $callback, $page, 'mslsmenu_section', $args );
94
95
			$callback = array( $this, 'display' );
96
			add_settings_field( 'mslsmenu_display', __( 'Display', 'mslsmenu' ), $callback, $page, 'mslsmenu_section', $args );
97
98
			$callback = array( $this, 'input' );
99
			$fields   = array(
100
				'mslsmenu_before_output' => __( 'Text/HTML before the list', 'mslsmenu' ),
101
				'mslsmenu_after_output'  => __( 'Text/HTML after the list', 'mslsmenu' ),
102
				'mslsmenu_before_item'   => __( 'Text/HTML before each item', 'mslsmenu' ),
103
				'mslsmenu_after_item'    => __( 'Text/HTML after each item', 'mslsmenu' ),
104
			);
105
			foreach ( $fields as $id => $label ) {
106
				$args['mslsmenu_input'] = $id;
107
				add_settings_field( $id, $label, $callback, $page, 'mslsmenu_section', $args );
108
			}
109
		}
110
111
		/**
112
		 * Callback for mslsmenu_theme_location
113
		 *
114
		 * @param array $args
115
		 */
116
		function theme_location( $args ) {
117
			$locations = array();
118
			foreach ( get_nav_menu_locations() as $key => $value ) {
119
				$locations[ $key ] = $key;
120
			}
121
122
			$selected = (array) MslsOptions::instance()->mslsmenu_theme_location;
123
124
			$options = array(
125
				sprintf(
126
					'<option value="" %s>%s</option>',
127
					selected( true, ( in_array( '', $selected ) ), false ),
128
					__( '-- empty --', 'mslsmenu' )
129
				)
130
			);
131
			foreach ( $locations as $value => $description ) {
132
				$options[] = sprintf(
133
					'<option value="%s" %s>%s</option>',
134
					$value,
135
					selected( true, ( in_array( $value, $selected ) ), false ),
136
					$description
137
				);
138
			}
139
140
			printf(
141
				'<select id="%1$s" name="msls[%1$s][]" multiple="multiple">%2$s</select>',
142
				'mslsmenu_theme_location',
143
				implode( '', $options )
144
			);
145
		}
146
147
		/**
148
		 * Callback for mslsmenu_display
149
		 *
150
		 * @param array $args
151
		 */
152
		function display( $args ) {
153
			echo $args['msls_admin']->render_select(
154
				'mslsmenu_display',
155
				MslsLink::get_types_description(),
156
				MslsOptions::instance()->mslsmenu_display
157
			);
158
		}
159
160
		/**
161
		 * Callback for mslsmenu text-inputs
162
		 *
163
		 * @param array $args
164
		 */
165
		function input( $args ) {
166
			echo $args['msls_admin']->render_input( $args['mslsmenu_input'] );
167
		}
168
169
	}
170
171
	MslsMenu::init();
172
}