Bp_Search_Media

BuddyPress全球搜索-搜索媒体类

描述

文件:bp-search /类/ class-bp-search-media.php

class Bp_Search_Media extends Bp_Search_Type{/** *搜索项类型名** @var string */ private $type = 'photos';/** *确保在任何时候只有一个类的实例存在于内存中。还可以防止需要到处定义全局变量。* * @since BuddyBoss 1.5.0 * * @return object Bp_Search_Media */ public static function instance(){//将实例存储在本地以避免私有静态复制。Static $instance = null;//如果这些方法之前没有运行过,则只运行它们。if (null === $instance) {$instance = new Bp_Search_Media();} //总是返回实例。返回$实例;} /** *一个伪构造函数来防止这个类被加载不止一次。 * * @since BuddyBoss 1.4.0 */ private function __construct() { /* Do nothing here */ } /** * Prepare SQL query for media search. * * @param string $search_term Search terms. * @param false $only_totalrow_count Total row count. * * @return mixed|void */ public function sql( $search_term, $only_totalrow_count = false ) { global $wpdb, $bp; $query_placeholder = array(); $user_groups = array(); if ( bp_is_active( 'groups' ) ) { // Fetch public groups. $public_groups = groups_get_groups( array( 'fields' => 'ids', 'status' => 'public', 'per_page' => - 1, ) ); if ( ! empty( $public_groups['groups'] ) ) { $public_groups = $public_groups['groups']; } else { $public_groups = array(); } $user_groups = array(); if ( is_user_logged_in() ) { $groups = groups_get_user_groups( bp_loggedin_user_id() ); if ( ! empty( $groups['groups'] ) ) { $user_groups = $groups['groups']; } else { $user_groups = array(); } } $user_groups = array_unique( array_merge( $user_groups, $public_groups ) ); } $friends = array(); if ( bp_is_active( 'friends' ) && is_user_logged_in() ) { // Determine friends of user. $friends = friends_get_friend_user_ids( bp_loggedin_user_id() ); if ( empty( $friends ) ) { $friends = array( 0 ); } array_push( $friends, bp_loggedin_user_id() ); } $sql = ' SELECT '; if ( $only_totalrow_count ) { $sql .= ' COUNT( DISTINCT m.id ) '; } else { $sql .= $wpdb->prepare( " DISTINCT m.id, 'photos' as type, m.title LIKE %s AS relevance, m.date_created as entry_date ", '%' . $wpdb->esc_like( $search_term ) . '%' ); } $sql .= " FROM {$bp->media->table_name} m WHERE"; $privacy = array( 'public' ); if ( is_user_logged_in() ) { $privacy[] = 'loggedin'; } $sql .= $wpdb->prepare( " ( ( m.title LIKE %s ) AND ( ( m.privacy IN ( '" . implode( "','", $privacy ) . "' ) ) " . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared ( isset( $user_groups ) && ! empty( $user_groups ) ? " OR ( m.group_id IN ( '" . implode( "','", $user_groups ) . "' ) AND m.privacy = 'grouponly' )" : '' ) . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration ( bp_is_active( 'friends' ) && ! empty( $friends ) ? " OR ( m.user_id IN ( '" . implode( "','", $friends ) . "' ) AND m.privacy = 'friends' )" : '' ) . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration ( is_user_logged_in() ? " OR ( m.user_id = '" . bp_loggedin_user_id() . "' AND m.privacy = 'onlyme' )" : '' ) . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared ') )', '%' . $wpdb->esc_like( $search_term ) . '%' ); return apply_filters( 'bp_search_photos_sql', $sql, array( 'search_term' => $search_term, 'only_totalrow_count' => $only_totalrow_count, ) ); } /** * Generare Html for media search * * @param string $template_type Template type. */ protected function generate_html( $template_type = '' ) { $document_ids = array(); foreach ( $this->search_results['items'] as $item_id => $item_html ) { $document_ids[] = $item_id; } // now we have all the posts. // lets do a media loop. $args = array( 'include' => implode( ',', $document_ids ), 'per_page' => count( $document_ids ), 'search_terms' => false, ); do_action( 'bp_before_search_photos_html' ); if ( bp_has_media( $args ) ) { while ( bp_media() ) : bp_the_media(); $result = array( 'id' => bp_get_media_id(), 'type' => $this->type, 'title' => bp_get_media_title(), 'html' => bp_search_buffer_template_part( 'loop/photos', $template_type, false ), ); $this->search_results['items'][ bp_get_media_id() ] = $result; endwhile; } do_action( 'bp_after_search_photos_html' ); } }

方法

  • __construct-一个伪构造函数,防止这个类被加载不止一次。
  • generate_html-为媒体搜索生成Html
  • 实例确保在任何时候只有一个类的实例存在于内存中。还可以防止需要到处定义全局变量。
  • sql—为媒体查询准备SQL查询。

问题吗?

我们总是很乐意帮助您解决代码或其他问题!搜索我们的开发人员文档联络支持,或与我们联系销售团队