Changeset 1783934
- Timestamp:
- 12/09/2017 02:02:47 PM (8 years ago)
- File:
-
- 1 edited
-
ah-display-widgets/trunk/ah-display-widgets.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ah-display-widgets/trunk/ah-display-widgets.php
r1739685 r1783934 6 6 Author: Andreas Hecht 7 7 Author URI: https://andreas-hecht.com 8 Version: 1.0. 18 Version: 1.0.2 9 9 License: GPL2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 13 13 */ 14 14 15 16 // If this file is called directly, abort. 17 if ( ! defined( 'WPINC' ) ) { 18 die; 15 /* 16 // Change the hook this is triggered on with a bit of custom code. Copy and paste into your theme functions.php or a new plugin. 17 add_filter('dw_callback_trigger', 'dw_callback_trigger'); 18 function dw_callback_trigger(){ 19 return 'wp_head'; //plugins_loaded, after_setup_theme, wp_loaded, wp_head 19 20 } 21 */ 20 22 21 23 22 24 23 25 class AH_Display_Widgets extends WP_Widget { 24 25 26 27 26 28 var $transient_name = 'ah_dw_details'; 27 var $checked = array(); 28 var $id_base = ''; 29 var $number = ''; 30 31 // pages on site 32 var $pages = array(); 33 34 // custom post types 35 var $cposts = array(); 36 37 // taxonomies 38 var $taxes = array(); 39 40 // categories 41 var $cats = array(); 42 43 // WPML languages 44 var $langs = array(); 45 46 29 var $checked = array(); 30 var $id_base = ''; 31 var $number = ''; 32 33 // pages on site 34 var $pages = array(); 35 36 // custom post types 37 var $cposts = array(); 38 39 // taxonomies 40 var $taxes = array(); 41 42 // categories 43 var $cats = array(); 44 45 // WPML languages 46 var $langs = array(); 47 47 48 48 public function __construct() { 49 49 50 add_filter( 'widget_display_callback', array( &$this, 'show_widget' ) );51 52 // change the hook that triggers widget check53 $hook = apply_filters( 'ah_dw_callback_trigger', 'wp_loaded' );54 55 add_action( $hook, array( &$this, 'trigger_widget_checks' ) );56 add_action( 'in_widget_form', array( &$this, 'hidden_widget_options'), 10, 3 );57 add_filter( 'widget_update_callback', array( &$this, 'update_widget_options' ), 10, 3 );58 add_action( 'wp_ajax_dw_show_widget', array( &$this, 'show_widget_options' ) );59 add_action( 'admin_footer', array( &$this, 'load_js' ) );60 61 // when a page is saved62 add_action( 'save_post_page', array( &$this, 'delete_transient' ) );63 64 // when a new category/taxonomy is created65 add_action( 'created_term', array( &$this, 'delete_transient' ) );66 67 // when a custom post type is added68 add_action( 'update_option_rewrite_rules', array( &$this, 'delete_transient' ) );69 70 // reset transient after activating the plugin71 register_activation_hook( dirname(__FILE__) . '/ah-display-widgets.php', array( &$this, 'delete_transient' ) );72 73 add_action( 'plugins_loaded', array( &$this, 'load_lang' ) );74 75 // get custom Page Walker50 add_filter( 'widget_display_callback', array( &$this, 'show_widget' ) ); 51 52 // change the hook that triggers widget check 53 $hook = apply_filters( 'dw_callback_trigger', 'wp_loaded' ); 54 55 add_action( $hook, array( &$this, 'trigger_widget_checks' ) ); 56 add_action( 'in_widget_form', array( &$this, 'hidden_widget_options'), 10, 3 ); 57 add_filter( 'widget_update_callback', array( &$this, 'update_widget_options' ), 10, 3 ); 58 add_action( 'wp_ajax_dw_show_widget', array( &$this, 'show_widget_options' ) ); 59 add_action( 'admin_footer', array( &$this, 'load_js' ) ); 60 61 // when a page is saved 62 add_action( 'save_post_page', array( &$this, 'delete_transient' ) ); 63 64 // when a new category/taxonomy is created 65 add_action( 'created_term', array( &$this, 'delete_transient' ) ); 66 67 // when a custom post type is added 68 add_action( 'update_option_rewrite_rules', array( &$this, 'delete_transient' ) ); 69 70 // reset transient after activating the plugin 71 register_activation_hook( dirname(__FILE__) . '/display-widgets.php', array( &$this, 'delete_transient' ) ); 72 73 add_action( 'plugins_loaded', array( &$this, 'load_lang' ) ); 74 75 // get custom Page Walker 76 76 $this->page_list = new AH_DW_Walker_Page_List(); 77 } 78 79 function trigger_widget_checks() { 80 81 add_filter( 'sidebars_widgets', array( &$this, 'sidebars_widgets' ) ); 82 } 83 84 85 function show_widget( $instance ) { 86 $instance['dw_logged'] = self::show_logged( $instance ); 87 88 // check logged in first 89 if ( in_array( $instance['dw_logged'], array( 'in', 'out' ) ) ) { 90 $user_ID = is_user_logged_in(); 91 if ( ( 'out' == $instance['dw_logged'] && $user_ID ) || ( 'in' == $instance['dw_logged'] && ! $user_ID ) ) { 92 return false; 93 } 94 } 95 96 $post_id = get_queried_object_id(); 97 $post_id = self::get_lang_id( $post_id, 'page' ); 98 99 if ( is_home() ) { 100 $show = isset( $instance['page-home'] ) ? $instance['page-home'] : false; 101 if ( ! $show && $post_id ) { 102 $show = isset( $instance[ 'page-' . $post_id ] ) ? $instance[ 'page-' . $post_id ] : false; 103 } 104 105 // check if blog page is front page too 106 if ( ! $show && is_front_page() && isset( $instance['page-front'] ) ) { 107 $show = $instance['page-front']; 108 } 109 } else if ( is_front_page() ) { 110 $show = isset( $instance['page-front'] ) ? $instance['page-front'] : false; 111 if ( ! $show && $post_id ) { 112 $show = isset( $instance[ 'page-' . $post_id ] ) ? $instance[ 'page-' . $post_id ] : false; 113 } 114 } else if ( is_category() ) { 115 $show = isset( $instance['cat-all'] ) ? $instance['cat-all'] : false; 116 117 if ( ! $show ) { 118 $show = isset( $instance['cat-' . get_query_var('cat') ] ) ? $instance[ 'cat-' . get_query_var('cat') ] : false; 119 } 120 } else if ( is_tax() ) { 121 $term = get_queried_object(); 122 $show = isset( $instance[ 'tax-' . $term->taxonomy ] ) ? $instance[ 'tax-'. $term->taxonomy] : false; 123 unset( $term ); 124 } else if ( is_post_type_archive() ) { 125 $type = get_post_type(); 126 $show = isset( $instance[ 'type-' . $type . '-archive' ] ) ? $instance[ 'type-' . $type . '-archive' ] : false; 127 } else if ( is_archive() ) { 128 $show = isset( $instance['page-archive'] ) ? $instance['page-archive'] : false; 129 } else if ( is_single() ) { 130 $type = get_post_type(); 131 if ( $type != 'page' && $type != 'post' ) { 132 $show = isset( $instance[ 'type-' . $type ] ) ? $instance[ 'type-' . $type ] : false; 133 } 134 135 if ( ! isset( $show ) ) { 136 $show = isset( $instance['page-single'] ) ? $instance['page-single'] : false; 137 } 138 139 if ( ! $show ) { 140 $cats = get_the_category(); 141 foreach ( $cats as $cat ) { 142 if ( $show ) { 143 break; 144 } 145 $c_id = self::get_lang_id( $cat->cat_ID, 'category' ); 146 if ( isset( $instance[ 'cat-' . $c_id ] ) ) { 147 $show = $instance[ 'cat-' . $c_id ]; 148 } 149 unset( $c_id, $cat ); 150 } 151 } 152 153 } else if ( is_404() ) { 154 $show = isset( $instance['page-404'] ) ? $instance['page-404'] : false; 155 } else if ( is_search() ) { 156 $show = isset( $instance['page-search'] ) ? $instance['page-search'] : false; 157 } else if ( $post_id ) { 158 $show = isset( $instance[ 'page-' . $post_id ] ) ? $instance[ 'page-' . $post_id ] : false; 159 } else { 160 $show = false; 161 } 162 163 if ( $post_id && ! $show && isset( $instance['other_ids'] ) && ! empty( $instance['other_ids'] ) ) { 164 $other_ids = explode( ',', $instance['other_ids'] ); 165 foreach ( $other_ids as $other_id ) { 166 if ( $post_id == (int) $other_id ) { 167 $show = true; 168 } 169 } 170 } 171 172 $show = apply_filters( 'dw_instance_visibility', $show, $instance ); 173 174 if ( ! $show && defined( 'ICL_LANGUAGE_CODE' ) ) { 175 // check for WPML widgets 176 $show = isset( $instance[ 'lang-' . ICL_LANGUAGE_CODE ] ) ? $instance[ 'lang-' . ICL_LANGUAGE_CODE ] : false; 177 } 178 179 if ( ! isset( $show ) ) { 180 $show = false; 181 } 182 183 $instance['dw_include'] = isset( $instance['dw_include'] ) ? $instance['dw_include'] : 0; 184 185 if ( ( $instance['dw_include'] && false == $show ) || ( 0 == $instance['dw_include'] && $show ) ) { 186 return false; 187 } else if ( defined('ICL_LANGUAGE_CODE') && $instance['dw_include'] && $show && ! isset( $instance[ 'lang-' . ICL_LANGUAGE_CODE ] ) ) { 188 //if the widget has to be visible here, but the current language has not been checked, return false 189 return false; 190 } 191 192 return $instance; 193 } 194 195 function sidebars_widgets( $sidebars ) { 196 if ( is_admin() ) { 197 return $sidebars; 198 } 199 200 global $wp_registered_widgets; 201 202 foreach ( $sidebars as $s => $sidebar ) { 203 if ( $s == 'wp_inactive_widgets' || strpos( $s, 'orphaned_widgets' ) === 0 || empty( $sidebar ) ) { 204 continue; 205 } 206 207 foreach ( $sidebar as $w => $widget ) { 208 // $widget is the id of the widget 209 if ( ! isset( $wp_registered_widgets[ $widget ] ) ) { 210 continue; 211 } 212 213 if ( isset( $this->checked[ $widget ] ) ) { 214 $show = $this->checked[ $widget ]; 215 } else { 216 $opts = $wp_registered_widgets[ $widget ]; 217 $id_base = is_array( $opts['callback'] ) ? $opts['callback'][0]->id_base : $opts['callback']; 218 219 if ( ! $id_base ) { 220 continue; 221 } 222 223 $instance = get_option( 'widget_' . $id_base ); 224 225 if ( ! $instance || ! is_array( $instance ) ) { 226 continue; 227 } 228 229 if ( isset( $instance['_multiwidget'] ) && $instance['_multiwidget'] ) { 230 $number = $opts['params'][0]['number']; 231 if ( ! isset( $instance[ $number ] ) ) { 232 continue; 233 } 234 235 $instance = $instance[ $number ]; 236 unset( $number ); 237 } 238 239 unset( $opts ); 240 241 $show = self::show_widget( $instance ); 242 243 $this->checked[ $widget ] = $show ? true : false; 244 } 245 246 if ( ! $show ) { 247 unset( $sidebars[ $s ][ $w ] ); 248 } 249 250 unset( $widget ); 251 } 252 unset( $sidebar ); 253 } 254 255 return $sidebars; 256 } 257 258 function hidden_widget_options( $widget, $return, $instance ) { 259 if ( $_POST && isset( $_POST['id_base'] ) && $_POST['id_base'] == $widget->id_base ) { 260 // widget was just saved so it's open 261 self::show_hide_widget_options( $widget, $return, $instance ); 262 return; 263 } 264 265 self::register_globals(); 266 267 $instance['dw_include'] = isset( $instance['dw_include'] ) ? $instance['dw_include'] : 0; 268 $instance['dw_logged'] = self::show_logged( $instance ); 269 $instance['other_ids'] = isset( $instance['other_ids'] ) ? $instance['other_ids'] : ''; 270 ?> 271 <div class="dw_opts"> 272 <input type="hidden" name="<?php echo esc_attr( $widget->get_field_name('dw_include') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('dw_include') ); ?>" value="<?php echo esc_attr( $instance['dw_include'] ) ?>" /> 273 <input type="hidden" id="<?php echo esc_attr( $widget->get_field_id('dw_logged') ); ?>" name="<?php echo esc_attr( $widget->get_field_name('dw_logged') ); ?>" value="<?php echo esc_attr( $instance['dw_logged'] ) ?>" /> 274 275 <?php 276 foreach ( $instance as $k => $v ) { 277 if ( ! $v ) { 278 continue; 279 } 280 281 if ( strpos( $k, 'page-' ) === 0 || strpos( $k, 'type-' ) === 0 || strpos( $k, 'cat-' ) === 0 || strpos( $k, 'tax-' ) === 0 || strpos( $k, 'lang-' ) === 0 ) { 282 ?> 283 <input type="hidden" id="<?php echo esc_attr( $widget->get_field_id( $k ) ); ?>" name="<?php echo esc_attr( $widget->get_field_name( $k ) ); ?>" value="<?php echo esc_attr( $v ) ?>" /> 284 <?php 285 } 286 } ?> 287 288 <input type="hidden" name="<?php echo esc_attr( $widget->get_field_name('other_ids') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('other_ids') ); ?>" value="<?php echo esc_attr( $instance['other_ids'] ) ?>" /> 289 </div> 290 <?php 77 291 } 78 292 79 293 80 public function trigger_widget_checks() { 81 add_filter( 'sidebars_widgets', array( &$this, 'sidebars_widgets' ) ); 82 } 83 84 85 86 public function show_widget( $instance ) { 87 88 $instance['ah_dw_logged'] = self::show_logged( $instance ); 89 90 // check logged in first 91 if ( in_array( $instance['ah_dw_logged'], array( 'in', 'out' ) ) ) { 92 93 $user_ID = is_user_logged_in(); 94 if ( ( 'out' == $instance['ah_dw_logged'] && $user_ID ) || ( 'in' == $instance['ah_dw_logged'] && ! $user_ID ) ) { 95 return false; 96 } 97 } 98 99 100 $post_id = get_queried_object_id(); 101 $post_id = self::get_lang_id( $post_id, 'page' ); 102 103 104 if ( is_home() ) { 105 106 $show = isset( $instance['page-home'] ) ? $instance['page-home'] : false; 107 if ( ! $show && $post_id ) { 108 $show = isset( $instance[ 'page-' . $post_id ] ) ? $instance[ 'page-' . $post_id ] : false; 109 } 110 111 112 // check if blog page is front page too 113 if ( ! $show && is_front_page() && isset( $instance['page-front'] ) ) { 114 $show = $instance['page-front']; 115 } 116 117 } else if ( is_front_page() ) { 118 119 $show = isset( $instance['page-front'] ) ? $instance['page-front'] : false; 120 if ( ! $show && $post_id ) { 121 $show = isset( $instance[ 'page-' . $post_id ] ) ? $instance[ 'page-' . $post_id ] : false; 122 } 123 124 } else if ( is_category() ) { 125 126 $show = isset( $instance['cat-all'] ) ? $instance['cat-all'] : false; 127 128 if ( ! $show ) { 129 $show = isset( $instance['cat-' . get_query_var('cat') ] ) ? $instance[ 'cat-' . get_query_var('cat') ] : false; 130 } 131 132 } else if ( is_tax() ) { 133 134 $term = get_queried_object(); 135 $show = isset( $instance[ 'tax-' . $term->taxonomy ] ) ? $instance[ 'tax-'. $term->taxonomy] : false; 136 unset( $term ); 137 138 } else if ( is_post_type_archive() ) { 139 140 $type = get_post_type(); 141 $show = isset( $instance[ 'type-' . $type . '-archive' ] ) ? $instance[ 'type-' . $type . '-archive' ] : false; 142 143 } else if ( is_archive() ) { 144 145 $show = isset( $instance['page-archive'] ) ? $instance['page-archive'] : false; 146 147 } else if ( is_single() ) { 148 149 $type = get_post_type(); 150 if ( $type != 'page' && $type != 'post' ) { 151 $show = isset( $instance[ 'type-' . $type ] ) ? $instance[ 'type-' . $type ] : false; 152 } 153 154 if ( ! isset( $show ) ) { 155 $show = isset( $instance['page-single'] ) ? $instance['page-single'] : false; 156 } 157 158 if ( ! $show ) { 159 $cats = get_the_category(); 160 foreach ( $cats as $cat ) { 161 if ( $show ) { 162 break; 163 } 164 $c_id = self::get_lang_id( $cat->cat_ID, 'category' ); 165 if ( isset( $instance[ 'cat-' . $c_id ] ) ) { 166 $show = $instance[ 'cat-' . $c_id ]; 167 } 168 unset( $c_id, $cat ); 169 } 170 } 171 172 } else if ( is_404() ) { 173 174 $show = isset( $instance['page-404'] ) ? $instance['page-404'] : false; 175 176 } else if ( is_search() ) { 177 178 $show = isset( $instance['page-search'] ) ? $instance['page-search'] : false; 179 180 } else if ( $post_id ) { 181 182 $show = isset( $instance[ 'page-' . $post_id ] ) ? $instance[ 'page-' . $post_id ] : false; 183 184 } else { 185 186 $show = false; 187 } 188 189 if ( $post_id && ! $show && isset( $instance['other_ids'] ) && ! empty( $instance['other_ids'] ) ) { 190 $other_ids = explode( ',', $instance['other_ids'] ); 191 foreach ( $other_ids as $other_id ) { 192 if ( $post_id == (int) $other_id ) { 193 $show = true; 194 } 195 } 196 } 197 198 $show = apply_filters( 'ah_dw_instance_visibility', $show, $instance ); 199 200 if ( ! $show && defined( 'ICL_LANGUAGE_CODE' ) ) { 201 // check for WPML widgets 202 $show = isset( $instance[ 'lang-' . ICL_LANGUAGE_CODE ] ) ? $instance[ 'lang-' . ICL_LANGUAGE_CODE ] : false; 203 } 204 205 if ( ! isset( $show ) ) { 206 $show = false; 207 } 208 209 $instance['ah_dw_include'] = isset( $instance['ah_dw_include'] ) ? $instance['ah_dw_include'] : 0; 210 211 212 213 if ( ( $instance['ah_dw_include'] && false == $show ) || ( 0 == $instance['ah_dw_include'] && $show ) ) { 214 215 return false; 216 217 } else if ( defined('ICL_LANGUAGE_CODE') && $instance['ah_dw_include'] && $show && ! isset( $instance[ 'lang-' . ICL_LANGUAGE_CODE ] ) ) { 218 //if the widget has to be visible here, but the current language has not been checked, return false 219 return false; 220 } 221 222 return $instance; 223 } 224 225 226 227 public function sidebars_widgets( $sidebars ) { 228 229 if ( is_admin() ) { 230 return $sidebars; 231 } 232 233 global $ah_wp_registered_widgets; 234 235 foreach ( $sidebars as $s => $sidebar ) { 236 if ( $s == 'wp_inactive_widgets' || strpos( $s, 'orphaned_widgets' ) === 0 || empty( $sidebar ) ) { 237 continue; 238 } 239 240 241 foreach ( $sidebar as $w => $widget ) { 242 // $widget is the id of the widget 243 if ( ! isset( $ah_wp_registered_widgets[ $widget ] ) ) { 244 continue; 245 } 246 247 248 if ( isset( $this->checked[ $widget ] ) ) { 249 250 $show = $this->checked[ $widget ]; 251 252 } else { 253 254 $opts = $ah_wp_registered_widgets[ $widget ]; 255 $id_base = is_array( $opts['callback'] ) ? $opts['callback'][0]->id_base : $opts['callback']; 256 257 if ( ! $id_base ) { 258 continue; 259 } 260 261 $instance = get_option( 'widget_' . $id_base ); 262 263 if ( ! $instance || ! is_array( $instance ) ) { 264 continue; 265 } 266 267 if ( isset( $instance['_multiwidget'] ) && $instance['_multiwidget'] ) { 268 269 $number = $opts['params'][0]['number']; 270 271 if ( ! isset( $instance[ $number ] ) ) { 272 continue; 273 } 274 275 $instance = $instance[ $number ]; 276 unset( $number ); 277 } 278 279 unset( $opts ); 280 281 $show = self::show_widget( $instance ); 282 283 $this->checked[ $widget ] = $show ? true : false; 284 } 285 286 if ( ! $show ) { 287 unset( $sidebars[ $s ][ $w ] ); 288 } 289 290 unset( $widget ); 291 } 292 unset( $sidebar ); 293 } 294 295 return $sidebars; 296 } 297 298 299 300 public function hidden_widget_options( $widget, $return, $instance ) { 301 302 if ( $_POST && isset( $_POST['id_base'] ) && $_POST['id_base'] == $widget->id_base ) { 303 // widget was just saved so it's open 304 self::show_hide_widget_options( $widget, $return, $instance ); 305 return; 306 } 307 308 self::register_globals(); 309 310 $instance['ah_dw_include'] = isset( $instance['ah_dw_include'] ) ? $instance['ah_dw_include'] : 0; 311 $instance['ah_dw_logged'] = self::show_logged( $instance ); 312 $instance['other_ids'] = isset( $instance['other_ids'] ) ? $instance['other_ids'] : ''; 313 ?> 314 <div class="dw_opts"> 315 <input type="hidden" name="<?php echo esc_attr( $widget->get_field_name('ah_dw_include') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('ah_dw_include') ); ?>" value="<?php echo esc_attr( $instance['ah_dw_include'] ) ?>" /> 316 <input type="hidden" id="<?php echo esc_attr( $widget->get_field_id('ah_dw_logged') ); ?>" name="<?php echo esc_attr( $widget->get_field_name('ah_dw_logged') ); ?>" value="<?php echo esc_attr( $instance['ah_dw_logged'] ) ?>" /> 317 318 <?php 319 foreach ( $instance as $k => $v ) { 320 if ( ! $v ) { 321 continue; 322 } 323 324 if ( strpos( $k, 'page-' ) === 0 || strpos( $k, 'type-' ) === 0 || strpos( $k, 'cat-' ) === 0 || strpos( $k, 'tax-' ) === 0 || strpos( $k, 'lang-' ) === 0 ) { 325 ?> 326 <input type="hidden" id="<?php echo esc_attr( $widget->get_field_id( $k ) ); ?>" name="<?php echo esc_attr( $widget->get_field_name( $k ) ); ?>" value="<?php echo esc_attr( $v ) ?>" /> 327 <?php 328 } 329 } ?> 330 331 <input type="hidden" name="<?php echo esc_attr( $widget->get_field_name('other_ids') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('other_ids') ); ?>" value="<?php echo esc_attr( $instance['other_ids'] ) ?>" /> 332 </div> 333 <?php 334 } 335 336 337 338 public function show_widget_options() { 339 $instance = htmlspecialchars_decode( nl2br( stripslashes( $_POST['opts'] ) ) ); 294 295 function show_widget_options() { 296 297 $instance = htmlspecialchars_decode( nl2br( stripslashes( $_POST['opts'] ) ) ); 340 298 $instance = json_decode( $instance, true ); 341 $this->id_base = sanitize_text_field( $_POST['id_base'] );342 $this->number = sanitize_text_field( $_POST['widget_number'] );343 299 $this->id_base = sanitize_text_field( $_POST['id_base'] ); 300 $this->number = sanitize_text_field( $_POST['widget_number'] ); 301 344 302 $new_instance = array(); 345 303 $prefix = 'widget-' . $this->id_base . '[' . $this->number . ']['; … … 353 311 } 354 312 355 356 357 public function show_hide_widget_options( $widget, $return, $instance ) { 358 self::register_globals(); 359 360 $wp_page_types = self::page_types(); 361 362 $instance['ah_dw_include'] = isset( $instance['ah_dw_include'] ) ? $instance['ah_dw_include'] : 0; 363 $instance['ah_dw_logged'] = self::show_logged( $instance ); 364 $instance['other_ids'] = isset( $instance['other_ids'] ) ? $instance['other_ids'] : ''; 365 ?> 366 <p> 367 <label for="<?php echo esc_attr( $widget->get_field_id('ah_dw_include') ); ?>"> 368 <?php _e( 'Show Widget for:', 'display-widgets' ) ?> 369 </label> 370 <select name="<?php echo esc_attr( $widget->get_field_name('ah_dw_logged') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('ah_dw_logged') ); ?>" class="widefat"> 371 <option value=""> 372 <?php _e( 'Everyone', 'display-widgets' ) ?> 373 </option> 374 <option value="out" <?php echo selected( $instance[ 'ah_dw_logged'], 'out' ) ?>> 375 <?php _e( 'Logged-out users', 'display-widgets' ) ?> 376 </option> 377 <option value="in" <?php echo selected( $instance[ 'ah_dw_logged'], 'in' ) ?>> 378 <?php _e( 'Logged-in users', 'display-widgets' ) ?> 379 </option> 380 </select> 381 </p> 382 383 <p> 384 <select name="<?php echo esc_attr( $widget->get_field_name('ah_dw_include') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('ah_dw_include') ); ?>" class="widefat"> 385 <option value="0"> 386 <?php _e( 'Hide on checked pages', 'display-widgets' ) ?> 387 </option> 388 <option value="1" <?php echo selected( $instance[ 'ah_dw_include'], 1 ) ?>> 389 <?php _e( 'Show on checked pages', 'display-widgets' ) ?> 390 </option> 391 </select> 392 </p> 393 394 <div style="height:150px; overflow:auto; border:1px solid #dfdfdf; padding:5px; margin-bottom:5px;"> 395 <h4 class="ah_dw_toggle" style="cursor:pointer;margin-top:0;"><?php _e( 'Miscellaneous', 'display-widgets' ) ?> +/-</h4> 396 <div class="dw_collapse"> 397 <?php 398 foreach ( $wp_page_types as $key => $label ) { 399 $instance['page-'. $key] = isset( $instance[ 'page-' . $key ] ) ? $instance[ 'page-' . $key ] : false; 400 ?> 401 <p> 402 <input class="checkbox" type="checkbox" <?php checked( $instance[ 'page-' . $key ], true ); ?> id=" 403 <?php echo esc_attr( $widget->get_field_id('page-'. $key) ); ?>" name=" 404 <?php echo esc_attr( $widget->get_field_name('page-'. $key) ); ?>" /> 405 <label for="<?php echo esc_attr( $widget->get_field_id('page-'. $key) ); ?>"> 406 <?php echo $label; ?> 407 </label> 408 </p> 409 <?php 410 } ?> 411 </div> 412 413 <h4 class="ah_dw_toggle" style="cursor:pointer;"><?php _e( 'Pages') ?> +/-</h4> 414 <div class="dw_collapse"> 415 <?php 416 foreach ( $this->pages as $page ) { 417 $instance[ 'page-' . $page->ID ] = isset( $instance[ 'page-' . $page->ID ] ) ? $instance[ 'page-' . $page->ID ] : false; 418 } 419 420 // use custom Page Walker to build page list 421 $args = array( 'instance' => $instance, 'widget' => $widget ); 422 $page_list = $this->page_list->walk( $this->pages, 0, $args ); 423 if ( $page_list ) { 424 echo '<ul>' . $page_list . '</ul>'; 425 } 426 ?> 427 </div> 428 429 <?php if ( ! empty( $this->cposts ) ) { ?> 430 <h4 class="ah_dw_toggle" style="cursor:pointer;"><?php _e( 'Custom Post Types', 'display-widgets' ) ?> +/-</h4> 431 <div class="dw_collapse"> 432 <?php 433 foreach ( $this->cposts as $post_key => $custom_post ) { 434 $instance[ 'type-' . $post_key ] = isset( $instance[ 'type-' . $post_key ] ) ? $instance[ 'type-' . $post_key ] : false; 435 ?> 436 <p> 437 <input class="checkbox" type="checkbox" <?php checked( $instance[ 'type-'. $post_key], true ); ?> id=" 438 <?php echo esc_attr( $widget->get_field_id('type-'. $post_key) ); ?>" name=" 439 <?php echo esc_attr( $widget->get_field_name('type-'. $post_key) ); ?>" /> 440 <label for="<?php echo esc_attr( $widget->get_field_id('type-'. $post_key) ); ?>"> 441 <?php echo stripslashes( $custom_post->labels->name ) ?> 442 </label> 443 </p> 444 <?php 445 unset( $post_key, $custom_post ); 446 } ?> 447 </div> 448 449 <h4 class="ah_dw_toggle" style="cursor:pointer;"><?php _e( 'Custom Post Type Archives', 'display-widgets' ) ?> +/-</h4> 450 <div class="dw_collapse"> 451 <?php 452 foreach ( $this->cposts as $post_key => $custom_post ) { 453 if ( ! $custom_post->has_archive ) { 454 // don't give the option if there is no archive page 455 continue; 456 } 457 $instance[ 'type-' . $post_key . '-archive' ] = isset( $instance[ 'type-' . $post_key . '-archive' ] ) ? $instance[ 'type-' . $post_key . '-archive' ] : false; 458 ?> 459 <p> 460 <input class="checkbox" type="checkbox" <?php checked( $instance[ 'type-' . $post_key . '-archive' ], true ); ?> id=" 461 <?php echo esc_attr( $widget->get_field_id( 'type-'. $post_key . '-archive' ) ); ?>" name=" 462 <?php echo esc_attr( $widget->get_field_name( 'type-' . $post_key . '-archive' ) ); ?>" /> 463 <label for="<?php echo esc_attr( $widget->get_field_id( 'type-' . $post_key . '-archive' ) ); ?>"> 464 <?php echo stripslashes( $custom_post->labels->name ) ?> 465 <?php _e( 'Archive', 'display-widgets' ) ?> 466 </label> 467 </p> 468 <?php } ?> 469 </div> 470 <?php } ?> 471 472 <h4 class="ah_dw_toggle" style="cursor:pointer;"><?php _e( 'Categories') ?> +/-</h4> 473 <div class="dw_collapse"> 474 <?php $instance['cat-all'] = isset( $instance['cat-all'] ) ? $instance['cat-all'] : false; ?> 475 <p> 476 <input class="checkbox" type="checkbox" <?php checked( $instance[ 'cat-all'], true ); ?> id=" 477 <?php echo esc_attr( $widget->get_field_id('cat-all') ); ?>" name=" 478 <?php echo esc_attr( $widget->get_field_name('cat-all') ); ?>" /> 479 <label for="<?php echo $widget->get_field_id('cat-all'); ?>"> 480 <?php _e( 'All Categories', 'display-widgets' ); ?> 481 </label> 482 </p> 483 <?php 484 foreach ( $this->cats as $cat ) { 485 $instance[ 'cat-' . $cat->cat_ID ] = isset( $instance[ 'cat-' . $cat->cat_ID ] ) ? $instance[ 'cat-' . $cat->cat_ID] : false; 486 ?> 487 <p> 488 <input class="checkbox" type="checkbox" <?php checked( $instance[ 'cat-'. $cat->cat_ID], true ); ?> id=" 489 <?php echo esc_attr( $widget->get_field_id('cat-'. $cat->cat_ID) ); ?>" name=" 490 <?php echo esc_attr( $widget->get_field_name('cat-'. $cat->cat_ID) ); ?>" /> 491 <label for="<?php echo $widget->get_field_id('cat-'. $cat->cat_ID); ?>"> 492 <?php echo $cat->cat_name ?> 493 </label> 494 </p> 495 <?php 496 unset( $cat ); 497 } 498 ?> 499 </div> 500 501 <?php if ( ! empty( $this->taxes ) ) { ?> 502 <h4 class="ah_dw_toggle" style="cursor:pointer;"><?php _e( 'Taxonomies', 'display-widgets' ) ?> +/-</h4> 503 <div class="dw_collapse"> 504 <?php 505 foreach ( $this->taxes as $tax => $taxname ) { 506 $instance[ 'tax-' . $tax ] = isset( $instance[ 'tax-' . $tax ] ) ? $instance[ 'tax-' . $tax ] : false; 507 ?> 508 <p> 509 <input class="checkbox" type="checkbox" <?php checked( $instance[ 'tax-'. $tax], true ); ?> id=" 510 <?php echo esc_attr( $widget->get_field_id('tax-'. $tax) ); ?>" name=" 511 <?php echo esc_attr( $widget->get_field_name('tax-'. $tax) ); ?>" /> 512 <label for="<?php echo esc_attr( $widget->get_field_id('tax-'. $tax) ); ?>"> 513 <?php echo str_replace( array( '_','-' ), ' ', ucfirst( $taxname ) ) ?> 514 </label> 515 </p> 516 <?php 517 unset( $tax ); 518 } 519 ?> 520 </div> 521 <?php } ?> 522 523 <?php if ( ! empty( $this->langs ) ) { ?> 524 <h4 class="ah_dw_toggle" style="cursor:pointer;"><?php _e( 'Languages', 'display-widgets' ) ?> +/-</h4> 525 <div class="dw_collapse"> 526 <?php 527 foreach ( $this->langs as $lang ) { 528 $key = $lang['language_code']; 529 $instance[ 'lang-' . $key ] = isset( $instance[ 'lang-' . $key ] ) ? $instance[ 'lang-' . $key ] : false; 530 ?> 531 <p> 532 <input class="checkbox" type="checkbox" <?php checked( $instance[ 'lang-' . $key ], true ); ?> id=" 533 <?php echo esc_attr( $widget->get_field_id('lang-'. $key) ); ?>" name=" 534 <?php echo esc_attr( $widget->get_field_name('lang-'. $key) ); ?>" /> 535 <label for="<?php echo esc_attr( $widget->get_field_id('lang-'. $key) ); ?>"> 536 <?php echo $lang['native_name'] ?> 537 </label> 538 </p> 539 540 <?php 541 unset( $lang, $key ); 542 } 543 ?> 544 </div> 545 <?php } ?> 546 547 <p> 548 <label for="<?php echo esc_attr( $widget->get_field_id('other_ids') ); ?>"> 549 <?php _e( 'Comma Separated list of IDs of posts not listed above', 'display-widgets' ) ?>:</label> 550 <input type="text" value="<?php echo esc_attr( $instance['other_ids'] ) ?>" name="<?php echo esc_attr( $widget->get_field_name('other_ids') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('other_ids') ); ?>" /> 551 </p> 552 </div> 553 <?php 313 function show_hide_widget_options( $widget, $return, $instance ) { 314 315 self::register_globals(); 316 317 $wp_page_types = self::page_types(); 318 319 $instance['dw_include'] = isset( $instance['dw_include'] ) ? $instance['dw_include'] : 0; 320 $instance['dw_logged'] = self::show_logged( $instance ); 321 $instance['other_ids'] = isset( $instance['other_ids'] ) ? $instance['other_ids'] : ''; 322 ?> 323 <p> 324 <label for="<?php echo esc_attr( $widget->get_field_id('dw_include') ); ?>"><?php _e( 'Show Widget for:', 'display-widgets' ) ?></label> 325 <select name="<?php echo esc_attr( $widget->get_field_name('dw_logged') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('dw_logged') ); ?>" class="widefat"> 326 <option value=""><?php _e( 'Everyone', 'display-widgets' ) ?></option> 327 <option value="out" <?php echo selected( $instance['dw_logged'], 'out' ) ?>><?php _e( 'Logged-out users', 'display-widgets' ) ?></option> 328 <option value="in" <?php echo selected( $instance['dw_logged'], 'in' ) ?>><?php _e( 'Logged-in users', 'display-widgets' ) ?></option> 329 </select> 330 </p> 331 332 <p> 333 <select name="<?php echo esc_attr( $widget->get_field_name('dw_include') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('dw_include') ); ?>" class="widefat"> 334 <option value="0"><?php _e( 'Hide on checked pages', 'display-widgets' ) ?></option> 335 <option value="1" <?php echo selected( $instance['dw_include'], 1 ) ?>><?php _e( 'Show on checked pages', 'display-widgets' ) ?></option> 336 </select> 337 </p> 338 339 <div style="height:150px; overflow:auto; border:1px solid #dfdfdf; padding:5px; margin-bottom:5px;"> 340 <h4 class="dw_toggle" style="cursor:pointer;margin-top:0;"><?php _e( 'Miscellaneous', 'display-widgets' ) ?> +/-</h4> 341 <div class="dw_collapse"> 342 <?php 343 foreach ( $wp_page_types as $key => $label ) { 344 $instance['page-'. $key] = isset( $instance[ 'page-' . $key ] ) ? $instance[ 'page-' . $key ] : false; 345 ?> 346 <p><input class="checkbox" type="checkbox" <?php checked( $instance[ 'page-' . $key ], true ); ?> id="<?php echo esc_attr( $widget->get_field_id('page-'. $key) ); ?>" name="<?php echo esc_attr( $widget->get_field_name('page-'. $key) ); ?>" /> 347 <label for="<?php echo esc_attr( $widget->get_field_id('page-'. $key) ); ?>"><?php echo $label; ?></label></p> 348 <?php 349 } ?> 350 </div> 351 352 <h4 class="dw_toggle" style="cursor:pointer;"><?php _e( 'Pages') ?> +/-</h4> 353 <div class="dw_collapse"> 354 <?php 355 foreach ( $this->pages as $page ) { 356 $instance[ 'page-' . $page->ID ] = isset( $instance[ 'page-' . $page->ID ] ) ? $instance[ 'page-' . $page->ID ] : false; 357 } 358 359 // use custom Page Walker to build page list 360 $args = array( 'instance' => $instance, 'widget' => $widget ); 361 $page_list = $this->page_list->walk( $this->pages, 0, $args ); 362 if ( $page_list ) { 363 echo '<ul>' . $page_list . '</ul>'; 364 } 365 ?> 366 </div> 367 368 <?php if ( ! empty( $this->cposts ) ) { ?> 369 <h4 class="dw_toggle" style="cursor:pointer;"><?php _e( 'Custom Post Types', 'display-widgets' ) ?> +/-</h4> 370 <div class="dw_collapse"> 371 <?php 372 foreach ( $this->cposts as $post_key => $custom_post ) { 373 $instance[ 'type-' . $post_key ] = isset( $instance[ 'type-' . $post_key ] ) ? $instance[ 'type-' . $post_key ] : false; 374 ?> 375 <p><input class="checkbox" type="checkbox" <?php checked( $instance['type-'. $post_key], true ); ?> id="<?php echo esc_attr( $widget->get_field_id('type-'. $post_key) ); ?>" name="<?php echo esc_attr( $widget->get_field_name('type-'. $post_key) ); ?>" /> 376 <label for="<?php echo esc_attr( $widget->get_field_id('type-'. $post_key) ); ?>"><?php echo stripslashes( $custom_post->labels->name ) ?></label></p> 377 <?php 378 unset( $post_key, $custom_post ); 379 } ?> 380 </div> 381 382 <h4 class="dw_toggle" style="cursor:pointer;"><?php _e( 'Custom Post Type Archives', 'display-widgets' ) ?> +/-</h4> 383 <div class="dw_collapse"> 384 <?php 385 foreach ( $this->cposts as $post_key => $custom_post ) { 386 if ( ! $custom_post->has_archive ) { 387 // don't give the option if there is no archive page 388 continue; 389 } 390 $instance[ 'type-' . $post_key . '-archive' ] = isset( $instance[ 'type-' . $post_key . '-archive' ] ) ? $instance[ 'type-' . $post_key . '-archive' ] : false; 391 ?> 392 <p><input class="checkbox" type="checkbox" <?php checked( $instance[ 'type-' . $post_key . '-archive' ], true ); ?> id="<?php echo esc_attr( $widget->get_field_id( 'type-'. $post_key . '-archive' ) ); ?>" name="<?php echo esc_attr( $widget->get_field_name( 'type-' . $post_key . '-archive' ) ); ?>" /> 393 <label for="<?php echo esc_attr( $widget->get_field_id( 'type-' . $post_key . '-archive' ) ); ?>"><?php echo stripslashes( $custom_post->labels->name ) ?> <?php _e( 'Archive', 'display-widgets' ) ?></label></p> 394 <?php } ?> 395 </div> 396 <?php } ?> 397 398 <h4 class="dw_toggle" style="cursor:pointer;"><?php _e( 'Categories') ?> +/-</h4> 399 <div class="dw_collapse"> 400 <?php $instance['cat-all'] = isset( $instance['cat-all'] ) ? $instance['cat-all'] : false; ?> 401 <p><input class="checkbox" type="checkbox" <?php checked( $instance['cat-all'], true ); ?> id="<?php echo esc_attr( $widget->get_field_id('cat-all') ); ?>" name="<?php echo esc_attr( $widget->get_field_name('cat-all') ); ?>" /> 402 <label for="<?php echo $widget->get_field_id('cat-all'); ?>"><?php _e( 'All Categories', 'display-widgets' ); ?></label></p> 403 <?php 404 foreach ( $this->cats as $cat ) { 405 $instance[ 'cat-' . $cat->cat_ID ] = isset( $instance[ 'cat-' . $cat->cat_ID ] ) ? $instance[ 'cat-' . $cat->cat_ID] : false; 406 ?> 407 <p><input class="checkbox" type="checkbox" <?php checked( $instance['cat-'. $cat->cat_ID], true ); ?> id="<?php echo esc_attr( $widget->get_field_id('cat-'. $cat->cat_ID) ); ?>" name="<?php echo esc_attr( $widget->get_field_name('cat-'. $cat->cat_ID) ); ?>" /> 408 <label for="<?php echo $widget->get_field_id('cat-'. $cat->cat_ID); ?>"><?php echo $cat->cat_name ?></label></p> 409 <?php 410 unset( $cat ); 411 } 412 ?> 413 </div> 414 415 <?php if ( ! empty( $this->taxes ) ) { ?> 416 <h4 class="dw_toggle" style="cursor:pointer;"><?php _e( 'Taxonomies', 'display-widgets' ) ?> +/-</h4> 417 <div class="dw_collapse"> 418 <?php 419 foreach ( $this->taxes as $tax => $taxname ) { 420 $instance[ 'tax-' . $tax ] = isset( $instance[ 'tax-' . $tax ] ) ? $instance[ 'tax-' . $tax ] : false; 421 ?> 422 <p><input class="checkbox" type="checkbox" <?php checked( $instance['tax-'. $tax], true ); ?> id="<?php echo esc_attr( $widget->get_field_id('tax-'. $tax) ); ?>" name="<?php echo esc_attr( $widget->get_field_name('tax-'. $tax) ); ?>" /> 423 <label for="<?php echo esc_attr( $widget->get_field_id('tax-'. $tax) ); ?>"><?php echo str_replace( array( '_','-' ), ' ', ucfirst( $taxname ) ) ?></label></p> 424 <?php 425 unset( $tax ); 426 } 427 ?> 428 </div> 429 <?php } ?> 430 431 <?php if ( ! empty( $this->langs ) ) { ?> 432 <h4 class="dw_toggle" style="cursor:pointer;"><?php _e( 'Languages', 'display-widgets' ) ?> +/-</h4> 433 <div class="dw_collapse"> 434 <?php 435 foreach ( $this->langs as $lang ) { 436 $key = $lang['language_code']; 437 $instance[ 'lang-' . $key ] = isset( $instance[ 'lang-' . $key ] ) ? $instance[ 'lang-' . $key ] : false; 438 ?> 439 <p><input class="checkbox" type="checkbox" <?php checked( $instance[ 'lang-' . $key ], true ); ?> id="<?php echo esc_attr( $widget->get_field_id('lang-'. $key) ); ?>" name="<?php echo esc_attr( $widget->get_field_name('lang-'. $key) ); ?>" /> 440 <label for="<?php echo esc_attr( $widget->get_field_id('lang-'. $key) ); ?>"><?php echo $lang['native_name'] ?></label></p> 441 442 <?php 443 unset( $lang, $key ); 444 } 445 ?> 446 </div> 447 <?php } ?> 448 449 <p><label for="<?php echo esc_attr( $widget->get_field_id('other_ids') ); ?>"><?php _e( 'Comma Separated list of IDs of posts not listed above', 'display-widgets' ) ?>:</label> 450 <input type="text" value="<?php echo esc_attr( $instance['other_ids'] ) ?>" name="<?php echo esc_attr( $widget->get_field_name('other_ids') ); ?>" id="<?php echo esc_attr( $widget->get_field_id('other_ids') ); ?>" /> 451 </p> 452 </div> 453 <?php 554 454 } 555 455 556 public function update_widget_options( $instance, $new_instance, $old_instance ) { 557 self::register_globals(); 558 559 if ( ! empty( $this->pages ) ) { 560 foreach ( $this->pages as $page ) { 561 if ( isset( $new_instance[ 'page-' . $page->ID ] ) ) { 562 $instance[ 'page-' . $page->ID ] = 1; 563 } else if ( isset( $instance[ 'page-' . $page->ID ] ) ) { 564 unset( $instance[ 'page-' . $page->ID ] ); 565 } 566 unset( $page ); 567 } 568 } 569 570 if ( isset( $new_instance['cat-all'] ) ) { 571 $instance['cat-all'] = 1; 572 573 foreach ( $this->cats as $cat ) { 574 if ( isset( $new_instance[ 'cat-' . $cat->cat_ID ] ) ) { 575 unset( $instance['cat-' . $cat->cat_ID ] ); 576 } 577 } 578 } else { 579 unset( $instance['cat-all'] ); 580 581 foreach ( $this->cats as $cat ) { 582 if ( isset( $new_instance[ 'cat-' . $cat->cat_ID ] ) ) { 583 $instance[ 'cat-' . $cat->cat_ID ] = 1; 584 } else if ( isset( $instance[ 'cat-' . $cat->cat_ID ] ) ) { 585 unset( $instance['cat-' . $cat->cat_ID ] ); 586 } 587 unset( $cat ); 588 } 589 } 590 591 if ( ! empty( $this->cposts ) ) { 592 foreach ( $this->cposts as $post_key => $custom_post ) { 593 if ( isset( $new_instance[ 'type-' . $post_key ] ) ) { 594 $instance['type-'. $post_key] = 1; 595 } else if ( isset( $instance['type-' . $post_key ] ) ) { 596 unset( $instance[ 'type-' . $post_key ] ); 597 } 598 599 if ( isset( $new_instance['type-' . $post_key . '-archive' ] ) ) { 600 $instance[ 'type-' . $post_key . '-archive' ] = 1; 601 } else if ( isset( $instance[ 'type-' . $post_key . '-archive' ] ) ) { 602 unset( $instance[ 'type-' . $post_key . '-archive' ] ); 603 } 604 605 unset( $custom_post ); 606 } 607 } 608 609 if ( ! empty( $this->taxes ) ) { 610 foreach ( $this->taxes as $tax => $taxname ) { 611 if ( isset( $new_instance[ 'tax-' . $tax ] ) ) { 612 $instance['tax-'. $tax] = 1; 613 } else if ( isset( $instance[ 'tax-' . $tax ] ) ) { 614 unset( $instance[ 'tax-' . $tax ] ); 615 } 616 unset( $tax ); 617 } 618 } 619 620 if ( ! empty( $this->langs ) ) { 621 foreach ( $this->langs as $lang ) { 622 if ( isset( $new_instance[ 'lang-' . $lang['language_code'] ] ) ) { 623 $instance[ 'lang-' . $lang['language_code'] ] = 1; 624 } else if ( isset( $instance[ 'lang-'. $lang['language_code'] ] ) ) { 625 unset( $instance[ 'lang-' . $lang['language_code'] ] ) ; 626 } 627 unset( $lang ); 628 } 629 } 630 631 $instance['ah_dw_include'] = ( isset( $new_instance['ah_dw_include'] ) && $new_instance['ah_dw_include'] ) ? 1 : 0; 632 $instance['ah_dw_logged'] = ( isset( $new_instance['ah_dw_logged'] ) && $new_instance['ah_dw_logged'] ) ? $new_instance['ah_dw_logged'] : ''; 633 $instance['other_ids'] = ( isset( $new_instance['other_ids'] ) && $new_instance['other_ids'] ) ? $new_instance['other_ids'] : ''; 634 635 $page_types = self::page_types(); 636 foreach ( array_keys( $page_types ) as $page ) { 637 if ( isset( $new_instance[ 'page-'. $page ] ) ) { 638 $instance[ 'page-' . $page ] = 1; 639 } else if ( isset( $instance['page-' . $page ] ) ) { 640 unset( $instance[ 'page-' . $page ] ); 641 } 642 } 643 unset( $page_types ); 644 645 return $instance; 646 } 647 648 649 public function get_field_name( $field_name ) { 650 return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']'; 651 } 652 653 654 655 public function get_field_id( $field_name ) { 656 return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name; 657 } 658 659 660 661 public function load_js() { 456 function update_widget_options( $instance, $new_instance, $old_instance ) { 457 self::register_globals(); 458 459 if ( ! empty( $this->pages ) ) { 460 foreach ( $this->pages as $page ) { 461 if ( isset( $new_instance[ 'page-' . $page->ID ] ) ) { 462 $instance[ 'page-' . $page->ID ] = 1; 463 } else if ( isset( $instance[ 'page-' . $page->ID ] ) ) { 464 unset( $instance[ 'page-' . $page->ID ] ); 465 } 466 unset( $page ); 467 } 468 } 469 470 if ( isset( $new_instance['cat-all'] ) ) { 471 $instance['cat-all'] = 1; 472 473 foreach ( $this->cats as $cat ) { 474 if ( isset( $new_instance[ 'cat-' . $cat->cat_ID ] ) ) { 475 unset( $instance['cat-' . $cat->cat_ID ] ); 476 } 477 } 478 } else { 479 unset( $instance['cat-all'] ); 480 481 foreach ( $this->cats as $cat ) { 482 if ( isset( $new_instance[ 'cat-' . $cat->cat_ID ] ) ) { 483 $instance[ 'cat-' . $cat->cat_ID ] = 1; 484 } else if ( isset( $instance[ 'cat-' . $cat->cat_ID ] ) ) { 485 unset( $instance['cat-' . $cat->cat_ID ] ); 486 } 487 unset( $cat ); 488 } 489 } 490 491 if ( ! empty( $this->cposts ) ) { 492 foreach ( $this->cposts as $post_key => $custom_post ) { 493 if ( isset( $new_instance[ 'type-' . $post_key ] ) ) { 494 $instance['type-'. $post_key] = 1; 495 } else if ( isset( $instance['type-' . $post_key ] ) ) { 496 unset( $instance[ 'type-' . $post_key ] ); 497 } 498 499 if ( isset( $new_instance['type-' . $post_key . '-archive' ] ) ) { 500 $instance[ 'type-' . $post_key . '-archive' ] = 1; 501 } else if ( isset( $instance[ 'type-' . $post_key . '-archive' ] ) ) { 502 unset( $instance[ 'type-' . $post_key . '-archive' ] ); 503 } 504 505 unset( $custom_post ); 506 } 507 } 508 509 if ( ! empty( $this->taxes ) ) { 510 foreach ( $this->taxes as $tax => $taxname ) { 511 if ( isset( $new_instance[ 'tax-' . $tax ] ) ) { 512 $instance['tax-'. $tax] = 1; 513 } else if ( isset( $instance[ 'tax-' . $tax ] ) ) { 514 unset( $instance[ 'tax-' . $tax ] ); 515 } 516 unset( $tax ); 517 } 518 } 519 520 if ( ! empty( $this->langs ) ) { 521 foreach ( $this->langs as $lang ) { 522 if ( isset( $new_instance[ 'lang-' . $lang['language_code'] ] ) ) { 523 $instance[ 'lang-' . $lang['language_code'] ] = 1; 524 } else if ( isset( $instance[ 'lang-'. $lang['language_code'] ] ) ) { 525 unset( $instance[ 'lang-' . $lang['language_code'] ] ) ; 526 } 527 unset( $lang ); 528 } 529 } 530 531 $instance['dw_include'] = ( isset( $new_instance['dw_include'] ) && $new_instance['dw_include'] ) ? 1 : 0; 532 $instance['dw_logged'] = ( isset( $new_instance['dw_logged'] ) && $new_instance['dw_logged'] ) ? $new_instance['dw_logged'] : ''; 533 $instance['other_ids'] = ( isset( $new_instance['other_ids'] ) && $new_instance['other_ids'] ) ? $new_instance['other_ids'] : ''; 534 535 $page_types = self::page_types(); 536 foreach ( array_keys( $page_types ) as $page ) { 537 if ( isset( $new_instance[ 'page-'. $page ] ) ) { 538 $instance[ 'page-' . $page ] = 1; 539 } else if ( isset( $instance['page-' . $page ] ) ) { 540 unset( $instance[ 'page-' . $page ] ); 541 } 542 } 543 unset( $page_types ); 544 545 return $instance; 546 } 547 548 function get_field_name( $field_name ) { 549 return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']'; 550 } 551 552 function get_field_id( $field_name ) { 553 return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name; 554 } 555 556 function load_js() { 662 557 global $pagenow; 663 558 664 559 if ( $pagenow != 'widgets.php' ) { 665 560 //only load the js on the widgets page … … 667 562 } 668 563 ?> 669 <script type="text/javascript"> 670 /*<![CDATA[*/ 671 jQuery(document).ready(function($) { 672 $('.widgets-holder-wrap').on('click', '.ah_dw_toggle', ah_dw_toggle); 673 }); 674 jQuery(document.body).bind('click.widgets-toggle', dw_show_opts); 675 676 function dw_show_opts(e) { 677 var target = jQuery(e.target); 678 var widget = target.closest('div.widget'); 679 var inside = widget.children('.widget-inside'); 680 var opts = inside.find('.dw_opts'); 681 if (opts.length == 0) { 682 return; 683 } 684 685 inside.find('.spinner').show(); 686 687 jQuery.ajax({ 688 type: 'POST', 689 url: ajaxurl, 690 data: { 691 'action': 'dw_show_widget', 692 'opts': JSON.stringify(opts.children('input').serializeArray()), 693 'id_base': inside.find('input.id_base').val(), 694 'widget_number': (inside.find('input.multi_number').val() == '') ? inside.find('input.widget_number').val() : inside.find('input.multi_number').val() 695 }, 696 success: function(html) { 697 opts.replaceWith(html); 698 inside.find('.spinner').hide(); 699 } 700 }); 701 } 702 703 function ah_dw_toggle() { 704 jQuery(this).next('.dw_collapse').toggle(); 705 } 706 /*]]>*/ 707 708 </script> 709 <?php 564 <script type="text/javascript"> 565 /*<![CDATA[*/ 566 jQuery(document).ready(function($){ 567 $('.widgets-holder-wrap').on('click', '.dw_toggle', dw_toggle); 568 }); 569 jQuery(document.body).bind('click.widgets-toggle', dw_show_opts); 570 function dw_show_opts(e){ 571 var target = jQuery(e.target); 572 var widget = target.closest('div.widget'); 573 var inside = widget.children('.widget-inside'); 574 var opts = inside.find('.dw_opts'); 575 if(opts.length == 0){ 576 return; 577 } 578 579 inside.find('.spinner').show(); 580 581 jQuery.ajax({ 582 type:'POST',url:ajaxurl, 583 data:{ 584 'action':'dw_show_widget', 585 'opts':JSON.stringify(opts.children('input').serializeArray()), 586 'id_base':inside.find('input.id_base').val(), 587 'widget_number':(inside.find('input.multi_number').val() == '') ? inside.find('input.widget_number').val() : inside.find('input.multi_number').val() 588 }, 589 success:function(html){ opts.replaceWith(html); inside.find('.spinner').hide(); } 590 }); 591 } 592 function dw_toggle(){jQuery(this).next('.dw_collapse').toggle();} 593 /*]]>*/ 594 </script> 595 <?php 710 596 } 711 712 713 public function show_logged( $instance ) { 714 if ( isset( $instance['ah_dw_logged'] ) ) { 715 return $instance['ah_dw_logged']; 597 598 function show_logged( $instance ) { 599 if ( isset( $instance['dw_logged'] ) ) { 600 return $instance['dw_logged']; 716 601 } 717 602 718 603 if ( isset( $instance['dw_logout'] ) && $instance['dw_logout'] ) { 719 $instance[' ah_dw_logged'] = 'out';604 $instance['dw_logged'] = 'out'; 720 605 } else if ( isset( $instance['dw_login'] ) && $instance['dw_login'] ) { 721 $instance[' ah_dw_logged'] = 'in';606 $instance['dw_logged'] = 'in'; 722 607 } else { 723 $instance[' ah_dw_logged'] = '';608 $instance['dw_logged'] = ''; 724 609 } 725 726 return $instance[' ah_dw_logged'];610 611 return $instance['dw_logged']; 727 612 } 728 729 730 731 public function page_types(){ 613 614 function page_types(){ 732 615 $page_types = array( 733 616 'front' => __( 'Front', 'display-widgets' ), … … 738 621 'search' => __( 'Search'), 739 622 ); 740 741 return apply_filters(' ah_dw_pages_types_register', $page_types);623 624 return apply_filters('dw_pages_types_register', $page_types); 742 625 } 743 626 744 745 746 public function register_globals(){ 747 if ( ! empty( $this->checked ) ) { 748 return; 749 } 750 751 $saved_details = get_transient( $this->transient_name ); 752 if ( $saved_details ) { 753 foreach ( $saved_details as $k => $d ) { 754 if ( empty( $this->{$k} ) ) { 755 $this->{$k} = $d; 756 } 757 758 unset( $k, $d ); 759 } 760 } 761 762 if ( empty( $this->pages ) ) { 763 $this->pages = get_posts( array( 764 'post_type' => 'page', 'post_status' => 'publish', 765 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC', 766 'fields' => array('ID', 'name'), 767 )); 768 } 769 770 if ( empty( $this->cats ) ) { 771 $this->cats = get_categories( array( 772 'hide_empty' => false, 773 //'fields' => 'id=>name', //added in 3.8 774 ) ); 775 } 776 777 if ( empty( $this->cposts ) ) { 778 $this->cposts = get_post_types( array( 779 'public' => true, 780 ), 'object'); 781 782 foreach ( array( 'revision', 'attachment', 'nav_menu_item' ) as $unset ) { 783 unset( $this->cposts[ $unset ] ); 784 } 785 786 foreach ( $this->cposts as $c => $type ) { 787 $post_taxes = get_object_taxonomies( $c ); 788 foreach ( $post_taxes as $post_tax) { 789 if ( in_array( $post_tax, array( 'category', 'post_format' ) ) ) { 790 continue; 791 } 792 793 $taxonomy = get_taxonomy( $post_tax ); 794 $name = $post_tax; 795 796 if ( isset( $taxonomy->labels->name ) && ! empty( $taxonomy->labels->name ) ) { 797 $name = $taxonomy->labels->name; 798 } 799 800 $this->taxes[ $post_tax ] = $name; 801 } 802 } 803 } 804 805 if ( empty( $this->langs ) && function_exists('icl_get_languages') ) { 806 $this->langs = icl_get_languages('skip_missing=0&orderby=code'); 807 } 808 809 // save for one week 810 set_transient( $this->transient_name, array( 811 'pages' => $this->pages, 812 'cats' => $this->cats, 813 'cposts' => $this->cposts, 814 'taxes' => $this->taxes, 815 ), 60*60*24*7 ); 816 817 if ( empty( $this->checked ) ) { 818 $this->checked[] = true; 819 } 820 } 821 822 823 824 public function delete_transient() { 825 delete_transient( $this->transient_name ); 826 } 827 828 829 830 public function load_lang(){ 831 load_plugin_textdomain( 'display-widgets', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); 832 } 833 834 835 836 /* WPML support */ 837 function get_lang_id( $id, $type = 'page' ) { 838 if ( function_exists('icl_object_id') ) { 839 $id = icl_object_id( $id, $type, true ); 840 } 841 842 return $id; 843 } 627 function register_globals(){ 628 if ( ! empty( $this->checked ) ) { 629 return; 630 } 631 632 $saved_details = get_transient( $this->transient_name ); 633 if ( $saved_details ) { 634 foreach ( $saved_details as $k => $d ) { 635 if ( empty( $this->{$k} ) ) { 636 $this->{$k} = $d; 637 } 638 639 unset( $k, $d ); 640 } 641 } 642 643 if ( empty( $this->pages ) ) { 644 $this->pages = get_posts( array( 645 'post_type' => 'page', 'post_status' => 'publish', 646 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC', 647 'fields' => array('ID', 'name'), 648 )); 649 } 650 651 if ( empty( $this->cats ) ) { 652 $this->cats = get_categories( array( 653 'hide_empty' => false, 654 //'fields' => 'id=>name', //added in 3.8 655 ) ); 656 } 657 658 if ( empty( $this->cposts ) ) { 659 $this->cposts = get_post_types( array( 660 'public' => true, 661 ), 'object'); 662 663 foreach ( array( 'revision', 'attachment', 'nav_menu_item' ) as $unset ) { 664 unset( $this->cposts[ $unset ] ); 665 } 666 667 foreach ( $this->cposts as $c => $type ) { 668 $post_taxes = get_object_taxonomies( $c ); 669 foreach ( $post_taxes as $post_tax) { 670 if ( in_array( $post_tax, array( 'category', 'post_format' ) ) ) { 671 continue; 672 } 673 674 $taxonomy = get_taxonomy( $post_tax ); 675 $name = $post_tax; 676 677 if ( isset( $taxonomy->labels->name ) && ! empty( $taxonomy->labels->name ) ) { 678 $name = $taxonomy->labels->name; 679 } 680 681 $this->taxes[ $post_tax ] = $name; 682 } 683 } 684 } 685 686 if ( empty( $this->langs ) && function_exists('icl_get_languages') ) { 687 $this->langs = icl_get_languages('skip_missing=0&orderby=code'); 688 } 689 690 // save for one week 691 set_transient( $this->transient_name, array( 692 'pages' => $this->pages, 693 'cats' => $this->cats, 694 'cposts' => $this->cposts, 695 'taxes' => $this->taxes, 696 ), 60*60*24*7 ); 697 698 if ( empty( $this->checked ) ) { 699 $this->checked[] = true; 700 } 701 } 702 703 function delete_transient() { 704 delete_transient( $this->transient_name ); 705 } 706 707 function load_lang(){ 708 load_plugin_textdomain( 'display-widgets', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); 709 } 710 711 /* WPML support */ 712 function get_lang_id( $id, $type = 'page' ) { 713 if ( function_exists('icl_object_id') ) { 714 $id = icl_object_id( $id, $type, true ); 715 } 716 717 return $id; 718 } 844 719 845 720 } … … 850 725 class AH_DW_Walker_Page_List extends Walker_Page { 851 726 852 853 public function start_lvl( &$output, $depth = 0, $args = array() ) { 854 $output .= "\n<ul class='children'>\n"; 855 } 856 857 858 859 public function end_lvl( &$output, $depth = 0, $args = array() ) { 860 $output .= "</ul>\n"; 861 } 862 863 864 865 function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) { 866 if ( $depth ) 867 $indent = str_repeat("— ", $depth); 868 else 869 $indent = ''; 870 871 // args: $instance, $widget 872 extract( $args, EXTR_SKIP ); 873 874 875 if ( '' === $page->post_title ) { 876 $page->post_title = sprintf( __( '#%d (no title)', 'display-widgets' ), $page->ID ); 877 } 878 879 $output .= '<li>' . $indent; 880 $output .= '<input class="checkbox" type="checkbox" ' . checked( $instance[ 'page-' . $page->ID ], true, false ) . ' id="' . esc_attr( $widget->get_field_id('page-'. $page->ID) ) . '" name="' . esc_attr( $widget->get_field_name('page-'. $page->ID) ) .'" />'; 881 882 $output .= '<label for="' . esc_attr( $widget->get_field_id('page-'. $page->ID) ) . '">' . apply_filters( 'the_title', $page->post_title, $page->ID ) . '</label>'; 883 } 884 885 function end_el( &$output, $page, $depth = 0, $args = array() ) { 886 $output .= "</li>\n"; 887 } 727 function start_lvl( &$output, $depth = 0, $args = array() ) { 728 $output .= "\n<ul class='children'>\n"; 729 } 730 731 function end_lvl( &$output, $depth = 0, $args = array() ) { 732 $output .= "</ul>\n"; 733 } 734 735 function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) { 736 if ( $depth ) 737 $indent = str_repeat("— ", $depth); 738 else 739 $indent = ''; 740 741 // args: $instance, $widget 742 extract( $args, EXTR_SKIP ); 743 744 745 if ( '' === $page->post_title ) { 746 $page->post_title = sprintf( __( '#%d (no title)', 'display-widgets' ), $page->ID ); 747 } 748 749 $output .= '<li>' . $indent; 750 $output .= '<input class="checkbox" type="checkbox" ' . checked( $instance[ 'page-' . $page->ID ], true, false ) . ' id="' . esc_attr( $widget->get_field_id('page-'. $page->ID) ) . '" name="' . esc_attr( $widget->get_field_name('page-'. $page->ID) ) .'" />'; 751 752 $output .= '<label for="' . esc_attr( $widget->get_field_id('page-'. $page->ID) ) . '">' . apply_filters( 'the_title', $page->post_title, $page->ID ) . '</label>'; 753 } 754 755 function end_el( &$output, $page, $depth = 0, $args = array() ) { 756 $output .= "</li>\n"; 757 } 888 758 889 759 } … … 894 764 custom Page Walker CSS 895 765 */ 896 function ah_dw_widgets_style() {897 echo '<style>';898 // use next line for normal indent instead of &mdash:899 // echo '.dw_collapse ul ul { padding-left: 1.5em; }';900 echo '.dw_collapse li { line-height: 1.5em; margin: 1em 0; }';901 echo '</style>';766 function dw_widgets_style() { 767 echo '<style>'; 768 // use next line for normal indent instead of &mdash: 769 // echo '.dw_collapse ul ul { padding-left: 1.5em; }'; 770 echo '.dw_collapse li { line-height: 1.5em; margin: 1em 0; }'; 771 echo '</style>'; 902 772 } 903 add_action( 'admin_print_styles-widgets.php', ' ah_dw_widgets_style' );773 add_action( 'admin_print_styles-widgets.php', 'dw_widgets_style' );
Note: See TracChangeset
for help on using the changeset viewer.