[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress Types Admin Class. 4 * 5 * @package BuddyPress 6 * @subpackage CoreAdministration 7 * @since 7.0.0 8 */ 9 10 // Exit if accessed directly. 11 if ( ! defined( 'ABSPATH' ) ) { 12 exit; 13 } 14 15 if ( ! class_exists( 'BP_Admin_Types' ) ) : 16 17 /** 18 * Load BuddyPress Types admin area. 19 * 20 * @since 7.O.0 21 */ 22 class BP_Admin_Types { 23 /** 24 * Current BuddyPress taxonomy. 25 * 26 * @since 7.0.0 27 * @var string 28 */ 29 public $taxonomy = ''; 30 31 /** 32 * All registered BuddyPress taxonomies. 33 * 34 * @since 7.0.0 35 * @var array() 36 */ 37 public $taxonomies = array(); 38 39 /** 40 * Current screen ID. 41 * 42 * @since 7.0.0 43 * @var string 44 */ 45 public $screen_id = ''; 46 47 /** 48 * The main BuddyPress Types admin loader. 49 * 50 * @since 7.0.0 51 */ 52 public function __construct() { 53 $this->setup_globals(); 54 55 if ( $this->taxonomy && $this->screen_id ) { 56 $this->includes(); 57 $this->setup_hooks(); 58 59 if ( isset( $_POST['action'] ) || isset( $_GET['action'] ) ) { 60 if ( isset( $_GET['action'] ) ) { 61 $action = wp_unslash( $_GET['action'] ); 62 } else { 63 $action = wp_unslash( $_POST['action'] ); 64 } 65 66 $this->handle_action( $action ); 67 } 68 } 69 } 70 71 /** 72 * Register BP Types Admin. 73 * 74 * @since 7.0.0 75 * 76 * @return BP_Admin_Types 77 */ 78 public static function register_types_admin() { 79 if ( ! is_admin() ) { 80 return; 81 } 82 83 $bp = buddypress(); 84 85 if ( empty( $bp->core->types_admin ) ) { 86 $bp->core->types_admin = new self; 87 } 88 89 return $bp->core->types_admin; 90 } 91 92 /** 93 * Set the globals. 94 * 95 * @since 7.0.0 96 */ 97 private function setup_globals() { 98 $current_screen = get_current_screen(); 99 100 if ( isset( $current_screen->taxonomy ) && $current_screen->taxonomy ) { 101 $this->taxonomies = bp_get_default_taxonomies(); 102 103 if ( isset( $this->taxonomies[ $current_screen->taxonomy ] ) ) { 104 $this->taxonomy = $current_screen->taxonomy; 105 $this->screen_id = $current_screen->id; 106 } 107 } 108 } 109 110 /** 111 * Include Admin functions. 112 * 113 * @since 7.0.0 114 */ 115 private function includes() { 116 require plugin_dir_path( dirname( __FILE__ ) ) . 'admin/bp-core-admin-types.php'; 117 } 118 119 /** 120 * Set hooks. 121 * 122 * @since 7.0.0 123 */ 124 private function setup_hooks() { 125 // Actions. 126 add_action( 'admin_head-edit-tags.php', array( $this, 'screen_head' ) ); 127 add_action( 'admin_head-term.php', array( $this, 'screen_head' ) ); 128 add_action( 'bp_admin_enqueue_scripts', array( $this, 'screen_scripts' ) ); 129 add_action( "{$this->taxonomy}_add_form_fields", array( $this, 'add_form_fields' ), 10, 1 ); 130 add_action( "{$this->taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 ); 131 132 // Filters 133 add_filter( 'bp_core_admin_register_scripts', array( $this, 'register_scripts' ) ); 134 add_filter( "manage_{$this->screen_id}_columns", array( $this, 'column_headers' ), 10, 1 ); 135 add_filter( "manage_{$this->taxonomy}_custom_column", array( $this, 'column_contents' ), 10, 3 ); 136 add_filter( "{$this->taxonomy}_row_actions", array( $this, 'row_actions' ), 10, 2 ); 137 add_filter( "bulk_actions-{$this->screen_id}", '__return_empty_array', 10, 1 ); 138 } 139 140 /** 141 * Handle BP Type actions. 142 * 143 * @since 7.0.0 144 * 145 * @param string $action Required. The action to handle ('add-tag', 'editedtag' or 'delete' ). 146 */ 147 private function handle_action( $action ) { 148 $referer = wp_get_referer(); 149 150 // Adding a new type into the database. 151 if ( 'add-tag' === $action ) { 152 check_admin_referer( 'add-tag', '_wpnonce_add-tag' ); 153 154 $result = bp_core_admin_insert_type( $_POST ); 155 156 if ( is_wp_error( $result ) ) { 157 $referer = add_query_arg( 158 array_merge( 159 $result->get_error_data(), 160 array( 161 'error' => 1, 162 ) 163 ), 164 $referer 165 ); 166 167 wp_safe_redirect( $referer ); 168 exit; 169 } 170 171 wp_safe_redirect( add_query_arg( 'message', 2, $referer ) ); 172 exit; 173 174 // Updating an existing type intot the Database. 175 } elseif ( 'editedtag' === $action ) { 176 $args = $_POST; 177 $args['type_term_id'] = 0; 178 unset( $args['tag_ID'] ); 179 180 if ( isset( $_POST['tag_ID'] ) ) { 181 $args['type_term_id'] = $_POST['tag_ID']; 182 } 183 184 if ( isset( $_POST['taxonomy'] ) ) { 185 $args['taxonomy'] = $_POST['taxonomy']; 186 } 187 188 check_admin_referer( 'update-tag_' . $args['type_term_id'] ); 189 190 $result = bp_core_admin_update_type( $args ); 191 192 if ( is_wp_error( $result ) ) { 193 $referer = add_query_arg( 194 array_merge( 195 $result->get_error_data(), 196 array( 197 'error' => 1, 198 ) 199 ), 200 $referer 201 ); 202 203 wp_safe_redirect( $referer ); 204 exit; 205 } 206 207 wp_safe_redirect( add_query_arg( 'message', 4, $referer ) ); 208 exit; 209 210 // Deletes a type. 211 } elseif ( 'delete' === $action ) { 212 $args = $_GET; 213 $args['type_term_id'] = 0; 214 unset( $args['tag_ID'] ); 215 216 if ( isset( $_GET['tag_ID'] ) ) { 217 $args['type_term_id'] = $_GET['tag_ID']; 218 } 219 220 if ( isset( $_GET['taxonomy'] ) ) { 221 $args['taxonomy'] = $_GET['taxonomy']; 222 } 223 224 check_admin_referer( 'delete-tag_' . $args['type_term_id'] ); 225 $referer = remove_query_arg( array( 'action', 'tag_ID', '_wpnonce' ), $referer ); 226 227 // Delete the type. 228 $result = bp_core_admin_delete_type( $args ); 229 230 if ( is_wp_error( $result ) ) { 231 $referer = add_query_arg( 232 array_merge( 233 $result->get_error_data(), 234 array( 235 'error' => 1, 236 ) 237 ), 238 $referer 239 ); 240 241 wp_safe_redirect( $referer ); 242 exit; 243 } 244 245 wp_safe_redirect( add_query_arg( 'message', 9, $referer ) ); 246 exit; 247 } 248 } 249 250 /** 251 * Override the Admin parent file to highlight the right menu. 252 * 253 * @since 7.0.0 254 */ 255 public function screen_head() { 256 global $parent_file; 257 258 if ( 'members' === $this->taxonomies[ $this->taxonomy ]['component'] ) { 259 $parent_file = 'users.php'; 260 } else { 261 $parent_file = 'bp-' . $this->taxonomies[ $this->taxonomy ]['component']; 262 } 263 } 264 265 /** 266 * Registers script. 267 * 268 * @since 7.0.0 269 */ 270 public function register_scripts( $scripts = array() ) { 271 // Neutralize WordPress Taxonomy scripts. 272 wp_dequeue_script( 'admin-tags' ); 273 wp_dequeue_script( 'inline-edit-tax' ); 274 275 // Adapt some styles. 276 wp_add_inline_style( 277 'common', 278 '.form-field:not(.bp-types-form), .term-bp_type_directory_slug-wrap:not(.bp-set-directory-slug), .edit-tag-actions #delete-link { display: none; }' 279 ); 280 281 // Register the Types admin script. 282 return array_merge( 283 $scripts, 284 array( 285 'bp-admin-types' => array( 286 'file' => sprintf( 287 '%1$sadmin/js/types-admin%2$s.js', 288 plugin_dir_url( dirname( __FILE__ ) ), 289 bp_core_get_minified_asset_suffix() 290 ), 291 'dependencies' => array(), 292 'footer' => true, 293 ), 294 ) 295 ); 296 } 297 298 /** 299 * Enqueues script. 300 * 301 * @since 7.0.0 302 */ 303 public function screen_scripts() { 304 wp_enqueue_script( 'bp-admin-types' ); 305 } 306 307 /** 308 * Outputs the BP type add form. 309 * 310 * @since 7.0.0 311 * 312 * @param string $taxonomy The type taxonomy name. 313 * @param null|object $type The type object, `null` if not passed to the method. 314 */ 315 public function add_form_fields( $taxonomy = '', $type = null ) { 316 $taxonomy_object = get_taxonomy( $taxonomy ); 317 $labels = get_taxonomy_labels( $taxonomy_object ); 318 319 // Default values for the Type ID field. 320 $type_id_label = __( 'Type ID', 'buddypress' ); 321 $type_id_desc = __( 'Enter a lower-case string without spaces or special characters (used internally to identify the type).', 'buddypress' ); 322 323 if ( isset( $labels->bp_type_id_label ) && $labels->bp_type_id_label ) { 324 $type_id_label = $labels->bp_type_id_label; 325 } 326 327 if ( isset( $labels->bp_type_id_description ) && $labels->bp_type_id_description ) { 328 $type_id_desc = $labels->bp_type_id_description; 329 } 330 331 // Outputs the Type ID field. 332 if ( isset( $type->name ) ) { 333 printf( 334 '<tr class="form-field bp-types-form form-required term-bp_type_id-wrap"> 335 <th scope="row"><label for="bp_type_id">%1$s</label></th> 336 <td> 337 <input name="bp_type_id" id="bp_type_id" type="text" value="%2$s" size="40" disabled="disabled"> 338 </td> 339 </tr>', 340 esc_html( $type_id_label ), 341 esc_attr( $type->name ), 342 esc_html( $type_id_desc ) 343 ); 344 } else { 345 printf( 346 '<div class="form-field bp-types-form form-required term-bp_type_id-wrap"> 347 <label for="bp_type_id">%1$s</label> 348 <input name="bp_type_id" id="bp_type_id" type="text" value="" size="40" aria-required="true"> 349 <p>%2$s</p> 350 </div>', 351 esc_html( $type_id_label ), 352 esc_html( $type_id_desc ) 353 ); 354 } 355 356 // Gets the Type's metadata. 357 $metafields = get_registered_meta_keys( 'term', $taxonomy ); 358 359 foreach ( $metafields as $meta_key => $meta_schema ) { 360 if ( ! isset( $labels->{ $meta_key } ) || ! $labels->{ $meta_key } ) { 361 _doing_it_wrong( 362 __METHOD__, 363 __( 'Type metadata labels need to be set into the labels argument when registering your taxonomy using the meta key as the label’s key.', 'buddypress' ) 364 . ' ' . 365 sprintf( 366 /* translators: %s is the name of the Type meta key */ 367 __( 'As a result, the form elements for the "%s" meta key cannot be displayed', 'buddypress' ), $meta_key ), 368 '7.0.0' 369 ); 370 continue; 371 } 372 373 $type_key = str_replace( 'bp_type_', '', $meta_key ); 374 375 if ( 'string' === $meta_schema['type'] ) { 376 if ( isset( $type->name ) ) { 377 $type_prop_value = null; 378 if ( in_array( $type_key, array( 'name', 'singular_name' ), true ) ) { 379 if ( isset( $type->labels[ $type_key ] ) ) { 380 $type_prop_value = $type->labels[ $type_key ]; 381 } 382 383 } elseif ( isset( $type->{$type_key} ) ) { 384 $type_prop_value = $type->{$type_key}; 385 } 386 387 printf( 388 '<tr class="form-field bp-types-form form-required term-%1$s-wrap"> 389 <th scope="row"><label for="%1$s">%2$s</label></th> 390 <td> 391 <input name="%1$s" id="%1$s" type="text" value="%3$s" size="40" aria-required="true"> 392 <p class="description">%4$s</p> 393 </td> 394 </tr>', 395 esc_attr( $meta_key ), 396 esc_html( $labels->{ $meta_key } ), 397 esc_attr( $type_prop_value ), 398 esc_html( $meta_schema['description'] ) 399 ); 400 401 } else { 402 printf( 403 '<div class="form-field bp-types-form form-required term-%1$s-wrap"> 404 <label for="%1$s">%2$s</label> 405 <input name="%1$s" id="%1$s" type="text" value="" size="40"> 406 <p>%3$s</p> 407 </div>', 408 esc_attr( $meta_key ), 409 esc_html( $labels->{ $meta_key } ), 410 esc_html( $meta_schema['description'] ) 411 ); 412 } 413 } else { 414 if ( isset( $type->name ) ) { 415 $checked = ''; 416 if ( isset( $type->{$type_key} ) && true === (bool) $type->{$type_key} ) { 417 $checked = ' checked="checked"'; 418 } 419 420 printf( 421 '<tr class="form-field bp-types-form term-%1$s-wrap"> 422 <th scope="row"><label for="%1$s">%2$s</label></th> 423 <td> 424 <input name="%1$s" id="%1$s" type="checkbox" value="1"%3$s> %4$s 425 <p class="description">%5$s</p> 426 </td> 427 </tr>', 428 esc_attr( $meta_key ), 429 esc_html( $labels->{ $meta_key } ), 430 $checked, 431 esc_html__( 'Yes', 'buddypress' ), 432 esc_html( $meta_schema['description'] ) 433 ); 434 } else { 435 printf( 436 '<div class="form-field bp-types-form term-%1$s-wrap"> 437 <label for="%1$s"> 438 <input name="%1$s" id="%1$s" type="checkbox" value="1"> %2$s 439 </label> 440 <p>%3$s</p> 441 </div>', 442 esc_attr( $meta_key ), 443 esc_html( $labels->{ $meta_key } ), 444 esc_html( $meta_schema['description'] ) 445 ); 446 } 447 } 448 } 449 } 450 451 /** 452 * Outputs the BP type edit form. 453 * 454 * @since 7.0.0 455 * 456 * @param WP_Term $term The term object for the BP Type. 457 * @param string $taxonomy The type taxonomy name. 458 * @return string HTML Output. 459 */ 460 public function edit_form_fields( $term = null, $taxonomy = '' ) { 461 if ( ! isset( $term->name ) || ! $term->name || ! $taxonomy ) { 462 return; 463 } 464 465 $type = new stdClass(); 466 $type->name = $term->name; 467 $type->labels = array(); 468 $metadatas = get_metadata( 'term', $term->term_id ); 469 470 foreach ( $metadatas as $meta_key => $meta_values ) { 471 $meta_value = reset( $meta_values ); 472 $type_key = str_replace( 'bp_type_', '', $meta_key ); 473 474 if ( in_array( $type_key, array( 'name', 'singular_name' ), true ) ) { 475 $type->labels[ $type_key ] = $meta_value; 476 } else { 477 $type->{$type_key} = $meta_value; 478 } 479 } 480 481 return $this->add_form_fields( $taxonomy, $type ); 482 } 483 484 /** 485 * Filters the terms list table column headers to customize them for BuddyPress Types. 486 * 487 * @since 7.0.0 488 * 489 * @param array $column_headers The column header labels keyed by column ID. 490 * @return array The column header labels keyed by column ID. 491 */ 492 public function column_headers( $column_headers = array() ) { 493 if ( isset( $column_headers['name'] ) ) { 494 $column_headers['name'] = __( 'Type ID', 'buddypress' ); 495 } 496 497 unset( $column_headers['cb'], $column_headers['description'], $column_headers['posts'] ); 498 499 $column_headers['plural_name'] = __( 'Name', 'buddypress' ); 500 $column_headers['counts'] = _x( 'Count', 'Number/count of types', 'buddypress' ); 501 502 return $column_headers; 503 } 504 505 /** 506 * Sets the content for the Plural name & Counts columns. 507 * 508 * @since 7.0.0 509 * 510 * @param string $string Blank string. 511 * @param string $column_name Name of the column. 512 * @param int $type_id The type's term ID. 513 * @return string The Type Plural name. 514 */ 515 public function column_contents( $column_content = '', $column_name = '', $type_id = 0 ) { 516 if ( 'plural_name' !== $column_name && 'counts' !== $column_name || ! $type_id ) { 517 return $column_content; 518 } 519 520 // Set the Plural name column. 521 if ( 'plural_name' === $column_name ) { 522 $type_plural_name = get_term_meta( $type_id, 'bp_type_name', true ); 523 524 // Plural name meta is not set? Let's check register by code types! 525 if ( ! $type_plural_name ) { 526 $type_name = get_term_field( 'name', $type_id, $this->taxonomy ); 527 528 /** 529 * Filter here to set missing term meta for registered by code types. 530 * 531 * @see bp_set_registered_by_code_member_type_metadata() for an example of use. 532 * 533 * @since 7.0.0 534 * 535 * @param string $value Metadata for the BP Type. 536 */ 537 $metadata = apply_filters( "{$this->taxonomy}_set_registered_by_code_metada", array(), $type_name ); 538 539 if ( isset( $metadata['bp_type_name'] ) ) { 540 $type_plural_name = $metadata['bp_type_name']; 541 } 542 } 543 544 echo esc_html( $type_plural_name ); 545 546 // Set the Totals column. 547 } elseif ( 'counts' === $column_name ) { 548 global $parent_file; 549 $type = bp_get_term_by( 'id', $type_id, $this->taxonomy ); 550 if ( 0 === (int) $type->count ) { 551 return 0; 552 } 553 554 // Format the count. 555 $count = number_format_i18n( $type->count ); 556 557 $args = array( 558 str_replace( '_', '-', $this->taxonomy ) => $type->slug, 559 ); 560 561 $base_url = $parent_file; 562 if ( false === strpos( $parent_file, '.php' ) ) { 563 $base_url = add_query_arg( 'page', $parent_file, 'admin.php' ); 564 } 565 566 printf( 567 '<a href="%1$s">%2$s</a>', 568 esc_url( add_query_arg( $args, bp_get_admin_url( $base_url ) ) ), 569 esc_html( $count ) 570 ); 571 } 572 } 573 574 /** 575 * Customizes the Types Admin list table row actions. 576 * 577 * @since 7.0.0 578 * 579 * @param array $actions The table row actions. 580 * @param WP_Term $type The current BP Type for the row. 581 * @return array The table row actions for the current BP type. 582 */ 583 public function row_actions( $actions = array(), $type = null ) { 584 if ( ! isset( $type->taxonomy ) || ! $type->taxonomy ) { 585 return $actions; 586 } 587 588 /** 589 * Filter here to set the types "registered by code". 590 * 591 * @see bp_get_member_types_registered_by_code() for an example of use. 592 * 593 * @since 7.0.0 594 */ 595 $registered_by_code_types = apply_filters( "{$type->taxonomy}_registered_by_code", array() ); 596 597 // Types registered by code cannot be deleted as long as the custom registration code exists. 598 if ( isset( $registered_by_code_types[ $type->name ] ) ) { 599 unset( $actions['delete'] ); 600 } 601 602 // Inline edits are disabled for all types. 603 unset( $actions['inline hide-if-no-js'] ); 604 605 // Removes the post type query argument for the edit action. 606 if ( isset( $actions['edit'] ) ) { 607 $actions['edit'] = str_replace( '&post_type=post', '', $actions['edit'] ); 608 } 609 610 return $actions; 611 } 612 } 613 614 endif;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 9 01:01:43 2021 | Cross-referenced by PHPXref 0.7.1 |