| 1 |
<?php |
| 2 |
/** |
| 3 |
* Menus Widget |
| 4 |
* |
| 5 |
* @package Essential_Widgets |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Makes a custom Widget for Displaying About |
| 11 |
* |
| 12 |
* Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets |
| 13 |
* |
| 14 |
* @package Essential_Widgets |
| 15 |
*/ |
| 16 |
class EW_Menus extends WP_Widget { |
| 17 |
|
| 18 |
/** |
| 19 |
* Holds widget settings defaults, populated in constructor. |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $defaults; |
| 24 |
|
| 25 |
function __construct() { |
| 26 |
|
| 27 |
$this->defaults = array( |
| 28 |
'title' => esc_attr__( 'Navigation', 'essential-widgets' ), |
| 29 |
'menu' => '', |
| 30 |
'container' => 'div', |
| 31 |
'container_id' => '', |
| 32 |
'container_class' => '', |
| 33 |
'menu_id' => '', |
| 34 |
'menu_class' => 'nav-menu', |
| 35 |
'depth' => 0, |
| 36 |
'before' => '', |
| 37 |
'after' => '', |
| 38 |
'link_before' => '', |
| 39 |
'link_after' => '', |
| 40 |
'fallback_cb' => 'wp_page_menu', |
| 41 |
); |
| 42 |
|
| 43 |
$widget_ops = array( |
| 44 |
'classname' => 'essential-widgets ew-menus ewmenus widget_nav_menu', |
| 45 |
'description' => esc_html__( 'Displays a list of menus.', 'essential-widgets' ), |
| 46 |
); |
| 47 |
|
| 48 |
$control_ops = array( |
| 49 |
'id_base' => 'ew-menus', |
| 50 |
); |
| 51 |
|
| 52 |
parent::__construct( |
| 53 |
'ew-menus', // Base ID |
| 54 |
esc_html__( 'EW: Menus', 'essential-widgets' ), // Name |
| 55 |
$widget_ops, |
| 56 |
$control_ops |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Displays the widget control options in the Widgets admin screen. |
| 62 |
* |
| 63 |
* @since 1.0.0 |
| 64 |
* @access public |
| 65 |
* @param array $instance |
| 66 |
* @param void |
| 67 |
*/ |
| 68 |
public function form( $instance ) { |
| 69 |
|
| 70 |
// Merge the user-selected arguments with the defaults. |
| 71 |
$instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 72 |
|
| 73 |
$container = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); ?> |
| 74 |
|
| 75 |
<p> |
| 76 |
<label> |
| 77 |
<?php esc_html_e( 'Title:', 'essential-widgets' ); ?> |
| 78 |
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['title'] ); ?>" /> |
| 79 |
</label> |
| 80 |
</p> |
| 81 |
|
| 82 |
<p> |
| 83 |
<label> |
| 84 |
<?php esc_html_e( 'Menu:', 'essential-widgets' ); ?> |
| 85 |
|
| 86 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'menu' ) ); ?>"> |
| 87 |
<option value=""></option> |
| 88 |
|
| 89 |
<?php foreach ( wp_get_nav_menus() as $menu ) : ?> |
| 90 |
|
| 91 |
<option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $instance['menu'], $menu->term_id ); ?>><?php echo esc_html( $menu->name ); ?></option> |
| 92 |
|
| 93 |
<?php endforeach; ?> |
| 94 |
|
| 95 |
</select> |
| 96 |
</label> |
| 97 |
</p> |
| 98 |
<p> |
| 99 |
<label> |
| 100 |
<?php esc_html_e( 'Container:', 'essential-widgets' ); ?> |
| 101 |
|
| 102 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'container' ) ); ?>"> |
| 103 |
|
| 104 |
<?php foreach ( $container as $option ) : ?> |
| 105 |
|
| 106 |
<option value="<?php echo esc_attr( $option ); ?>" <?php selected( $instance['container'], $option ); ?>><?php echo esc_html( $option ); ?></option> |
| 107 |
|
| 108 |
<?php endforeach; ?> |
| 109 |
|
| 110 |
</select> |
| 111 |
</label> |
| 112 |
</p> |
| 113 |
|
| 114 |
<p> |
| 115 |
<label> |
| 116 |
<?php esc_html_e( 'Container ID:', 'essential-widgets' ); ?> |
| 117 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'container_id' ) ); ?>" value="<?php echo esc_attr( $instance['container_id'] ); ?>" placeholder="<?php echo esc_html__( 'example' ); ?>" /> |
| 118 |
</label> |
| 119 |
</p> |
| 120 |
|
| 121 |
<p> |
| 122 |
<label> |
| 123 |
<?php esc_html_e( 'Container Class:', 'essential-widgets' ); ?> |
| 124 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'container_class' ) ); ?>" value="<?php echo esc_attr( $instance['container_class'] ); ?>" placeholder="<?php echo esc_html__( 'example' ); ?>" /> |
| 125 |
</label> |
| 126 |
</p> |
| 127 |
|
| 128 |
<p> |
| 129 |
<label> |
| 130 |
<?php esc_html_e( 'Menu ID:', 'essential-widgets' ); ?> |
| 131 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'menu_id' ) ); ?>" value="<?php echo esc_attr( $instance['menu_id'] ); ?>" placeholder="<?php echo esc_html__( 'example' ); ?>" /> |
| 132 |
</label> |
| 133 |
</p> |
| 134 |
|
| 135 |
<p> |
| 136 |
<label> |
| 137 |
<?php esc_html_e( 'Menu Class:', 'essential-widgets' ); ?> |
| 138 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'menu_class' ) ); ?>" value="<?php echo esc_attr( $instance['menu_class'] ); ?>" placeholder="<?php echo esc_html__( 'example' ); ?>" /> |
| 139 |
</label> |
| 140 |
</p> |
| 141 |
|
| 142 |
<p class="button-primary more"><?php esc_html_e( 'More Options', 'essential-widgets' ); ?><i class="dashicons dashicons-arrow-down"></i></p> |
| 143 |
|
| 144 |
<div class="advanced-section"> |
| 145 |
|
| 146 |
<p> |
| 147 |
<label> |
| 148 |
<?php esc_html_e( 'Depth:', 'essential-widgets' ); ?> |
| 149 |
<input type="number" class="widefat" size="5" min="0" name="<?php echo esc_attr( $this->get_field_name( 'depth' ) ); ?>" value="<?php echo esc_attr( $instance['depth'] ); ?>" placeholder="0" /> |
| 150 |
</label> |
| 151 |
</p> |
| 152 |
|
| 153 |
<p> |
| 154 |
<label> |
| 155 |
<?php esc_html_e( 'Before:', 'essential-widgets' ); ?> |
| 156 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'before' ) ); ?>" value="<?php echo esc_attr( $instance['before'] ); ?>" /> |
| 157 |
</label> |
| 158 |
</p> |
| 159 |
|
| 160 |
<p> |
| 161 |
<label> |
| 162 |
<?php esc_html_e( 'After:', 'essential-widgets' ); ?> |
| 163 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'after' ) ); ?>" value="<?php echo esc_attr( $instance['after'] ); ?>" /> |
| 164 |
</label> |
| 165 |
</p> |
| 166 |
|
| 167 |
<p> |
| 168 |
<label> |
| 169 |
<?php esc_html_e( 'Link Before:', 'essential-widgets' ); ?> |
| 170 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_before' ) ); ?>" value="<?php echo esc_attr( $instance['link_before'] ); ?>" /> |
| 171 |
</label> |
| 172 |
</p> |
| 173 |
|
| 174 |
<p> |
| 175 |
<label> |
| 176 |
<?php esc_html_e( 'Link After:', 'essential-widgets' ); ?> |
| 177 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_after' ) ); ?>" value="<?php echo esc_attr( $instance['link_after'] ); ?>" /> |
| 178 |
</label> |
| 179 |
</p> |
| 180 |
|
| 181 |
<p> |
| 182 |
<label> |
| 183 |
<?php esc_html_e( 'Fallback Callback Function:', 'essential-widgets' ); ?> |
| 184 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'fallback_cb' ) ); ?>" value="<?php echo esc_attr( $instance['fallback_cb'] ); ?>" placeholder="wp_page_menu" /> |
| 185 |
</label> |
| 186 |
</p> |
| 187 |
|
| 188 |
</div><!-- .advanced-section --> |
| 189 |
|
| 190 |
<div style="clear:both;"> </div> |
| 191 |
<?php } |
| 192 |
|
| 193 |
/** |
| 194 |
* update the particular instant |
| 195 |
* |
| 196 |
* This function should check that $new_instance is set correctly. |
| 197 |
* The newly calculated value of $instance should be returned. |
| 198 |
* If "false" is returned, the instance won't be saved/updated. |
| 199 |
* |
| 200 |
* $new_instance New settings for this instance as input by the user via form() |
| 201 |
* $old_instance Old settings for this instance |
| 202 |
* Settings to save or bool false to cancel saving |
| 203 |
*/ |
| 204 |
function update( $new_instance, $old_instance ) { |
| 205 |
$instance = $old_instance; |
| 206 |
|
| 207 |
// Sanitize title. |
| 208 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 209 |
|
| 210 |
// Strip tags. |
| 211 |
$instance['menu'] = strip_tags( $new_instance['menu'] ); |
| 212 |
|
| 213 |
// Whitelist options. |
| 214 |
$container = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); |
| 215 |
|
| 216 |
$instance['container'] = in_array( $new_instance['container'], $container ) ? $new_instance['container'] : 'div'; |
| 217 |
|
| 218 |
// Integers. |
| 219 |
$instance['depth'] = absint( $new_instance['depth'] ); |
| 220 |
|
| 221 |
// HTML class. |
| 222 |
$instance['container_id'] = sanitize_html_class( $new_instance['container_id'] ); |
| 223 |
$instance['container_class'] = sanitize_html_class( $new_instance['container_class'] ); |
| 224 |
$instance['menu_id'] = sanitize_html_class( $new_instance['menu_id'] ); |
| 225 |
$instance['menu_class'] = sanitize_html_class( $new_instance['menu_class'] ); |
| 226 |
|
| 227 |
// Text boxes. Make sure user can use 'unfiltered_html'. |
| 228 |
$instance['before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['before'] : wp_kses_post( $new_instance['before'] ); |
| 229 |
$instance['after'] = current_user_can( 'unfiltered_html' ) ? $new_instance['after'] : wp_kses_post( $new_instance['after'] ); |
| 230 |
$instance['link_before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['link_before'] : wp_kses_post( $new_instance['link_before'] ); |
| 231 |
$instance['link_after'] = current_user_can( 'unfiltered_html' ) ? $new_instance['link_after'] : wp_kses_post( $new_instance['link_after'] ); |
| 232 |
|
| 233 |
// Function name. |
| 234 |
$instance['fallback_cb'] = empty( $new_instance['fallback_cb'] ) || function_exists( $new_instance['fallback_cb'] ) ? $new_instance['fallback_cb'] : 'wp_page_menu'; |
| 235 |
|
| 236 |
// Return sanitized options. |
| 237 |
return $instance; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Displays the Widget in the front-end. |
| 242 |
* |
| 243 |
* $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 244 |
* $instance The settings for the particular instance of the widget |
| 245 |
*/ |
| 246 |
function widget( $args, $instance ) { |
| 247 |
|
| 248 |
// Set the $args for wp_nav_menu() to the $instance array. |
| 249 |
$instance = wp_parse_args( $instance, $this->defaults ); |
| 250 |
|
| 251 |
// Overwrite the $echo argument and set it to false. |
| 252 |
$instance['echo'] = false; |
| 253 |
|
| 254 |
// Output the sidebar's $before_widget wrapper. |
| 255 |
echo $args['before_widget']; |
| 256 |
|
| 257 |
// If a title was input by the user, display it. |
| 258 |
if ( ! empty( $instance['title'] ) ) |
| 259 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; |
| 260 |
|
| 261 |
// Output the nav menu. |
| 262 |
echo str_replace( array( "\r", "\n", "\t" ), '', wp_nav_menu( $instance ) ); |
| 263 |
|
| 264 |
// Close the sidebar's widget wrapper. |
| 265 |
echo $args['after_widget']; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Intiate Menu_Widget Class. |
| 271 |
* |
| 272 |
* @since 1.0.0 |
| 273 |
*/ |
| 274 |
function ew_menu_register() { |
| 275 |
register_widget( 'EW_Menus' ); |
| 276 |
} |
| 277 |
add_action( 'widgets_init', 'ew_menu_register' ); |
| 278 |
|