singleton. * * @since 2.0.9.1 */ private function __construct() { $this->debug = false; $this->current_user_id = get_current_user_id(); if ( 0 == $this->current_user_id ) { $this->debug = apply_filters( 'custom_sidebars_explain', $this->debug ); } else { $this->debug = (boolean) get_user_meta( $this->current_user_id, 'custom_sidebars_explain', true ); $this->set_explain(); } add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 999 ); if ( false === $this->debug ) { return; } if ( is_admin() ) { return; } add_action( 'cs_explain', array( $this, 'add_info' ), 10, 2 ); add_action( 'wp_footer', array( $this, 'show_infos' ) ); add_action( 'dynamic_sidebar_before', array( $this, 'before_sidebar' ), 0, 2 ); add_action( 'dynamic_sidebar_after', array( $this, 'after_sidebar' ), 0, 2 ); add_action( 'wp_print_styles', array( $this, 'print_styles' ) ); } /** * Returns true if the "explain mode" is enabled. * Explain mode will display additional information in the front-end of the * website on why which sidebar/widget is displayed. * This is a per-user option (stored in current session) * * @since 2.0.9.1 * @return boolean */ public function do_explain() { return $this->debug; } /** * Sets the explain state * * @since 2.0.9.1 * @param string $state [on|off] */ public function set_explain() { if ( ! isset( $_GET['cs-explain'] ) ) { return; } if ( current_user_can( 'manage_options' ) && 'on' == $_GET['cs-explain'] ) { $this->debug = true; $result = add_user_meta( $this->current_user_id, 'custom_sidebars_explain', $this->debug, true ); if ( ! $result ) { update_user_meta( $this->current_user_id, 'custom_sidebars_explain', $this->debug ); } return; } $this->debug = false; delete_user_meta( $this->current_user_id, 'custom_sidebars_explain' ); } /** * Adds an info to the explanation output. * * @since 2.0.9.1 */ public function add_info( $info, $new_item = false ) { if ( $new_item || 0 === count( $this->infos ) ) { $this->infos[] = $info; } else { $this->infos[ count( $this->infos ) - 1 ] .= '
' . $info; } } /** * Outputs the collected information to the webpage. * * @since 2.0.9.1 */ public function show_infos() { ?>

Sidebar Infos

' . '
%1$s
' . @$wp_registered_sidebars[ $index ]['before_widget']; $wp_registered_sidebars[ $index ]['after_widget'] = @$wp_registered_sidebars[ $index ]['after_widget'] . '
' . ''; ?>
'cs-explain', 'title' => __( 'Sidebar Debug', 'custom-sidebars' ), 'href' => add_query_arg( 'cs-explain', 'on' ), 'parent' => 'top-secondary', 'meta' => array( 'title' => __( 'Turn on Custom Sidebars explain mode.', 'custom-sidebars' ), 'class' => 'debug-is-off', ), ); if ( $this->debug ) { $args['href'] = add_query_arg( 'cs-explain', 'off' ); $args['meta'] = array( 'title' => __( 'Turn off Custom Sidebars explain mode.', 'custom-sidebars' ), 'class' => 'cs-explain-on', ); } $wp_admin_bar->add_node( $args ); } /** * Print style for debug * * @since 3.0.8 */ public function print_styles() { echo ''; } };