PluginProbe
Menu In Post / trunk
Menu In Post vtrunk
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 trunk, at menu-in-post.php

221 lines 5.3 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.5.0
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.5.0' ); // Keep in sync with the plugin header.
29 }
30
31 // Used in uninstall.php to prevent warnings about non-prefixed globals.
32 if ( ! defined( 'MIP_OPTION_NAME' ) ) {
33 define( 'MIP_OPTION_NAME', 'mip_options' );
34 }
35
36 // Register a simple autoloader that looks in the plugin’s root directory.
37 spl_autoload_register(
38 function ( $theclass ) {
39
40
41
42 $prefix = 'MenuInPost\\';
43 $base = __DIR__ . '/';
44
45 // Only handle our own namespace.
46 $len = strlen( $prefix );
47 if ( strncmp( $prefix, $theclass, $len ) !== 0 ) {
48 return;
49 }
50
51 // Strip the namespace prefix.
52 $relative = substr( $theclass, $len );
53
54 // Convert namespace separators to directory separators.
55 $relative = str_replace( '\\', '/', $relative );
56
57 // Build the full path to the file.
58 $file = $base . 'class-' . strtolower($relative) . '.php';
59
60 // ******************** Debugging code. ********************
61 //error_log( "[MenuInPost] Autoloader invoked for class: $theclass" );
62 //error_log( "[MenuInPost] class filename = $file" );
63 // ******************** End debugging code. ****************
64
65 if ( file_exists( $file ) ) {
66 require_once $file;
67 }
68 }
69 );
70
71 if ( is_admin() ) {
72 include_once MENUINPOST_PLUGIN_DIR . '/admin/admin.php';
73 }
74
75 add_shortcode( 'menu_in_post_menu', 'menuinpost_output_menu' );
76
77
78 /**
79 * Initiates the output of a Menu In Post Menu.
80 *
81 * @param array $atts The attributes of the menu.
82 *
83 * @return Returns the menu via wp_nav_menu()
84 */
85 function menuinpost_output_menu( $atts = array() ) {
86 if ( isset( $atts['menu'] ) ) {
87 $menu = absint( $atts['menu'] );
88 } else {
89 $menu = 0;
90 }
91 if ( 0 === $menu ) {
92 return menuinpost_fallback();
93 } else {
94 $args = array(
95 'menu' => $menu,
96 'fallback_cb' => 'menuinpost_fallback',
97 'echo' => false,
98 );
99 }
100
101 /*
102 If menu_id is empty, don't pass a value, and the menu slug with an
103 incremented value added will be used.
104
105 If container_class is empty, don't pass a value and
106 'menu-{menu slug}-container' will be used.
107 */
108 $defaults = array(
109 'menu_class' => 'menu',
110 'menu_id' => '',
111 'container' => 'div',
112 'container_class' => '',
113 'container_id' => '',
114 'style' => 'list',
115 'placeholder_text' => esc_html( __( 'Select...', 'menu-in-post' ) ),
116 'append_to_url' => '',
117 'depth' => 0,
118 );
119 foreach ( $defaults as $att => $default ) {
120 switch ( $att ) {
121 case 'depth':
122 if ( isset( $atts[ $att ] ) ) {
123 $passed_depth = absint( $atts[ $att ] );
124 if ( $passed_depth > 0 ) {
125 $args['depth'] = $passed_depth;
126 }
127 } else {
128 $atts['depth'] = $default;
129 }
130 break;
131 // These should be only strings.
132 default:
133 if ( isset( $atts[ $att ] ) ) {
134 $passed_att = sanitize_text_field( $atts[ $att ] );
135 if ( '' !== $passed_att ) {
136 $args[ $att ] = $passed_att;
137 }
138 } else {
139 $atts[ $att ] = $default;
140 }
141 }
142 }
143 if ( 'dropdown' === $atts['style'] ) {
144 $select = '<select class="mip-drop-nav"';
145 if ( '' !== $atts['menu_id'] ) {
146 $select .= ' id="' . $args['menu_id'] . '"';
147 }
148 $select .= '>';
149 $args['items_wrap'] = $select . '<option value="#">' .
150 $atts['placeholder_text'] . '</option>%3$s</select>';
151 $args['walker'] = new MIPWalkerNavMenuDropdownBuilder();
152 } elseif ( '' !== $atts['append_to_url'] ) {
153 $args['walker'] = new MIPWalkerNavMenuListBuilder();
154 }
155 if ( '' !== $atts['append_to_url'] ) {
156 $args['append_to_url'] = $atts['append_to_url'];
157 }
158 return wp_nav_menu( $args );
159 }
160
161 /**
162 * Menu In Post fallback function.
163 *
164 * @return void
165 */
166 function menuinpost_fallback() {}
167
168 add_action( 'wp_enqueue_scripts', 'menuinpost_enqueue_front_end_scripts' );
169
170 /**
171 * Selectively enqueues the JavaScript for Menu In Post
172 *
173 * @return void
174 */
175 function menuinpost_enqueue_front_end_scripts() {
176 $options = get_option(
177 'mip_options',
178 array(
179 'miploadjs' => 'always',
180 'miponlypages' => '',
181 'mipminimizejs' => 'yes',
182 )
183 );
184
185 $load = false;
186 $loadjs = $options['miploadjs'];
187 if ( 'yes' === $options['mipminimizejs'] ) {
188 $file = 'main-min.js';
189 } else {
190 $file = 'main.js';
191 }
192
193 switch ( $loadjs ) {
194 case 'always':
195 $load = true;
196 break;
197 case 'onlypages':
198 $pagestr = trim( str_replace( ' ', '', $options['miponlypages'] ) );
199 if ( '' !== $pagestr ) {
200 $pages = explode( ',', $pagestr );
201 if ( is_array( $pages ) ) {
202 $pageid = get_queried_object_id();
203 if ( in_array( $pageid, $pages, true ) ) {
204 $load = true;
205 }
206 }
207 }
208 break;
209 }
210
211 if ( true === $load ) {
212 wp_enqueue_script(
213 'menu_in_post_frontend_script',
214 plugins_url( 'js/' . $file, __FILE__ ),
215 array( 'jquery' ),
216 MENU_IN_POST_VERSION,
217 true
218 );
219 }
220 }
221