Skip to:
Content

BuddyPress.org

Changeset 13397


Ignore:
Timestamp:
01/07/2023 01:09:00 AM (11 months 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

Location:
trunk
Files:
2 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().
  • trunk/tests/phpunit/testcases/notifications/class-bp-notifications-notification.php

    r12516 r13397  
    55 */
    66class BP_Tests_BP_Notifications_Notification_TestCases extends BP_UnitTestCase {
     7
    78    /**
    89     * @group get
     
    276277
    277278        // Check that the correct items are pulled up
    278         $expected = array( $n2 );
    279         $actual = wp_list_pluck( $n, 'id' );
    280         $this->assertEquals( $expected, $actual );
    281     }
    282 
    283     /**
     279        $this->assertEquals( [ $n2 ], wp_list_pluck( $n, 'id' ) );
     280    }
     281
     282    /**
     283     * @group get
    284284     * @group pagination
    285285     * @group BP6229
     
    291291        for ( $i = 1; $i <= 6; $i++ ) {
    292292            $notifications[] = self::factory()->notification->create( array(
    293                 'component_name' => 'activity',
     293                'component_name'    => 'activity',
    294294                'secondary_item_id' => $i,
    295                 'user_id' => $u,
    296                 'is_new' => true,
     295                'user_id'           => $u,
     296                'is_new'            => true,
    297297            ) );
    298298        }
    299299
    300300        $found = BP_Notifications_Notification::get( array(
    301             'user_id' => $u,
    302             'is_new' => true,
    303             'page' => 2,
     301            'user_id'  => $u,
     302            'is_new'   => true,
     303            'page'     => 2,
    304304            'per_page' => 2,
    305305            'order_by' => 'id',
     
    307307
    308308        // Check that the correct number of items are pulled up
    309         $expected = array( $notifications[2], $notifications[3] );
    310         $this->assertEquals( $expected, wp_list_pluck( $found, 'id' ) );
     309        $this->assertEquals(
     310            [ $notifications[2], $notifications[3] ],
     311            wp_list_pluck( $found, 'id' )
     312        );
     313    }
     314
     315    /**
     316     * @group get
     317     * @group meta_query
     318     */
     319    public function test_get_notifications_meta_query() {
     320        $u        = self::factory()->user->create();
     321        $meta_key = 'foo';
     322        $args     = [
     323            'user_id'         => $u,
     324            'component_name'  => 'activity',
     325            'allow_duplicate' => true,
     326        ];
     327
     328        $n1 = bp_notifications_add_notification( $args );
     329
     330        bp_notifications_add_meta( $n1, $meta_key, 'bar' );
     331
     332        $n2 = bp_notifications_add_notification( $args );
     333
     334        $found_1 = BP_Notifications_Notification::get(
     335            [
     336                'user_id'    => $u,
     337                'meta_query' => [
     338                    [
     339                        'key'     => $meta_key,
     340                        'compare' => 'EXISTS'
     341                    ]
     342                ],
     343            ]
     344        );
     345
     346        $this->assertEquals( [ $n1 ], wp_list_pluck( $found_1, 'id' ) );
     347
     348        $found_2 = BP_Notifications_Notification::get(
     349            [
     350                'user_id'    => $u,
     351                'meta_query' => [
     352                    [
     353                        'key'     => $meta_key,
     354                        'compare' => 'NOT EXISTS'
     355                    ]
     356                ],
     357            ]
     358        );
     359
     360        $this->assertEquals( [ $n2 ], wp_list_pluck( $found_2, 'id' ) );
     361    }
     362
     363    /**
     364     * @group get
     365     * @group meta_query
     366     */
     367    public function test_get_notifications_sorted_sql_with_meta_query() {
     368        $u        = self::factory()->user->create();
     369        $meta_key = 'foo';
     370        $args     = [
     371            'user_id'         => $u,
     372            'component_name'  => 'activity',
     373            'allow_duplicate' => true,
     374        ];
     375
     376        $n1 = bp_notifications_add_notification( $args );
     377        $n2 = bp_notifications_add_notification( $args );
     378        $n3 = bp_notifications_add_notification( $args );
     379        $n4 = bp_notifications_add_notification( $args );
     380
     381        bp_notifications_add_meta( $n1, $meta_key, 'bar' );
     382        bp_notifications_add_meta( $n2, $meta_key, 'bar' );
     383
     384        $found_1 = BP_Notifications_Notification::get(
     385            [
     386                'user_id'    => $u,
     387                'order_by'   => 'id',
     388                'sort_order' => 'DESC',
     389                'meta_query' => [
     390                    [
     391                        'key'     => $meta_key,
     392                        'compare' => 'EXISTS'
     393                    ]
     394                ],
     395            ]
     396        );
     397
     398        $this->assertEquals( [ $n2, $n1 ], wp_list_pluck( $found_1, 'id' ) );
     399
     400        $found_2 = BP_Notifications_Notification::get(
     401            [
     402                'user_id'    => $u,
     403                'order_by'   => 'id',
     404                'sort_order' => 'ASC',
     405                'meta_query' => [
     406                    [
     407                        'key'     => $meta_key,
     408                        'compare' => 'EXISTS'
     409                    ]
     410                ],
     411            ]
     412        );
     413
     414        $this->assertEquals( [ $n1, $n2 ], wp_list_pluck( $found_2, 'id' ) );
     415
     416        $found_3 = BP_Notifications_Notification::get(
     417            [
     418                'user_id'    => $u,
     419                'order_by'   => 'id',
     420                'sort_order' => 'DESC',
     421                'meta_query' => [
     422                    [
     423                        'key'     => $meta_key,
     424                        'compare' => 'NOT EXISTS'
     425                    ]
     426                ],
     427            ]
     428        );
     429
     430        $this->assertEquals( [ $n4, $n3 ], wp_list_pluck( $found_3, 'id' ) );
     431
     432        $found_4 = BP_Notifications_Notification::get(
     433            [
     434                'user_id'    => $u,
     435                'order_by'   => 'id',
     436                'sort_order' => 'ASC',
     437                'meta_query' => [
     438                    [
     439                        'key'     => $meta_key,
     440                        'compare' => 'NOT EXISTS'
     441                    ]
     442                ],
     443            ]
     444        );
     445
     446        $this->assertEquals( [ $n3, $n4 ], wp_list_pluck( $found_4, 'id' ) );
    311447    }
    312448}
Note: See TracChangeset for help on using the changeset viewer.