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-visibility.php
1020 lines
| 1 | <?php |
| 2 | |
| 3 | add_action( 'cs_init', array( 'CustomSidebarsVisibility', 'instance' ) ); |
| 4 | |
| 5 | /** |
| 6 | * Adds visibility options to all widgets: Hide or show widgets only when |
| 7 | * specific conditions are met. |
| 8 | * |
| 9 | * @since 2.0 |
| 10 | */ |
| 11 | class CustomSidebarsVisibility extends CustomSidebars { |
| 12 | |
| 13 | public static $filtered_tax_list = false; |
| 14 | /** |
| 15 | * Returns the singleton object. |
| 16 | * |
| 17 | * @since 2.0 |
| 18 | */ |
| 19 | public static function instance() { |
| 20 | static $Inst = null; |
| 21 | |
| 22 | if ( null === $Inst ) { |
| 23 | $Inst = new CustomSidebarsVisibility(); |
| 24 | } |
| 25 | |
| 26 | return $Inst; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Constructor is private -> singleton. |
| 31 | * |
| 32 | * @since 2.0 |
| 33 | */ |
| 34 | private function __construct() { |
| 35 | if ( is_admin() ) { |
| 36 | // in_widget_form: Add our button inside each widget. |
| 37 | add_action( |
| 38 | 'in_widget_form', |
| 39 | array( $this, 'admin_widget_button' ), |
| 40 | 10, 3 |
| 41 | ); |
| 42 | |
| 43 | // When the widget is saved (via Ajax) we save our options. |
| 44 | add_filter( |
| 45 | 'widget_update_callback', |
| 46 | array( $this, 'admin_widget_update' ), |
| 47 | 10, 3 |
| 48 | ); |
| 49 | |
| 50 | $url = 'widgets.php'; |
| 51 | if ( isset( $_SERVER['SCRIPT_NAME'] ) ) { |
| 52 | $url = explode( '/', $_SERVER['SCRIPT_NAME'] ); |
| 53 | $url = array_pop( $url ); |
| 54 | } |
| 55 | lib3()->ui->add( CSB_JS_URL . 'cs-visibility.min.js', $url ); |
| 56 | lib3()->ui->add( CSB_CSS_URL . 'cs-visibility.css', $url ); |
| 57 | |
| 58 | // Custom Sidebars Ajax request. |
| 59 | add_action( |
| 60 | 'cs_ajax_request_get', |
| 61 | array( $this, 'handle_ajax' ) |
| 62 | ); |
| 63 | } else { |
| 64 | // Filters the list of widget-areas and their widgets |
| 65 | add_filter( |
| 66 | 'sidebars_widgets', |
| 67 | array( $this, 'sidebars_widgets' ) |
| 68 | ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Extracts and sanitizes the CSB visibility data from the widget instance. |
| 74 | * |
| 75 | * @since 2.0 |
| 76 | * @param array $instance The widget instance data. |
| 77 | * @return array Sanitized CSB visibility data. |
| 78 | */ |
| 79 | protected function get_widget_data( $instance ) { |
| 80 | static $Condition_keys = null; |
| 81 | $data = array(); |
| 82 | |
| 83 | if ( null === $Condition_keys ) { |
| 84 | $tax_list = get_taxonomies( array( 'public' => true ), 'objects' ); |
| 85 | $type_list = CustomSidebars::get_post_types( 'objects' ); |
| 86 | $Condition_keys = array( |
| 87 | 'guest' => array(), |
| 88 | 'date' => array(), |
| 89 | 'roles' => array(), |
| 90 | 'pagetypes' => array(), |
| 91 | 'posttypes' => array(), |
| 92 | 'membership' => array(), |
| 93 | 'membership2' => array(), |
| 94 | 'prosite' => array(), |
| 95 | ); |
| 96 | foreach ( $type_list as $type_item ) { |
| 97 | $Condition_keys[ 'pt-' . $type_item->name ] = array(); |
| 98 | } |
| 99 | foreach ( $tax_list as $tax_item ) { |
| 100 | $Condition_keys[ 'tax-' . $tax_item->name ] = array(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if ( isset( $instance['csb_visibility'] ) ) { |
| 105 | $data = $instance['csb_visibility']; |
| 106 | } |
| 107 | |
| 108 | $valid_action = array( 'show', 'hide' ); |
| 109 | if ( ! isset( $data['action'] ) || ! in_array( $data['action'], $valid_action ) ) { |
| 110 | $data['action'] = reset( $valid_action ); |
| 111 | } |
| 112 | |
| 113 | $conditions = isset( $data['conditions'] )? $data['conditions'] : array(); |
| 114 | if ( ! is_array( $conditions ) ) { |
| 115 | $conditions = array(); |
| 116 | } |
| 117 | $data['conditions'] = array(); |
| 118 | |
| 119 | $data['always'] = true; |
| 120 | foreach ( $Condition_keys as $key => $def_value ) { |
| 121 | $val = $def_value; |
| 122 | if ( isset( $conditions[ $key ] ) && ! empty( $conditions[ $key ] ) ) { |
| 123 | $data['always'] = false; |
| 124 | $val = $conditions[ $key ]; |
| 125 | } |
| 126 | $data['conditions'][ $key ] = $val; |
| 127 | } |
| 128 | |
| 129 | return $data; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Action handler for 'in_widget_form' |
| 134 | * |
| 135 | * @since 2.0 |
| 136 | */ |
| 137 | public function admin_widget_button( $widget, $return, $instance ) { |
| 138 | static $Loaded = false; |
| 139 | static $tax_list = array(); |
| 140 | static $type_list = array(); |
| 141 | static $role_list = array(); |
| 142 | static $membership_levels = array(); |
| 143 | static $pagetype_list = array(); |
| 144 | |
| 145 | if ( false === $Loaded ) { |
| 146 | $tax_list = get_taxonomies( array( 'public' => true ), 'objects' ); |
| 147 | $type_list = CustomSidebars::get_post_types( 'objects' ); |
| 148 | $role_list = array_reverse( get_editable_roles() ); |
| 149 | $membership_levels = $this->get_membership_levels(); |
| 150 | $membership2_items = $this->get_membership2_items(); |
| 151 | $pagetype_list = array( |
| 152 | 'frontpage' => __( 'aFront Page', 'custom-sidebars' ), |
| 153 | 'home' => __( 'Post Index', 'custom-sidebars' ), |
| 154 | 'single' => __( 'Single page', 'custom-sidebars' ), |
| 155 | //'posts' => __( 'Posts page', 'custom-sidebars' ), "Posts page" is same as "Post Index"... |
| 156 | 'archive' => __( 'Archives', 'custom-sidebars' ), |
| 157 | 'search' => __( 'Search results', 'custom-sidebars' ), |
| 158 | 'e404' => __( 'Not found (404)', 'custom-sidebars' ), |
| 159 | 'preview' => __( 'Preview', 'custom-sidebars' ), |
| 160 | 'day' => __( 'Archive: Day', 'custom-sidebars' ), |
| 161 | 'month' => __( 'Archive: Month', 'custom-sidebars' ), |
| 162 | 'year' => __( 'Archive: Year', 'custom-sidebars' ), |
| 163 | ); |
| 164 | |
| 165 | // Remove taxonomies without values. |
| 166 | if ( ! self::$filtered_tax_list ) { |
| 167 | foreach ( $tax_list as $index => $tax_item ) { |
| 168 | $tags = get_terms( $tax_item->name, array( 'hide_empty' => false ) ); |
| 169 | if ( empty( $tags ) ) { |
| 170 | unset( $tax_list[ $index ] ); |
| 171 | } |
| 172 | } |
| 173 | self::$filtered_tax_list = $tax_list; |
| 174 | } else { |
| 175 | $tax_list = self::$filtered_tax_list; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | $is_visible = ( isset( $_POST['csb_visible'] ) && '1' == $_POST['csb_visible'] ? 1 : 0); |
| 180 | $data = $this->get_widget_data( $instance ); |
| 181 | $action_show = ('show' == $data['action']); |
| 182 | $cond = $data['conditions']; |
| 183 | |
| 184 | ?> |
| 185 | <div class="csb-visibility csb-visibility-<?php echo esc_attr( $widget->id ); ?>" |
| 186 | data-widget="<?php echo esc_attr( $widget->option_name ); ?>" |
| 187 | data-number="<?php echo esc_attr( absint( @$widget->number ) ); ?>"> |
| 188 | <?php |
| 189 | /* |
| 190 | * This input is only used to determine if the "visibility" button |
| 191 | * should be displayed in the widget form. |
| 192 | */ |
| 193 | ?> |
| 194 | <input type="hidden" name="csb-visibility-button" value="0" /> |
| 195 | <?php if ( ! isset( $_POST['csb-visibility-button'] ) ) : ?> |
| 196 | <a href="#" class="button csb-visibility-button"><span class="dashicons dashicons-visibility"></span> <?php _e( 'Visibility', 'custom-sidebars' ); ?></a> |
| 197 | <?php else : ?> |
| 198 | <script>jQuery(function() { jQuery('.csb-visibility-<?php echo esc_js( $widget->id ); ?>').closest('.widget').trigger('csb:update'); }); </script> |
| 199 | <?php endif; ?> |
| 200 | |
| 201 | <div class="csb-visibility-inner" <?php if ( ! $is_visible ) : ?>style="display:none"<?php endif; ?>> |
| 202 | <input type="hidden" name="csb_visible" class="csb-visible-flag" value="<?php echo esc_attr( $is_visible ); ?>" /> |
| 203 | |
| 204 | <div class="csb-option-row csb-action"> |
| 205 | <label for="<?php echo esc_attr( $widget->id ); ?>-action" class="lbl-show-if toggle-action" <?php if ( ! $action_show ) : ?>style="display:none"<?php endif; ?>><?php _e( '<b>Show</b> widget if:', 'custom-sidebars' ); ?></label> |
| 206 | <label for="<?php echo esc_attr( $widget->id ); ?>-action" class="lbl-hide-if toggle-action" <?php if ( $action_show ) : ?>style="display:none"<?php endif; ?>><?php _e( '<b>Hide</b> widget if:', 'custom-sidebars' ); ?></label> |
| 207 | <input type="hidden" id="<?php echo esc_attr( $widget->id ); ?>-action" name="csb_visibility[action]" value="<?php echo esc_attr( $data['action'] ); ?>" /> |
| 208 | <i class="dashicons dashicons-plus choose-filters show-on-hover action"></i> |
| 209 | <ul class="dropdown" style="display:none"> |
| 210 | <li class="csb-group"><?php _e( 'Filters', 'custom-sidebars' ); ?></li> |
| 211 | <li class="add-filter" |
| 212 | data-for=".csb-date" |
| 213 | style="display:none"> |
| 214 | <?php _e( 'Date', 'custom-sidebars' ); ?> |
| 215 | </li> |
| 216 | <li class="add-filter" |
| 217 | data-for=".csb-guest" |
| 218 | <?php if ( ! empty( $cond['guest'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 219 | <?php _e( 'Guests', 'custom-sidebars' ); ?> |
| 220 | </li> |
| 221 | <li class="add-filter" |
| 222 | data-for=".csb-roles" |
| 223 | <?php if ( ! empty( $cond['roles'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 224 | <?php _e( 'Roles', 'custom-sidebars' ); ?> |
| 225 | </li> |
| 226 | <?php if ( false != $membership_levels ) : ?> |
| 227 | <li class="add-filter" |
| 228 | data-for=".csb-membership"> |
| 229 | <?php _e( 'Membership', 'custom-sidebars' ); ?> |
| 230 | </li> |
| 231 | <?php endif; ?> |
| 232 | <?php if ( false != $membership2_items ) : ?> |
| 233 | <li class="add-filter" |
| 234 | data-for=".csb-membership2"> |
| 235 | <?php _e( 'Membership2', 'custom-sidebars' ); ?> |
| 236 | </li> |
| 237 | <?php endif; ?> |
| 238 | <li class="add-filter" |
| 239 | data-for=".csb-pagetypes" |
| 240 | <?php if ( ! empty( $cond['pagetypes'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 241 | <?php _e( 'Special pages', 'custom-sidebars' ); ?> |
| 242 | </li> |
| 243 | <li class="add-filter" |
| 244 | data-for=".csb-posttypes" |
| 245 | <?php if ( ! empty( $cond['posttypes'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 246 | <?php _e( 'For posttype', 'custom-sidebars' ); ?> |
| 247 | </li> |
| 248 | <li class="csb-group"><?php _e( 'Taxonomy', 'custom-sidebars' ); ?></li> |
| 249 | <?php foreach ( $tax_list as $tax_item ) : |
| 250 | $row_id = 'tax-' . $tax_item->name; |
| 251 | ?> |
| 252 | <li class="add-filter" |
| 253 | data-for=".csb-<?php echo esc_attr( $row_id ); ?>" |
| 254 | <?php if ( ! empty( $cond[ $row_id ] ) ) : ?>style="display:none"<?php endif; ?>> |
| 255 | <?php echo esc_html( $tax_item->labels->name ); ?> |
| 256 | </li> |
| 257 | <?php |
| 258 | endforeach; ?> |
| 259 | </ul> |
| 260 | </div> |
| 261 | |
| 262 | <?php $block_name = 'csb_visibility[conditions]'; ?> |
| 263 | |
| 264 | <div class="csb-option-row csb-always" <?php if ( ! $data['always'] ) : ?>style="display:none"<?php endif; ?>> |
| 265 | <label><?php _e( 'Always', 'custom-sidebars' ); ?></label> |
| 266 | </div> |
| 267 | |
| 268 | <?php /* DATE */ /* ?> |
| 269 | <div class="csb-option-row csb-date" style="display:none"> |
| 270 | <label for="<?php echo esc_attr( $widget->id ); ?>-date"> |
| 271 | <span class="csb-and" style="display:none"><?php _e( 'AND', 'custom-sidebars' ); ?></span> |
| 272 | <?php _e( 'On these dates', 'custom-sidebars' ); ?> |
| 273 | </label> |
| 274 | <i class="dashicons dashicons-trash clear-filter show-on-hover action"></i> |
| 275 | <input type="text" |
| 276 | id="<?php echo esc_attr( $widget->id ); ?>-date" |
| 277 | name="<?php echo esc_attr( $block_name ); ?>[date][from]" |
| 278 | value="<?php echo esc_attr( @$cond['date']['from'] ); ?>" /> |
| 279 | <input type="text" |
| 280 | id="<?php echo esc_attr( $widget->id ); ?>-date-to" |
| 281 | name="<?php echo esc_attr( $block_name ); ?>[date][to]" |
| 282 | value="<?php echo esc_attr( @$cond['date']['to'] ); ?>" /> |
| 283 | </div> |
| 284 | <?php */ ?> |
| 285 | |
| 286 | <?php /* GUEST */ ?> |
| 287 | <div class="csb-option-row csb-guest" <?php if ( empty( $cond['guest'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 288 | <label for="<?php echo esc_attr( $widget->id ); ?>-guest1" style="padding-top:10px;margin-bottom:0"> |
| 289 | <input id="<?php echo esc_attr( $widget->id ); ?>-guest1" type="radio" name="<?php echo esc_attr( $block_name ); ?>[guest][]" value="guest" <?php checked( in_array( 'guest', $cond['guest'] ) ); ?> /> |
| 290 | <span class="csb-and" style="display:none"><?php _e( 'AND', 'custom-sidebars' ); ?></span> |
| 291 | <?php _e( 'User is not logged-in (Guest)', 'custom-sidebars' ); ?><br /> |
| 292 | </label> |
| 293 | <label for="<?php echo esc_attr( $widget->id ); ?>-guest2" style="border:0;margin-bottom:0"> |
| 294 | <input id="<?php echo esc_attr( $widget->id ); ?>-guest2" type="radio" name="<?php echo esc_attr( $block_name ); ?>[guest][]" value="member" <?php checked( in_array( 'member', $cond['guest'] ) ); ?> /> |
| 295 | <span class="csb-and" style="display:none"><?php _e( 'AND', 'custom-sidebars' ); ?></span> |
| 296 | <?php _e( 'User is logged-in (Member)', 'custom-sidebars' ); ?> |
| 297 | </label> |
| 298 | <i class="dashicons dashicons-trash clear-filter show-on-hover action"></i> |
| 299 | </div> |
| 300 | |
| 301 | <?php /* ROLES */ ?> |
| 302 | <div class="csb-option-row csb-roles" <?php if ( empty( $cond['roles'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 303 | <label for="<?php echo esc_attr( $widget->id ); ?>-roles"> |
| 304 | <span class="csb-and" style="display:none"><?php _e( 'AND', 'custom-sidebars' ); ?></span> |
| 305 | <?php _e( 'User has role', 'custom-sidebars' ); ?> |
| 306 | </label> |
| 307 | <i class="dashicons dashicons-trash clear-filter show-on-hover action"></i> |
| 308 | <select id="<?php echo esc_attr( $widget->id ); ?>-roles" name="<?php echo esc_attr( $block_name ); ?>[roles][]" multiple="multiple"> |
| 309 | <?php foreach ( $role_list as $role => $details ) : ?> |
| 310 | <?php $name = translate_user_role( $details['name'] ); ?> |
| 311 | <?php $is_selected = in_array( $role, $cond['roles'] ); ?> |
| 312 | <option <?php selected( $is_selected, true ); ?> value="<?php echo esc_attr( $role ); ?>"> |
| 313 | <?php echo esc_html( $name ); ?> |
| 314 | </option> |
| 315 | <?php endforeach; ?> |
| 316 | </select> |
| 317 | </div> |
| 318 | |
| 319 | <?php /* MEMBERSHIP */ ?> |
| 320 | <?php if ( is_array( $membership_levels ) ) : ?> |
| 321 | <div class="csb-option-row csb-membership" <?php if ( empty( $cond['membership'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 322 | <label for="<?php echo esc_attr( $widget->id ); ?>-membership"> |
| 323 | <span class="csb-and" style="display:none"><?php _e( 'AND', 'custom-sidebars' ); ?></span> |
| 324 | <?php _e( 'User has Membership Level', 'custom-sidebars' ); ?> |
| 325 | </label> |
| 326 | <i class="dashicons dashicons-trash clear-filter show-on-hover action"></i> |
| 327 | <select id="<?php echo esc_attr( $widget->id ); ?>-membership" name="<?php echo esc_attr( $block_name ); ?>[membership][]" multiple="multiple"> |
| 328 | <?php foreach ( $membership_levels as $level ) : ?> |
| 329 | <?php $is_selected = in_array( $level['id'], $cond['membership'] ); ?> |
| 330 | <option <?php selected( $is_selected ); ?> value="<?php echo esc_attr( $level['id'] ); ?>"> |
| 331 | <?php echo esc_html( $level['level_title'] ); ?> |
| 332 | <?php if ( ! $level['level_active'] ) { _e( '(inactive)', 'custom-sidebars' ); } ?> |
| 333 | </option> |
| 334 | <?php endforeach; ?> |
| 335 | </select> |
| 336 | </div> |
| 337 | <?php endif; ?> |
| 338 | |
| 339 | <?php /* MEMBERSHIP2 (PROTECTED CONTENT) */ ?> |
| 340 | <?php if ( is_array( $membership2_items ) ) : ?> |
| 341 | <div class="csb-option-row csb-membership2" <?php if ( empty( $cond['membership2'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 342 | <label for="<?php echo esc_attr( $widget->id ); ?>-membership2"> |
| 343 | <span class="csb-and" style="display:none"><?php _e( 'AND', 'custom-sidebars' ); ?></span> |
| 344 | <?php _e( 'User has Membership', 'custom-sidebars' ); ?> |
| 345 | </label> |
| 346 | <i class="dashicons dashicons-trash clear-filter show-on-hover action"></i> |
| 347 | <select id="<?php echo esc_attr( $widget->id ); ?>-membership2" name="<?php echo esc_attr( $block_name ); ?>[membership2][]" multiple="multiple"> |
| 348 | <?php foreach ( $membership2_items as $item ) : ?> |
| 349 | <?php $is_selected = in_array( $item->id, $cond['membership2'] ); ?> |
| 350 | <option <?php selected( $is_selected ); ?> value="<?php echo esc_attr( $item->id ); ?>"> |
| 351 | <?php echo esc_html( $item->name ); ?> |
| 352 | <?php if ( ! $item->active ) { _e( '(inactive)', 'custom-sidebars' ); } ?> |
| 353 | </option> |
| 354 | <?php endforeach; ?> |
| 355 | </select> |
| 356 | </div> |
| 357 | <?php endif; ?> |
| 358 | |
| 359 | <?php /* PAGE TYPES */ ?> |
| 360 | <div class="csb-option-row csb-pagetypes" <?php if ( empty( $cond['pagetypes'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 361 | <label for="<?php echo esc_attr( $widget->id ); ?>-pagetypes"> |
| 362 | <span class="csb-and" style="display:none"><?php _e( 'AND', 'custom-sidebars' ); ?></span> |
| 363 | <?php _e( 'On these special pages', 'custom-sidebars' ); ?> |
| 364 | </label> |
| 365 | <i class="dashicons dashicons-trash clear-filter show-on-hover action"></i> |
| 366 | <select id="<?php echo esc_attr( $widget->id ); ?>-pagetypes" name="<?php echo esc_attr( $block_name ); ?>[pagetypes][]" multiple="multiple"> |
| 367 | <?php foreach ( $pagetype_list as $type => $name ) : ?> |
| 368 | <?php $is_selected = in_array( $type, $cond['pagetypes'] ); ?> |
| 369 | <option <?php selected( $is_selected ); ?> value="<?php echo esc_attr( $type ); ?>"> |
| 370 | <?php echo esc_html( $name ); ?> |
| 371 | </option> |
| 372 | <?php endforeach; ?> |
| 373 | </select> |
| 374 | </div> |
| 375 | |
| 376 | <?php /* POSTTYPES */ ?> |
| 377 | <div class="csb-option-row csb-posttypes" |
| 378 | <?php if ( empty( $cond['posttypes'] ) ) : ?>style="display:none"<?php endif; ?>> |
| 379 | |
| 380 | <label for="<?php echo esc_attr( $widget->id ); ?>-posttypes"> |
| 381 | <span class="csb-and" style="display:none"><?php _e( 'AND', 'custom-sidebars' ); ?></span> |
| 382 | <?php _e( 'On any page of these types', 'custom-sidebars' ); ?> |
| 383 | </label> |
| 384 | <i class="dashicons dashicons-trash clear-filter show-on-hover action"></i> |
| 385 | <select class="posttype" |
| 386 | id="<?php echo esc_attr( $widget->id ); ?>-posttypes" |
| 387 | name="<?php echo esc_attr( $block_name ); ?>[posttypes][]" |
| 388 | multiple="multiple"> |
| 389 | <?php foreach ( $type_list as $type_item ) : ?> |
| 390 | <?php $is_selected = in_array( $type_item->name, $cond['posttypes'] ); ?> |
| 391 | <option <?php selected( $is_selected ); ?> value="<?php echo esc_attr( $type_item->name ); ?>"> |
| 392 | <?php echo esc_html( $type_item->labels->name ); ?> |
| 393 | </option> |
| 394 | <?php endforeach; ?> |
| 395 | </select> |
| 396 | |
| 397 | <?php /* SPECIFIC POSTS */ ?> |
| 398 | <?php foreach ( $type_list as $type_item ) : |
| 399 | $row_id = 'pt-' . $type_item->name; |
| 400 | $lbl_all = sprintf( __( 'Only for specific %s', 'custom-sidebars' ), $type_item->labels->name ); |
| 401 | $lbl_single = sprintf( __( 'Only these %s:', 'custom-sidebars' ), $type_item->labels->name ); |
| 402 | $is_selected = in_array( $type_item->name, $cond['posttypes'] ); |
| 403 | $ajax_url = admin_url( 'admin-ajax.php?action=cs-ajax&do=visibility&posttype=' . $type_item->name ); |
| 404 | $posts = array(); |
| 405 | |
| 406 | if ( ! empty( $cond[ $row_id ] ) ) { |
| 407 | $posts = get_posts( |
| 408 | array( |
| 409 | 'post_type' => $type_item->name, |
| 410 | 'order_by' => 'title', |
| 411 | 'order' => 'ASC', |
| 412 | 'numberposts' => '0', |
| 413 | 'include' => implode( ',', $cond[ $row_id ] ), |
| 414 | ) |
| 415 | ); |
| 416 | } |
| 417 | |
| 418 | ?> |
| 419 | <div class="csb-detail-row csb-<?php echo esc_attr( $row_id ); ?>" |
| 420 | <?php if ( ! $is_selected ) : ?>style="display:none"<?php endif; ?>> |
| 421 | |
| 422 | <label for="<?php echo esc_attr( $widget->id ); ?>-<?php echo esc_attr( $row_id ); ?>"> |
| 423 | <input type="checkbox" |
| 424 | id="<?php echo esc_attr( $widget->id ); ?>-<?php echo esc_attr( $row_id ); ?>" |
| 425 | <?php checked( ! empty( $cond[ $row_id ] ) ); ?> |
| 426 | data-lbl-all="<?php echo esc_attr( $lbl_all ); ?>" |
| 427 | data-lbl-single="<?php echo esc_attr( $lbl_single ); ?>" /> |
| 428 | <span class="lbl"> |
| 429 | <?php echo esc_html( empty( $cond[ $row_id ] ) ? $lbl_all : $lbl_single ); ?> |
| 430 | </span> |
| 431 | </label> |
| 432 | <div class="detail" <?php if ( empty( $cond[ $row_id ] ) ) : ?>style="display:none"<?php endif; ?>> |
| 433 | |
| 434 | <select name="<?php echo esc_attr( $block_name ); ?>[<?php echo esc_attr( $row_id ); ?>][]" data-select-ajax="<?php echo esc_url( $ajax_url ); ?>" multiple="multiple"> |
| 435 | <?php if ( ! empty( $posts ) ) : ?> |
| 436 | <?php foreach ( $posts as $post ) : ?> |
| 437 | <option value="<?php echo $post->ID; ?>" selected="selected"><?php echo $post->post_title; ?></option> |
| 438 | <?php endforeach; ?> |
| 439 | <?php endif; ?> |
| 440 | </select> |
| 441 | </div> |
| 442 | </div> |
| 443 | <?php endforeach; ?> |
| 444 | </div> |
| 445 | |
| 446 | <?php /* SPECIFIC TAXONOMY */ ?> |
| 447 | <?php |
| 448 | foreach ( $tax_list as $tax_item ) { |
| 449 | $row_id = 'tax-' . $tax_item->name; |
| 450 | $ajax_url = admin_url( 'admin-ajax.php?action=cs-ajax&do=visibility&tag=' . $tax_item->name ); |
| 451 | $tags = array(); |
| 452 | if ( ! empty( $cond[ $row_id ] ) ) { |
| 453 | $tags = get_terms( |
| 454 | $tax_item->name, |
| 455 | array( |
| 456 | 'include' => implode( ',', $cond[ $row_id ] ), |
| 457 | 'hide_empty' => false, |
| 458 | ) |
| 459 | ); |
| 460 | } |
| 461 | ?> |
| 462 | <div class="csb-option-row csb-<?php echo esc_attr( $row_id ); ?>" |
| 463 | <?php if ( empty( $cond[ $row_id ] ) ) : ?>style="display:none"<?php endif; ?>> |
| 464 | <label for="<?php echo esc_attr( $widget->id ); ?>-<?php echo esc_attr( $row_id ); ?>"> |
| 465 | <span class="csb-and" style="display:none"><?php _e( 'AND', 'custom-sidebars' ); ?></span> |
| 466 | <?php echo esc_html( $tax_item->labels->name ); ?> |
| 467 | </label> |
| 468 | <i class="dashicons dashicons-trash clear-filter show-on-hover action"></i> |
| 469 | <select name="<?php echo esc_attr( $block_name ); ?>[<?php echo esc_attr( $row_id ); ?>][]" data-select-ajax="<?php echo esc_url( $ajax_url ); ?>" multiple="multiple"> |
| 470 | <?php |
| 471 | foreach ( $tags as $one ) { |
| 472 | printf( |
| 473 | '<option value="%d" selected="selected">%s</option>', |
| 474 | esc_attr( $one->term_id ), |
| 475 | esc_html( $one->name ) |
| 476 | ); |
| 477 | } |
| 478 | ?> |
| 479 | </select> |
| 480 | </div> |
| 481 | <?php |
| 482 | } |
| 483 | ?> |
| 484 | |
| 485 | </div> |
| 486 | </div> |
| 487 | <?php |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Integration with the WPMU Dev Membership plugin: |
| 492 | * If the plugin is installed and active this function returns a list of |
| 493 | * all membership levels. |
| 494 | * |
| 495 | * If the plugin is not active the return value is boolean false. |
| 496 | * |
| 497 | * @since 2.0 |
| 498 | * @return bool|array |
| 499 | */ |
| 500 | public function get_membership_levels() { |
| 501 | $Result = null; |
| 502 | |
| 503 | if ( null === $Result ) { |
| 504 | if ( |
| 505 | function_exists( 'M_get_membership_active' ) && |
| 506 | 'no' != M_get_membership_active() && |
| 507 | defined( 'MEMBERSHIP_TABLE_LEVELS' ) |
| 508 | ) { |
| 509 | global $wpdb; |
| 510 | $Result = $wpdb->get_results( |
| 511 | sprintf( |
| 512 | 'SELECT |
| 513 | id, level_title, level_active |
| 514 | FROM %s |
| 515 | ORDER BY id', |
| 516 | MEMBERSHIP_TABLE_LEVELS |
| 517 | ), ARRAY_A |
| 518 | ); |
| 519 | } else { |
| 520 | $Result = false; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return $Result; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Integration with the WPMU Dev Membership2 plugin: |
| 529 | * If the plugin is installed and active this function returns a list of |
| 530 | * all membership levels. |
| 531 | * |
| 532 | * If the plugin is not active the return value is boolean false. |
| 533 | * |
| 534 | * @since 2.0 |
| 535 | * @return bool|array |
| 536 | */ |
| 537 | public function get_membership2_items() { |
| 538 | $Result = null; |
| 539 | |
| 540 | if ( null === $Result ) { |
| 541 | $is_active_membership = apply_filters( 'ms_active', false ); |
| 542 | if ( $is_active_membership ) { |
| 543 | $Result = MS_Plugin::$api->list_memberships( true ); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | return $Result; |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * When user saves the widget we check for the |
| 552 | * |
| 553 | * @since 2.0 |
| 554 | * @param array $new_instance New settings for this instance as input by the user. |
| 555 | * @param array $old_instance Old settings for this instance. |
| 556 | * @return array Modified settings. |
| 557 | */ |
| 558 | public function admin_widget_update( $instance, $new_instance, $old_instance ) { |
| 559 | $data = $this->get_widget_data( $_POST ); |
| 560 | foreach ( $data['conditions'] as $key => $list ) { |
| 561 | if ( ! is_array( $list ) ) { |
| 562 | $list = explode( ',', $list ); |
| 563 | $data['conditions'][ $key ] = $list; |
| 564 | } |
| 565 | } |
| 566 | $instance['csb_visibility'] = $data; |
| 567 | return $instance; |
| 568 | } |
| 569 | |
| 570 | // == Front-end functions |
| 571 | |
| 572 | /** |
| 573 | * Filter the list of widgets for a sidebar so that active sidebars work as expected. |
| 574 | * |
| 575 | * @since 2.0 |
| 576 | * @param array $widget_areas An array of widget areas and their widgets. |
| 577 | * @return array The modified $widget_area array. |
| 578 | */ |
| 579 | public function sidebars_widgets( $widget_areas ) { |
| 580 | static $Settings = array(); |
| 581 | static $Result = array(); |
| 582 | |
| 583 | $custom_sidebars_explain = CustomSidebarsExplain::instance(); |
| 584 | $expl = $custom_sidebars_explain->do_explain(); |
| 585 | |
| 586 | if ( ! did_action( 'cs_before_replace_sidebars' ) ) { |
| 587 | return $widget_areas; |
| 588 | } |
| 589 | |
| 590 | $key = serialize( $widget_areas ); |
| 591 | |
| 592 | if ( ! isset( $Result[ $key ] ) ) { |
| 593 | $expl && do_action( 'cs_explain', '<h4>Filter widgets</h4>', true ); |
| 594 | foreach ( $widget_areas as $widget_area => $widgets ) { |
| 595 | if ( empty( $widgets ) ) { |
| 596 | continue; |
| 597 | } |
| 598 | |
| 599 | if ( 'wp_inactive_widgets' == $widget_area ) { |
| 600 | continue; |
| 601 | } |
| 602 | |
| 603 | $expl && do_action( 'cs_explain', '<h5>Sidebar "' . $widget_area . '"</h5>', true ); |
| 604 | |
| 605 | foreach ( $widgets as $position => $widget_id ) { |
| 606 | // Find the conditions for this widget. |
| 607 | if ( preg_match( '/^(.+?)-(\d+)$/', $widget_id, $matches ) ) { |
| 608 | $id_base = $matches[1]; |
| 609 | $widget_number = intval( $matches[2] ); |
| 610 | } else { |
| 611 | $id_base = $widget_id; |
| 612 | $widget_number = null; |
| 613 | } |
| 614 | |
| 615 | if ( ! isset( $Settings[ $id_base ] ) ) { |
| 616 | $Settings[ $id_base ] = get_option( 'widget_' . $id_base ); |
| 617 | } |
| 618 | |
| 619 | $expl && do_action( 'cs_explain', 'Widget "' . $widget_id . '"', true ); |
| 620 | |
| 621 | // New multi widget (WP_Widget) |
| 622 | if ( ! is_null( $widget_number ) ) { |
| 623 | if ( isset( $Settings[ $id_base ][ $widget_number ] ) && false === $this->maybe_display_widget( $Settings[ $id_base ][ $widget_number ] ) ) { |
| 624 | unset( $widget_areas[ $widget_area ][ $position ] ); |
| 625 | } |
| 626 | } elseif ( ! empty( $Settings[ $id_base ] ) && false === $this->maybe_display_widget( $Settings[ $id_base ] ) ) { |
| 627 | // Old single widget. |
| 628 | unset( $widget_areas[ $widget_area ][ $position ] ); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | $Result[ $key ] = $widget_areas; |
| 634 | } |
| 635 | |
| 636 | return $Result[ $key ]; |
| 637 | } |
| 638 | |
| 639 | public function maybe_display_widget( $instance ) { |
| 640 | global $post, $wp_query; |
| 641 | static $Type_list = null; |
| 642 | static $Tax_list = null; |
| 643 | |
| 644 | $show_widget = true; |
| 645 | $condition_true = true; |
| 646 | $action = 'show'; |
| 647 | $explain = ''; // This is used to explain why a widget is not displayed. |
| 648 | |
| 649 | $custom_sidebars_explain = CustomSidebarsExplain::instance(); |
| 650 | $expl = $custom_sidebars_explain->do_explain(); |
| 651 | |
| 652 | if ( empty( $instance['csb_visibility'] ) || empty( $instance['csb_visibility']['conditions'] ) ) { |
| 653 | return $show_widget; |
| 654 | } |
| 655 | |
| 656 | $cond = $instance['csb_visibility']['conditions']; |
| 657 | $action = 'hide' != $instance['csb_visibility']['action'] ? 'show' : 'hide'; |
| 658 | |
| 659 | if ( $instance['csb_visibility']['always'] ) { |
| 660 | $expl && do_action( 'cs_explain', '<span style="color:#090">Always</span> <b>' . $action . '</b>' ); |
| 661 | return ( 'hide' == $action ? false : true ); |
| 662 | } |
| 663 | |
| 664 | if ( null === $Type_list ) { |
| 665 | $Tax_list = get_taxonomies( array( 'public' => true ), 'objects' ); |
| 666 | $Type_list = get_post_types( array( 'public' => true ), 'objects' ); |
| 667 | } |
| 668 | |
| 669 | // Filter for DATE-RANGE. |
| 670 | if ( $condition_true && ! empty( $cond['date'] ) ) { |
| 671 | // not implemented yet... |
| 672 | } |
| 673 | |
| 674 | // Filter for GUEST STATUS. |
| 675 | if ( $condition_true && ! empty( $cond['guest'] ) && is_array( $cond['guest'] ) ) { |
| 676 | $expl && $explain .= '<br />GUEST ['; |
| 677 | if ( is_user_logged_in() ) { |
| 678 | if ( 'member' != $cond['guest'][0] ) { |
| 679 | $expl && $explain .= 'user is logged in'; |
| 680 | $condition_true = false; |
| 681 | } |
| 682 | } else { |
| 683 | if ( 'guest' != $cond['guest'][0] ) { |
| 684 | $expl && $explain .= 'user not logged in'; |
| 685 | $condition_true = false; |
| 686 | } |
| 687 | } |
| 688 | $expl && $explain .= '] '; |
| 689 | } |
| 690 | |
| 691 | // Filter for USER ROLES. |
| 692 | if ( $condition_true && ! empty( $cond['roles'] ) && is_array( $cond['roles'] ) ) { |
| 693 | $expl && $explain .= '<br />ROLE ['; |
| 694 | if ( ! is_user_logged_in() ) { |
| 695 | $expl && $explain .= 'user not logged in'; |
| 696 | $condition_true = false; |
| 697 | } else { |
| 698 | global $current_user; |
| 699 | $has_role = false; |
| 700 | foreach ( $current_user->roles as $user_role ) { |
| 701 | if ( in_array( $user_role, $cond['roles'] ) ) { |
| 702 | $expl && $explain .= 'ok:' . $user_role; |
| 703 | $has_role = true; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | if ( ! $has_role ) { |
| 708 | $expl && $explain .= 'invalid role'; |
| 709 | $condition_true = false; |
| 710 | } |
| 711 | } |
| 712 | $expl && $explain .= '] '; |
| 713 | } |
| 714 | |
| 715 | // Filter for MEMBERSHIP Level. |
| 716 | if ( $condition_true && ! empty( $cond['membership'] ) ) { |
| 717 | $expl && $explain .= '<br />MEMBERSHIP ['; |
| 718 | if ( class_exists( 'Membership_Factory' ) ) { |
| 719 | $has_level = false; |
| 720 | $wpuser = get_userdata( get_current_user_id() ); |
| 721 | |
| 722 | $is_admin = $wpuser && ( |
| 723 | $wpuser->has_cap( 'membershipadmin' ) || |
| 724 | $wpuser->has_cap( 'manage_options' ) || |
| 725 | is_super_admin() |
| 726 | ); |
| 727 | |
| 728 | if ( $is_admin ) { |
| 729 | $expl && $explain .= 'is admin'; |
| 730 | $has_level = true; |
| 731 | } else { |
| 732 | $factory = new Membership_Factory(); |
| 733 | $user = $factory->get_member( get_current_user_id() ); |
| 734 | $levels = $user->get_level_ids(); |
| 735 | |
| 736 | if ( ! is_array( $levels ) ) { $levels = array( $levels ); } |
| 737 | |
| 738 | foreach ( $cond['membership'] as $need_level_id ) { |
| 739 | if ( empty( $need_level_id ) ) { continue; } |
| 740 | foreach ( $levels as $the_level ) { |
| 741 | if ( $the_level->level_id == $need_level_id ) { |
| 742 | $expl && $explain .= 'ok'; |
| 743 | $has_level = true; |
| 744 | break; |
| 745 | } |
| 746 | } |
| 747 | if ( $has_level ) { break; } |
| 748 | } |
| 749 | } |
| 750 | if ( ! $has_level ) { |
| 751 | $expl && $explain .= 'invalid user level'; |
| 752 | $condition_true = false; |
| 753 | } |
| 754 | } |
| 755 | $expl && $explain .= '] '; |
| 756 | } |
| 757 | |
| 758 | // Filter for MEMBERSHIP2 Level. |
| 759 | if ( $condition_true && ! empty( $cond['membership2'] ) ) { |
| 760 | $expl && $explain .= '<br />MEMBERSHIP2 ['; |
| 761 | if ( apply_filters( 'ms_active', false ) ) { |
| 762 | $is_member = false; |
| 763 | $member = MS_Plugin::$api->get_current_member(); |
| 764 | |
| 765 | if ( $member->is_admin_user() ) { |
| 766 | $expl && $explain .= 'is admin'; |
| 767 | $is_member = true; |
| 768 | } else { |
| 769 | foreach ( $cond['membership2'] as $membership_id ) { |
| 770 | if ( $member->has_membership( $membership_id ) ) { |
| 771 | $is_member = true; |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | if ( ! $is_member ) { |
| 777 | $expl && $explain .= 'is no member'; |
| 778 | $condition_true = false; |
| 779 | } |
| 780 | } |
| 781 | $expl && $explain .= '] '; |
| 782 | } |
| 783 | |
| 784 | // Filter for PRO-SITE Level. |
| 785 | if ( $condition_true && ! empty( $cond['prosite'] ) ) { |
| 786 | $expl && $explain .= '<br />PROSITE ['; |
| 787 | // not implemented yet... |
| 788 | $expl && $explain .= '] '; |
| 789 | } |
| 790 | |
| 791 | // Filter for SPECIAL PAGES. |
| 792 | if ( $condition_true && ! empty( $cond['pagetypes'] ) && is_array( $cond['pagetypes'] ) ) { |
| 793 | $expl && $explain .= '<br />PAGETYPE ['; |
| 794 | $is_type = false; |
| 795 | foreach ( $cond['pagetypes'] as $type ) { |
| 796 | if ( $is_type ) { |
| 797 | break; |
| 798 | } |
| 799 | |
| 800 | switch ( $type ) { |
| 801 | case 'e404': |
| 802 | $is_type = $is_type || is_404(); |
| 803 | break; |
| 804 | case 'single': |
| 805 | $is_type = $is_type || is_singular(); |
| 806 | break; |
| 807 | case 'search': |
| 808 | $is_type = $is_type || is_search(); |
| 809 | break; |
| 810 | case 'archive': |
| 811 | $is_type = $is_type || is_archive(); |
| 812 | break; |
| 813 | case 'preview': |
| 814 | $is_type = $is_type || is_preview(); |
| 815 | break; |
| 816 | case 'day': |
| 817 | $is_type = $is_type || is_day(); |
| 818 | break; |
| 819 | case 'month': |
| 820 | $is_type = $is_type || is_month(); |
| 821 | break; |
| 822 | case 'year': |
| 823 | $is_type = $is_type || is_year(); |
| 824 | break; |
| 825 | case 'frontpage': |
| 826 | if ( current_theme_supports( 'infinite-scroll' ) ) { |
| 827 | $is_type = $is_type || is_front_page(); |
| 828 | } else { |
| 829 | $is_type = $is_type || ( is_front_page() && ! is_paged() ); |
| 830 | } |
| 831 | break; |
| 832 | case 'posts': |
| 833 | case 'home': |
| 834 | $is_type = $is_type || is_home(); |
| 835 | break; |
| 836 | } |
| 837 | $expl && $explain .= $type . ':' . ($is_type ? 'ok' : 'invalid'); |
| 838 | } |
| 839 | if ( ! $is_type ) { |
| 840 | $condition_true = false; |
| 841 | } |
| 842 | $expl && $explain .= '] '; |
| 843 | } |
| 844 | |
| 845 | // Filter for POST-TYPE. |
| 846 | if ( $condition_true && ! empty( $cond['posttypes'] ) ) { |
| 847 | $posttype = get_post_type(); |
| 848 | $expl && $explain .= '<br />POSTTYPE-' . strtoupper( $posttype ) . ' ['; |
| 849 | |
| 850 | if ( ! in_array( $posttype, $cond['posttypes'] ) ) { |
| 851 | $expl && $explain .= 'invalid posttype'; |
| 852 | $condition_true = false; |
| 853 | } else { |
| 854 | // Filter for SPECIFIC POSTS. |
| 855 | if ( ! empty( $cond[ 'pt-' . $posttype ] ) ) { |
| 856 | if ( ! in_array( get_the_ID(), $cond[ 'pt-' . $posttype ] ) ) { |
| 857 | $expl && $explain .= 'invalid post_id'; |
| 858 | $condition_true = false; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | if ( $condition_true ) { |
| 863 | $expl && $explain .= 'ok'; |
| 864 | } |
| 865 | $expl && $explain .= '] '; |
| 866 | } |
| 867 | |
| 868 | if ( $condition_true ) { |
| 869 | // TAXONOMY condition. |
| 870 | $tax_query = null; |
| 871 | if ( isset( $wp_query->tax_query ) ) { |
| 872 | $tax_query = $wp_query->tax_query->queries; |
| 873 | } |
| 874 | $tax_type = $tax_terms = false; |
| 875 | if ( ! empty( $tax_query ) && is_array( $tax_query ) ) { |
| 876 | $tax_type = $tax_query[0]['taxonomy']; |
| 877 | $tax_terms = $tax_query[0]['terms']; |
| 878 | } |
| 879 | |
| 880 | foreach ( $Tax_list as $tax_item ) { |
| 881 | if ( ! $condition_true ) { |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | $tax_key = 'tax-' . $tax_item->name; |
| 886 | if ( isset( $cond[ $tax_key ] ) && ! empty( $cond[ $tax_key ] ) ) { |
| 887 | $expl && $explain .= '<br />TAX-' . strtoupper( $tax_item->name ) . ' ['; |
| 888 | $has_term = false; |
| 889 | |
| 890 | if ( $tax_type && $tax_type == $tax_item->name ) { |
| 891 | // Check if we did filter for the specific taxonomy. |
| 892 | foreach ( $tax_terms as $slug ) { |
| 893 | $term_data = get_term_by( 'slug', $slug, $tax_type ); |
| 894 | if ( in_array( $term_data->term_id, $cond[ $tax_key ] ) ) { |
| 895 | $expl && $explain .= 'ok:' . $term_data->term_id; |
| 896 | $has_term = true; |
| 897 | } |
| 898 | } |
| 899 | } else { |
| 900 | // Check if current post has the specific taxonomy. |
| 901 | foreach ( $cond[ $tax_key ] as $term ) { |
| 902 | if ( has_term( $term, $tax_item->name ) ) { |
| 903 | $expl && $explain .= 'ok:' . $term; |
| 904 | $has_term = true; |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | if ( ! $has_term ) { |
| 910 | $expl && $explain .= 'no match'; |
| 911 | $condition_true = false; |
| 912 | } |
| 913 | $expl && $explain .= '] '; |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | if ( ( 'show' == $action && ! $condition_true ) || ( 'hide' == $action && $condition_true ) ) { |
| 919 | $show_widget = false; |
| 920 | } |
| 921 | |
| 922 | $expl && do_action( |
| 923 | 'cs_explain', |
| 924 | ($condition_true ? '<span style="color:#090">Do</span>' : '<span style="color:#900">Dont</span>') . |
| 925 | ' <b>' . $action . '</b> - ' . |
| 926 | $explain |
| 927 | ); |
| 928 | |
| 929 | return $show_widget; |
| 930 | } |
| 931 | |
| 932 | // |
| 933 | // ========== AJAX Handler |
| 934 | // |
| 935 | |
| 936 | /** |
| 937 | * Ajax handler. If the action is processed the request is closed via die() |
| 938 | * |
| 939 | * @since 2.0.9.7 |
| 940 | * @param string $action |
| 941 | */ |
| 942 | public function handle_ajax( $action ) { |
| 943 | // The ajax request was not meant for us... |
| 944 | if ( 'visibility' != $action ) { |
| 945 | return false; |
| 946 | } |
| 947 | |
| 948 | $data = array(); |
| 949 | if ( isset( $_GET['tag'] ) ) { |
| 950 | $data = $this->ajax_data_terms( @$_GET['tag'], @$_REQUEST['q'] ); |
| 951 | } elseif ( isset( $_GET['posttype'] ) ) { |
| 952 | $data = $this->ajax_data_posts( @$_GET['posttype'], @$_REQUEST['q'] ); |
| 953 | } |
| 954 | |
| 955 | self::json_response( array( 'items' => $data ) ); |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Returns an array with tags that contain the specified search term. |
| 960 | * |
| 961 | * @since 2.0.9.7 |
| 962 | * @param string $term_name Taxonomy type. |
| 963 | * @param string $search Search term. |
| 964 | * @return array |
| 965 | */ |
| 966 | protected function ajax_data_terms( $term_name, $search ) { |
| 967 | $data = array(); |
| 968 | $tags = get_terms( |
| 969 | $term_name, |
| 970 | array( |
| 971 | 'hide_empty' => false, |
| 972 | 'search' => $search, |
| 973 | ) |
| 974 | ); |
| 975 | |
| 976 | foreach ( $tags as $tag ) { |
| 977 | $key = $tag->term_id; |
| 978 | $name = $tag->name; |
| 979 | $data[] = array( |
| 980 | 'id' => $key, |
| 981 | 'text' => esc_html( $name ), |
| 982 | ); |
| 983 | } |
| 984 | |
| 985 | return $data; |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * Returns an array with post-titles that contain the specified search term. |
| 990 | * |
| 991 | * @since 2.0.9.7 |
| 992 | * @param string $posttype Post-type to search. |
| 993 | * @param string $search Search term. |
| 994 | * @return array |
| 995 | */ |
| 996 | protected function ajax_data_posts( $posttype, $search ) { |
| 997 | $data = array(); |
| 998 | $posts = get_posts( |
| 999 | array( |
| 1000 | 'post_type' => $posttype, |
| 1001 | 'order_by' => 'title', |
| 1002 | 'order' => 'ASC', |
| 1003 | 'numberposts' => '0', |
| 1004 | 's' => $search, |
| 1005 | ) |
| 1006 | ); |
| 1007 | |
| 1008 | foreach ( $posts as $post ) { |
| 1009 | $id = $post->ID; |
| 1010 | $text = $post->post_title; |
| 1011 | $data[] = array( |
| 1012 | 'id' => $post->ID, |
| 1013 | 'text' => esc_html( $text ), |
| 1014 | ); |
| 1015 | } |
| 1016 | |
| 1017 | return $data; |
| 1018 | } |
| 1019 | }; |
| 1020 |