PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/roots-widgets.php

http://github.com/retlehs/roots
PHP | 136 lines | 118 code | 18 blank | 0 comment | 11 complexity | 2a49b1a5a3b266304941eecb66eb2c26 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. class Roots_Vcard_Widget extends WP_Widget {
  3. function Roots_Vcard_Widget() {
  4. $widget_ops = array('classname' => 'widget_roots_vcard', 'description' => __('Use this widget to add a vCard', 'roots'));
  5. $this->WP_Widget('widget_roots_vcard', __('Roots: vCard', 'roots'), $widget_ops);
  6. $this->alt_option_name = 'widget_roots_vcard';
  7. add_action('save_post', array(&$this, 'flush_widget_cache'));
  8. add_action('deleted_post', array(&$this, 'flush_widget_cache'));
  9. add_action('switch_theme', array(&$this, 'flush_widget_cache'));
  10. }
  11. function widget($args, $instance) {
  12. $cache = wp_cache_get('widget_roots_vcard', 'widget');
  13. if (!is_array($cache)) {
  14. $cache = array();
  15. }
  16. if (!isset($args['widget_id'])) {
  17. $args['widget_id'] = null;
  18. }
  19. if (isset($cache[$args['widget_id']])) {
  20. echo $cache[$args['widget_id']];
  21. return;
  22. }
  23. ob_start();
  24. extract($args, EXTR_SKIP);
  25. $title = apply_filters('widget_title', empty($instance['title']) ? __('vCard', 'roots') : $instance['title'], $instance, $this->id_base);
  26. if (!isset($instance['street_address'])) { $instance['street_address'] = ''; }
  27. if (!isset($instance['locality'])) { $instance['locality'] = ''; }
  28. if (!isset($instance['region'])) { $instance['region'] = ''; }
  29. if (!isset($instance['postal_code'])) { $instance['postal_code'] = ''; }
  30. if (!isset($instance['tel'])) { $instance['tel'] = ''; }
  31. if (!isset($instance['email'])) { $instance['email'] = ''; }
  32. echo $before_widget;
  33. if ($title) {
  34. echo $before_title;
  35. echo $title;
  36. echo $after_title;
  37. }
  38. ?>
  39. <p class="vcard">
  40. <a class="fn org url" href="<?php echo home_url('/'); ?>"><?php bloginfo('name'); ?></a><br>
  41. <span class="adr">
  42. <span class="street-address"><?php echo $instance['street_address']; ?></span><br>
  43. <span class="locality"><?php echo $instance['locality']; ?></span>,
  44. <span class="region"><?php echo $instance['region']; ?></span>
  45. <span class="postal-code"><?php echo $instance['postal_code']; ?></span><br>
  46. </span>
  47. <span class="tel"><span class="value"><span class="hidden">+1-</span><?php echo $instance['tel']; ?></span></span><br>
  48. <a class="email" href="mailto:<?php echo $instance['email']; ?>"><?php echo $instance['email']; ?></a>
  49. </p>
  50. <?php
  51. echo $after_widget;
  52. $cache[$args['widget_id']] = ob_get_flush();
  53. wp_cache_set('widget_roots_vcard', $cache, 'widget');
  54. }
  55. function update($new_instance, $old_instance) {
  56. $instance = $old_instance;
  57. $instance['title'] = strip_tags($new_instance['title']);
  58. $instance['street_address'] = strip_tags($new_instance['street_address']);
  59. $instance['locality'] = strip_tags($new_instance['locality']);
  60. $instance['region'] = strip_tags($new_instance['region']);
  61. $instance['postal_code'] = strip_tags($new_instance['postal_code']);
  62. $instance['tel'] = strip_tags($new_instance['tel']);
  63. $instance['email'] = strip_tags($new_instance['email']);
  64. $this->flush_widget_cache();
  65. $alloptions = wp_cache_get('alloptions', 'options');
  66. if (isset($alloptions['widget_roots_vcard'])) {
  67. delete_option('widget_roots_vcard');
  68. }
  69. return $instance;
  70. }
  71. function flush_widget_cache() {
  72. wp_cache_delete('widget_roots_vcard', 'widget');
  73. }
  74. function form($instance) {
  75. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  76. $street_address = isset($instance['street_address']) ? esc_attr($instance['street_address']) : '';
  77. $locality = isset($instance['locality']) ? esc_attr($instance['locality']) : '';
  78. $region = isset($instance['region']) ? esc_attr($instance['region']) : '';
  79. $postal_code = isset($instance['postal_code']) ? esc_attr($instance['postal_code']) : '';
  80. $tel = isset($instance['tel']) ? esc_attr($instance['tel']) : '';
  81. $email = isset($instance['email']) ? esc_attr($instance['email']) : '';
  82. ?>
  83. <p>
  84. <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title (optional):', 'roots'); ?></label>
  85. <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
  86. </p>
  87. <p>
  88. <label for="<?php echo esc_attr($this->get_field_id('street_address')); ?>"><?php _e('Street Address:', 'roots'); ?></label>
  89. <input class="widefat" id="<?php echo esc_attr($this->get_field_id('street_address')); ?>" name="<?php echo esc_attr($this->get_field_name('street_address')); ?>" type="text" value="<?php echo esc_attr($street_address); ?>" />
  90. </p>
  91. <p>
  92. <label for="<?php echo esc_attr($this->get_field_id('locality')); ?>"><?php _e('City/Locality:', 'roots'); ?></label>
  93. <input class="widefat" id="<?php echo esc_attr($this->get_field_id('locality')); ?>" name="<?php echo esc_attr($this->get_field_name('locality')); ?>" type="text" value="<?php echo esc_attr($locality); ?>" />
  94. </p>
  95. <p>
  96. <label for="<?php echo esc_attr($this->get_field_id('region')); ?>"><?php _e('State/Region:', 'roots'); ?></label>
  97. <input class="widefat" id="<?php echo esc_attr($this->get_field_id('region')); ?>" name="<?php echo esc_attr($this->get_field_name('region')); ?>" type="text" value="<?php echo esc_attr($region); ?>" />
  98. </p>
  99. <p>
  100. <label for="<?php echo esc_attr($this->get_field_id('postal_code')); ?>"><?php _e('Zipcode/Postal Code:', 'roots'); ?></label>
  101. <input class="widefat" id="<?php echo esc_attr($this->get_field_id('postal_code')); ?>" name="<?php echo esc_attr($this->get_field_name('postal_code')); ?>" type="text" value="<?php echo esc_attr($postal_code); ?>" />
  102. </p>
  103. <p>
  104. <label for="<?php echo esc_attr($this->get_field_id('tel')); ?>"><?php _e('Telephone:', 'roots'); ?></label>
  105. <input class="widefat" id="<?php echo esc_attr($this->get_field_id('tel')); ?>" name="<?php echo esc_attr($this->get_field_name('tel')); ?>" type="text" value="<?php echo esc_attr($tel); ?>" />
  106. </p>
  107. <p>
  108. <label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php _e('Email:', 'roots'); ?></label>
  109. <input class="widefat" id="<?php echo esc_attr($this->get_field_id('email')); ?>" name="<?php echo esc_attr($this->get_field_name('email')); ?>" type="text" value="<?php echo esc_attr($email); ?>" />
  110. </p>
  111. <?php
  112. }
  113. }
  114. function roots_widget_init() {
  115. register_widget('Roots_Vcard_Widget');
  116. }
  117. add_action('widgets_init', 'roots_widget_init');
  118. ?>