Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/07/2023 01:09:00 AM (4 years ago)
Author:
espellcaste
Message:

Notifications: returns the proper items when using the argument.

returns the notifications, together with the notification data, when the query is performed using the argument with the .

Closes https://github.com/buddypress/buddypress/pull/49
Fixes #8556

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/classes/class-bp-notifications-notification.php

    r13395 r13397  
    667667         *     @type string       $order_by          Database column to order notifications by.
    668668         *     @type string       $sort_order        Either 'ASC' or 'DESC'.
    669          *     @type string       $order_by          Field to order results by.
    670          *     @type string       $sort_order        ASC or DESC.
    671669         *     @type int          $page              Number of the current page of results. Default:
    672670         *                                           false (no pagination - all items).
     
    693691
    694692                // SELECT.
    695                 $select_sql = "SELECT *";
     693                $select_sql = "SELECT n.*";
     694
     695                // FROM.
     696                $from_sql = "FROM {$bp->notifications->table_name} n ";
     697
     698                // Append meta data to the results.
     699                if ( isset( $r['meta_query'][0]['compare'] ) && 'EXISTS' === $r['meta_query'][0]['compare'] ) {
     700                        $meta_table = $bp->notifications->table_name_meta;
     701                        $select_sql = "SELECT n.*, {$meta_table}.id as meta_id, {$meta_table}.meta_key, {$meta_table}.meta_value";
     702                }
     703
     704                // JOIN.
     705                $join_sql = $meta_query_sql['join'];
     706
     707                // WHERE.
     708                $where_sql = self::get_where_sql( array(
     709                        'id'                => $r['id'],
     710                        'user_id'           => $r['user_id'],
     711                        'item_id'           => $r['item_id'],
     712                        'secondary_item_id' => $r['secondary_item_id'],
     713                        'component_name'    => $r['component_name'],
     714                        'component_action'  => $r['component_action'],
     715                        'is_new'            => $r['is_new'],
     716                        'search_terms'      => $r['search_terms'],
     717                        'date_query'        => $r['date_query']
     718                ), $select_sql, $from_sql, $join_sql, $meta_query_sql );
     719
     720                // ORDER BY.
     721                $order_sql  = self::get_order_by_sql( array(
     722                        'order_by'   => $r['order_by'],
     723                        'sort_order' => $r['sort_order']
     724                ) );
     725
     726                // LIMIT %d, %d.
     727                $pag_sql    = self::get_paged_sql( array(
     728                        'page'     => $r['page'],
     729                        'per_page' => $r['per_page']
     730                ) );
     731
     732                // Concatenate query parts.
     733                $sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} {$order_sql} {$pag_sql}";
     734
     735                // Perform query.
     736                $results = $wpdb->get_results( $sql );
     737
     738                // Integer casting.
     739                foreach ( $results as $key => $result ) {
     740                        $results[$key]->id                = (int) $results[$key]->id;
     741                        $results[$key]->user_id           = (int) $results[$key]->user_id;
     742                        $results[$key]->item_id           = (int) $results[$key]->item_id;
     743                        $results[$key]->secondary_item_id = (int) $results[$key]->secondary_item_id;
     744                        $results[$key]->is_new            = (int) $results[$key]->is_new;
     745                }
     746
     747                // Update meta cache.
     748                if ( true === $r['update_meta_cache'] ) {
     749                        bp_notifications_update_meta_cache( wp_list_pluck( $results, 'id' ) );
     750                }
     751
     752                return $results;
     753        }
     754
     755        /**
     756         * Get a count of total notifications matching a set of arguments.
     757         *
     758         * @since 1.9.0
     759         *
     760         * @global wpdb $wpdb WordPress database object.
     761         *
     762         * @param array|string $args See {@link BP_Notifications_Notification::get()}.
     763         * @return int Count of located items.
     764         */
     765        public static function get_total_count( $args ) {
     766                global $wpdb;
     767
     768                // Parse the arguments.
     769                $r = self::parse_args( $args );
     770
     771                // Load BuddyPress.
     772                $bp = buddypress();
     773
     774                // METADATA.
     775                $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] );
     776
     777                // SELECT.
     778                $select_sql = "SELECT COUNT(*)";
    696779
    697780                // FROM.
     
    714797                ), $select_sql, $from_sql, $join_sql, $meta_query_sql );
    715798
    716                 // ORDER BY.
    717                 $order_sql  = self::get_order_by_sql( array(
    718                         'order_by'   => $r['order_by'],
    719                         'sort_order' => $r['sort_order']
    720                 ) );
    721 
    722                 // LIMIT %d, %d.
    723                 $pag_sql    = self::get_paged_sql( array(
    724                         'page'     => $r['page'],
    725                         'per_page' => $r['per_page']
    726                 ) );
    727 
    728                 // Concatenate query parts.
    729                 $sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} {$order_sql} {$pag_sql}";
    730 
    731                 $results = $wpdb->get_results( $sql );
    732 
    733                 // Integer casting.
    734                 foreach ( $results as $key => $result ) {
    735                         $results[$key]->id                = (int) $results[$key]->id;
    736                         $results[$key]->user_id           = (int) $results[$key]->user_id;
    737                         $results[$key]->item_id           = (int) $results[$key]->item_id;
    738                         $results[$key]->secondary_item_id = (int) $results[$key]->secondary_item_id;
    739                         $results[$key]->is_new            = (int) $results[$key]->is_new;
    740                 }
    741 
    742                 // Update meta cache.
    743                 if ( true === $r['update_meta_cache'] ) {
    744                         bp_notifications_update_meta_cache( wp_list_pluck( $results, 'id' ) );
    745                 }
    746 
    747                 return $results;
    748         }
    749 
    750         /**
    751          * Get a count of total notifications matching a set of arguments.
    752          *
    753          * @since 1.9.0
    754          *
    755          * @global wpdb $wpdb WordPress database object.
    756          *
    757          * @param array|string $args See {@link BP_Notifications_Notification::get()}.
    758          * @return int Count of located items.
    759          */
    760         public static function get_total_count( $args ) {
    761                 global $wpdb;
    762 
    763                 // Parse the arguments.
    764                 $r = self::parse_args( $args );
    765 
    766                 // Load BuddyPress.
    767                 $bp = buddypress();
    768 
    769                 // METADATA.
    770                 $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] );
    771 
    772                 // SELECT.
    773                 $select_sql = "SELECT COUNT(*)";
    774 
    775                 // FROM.
    776                 $from_sql   = "FROM {$bp->notifications->table_name} n ";
    777 
    778                 // JOIN.
    779                 $join_sql   = $meta_query_sql['join'];
    780 
    781                 // WHERE.
    782                 $where_sql  = self::get_where_sql( array(
    783                         'id'                => $r['id'],
    784                         'user_id'           => $r['user_id'],
    785                         'item_id'           => $r['item_id'],
    786                         'secondary_item_id' => $r['secondary_item_id'],
    787                         'component_name'    => $r['component_name'],
    788                         'component_action'  => $r['component_action'],
    789                         'is_new'            => $r['is_new'],
    790                         'search_terms'      => $r['search_terms'],
    791                         'date_query'        => $r['date_query']
    792                 ), $select_sql, $from_sql, $join_sql, $meta_query_sql );
    793 
    794799                // Concatenate query parts.
    795800                $sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql}";
     
    810815         * @since 2.3.0
    811816         *
     817         * @global wpdb $wpdb WordPress database object.
     818         *
    812819         * @param  array $meta_query An array of meta_query filters. See the
    813820         *                           documentation for WP_Meta_Query for details.
     
    815822         */
    816823        public static function get_meta_query_sql( $meta_query = array() ) {
     824                global $wpdb;
    817825
    818826                // Default array keys & empty values.
     
    827835                }
    828836
     837                $bp       = buddypress();
     838                $meta_sql = new WP_Meta_Query( $meta_query );
     839
    829840                // WP_Meta_Query expects the table name at $wpdb->notificationmeta.
    830                 $GLOBALS['wpdb']->notificationmeta = buddypress()->notifications->table_name_meta;
    831 
    832                 $n_meta_query = new WP_Meta_Query( $meta_query );
    833                 $meta_sql     = $n_meta_query->get_sql( 'notification', 'n', 'id' );
     841                $wpdb->notificationmeta = $bp->notifications->table_name_meta;
     842
     843                $meta_sql = $meta_sql->get_sql( 'notification', 'n', 'id' );
    834844
    835845                // Strip the leading AND - it's handled in get().
Note: See TracChangeset for help on using the changeset viewer.