Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/09/2013 05:04:41 PM (13 years ago)
Author:
johnjamesjacoby
Message:

Introduce BP_Notifications_Notification methods for handling LIMIT and ORDER BY MySQL to act as the foundation for customizing sorting and pagination arguments. Also flip default notification order from ASC to DESC for a top-down view. See #5148.

File:
1 edited

Legend:

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

    r7536 r7549  
    255255
    256256                $where_conditions = array();
     257                $where            = '';
    257258
    258259                // id
     
    327328                }
    328329
     330                // Custom WHERE
    329331                if ( ! empty( $where_conditions ) ) {
    330332                        $where = 'WHERE ' . implode( ' AND ', $where_conditions );
    331                 } else {
    332                         $where = '';
    333333                }
    334334
    335335                return $where;
     336        }
     337
     338        /**
     339         * Assemble the ORDER BY clause of a get() SQL statement.
     340         *
     341         * Used by BP_Notifications_Notification::get() to create its ORDER BY
     342         * clause.
     343         *
     344         * @since BuddyPress (1.9.0)
     345         *
     346         * @param array $args See {@link BP_Notifications_Notification::get()}
     347         *        for more details.
     348         * @return string ORDER BY clause.
     349         */
     350        protected static function get_order_by_sql( $args = array() ) {
     351
     352                // Setup local variable
     353                $conditions = array();
     354                $retval     = '';
     355
     356                // Order by
     357                if ( ! empty( $args['order_by'] ) ) {
     358                        $order_by               = implode( ', ', (array) $args['order_by'] );
     359                        $conditions['order_by'] = "{$order_by}";
     360                }
     361
     362                // Sort order direction
     363                if ( ! empty( $args['sort_order'] ) && in_array( $args['sort_order'], array( 'ASC', 'DESC' ) ) ) {
     364                        $sort_order               = $args['sort_order'];
     365                        $conditions['sort_order'] = "{$sort_order}";
     366                }
     367
     368                // Custom ORDER BY
     369                if ( ! empty( $conditions ) ) {
     370                        $retval = 'ORDER BY ' . implode( ' ', $conditions );
     371                }
     372
     373                return $retval;
     374        }
     375
     376        /**
     377         * Assemble the LIMIT clause of a get() SQL statement.
     378         *
     379         * Used by BP_Notifications_Notification::get() to create its LIMIT clause.
     380         *
     381         * @since BuddyPress (1.9.0)
     382         *
     383         * @param array $args See {@link BP_Notifications_Notification::get()}
     384         *        for more details.
     385         * @return string LIMIT clause.
     386         */
     387        protected static function get_paged_sql( $args = array() ) {
     388                global $wpdb;
     389
     390                // Setup local variable
     391                $retval = '';
     392
     393                // Custom LIMIT
     394                if ( ! empty( $args['page'] ) && ! empty( $args['per_page'] ) ) {
     395                        $page     = absint( $args['page']     );
     396                        $per_page = absint( $args['per_page'] );
     397                        $offset   = $per_page * ( $page - 1 );
     398                        $retval   = $wpdb->prepare( "LIMIT %d, %d", $offset, $per_page );
     399                }
     400
     401                return $retval;
    336402        }
    337403
     
    482548                global $wpdb;
    483549
    484                 $r = wp_parse_args( $args, array(
     550                // Parse the arguments
     551                $r  = wp_parse_args( $args, array(
    485552                        'id'                => false,
    486553                        'user_id'           => false,
     
    491558                        'is_new'            => true,
    492559                        'search_terms'      => '',
     560                        'order_by'          => false,
     561                        'sort_order'        => false,
    493562                        'page'              => false,
    494563                        'per_page'          => false,
    495564                ) );
    496565
    497                 $bp         = buddypress();
    498                 $pag_sql    = '';
     566                // SELECT
    499567                $select_sql = "SELECT *";
    500                 $from_sql   = "FROM {$bp->notifications->table_name}";
     568
     569                // FROM
     570                $from_sql   = "FROM " . buddypress()->notifications->table_name;
     571
     572                // WHERE
    501573                $where_sql  = self::get_where_sql( array(
    502574                        'id'                => $r['id'],
     
    510582                ) );
    511583
    512                 if ( ! empty( $r['page'] ) && ! empty( $r['per_page'] ) ) {
    513                         $page     = absint( $r['page']     );
    514                         $per_page = absint( $r['per_page'] );
    515                         $offset   = $per_page * ( $page - 1 );
    516                         $pag_sql  = $wpdb->prepare( "LIMIT %d, %d", $offset, $per_page );
    517                 }
    518 
    519                 $sql = "{$select_sql} {$from_sql} {$where_sql} {$pag_sql}";
     584                // ORDER BY
     585                $order_sql  = self::get_order_by_sql( array(
     586                        'order_by'   => $r['order_by'],
     587                        'sort_order' => $r['sort_order']
     588                ) );
     589
     590                // LIMIT %d, %d
     591                $pag_sql    = self::get_paged_sql( array(
     592                        'page'     => $r['page'],
     593                        'per_page' => $r['per_page'],
     594                ) );
     595
     596                $sql = "{$select_sql} {$from_sql} {$where_sql} {$order_sql} {$pag_sql}";
    520597
    521598                return $wpdb->get_results( $sql );
Note: See TracChangeset for help on using the changeset viewer.