<!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][5993] trunk: Refactors bp_has_activities() stack to support the passing of arguments as associative arrays.</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 { 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, #logmsg > ol { margin-left: 0; 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">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/5993">5993</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2012-04-17 14:01:16 +0000 (Tue, 17 Apr 2012)</dd>
</dl>
<h3>Log Message</h3>
<pre>Refactors bp_has_activities() stack to support the passing of arguments as associative arrays.
- Maintains backward compatibility by parsing old-style parameters using bp_core_parse_args_array(), converting to new-style array, and throwing _deprecated_argument() error
- Changes calls to BP_Activity_Template::__construct() and BP_Activity_Activity::get() to use the new format throughout BuddyPress
- See <a href="http://buddypress.trac.wordpress.org/ticket/3797">#3797</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpactivitybpactivityclassesphp">trunk/bp-activity/bp-activity-classes.php</a></li>
<li><a href="#trunkbpactivitybpactivityfunctionsphp">trunk/bp-activity/bp-activity-functions.php</a></li>
<li><a href="#trunkbpactivitybpactivitytemplatephp">trunk/bp-activity/bp-activity-template.php</a></li>
<li><a href="#trunkbpcorebpcorefunctionsphp">trunk/bp-core/bp-core-functions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpactivitybpactivityclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-classes.php (5992 => 5993)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-classes.php        2012-04-15 03:57:10 UTC (rev 5992)
+++ trunk/bp-activity/bp-activity-classes.php        2012-04-17 14:01:16 UTC (rev 5993)
</span><span class="lines">@@ -100,9 +100,53 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         // Static Functions
</span><del>-        function get( $max = false, $page = 1, $per_page = 25, $sort = 'DESC', $search_terms = false, $filter = false, $display_comments = false, $show_hidden = false, $exclude = false, $in = false, $spam = 'ham_only' ) {
</del><ins>+
+        /**
+         * Get activity items, as specified by parameters
+         *
+         * @param array $args See $defaults for explanation of arguments
+         * @return array
+         */
+        function get( $args = array() ) {
</ins><span class="cx">                 global $wpdb, $bp;
</span><span class="cx">
</span><ins>+                // Backward compatibility with old method of passing arguments
+                if ( !is_array( $args ) || func_num_args() > 1 ) {
+                        _deprecated_argument( __METHOD__, '1.6', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
+
+                        $old_args_keys = array(
+                                0 => 'max',
+                                1 => 'page',
+                                2 => 'per_page',
+                                3 => 'sort',
+                                4 => 'search_terms',
+                                5 => 'filter',
+                                6 => 'display_comments',
+                                7 => 'show_hidden',
+                                8 => 'exclude',
+                                9 => 'in',
+                                10 => 'spam'
+                        );
+
+                        $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
+                }
+
+                $defaults = array(
+                        'page' => 1, // The current page
+                        'per_page' => 25, // Activity items per page
+                        'max' => false, // Max number of items to return
+                        'sort' => 'DESC', // ASC or DESC
+                        'exclude' => false, // Array of ids to exclude
+                        'in' => false, // Array of ids to limit query by (IN)
+                        'filter' => false, // See self::get_filter_sql()
+                        'search_terms' => false, // Terms to search by
+                        'display_comments' => false, // Whether to include activity comments
+                        'show_hidden' => false, // Show items marked hide_sitewide
+                        'spam' => 'ham_only', // Spam status
+                );
+                $r = wp_parse_args( $args, $defaults );
+                extract( $r );
+
</ins><span class="cx">                 // Select conditions
</span><span class="cx">                 $select_sql = "SELECT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name";
</span><span class="cx">
</span><span class="lines">@@ -236,10 +280,10 @@
</span><span class="cx">
</span><span class="cx">         /**
</span><span class="cx">          * In BuddyPress 1.2.x, this was used to retrieve specific activity stream items (for example, on an activity's permalink page).
</span><del>-         * As of 1.5.x, use BP_Activity_Activity::get( ..., $in ) instead.
</del><ins>+         * As of 1.5.x, use BP_Activity_Activity::get() with an 'in' parameter instead.
</ins><span class="cx">          *
</span><span class="cx">          * @deprecated 1.5
</span><del>-         * @deprecated Use BP_Activity_Activity::get( ..., $in ) instead.
</del><ins>+         * @deprecated Use BP_Activity_Activity::get() with an 'in' parameter instead.
</ins><span class="cx">          * @param mixed $activity_ids Array or comma-separated string of activity IDs to retrieve
</span><span class="cx">          * @param int $max Maximum number of results to return. (Optional; default is no maximum)
</span><span class="cx">          * @param int $page The set of results that the user is viewing. Used in pagination. (Optional; default is 1)
</span><span class="lines">@@ -250,7 +294,7 @@
</span><span class="cx">          * @since 1.2
</span><span class="cx">          */
</span><span class="cx">         function get_specific( $activity_ids, $max = false, $page = 1, $per_page = 25, $sort = 'DESC', $display_comments = false ) {
</span><del>-                _deprecated_function( __FUNCTION__, '1.5', 'Use BP_Activity_Activity::get( ..., $in ) instead.' );
</del><ins>+                _deprecated_function( __FUNCTION__, '1.5', 'Use BP_Activity_Activity::get() with the "in" parameter instead.' );
</ins><span class="cx">                 return BP_Activity_Activity::get( $max, $page, $per_page, $sort, false, false, $display_comments, false, false, $activity_ids );
</span><span class="cx">         }
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpactivitybpactivityfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-functions.php (5992 => 5993)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-functions.php        2012-04-15 03:57:10 UTC (rev 5992)
+++ trunk/bp-activity/bp-activity-functions.php        2012-04-17 14:01:16 UTC (rev 5993)
</span><span class="lines">@@ -820,12 +820,36 @@
</span><span class="cx">         // Attempt to return a cached copy of the first page of sitewide activity.
</span><span class="cx">         if ( 1 == (int) $page && empty( $max ) && empty( $search_terms ) && empty( $filter ) && empty( $exclude ) && empty( $in ) && 'DESC' == $sort && empty( $exclude ) && 'ham_only' == $spam ) {
</span><span class="cx">                 if ( !$activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' ) ) {
</span><del>-                        $activity = BP_Activity_Activity::get( $max, $page, $per_page, $sort, $search_terms, $filter, $display_comments, $show_hidden, false, false, $spam );
</del><ins>+                        $args = array(
+                                'page' => $page,
+                                'per_page' => $per_page,
+                                'max' => $max,
+                                'sort' => $sort,
+                                'search_terms' => $search_terms,
+                                'filter' => $filter,
+                                'display_comments' => $display_comments,
+                                'show_hidden' => $show_hidden,
+                                'spam' => $spam
+                        );
+                        $activity = BP_Activity_Activity::get( $args );
</ins><span class="cx">                         wp_cache_set( 'bp_activity_sitewide_front', $activity, 'bp' );
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">         } else {
</span><del>-                $activity = BP_Activity_Activity::get( $max, $page, $per_page, $sort, $search_terms, $filter, $display_comments, $show_hidden, $exclude, $in, $spam );
</del><ins>+                $args = array(
+                        'page' => $page,
+                        'per_page' => $per_page,
+                        'max' => $max,
+                        'sort' => $sort,
+                        'search_terms' => $search_terms,
+                        'filter' => $filter,
+                        'display_comments' => $display_comments,
+                        'show_hidden' => $show_hidden,
+                        'exclude' => $exclude,
+                        'in' => $in,
+                        'spam' => $spam
+                );
+                $activity = BP_Activity_Activity::get( $args );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         return apply_filters_ref_array( 'bp_activity_get', array( &$activity, &$r ) );
</span><span class="lines">@@ -858,7 +882,17 @@
</span><span class="cx">         $r = wp_parse_args( $args, $defaults );
</span><span class="cx">         extract( $r, EXTR_SKIP );
</span><span class="cx">
</span><del>-        return apply_filters( 'bp_activity_get_specific', BP_Activity_Activity::get( $max, $page, $per_page, $sort, false, false, $display_comments, $show_hidden, false, $activity_ids, $spam ) );
</del><ins>+        $get_args = array(
+                'page' => $page,
+                'per_page' => $per_page,
+                'max' => $max,
+                'sort' => $sort,
+                'display_comments' => $display_comments,
+                'show_hidden' => $show_hidden,
+                'in' => $activity_ids,
+                'spam' => $spam
+        );
+        return apply_filters( 'bp_activity_get_specific', BP_Activity_Activity::get( $get_args ), $args, $get_args );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkbpactivitybpactivitytemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-template.php (5992 => 5993)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-template.php        2012-04-15 03:57:10 UTC (rev 5992)
+++ trunk/bp-activity/bp-activity-template.php        2012-04-17 14:01:16 UTC (rev 5993)
</span><span class="lines">@@ -104,9 +104,58 @@
</span><span class="cx">
</span><span class="cx">         var $full_name;
</span><span class="cx">
</span><del>-        function __construct( $page, $per_page, $max, $include, $sort, $filter, $search_terms, $display_comments, $show_hidden, $exclude = false, $in = false, $spam = 'ham_only', $page_arg = 'acpage' ) {
</del><ins>+        /**
+         * Constructor method
+         *
+         * See definition of $defaults below, as well as $defaults in bp_has_activities(), for
+         * description of $args array
+         *
+         * @param array $args
+         */
+        function __construct( $args ) {
</ins><span class="cx">                 global $bp;
</span><span class="cx">
</span><ins>+                // Backward compatibility with old method of passing arguments
+                if ( !is_array( $args ) || func_num_args() > 1 ) {
+                        _deprecated_argument( __METHOD__, '1.6', sprintf( __( 'Arguments passed to %s should be in an associative array. See the inline documentation for more details.', 'buddypress' ), __METHOD__ ) );
+                
+                        $old_args_keys = array(
+                                0 => 'page',
+                                1 => 'per_page',
+                                2 => 'max',
+                                3 => 'include',
+                                4 => 'sort',
+                                5 => 'filter',
+                                6 => 'search_terms',
+                                7 => 'display_comments',
+                                8 => 'show_hidden',
+                                9 => 'exclude',
+                                10 => 'in',
+                                11 => 'spam',
+                                12 => 'page_arg'
+                        );
+                        
+                        $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
+                }
+                
+                $defaults = array(
+                        'page' => 1,
+                        'per_page' => 20,
+                        'page_arg' => 'acpage',
+                        'max' => false,
+                        'sort' => false,
+                        'include' => false,
+                        'exclude' => false,
+                        'in' => false,
+                        'filter' => false,
+                        'search_terms' => false,
+                        'display_comments' => 'threaded',
+                        'show_hidden' => false,
+                        'spam' => 'ham_only',
+                );
+                $r = wp_parse_args( $args, $defaults );
+                extract( $r );
+                
</ins><span class="cx">                 $this->pag_page = isset( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : $page;
</span><span class="cx">                 $this->pag_num = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;
</span><span class="cx">
</span><span class="lines">@@ -389,9 +438,25 @@
</span><span class="cx">         if ( !empty( $include ) && ( 'ham_only' == $spam ) )
</span><span class="cx">                 $spam = 'all';
</span><span class="cx">
</span><del>-        $activities_template = new BP_Activity_Template( $page, $per_page, $max, $include, $sort, $filter, $search_terms, $display_comments, $show_hidden, $exclude, $in, $spam, $page_arg );
</del><ins>+        $template_args = array(
+                'page' => $page,
+                'per_page' => $per_page,
+                'page_arg' => $page_arg,
+                'max' => $max,
+                'sort' => $sort,
+                'include' => $include,
+                'exclude' => $exclude,
+                'in' => $in,
+                'filter' => $filter,
+                'search_terms' => $search_terms,
+                'display_comments' => $display_comments,
+                'show_hidden' => $show_hidden,
+                'spam' => $spam
+        );
</ins><span class="cx">
</span><del>-        return apply_filters( 'bp_has_activities', $activities_template->has_activities(), $activities_template );
</del><ins>+        $activities_template = new BP_Activity_Template( $template_args );
+
+        return apply_filters( 'bp_has_activities', $activities_template->has_activities(), $activities_template, $template_args );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkbpcorebpcorefunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-functions.php (5992 => 5993)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-functions.php        2012-04-15 03:57:10 UTC (rev 5992)
+++ trunk/bp-core/bp-core-functions.php        2012-04-17 14:01:16 UTC (rev 5993)
</span><span class="lines">@@ -953,6 +953,53 @@
</span><span class="cx">         return apply_filters( 'bp_use_wp_admin_bar', $use_admin_bar );
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/**
+ * A utility for parsing individual function arguments into an array.
+ *
+ * The purpose of this function is to help with backward compatibility in cases where
+ *
+ * function foo( $bar = 1, $baz = false, $barry = array(), $blip = false ) { // ...
+ *
+ * is deprecated in favor of
+ *
+ * function foo( $args = array() ) {
+ * $defaults = array(
+ * 'bar' => 1,
+ * 'arg2' => false,
+ * 'arg3' => array(),
+ * 'arg4' => false,
+ * );
+ * $r = wp_parse_args( $args, $defaults ); // ...
+ *
+ * The first argument, $old_args_keys, is an array that matches the parameter positions (keys) to
+ * the new $args keys (values):
+ *
+ * $old_args_keys = array(
+ * 0 => 'bar', // because $bar was the 0th parameter for foo()
+ * 1 => 'baz', // because $baz was the 1st parameter for foo()
+ * 2 => 'barry', // etc
+ * 3 => 'blip'
+ * );
+ *
+ * For the second argument, $func_args, you should just pass func_get_args().
+ *
+ * @since BuddyPress (1.6)
+ * @param array $old_args_keys
+ * @param array $func_args
+ * @return array $new_args
+ */
+function bp_core_parse_args_array( $old_args_keys, $func_args ) {
+        $new_args = array();
+
+        foreach( $old_args_keys as $arg_num => $arg_key ) {
+                if ( isset( $func_args[$arg_num] ) ) {
+                        $new_args[$arg_key] = $func_args[$arg_num];
+                }
+        }
+
+        return $new_args;
+}
+
</ins><span class="cx"> /** Embeds ********************************************************************/
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>
</body>
</html>