<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][13397] trunk: Notifications:  returns the proper items when using the  argument.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/13397">13397</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/13397","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>espellcaste</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-01-07 01:09:00 +0000 (Sat, 07 Jan 2023)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="http://buddypress.trac.wordpress.org/ticket/8556">#8556</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpnotificationsclassesclassbpnotificationsnotificationphp">trunk/src/bp-notifications/classes/class-bp-notifications-notification.php</a></li>
<li><a href="#trunktestsphpunittestcasesnotificationsclassbpnotificationsnotificationphp">trunk/tests/phpunit/testcases/notifications/class-bp-notifications-notification.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpnotificationsclassesclassbpnotificationsnotificationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-notifications/classes/class-bp-notifications-notification.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-notifications/classes/class-bp-notifications-notification.php        2023-01-07 00:42:51 UTC (rev 13396)
+++ trunk/src/bp-notifications/classes/class-bp-notifications-notification.php  2023-01-07 01:09:00 UTC (rev 13397)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -666,8 +666,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *                                           or component_action fields.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type string       $order_by          Database column to order notifications by.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type string       $sort_order        Either 'ASC' or 'DESC'.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *     @type string       $order_by          Field to order results by.
-        *     @type string       $sort_order        ASC or DESC.
</del><span class="cx" style="display: block; padding: 0 10px">          *     @type int          $page              Number of the current page of results. Default:
</span><span class="cx" style="display: block; padding: 0 10px">         *                                           false (no pagination - all items).
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int          $per_page          Number of items to show per page. Default:
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -692,16 +690,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // SELECT.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $select_sql = "SELECT *";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $select_sql = "SELECT n.*";
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // FROM.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $from_sql   = "FROM {$bp->notifications->table_name} n ";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $from_sql = "FROM {$bp->notifications->table_name} n ";
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Append meta data to the results.
+               if ( isset( $r['meta_query'][0]['compare'] ) && 'EXISTS' === $r['meta_query'][0]['compare'] ) {
+                       $meta_table = $bp->notifications->table_name_meta;
+                       $select_sql = "SELECT n.*, {$meta_table}.id as meta_id, {$meta_table}.meta_key, {$meta_table}.meta_value";
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // JOIN.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $join_sql   = $meta_query_sql['join'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $join_sql = $meta_query_sql['join'];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // WHERE.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $where_sql  = self::get_where_sql( array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $where_sql = self::get_where_sql( array(
</ins><span class="cx" style="display: block; padding: 0 10px">                         'id'                => $r['id'],
</span><span class="cx" style="display: block; padding: 0 10px">                        'user_id'           => $r['user_id'],
</span><span class="cx" style="display: block; padding: 0 10px">                        'item_id'           => $r['item_id'],
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -728,6 +732,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Concatenate query parts.
</span><span class="cx" style="display: block; padding: 0 10px">                $sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} {$order_sql} {$pag_sql}";
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Perform query.
</ins><span class="cx" style="display: block; padding: 0 10px">                 $results = $wpdb->get_results( $sql );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Integer casting.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -809,11 +814,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.3.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @global wpdb $wpdb WordPress database object.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @param  array $meta_query An array of meta_query filters. See the
</span><span class="cx" style="display: block; padding: 0 10px">         *                           documentation for WP_Meta_Query for details.
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array $sql_array 'join' and 'where' clauses.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public static function get_meta_query_sql( $meta_query = array() ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                global $wpdb;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Default array keys & empty values.
</span><span class="cx" style="display: block; padding: 0 10px">                $sql_array = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -826,11 +834,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $sql_array;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $bp       = buddypress();
+               $meta_sql = new WP_Meta_Query( $meta_query );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // WP_Meta_Query expects the table name at $wpdb->notificationmeta.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $GLOBALS['wpdb']->notificationmeta = buddypress()->notifications->table_name_meta;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $wpdb->notificationmeta = $bp->notifications->table_name_meta;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $n_meta_query = new WP_Meta_Query( $meta_query );
-               $meta_sql     = $n_meta_query->get_sql( 'notification', 'n', 'id' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $meta_sql = $meta_sql->get_sql( 'notification', 'n', 'id' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Strip the leading AND - it's handled in get().
</span><span class="cx" style="display: block; padding: 0 10px">                $sql_array['where'] = preg_replace( '/^\sAND/', '', $meta_sql['where'] );
</span></span></pre></div>
<a id="trunktestsphpunittestcasesnotificationsclassbpnotificationsnotificationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/notifications/class-bp-notifications-notification.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/notifications/class-bp-notifications-notification.php       2023-01-07 00:42:51 UTC (rev 13396)
+++ trunk/tests/phpunit/testcases/notifications/class-bp-notifications-notification.php 2023-01-07 01:09:00 UTC (rev 13397)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4,6 +4,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @group notifications
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class BP_Tests_BP_Notifications_Notification_TestCases extends BP_UnitTestCase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @group get
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -275,12 +276,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Check that the correct items are pulled up
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $expected = array( $n2 );
-               $actual = wp_list_pluck( $n, 'id' );
-               $this->assertEquals( $expected, $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( [ $n2 ], wp_list_pluck( $n, 'id' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @group get
</ins><span class="cx" style="display: block; padding: 0 10px">          * @group pagination
</span><span class="cx" style="display: block; padding: 0 10px">         * @group BP6229
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -290,23 +290,159 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $notifications = array();
</span><span class="cx" style="display: block; padding: 0 10px">                for ( $i = 1; $i <= 6; $i++ ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $notifications[] = self::factory()->notification->create( array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'component_name' => 'activity',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'component_name'    => 'activity',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'secondary_item_id' => $i,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'user_id' => $u,
-                               'is_new' => true,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'user_id'           => $u,
+                               'is_new'            => true,
</ins><span class="cx" style="display: block; padding: 0 10px">                         ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $found = BP_Notifications_Notification::get( array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'user_id' => $u,
-                       'is_new' => true,
-                       'page' => 2,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'user_id'  => $u,
+                       'is_new'   => true,
+                       'page'     => 2,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'per_page' => 2,
</span><span class="cx" style="display: block; padding: 0 10px">                        'order_by' => 'id',
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Check that the correct number of items are pulled up
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $expected = array( $notifications[2], $notifications[3] );
-               $this->assertEquals( $expected, wp_list_pluck( $found, 'id' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals(
+                       [ $notifications[2], $notifications[3] ],
+                       wp_list_pluck( $found, 'id' )
+               );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @group get
+        * @group meta_query
+        */
+       public function test_get_notifications_meta_query() {
+               $u        = self::factory()->user->create();
+               $meta_key = 'foo';
+               $args     = [
+                       'user_id'         => $u,
+                       'component_name'  => 'activity',
+                       'allow_duplicate' => true,
+               ];
+
+               $n1 = bp_notifications_add_notification( $args );
+
+               bp_notifications_add_meta( $n1, $meta_key, 'bar' );
+
+               $n2 = bp_notifications_add_notification( $args );
+
+               $found_1 = BP_Notifications_Notification::get(
+                       [
+                               'user_id'    => $u,
+                               'meta_query' => [
+                                       [
+                                               'key'     => $meta_key,
+                                               'compare' => 'EXISTS'
+                                       ]
+                               ],
+                       ]
+               );
+
+               $this->assertEquals( [ $n1 ], wp_list_pluck( $found_1, 'id' ) );
+
+               $found_2 = BP_Notifications_Notification::get(
+                       [
+                               'user_id'    => $u,
+                               'meta_query' => [
+                                       [
+                                               'key'     => $meta_key,
+                                               'compare' => 'NOT EXISTS'
+                                       ]
+                               ],
+                       ]
+               );
+
+               $this->assertEquals( [ $n2 ], wp_list_pluck( $found_2, 'id' ) );
+       }
+
+       /**
+        * @group get
+        * @group meta_query
+        */
+       public function test_get_notifications_sorted_sql_with_meta_query() {
+               $u        = self::factory()->user->create();
+               $meta_key = 'foo';
+               $args     = [
+                       'user_id'         => $u,
+                       'component_name'  => 'activity',
+                       'allow_duplicate' => true,
+               ];
+
+               $n1 = bp_notifications_add_notification( $args );
+               $n2 = bp_notifications_add_notification( $args );
+               $n3 = bp_notifications_add_notification( $args );
+               $n4 = bp_notifications_add_notification( $args );
+
+               bp_notifications_add_meta( $n1, $meta_key, 'bar' );
+               bp_notifications_add_meta( $n2, $meta_key, 'bar' );
+
+               $found_1 = BP_Notifications_Notification::get(
+                       [
+                               'user_id'    => $u,
+                               'order_by'   => 'id',
+                               'sort_order' => 'DESC',
+                               'meta_query' => [
+                                       [
+                                               'key'     => $meta_key,
+                                               'compare' => 'EXISTS'
+                                       ]
+                               ],
+                       ]
+               );
+
+               $this->assertEquals( [ $n2, $n1 ], wp_list_pluck( $found_1, 'id' ) );
+
+               $found_2 = BP_Notifications_Notification::get(
+                       [
+                               'user_id'    => $u,
+                               'order_by'   => 'id',
+                               'sort_order' => 'ASC',
+                               'meta_query' => [
+                                       [
+                                               'key'     => $meta_key,
+                                               'compare' => 'EXISTS'
+                                       ]
+                               ],
+                       ]
+               );
+
+               $this->assertEquals( [ $n1, $n2 ], wp_list_pluck( $found_2, 'id' ) );
+
+               $found_3 = BP_Notifications_Notification::get(
+                       [
+                               'user_id'    => $u,
+                               'order_by'   => 'id',
+                               'sort_order' => 'DESC',
+                               'meta_query' => [
+                                       [
+                                               'key'     => $meta_key,
+                                               'compare' => 'NOT EXISTS'
+                                       ]
+                               ],
+                       ]
+               );
+
+               $this->assertEquals( [ $n4, $n3 ], wp_list_pluck( $found_3, 'id' ) );
+
+               $found_4 = BP_Notifications_Notification::get(
+                       [
+                               'user_id'    => $u,
+                               'order_by'   => 'id',
+                               'sort_order' => 'ASC',
+                               'meta_query' => [
+                                       [
+                                               'key'     => $meta_key,
+                                               'compare' => 'NOT EXISTS'
+                                       ]
+                               ],
+                       ]
+               );
+
+               $this->assertEquals( [ $n3, $n4 ], wp_list_pluck( $found_4, 'id' ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>