PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/theme/hybrid/library/functions/widgets.php

http://ooxx.googlecode.com/
PHP | 228 lines | 79 code | 25 blank | 124 comment | 7 complexity | 07ba8133c8740cd7428965817549d6ce MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Functions for dealing with widgets and widget areas within the theme. WP widgets must be
  4. * unregistered. Hybrid widgets must be registered in their place. All widget areas are loaded
  5. * and registered with WP.
  6. *
  7. * @package Hybrid
  8. * @subpackage Functions
  9. */
  10. /**
  11. * Register widget areas
  12. * @since 0.7
  13. */
  14. add_action( 'init', 'hybrid_register_sidebars' );
  15. /**
  16. * Unregister WP widgets
  17. * @since 0.3.2
  18. */
  19. add_action( 'widgets_init', 'hybrid_unregister_widgets' );
  20. /**
  21. * Register Hybrid Widgets
  22. * @since 0.6
  23. */
  24. add_action( 'widgets_init', 'hybrid_register_widgets' );
  25. /**
  26. * Disables widget areas
  27. * @since 0.5
  28. */
  29. add_filter( 'sidebars_widgets', 'remove_sidebars' );
  30. /**
  31. * Registers each widget area for the theme. This includes all of the asides
  32. * and the utility widget areas throughout the theme.
  33. *
  34. * We're retaining the 'sidebar' terminology until such time that the
  35. * WordPress community decides on a better term.
  36. *
  37. * @since 0.7
  38. * @uses register_sidebar() Registers a widget area.
  39. */
  40. function hybrid_register_sidebars() {
  41. $domain = hybrid_get_textdomain();
  42. /* Register aside widget areas. */
  43. register_sidebar( array( 'name' => __( 'Primary', $domain ), 'id' => 'primary', 'description' => __( 'The main (primary) widget area, most often used as a sidebar.', $domain ), 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
  44. register_sidebar( array( 'name' => __( 'Secondary', $domain ), 'id' => 'secondary', 'description' => __( 'The second most important widget area, most often used as a secondary sidebar', $domain ), 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
  45. register_sidebar( array( 'name' => __( 'Subsidiary', $domain ), 'id' => 'subsidiary', 'description' => __( 'A widget area loaded in the footer of the site.', $domain ), 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
  46. /* Register utility widget areas. */
  47. register_sidebar( array( 'name' => __( 'Utility: Before Content', $domain ), 'id' => 'utility-before-content', 'description' => __( 'Loaded before the page\'s main content area.', $domain ), 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
  48. register_sidebar( array( 'name' => __( 'Utility: After Content', $domain ), 'id' => 'utility-after-content', 'description' => __( 'Loaded after the page\'s main content area.', $domain ), 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
  49. register_sidebar( array( 'name' => __( 'Utility: After Singular', $domain ), 'id' => 'utility-after-singular', 'description' => __( 'Loaded on singular post (page, attachment, etc.) views before the comments area.', $domain ), 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
  50. /* Register template widget areas. */
  51. register_sidebar( array( 'name' => __( 'Widgets Template', $domain ), 'id' => 'utility-widgets-template', 'description' => __( 'Used as the content of the Widgets page template.', $domain ), 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
  52. register_sidebar( array( 'name' => __( '404 Template', $domain ), 'id' => 'utility-404', 'description' => __( 'Replaces the default 404 error page content.', $domain ), 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
  53. }
  54. /**
  55. * Register Hybrid's extra widgets. Each widget is meant to replace or extend the
  56. * current default WordPress widgets.
  57. *
  58. * @since 0.6
  59. * @uses register_widget() Registers individual widgets.
  60. * @link http://codex.wordpress.org/WordPress_Widgets_Api
  61. */
  62. function hybrid_register_widgets() {
  63. /* Load each widget file. */
  64. require_once( THEME_CLASSES . '/widget-archives.php' );
  65. require_once( THEME_CLASSES . '/widget-authors.php' );
  66. require_once( THEME_CLASSES . '/widget-bookmarks.php' );
  67. require_once( THEME_CLASSES . '/widget-calendar.php' );
  68. require_once( THEME_CLASSES . '/widget-categories.php' );
  69. require_once( THEME_CLASSES . '/widget-pages.php' );
  70. require_once( THEME_CLASSES . '/widget-search.php' );
  71. require_once( THEME_CLASSES . '/widget-tags.php' );
  72. /* Register each widget. */
  73. register_widget( 'Hybrid_Widget_Archives' );
  74. register_widget( 'Hybrid_Widget_Authors' );
  75. register_widget( 'Hybrid_Widget_Bookmarks' );
  76. register_widget( 'Hybrid_Widget_Calendar' );
  77. register_widget( 'Hybrid_Widget_Categories' );
  78. register_widget( 'Hybrid_Widget_Pages' );
  79. register_widget( 'Hybrid_Widget_Search' );
  80. register_widget( 'Hybrid_Widget_Tags' );
  81. }
  82. /**
  83. * Unregister default WordPress widgets we don't need. The theme adds its own
  84. * versions of these widgets.
  85. *
  86. * @since 0.3.2
  87. * @uses unregister_widget() Removes individual widgets.
  88. * @link http://codex.wordpress.org/WordPress_Widgets_Api
  89. */
  90. function hybrid_unregister_widgets() {
  91. unregister_widget( 'WP_Widget_Pages' );
  92. unregister_widget( 'WP_Widget_Calendar' );
  93. unregister_widget( 'WP_Widget_Archives' );
  94. unregister_widget( 'WP_Widget_Links' );
  95. unregister_widget( 'WP_Widget_Categories' );
  96. unregister_widget( 'WP_Widget_Recent_Posts' );
  97. unregister_widget( 'WP_Widget_Search' );
  98. unregister_widget( 'WP_Widget_Tag_Cloud' );
  99. }
  100. /**
  101. * Loads the Primary widget area. Users can overwrite 'aside-primary.php'.
  102. *
  103. * @since 0.2.2
  104. * @uses locate_template() Checks for template in child and parent theme.
  105. */
  106. function hybrid_get_primary() {
  107. locate_template( array( 'sidebar-primary.php' ), true );
  108. }
  109. /**
  110. * Loads the Secondary widget area. Users can overwrite 'aside-secondary.php'.
  111. *
  112. * @since 0.2.2
  113. * @uses locate_template() Checks for template in child and parent theme.
  114. */
  115. function hybrid_get_secondary() {
  116. locate_template( array( 'sidebar-secondary.php' ), true );
  117. }
  118. /**
  119. * Loads the Subsidiary widget area. Users can overwrite 'aside-subsidiary.php'.
  120. *
  121. * @since 0.3.1
  122. * @uses locate_template() Checks for template in child and parent theme.
  123. */
  124. function hybrid_get_subsidiary() {
  125. locate_template( array( 'sidebar-subsidiary.php' ), true );
  126. }
  127. /**
  128. * Loads the Utility: Before Content widget area. Users can overwrite
  129. * 'utility-before-content.php' in child themes.
  130. *
  131. * @since 0.4
  132. * @uses locate_template() Checks for template in child and parent theme.
  133. */
  134. function hybrid_get_utility_before_content() {
  135. locate_template( array( 'sidebar-before-content.php' ), true );
  136. }
  137. /**
  138. * Loads the Utility: After Content widget area. Users can overwrite
  139. * 'utility-after-content.php' in child themes.
  140. *
  141. * @since 0.4
  142. * @uses locate_template() Checks for template in child and parent theme.
  143. */
  144. function hybrid_get_utility_after_content() {
  145. locate_template( array( 'sidebar-after-content.php' ), true );
  146. }
  147. /**
  148. * Loads the Utility: After Singular widget area. Users can overwrite
  149. * 'utility-after-singular.php' in child themes.
  150. *
  151. * @since 0.7
  152. * @uses locate_template() Checks for template in child and parent theme.
  153. */
  154. function hybrid_get_utility_after_singular() {
  155. locate_template( array( 'sidebar-after-singular.php' ), true );
  156. }
  157. /**
  158. * Check for widgets in widget-ready areas. Allows user to completely collapse
  159. * widget-ready areas by not adding widgets to particular widget areas. Checks widget
  160. * areas by name and/or ID.
  161. *
  162. * @deprecated 0.6.1
  163. * Realizing that WordPress 2.8 introduced is_active_sidebar(), we're still using this
  164. * function because the WP function doesn't perform correctly. See ticket #10136.
  165. * @link http://core.trac.wordpress.org/ticket/10136
  166. *
  167. * Major props to Chaos Kaizer and Ian Stewart.
  168. * @link http://wordpress.org/support/topic/190184#post-808787
  169. * @link http://blog.kaizeku.com
  170. * @link http://themeshaper.com/collapsing-wordpress-widget-ready-areas-sidebars
  171. *
  172. * @since 0.2
  173. * @param string|int $index name|ID of widget area.
  174. * @return bool
  175. */
  176. function is_sidebar_active( $index = 1 ) {
  177. $sidebars_widgets = wp_get_sidebars_widgets();
  178. $index = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index );
  179. if ( isset( $sidebars_widgets[$index] ) && !empty( $sidebars_widgets[$index] ) )
  180. return true;
  181. return false;
  182. }
  183. /**
  184. * Removes all widget areas on the No Widgets page template. We're only going to run
  185. * it on the No Widgets template. Users that need additional templates without widgets
  186. * should create a simliar function in their child theme.
  187. *
  188. * We're retaining the 'sidebar' terminology until such time that the
  189. * WordPress community decides on a better term.
  190. *
  191. * @since 0.5
  192. * @uses sidebars_widgets Filter to remove all widget areas
  193. */
  194. function remove_sidebars( $sidebars_widgets ) {
  195. global $wp_query;
  196. if ( is_singular() ) {
  197. $template = get_post_meta( $wp_query->post->ID, "_wp_{$wp_query->post->post_type}_template", true );
  198. if ( 'no-widgets.php' == $template || "{$wp_query->post->post_type}-no-widgets.php" == $template )
  199. $sidebars_widgets = array( false );
  200. }
  201. return $sidebars_widgets;
  202. }
  203. ?>