external
9 years ago
class-custom-sidebars-checkup-notification.php
9 years ago
class-custom-sidebars-cloning.php
9 years ago
class-custom-sidebars-editor.php
9 years ago
class-custom-sidebars-explain.php
9 years ago
class-custom-sidebars-export.php
9 years ago
class-custom-sidebars-replacer.php
9 years ago
class-custom-sidebars-visibility.php
9 years ago
class-custom-sidebars-widgets.php
9 years ago
class-custom-sidebars.php
9 years ago
class-custom-sidebars-explain.php
235 lines
| 1 | <?php |
| 2 | |
| 3 | add_action( 'cs_init', array( 'CustomSidebarsExplain', 'instance' ) ); |
| 4 | |
| 5 | /** |
| 6 | * Adds some additional information to the page output which explain why which |
| 7 | * Sidebar/widgets were added to the current page. |
| 8 | * |
| 9 | * =================================== USAGE =================================== |
| 10 | * |
| 11 | * Activate the explanation mode via URL parameter: "?cs-explain=on" |
| 12 | * Deactiavte by setting the parameter to "off" |
| 13 | * |
| 14 | * The explanation is only displayed for the user that did activate it, other |
| 15 | * users will not see anything. |
| 16 | * |
| 17 | * Explain-mode will possibly break the layout of the page, but it makes it |
| 18 | * much easier to understand which sidebars and widgets are displayed and why. |
| 19 | * It is meant for temporary debugging only and should be turned off when not |
| 20 | * needed anymore. |
| 21 | * |
| 22 | * ============================================================================= |
| 23 | * |
| 24 | */ |
| 25 | class CustomSidebarsExplain extends CustomSidebars { |
| 26 | |
| 27 | /** |
| 28 | * Infos added via cs_explain. |
| 29 | * @var array |
| 30 | */ |
| 31 | private $infos = array(); |
| 32 | |
| 33 | /** |
| 34 | * Explain debug status. |
| 35 | * |
| 36 | * @since 3.0.7 |
| 37 | */ |
| 38 | private $debug = false; |
| 39 | |
| 40 | /** |
| 41 | * Current user id |
| 42 | * |
| 43 | * @since 3.0.7 |
| 44 | */ |
| 45 | private $current_user_id = 0; |
| 46 | |
| 47 | /** |
| 48 | * Returns the singleton object. |
| 49 | * |
| 50 | * @since 2.0.9.1 |
| 51 | */ |
| 52 | public static function instance() { |
| 53 | static $Inst = null; |
| 54 | |
| 55 | if ( null === $Inst ) { |
| 56 | $Inst = new CustomSidebarsExplain(); |
| 57 | } |
| 58 | |
| 59 | return $Inst; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Constructor is private -> singleton. |
| 64 | * |
| 65 | * @since 2.0.9.1 |
| 66 | */ |
| 67 | private function __construct() { |
| 68 | $this->debug = false; |
| 69 | $this->current_user_id = get_current_user_id(); |
| 70 | if ( 0 == $this->current_user_id ) { |
| 71 | $this->debug = apply_filters( 'custom_sidebars_explain', $this->debug ); |
| 72 | } else { |
| 73 | $this->debug = (boolean) get_user_meta( $this->current_user_id, 'custom_sidebars_explain', true ); |
| 74 | $this->set_explain(); |
| 75 | } |
| 76 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 999 ); |
| 77 | if ( false === $this->debug ) { |
| 78 | return; |
| 79 | } |
| 80 | if ( is_admin() ) { |
| 81 | return; |
| 82 | } |
| 83 | add_action( 'cs_explain', array( $this, 'add_info' ), 10, 2 ); |
| 84 | add_action( 'wp_footer', array( $this, 'show_infos' ) ); |
| 85 | add_action( 'dynamic_sidebar_before', array( $this, 'before_sidebar' ), 0, 2 ); |
| 86 | add_action( 'dynamic_sidebar_after', array( $this, 'after_sidebar' ), 0, 2 ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns true if the "explain mode" is enabled. |
| 91 | * Explain mode will display additional information in the front-end of the |
| 92 | * website on why which sidebar/widget is displayed. |
| 93 | * This is a per-user option (stored in current session) |
| 94 | * |
| 95 | * @since 2.0.9.1 |
| 96 | * @return boolean |
| 97 | */ |
| 98 | public function do_explain() { |
| 99 | return $this->debug; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Sets the explain state |
| 104 | * |
| 105 | * @since 2.0.9.1 |
| 106 | * @param string $state [on|off] |
| 107 | */ |
| 108 | public function set_explain() { |
| 109 | if ( ! isset( $_GET['cs-explain'] ) ) { |
| 110 | return; |
| 111 | } |
| 112 | if ( current_user_can( 'manage_options' ) && 'on' == $_GET['cs-explain'] ) { |
| 113 | $this->debug = true; |
| 114 | $result = add_user_meta( $this->current_user_id, 'custom_sidebars_explain', $this->debug, true ); |
| 115 | if ( ! $result ) { |
| 116 | update_user_meta( $this->current_user_id, 'custom_sidebars_explain', $this->debug ); |
| 117 | } |
| 118 | return; |
| 119 | } |
| 120 | $this->debug = false; |
| 121 | delete_user_meta( $this->current_user_id, 'custom_sidebars_explain' ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Adds an info to the explanation output. |
| 126 | * |
| 127 | * @since 2.0.9.1 |
| 128 | */ |
| 129 | public function add_info( $info, $new_item = false ) { |
| 130 | if ( $new_item ) { |
| 131 | $this->infos[] = $info; |
| 132 | } else { |
| 133 | $this->infos[ count( $this->infos ) - 1 ] .= '<br />' . $info; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Outputs the collected information to the webpage. |
| 139 | * |
| 140 | * @since 2.0.9.1 |
| 141 | */ |
| 142 | public function show_infos() { |
| 143 | ?> |
| 144 | <div class="cs-infos" style="width:600px;margin:10px auto;padding:10px;color:#666;background:#FFF;"> |
| 145 | <style> |
| 146 | .cs-infos > ul { list-style:none; padding: 0; margin: 0; } |
| 147 | .cs-infos > ul > li { margin: 0; padding: 10px 0 10px 30px; border-bottom: 1px solid #eee; } |
| 148 | .cs-infos h4 { color: #600; margin: 10px 0 0 -30px; } |
| 149 | .cs-infos h5 { color: #006; margin: 10px 0 0 -15px; } |
| 150 | </style> |
| 151 | <h3>Sidebar Infos</h3> |
| 152 | <ul> |
| 153 | <?php foreach ( $this->infos as $info ) : ?> |
| 154 | <li><?php echo $info; ?></li> |
| 155 | <?php endforeach; ?> |
| 156 | </ul> |
| 157 | </div> |
| 158 | <?php |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns a random hex color. |
| 163 | * |
| 164 | * @since 2.0.9.1 |
| 165 | * @return [type] [description] |
| 166 | */ |
| 167 | static public function get_color() { |
| 168 | $r = rand( 40, 140 ); |
| 169 | $g = rand( 40, 140 ); |
| 170 | $b = rand( 40, 140 ); |
| 171 | return '#' . dechex( $r ) . dechex( $g ) . dechex( $b ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Adds a border/title to the sidebar to better illustrate the position/ID. |
| 176 | * |
| 177 | * @since 2.0.9.1 |
| 178 | */ |
| 179 | public function before_sidebar( $index, $has_widgets ) { |
| 180 | global $wp_registered_sidebars; |
| 181 | $col = self::get_color(); |
| 182 | $w_col = self::get_color(); |
| 183 | |
| 184 | $wp_registered_sidebars[ $index ]['before_widget'] = |
| 185 | '<div style="border:2px solid ' . $w_col . ';margin:2px;width:auto;clear:both">' . |
| 186 | '<div style="font-size:12px;padding:1px 4px 1px 6px;float:right;background-color:' . $w_col . ';color:#FFF">%1$s</div>' . |
| 187 | @$wp_registered_sidebars[ $index ]['before_widget']; |
| 188 | $wp_registered_sidebars[ $index ]['after_widget'] = |
| 189 | @$wp_registered_sidebars[ $index ]['after_widget'] . |
| 190 | '<div style="clear:both"> </div>' . |
| 191 | '</div>'; |
| 192 | ?> |
| 193 | <div style="border:2px solid <?php echo esc_attr( $col ); ?>;position:relative;"> |
| 194 | <div style="font-size:12px;padding:1px 4px 1px 6px;float:right;background-color:<?php echo esc_attr( $col ); ?>;margin-bottom:2px;color:#FFF"><?php echo esc_html( $index ); ?></div> |
| 195 | <?php |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Closes the border around sidebar. |
| 200 | * |
| 201 | * @since 2.0.9.1 |
| 202 | */ |
| 203 | public function after_sidebar( $index, $has_widgets ) { |
| 204 | ?> |
| 205 | <div style="clear:both"> </div> |
| 206 | </div> |
| 207 | <?php |
| 208 | } |
| 209 | |
| 210 | |
| 211 | public function admin_bar_menu( $wp_admin_bar ) { |
| 212 | if ( ! current_user_can( 'manage_options' ) ) { |
| 213 | return; |
| 214 | } |
| 215 | $args = array( |
| 216 | 'id' => 'cs-explain', |
| 217 | 'title' => __( 'Turn on explanations', 'custom-sidebars' ), |
| 218 | 'href' => add_query_arg( 'cs-explain', 'on' ), |
| 219 | 'parent' => 'top-secondary', |
| 220 | 'meta' => array( |
| 221 | 'title' => __( 'Turn on Custom Sidebars explain mode.', 'custom-sidebars' ), |
| 222 | ), |
| 223 | ); |
| 224 | if ( $this->debug ) { |
| 225 | $args['href'] = add_query_arg( 'cs-explain', 'off' ); |
| 226 | $args['title'] = __( 'Turn off explanations', 'custom-sidebars' ); |
| 227 | $args['meta'] = array( |
| 228 | 'title' => __( 'Turn off Custom Sidebars explain mode.', 'custom-sidebars' ), |
| 229 | 'class' => 'cs-explain-on', |
| 230 | ); |
| 231 | } |
| 232 | $wp_admin_bar->add_node( $args ); |
| 233 | } |
| 234 | }; |
| 235 |