/includes/bws-widget-filters.php

  1. <?php 
  2.  
  3. /** Filter setup for widgets : 
  4. 'Categories' , 'Pages' , 'Archives' , 'Search' , 'Tag Cloud' , 'Custom Menu' , 'Recent Posts' , 'Meta' , and 'Comments' 
  5. */ 
  6.  
  7.  
  8. // Maybe filter 'Categories' , 'Pages' , and 'Archives' widgets 
  9. add_action( 'init' , 'bws_widget_filters' ) ; 
  10. function bws_widget_filters() {  
  11. bws_maybe_add_filters_of_types( array( 'categories' , 'pages' , 'archives' ) ) ; 
  12.  
  13. function bws_maybe_add_filters_of_types( $types ) { 
  14. foreach( $types as $type ) { 
  15. bws_add_filter_if_options_allow( $type ) ; 
  16.  
  17. function bws_add_filter_if_options_allow( $type ) { 
  18. if ( bws_do_options_allow_adding_filter_for_widget_type( $type ) ) { 
  19. bws_add_filter_for_widget_type( $type ) ; 
  20.  
  21. function bws_do_options_allow_adding_filter_for_widget_type( $type_of_widget ) { 
  22. $options = get_option( 'bws_plugin_options' ) ; 
  23. $widget_key = 'disable_' . $type_of_widget . '_widget' ; 
  24. if ( ( isset( $options[ $widget_key ] ) ) && ( '1' === $options[ $widget_key ] ) ) { 
  25. return false ; 
  26. return true ; 
  27.  
  28. function bws_add_filter_for_widget_type( $type ) { 
  29. if ( 'archives' === $type ) { 
  30. add_filter( 'get_archives_link' , array( 'BWS_Archives' , 'filter' ) ) ; 
  31. add_filter( 'dynamic_sidebar_params' , 'bws_add_closing_div_to_archives_widget' ) ; 
  32. else { 
  33. add_filter( 'wp_list_' . $type , array( 'BWS_' . $type , 'filter' ) ) ; 
  34.  
  35. function bws_add_closing_div_to_archives_widget( $params ) { 
  36. if ( isset( $params[ 0 ][ 'widget_name' ] ) && 'Archives' == $params[ 0 ][ 'widget_name' ] ) { 
  37. $params[ 0 ][ 'after_widget' ] = '</div>' . $params[ 0 ][ 'after_widget' ] ; 
  38. return $params ; 
  39. }  
  40.  
  41. // Filter search form widget 
  42. add_action( 'init' , 'bws_add_search_form_filter_if_option_allows' ) ;  
  43. function bws_add_search_form_filter_if_option_allows() { 
  44. $options = get_option( 'bws_plugin_options' ) ; 
  45. if ( ( isset( $options[ 'disable_search_widget' ] ) ) && ( '1' === $options[ 'disable_search_widget' ] ) ) { 
  46. return ; 
  47. } else { 
  48. add_filter( 'get_search_form' , array( 'BWS_Search_Widget' , 'filter' ) , '1' ) ; 
  49.  
  50.  
  51. // Filter tag cloud widget 
  52. add_filter( 'wp_tag_cloud' , 'bwp_filter_tag_cloud' ) ;  
  53. function bwp_filter_tag_cloud( $markup ) { 
  54. $regex = '/(<a[^>]+?>)([^<]+?)(<\/a>)/' ; 
  55. $replace_with = "$1<span class='label label-primary'>$2</span>$3" ; 
  56. $markup = preg_replace( $regex , $replace_with , $markup ) ; 
  57. return $markup ; 
  58.  
  59. // Filter menu widget 
  60. add_filter( 'widget_display_callback' , 'bws_search_for_menu_widget' , 4 , 10 ) ;  
  61. function bws_search_for_menu_widget( $instance , $widget , $args ) { 
  62. if ( ( isset( $widget->widget_options[ 'classname' ] ) ) && ( 'widget_nav_menu' == $widget->widget_options[ 'classname' ] ) ) { 
  63. add_filter( 'wp_nav_menu_items' , 'bws_filter_widget_menu' , 2 , 10 ) ; 
  64. return $instance ; 
  65.  
  66. function bws_filter_widget_menu( $menu_markup , $args ) { 
  67. if ( ( ! isset( $args->fallback_cb ) ) || ( "" == $args->fallback_cb ) ) { 
  68. $menu_markup = BWS_Menu::filter( $menu_markup ) ; 
  69. return $menu_markup ; 
  70.  
  71. // If there' a "Recent Posts" widget, enqueue bws-change-markup.js 
  72. add_filter( 'widget_posts_args' , 'bws_posts_enqueue_javascript' ) ;  
  73. function bws_posts_enqueue_javascript( $args ) { 
  74. wp_enqueue_script( BWS_PLUGIN_SLUG . '-script' , plugins_url( '/' . BWS_PLUGIN_SLUG . '/js/bws-change-markup.js' ) , array( 'jquery' ) ) ; 
  75. return $args ; 
  76.  
  77. // If there' a "Meta" widget, enqueue bws-change-markup.js 
  78. add_filter( 'widget_meta_poweredby' , 'bws_meta_enqueue_javascript' ) ; 
  79. function bws_meta_enqueue_javascript( $args ) { 
  80. wp_enqueue_script( BWS_PLUGIN_SLUG . '-script' , plugins_url( '/' . BWS_PLUGIN_SLUG . '/js/bws-change-markup.js' ) , array( 'jquery' ) ) ; 
  81. return $args ; 
  82.  
  83. // If there' a "Comments" widget, enqueue bws-change-markup.js 
  84. add_filter( 'widget_comments_args' , 'bws_comments_enqueue_javascript' ) ; 
  85. function bws_comments_enqueue_javascript( $args ) { 
  86. wp_enqueue_script( BWS_PLUGIN_SLUG . '-script' , plugins_url( '/' . BWS_PLUGIN_SLUG . '/js/bws-change-markup.js' ) , array( 'jquery' ) ) ; 
  87. return $args ; 
.