[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/ -> bp-core-wpabstraction.php (source)

   1  <?php
   2  /**
   3   * WordPress Abstraction.
   4   *
   5   * The functions within this file will detect the version of WordPress you are
   6   * running and will alter the environment so BuddyPress can run regardless.
   7   *
   8   * The code below mostly contains function mappings. This file is subject to
   9   * change at any time.
  10   *
  11   * @package BuddyPress
  12   * @subpackage WPAbstraction
  13   * @since 1.2.0
  14   */
  15  
  16  // Exit if accessed directly.
  17  defined( 'ABSPATH' ) || exit;
  18  
  19  /**
  20   * Parse the WordPress core version number into the major release.
  21   *
  22   * @since 1.5.2
  23   *
  24   * @global string $wp_version
  25   *
  26   * @return double $wp_version
  27   */
  28  function bp_get_major_wp_version() {
  29      global $wp_version;
  30  
  31      return (float) $wp_version;
  32  }
  33  
  34  /*
  35   * Only add MS-specific abstraction functions if WordPress is not in multisite mode.
  36   */
  37  if ( !is_multisite() ) {
  38      global $wpdb;
  39  
  40      $wpdb->base_prefix = $wpdb->prefix;
  41      $wpdb->blogid      = BP_ROOT_BLOG;
  42  
  43      if ( !function_exists( 'get_blog_option' ) ) {
  44  
  45          /**
  46           * Retrieve blog option.
  47           *
  48           * @since 1.0.0
  49           *
  50           * @see get_blog_option()
  51           *
  52           * @param int    $blog_id     Blog ID to fetch for. Not used.
  53           * @param string $option_name Option name to fetch.
  54           * @param bool   $default     Whether or not default.
  55           * @return mixed
  56           */
  57  		function get_blog_option( $blog_id, $option_name, $default = false ) {
  58              return get_option( $option_name, $default );
  59          }
  60      }
  61  
  62      if ( ! function_exists( 'add_blog_option' ) ) {
  63  
  64          /**
  65           * Add blog option.
  66           *
  67           * @since 1.2.0
  68           *
  69           * @see add_blog_option()
  70           *
  71           * @param int    $blog_id      Blog ID to add for. Not used.
  72           * @param string $option_name  Option name to add.
  73           * @param mixed  $option_value Option value to add.
  74           * @return mixed
  75           */
  76  		function add_blog_option( $blog_id, $option_name, $option_value ) {
  77              return add_option( $option_name, $option_value );
  78          }
  79      }
  80  
  81      if ( !function_exists( 'update_blog_option' ) ) {
  82  
  83          /**
  84           * Update blog option.
  85           *
  86           * @since 1.2.0
  87           *
  88           * @see update_blog_option()
  89           *
  90           * @param int    $blog_id     Blog ID to update for. Not used.
  91           * @param string $option_name Option name to update.
  92           * @param mixed  $value       Option value to update.
  93           * @return mixed
  94           */
  95  		function update_blog_option( $blog_id, $option_name, $value ) {
  96              return update_option( $option_name, $value );
  97          }
  98      }
  99  
 100      if ( !function_exists( 'delete_blog_option' ) ) {
 101  
 102          /**
 103           * Delete blog option.
 104           *
 105           * @since 1.5.0
 106           *
 107           * @see delete_blog_option()
 108           *
 109           * @param int    $blog_id     Blog ID to delete for. Not used.
 110           * @param string $option_name Option name to delete.
 111           * @return mixed
 112           */
 113  		function delete_blog_option( $blog_id, $option_name ) {
 114              return delete_option( $option_name );
 115          }
 116      }
 117  
 118      if ( !function_exists( 'switch_to_blog' ) ) {
 119  
 120          /**
 121           * Switch to specified blog.
 122           *
 123           * @since 1.2.0
 124           *
 125           * @see switch_to_blog()
 126           *
 127           * @param mixed $new_blog   New blog to switch to. Not used.
 128           * @param null  $deprecated Whether or not deprecated. Not used.
 129           * @return int
 130           */
 131  		function switch_to_blog( $new_blog, $deprecated = null ) {
 132              return bp_get_root_blog_id();
 133          }
 134      }
 135  
 136      if ( !function_exists( 'restore_current_blog' ) ) {
 137  
 138          /**
 139           * Restore current blog.
 140           *
 141           * @since 1.2.0
 142           *
 143           * @see restore_current_blog()
 144           *
 145           * @return int
 146           */
 147  		function restore_current_blog() {
 148              return bp_get_root_blog_id();
 149          }
 150      }
 151  
 152      if ( !function_exists( 'get_blogs_of_user' ) ) {
 153  
 154          /**
 155           * Retrive blogs associated with user.
 156           *
 157           * @since 1.2.0
 158           *
 159           * @see get_blogs_of_user()
 160           *
 161           * @param int  $user_id ID of the user. Not used.
 162           * @param bool $all     Whether or not to return all. Not used.
 163           * @return bool
 164           */
 165  		function get_blogs_of_user( $user_id, $all = false ) {
 166              return false;
 167          }
 168      }
 169  
 170      if ( !function_exists( 'update_blog_status' ) ) {
 171  
 172          /**
 173           * Whether or not to update blog status.
 174           *
 175           * @since 1.2.0
 176           *
 177           * @see update_blog_status()
 178           *
 179           * @param int    $blog_id    Blog to update status for. Not used.
 180           * @param mixed  $pref       Preference. Not used.
 181           * @param string $value      Value. Not used.
 182           * @param null   $deprecated Whether or not deprecated. Not used.
 183           * @return bool
 184           */
 185  		function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
 186              return true;
 187          }
 188      }
 189  
 190      if ( !function_exists( 'is_subdomain_install' ) ) {
 191  
 192          /**
 193           * Whether or not if subdomain install.
 194           *
 195           * @since 1.2.5.1
 196           *
 197           * @see is_subdomain_install()
 198           *
 199           * @return bool
 200           */
 201  		function is_subdomain_install() {
 202              if ( ( defined( 'VHOST' ) && 'yes' == VHOST ) || ( defined( 'SUBDOMAIN_INSTALL' ) && SUBDOMAIN_INSTALL ) )
 203                  return true;
 204  
 205              return false;
 206          }
 207      }
 208  }
 209  
 210  /**
 211   * Get SQL chunk for filtering spam users from member queries.
 212   *
 213   * @internal
 214   * @todo Why is this function defined in this file?
 215   *
 216   * @param string|bool $prefix Global table prefix.
 217   * @return string SQL chunk.
 218   */
 219  function bp_core_get_status_sql( $prefix = false ) {
 220      if ( !is_multisite() )
 221          return "{$prefix}user_status = 0";
 222      else
 223          return "{$prefix}spam = 0 AND {$prefix}deleted = 0 AND {$prefix}user_status = 0";
 224  }
 225  
 226  /**
 227   * Multibyte encoding fallback functions.
 228   *
 229   * The PHP multibyte encoding extension is not enabled by default. In cases where it is not enabled,
 230   * these functions provide a fallback.
 231   *
 232   * Borrowed from MediaWiki, under the GPLv2. Thanks!
 233   */
 234  if ( !function_exists( 'mb_strlen' ) ) {
 235  
 236      /**
 237       * Fallback implementation of mb_strlen(), hardcoded to UTF-8.
 238       *
 239       * @param string $str String to be measured.
 240       * @param string $enc Optional. Encoding type. Ignored.
 241       * @return int String length.
 242       */
 243  	function mb_strlen( $str, $enc = '' ) {
 244          $counts = count_chars( $str );
 245          $total = 0;
 246  
 247          // Count ASCII bytes.
 248          for( $i = 0; $i < 0x80; $i++ ) {
 249              $total += $counts[$i];
 250          }
 251  
 252          // Count multibyte sequence heads.
 253          for( $i = 0xc0; $i < 0xff; $i++ ) {
 254              $total += $counts[$i];
 255          }
 256          return $total;
 257      }
 258  }
 259  
 260  if ( !function_exists( 'mb_strpos' ) ) {
 261  
 262      /**
 263       * Fallback implementation of mb_strpos(), hardcoded to UTF-8.
 264       *
 265       * @param string $haystack String to search in.
 266       * @param string $needle String to search for.
 267       * @param int    $offset Optional. Start position for the search. Default: 0.
 268       * @param string $encoding Optional. Encoding type. Ignored.
 269       * @return int|false Position of needle in haystack if found, else false.
 270       */
 271  	function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
 272          $needle = preg_quote( $needle, '/' );
 273  
 274          $ar = array();
 275          preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
 276  
 277          if( isset( $ar[0][1] ) ) {
 278              return $ar[0][1];
 279          } else {
 280              return false;
 281          }
 282      }
 283  }
 284  
 285  if ( !function_exists( 'mb_strrpos' ) ) {
 286  
 287      /**
 288       * Fallback implementation of mb_strrpos(), hardcoded to UTF-8.
 289       *
 290       * @param string $haystack String to search in.
 291       * @param string $needle String to search for.
 292       * @param int    $offset Optional. Start position for the search. Default: 0.
 293       * @param string $encoding Optional. Encoding type. Ignored.
 294       * @return string|false Position of last needle in haystack if found, else false.
 295       */
 296  	function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
 297          $needle = preg_quote( $needle, '/' );
 298  
 299          $ar = array();
 300          preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
 301  
 302          if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
 303              isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
 304              return $ar[0][count( $ar[0] ) - 1][1];
 305          } else {
 306              return false;
 307          }
 308      }
 309  }
 310  
 311  /**
 312   * Returns the name of the hook to use once a WordPress Site is inserted into the Database.
 313   *
 314   * WordPress 5.1.0 deprecated the `wpmu_new_blog` action. As BuddyPress is supporting WordPress back
 315   * to 4.9.0, this function makes sure we are using the new hook `wp_initialize_site` when the current
 316   * WordPress version is upper or equal to 5.1.0 and that we keep on using `wpmu_new_blog` for earlier
 317   * versions of WordPress.
 318   *
 319   * @since 6.0.0
 320   *
 321   * @return string The name of the hook to use.
 322   */
 323  function bp_insert_site_hook() {
 324      $wp_hook = 'wpmu_new_blog';
 325  
 326      if ( function_exists( 'wp_insert_site' ) ) {
 327          $wp_hook = 'wp_initialize_site';
 328      }
 329  
 330      return $wp_hook;
 331  }
 332  
 333  /**
 334   * Catch the new site data for a later use.
 335   *
 336   * @since 6.0.0
 337   */
 338  function bp_catch_site_data( $errors = null, $data = array() ) {
 339      buddypress()->new_site_data = $data;
 340  }
 341  add_action( 'wp_validate_site_data', 'bp_catch_site_data', 10, 2 );
 342  
 343  /**
 344   * Fires a BuddyPress hook when a new WordPress site is inserted into the database.
 345   *
 346   * This hook makes sure BuddyPress is back compatible with WordPress versions < 5.1.0.
 347   *
 348   * @since 6.0.0
 349   *
 350   * @param int|WP_Site $site            The Site ID or the WP Site object.
 351   * @param int|array   $args_or_user_id An array of Site arguments or the User ID.
 352   * @param string      $domain          Site domain.
 353   * @param string      $path            Site path.
 354   * @param int         $network_id      Network ID. Only relevant on multi-network installations.
 355   * @param array       $meta            Meta data. Used to set initial site options.
 356   */
 357  function bp_insert_site( $site, $args_or_user_id = null, $domain = '', $path = '', $network_id = 0, $meta = array() ) {
 358      if ( $site instanceof WP_Site ) {
 359          $bp         = buddypress();
 360          $site_id    = $site->id;
 361          $domain     = $site->domain;
 362          $path       = $site->path;
 363          $network_id = $site->network_id;
 364          $args       = (array) $args_or_user_id;
 365  
 366          $user_id = 0;
 367          if ( isset( $args['user_id'] ) && $args['user_id'] ) {
 368              $user_id = (int) $args['user_id'];
 369          }
 370  
 371          $meta = array();
 372          if ( isset( $args['options'] ) && $args['options'] ) {
 373              $meta = (array) $args['options'];
 374  
 375              if ( ! array_key_exists( 'WPLANG', $meta ) ) {
 376                  $meta['WPLANG'] = get_network_option( $site->network_id, 'WPLANG' );
 377              }
 378  
 379              if ( isset( $bp->new_site_data ) ) {
 380                  $meta = array_merge( $bp->new_site_data, $meta );
 381              }
 382          }
 383      } else {
 384          $site_id = $site;
 385          $user_id = (int) $args_or_user_id;
 386      }
 387  
 388      /**
 389       * Fires when a new WordPress site has been inserted into the database.
 390       *
 391       * @since 6.0.0
 392       *
 393       * @param int    $site_id    Site ID.
 394       * @param int    $user_id    User ID.
 395       * @param string $domain     Site domain.
 396       * @param string $path       Site path.
 397       * @param int    $network_id Network ID. Only relevant on multi-network installations.
 398       * @param array  $meta       Meta data. Used to set initial site options.
 399       */
 400      do_action( 'bp_insert_site', $site_id, $user_id, $domain, $path, $network_id, $meta );
 401  }
 402  add_action( bp_insert_site_hook(), 'bp_insert_site' );
 403  
 404  /**
 405   * Returns the name of the hook to use once a WordPress Site is deleted.
 406   *
 407   * WordPress 5.1.0 deprecated the `delete_blog` action. As BuddyPress is supporting WordPress back
 408   * to 4.9.0, this function makes sure we are using the new hook `wp_validate_site_deletion` when the
 409   * current WordPress version is upper or equal to 5.1.0 and that we keep on using `delete_blog` for
 410   * earlier versions of WordPress.
 411   *
 412   * @since 6.0.0
 413   *
 414   * @return string The name of the hook to use.
 415   */
 416  function bp_delete_site_hook() {
 417      $wp_hook = 'delete_blog';
 418  
 419      if ( function_exists( 'wp_delete_site' ) ) {
 420          $wp_hook = 'wp_validate_site_deletion';
 421      }
 422  
 423      return $wp_hook;
 424  }
 425  
 426  /**
 427   * Makes sure the `bp_delete_site` hook is fired if site's deletion
 428   * was performed without dropping tables.
 429   *
 430   * @since 6.0.0
 431   *
 432   * @param WP_Site $site The site object.
 433   */
 434  function bp_delete_site_no_tables_drop( $site ) {
 435      if ( isset( $site->deleted ) && 1 === (int) $site->deleted ) {
 436          return bp_delete_site( $site->id, false );
 437      }
 438  }
 439  add_action( 'wp_update_site', 'bp_delete_site_no_tables_drop', 10, 1 );
 440  
 441  /**
 442   * Fires a BuddyPress hook when a new WordPress site is deleted.
 443   *
 444   * This hook makes sure BuddyPress is back compatible with WordPress versions < 5.1.0.
 445   *
 446   * @since 6.0.0
 447   *
 448   * @param int|WP_Error $site_id_or_error A WP Error object or the site ID.
 449   * @param bool|WP_Site $drop_or_site     A WP Site object or a boolean to inform whether site's table should be dropped.
 450   */
 451  function bp_delete_site( $site_id_or_error, $drop_or_site = false ) {
 452      if ( $drop_or_site instanceof WP_Site ) {
 453          if ( ! empty( $site_id_or_error->errors ) ) {
 454              return;
 455          }
 456  
 457          $site_id = (int) $drop_or_site->id;
 458          $drop    = true;
 459      } else {
 460          $site_id = (int) $site_id_or_error;
 461          $drop    = (bool) $drop_or_site;
 462      }
 463  
 464      /**
 465       * Fires when a WordPress site is deleted.
 466       *
 467       * @since 6.0.0
 468       *
 469       * @param int  $site_id The site ID.
 470       * @param bool $drop    True if site's table should be dropped. Default is false.
 471       */
 472      do_action( 'bp_delete_site', $site_id, $drop );
 473  }
 474  add_action( bp_delete_site_hook(), 'bp_delete_site', 10, 2 );
 475  
 476  if ( ! function_exists( 'wp_parse_list' ) ) {
 477      /**
 478       * Cleans up an array, comma- or space-separated list of scalar values.
 479       *
 480       * As BuddyPress supports older WordPress versions than 5.1 (4.9 & 5.0),
 481       * the BP REST API needs this function to be available.
 482       *
 483       * @since 7.0.0
 484       *
 485       * @param array|string $list List of values.
 486       * @return array Sanitized array of values.
 487       */
 488  	function wp_parse_list( $list ) {
 489          if ( ! is_array( $list ) ) {
 490              return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY );
 491          }
 492  
 493          return $list;
 494      }
 495  }


Generated: Fri Aug 7 01:01:33 2020 Cross-referenced by PHPXref 0.7.1