Advertisement
Guest User

widgets.php

a guest
May 27th, 2022
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.31 KB | None | 0 0
  1. <?php
  2. // widget extra options
  3. global $theme_widgets_style;
  4. $theme_widgets_style = array(
  5.     'default' => __('sidebar default', THEME_NS),
  6.     'block'   => __('block', THEME_NS),
  7.     'post'    => __('post', THEME_NS),
  8.     'simple'  => __('simple text', THEME_NS)
  9. );
  10.  
  11. function theme_get_widget_style($id, $style = null) {
  12.  
  13.     $result = theme_get_widget_meta_option($id, 'theme_widget_styles');
  14.     global $theme_widgets_style;
  15.     if (!in_array($result, array_keys($theme_widgets_style))) {
  16.         $result = 'default';
  17.     }
  18.     if ($style != null) {
  19.         if (!in_array($style, array('block', 'post', 'simple'))) {
  20.             $style = 'block';
  21.         }
  22.         if ($result == 'default') {
  23.             $result = $style;
  24.         }
  25.     }
  26.     return $result;
  27. }
  28.  
  29. function theme_set_widget_style($id, $style) {
  30.     global $theme_widgets_style;
  31.     if (!in_array($style, array_keys($theme_widgets_style))) {
  32.         $style = 'default';
  33.     }
  34.     theme_set_widget_meta_option($id, 'theme_widget_styles', $style);
  35. }
  36.  
  37. function theme_widget_expand_control($id) {
  38.     global $wp_registered_widget_controls;
  39.     $controls = &$wp_registered_widget_controls[$id];
  40.     if (!is_array($controls['params'])) {
  41.         $controls['params'] = array($controls['params']);
  42.     }
  43.     $controls['params'][] = $id;
  44.     if (isset($controls['callback'])) {
  45.         $controls['callback_redirect'] = $controls['callback'];
  46.     }
  47.     $controls['callback'] = 'theme_widget_extra_control';
  48. }
  49.  
  50. function theme_update_widget_additional($instance) {
  51.     global $theme_widget_meta_options;
  52.     foreach ($theme_widget_meta_options as $value) {
  53.         $id = theme_get_array_value($value, 'id');
  54.         $val = stripslashes(theme_get_array_value($_POST, $id));
  55.         $type = theme_get_array_value($value, 'type');
  56.         $options = theme_get_array_value($value, 'options');
  57.         switch ($type) {
  58.             case 'checkbox':
  59.                 $val = ($val ? 1 : 0);
  60.                 break;
  61.             case 'numeric':
  62.                 $val = (int) $val;
  63.                 break;
  64.             case 'select':
  65.                 if (!in_array($val, array_keys($options))) {
  66.                     $val = reset(array_keys($options));
  67.                 }
  68.                 break;
  69.         }
  70.         $instance[$id] = $val;
  71.     }
  72.     return $instance;
  73. }
  74. function theme_widget_process_control() {
  75.     global $wp_registered_widget_controls;
  76.     if ('post' == strtolower($_SERVER['REQUEST_METHOD']) && isset($_POST['widget-id'])) {
  77.         theme_widget_expand_control($_POST['widget-id']);
  78.         return;
  79.     }
  80.     foreach ($wp_registered_widget_controls as $id => $widget) {
  81.         theme_widget_expand_control($id);
  82.     }
  83. }
  84.  
  85. function theme_widget_extra_control() {
  86.     global $wp_registered_widget_controls, $theme_widgets_style, $theme_widget_meta_options;
  87.     $_theme_widget_meta_options = $theme_widget_meta_options;
  88.     $params = func_get_args();
  89.     $widget_id = $params[count($params) - 1]; // see theme_widget_expand_control func
  90.     $widget_controls = theme_get_array_value($wp_registered_widget_controls, $widget_id, array());
  91.     if (isset($widget_controls['callback_redirect'])) {
  92.         $callback = $widget_controls['callback_redirect'];
  93.         if (is_callable($callback)) {
  94.             call_user_func_array($callback, $params);
  95.         }
  96.     }
  97.     if (!preg_match('/^(.*[^-])-([0-9]+)$/', $widget_id, $matches) || !isset($matches[1]) || !isset($matches[2])) {
  98.         return false;
  99.     }
  100.     $id = $matches[1] . '-' . $params[0]['number'];
  101.    
  102.     ?>
  103.     <h3 style="margin-bottom:3px;"><?php _e('Theme Options', THEME_NS); ?></h3>
  104.     <?php
  105.     theme_print_meta_box($id, $_theme_widget_meta_options);
  106. }
  107.  
  108. // widgets
  109.  
  110. class VMenuWidget extends WP_Widget {
  111.  
  112.     function VMenuWidget() {
  113.         $widget_ops = array('classname' => 'vmenu', 'description' => __('Use this widget to add one of your custom menus as a widget.', THEME_NS));
  114.         parent::WP_Widget(false, __('Vertical Menu', THEME_NS), $widget_ops);
  115.     }
  116.  
  117.     function widget($args, $instance) {
  118.         extract($args);
  119.         $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
  120.         echo $before_widget;
  121.         echo $before_title . $title . $after_title;
  122.         echo theme_get_menu(array(
  123.             'source' => $instance['source'],
  124.             'depth'  => theme_get_option('theme_vmenu_depth'),
  125.             'menu'   => wp_get_nav_menu_object($instance['nav_menu']),
  126.             'class'  => ''
  127.         ));
  128.         echo $after_widget;
  129.     }
  130.  
  131.     function update($new_instance, $old_instance) {
  132.         $instance['title'] = strip_tags($new_instance['title']);
  133.         $instance['source'] = $new_instance['source'];
  134.         $instance['nav_menu'] = (int) $new_instance['nav_menu'];
  135.         return $instance;
  136.     }
  137.  
  138.     function form($instance) {
  139.         //Defaults
  140.         $instance = wp_parse_args((array) $instance, array('title' => '', 'source' => 'Pages', 'nav_menu' => ''));
  141.         $title = esc_attr($instance['title']);
  142.         $source = $instance['source'];
  143.         $nav_menu = $instance['nav_menu'];
  144.  
  145.         // Get menus
  146.         $menus = get_terms('nav_menu', array('hide_empty' => false));
  147.         $sources = array(
  148.                       'Pages' => __('Pages', THEME_NS),
  149.                       'Categories' => __('Categories', THEME_NS),
  150.                       'Custom Menu' => __('Custom Menu', THEME_NS)
  151.                       );
  152.         ?>
  153.         <p>
  154.             <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', THEME_NS) ?></label>
  155.             <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
  156.         </p>
  157.         <p>
  158.         <label for="<?php echo $this->get_field_id('source'); ?>"><?php echo __('Source', THEME_NS) . ':'; ?></label>
  159.         <select class="widefat" id="<?php echo $this->get_field_id('source'); ?>" name="<?php echo $this->get_field_name('source'); ?>" onchange="var s = jQuery('.p-<?php echo $this->get_field_id('nav_menu'); ?>'); if (this.value == 'Custom Menu') s.show(); else s.hide();">
  160.         <?php
  161.         foreach ($sources as $s => $t ) {
  162.             $selected = ($source == $s ? ' selected="selected"' : '');
  163.             echo '<option' . $selected . ' value="' . $s . '">' . $t . '</option>';
  164.         }
  165.         ?>
  166.         </select>
  167.         </p>
  168.         <p class="p-<?php echo $this->get_field_id('nav_menu'); ?>" <?php if ($source !== 'Custom Menu') echo ' style="display:none"' ?>>
  169.         <?php
  170.         // If no menus exists, direct the user to go and create some.
  171.         if (!$menus) {
  172.             printf(__('No menus have been created yet. <a href="%s">Create some</a>.', THEME_NS), admin_url('nav-menus.php'));
  173.         } else {
  174.             ?>
  175.             <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:', THEME_NS); ?></label><br />
  176.             <select class="widefat" id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
  177.             <?php
  178.             foreach ($menus as $menu) {
  179.                 $selected = $nav_menu == $menu->term_id ? ' selected="selected"' : '';
  180.                 echo '<option' . $selected . ' value="' . $menu->term_id . '">' . $menu->name . '</option>';
  181.             }
  182.             ?>
  183.             </select>
  184.             <?php
  185.         }
  186.         ?>
  187.         </p>
  188.         <?php
  189.     }
  190.  
  191. }
  192.  
  193. class LoginWidget extends WP_Widget {
  194.  
  195.     function LoginWidget() {
  196.         $widget_ops = array('classname' => 'login', 'description' => __('Login form', THEME_NS));
  197.         $this->WP_Widget(false, __('Login', THEME_NS), $widget_ops);
  198.     }
  199.  
  200.     function widget($args, $instance) {
  201.         global $user_ID, $user_identity, $user_level, $user_email, $user_login;
  202.         extract($args);
  203.         echo $before_widget;
  204.         echo $before_title;
  205.         if ($user_ID):
  206.             echo $user_identity;
  207.             echo $after_title;
  208.             ?>
  209.             <ul>
  210.             <li><a href="<?php echo esc_url( site_url() ) ?>/wp-admin/"><?php _e('Dashboard', THEME_NS); ?></a></li>
  211.             <?php if ($user_level >= 1): ?>
  212.                 <li><a href="<?php echo esc_url( site_url() ) ?>/wp-admin/post-new.php"><?php _e('Publish', THEME_NS); ?></a></li>
  213.                 <li><a href="<?php echo esc_url( site_url() ) ?>/wp-admin/edit-comments.php"><?php _e('Comments', THEME_NS); ?></a></li>
  214.             <?php endif; ?>
  215.             <li><a href="<?php echo wp_logout_url() ?>&amp;redirect_to=<?php echo urlencode(theme_get_current_url()); ?>"><?php _e("Log out", THEME_NS); ?></a></li>
  216.             </ul>
  217.         <?php
  218.         else:
  219.             _e('Log In', THEME_NS);
  220.             echo $after_title;
  221.             ?>
  222.             <form action="<?php echo esc_url( site_url() ) ?>/wp-login.php" method="post" name="login" id="form-login">
  223.                 <fieldset class="input" style="border: 0 none;">
  224.                     <p id="form-login-username">
  225.                         <label for="log"><?php _e('Username', THEME_NS) ?></label>
  226.                         <br>
  227.                         <input type="text" name="log" id="log" value="<?php echo esc_attr(stripslashes($user_login), 1) ?>" size="20" />
  228.                     </p>
  229.                     <p id="form-login-password">
  230.                         <label for="pwd"><?php _e("Password", THEME_NS); ?></label>
  231.                         <br>
  232.                         <input type="password" name="pwd" id="pwd" size="20" /><br />
  233.                     </p>
  234.                     <p id="form-login-remember">
  235.                         <label for="rememberme"><?php _e('Remember Me', THEME_NS); ?></label>
  236.                         <input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" />
  237.                     </p>
  238.                     <input class="art-button" type="submit" name="submit" value="<?php echo esc_attr(__('Log In', THEME_NS)); ?>" />
  239.                 </fieldset>
  240.                 <input type="hidden" name="redirect_to" value="<?php echo theme_get_current_url(); ?>"/>
  241.             </form>
  242.             <ul>
  243.                 <?php if (get_option('users_can_register')) { ?>
  244.                 <li><a href="<?php echo esc_url( site_url() ) ?>/wp-register.php"><?php _e("Register", THEME_NS); ?></a></li>
  245.                 <?php } ?>
  246.                 <li><a href="<?php echo esc_url( site_url() ) ?>/wp-login.php?action=lostpassword"><?php _e("Lost your password?", THEME_NS); ?></a></li>
  247.             </ul>
  248.         <?php endif;
  249.         echo $after_widget;
  250.     }
  251. }
  252.  
  253. // init widgets
  254. function artWidgetsInit() {
  255.     register_widget('VMenuWidget');
  256.     register_widget('LoginWidget');
  257. }
  258.  
  259. add_action('widgets_init', 'artWidgetsInit');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement