sidebars.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Sidebar helpers
  4. *
  5. * @package vamtam/consulting
  6. */
  7. /**
  8. * class VamtamSidebars
  9. *
  10. * register right/left, header and footer sidebars
  11. * also provides a function which outputs the correct right/left sidebar
  12. */
  13. class VamtamSidebars {
  14. /**
  15. * List of widget areas
  16. * @var array
  17. */
  18. private $sidebars = array();
  19. /**
  20. * List of sidebar placements
  21. * @var array
  22. */
  23. private $places = array();
  24. /**
  25. * Singleton instance
  26. * @var VamtamSidebars
  27. */
  28. private static $instance;
  29. /**
  30. * Set the available widgets area
  31. */
  32. public function __construct() {
  33. $this->sidebars = array(
  34. 'page' => esc_html__( 'Main Widget Area', 'vamtam-consulting' ),
  35. );
  36. if ( vamtam_has_woocommerce() )
  37. $this->sidebars['vamtam-woocommerce'] = esc_html__( 'WooCommerce Widget Area', 'vamtam-consulting' );
  38. $this->places = array( 'left', 'right' );
  39. }
  40. /**
  41. * Get singleton instance
  42. * @return VamtamSidebars singleton instance
  43. */
  44. public static function get_instance() {
  45. if ( ! isset( self::$instance ) )
  46. self::$instance = new self();
  47. return self::$instance;
  48. }
  49. /**
  50. * Register sidebars
  51. */
  52. public function register_sidebars() {
  53. unregister_sidebar( 'sidebar-event' );
  54. foreach ( $this->sidebars as $id => $name ) {
  55. foreach ( $this->places as $place ) {
  56. register_sidebar( array(
  57. 'id' => $id . '-' . $place,
  58. 'name' => $name . " ( $place )",
  59. 'description' => $name . " ( $place )",
  60. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  61. 'after_widget' => '</section>',
  62. ) );
  63. }
  64. }
  65. register_sidebar( array(
  66. 'id' => 'overlay-menu-sidebar',
  67. 'name' => __( 'Overlay Menu Widget Area', 'vamtam-consulting' ),
  68. 'description' => __( 'Displayed below the menu items in the overlay menu.', 'vamtam-consulting' ),
  69. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  70. 'after_widget' => '</section>',
  71. ) );
  72. }
  73. private function get_sidebar_name( $place = 'left' ) {
  74. global $post;
  75. if ( vamtam_has_woocommerce() && is_woocommerce() ) {
  76. $sidebar = 'vamtam-woocommerce';
  77. }
  78. if ( isset( $sidebar ) ) {
  79. return $sidebar . '-' . $place;
  80. }
  81. return 'page-' . $place;
  82. }
  83. /**
  84. * Output the correct sidebar
  85. *
  86. * @uses dynamic_sidebar()
  87. *
  88. * @param string $place one of $this->placements
  89. * @return bool result of dynamic_sidebar()
  90. */
  91. public function get_sidebar( $place = 'left' ) {
  92. $name = $this->get_sidebar_name( $place );
  93. dynamic_sidebar( $name );
  94. }
  95. /**
  96. * Check if we should show a sidebar
  97. *
  98. * @uses is_active_sidebar()
  99. *
  100. * @param string $place one of $this->placements
  101. * @return bool result of dynamic_sidebar()
  102. */
  103. public function has_sidebar( $place = 'left' ) {
  104. $name = $this->get_sidebar_name( $place );
  105. return is_active_sidebar( $name );
  106. }
  107. };