[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Types Admin functions.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 7.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  if ( ! defined( 'ABSPATH' ) ) {
  12      exit;
  13  }
  14  
  15  /**
  16   * Get default values for the taxonomy registered metadata.
  17   *
  18   * @since 7.0.0
  19   *
  20   * @param string $type_taxonomy The type's taxonomy name.
  21   * @return array                Default values for the taxonomy registered metadata.
  22   */
  23  function bp_core_admin_get_type_default_meta_values( $type_taxonomy ) {
  24      $metadata_schema = bp_get_type_metadata_schema( false, $type_taxonomy );
  25      $metadata        = wp_list_pluck( $metadata_schema, 'type' );
  26  
  27      // Set default values according to their schema type.
  28      foreach ( $metadata as $meta_key => $meta_value ) {
  29          if ( in_array( $meta_value, array( 'boolean', 'integer' ), true ) ) {
  30              $metadata[ $meta_key ] = 0;
  31          } else {
  32              $metadata[ $meta_key ] = '';
  33          }
  34      }
  35  
  36      return $metadata;
  37  }
  38  
  39  /**
  40   * Insert a new type into the database.
  41   *
  42   * @since 7.0.0
  43   *
  44   * @param array  $args {
  45   *     Array of arguments describing the object type.
  46   *
  47   *     @type string $taxonomy   The Type's taxonomy. Required.
  48   *     @type string $bp_type_id Unique string identifier for the member type. Required.
  49   *     @see keys of the array returned by bp_get_type_metadata_schema() for the other arguments.
  50   * }
  51   * @return integer|WP_Error The Type's term ID on success. A WP_Error object otherwise.
  52   */
  53  function bp_core_admin_insert_type( $args = array() ) {
  54      $default_args = array(
  55          'taxonomy'   => '',
  56          'bp_type_id' => '',
  57      );
  58  
  59      $args = array_map( 'wp_unslash', $args );
  60      $args = bp_parse_args(
  61          $args,
  62          $default_args,
  63          'admin_insert_type'
  64      );
  65  
  66      if ( ! $args['bp_type_id'] || ! $args['taxonomy'] ) {
  67           return new WP_Error(
  68               'invalid_type_taxonomy',
  69               __( 'The Type ID value is missing', 'buddypress' ),
  70               array(
  71                  'message' => 1,
  72               )
  73          );
  74      }
  75  
  76      $type_id       = sanitize_title( $args['bp_type_id'] );
  77      $type_taxonomy = sanitize_key( $args['taxonomy'] );
  78  
  79      /**
  80       * Filter here to check for an already existing type.
  81       *
  82       * @since 7.0.0
  83       *
  84       * @param boolean $value   True if the type exists. False otherwise.
  85       * @param string  $type_id The Type's ID.
  86       */
  87      $type_exists = apply_filters( "{$type_taxonomy}_check_existing_type", false, $type_id );
  88  
  89      if ( false !== $type_exists ) {
  90          return new WP_Error(
  91              'type_already_exists',
  92              __( 'The Type already exists', 'buddypress' ),
  93              array(
  94                 'message' => 5,
  95              )
  96         );
  97      }
  98  
  99      // Get defaulte values for metadata.
 100      $metadata = bp_core_admin_get_type_default_meta_values( $type_taxonomy );
 101  
 102      // Validate metadata
 103      $metas = array_filter( array_intersect_key( $args, $metadata ) );
 104  
 105      // Insert the Type into the database.
 106      $type_term_id = bp_insert_term(
 107          $type_id,
 108          $type_taxonomy,
 109          array(
 110              'slug'  => $type_id,
 111              'metas' => $metas,
 112          )
 113      );
 114  
 115      if ( is_wp_error( $type_term_id ) ) {
 116          $type_term_id->add_data(
 117              array(
 118                  'message' => 3,
 119              )
 120          );
 121  
 122          return $type_term_id;
 123      }
 124  
 125      /**
 126       * Hook here to add code once the type has been inserted.
 127       *
 128       * @since 7.0.0
 129       *
 130       * @param integer $type_term_id  The Type's term_ID.
 131       * @param string  $type_taxonomy The Type's taxonomy name.
 132       * @param string  $type_id       The Type's ID.
 133       */
 134      do_action( 'bp_type_inserted', $type_term_id, $type_taxonomy, $type_id );
 135  
 136      // Finally return the inserted Type's term ID.
 137      return $type_term_id;
 138  }
 139  
 140  /**
 141   * Update a type into the database.
 142   *
 143   * @since 7.0.0
 144   *
 145   * @param array  $args {
 146   *     Array of arguments describing the object type.
 147   *
 148   *     @type string  $taxonomy     The Type's taxonomy. Required.
 149   *     @type integer $type_term_id The Type's term ID. Required.
 150   *     @see keys of the array returned by bp_get_type_metadata_schema() for the other arguments.
 151   * }
 152   * @return boolean|WP_Error True on success. A WP_Error object otherwise.
 153   */
 154  function bp_core_admin_update_type( $args = array() ) {
 155      $default_args = array(
 156          'taxonomy'     => '',
 157          'type_term_id' => 0,
 158      );
 159  
 160      $args = array_map( 'wp_unslash', $args );
 161      $args = bp_parse_args(
 162          $args,
 163          $default_args,
 164          'admin_update_type'
 165      );
 166  
 167      if ( ! $args['type_term_id'] || ! $args['taxonomy'] ) {
 168           return new WP_Error(
 169               'invalid_type_taxonomy',
 170               __( 'The Term Type ID value is missing', 'buddypress' ),
 171               array(
 172                  'message' => 10,
 173              )
 174          );
 175      }
 176  
 177      $type_term_id  = (int) $args['type_term_id'];
 178      $type_taxonomy = sanitize_key( $args['taxonomy'] );
 179  
 180      // Get defaulte values for metadata.
 181      $metadata  = bp_core_admin_get_type_default_meta_values( $type_taxonomy );
 182  
 183      // Merge customs with defaults.
 184      $metas = wp_parse_args( $args, $metadata );
 185  
 186      // Validate metadata
 187      $metas = array_intersect_key( $metas, $metadata );
 188  
 189      foreach ( $metas as $meta_key => $meta_value ) {
 190          if ( '' === $meta_value ) {
 191              delete_term_meta( $type_term_id, $meta_key );
 192          } else {
 193              update_term_meta( $type_term_id, $meta_key, $meta_value );
 194          }
 195      }
 196  
 197      /**
 198       * Hook here to add code once the type has been updated.
 199       *
 200       * @since 7.0.0
 201       *
 202       * @param integer $type_term_id  The Type's term_ID.
 203       * @param string  $type_taxonomy The Type's taxonomy name.
 204       */
 205      do_action( 'bp_type_updated', $type_term_id, $type_taxonomy );
 206  
 207      // Finally informs about the successfull update.
 208      return true;
 209  }
 210  
 211  /**
 212   * Delete a type from the database.
 213   *
 214   * @since 7.0.0
 215   *
 216   * @param array  $args {
 217   *     Array of arguments describing the object type.
 218   *
 219   *     @type string  $taxonomy     The Type's taxonomy. Required.
 220   *     @type integer $type_term_id The Type's term ID. Required.
 221   * }
 222   * @return boolean|WP_Error True on success. A WP_Error object otherwise.
 223   */
 224  function bp_core_admin_delete_type( $args = array() ) {
 225      $default_args = array(
 226          'taxonomy'     => '',
 227          'type_term_id' => 0,
 228      );
 229  
 230      $args = array_map( 'wp_unslash', $args );
 231      $args = bp_parse_args(
 232          $args,
 233          $default_args,
 234          'admin_delete_type'
 235      );
 236  
 237      if ( ! $args['type_term_id'] || ! $args['taxonomy'] ) {
 238           return new WP_Error(
 239               'invalid_type_taxonomy',
 240               __( 'The Term Type ID value is missing', 'buddypress' ),
 241               array(
 242                  'message' => 10,
 243              )
 244          );
 245      }
 246  
 247      $type_term_id  = (int) $args['type_term_id'];
 248      $type_taxonomy = sanitize_key( $args['taxonomy'] );
 249      $type_term     = bp_get_term_by( 'id', $type_term_id, $type_taxonomy );
 250  
 251      if ( ! $type_term ) {
 252          return new WP_Error(
 253              'type_doesnotexist',
 254              __( 'The type was not deleted: it does not exist.', 'buddypress' ),
 255              array(
 256                 'message' => 6,
 257              )
 258          );
 259      }
 260  
 261      /** This filter is documented in bp-core/classes/class-bp-admin-types.php */
 262      $registered_by_code_types = apply_filters( "{$type_taxonomy}_registered_by_code", array() );
 263  
 264      if ( isset( $registered_by_code_types[ $type_term->name ] ) ) {
 265          return new WP_Error(
 266              'type_register_by_code',
 267              __( 'This type is registered using code, deactivate the plugin or remove the custom code before trying to delete it again.', 'buddypress' ),
 268              array(
 269                 'message' => 7,
 270              )
 271          );
 272      }
 273  
 274      $deleted = bp_delete_term( $type_term_id, $type_taxonomy );
 275  
 276      if ( true !== $deleted ) {
 277          return new WP_Error(
 278              'type_not_deleted',
 279              __( 'There was an error while trying to delete this type.', 'buddypress' ),
 280              array(
 281                 'message' => 8,
 282              )
 283          );
 284      }
 285  
 286      /**
 287       * Hook here to add code once the type has been deleted.
 288       *
 289       * @since 7.0.0
 290       *
 291       * @param integer $type_term_id  The Type's term_ID.
 292       * @param string  $type_taxonomy The Type's taxonomy name.
 293       */
 294      do_action( 'bp_type_deleted', $type_term_id, $type_taxonomy );
 295  
 296      // Finally informs about the successfull delete.
 297      return true;
 298  }


Generated: Sun Jun 13 01:01:37 2021 Cross-referenced by PHPXref 0.7.1