CMB2_Show_Filters

Show On Filters Use the 'cmb2_show_on' filter to further refine the conditions under which a metabox is displayed.

Defined (1)

The class is defined in the following location(s).

/includes/CMB2_Show_Filters.php  
  1. class CMB2_Show_Filters { 
  2.  
  3. /** 
  4. * Get Show_on key. backwards compatible w/ 'key' indexes 
  5. * @since 2.0.0 
  6. * @param array $meta_box_args Metabox config array 
  7. * @return mixed show_on key or false 
  8. */ 
  9. private static function get_show_on_key( $meta_box_args ) { 
  10. $show_on = isset( $meta_box_args['show_on'] ) ? (array) $meta_box_args['show_on'] : false; 
  11. if ( $show_on && is_array( $show_on ) ) { 
  12.  
  13. if ( array_key_exists( 'key', $show_on ) ) { 
  14. return $show_on['key']; 
  15.  
  16. $keys = array_keys( $show_on ); 
  17. return $keys[0]; 
  18.  
  19. return false; 
  20.  
  21. /** 
  22. * Get Show_on value. backwards compatible w/ 'value' indexes 
  23. * @since 2.0.0 
  24. * @param array $meta_box_args Metabox config array 
  25. * @return mixed show_on value or false 
  26. */ 
  27. private static function get_show_on_value( $meta_box_args ) { 
  28. $show_on = isset( $meta_box_args['show_on'] ) ? (array) $meta_box_args['show_on'] : false; 
  29.  
  30. if ( $show_on && is_array( $show_on ) ) { 
  31.  
  32. if ( array_key_exists( 'value', $show_on ) ) { 
  33. return $show_on['value']; 
  34.  
  35. $keys = array_keys( $show_on ); 
  36.  
  37. return $show_on[ $keys[0] ]; 
  38.  
  39. return array(); 
  40.  
  41. /** 
  42. * Add metaboxes for an specific ID 
  43. * @since 1.0.0 
  44. * @param bool $display To display or not 
  45. * @param array $meta_box_args Metabox config array 
  46. * @param CMB2 $cmb The CMB2 instance. 
  47. * @return bool Whether to display this metabox on the current page. 
  48. */ 
  49. public static function check_id( $display, $meta_box_args, $cmb ) { 
  50.  
  51. $key = self::get_show_on_key( $meta_box_args ); 
  52. if ( ! $key || 'id' !== $key ) { 
  53. return $display; 
  54.  
  55. $object_id = is_admin() ? $cmb->object_id() : get_the_ID(); 
  56.  
  57. if ( ! $object_id ) { 
  58. return false; 
  59.  
  60. // If current page id is in the included array, display the metabox 
  61. return in_array( $object_id, (array) self::get_show_on_value( $meta_box_args ) ); 
  62.  
  63. /** 
  64. * Add metaboxes for an specific Page Template 
  65. * @since 1.0.0 
  66. * @param bool $display To display or not 
  67. * @param array $meta_box_args Metabox config array 
  68. * @return bool Whether to display this metabox on the current page. 
  69. */ 
  70. public static function check_page_template( $display, $meta_box_args, $cmb ) { 
  71.  
  72. $key = self::get_show_on_key( $meta_box_args ); 
  73. if ( ! $key || 'page-template' !== $key ) { 
  74. return $display; 
  75.  
  76. $object_id = $cmb->object_id(); 
  77.  
  78. if ( ! $object_id || 'post' !== $cmb->object_type() ) { 
  79. return false; 
  80.  
  81. // Get current template 
  82. $current_template = get_post_meta( $object_id, '_wp_page_template', true ); 
  83.  
  84. // See if there's a match 
  85. if ( $current_template && in_array( $current_template, (array) self::get_show_on_value( $meta_box_args ) ) ) { 
  86. return true; 
  87.  
  88. return false; 
  89.  
  90. /** 
  91. * Only show options-page metaboxes on their options page (but only enforce on the admin side) 
  92. * @since 1.0.0 
  93. * @param bool $display To display or not 
  94. * @param array $meta_box_args Metabox config array 
  95. * @return bool Whether to display this metabox on the current page. 
  96. */ 
  97. public static function check_admin_page( $display, $meta_box_args ) { 
  98.  
  99. $key = self::get_show_on_key( $meta_box_args ); 
  100. // check if this is a 'options-page' metabox 
  101. if ( ! $key || 'options-page' !== $key ) { 
  102. return $display; 
  103.  
  104. // Enforce 'show_on' filter in the admin 
  105. if ( is_admin() ) { 
  106.  
  107. // If there is no 'page' query var, our filter isn't applicable 
  108. if ( ! isset( $_GET['page'] ) ) { 
  109. return $display; 
  110.  
  111. $show_on = self::get_show_on_value( $meta_box_args ); 
  112.  
  113. if ( empty( $show_on ) ) { 
  114. return false; 
  115.  
  116. if ( is_array( $show_on ) ) { 
  117. foreach ( $show_on as $page ) { 
  118. if ( $_GET['page'] == $page ) { 
  119. return true; 
  120. } else { 
  121. if ( $_GET['page'] == $show_on ) { 
  122. return true; 
  123.  
  124. return false; 
  125.  
  126.  
  127. // Allow options-page metaboxes to be displayed anywhere on the front-end 
  128. return true; 
  129.