external
7 years ago
integrations
7 years ago
class-custom-sidebars-checkup-notification.php
5 years ago
class-custom-sidebars-cloning.php
9 years ago
class-custom-sidebars-editor.php
7 years ago
class-custom-sidebars-explain.php
7 years ago
class-custom-sidebars-export.php
8 years ago
class-custom-sidebars-replacer.php
7 years ago
class-custom-sidebars-visibility.php
7 years ago
class-custom-sidebars-widgets.php
9 years ago
class-custom-sidebars.php
5 years ago
class-custom-sidebars-editor.php
1540 lines
| 1 | <?php |
| 2 | |
| 3 | add_action( 'cs_init', array( 'CustomSidebarsEditor', 'instance' ) ); |
| 4 | |
| 5 | /** |
| 6 | * Provides all the functionality for editing sidebars on the widgets page. |
| 7 | */ |
| 8 | class CustomSidebarsEditor extends CustomSidebars { |
| 9 | |
| 10 | private $modifiable = null; |
| 11 | |
| 12 | /** |
| 13 | * Capability required to use *any* of the plugin features. If user does not |
| 14 | * have this capability then he will not see any change on admin dashboard. |
| 15 | * @var string |
| 16 | */ |
| 17 | static protected $cap_required = 'edit_theme_options'; |
| 18 | |
| 19 | /** |
| 20 | * Metabox roles name |
| 21 | * |
| 22 | * @since 3.0.9 |
| 23 | */ |
| 24 | private $metabox_roles_name = 'custom_sidebars_metabox_roles'; |
| 25 | |
| 26 | /** |
| 27 | * Custom taxoniomies name |
| 28 | * |
| 29 | * @since 3.0.9 |
| 30 | */ |
| 31 | private $custom_taxonomies_name = 'custom_sidebars_custom_taxonomies'; |
| 32 | |
| 33 | /** |
| 34 | * Allow author to change sidebars |
| 35 | * |
| 36 | * @since 3.1.5 |
| 37 | */ |
| 38 | private $allow_author_name = 'custom_sidebars_allow_author'; |
| 39 | |
| 40 | /** |
| 41 | * Returns the singleton object. |
| 42 | * |
| 43 | * @since 2.0 |
| 44 | */ |
| 45 | public static function instance() { |
| 46 | static $Inst = null; |
| 47 | if ( null === $Inst ) { |
| 48 | $Inst = new CustomSidebarsEditor(); |
| 49 | } |
| 50 | return $Inst; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Constructor is private -> singleton. |
| 55 | * |
| 56 | * @since 2.0 |
| 57 | */ |
| 58 | private function __construct() { |
| 59 | if ( ! is_admin() ) { |
| 60 | return; |
| 61 | } |
| 62 | // Add the sidebar metabox to posts. |
| 63 | add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); |
| 64 | // Save the options from the sidebars-metabox. |
| 65 | add_action( 'save_post', array( $this, 'store_replacements' ) ); |
| 66 | // Handle ajax requests. |
| 67 | add_action( 'cs_ajax_request', array( $this, 'handle_ajax' ) ); |
| 68 | add_action( 'admin_init', array( $this, 'settings' ) ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Settings function |
| 73 | * |
| 74 | * @since 3.1.0 |
| 75 | */ |
| 76 | public function settings() { |
| 77 | /** |
| 78 | * metabox role |
| 79 | */ |
| 80 | add_filter( 'screen_settings', array( $this, 'add_capabilities_select_box' ), 10, 2 ); |
| 81 | add_action( 'wp_ajax_custom_sidebars_metabox_roles', array( $this, 'update_custom_sidebars_metabox_roles' ) ); |
| 82 | add_action( 'wp_ajax_custom_sidebars_metabox_custom_taxonomies', array( $this, 'update_custom_sidebars_metabox_custom_taxonomies' ) ); |
| 83 | add_action( 'wp_ajax_custom_sidebars_allow_author', array( $this, 'update_custom_sidebars_allow_author' ) ); |
| 84 | /** |
| 85 | * Check user privileges |
| 86 | */ |
| 87 | $user_can_save = $this->current_user_can_update_custom_sidebars(); |
| 88 | if ( ! $user_can_save ) { |
| 89 | return; |
| 90 | } |
| 91 | // Add a custom column to post list. |
| 92 | $posttypes = self::get_post_types( 'objects' ); |
| 93 | foreach ( $posttypes as $pt ) { |
| 94 | add_filter( 'manage_' . $pt->name . '_posts_columns', array( $this, 'post_columns' ) ); |
| 95 | add_action( 'manage_' . $pt->name . '_posts_custom_column', array( $this, 'post_column_content' ), 10, 2 |
| 96 | ); |
| 97 | } |
| 98 | /** This action is documented in wp-admin/includes/screen.php */ |
| 99 | add_filter( 'default_hidden_columns', array( $this, 'default_hidden_columns' ), 10, 2 ); |
| 100 | add_action( 'quick_edit_custom_box', array( $this, 'post_quick_edit' ), 10, 2 ); |
| 101 | add_action( 'bulk_edit_custom_box', array( $this, 'post_bulk_edit' ), 10, 2 ); |
| 102 | add_action( 'admin_footer', array( $this, 'post_quick_edit_js' ) ); |
| 103 | /** |
| 104 | * Bulk Edit save |
| 105 | * |
| 106 | * @since 3.0.8 |
| 107 | */ |
| 108 | add_action( 'save_post', array( $this, 'bulk_edit_save' ) ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Handles the ajax requests. |
| 113 | */ |
| 114 | public function handle_ajax( $action ) { |
| 115 | $req = (object) array( |
| 116 | 'status' => 'ERR', |
| 117 | ); |
| 118 | $is_json = true; |
| 119 | $handle_it = false; |
| 120 | $view_file = ''; |
| 121 | $sb_id = ''; |
| 122 | if ( isset( $_POST['sb'] ) ) { |
| 123 | $sb_id = $_POST['sb']; |
| 124 | } |
| 125 | switch ( $action ) { |
| 126 | case 'get': |
| 127 | case 'save': |
| 128 | case 'delete': |
| 129 | case 'get-location': |
| 130 | case 'set-location': |
| 131 | case 'replaceable': |
| 132 | $handle_it = true; |
| 133 | $req->status = 'OK'; |
| 134 | $req->action = $action; |
| 135 | $req->id = $sb_id; |
| 136 | break; |
| 137 | } |
| 138 | // The ajax request was not meant for us... |
| 139 | if ( ! $handle_it ) { |
| 140 | return false; |
| 141 | } |
| 142 | $sb_data = self::get_sidebar( $sb_id ); |
| 143 | if ( ! current_user_can( self::$cap_required ) ) { |
| 144 | $req = self::req_err( |
| 145 | $req, |
| 146 | __( 'You do not have permission for this', 'custom-sidebars' ) |
| 147 | ); |
| 148 | } else { |
| 149 | switch ( $action ) { |
| 150 | // Return details for the specified sidebar. |
| 151 | case 'get': |
| 152 | /** |
| 153 | * check nonce |
| 154 | */ |
| 155 | if ( |
| 156 | ! isset( $_POST['_wpnonce'] ) |
| 157 | || ! wp_verify_nonce( $_POST['_wpnonce'], 'custom-sidebars-get' ) |
| 158 | ) { |
| 159 | $req = self::req_err( |
| 160 | $req, |
| 161 | __( 'You do not have permission for this', 'custom-sidebars' ) |
| 162 | ); |
| 163 | } else { |
| 164 | $sb_data['advance'] = false; |
| 165 | $user_id = get_current_user_id(); |
| 166 | if ( $user_id ) { |
| 167 | $advance = get_user_option( 'custom-sidebars-editor-advance', $user_id ); |
| 168 | if ( is_array( $advance ) && isset( $advance[ $sb_data['id'] ] ) ) { |
| 169 | $sb_data['advance'] = $advance[ $sb_data['id'] ]; |
| 170 | } |
| 171 | } |
| 172 | $req->sidebar = $sb_data; |
| 173 | } |
| 174 | break; |
| 175 | // Save or insert the specified sidebar. |
| 176 | case 'save': |
| 177 | $req = $this->save_item( $req, $_POST ); |
| 178 | break; |
| 179 | // Delete the specified sidebar. |
| 180 | case 'delete': |
| 181 | $req->sidebar = $sb_data; |
| 182 | $req = $this->delete_item( $req, $_POST ); |
| 183 | break; |
| 184 | // Get the location data. |
| 185 | case 'get-location': |
| 186 | $req->sidebar = $sb_data; |
| 187 | $req = $this->get_location_data( $req ); |
| 188 | break; |
| 189 | // Update the location data. |
| 190 | case 'set-location': |
| 191 | $req->sidebar = $sb_data; |
| 192 | $req = $this->set_location_data( $req ); |
| 193 | break; |
| 194 | // Toggle theme sidebar replaceable-flag. |
| 195 | case 'replaceable': |
| 196 | $req = $this->set_replaceable( $req ); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | // Make the ajax response either as JSON or plain text. |
| 201 | if ( $is_json ) { |
| 202 | self::json_response( $req ); |
| 203 | } else { |
| 204 | ob_start(); |
| 205 | include CSB_VIEWS_DIR . $view_file; |
| 206 | $resp = ob_get_clean(); |
| 207 | self::plain_response( $resp ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Saves the item specified by $data array and populates the response |
| 213 | * object. When $req->id is empty a new sidebar will be created. Otherwise |
| 214 | * the existing sidebar is updated. |
| 215 | * |
| 216 | * @since 2.0 |
| 217 | * @param object $req Initial response object. |
| 218 | * @param array $data Sidebar data to save (typically this is $_POST). |
| 219 | * @return object Updated response object. |
| 220 | */ |
| 221 | private function save_item( $req, $data ) { |
| 222 | /** |
| 223 | * check nonce |
| 224 | */ |
| 225 | if ( |
| 226 | ! isset( $data['_wpnonce'] ) |
| 227 | || ! wp_verify_nonce( $data['_wpnonce'], 'custom-sidebars-edit-sidebar' ) |
| 228 | ) { |
| 229 | return self::req_err( |
| 230 | $req, |
| 231 | __( 'You have no permission to do this operation.', 'custom-sidebars' ) |
| 232 | ); |
| 233 | } |
| 234 | $sidebars = self::get_custom_sidebars(); |
| 235 | $sb_id = $req->id; |
| 236 | $sb_desc = ''; |
| 237 | if ( isset( $data['description'] ) ) { |
| 238 | $sb_desc = stripslashes( trim( $data['description'] ) ); |
| 239 | } |
| 240 | $sb_name = isset( $data['name'] )? $data['name']:''; |
| 241 | if ( function_exists( 'mb_substr' ) ) { |
| 242 | $sb_name = mb_substr( stripslashes( trim( $sb_name ) ), 0, 40 ); |
| 243 | } else { |
| 244 | $sb_name = substr( stripslashes( trim( $sb_name ) ), 0, 40 ); |
| 245 | } |
| 246 | if ( empty( $sb_name ) ) { |
| 247 | return self::req_err( |
| 248 | $req, |
| 249 | __( 'Sidebar-name cannot be empty', 'custom-sidebars' ) |
| 250 | ); |
| 251 | } |
| 252 | if ( empty( $sb_id ) ) { |
| 253 | // Create a new sidebar. |
| 254 | $action = 'insert'; |
| 255 | $num = count( $sidebars ); |
| 256 | do { |
| 257 | $num += 1; |
| 258 | $sb_id = self::$sidebar_prefix . $num; |
| 259 | } while ( self::get_sidebar( $sb_id, 'cust' ) ); |
| 260 | $sidebar = array( |
| 261 | 'id' => $sb_id, |
| 262 | ); |
| 263 | } else { |
| 264 | // Update existing sidebar |
| 265 | $action = 'update'; |
| 266 | $sidebar = self::get_sidebar( $sb_id, 'cust' ); |
| 267 | if ( ! $sidebar ) { |
| 268 | return self::req_err( |
| 269 | $req, |
| 270 | __( 'The sidebar does not exist', 'custom-sidebars' ) |
| 271 | ); |
| 272 | } |
| 273 | } |
| 274 | if ( function_exists( 'mb_strlen' ) ) { |
| 275 | if ( mb_strlen( $sb_desc ) > 200 ) { |
| 276 | $sb_desc = mb_substr( $sb_desc, 0, 200 ); |
| 277 | } |
| 278 | } else { |
| 279 | if ( strlen( $sb_desc ) > 200 ) { |
| 280 | $sb_desc = substr( $sb_desc, 0, 200 ); |
| 281 | } |
| 282 | } |
| 283 | // Populate the sidebar object. |
| 284 | if ( 'insert' == $action || self::wpml_is_default_lang() ) { |
| 285 | $sidebar['name'] = $sb_name; |
| 286 | $sidebar['description'] = $sb_desc; |
| 287 | } else { |
| 288 | $sidebar['name_lang'] = $sb_name; |
| 289 | $sidebar['description_lang'] = $sb_desc; |
| 290 | } |
| 291 | foreach ( array( 'before', 'after' ) as $prefix ) { |
| 292 | foreach ( array( 'widget', 'title' ) as $sufix ) { |
| 293 | $name = sprintf( '%s_%s', $prefix, $sufix ); |
| 294 | $sidebar[ $name ] = ''; |
| 295 | if ( isset( $_POST[ $name ] ) ) { |
| 296 | $sidebar[ $name ] = stripslashes( trim( $_POST[ $name ] ) ); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | if ( 'insert' == $action ) { |
| 301 | $sidebars[] = $sidebar; |
| 302 | $req->message = sprintf( |
| 303 | __( 'Created new sidebar <strong>%1$s</strong>', 'custom-sidebars' ), |
| 304 | esc_html( $sidebar['name'] ) |
| 305 | ); |
| 306 | } else { |
| 307 | $found = false; |
| 308 | foreach ( $sidebars as $ind => $item ) { |
| 309 | if ( $item['id'] == $sb_id ) { |
| 310 | $req->message = sprintf( |
| 311 | __( 'Updated sidebar <strong>%1$s</strong>', 'custom-sidebars' ), |
| 312 | esc_html( $sidebar['name'] ) |
| 313 | ); |
| 314 | $sidebars[ $ind ] = $sidebar; |
| 315 | $found = true; |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | if ( ! $found ) { |
| 320 | return self::req_err( |
| 321 | $req, |
| 322 | __( 'The sidebar was not found', 'custom-sidebars' ) |
| 323 | ); |
| 324 | } |
| 325 | } |
| 326 | // Save the changes. |
| 327 | self::set_custom_sidebars( $sidebars ); |
| 328 | self::refresh_sidebar_widgets(); |
| 329 | $req->data = $sidebar; |
| 330 | $req->action = $action; |
| 331 | // Allow user to translate sidebar name/description via WPML. |
| 332 | self::wpml_update( $sidebars ); |
| 333 | $req->data = self::wpml_translate( $sidebar ); |
| 334 | /** |
| 335 | * save user preferences (advance). |
| 336 | * |
| 337 | * @since 3.1.3 |
| 338 | */ |
| 339 | $user_id = get_current_user_id(); |
| 340 | if ( $user_id ) { |
| 341 | $advance = get_user_option( 'custom-sidebars-editor-advance', $user_id ); |
| 342 | if ( ! is_array( $advance ) ) { |
| 343 | $advance = array(); |
| 344 | } |
| 345 | $advance[ $req->data['id'] ] = isset( $_POST['advance'] ) && 'show' === $_POST['advance']; |
| 346 | update_user_option( $user_id, 'custom-sidebars-editor-advance', $advance ); |
| 347 | } |
| 348 | return $req; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Delete the specified sidebar and update the response object. |
| 353 | * |
| 354 | * @since 2.0 |
| 355 | * @since 3.0.8.1 Added the $data param. |
| 356 | * |
| 357 | * @param object $req Initial response object. |
| 358 | * @param array $data Sidebar data to save (typically this is $_POST). |
| 359 | * @return object Updated response object. |
| 360 | */ |
| 361 | private function delete_item( $req, $data ) { |
| 362 | /** |
| 363 | * check nonce |
| 364 | */ |
| 365 | if ( |
| 366 | ! isset( $data['_wpnonce'] ) |
| 367 | || ! wp_verify_nonce( $data['_wpnonce'], 'custom-sidebars-delete-sidebar' ) |
| 368 | ) { |
| 369 | return self::req_err( |
| 370 | $req, |
| 371 | __( 'You have no permission to do this operation.', 'custom-sidebars' ) |
| 372 | ); |
| 373 | } |
| 374 | $sidebars = self::get_custom_sidebars(); |
| 375 | $sidebar = self::get_sidebar( $req->id, 'cust' ); |
| 376 | if ( ! $sidebar ) { |
| 377 | return self::req_err( |
| 378 | $req, |
| 379 | __( 'The sidebar does not exist', 'custom-sidebars' ) |
| 380 | ); |
| 381 | } |
| 382 | $found = false; |
| 383 | foreach ( $sidebars as $ind => $item ) { |
| 384 | if ( $item['id'] == $req->id ) { |
| 385 | $found = true; |
| 386 | $req->message = sprintf( |
| 387 | __( 'Deleted sidebar <strong>%1$s</strong>', 'custom-sidebars' ), |
| 388 | esc_html( $req->sidebar['name'] ) |
| 389 | ); |
| 390 | unset( $sidebars[ $ind ] ); |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | if ( ! $found ) { |
| 395 | return self::req_err( |
| 396 | $req, |
| 397 | __( 'The sidebar was not found', 'custom-sidebars' ) |
| 398 | ); |
| 399 | } |
| 400 | // Save the changes. |
| 401 | self::set_custom_sidebars( $sidebars ); |
| 402 | self::refresh_sidebar_widgets(); |
| 403 | return $req; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Save the repaceable flag of a theme sidebar. |
| 408 | * |
| 409 | * @since 2.0 |
| 410 | * @param object $req Initial response object. |
| 411 | * @return object Updated response object. |
| 412 | */ |
| 413 | private function set_replaceable( $req ) { |
| 414 | $state = @$_POST['state']; |
| 415 | $options = self::get_options(); |
| 416 | if ( 'true' === $state ) { |
| 417 | $req->status = true; |
| 418 | if ( ! in_array( $req->id, $options['modifiable'] ) ) { |
| 419 | $options['modifiable'][] = $req->id; |
| 420 | } |
| 421 | } else { |
| 422 | $req->status = false; |
| 423 | foreach ( $options['modifiable'] as $i => $sb_id ) { |
| 424 | if ( $sb_id == $req->id ) { |
| 425 | unset( $options['modifiable'][ $i ] ); |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | $options['modifiable'] = array_values( $options['modifiable'] ); |
| 431 | self::set_options( $options ); |
| 432 | $req->replaceable = (object) $options['modifiable']; |
| 433 | return $req; |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Populates the response object for the "get-location" ajax call. |
| 438 | * Location data defines where a custom sidebar is displayed, i.e. on which |
| 439 | * pages it is used and which theme-sidebars are replaced. |
| 440 | * |
| 441 | * @since 2.0 |
| 442 | * @param object $req Initial response object. |
| 443 | * @return object Updated response object. |
| 444 | */ |
| 445 | private function get_location_data( $req ) { |
| 446 | $defaults = self::get_options(); |
| 447 | $raw_posttype = self::get_post_types( 'objects' ); |
| 448 | $raw_cat = self::get_all_categories(); |
| 449 | $raw_taxonomies = array( |
| 450 | '_builtin' => self::get_taxonomies( 'objects', true ), |
| 451 | 'custom' => self::get_taxonomies( 'objects', false ), |
| 452 | ); |
| 453 | $archive_type = array( |
| 454 | '_blog' => __( 'Front Page', 'custom-sidebars' ), |
| 455 | '_search' => __( 'Search Results', 'custom-sidebars' ), |
| 456 | '_404' => __( 'Not Found (404)', 'custom-sidebars' ), |
| 457 | '_authors' => __( 'Any Author Archive', 'custom-sidebars' ), |
| 458 | '_date' => __( 'Date Archives', 'custom-sidebars' ), |
| 459 | ); |
| 460 | /** |
| 461 | * taxonomies |
| 462 | * |
| 463 | * @since 3.0.7 |
| 464 | */ |
| 465 | $default_taxonomies = array(); |
| 466 | foreach ( $raw_taxonomies['_builtin'] as $taxonomy ) { |
| 467 | $default_taxonomies[] = $taxonomy->labels->singular_name; |
| 468 | switch ( $taxonomy->name ) { |
| 469 | case 'post_format': |
| 470 | break; |
| 471 | case 'post_tag': |
| 472 | /** |
| 473 | * this a legacy and backward compatibility |
| 474 | */ |
| 475 | $archive_type['_tags'] = sprintf( __( '%s Archives', 'custom-sidebars' ), $taxonomy->labels->singular_name ); |
| 476 | break; |
| 477 | case 'category': |
| 478 | $archive_type[ '_'.$taxonomy->name ] = sprintf( __( '%s Archives', 'custom-sidebars' ), $taxonomy->labels->singular_name ); |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | foreach ( $raw_taxonomies['custom'] as $taxonomy ) { |
| 483 | if ( in_array( $taxonomy->labels->singular_name, $default_taxonomies ) ) { |
| 484 | $archive_type[ '_taxonomy_'.$taxonomy->name ] = sprintf( __( '%s Archives', 'custom-sidebars' ), ucfirst( $taxonomy->name ) ); |
| 485 | } else { |
| 486 | $archive_type[ '_taxonomy_'.$taxonomy->name ] = sprintf( __( '%s Archives', 'custom-sidebars' ), $taxonomy->labels->singular_name ); |
| 487 | } |
| 488 | } |
| 489 | /** |
| 490 | * sort array by values |
| 491 | */ |
| 492 | asort( $archive_type ); |
| 493 | $raw_authors = array(); |
| 494 | $raw_authors = get_users( |
| 495 | array( |
| 496 | 'order_by' => 'display_name', |
| 497 | 'fields' => array( 'ID', 'display_name' ), |
| 498 | 'who' => 'authors', |
| 499 | ) |
| 500 | ); |
| 501 | // Collect required data for all posttypes. |
| 502 | $posttypes = array(); |
| 503 | foreach ( $raw_posttype as $item ) { |
| 504 | $sel_single = @$defaults['post_type_single'][ $item->name ]; |
| 505 | $posttypes[ $item->name ] = array( |
| 506 | 'name' => $item->labels->name, |
| 507 | 'single' => self::get_array( $sel_single ), |
| 508 | ); |
| 509 | } |
| 510 | // Extract the data from categories list that we need. |
| 511 | $categories = array(); |
| 512 | foreach ( $raw_cat as $item ) { |
| 513 | $sel_single = @$defaults['category_single'][ $item->term_id ]; |
| 514 | $sel_archive = @$defaults['category_archive'][ $item->term_id ]; |
| 515 | $categories[ $item->term_id ] = array( |
| 516 | 'name' => $item->name, |
| 517 | 'count' => $item->count, |
| 518 | 'single' => self::get_array( $sel_single ), |
| 519 | 'archive' => self::get_array( $sel_archive ), |
| 520 | ); |
| 521 | } |
| 522 | // Build a list of archive types. |
| 523 | $archives = array(); // Start with a copy of the posttype list. |
| 524 | foreach ( $raw_posttype as $item ) { |
| 525 | if ( $item->name == 'post' ) { |
| 526 | $label = __( 'Post Index', 'custom-sidebars' ); |
| 527 | } else { |
| 528 | if ( ! $item->has_archive ) { continue; } |
| 529 | $label = sprintf( |
| 530 | __( '%1$s Archives', 'custom-sidebars' ), |
| 531 | $item->labels->singular_name |
| 532 | ); |
| 533 | } |
| 534 | $sel_archive = @$defaults['post_type_archive'][ $item->name ]; |
| 535 | $archives[ $item->name ] = array( |
| 536 | 'name' => $label, |
| 537 | 'archive' => self::get_array( $sel_archive ), |
| 538 | ); |
| 539 | } |
| 540 | foreach ( $archive_type as $key => $name ) { |
| 541 | $sel_archive = @$defaults[ substr( $key, 1 ) ]; |
| 542 | $archives[ $key ] = array( |
| 543 | 'name' => $name, |
| 544 | 'archive' => self::get_array( $sel_archive ), |
| 545 | ); |
| 546 | } |
| 547 | /** |
| 548 | * Custom taxonomies archive |
| 549 | * |
| 550 | * @since 3.0.7 |
| 551 | */ |
| 552 | foreach ( $raw_taxonomies['custom'] as $t ) { |
| 553 | $taxonomy = $t->name; |
| 554 | if ( |
| 555 | isset( $defaults['taxonomies_archive'] ) |
| 556 | && isset( $defaults['taxonomies_archive'][ $taxonomy ] ) |
| 557 | ) { |
| 558 | $name = sprintf( __( '%s Archives', 'custom-sidebars' ), $t->labels->singular_name ); |
| 559 | if ( in_array( $t->labels->singular_name, $default_taxonomies ) ) { |
| 560 | $name = sprintf( __( '%s Archives', 'custom-sidebars' ), ucfirst( $taxonomy ) ); |
| 561 | } |
| 562 | $sel_archive = $defaults['taxonomies_archive'][ $taxonomy ]; |
| 563 | $key = '_taxonomy_'.$taxonomy; |
| 564 | $archives[ $key ] = array( |
| 565 | 'name' => $name, |
| 566 | 'archive' => self::get_array( $sel_archive ), |
| 567 | ); |
| 568 | } |
| 569 | } |
| 570 | /** |
| 571 | * Custom taxonomies taxes |
| 572 | * |
| 573 | * @since 3.1.4 |
| 574 | */ |
| 575 | $allowed = get_option( $this->custom_taxonomies_name, array() ); |
| 576 | $args = array( |
| 577 | 'hide_empty' => true, |
| 578 | ); |
| 579 | foreach ( $allowed as $key ) { |
| 580 | $t = get_terms( $key, $args ); |
| 581 | $terms = array(); |
| 582 | foreach ( $t as $item ) { |
| 583 | $sel_single = $sel_archive = array(); |
| 584 | if ( |
| 585 | isset( $defaults['taxonomies_single'] ) |
| 586 | && isset( $defaults['taxonomies_single'][ $key ] ) |
| 587 | && isset( $defaults['taxonomies_single'][ $key ][ $item->term_id ] ) |
| 588 | ) { |
| 589 | $sel_single = $defaults['taxonomies_single'][ $key ][ $item->term_id ]; |
| 590 | } |
| 591 | if ( |
| 592 | isset( $defaults[ $key.'_archive' ] ) |
| 593 | && isset( $defaults[ $key.'_archive' ][ $item->term_id ] ) |
| 594 | ) { |
| 595 | $sel_archive = $defaults[ $key.'_archive' ][ $item->term_id ]; |
| 596 | } |
| 597 | $terms[ $item->term_id ] = array( |
| 598 | 'name' => $item->name, |
| 599 | 'count' => $item->count, |
| 600 | 'single' => self::get_array( $sel_single ), |
| 601 | 'archive' => self::get_array( $sel_archive ), |
| 602 | ); |
| 603 | } |
| 604 | $req->$key = $terms; |
| 605 | } |
| 606 | /** |
| 607 | * Category archive. |
| 608 | */ |
| 609 | foreach ( $raw_taxonomies['_builtin'] as $t ) { |
| 610 | if ( 'category' == $t->name ) { |
| 611 | if ( isset( $defaults['category_archive'] ) ) { |
| 612 | $sel_archive = $defaults['category_archive']; |
| 613 | $archives['_category'] = array( |
| 614 | 'name' => sprintf( __( '%s Archives', 'custom-sidebars' ), $t->labels->singular_name ), |
| 615 | 'archive' => self::get_array( $sel_archive ), |
| 616 | ); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | // Build a list of authors. |
| 621 | $authors = array(); |
| 622 | foreach ( $raw_authors as $user ) { |
| 623 | $sel_archive = @$defaults['author_archive'][ @$user->ID ]; |
| 624 | $authors[ @$user->ID ] = array( |
| 625 | 'name' => @$user->display_name, |
| 626 | 'archive' => self::get_array( $sel_archive ), |
| 627 | ); |
| 628 | } |
| 629 | $req->authors = $authors; |
| 630 | $req->replaceable = $defaults['modifiable']; |
| 631 | $req->posttypes = $posttypes; |
| 632 | $req->categories = $categories; |
| 633 | $req->archives = $archives; |
| 634 | /** |
| 635 | * screen |
| 636 | */ |
| 637 | $screen = array(); |
| 638 | if ( |
| 639 | isset( $defaults['screen'] ) |
| 640 | && isset( $defaults['screen'][ $req->id ] ) |
| 641 | ) { |
| 642 | $screen = $defaults['screen'][ $req->id ]; |
| 643 | } |
| 644 | $req->screen = $screen; |
| 645 | /** |
| 646 | * Allow to change data of locations. |
| 647 | * |
| 648 | * @since 3.1.2 |
| 649 | * |
| 650 | * @param object $req Object of avaialble data. |
| 651 | * @pages $defaults Data from db. |
| 652 | */ |
| 653 | return apply_filters( 'custom_sidebars_get_location', $req, $defaults ); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Save location data for a single sidebar and populate the response object. |
| 658 | * Location data defines where a custom sidebar is displayed, i.e. on which |
| 659 | * pages it is used and which theme-sidebars are replaced. |
| 660 | * |
| 661 | * @since 2.0 |
| 662 | * @param object $req Initial response object. |
| 663 | * @return object Updated response object. |
| 664 | */ |
| 665 | private function set_location_data( $req ) { |
| 666 | /** |
| 667 | * check nonce |
| 668 | */ |
| 669 | if ( |
| 670 | ! isset( $_POST['_wpnonce'] ) |
| 671 | || ! wp_verify_nonce( $_POST['_wpnonce'], 'custom-sidebars-set-location' ) |
| 672 | ) { |
| 673 | return self::req_err( |
| 674 | $req, |
| 675 | __( 'You have no permission to do this operation.', 'custom-sidebars' ) |
| 676 | ); |
| 677 | } |
| 678 | $options = self::get_options(); |
| 679 | $sidebars = $options['modifiable']; |
| 680 | $raw_posttype = self::get_post_types( 'objects' ); |
| 681 | $raw_cat = self::get_all_categories(); |
| 682 | $data = array(); |
| 683 | $raw_taxonomies = array( |
| 684 | 'custom' => self::get_taxonomies( 'names', false ), |
| 685 | ); |
| 686 | foreach ( $_POST as $key => $value ) { |
| 687 | if ( strlen( $key ) > 8 && '___cs___' == substr( $key, 0, 8 ) ) { |
| 688 | list( $prefix, $id ) = explode( '___', substr( $key, 8 ) ); |
| 689 | if ( ! isset( $data[ $prefix ] ) ) { |
| 690 | $data[ $prefix ] = array(); |
| 691 | } |
| 692 | $data[ $prefix ][ $id ] = $value; |
| 693 | } |
| 694 | } |
| 695 | $special_arc = array( |
| 696 | 'blog', |
| 697 | '404', |
| 698 | 'tags', |
| 699 | 'authors', |
| 700 | 'search', |
| 701 | 'date', |
| 702 | ); |
| 703 | $raw_authors = array(); |
| 704 | $raw_authors = get_users( |
| 705 | array( |
| 706 | 'order_by' => 'display_name', |
| 707 | 'fields' => array( 'ID', 'display_name' ), |
| 708 | 'who' => 'authors', |
| 709 | ) |
| 710 | ); |
| 711 | // == Update the options |
| 712 | foreach ( $sidebars as $sb_id ) { |
| 713 | // Post-type settings. |
| 714 | foreach ( $raw_posttype as $item ) { |
| 715 | $pt = $item->name; |
| 716 | if ( |
| 717 | is_array( @$data['pt'][ $sb_id ] ) && |
| 718 | in_array( $pt, $data['pt'][ $sb_id ] ) |
| 719 | ) { |
| 720 | $options['post_type_single'][ $pt ][ $sb_id ] = $req->id; |
| 721 | } elseif ( |
| 722 | isset( $options['post_type_single'][ $pt ][ $sb_id ] ) && |
| 723 | $options['post_type_single'][ $pt ][ $sb_id ] == $req->id |
| 724 | ) { |
| 725 | unset( $options['post_type_single'][ $pt ][ $sb_id ] ); |
| 726 | } |
| 727 | if ( |
| 728 | is_array( @$data['arc'][ $sb_id ] ) && |
| 729 | in_array( $pt, $data['arc'][ $sb_id ] ) |
| 730 | ) { |
| 731 | $options['post_type_archive'][ $pt ][ $sb_id ] = $req->id; |
| 732 | } elseif ( |
| 733 | isset( $options['post_type_archive'][ $pt ][ $sb_id ] ) && |
| 734 | $options['post_type_archive'][ $pt ][ $sb_id ] == $req->id |
| 735 | ) { |
| 736 | unset( $options['post_type_archive'][ $pt ][ $sb_id ] ); |
| 737 | } |
| 738 | } |
| 739 | // Category settings. |
| 740 | foreach ( $raw_cat as $item ) { |
| 741 | $cat = $item->term_id; |
| 742 | if ( |
| 743 | is_array( @$data['cat'][ $sb_id ] ) && |
| 744 | in_array( $cat, $data['cat'][ $sb_id ] ) |
| 745 | ) { |
| 746 | $options['category_single'][ $cat ][ $sb_id ] = $req->id; |
| 747 | } elseif ( |
| 748 | isset( $options['category_single'][ $cat ][ $sb_id ] ) && |
| 749 | $options['category_single'][ $cat ][ $sb_id ] == $req->id |
| 750 | ) { |
| 751 | unset( $options['category_single'][ $cat ][ $sb_id ] ); |
| 752 | } |
| 753 | if ( |
| 754 | is_array( @$data['arc-cat'][ $sb_id ] ) && |
| 755 | in_array( $cat, $data['arc-cat'][ $sb_id ] ) |
| 756 | ) { |
| 757 | $options['category_archive'][ $cat ][ $sb_id ] = $req->id; |
| 758 | } elseif ( |
| 759 | isset( $options['category_archive'][ $cat ][ $sb_id ] ) && |
| 760 | $options['category_archive'][ $cat ][ $sb_id ] == $req->id |
| 761 | ) { |
| 762 | unset( $options['category_archive'][ $cat ][ $sb_id ] ); |
| 763 | } |
| 764 | } |
| 765 | foreach ( $special_arc as $key ) { |
| 766 | if ( |
| 767 | is_array( @$data['arc'][ $sb_id ] ) && |
| 768 | in_array( '_' . $key, $data['arc'][ $sb_id ] ) |
| 769 | ) { |
| 770 | $options[ $key ][ $sb_id ] = $req->id; |
| 771 | } elseif ( |
| 772 | isset( $options[ $key ][ $sb_id ] ) && |
| 773 | $options[ $key ][ $sb_id ] == $req->id |
| 774 | ) { |
| 775 | unset( $options[ $key ][ $sb_id ] ); |
| 776 | } |
| 777 | } |
| 778 | // Author settings. |
| 779 | foreach ( $raw_authors as $user ) { |
| 780 | $key = $user->ID; |
| 781 | if ( |
| 782 | is_array( @$data['arc-aut'][ $sb_id ] ) && |
| 783 | in_array( $key, $data['arc-aut'][ $sb_id ] ) |
| 784 | ) { |
| 785 | $options['author_archive'][ $key ][ $sb_id ] = $req->id; |
| 786 | } elseif ( |
| 787 | isset( $options['author_archive'][ $key ][ $sb_id ] ) && |
| 788 | $options['author_archive'][ $key ][ $sb_id ] == $req->id |
| 789 | ) { |
| 790 | unset( $options['author_archive'][ $key ][ $sb_id ] ); |
| 791 | } |
| 792 | } |
| 793 | /** |
| 794 | * Custom taxonomies |
| 795 | * |
| 796 | * @since 3.0.7 |
| 797 | */ |
| 798 | foreach ( $raw_taxonomies['custom'] as $taxonomy ) { |
| 799 | $key = '_taxonomy_'.$taxonomy; |
| 800 | if ( |
| 801 | isset( $data['arc'][ $sb_id ] ) |
| 802 | && is_array( $data['arc'][ $sb_id ] ) |
| 803 | && in_array( $key, $data['arc'][ $sb_id ] ) |
| 804 | ) { |
| 805 | $options['taxonomies_archive'][ $taxonomy ][ $sb_id ] = $req->id; |
| 806 | } elseif ( |
| 807 | isset( $options['taxonomies_archive'][ $taxonomy ][ $sb_id ] ) && |
| 808 | $options['taxonomies_archive'][ $taxonomy ][ $sb_id ] == $req->id |
| 809 | ) { |
| 810 | unset( $options['taxonomies_archive'][ $taxonomy ][ $sb_id ] ); |
| 811 | } |
| 812 | /** |
| 813 | * single custom taxonomy |
| 814 | * |
| 815 | * @since 3.1.4 |
| 816 | */ |
| 817 | foreach ( $raw_taxonomies['custom'] as $taxonomy ) { |
| 818 | $key = '_taxonomy_'.$taxonomy.'_single'; |
| 819 | $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); |
| 820 | foreach ( $terms as $term ) { |
| 821 | $term_id = $term->term_id; |
| 822 | if ( |
| 823 | isset( $data[ $taxonomy ][ $sb_id ] ) |
| 824 | && is_array( $data[ $taxonomy ][ $sb_id ] ) |
| 825 | && in_array( $term_id, $data[ $taxonomy ][ $sb_id ] ) |
| 826 | ) { |
| 827 | $options['taxonomies_single'][ $taxonomy ][ $term_id ][ $sb_id ] = $req->id; |
| 828 | } elseif ( |
| 829 | isset( $options['taxonomies_single'][ $taxonomy ][ $term_id ][ $sb_id ] ) && |
| 830 | $options['taxonomies_single'][ $taxonomy ][ $term_id ][ $sb_id ] == $req->id |
| 831 | ) { |
| 832 | unset( $options['taxonomies_single'][ $taxonomy ][ $term_id ][ $sb_id ] ); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | /** |
| 838 | * category Archive |
| 839 | * |
| 840 | * @since 3.0.7 |
| 841 | */ |
| 842 | if ( |
| 843 | isset( $data['arc'][ $sb_id ] ) |
| 844 | && is_array( $data['arc'][ $sb_id ] ) |
| 845 | && in_array( '_category', $data['arc'][ $sb_id ] ) |
| 846 | ) { |
| 847 | $options['category_archive'][ $sb_id ] = $req->id; |
| 848 | } elseif ( |
| 849 | isset( $options['category_archive'][ $sb_id ] ) && |
| 850 | $options['category_archive'][ $sb_id ] == $req->id |
| 851 | ) { |
| 852 | unset( $options['category_archive'][ $sb_id ] ); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * screen size |
| 858 | */ |
| 859 | $size = array(); |
| 860 | if ( |
| 861 | isset( $_POST['cs-screen'] ) |
| 862 | && isset( $_POST['cs-screen']['mode'] ) |
| 863 | && isset( $_POST['cs-screen']['minmax'] ) |
| 864 | && isset( $_POST['cs-screen']['size'] ) |
| 865 | && is_array( $_POST['cs-screen']['mode'] ) |
| 866 | && is_array( $_POST['cs-screen']['minmax'] ) |
| 867 | && is_array( $_POST['cs-screen']['size'] ) |
| 868 | ) { |
| 869 | $screen_size = $_POST['cs-screen']; |
| 870 | for ( $i = 0; $i < count( $screen_size['size'] ); $i++ ) { |
| 871 | if ( ! empty( $screen_size['size'][ $i ] ) ) { |
| 872 | $size[ $screen_size['size'][ $i ] ][ $screen_size['minmax'][ $i ] ] = $screen_size['mode'][ $i ]; |
| 873 | } |
| 874 | } |
| 875 | krsort( $size ); |
| 876 | } |
| 877 | $options['screen'][ $req->id ] = $size; |
| 878 | $req->message = sprintf( |
| 879 | __( 'Updated sidebar <strong>%1$s</strong> settings.', 'custom-sidebars' ), |
| 880 | esc_html( $req->sidebar['name'] ) |
| 881 | ); |
| 882 | /** |
| 883 | * Allow to change data of locations before save. |
| 884 | * |
| 885 | * @since 3.1.2 |
| 886 | * |
| 887 | * @param array $options Current options to save. |
| 888 | * @param string $req->id Sidebar |
| 889 | * @param array $sidebars Allowed sidebars. |
| 890 | * @oaram array $data Data send by request. |
| 891 | */ |
| 892 | $options = apply_filters( 'custom_sidebars_set_location', $options, $req->id, $sidebars, $data ); |
| 893 | self::set_options( $options ); |
| 894 | return $req; |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * Registers the "Sidebars" meta box in the post-editor. |
| 899 | */ |
| 900 | public function add_meta_box() { |
| 901 | global $post; |
| 902 | /** |
| 903 | * check capabilities |
| 904 | */ |
| 905 | $user_can_change_sidebars = $this->check_author_ability_to_replace(); |
| 906 | if ( ! $user_can_change_sidebars ) { |
| 907 | return; |
| 908 | } |
| 909 | $post_type = get_post_type( $post ); |
| 910 | if ( ! $post_type ) { |
| 911 | return false; |
| 912 | } |
| 913 | if ( ! self::supported_post_type( $post_type ) ) { |
| 914 | return false; |
| 915 | } |
| 916 | /** |
| 917 | * Option that can be set in wp-config.php to remove the custom sidebar |
| 918 | * meta box for certain post types. |
| 919 | * |
| 920 | * @since 2.0 |
| 921 | * |
| 922 | * @option bool TRUE will hide all meta boxes. |
| 923 | */ |
| 924 | if ( |
| 925 | defined( 'CUSTOM_SIDEBAR_DISABLE_METABOXES' ) && |
| 926 | CUSTOM_SIDEBAR_DISABLE_METABOXES == true |
| 927 | ) { |
| 928 | return false; |
| 929 | } |
| 930 | $pt_obj = get_post_type_object( $post_type ); |
| 931 | if ( $pt_obj->publicly_queryable || $pt_obj->public ) { |
| 932 | add_meta_box( |
| 933 | 'customsidebars-mb', |
| 934 | __( 'Sidebars', 'custom-sidebars' ), |
| 935 | array( $this, 'print_metabox_editor' ), |
| 936 | $post_type, |
| 937 | 'side' |
| 938 | ); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * Renders the Custom Sidebars meta box in the post-editor. |
| 944 | */ |
| 945 | public function print_metabox_editor() { |
| 946 | global $post; |
| 947 | $this->print_sidebars_form( $post->ID, 'metabox' ); |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Renders the sidebar-fields inside the quick-edit form. |
| 952 | */ |
| 953 | public function print_metabox_quick() { |
| 954 | $this->print_sidebars_form( 0, 'quick-edit' ); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * Renders the Custom Sidebars form. |
| 959 | * |
| 960 | * @param int $post_id The post-ID to display |
| 961 | * @param string $type Which form to display. 'metabox/quick-edit/col-sidebars'. |
| 962 | */ |
| 963 | protected function print_sidebars_form( $post_id, $type = 'metabox' ) { |
| 964 | /** |
| 965 | * check capabilities |
| 966 | */ |
| 967 | $user_can_change_sidebars = $this->check_author_ability_to_replace(); |
| 968 | if ( ! $user_can_change_sidebars ) { |
| 969 | return; |
| 970 | } |
| 971 | global $wp_registered_sidebars; |
| 972 | $available = CustomSidebars::sort_sidebars_by_name( $wp_registered_sidebars ); |
| 973 | $replacements = self::get_replacements( $post_id ); |
| 974 | $sidebars = self::get_options( 'modifiable' ); |
| 975 | $selected = array(); |
| 976 | if ( ! empty( $sidebars ) ) { |
| 977 | foreach ( $sidebars as $s ) { |
| 978 | if ( isset( $replacements[ $s ] ) ) { |
| 979 | $selected[ $s ] = $replacements[ $s ]; |
| 980 | } else { |
| 981 | $selected[ $s ] = ''; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | switch ( $type ) { |
| 986 | case 'col-sidebars': |
| 987 | include CSB_VIEWS_DIR . 'col-sidebars.php'; |
| 988 | break; |
| 989 | case 'quick-edit': |
| 990 | include CSB_VIEWS_DIR . 'quick-edit.php'; |
| 991 | break; |
| 992 | case 'bulk-edit': |
| 993 | /** |
| 994 | * @since 3.0.8 |
| 995 | */ |
| 996 | include CSB_VIEWS_DIR . 'bulk-edit.php'; |
| 997 | break; |
| 998 | default: |
| 999 | include CSB_VIEWS_DIR . 'metabox.php'; |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | public function store_replacements( $post_id ) { |
| 1005 | global $action; |
| 1006 | /** |
| 1007 | * check capabilities |
| 1008 | */ |
| 1009 | $user_can_change_sidebars = $this->check_author_ability_to_replace(); |
| 1010 | if ( ! $user_can_change_sidebars ) { |
| 1011 | return; |
| 1012 | } |
| 1013 | /* |
| 1014 | * Verify if this is an auto save routine. If it is our form has not |
| 1015 | * been submitted, so we dont want to do anything |
| 1016 | * (Copied and pasted from wordpress add_metabox_tutorial) |
| 1017 | */ |
| 1018 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 1019 | return $post_id; |
| 1020 | } |
| 1021 | /* |
| 1022 | * 'editpost' .. Saved from full Post-Editor screen. |
| 1023 | * 'inline-save' .. Saved via the quick-edit form. |
| 1024 | */ |
| 1025 | if ( ( isset( $_REQUEST['action'] ) && 'inline-save' != $_REQUEST['action'] ) && 'editpost' != $action ) { |
| 1026 | return $post_id; |
| 1027 | } |
| 1028 | // Make sure meta is added to the post, not a revision. |
| 1029 | if ( $the_post = wp_is_post_revision( $post_id ) ) { |
| 1030 | $post_id = $the_post; |
| 1031 | } |
| 1032 | $sidebars = self::get_options( 'modifiable' ); |
| 1033 | $data = array(); |
| 1034 | if ( ! empty( $sidebars ) ) { |
| 1035 | foreach ( $sidebars as $sb_id ) { |
| 1036 | if ( isset( $_POST[ 'cs_replacement_' . $sb_id ] ) ) { |
| 1037 | $replacement = $_POST[ 'cs_replacement_' . $sb_id ]; |
| 1038 | if ( ! empty( $replacement ) ) { |
| 1039 | $data[ $sb_id ] = $replacement; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | self::set_post_meta( $post_id, $data ); |
| 1045 | } |
| 1046 | |
| 1047 | // ========== WPML support. |
| 1048 | /** |
| 1049 | * Updates the WPML string register with the current sidebar string so the |
| 1050 | * user can translate the sidebar details using the WPML string translation. |
| 1051 | * |
| 1052 | * @since 2.0.9.7 |
| 1053 | * @param array $custom_sidebars List of the custom sidebars. |
| 1054 | */ |
| 1055 | static protected function wpml_update( $custom_sidebars ) { |
| 1056 | if ( ! function_exists( 'icl_register_string' ) ) { return false; } |
| 1057 | $theme_sidebars = self::get_sidebars(); |
| 1058 | // This is used to identify the sidebar-translations by WPML. |
| 1059 | $context = 'Sidebar'; |
| 1060 | // First do the theme sidebars, so they will be displayed in the |
| 1061 | // *bottom* of the translations list. |
| 1062 | foreach ( $theme_sidebars as $fields ) { |
| 1063 | self::wpml_update_field( $context, $fields['id'] . '-name', @$fields['name'], false ); |
| 1064 | self::wpml_update_field( $context, $fields['id'] . '-description', @$fields['description'], false ); |
| 1065 | } |
| 1066 | foreach ( $custom_sidebars as $fields ) { |
| 1067 | $name = isset( $fields['name_lang'] ) ? $fields['name_lang'] : $fields['name']; |
| 1068 | $description = isset( $fields['description_lang'] ) ? $fields['description_lang'] : $fields['description']; |
| 1069 | self::wpml_update_field( $context, $fields['id'] . '-name', $name, true ); |
| 1070 | self::wpml_update_field( $context, $fields['id'] . '-description', $description, true ); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | /** |
| 1075 | * Updates the WPML string register for a single field. |
| 1076 | * |
| 1077 | * @since 2.0.9.7 |
| 1078 | * @param string $context |
| 1079 | * @param string $field |
| 1080 | * @param string $value |
| 1081 | * @param bool $update_string If false then the translation will only be |
| 1082 | * registered but not updated. |
| 1083 | */ |
| 1084 | static protected function wpml_update_field( $context, $field, $value, $update_string = true ) { |
| 1085 | global $sitepress, $sitepress_settings; |
| 1086 | if ( empty( $sitepress ) || empty( $sitepress_settings ) ) { return false; } |
| 1087 | if ( ! function_exists( 'icl_t' ) ) { return false; } |
| 1088 | if ( ! icl_st_is_registered_string( $context, $field ) ) { |
| 1089 | // Register the field if it does not exist. |
| 1090 | icl_register_string( $context, $field, $value, false ); |
| 1091 | $active_languages = $sitepress->get_active_languages(); |
| 1092 | foreach ( $active_languages as $lang => $data ) { |
| 1093 | icl_update_string_translation( $field, $lang, $value, ICL_STRING_TRANSLATION_COMPLETE ); |
| 1094 | } |
| 1095 | $default_language = ! empty( $sitepress_settings['st']['strings_language'] ) |
| 1096 | ? $sitepress_settings['st']['strings_language'] |
| 1097 | : $sitepress->get_default_language(); |
| 1098 | icl_update_string_translation( $field, $default_language, $value, ICL_STRING_TRANSLATION_COMPLETE ); |
| 1099 | } else if ( $update_string ) { |
| 1100 | // Add translation. |
| 1101 | if ( defined( 'DOING_AJAX' ) ) { |
| 1102 | $current_language = $sitepress->get_language_cookie(); |
| 1103 | } else { |
| 1104 | $current_language = $sitepress->get_current_language(); |
| 1105 | } |
| 1106 | icl_update_string_translation( $field, $current_language, $value, ICL_STRING_TRANSLATION_COMPLETE ); |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * Returns boolean true, when site is currently using the default language. |
| 1112 | * |
| 1113 | * @since 2.0.9.7 |
| 1114 | * @return bool |
| 1115 | */ |
| 1116 | static protected function wpml_is_default_lang() { |
| 1117 | global $sitepress, $sitepress_settings; |
| 1118 | if ( empty( $sitepress ) || empty( $sitepress_settings ) ) { return true; } |
| 1119 | if ( ! function_exists( 'icl_t' ) ) { return true; } |
| 1120 | if ( defined( 'DOING_AJAX' ) ) { |
| 1121 | $current_language = $sitepress->get_language_cookie(); |
| 1122 | } else { |
| 1123 | $current_language = $sitepress->get_current_language(); |
| 1124 | } |
| 1125 | $default_language = ! empty( $sitepress_settings['st']['strings_language'] ) |
| 1126 | ? $sitepress_settings['st']['strings_language'] |
| 1127 | : $sitepress->get_default_language(); |
| 1128 | return $default_language == $current_language; |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * Translates the text inside the specified sidebar object. |
| 1133 | * |
| 1134 | * @since 2.0.9.7 |
| 1135 | * @param array $sidebar Sidebar object. |
| 1136 | * @return array Translated sidebar object. |
| 1137 | */ |
| 1138 | static protected function wpml_translate( $sidebar ) { |
| 1139 | if ( ! function_exists( 'icl_t' ) ) { return $sidebar; } |
| 1140 | $context = 'Sidebar'; |
| 1141 | // Translate the name and description. |
| 1142 | // Note: When changing a translation the icl_t() function will not |
| 1143 | // return the updated value due to caching. |
| 1144 | if ( isset( $sidebar['name_lang'] ) ) { |
| 1145 | $sidebar['name'] = $sidebar['name_lang']; |
| 1146 | } else { |
| 1147 | $sidebar['name'] = icl_t( $context, $sidebar['id'] . '-name', $sidebar['name'] ); |
| 1148 | } |
| 1149 | if ( isset( $sidebar['description_lang'] ) ) { |
| 1150 | $sidebar['description'] = $sidebar['description_lang']; |
| 1151 | } else { |
| 1152 | $sidebar['description'] = icl_t( $context, $sidebar['id'] . '-description', $sidebar['description'] ); |
| 1153 | } |
| 1154 | return $sidebar; |
| 1155 | } |
| 1156 | |
| 1157 | // |
| 1158 | // ========== Custom column an Quick-Edit fields for post list. |
| 1159 | // http://shibashake.com/wordpress-theme/expand-the-wordpress-quick-edit-menu |
| 1160 | // |
| 1161 | /** |
| 1162 | * Adds a custom column to post-types that support custom sidebars. |
| 1163 | * |
| 1164 | * @since 2.0.9.7 |
| 1165 | * @param array $columns Column list. |
| 1166 | * @return array Modified column list. |
| 1167 | */ |
| 1168 | public function post_columns( $columns ) { |
| 1169 | // This column is added. |
| 1170 | $insert = array( |
| 1171 | 'cs_replacement' => _x( 'Custom Sidebars', 'Column name on entries list.', 'custom-sidebars' ), |
| 1172 | ); |
| 1173 | // Column is added after column 'title'. |
| 1174 | $insert_after = 'title'; |
| 1175 | $pos = array_search( $insert_after, array_keys( $columns ) ) + 1; |
| 1176 | $columns = array_merge( |
| 1177 | array_slice( $columns, 0, $pos ), |
| 1178 | $insert, |
| 1179 | array_slice( $columns, $pos ) |
| 1180 | ); |
| 1181 | return $columns; |
| 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * Display values in the custom column. |
| 1186 | * |
| 1187 | * @since 2.0.9.7 |
| 1188 | * @param string $column_name Column-Key defined in post_columns above. |
| 1189 | * @param int $post_id Post-ID |
| 1190 | */ |
| 1191 | public function post_column_content( $column_name, $post_id ) { |
| 1192 | switch ( $column_name ) { |
| 1193 | case 'cs_replacement': |
| 1194 | $this->print_sidebars_form( $post_id, 'col-sidebars' ); |
| 1195 | break; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * Adds a custom field to the quick-edit box to select custom columns. |
| 1201 | * |
| 1202 | * @since 2.0.9.7 |
| 1203 | * @param string $column_name Column-Key defined in post_columns above. |
| 1204 | * @param string $post_type Post-type that is currently edited. |
| 1205 | */ |
| 1206 | public function post_quick_edit( $column_name, $post_type ) { |
| 1207 | if ( ! self::supported_post_type( $post_type ) ) { return false; } |
| 1208 | switch ( $column_name ) { |
| 1209 | case 'cs_replacement': |
| 1210 | $this->print_metabox_quick(); |
| 1211 | break; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | /** |
| 1216 | * Javascript to set the values of the quick-edit form. |
| 1217 | * |
| 1218 | * Note: There is only 1 quick-edit form on the page. The form is displayed |
| 1219 | * when the user clicks the quick edit action; all fields are then populated |
| 1220 | * with values of the corresponding post. |
| 1221 | * |
| 1222 | * @since 2.0.9.7 |
| 1223 | */ |
| 1224 | public function post_quick_edit_js() { |
| 1225 | global $current_screen; |
| 1226 | if ( ( $current_screen->base != 'edit' ) ) { return false; } |
| 1227 | if ( ! self::supported_post_type( $current_screen->post_type ) ) { return false; } |
| 1228 | ?> |
| 1229 | <script type="text/javascript"> |
| 1230 | <!-- |
| 1231 | jQuery(function() { |
| 1232 | // we create a copy of the WP inline edit post function |
| 1233 | var wp_inline_edit = inlineEditPost.edit; |
| 1234 | // and then we overwrite the function with our own code |
| 1235 | inlineEditPost.edit = function( id ) { |
| 1236 | // "call" the original WP edit function |
| 1237 | // we don't want to leave WordPress hanging |
| 1238 | wp_inline_edit.apply( this, arguments ); |
| 1239 | // now we take care of our business. |
| 1240 | // get the post ID |
| 1241 | var post_id = 0; |
| 1242 | if ( typeof( id ) == 'object' ) { |
| 1243 | post_id = parseInt( this.getId( id ) ); |
| 1244 | } |
| 1245 | if ( post_id > 0 ) { |
| 1246 | // define the edit row |
| 1247 | var edit_row = jQuery( '#edit-' + post_id ); |
| 1248 | var post_row = jQuery( '#post-' + post_id ); |
| 1249 | // Our custom column |
| 1250 | var sidebar_col = post_row.find( '.cs_replacement' ); |
| 1251 | sidebar_col.find( '[data-sidebar]' ).each(function() { |
| 1252 | var key = jQuery( this ).attr( 'data-sidebar' ), |
| 1253 | val = jQuery( this ).attr( 'data-replaced' ), |
| 1254 | hide = 'yes' === jQuery( this ).attr( 'data-cshide' ); |
| 1255 | if ( hide ) { |
| 1256 | edit_row.find( '.cs-replacement-field.' + key ).val( val ).parent().hide(); |
| 1257 | } else { |
| 1258 | edit_row.find( '.cs-replacement-field.' + key ).val( val ).parent().show(); |
| 1259 | } |
| 1260 | }); |
| 1261 | } |
| 1262 | }; |
| 1263 | }); |
| 1264 | //--> |
| 1265 | </script> |
| 1266 | <?php |
| 1267 | } |
| 1268 | |
| 1269 | /** |
| 1270 | * Hide column "Custom Sidebars" by default. |
| 1271 | * |
| 1272 | * @since 3.0.5 |
| 1273 | * |
| 1274 | * @param array $hidden Array of hidden columns. |
| 1275 | * @param WP_Screen $screen Current WP screen. |
| 1276 | * @return array $hidden |
| 1277 | */ |
| 1278 | public function default_hidden_columns( $hidden, $screen ) { |
| 1279 | if ( is_object( $screen ) && isset( $screen->post_type ) && 'post' == $screen->post_type ) { |
| 1280 | $hidden[] = 'cs_replacement'; |
| 1281 | } |
| 1282 | return $hidden; |
| 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * Adds a custom field to the bulk-edit box to select custom columns. |
| 1287 | * |
| 1288 | * @since 3.0.8 |
| 1289 | * @param string $column_name Column-Key defined in post_columns above. |
| 1290 | * @param string $post_type Post-type that is currently edited. |
| 1291 | */ |
| 1292 | public function post_bulk_edit( $column_name, $post_type ) { |
| 1293 | if ( ! self::supported_post_type( $post_type ) ) { return false; } |
| 1294 | switch ( $column_name ) { |
| 1295 | case 'cs_replacement': |
| 1296 | $this->print_metabox_bulk(); |
| 1297 | break; |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | /** |
| 1302 | * Renders the sidebar-fields inside the bulk-edit form. |
| 1303 | * |
| 1304 | * @since 3.0.8 |
| 1305 | */ |
| 1306 | public function print_metabox_bulk() { |
| 1307 | $this->print_sidebars_form( 0, 'bulk-edit' ); |
| 1308 | } |
| 1309 | |
| 1310 | /** |
| 1311 | * Bulk Edit save |
| 1312 | * |
| 1313 | * @since 3.0.8 |
| 1314 | */ |
| 1315 | public function bulk_edit_save( $post_id ) { |
| 1316 | if ( ! isset( $_REQUEST['custom-sidebars-editor-bulk-edit'] ) ) { |
| 1317 | return; |
| 1318 | } |
| 1319 | if ( ! wp_verify_nonce( $_REQUEST['custom-sidebars-editor-bulk-edit'], 'bulk-edit-cs' ) ) { |
| 1320 | return; |
| 1321 | } |
| 1322 | if ( null == $this->modifiable ) { |
| 1323 | $this->modifiable = CustomSidebars::get_options( 'modifiable' ); |
| 1324 | } |
| 1325 | if ( empty( $this->modifiable ) ) { |
| 1326 | return; |
| 1327 | } |
| 1328 | $update = false; |
| 1329 | $data = CustomSidebars::get_post_meta( $post_id ); |
| 1330 | foreach ( $this->modifiable as $key ) { |
| 1331 | $k = sprintf( 'cs_replacement_%s', $key ); |
| 1332 | $value = isset( $_REQUEST[ $k ] )? $_REQUEST[ $k ]:'-'; |
| 1333 | if ( '-' != $value ) { |
| 1334 | $update = true; |
| 1335 | $data[ $key ] = $value; |
| 1336 | } |
| 1337 | } |
| 1338 | if ( ! $update ) { |
| 1339 | return; |
| 1340 | } |
| 1341 | self::set_post_meta( $post_id, $data ); |
| 1342 | } |
| 1343 | |
| 1344 | /** |
| 1345 | * Add capabilities for options on widgets.php |
| 1346 | * |
| 1347 | * @param string $screen_settings Screen settings. |
| 1348 | * @param WP_Screen $screen WP_Screen object. |
| 1349 | */ |
| 1350 | public function add_capabilities_select_box( $screen_settings, $screen ) { |
| 1351 | if ( 'widgets' == $screen->base && current_user_can( 'manage_options' ) ) { |
| 1352 | $allowed = get_option( $this->metabox_roles_name, 'any' ); |
| 1353 | $roles = get_editable_roles(); |
| 1354 | $screen_settings .= '<fieldset class="metabox-prefs cs-roles">'; |
| 1355 | $screen_settings .= wp_nonce_field( $this->metabox_roles_name, $this->metabox_roles_name, false, false ); |
| 1356 | $screen_settings .= sprintf( '<legend>%s</legend>', __( 'Custom sidebars configuration is allowed for:', 'custom-sidebars' ) ); |
| 1357 | foreach ( $roles as $role => $data ) { |
| 1358 | if ( isset( $data['capabilities'][ self::$cap_required ] ) && $data['capabilities'][ self::$cap_required ] ) { |
| 1359 | $checked = false; |
| 1360 | if ( is_string( $allowed ) && 'any' == $allowed ) { |
| 1361 | $checked = true; |
| 1362 | } else if ( is_array( $allowed ) && in_array( $role, $allowed ) ) { |
| 1363 | $checked = true; |
| 1364 | } |
| 1365 | $screen_settings .= sprintf( |
| 1366 | '<label><input type="checkbox" name="cs-roles[]" value="%s" %s /> %s</label>', |
| 1367 | esc_attr( $role ), |
| 1368 | checked( $checked, true, false ), |
| 1369 | esc_html( $data['name'] ) |
| 1370 | ); |
| 1371 | } |
| 1372 | } |
| 1373 | $screen_settings .= '</fieldset>'; |
| 1374 | /** |
| 1375 | * allow author to edit custom sidebars |
| 1376 | */ |
| 1377 | $screen_settings .= '<fieldset class="metabox-prefs cs-allow-author">'; |
| 1378 | $screen_settings .= wp_nonce_field( $this->allow_author_name, $this->allow_author_name, false, false ); |
| 1379 | $screen_settings .= sprintf( '<legend>%s</legend>', __( 'Allowed to replace sidebars:', 'custom-sidebars' ) ); |
| 1380 | $checked = get_option( $this->allow_author_name, false ); |
| 1381 | $screen_settings .= sprintf( |
| 1382 | '<label><input type="checkbox" name="cs-allow-author" value="%s" %s /> %s</label>', |
| 1383 | esc_attr( $role ), |
| 1384 | checked( $checked, true, false ), |
| 1385 | esc_html__( 'Allow entry author to change sidebars', 'custom-sidebars' ) |
| 1386 | ); |
| 1387 | $screen_settings .= '</fieldset>'; |
| 1388 | /** |
| 1389 | * Custom taxonomies |
| 1390 | */ |
| 1391 | $taxonomies = CustomSidebars::get_custom_taxonomies(); |
| 1392 | if ( ! empty( $taxonomies ) ) { |
| 1393 | $allowed = get_option( $this->custom_taxonomies_name, array() ); |
| 1394 | $screen_settings .= '<fieldset class="metabox-prefs cs-custom-taxonomies">'; |
| 1395 | $screen_settings .= wp_nonce_field( $this->custom_taxonomies_name, $this->custom_taxonomies_name, false, false ); |
| 1396 | $screen_settings .= sprintf( '<legend>%s</legend>', __( 'Allow Custom Taxonomies in Sidebar Location:', 'custom-sidebars' ) ); |
| 1397 | foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) { |
| 1398 | $checked = in_array( $taxonomy_slug, $allowed ); |
| 1399 | $screen_settings .= sprintf( |
| 1400 | '<label><input type="checkbox" name="cs-custom-taxonomy[]" value="%s" %s /> %s</label>', |
| 1401 | esc_attr( $taxonomy_slug ), |
| 1402 | checked( $checked, true, false ), |
| 1403 | esc_html( $taxonomy->label ) |
| 1404 | ); |
| 1405 | } |
| 1406 | $screen_settings .= '</fieldset>'; |
| 1407 | $screen_settings .= sprintf( '<p class="description">%s</p>', __( 'After turn on any Custom Taxonomy you need to reload this screen to be able choose it in Sidebar Location.', 'custom-sidebars' ) ); |
| 1408 | } |
| 1409 | } |
| 1410 | return $screen_settings; |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * Update capabilities select box |
| 1415 | * |
| 1416 | * @since 3.0.9 |
| 1417 | */ |
| 1418 | public function update_custom_sidebars_metabox_roles() { |
| 1419 | $this->update_option_field( $this->metabox_roles_name ); |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * Update custom taxoniomies select box |
| 1424 | * |
| 1425 | * @since 3.1.4 |
| 1426 | */ |
| 1427 | public function update_custom_sidebars_metabox_custom_taxonomies() { |
| 1428 | $this->update_option_field( $this->custom_taxonomies_name ); |
| 1429 | } |
| 1430 | |
| 1431 | /** |
| 1432 | * Helper for update metabox functions. |
| 1433 | * |
| 1434 | * @since 3.1.4 |
| 1435 | */ |
| 1436 | private function update_option_field( $name ) { |
| 1437 | /** |
| 1438 | * check required data |
| 1439 | */ |
| 1440 | if ( ! isset( $_REQUEST['_wpnonce'] ) || ! isset( $_REQUEST['fields'] ) ) { |
| 1441 | wp_send_json_error(); |
| 1442 | } |
| 1443 | /** |
| 1444 | * check nonce value |
| 1445 | */ |
| 1446 | if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], $name ) ) { |
| 1447 | wp_send_json_error(); |
| 1448 | } |
| 1449 | $value = array(); |
| 1450 | foreach ( $_REQUEST['fields'] as $role => $status ) { |
| 1451 | if ( 'true' == $status ) { |
| 1452 | $value[] = $role; |
| 1453 | } |
| 1454 | } |
| 1455 | $status = add_option( $name, $value, '', 'no' ); |
| 1456 | if ( ! $status ) { |
| 1457 | update_option( $name, $value ); |
| 1458 | } |
| 1459 | wp_send_json_success(); |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * Update entry author is allowed to change custom sidebars. |
| 1464 | * |
| 1465 | * @since 3.1.5 |
| 1466 | */ |
| 1467 | public function update_custom_sidebars_allow_author() { |
| 1468 | /** |
| 1469 | * check required data |
| 1470 | */ |
| 1471 | if ( ! isset( $_REQUEST['_wpnonce'] ) ) { |
| 1472 | wp_send_json_error(); |
| 1473 | } |
| 1474 | /** |
| 1475 | * check nonce value |
| 1476 | */ |
| 1477 | if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], $this->allow_author_name ) ) { |
| 1478 | wp_send_json_error(); |
| 1479 | } |
| 1480 | $value = isset( $_REQUEST['value'] )? 'true' === $_REQUEST['value'] : false; |
| 1481 | $status = add_option( $this->allow_author_name, $value, '', 'no' ); |
| 1482 | if ( ! $status ) { |
| 1483 | update_option( $this->allow_author_name, $value ); |
| 1484 | } |
| 1485 | wp_send_json_success(); |
| 1486 | } |
| 1487 | /** |
| 1488 | * Check ability to save sidebars |
| 1489 | * |
| 1490 | * @since 3.0.9 |
| 1491 | */ |
| 1492 | public function current_user_can_update_custom_sidebars() { |
| 1493 | $allowed = get_option( $this->metabox_roles_name, 'any' ); |
| 1494 | if ( is_string( $allowed ) && 'any' == $allowed ) { |
| 1495 | return true; |
| 1496 | } else { |
| 1497 | $current_user = wp_get_current_user(); |
| 1498 | $current_user_roles = (array) $current_user->roles; |
| 1499 | foreach ( $allowed as $role ) { |
| 1500 | if ( in_array( $role, $current_user_roles ) ) { |
| 1501 | return true; |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | return false; |
| 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * Get allowed taxoniomies. |
| 1510 | * |
| 1511 | * @since 3.1.4 |
| 1512 | */ |
| 1513 | public function get_allowed_custom_taxonmies() { |
| 1514 | $value = get_option( $this->custom_taxonomies_name ); |
| 1515 | if ( empty( $value ) || ! is_array( $value ) ) { |
| 1516 | return array(); |
| 1517 | } |
| 1518 | return $value; |
| 1519 | } |
| 1520 | |
| 1521 | /** |
| 1522 | * Check author ability to change sidebars during entry edit |
| 1523 | * |
| 1524 | * @since 3.1.5 |
| 1525 | */ |
| 1526 | private function check_author_ability_to_replace() { |
| 1527 | global $post; |
| 1528 | if ( current_user_can( self::$cap_required ) ) { |
| 1529 | return true; |
| 1530 | } |
| 1531 | $checked = get_option( $this->allow_author_name, false ); |
| 1532 | if ( $checked ) { |
| 1533 | if ( current_user_can( 'edit_posts', $post->ID ) ) { |
| 1534 | return true; |
| 1535 | } |
| 1536 | } |
| 1537 | return false; |
| 1538 | } |
| 1539 | }; |
| 1540 |