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