[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-admin-types.php (source)

   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          if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 151              return;
 152          }
 153  
 154          // Adding a new type into the database.
 155          if ( 'add-tag' === $action ) {
 156              check_admin_referer( 'add-tag', '_wpnonce_add-tag' );
 157  
 158              $result = bp_core_admin_insert_type( $_POST );
 159  
 160              if ( is_wp_error( $result ) ) {
 161                  $referer = add_query_arg(
 162                      array_merge(
 163                          $result->get_error_data(),
 164                          array(
 165                              'error' => 1,
 166                          )
 167                      ),
 168                      $referer
 169                  );
 170  
 171                  wp_safe_redirect( $referer );
 172                  exit;
 173              }
 174  
 175              wp_safe_redirect( add_query_arg( 'message', 2, $referer ) );
 176              exit;
 177  
 178              // Updating an existing type intot the Database.
 179          } elseif ( 'editedtag' === $action ) {
 180              $args                 = $_POST;
 181              $args['type_term_id'] = 0;
 182              unset( $args['tag_ID'] );
 183  
 184              if ( isset( $_POST['tag_ID'] ) ) {
 185                  $args['type_term_id'] = $_POST['tag_ID'];
 186              }
 187  
 188              if ( isset( $_POST['taxonomy'] ) ) {
 189                  $args['taxonomy'] = $_POST['taxonomy'];
 190              }
 191  
 192              check_admin_referer( 'update-tag_' . $args['type_term_id'] );
 193  
 194              $result = bp_core_admin_update_type( $args );
 195  
 196              if ( is_wp_error( $result ) ) {
 197                  $referer = add_query_arg(
 198                      array_merge(
 199                          $result->get_error_data(),
 200                          array(
 201                              'error' => 1,
 202                          )
 203                      ),
 204                      $referer
 205                  );
 206  
 207                  wp_safe_redirect( $referer );
 208                  exit;
 209              }
 210  
 211              wp_safe_redirect( add_query_arg( 'message', 4, $referer ) );
 212              exit;
 213  
 214              // Deletes a type.
 215          } elseif ( 'delete' === $action ) {
 216              $args                 = $_GET;
 217              $args['type_term_id'] = 0;
 218              unset( $args['tag_ID'] );
 219  
 220              if ( isset( $_GET['tag_ID'] ) ) {
 221                  $args['type_term_id'] = $_GET['tag_ID'];
 222              }
 223  
 224              if ( isset( $_GET['taxonomy'] ) ) {
 225                  $args['taxonomy'] = $_GET['taxonomy'];
 226              }
 227  
 228              check_admin_referer( 'delete-tag_' . $args['type_term_id'] );
 229              $referer = remove_query_arg( array( 'action', 'tag_ID', '_wpnonce' ), $referer );
 230  
 231              // Delete the type.
 232              $result = bp_core_admin_delete_type( $args );
 233  
 234              if ( is_wp_error( $result ) ) {
 235                  $referer = add_query_arg(
 236                      array_merge(
 237                          $result->get_error_data(),
 238                          array(
 239                              'error' => 1,
 240                          )
 241                      ),
 242                      $referer
 243                  );
 244  
 245                  wp_safe_redirect( $referer );
 246                  exit;
 247              }
 248  
 249              wp_safe_redirect( add_query_arg( 'message', 9, $referer ) );
 250              exit;
 251          }
 252      }
 253  
 254      /**
 255       * Override the Admin parent file to highlight the right menu.
 256       *
 257       * @since 7.0.0
 258       */
 259  	public function screen_head() {
 260          global $parent_file;
 261  
 262          if ( 'members' === $this->taxonomies[ $this->taxonomy ]['component'] ) {
 263              $parent_file = 'users.php';
 264          } else {
 265              $parent_file = 'bp-' . $this->taxonomies[ $this->taxonomy ]['component'];
 266          }
 267      }
 268  
 269      /**
 270       * Registers script.
 271       *
 272       * @since 7.0.0
 273       */
 274  	public function register_scripts( $scripts = array() ) {
 275          // Neutralize WordPress Taxonomy scripts.
 276          wp_dequeue_script( 'admin-tags' );
 277          wp_dequeue_script( 'inline-edit-tax' );
 278  
 279          // Adapt some styles.
 280          wp_add_inline_style(
 281              'common',
 282              '.form-field:not(.bp-types-form), .term-bp_type_directory_slug-wrap:not(.bp-set-directory-slug), .edit-tag-actions #delete-link { display: none; }'
 283          );
 284  
 285          // Register the Types admin script.
 286          return array_merge(
 287              $scripts,
 288              array(
 289                  'bp-admin-types' => array(
 290                      'file'         => sprintf(
 291                          '%1$sadmin/js/types-admin%2$s.js',
 292                          plugin_dir_url( dirname( __FILE__ ) ),
 293                          bp_core_get_minified_asset_suffix()
 294                      ),
 295                      'dependencies' => array(),
 296                      'footer'       => true,
 297                  ),
 298              )
 299          );
 300      }
 301  
 302      /**
 303       * Enqueues script.
 304       *
 305       * @since 7.0.0
 306       */
 307  	public function screen_scripts() {
 308          wp_enqueue_script( 'bp-admin-types' );
 309      }
 310  
 311      /**
 312       * Outputs the BP type add form.
 313       *
 314       * @since 7.0.0
 315       *
 316       * @param string      $taxonomy The type taxonomy name.
 317       * @param null|object $type     The type object, `null` if not passed to the method.
 318       */
 319  	public function add_form_fields( $taxonomy = '', $type = null ) {
 320          $taxonomy_object = get_taxonomy( $taxonomy );
 321          $labels          = get_taxonomy_labels( $taxonomy_object );
 322  
 323          // Default values for the Type ID field.
 324          $type_id_label   = __( 'Type ID', 'buddypress' );
 325          $type_id_desc    = __( 'Enter a lower-case string without spaces or special characters (used internally to identify the type).', 'buddypress' );
 326  
 327          if ( isset( $labels->bp_type_id_label ) && $labels->bp_type_id_label ) {
 328              $type_id_label = $labels->bp_type_id_label;
 329          }
 330  
 331          if ( isset( $labels->bp_type_id_description ) && $labels->bp_type_id_description ) {
 332              $type_id_desc = $labels->bp_type_id_description;
 333          }
 334  
 335          // Outputs the Type ID field.
 336          if ( isset( $type->name ) ) {
 337              printf(
 338                  '<tr class="form-field bp-types-form form-required term-bp_type_id-wrap">
 339                      <th scope="row"><label for="bp_type_id">%1$s</label></th>
 340                      <td>
 341                          <input name="bp_type_id" id="bp_type_id" type="text" value="%2$s" size="40" disabled="disabled">
 342                      </td>
 343                  </tr>',
 344                  esc_html( $type_id_label ),
 345                  esc_attr( $type->name ),
 346                  esc_html( $type_id_desc )
 347              );
 348          } else {
 349              printf(
 350                  '<div class="form-field bp-types-form form-required term-bp_type_id-wrap">
 351                      <label for="bp_type_id">%1$s</label>
 352                      <input name="bp_type_id" id="bp_type_id" type="text" value="" size="40" aria-required="true">
 353                      <p>%2$s</p>
 354                  </div>',
 355                  esc_html( $type_id_label ),
 356                  esc_html( $type_id_desc )
 357              );
 358          }
 359  
 360          // Gets the Type's metadata.
 361          $metafields = get_registered_meta_keys( 'term', $taxonomy );
 362  
 363          foreach ( $metafields as $meta_key => $meta_schema ) {
 364              if ( ! isset( $labels->{ $meta_key } ) || ! $labels->{ $meta_key } ) {
 365                  _doing_it_wrong(
 366                      __METHOD__,
 367                      __( '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' )
 368                      . ' ' .
 369                      sprintf(
 370                          /* translators: %s is the name of the Type meta key */
 371                          __( 'As a result, the form elements for the "%s" meta key cannot be displayed', 'buddypress' ), $meta_key ),
 372                      '7.0.0'
 373                  );
 374                  continue;
 375              }
 376  
 377              $type_key = str_replace( 'bp_type_', '', $meta_key );
 378  
 379              if ( 'string' === $meta_schema['type'] ) {
 380                  if ( isset( $type->name ) ) {
 381                      $type_prop_value = null;
 382                      if ( in_array( $type_key, array( 'name', 'singular_name' ), true ) ) {
 383                          if ( isset( $type->labels[ $type_key ] ) ) {
 384                              $type_prop_value = $type->labels[ $type_key ];
 385                          }
 386  
 387                      } elseif ( isset( $type->{$type_key} ) ) {
 388                          $type_prop_value = $type->{$type_key};
 389                      }
 390  
 391                      printf(
 392                          '<tr class="form-field bp-types-form form-required term-%1$s-wrap">
 393                              <th scope="row"><label for="%1$s">%2$s</label></th>
 394                              <td>
 395                                  <input name="%1$s" id="%1$s" type="text" value="%3$s" size="40" aria-required="true">
 396                                  <p class="description">%4$s</p>
 397                              </td>
 398                          </tr>',
 399                          esc_attr( $meta_key ),
 400                          esc_html( $labels->{ $meta_key } ),
 401                          esc_attr( $type_prop_value ),
 402                          esc_html( $meta_schema['description'] )
 403                      );
 404  
 405                  } else {
 406                      printf(
 407                          '<div class="form-field bp-types-form form-required term-%1$s-wrap">
 408                              <label for="%1$s">%2$s</label>
 409                              <input name="%1$s" id="%1$s" type="text" value="" size="40">
 410                              <p>%3$s</p>
 411                          </div>',
 412                          esc_attr( $meta_key ),
 413                          esc_html( $labels->{ $meta_key } ),
 414                          esc_html( $meta_schema['description'] )
 415                      );
 416                  }
 417              } else {
 418                  if ( isset( $type->name ) ) {
 419                      $checked = '';
 420                      if ( isset( $type->{$type_key} ) && true === (bool) $type->{$type_key} ) {
 421                          $checked = ' checked="checked"';
 422                      }
 423  
 424                      printf(
 425                          '<tr class="form-field bp-types-form term-%1$s-wrap">
 426                              <th scope="row"><label for="%1$s">%2$s</label></th>
 427                              <td>
 428                                  <input name="%1$s" id="%1$s" type="checkbox" value="1"%3$s> %4$s
 429                                  <p class="description">%5$s</p>
 430                              </td>
 431                          </tr>',
 432                          esc_attr( $meta_key ),
 433                          esc_html( $labels->{ $meta_key } ),
 434                          $checked,
 435                          esc_html__( 'Yes', 'buddypress' ),
 436                          esc_html( $meta_schema['description'] )
 437                      );
 438                  } else {
 439                      printf(
 440                          '<div class="form-field bp-types-form term-%1$s-wrap">
 441                              <label for="%1$s">
 442                                  <input name="%1$s" id="%1$s" type="checkbox" value="1"> %2$s
 443                              </label>
 444                              <p>%3$s</p>
 445                          </div>',
 446                          esc_attr( $meta_key ),
 447                          esc_html( $labels->{ $meta_key } ),
 448                          esc_html( $meta_schema['description'] )
 449                      );
 450                  }
 451              }
 452          }
 453      }
 454  
 455      /**
 456       * Outputs the BP type edit form.
 457       *
 458       * @since 7.0.0
 459       *
 460       * @param WP_Term $term     The term object for the BP Type.
 461       * @param string  $taxonomy The type taxonomy name.
 462       * @return string           HTML Output.
 463       */
 464  	public function edit_form_fields( $term = null, $taxonomy = '' ) {
 465          if ( ! isset( $term->name ) || ! $term->name || ! $taxonomy ) {
 466              return;
 467          }
 468  
 469          $type         = new stdClass();
 470          $type->name   = $term->name;
 471          $type->labels = array();
 472          $metadatas    = get_metadata( 'term', $term->term_id );
 473  
 474          foreach ( $metadatas as $meta_key => $meta_values ) {
 475              $meta_value = reset( $meta_values );
 476              $type_key   = str_replace( 'bp_type_', '', $meta_key );
 477  
 478              if ( in_array( $type_key, array( 'name', 'singular_name' ), true ) ) {
 479                  $type->labels[ $type_key ] = $meta_value;
 480              } else {
 481                  $type->{$type_key} = $meta_value;
 482              }
 483          }
 484  
 485          return $this->add_form_fields( $taxonomy, $type );
 486      }
 487  
 488      /**
 489       * Filters the terms list table column headers to customize them for BuddyPress Types.
 490       *
 491       * @since 7.0.0
 492       *
 493       * @param array  $column_headers The column header labels keyed by column ID.
 494       * @return array                 The column header labels keyed by column ID.
 495       */
 496  	public function column_headers( $column_headers = array() ) {
 497          if ( isset( $column_headers['name'] ) ) {
 498              $column_headers['name'] = __( 'Type ID', 'buddypress' );
 499          }
 500  
 501          unset( $column_headers['cb'], $column_headers['description'], $column_headers['posts'] );
 502  
 503          $column_headers['plural_name'] = __( 'Name', 'buddypress' );
 504          $column_headers['counts']      = _x( 'Count', 'Number/count of types', 'buddypress' );
 505  
 506          return $column_headers;
 507      }
 508  
 509      /**
 510       * Sets the content for the Plural name & Counts columns.
 511       *
 512       * @since 7.0.0
 513       *
 514       * @param string  $string      Blank string.
 515       * @param string  $column_name Name of the column.
 516       * @param int     $type_id     The type's term ID.
 517       * @return string              The Type Plural name.
 518       */
 519  	public function column_contents( $column_content = '', $column_name = '', $type_id = 0 ) {
 520          if ( 'plural_name' !== $column_name && 'counts' !== $column_name || ! $type_id ) {
 521              return $column_content;
 522          }
 523  
 524          // Set the Plural name column.
 525          if ( 'plural_name' === $column_name ) {
 526              $type_plural_name = get_term_meta( $type_id, 'bp_type_name', true );
 527  
 528              // Plural name meta is not set? Let's check register by code types!
 529              if ( ! $type_plural_name ) {
 530                  $type_name = get_term_field( 'name', $type_id, $this->taxonomy );
 531  
 532                  /**
 533                   * Filter here to set missing term meta for registered by code types.
 534                   *
 535                   * @see bp_set_registered_by_code_member_type_metadata() for an example of use.
 536                   *
 537                   * @since 7.0.0
 538                   *
 539                   * @param string $value Metadata for the BP Type.
 540                   */
 541                  $metadata = apply_filters( "{$this->taxonomy}_set_registered_by_code_metada", array(), $type_name );
 542  
 543                  if ( isset( $metadata['bp_type_name'] ) ) {
 544                      $type_plural_name = $metadata['bp_type_name'];
 545                  }
 546              }
 547  
 548              echo esc_html( $type_plural_name );
 549  
 550              // Set the Totals column.
 551          } elseif ( 'counts' === $column_name ) {
 552              global $parent_file;
 553              $type  = bp_get_term_by( 'id', $type_id, $this->taxonomy );
 554              if ( 0 === (int) $type->count ) {
 555                  return 0;
 556              }
 557  
 558              // Format the count.
 559              $count = number_format_i18n( $type->count );
 560  
 561              $args = array(
 562                  str_replace( '_', '-', $this->taxonomy ) => $type->slug,
 563              );
 564  
 565              $base_url = $parent_file;
 566              if ( false === strpos( $parent_file, '.php' ) ) {
 567                  $base_url = add_query_arg( 'page', $parent_file, 'admin.php' );
 568              }
 569  
 570              printf(
 571                  '<a href="%1$s">%2$s</a>',
 572                  esc_url( add_query_arg( $args, bp_get_admin_url( $base_url ) ) ),
 573                  esc_html( $count )
 574              );
 575          }
 576      }
 577  
 578      /**
 579       * Customizes the Types Admin list table row actions.
 580       *
 581       * @since 7.0.0
 582       *
 583       * @param array   $actions The table row actions.
 584       * @param WP_Term $type    The current BP Type for the row.
 585       * @return array           The table row actions for the current BP type.
 586       */
 587  	public function row_actions( $actions = array(), $type = null ) {
 588          if ( ! isset( $type->taxonomy ) || ! $type->taxonomy ) {
 589              return $actions;
 590          }
 591  
 592          /**
 593           * Filter here to set the types "registered by code".
 594           *
 595           * @see bp_get_member_types_registered_by_code() for an example of use.
 596           *
 597           * @since 7.0.0
 598           */
 599          $registered_by_code_types = apply_filters( "{$type->taxonomy}_registered_by_code", array() );
 600  
 601          // Types registered by code cannot be deleted as long as the custom registration code exists.
 602          if ( isset( $registered_by_code_types[ $type->name ] ) ) {
 603              unset( $actions['delete'] );
 604          }
 605  
 606          // Inline edits are disabled for all types.
 607          unset( $actions['inline hide-if-no-js'] );
 608  
 609          // Removes the post type query argument for the edit action.
 610          if ( isset( $actions['edit'] ) ) {
 611              $actions['edit'] = str_replace( '&#038;post_type=post', '', $actions['edit'] );
 612          }
 613  
 614          return $actions;
 615      }
 616  }
 617  
 618  endif;


Generated: Wed Jun 23 01:01:38 2021 Cross-referenced by PHPXref 0.7.1