PluginProbe
Menu In Post / 1.4.1
Menu In Post v1.4.1
trunk 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.4.0 1.4.1 1.5.0
menu-in-post / menu-in-post.php

menu-in-post.php in Menu In Post 1.4.1, at menu-in-post.php

216 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Menu In Post
4 * Description: A simple but flexible plugin to add menus to a post or page.
5 * Author: linux4me2
6 * Author URI: https://profiles.wordpress.org/linux4me2
7 * Text Domain: menu-in-post
8 * Version: 1.4.1
9 * License: GPL3
10 * License URI: https://www.gnu.org/licenses/gpl-3.0-standalone.html
11 *
12 * @package menu-in-post
13 */
14
15 use MenuInPost\MIPHelpTabs;
16 use MenuInPost\MIPWalkerNavMenuDropdownBuilder;
17 use MenuInPost\MIPWalkerNavMenuListBuilder;
18
19 // Prevent direct access.
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 define( 'MENUINPOST_PLUGIN', __FILE__ );
25 define( 'MENUINPOST_PLUGIN_DIR', untrailingslashit( dirname( MENUINPOST_PLUGIN ) ) );
26
27 if ( ! defined( 'MENU_IN_POST_VERSION' ) ) {
28 define( 'MENU_IN_POST_VERSION', '1.4.1' ); // keep in sync with the plugin header.
29 }
30
31 // Register a simple autoloader that looks in the plugin’s root directory.
32 spl_autoload_register(
33 function ( $theclass ) {
34
35
36
37 $prefix = 'MenuInPost\\';
38 $base = __DIR__ . '/';
39
40 // Only handle our own namespace.
41 $len = strlen( $prefix );
42 if ( strncmp( $prefix, $theclass, $len ) !== 0 ) {
43 return;
44 }
45
46 // Strip the namespace prefix.
47 $relative = substr( $theclass, $len );
48
49 // Convert namespace separators to directory separators.
50 $relative = str_replace( '\\', '/', $relative );
51
52 // Build the full path to the file.
53 $file = $base . 'class-' . strtolower($relative) . '.php';
54
55 // ******************** Debugging code. ********************
56 //error_log( "[MenuInPost] Autoloader invoked for class: $theclass" );
57 //error_log( "[MenuInPost] class filename = $file" );
58 // ******************** End debugging code. ****************
59
60 if ( file_exists( $file ) ) {
61 require_once $file;
62 }
63 }
64 );
65
66 if ( is_admin() ) {
67 include_once MENUINPOST_PLUGIN_DIR . '/admin/admin.php';
68 }
69
70 add_shortcode( 'menu_in_post_menu', 'output_mip_menu' );
71
72
73 /**
74 * Initiates the output of a Menu In Post Menu.
75 *
76 * @param array $atts The attributes of the menu.
77 *
78 * @return Returns the menu via wp_nav_menu()
79 */
80 function output_mip_menu( $atts = array() ) {
81 if ( isset( $atts['menu'] ) ) {
82 $menu = absint( $atts['menu'] );
83 } else {
84 $menu = 0;
85 }
86 if ( 0 === $menu ) {
87 return fallback_mip();
88 } else {
89 $args = array(
90 'menu' => $menu,
91 'fallback_cb' => 'fallback_mip',
92 'echo' => false,
93 );
94 }
95
96 /*
97 If menu_id is empty, don't pass a value, and the menu slug with an
98 incremented value added will be used.
99
100 If container_class is empty, don't pass a value and
101 'menu-{menu slug}-container' will be used.
102 */
103 $defaults = array(
104 'menu_class' => 'menu',
105 'menu_id' => '',
106 'container' => 'div',
107 'container_class' => '',
108 'container_id' => '',
109 'style' => 'list',
110 'placeholder_text' => esc_html( __( 'Select...', 'menu-in-post' ) ),
111 'append_to_url' => '',
112 'depth' => 0,
113 );
114 foreach ( $defaults as $att => $default ) {
115 switch ( $att ) {
116 case 'depth':
117 if ( isset( $atts[ $att ] ) ) {
118 $passed_depth = absint( $atts[ $att ] );
119 if ( $passed_depth > 0 ) {
120 $args['depth'] = $passed_depth;
121 }
122 } else {
123 $atts['depth'] = $default;
124 }
125 break;
126 // These should be only strings.
127 default:
128 if ( isset( $atts[ $att ] ) ) {
129 $passed_att = sanitize_text_field( $atts[ $att ] );
130 if ( '' !== $passed_att ) {
131 $args[ $att ] = $passed_att;
132 }
133 } else {
134 $atts[ $att ] = $default;
135 }
136 }
137 }
138 if ( 'dropdown' === $atts['style'] ) {
139 $select = '<select class="mip-drop-nav"';
140 if ( '' !== $atts['menu_id'] ) {
141 $select .= ' id="' . $args['menu_id'] . '"';
142 }
143 $select .= '>';
144 $args['items_wrap'] = $select . '<option value="#">' .
145 $atts['placeholder_text'] . '</option>%3$s</select>';
146 $args['walker'] = new MIPWalkerNavMenuDropdownBuilder();
147 } elseif ( '' !== $atts['append_to_url'] ) {
148 $args['walker'] = new MIPWalkerNavMenuListBuilder();
149 }
150 if ( '' !== $atts['append_to_url'] ) {
151 $args['append_to_url'] = $atts['append_to_url'];
152 }
153 return wp_nav_menu( $args );
154 }
155
156 /**
157 * Menu In Post fallback function.
158 *
159 * @return void
160 */
161 function fallback_mip() {}
162
163 add_action( 'wp_enqueue_scripts', 'enqueue_mip_front_end_js' );
164
165 /**
166 * Selectively enqueues the JavaScript for Menu In Post
167 *
168 * @return void
169 */
170 function enqueue_mip_front_end_js() {
171 $options = get_option(
172 'mip_options',
173 array(
174 'miploadjs' => 'always',
175 'miponlypages' => '',
176 'mipminimizejs' => 'yes',
177 )
178 );
179
180 $load = false;
181 $loadjs = $options['miploadjs'];
182 if ( 'yes' === $options['mipminimizejs'] ) {
183 $file = 'main-min.js';
184 } else {
185 $file = 'main.js';
186 }
187
188 switch ( $loadjs ) {
189 case 'always':
190 $load = true;
191 break;
192 case 'onlypages':
193 $pagestr = trim( str_replace( ' ', '', $options['miponlypages'] ) );
194 if ( '' !== $pagestr ) {
195 $pages = explode( ',', $pagestr );
196 if ( is_array( $pages ) ) {
197 $pageid = get_queried_object_id();
198 if ( in_array( $pageid, $pages, true ) ) {
199 $load = true;
200 }
201 }
202 }
203 break;
204 }
205
206 if ( true === $load ) {
207 wp_enqueue_script(
208 'menu_in_post_frontend_script',
209 plugins_url( 'js/' . $file, __FILE__ ),
210 array( 'jquery' ),
211 MENU_IN_POST_VERSION,
212 true
213 );
214 }
215 }
216