PluginProbe ʕ •ᴥ•ʔ
Custom Sidebars – Dynamic Sidebar Classic Widget Area Manager / 3.38
Custom Sidebars – Dynamic Sidebar Classic Widget Area Manager v3.38
trunk 2.1.2.0 3.0.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3 3.31 3.32 3.35 3.36 3.37 3.38
custom-sidebars / inc / class-custom-sidebars-explain.php
custom-sidebars / inc Last commit date
external 1 year ago integrations 7 years ago class-custom-sidebars-checkup-notification.php 1 year ago class-custom-sidebars-cloning.php 1 year ago class-custom-sidebars-editor.php 1 year ago class-custom-sidebars-explain.php 1 year ago class-custom-sidebars-export.php 1 year ago class-custom-sidebars-replacer.php 3 years ago class-custom-sidebars-visibility.php 1 year ago class-custom-sidebars-widgets.php 9 years ago class-custom-sidebars.php 1 year ago
class-custom-sidebars-explain.php
255 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 add_action( 'wp_print_styles', array( $this, 'print_styles' ) );
88 }
89
90 /**
91 * Returns true if the "explain mode" is enabled.
92 * Explain mode will display additional information in the front-end of the
93 * website on why which sidebar/widget is displayed.
94 * This is a per-user option (stored in current session)
95 *
96 * @since 2.0.9.1
97 * @return boolean
98 */
99 public function do_explain() {
100 return $this->debug;
101 }
102
103 /**
104 * Sets the explain state
105 *
106 * @since 2.0.9.1
107 * @param string $state [on|off]
108 */
109 public function set_explain() {
110 if ( ! isset( $_GET['cs-explain'] ) ) {
111 return;
112 }
113 if ( current_user_can( 'manage_options' ) && 'on' == $_GET['cs-explain'] ) {
114 $this->debug = true;
115 $result = add_user_meta( $this->current_user_id, 'custom_sidebars_explain', $this->debug, true );
116 if ( ! $result ) {
117 update_user_meta( $this->current_user_id, 'custom_sidebars_explain', $this->debug );
118 }
119 return;
120 }
121 $this->debug = false;
122 delete_user_meta( $this->current_user_id, 'custom_sidebars_explain' );
123 }
124
125 /**
126 * Adds an info to the explanation output.
127 *
128 * @since 2.0.9.1
129 */
130 public function add_info( $info, $new_item = false ) {
131 if ( $new_item || 0 === count( $this->infos ) ) {
132 $this->infos[] = $info;
133 } else {
134 $this->infos[ count( $this->infos ) - 1 ] .= '<br />' . $info;
135 }
136 }
137
138 /**
139 * Outputs the collected information to the webpage.
140 *
141 * @since 2.0.9.1
142 */
143 public function show_infos() {
144 ?>
145 <div class="cs-infos" style="width:600px;margin:10px auto;padding:10px;color:#666;background:#FFF;">
146 <style>
147 .cs-infos > ul { list-style:none; padding: 0; margin: 0; }
148 .cs-infos > ul > li { margin: 0; padding: 10px 0 10px 30px; border-bottom: 1px solid #eee; }
149 .cs-infos h4 { color: #600; margin: 10px 0 0 -30px; }
150 .cs-infos h5 { color: #006; margin: 10px 0 0 -15px; }
151 </style>
152 <h3>Sidebar Infos</h3>
153 <ul>
154 <?php foreach ( $this->infos as $info ) : ?>
155 <li><?php CustomSidebars::wp_kses_wf($info); ?></li>
156 <?php endforeach; ?>
157 </ul>
158 </div>
159 <?php
160 }
161
162 /**
163 * Returns a random hex color.
164 *
165 * @since 2.0.9.1
166 * @return [type] [description]
167 */
168 static public function get_color() {
169 $r = wp_rand( 40, 140 );
170 $g = wp_rand( 40, 140 );
171 $b = wp_rand( 40, 140 );
172 return '#' . dechex( $r ) . dechex( $g ) . dechex( $b );
173 }
174
175 /**
176 * Adds a border/title to the sidebar to better illustrate the position/ID.
177 *
178 * @since 2.0.9.1
179 */
180 public function before_sidebar( $index, $has_widgets ) {
181 global $wp_registered_sidebars;
182 $col = self::get_color();
183 $w_col = self::get_color();
184
185 $wp_registered_sidebars[ $index ]['before_widget'] =
186 '<div style="border:2px solid ' . $w_col . ';margin:2px;width:auto;clear:both">' .
187 '<div style="font-size:12px;padding:1px 4px 1px 6px;float:right;background-color:' . $w_col . ';color:#FFF">%1$s</div>' .
188 @$wp_registered_sidebars[ $index ]['before_widget'];
189 $wp_registered_sidebars[ $index ]['after_widget'] =
190 @$wp_registered_sidebars[ $index ]['after_widget'] .
191 '<div style="clear:both"> </div>' .
192 '</div>';
193 ?>
194 <div style="border:2px solid <?php echo esc_attr( $col ); ?>;position:relative;">
195 <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>
196 <?php
197 }
198
199 /**
200 * Closes the border around sidebar.
201 *
202 * @since 2.0.9.1
203 */
204 public function after_sidebar( $index, $has_widgets ) {
205 ?>
206 <div style="clear:both"> </div>
207 </div>
208 <?php
209 }
210
211 /**
212 * Added adminbar position for debug
213 *
214 * @since 3.0.7
215 */
216 public function admin_bar_menu( $wp_admin_bar ) {
217 if ( is_admin() ) {
218 return;
219 }
220 if ( ! current_user_can( 'manage_options' ) ) {
221 return;
222 }
223 $args = array(
224 'id' => 'cs-explain',
225 'title' => __( 'Sidebar Debug', 'custom-sidebars' ),
226 'href' => add_query_arg( 'cs-explain', 'on' ),
227 'parent' => 'top-secondary',
228 'meta' => array(
229 'title' => __( 'Turn on Custom Sidebars explain mode.', 'custom-sidebars' ),
230 'class' => 'debug-is-off',
231 ),
232 );
233 if ( $this->debug ) {
234 $args['href'] = add_query_arg( 'cs-explain', 'off' );
235 $args['meta'] = array(
236 'title' => __( 'Turn off Custom Sidebars explain mode.', 'custom-sidebars' ),
237 'class' => 'cs-explain-on',
238 );
239 }
240 $wp_admin_bar->add_node( $args );
241 }
242
243 /**
244 * Print style for debug
245 *
246 * @since 3.0.8
247 */
248 public function print_styles() {
249 echo '<style type="text/css" media="screen">';
250 echo '#wpadminbar .cs-explain-on{ background-color:#050}';
251 echo 'html body #wpadminbar .ab-top-menu .cs-explain-on:hover>.ab-item{background-color:#251}';
252 echo '</style>';
253 }
254 };
255