<!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() &gt; 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 =&gt; 'max',
+                                1 =&gt; 'page',
+                                2 =&gt; 'per_page',
+                                3 =&gt; 'sort',
+                                4 =&gt; 'search_terms',
+                                5 =&gt; 'filter',
+                                6 =&gt; 'display_comments',
+                                7 =&gt; 'show_hidden',
+                                8 =&gt; 'exclude',
+                                9 =&gt; 'in',
+                                10 =&gt; 'spam'
+                        );
+
+                        $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
+                }
+
+                $defaults = array(
+                        'page'             =&gt; 1,          // The current page
+                        'per_page'         =&gt; 25,         // Activity items per page
+                        'max'              =&gt; false,      // Max number of items to return
+                        'sort'             =&gt; 'DESC',     // ASC or DESC
+                        'exclude'          =&gt; false,      // Array of ids to exclude
+                        'in'               =&gt; false,      // Array of ids to limit query by (IN)
+                        'filter'           =&gt; false,      // See self::get_filter_sql()
+                        'search_terms'     =&gt; false,      // Terms to search by
+                        'display_comments' =&gt; false,      // Whether to include activity comments
+                        'show_hidden'      =&gt; false,      // Show items marked hide_sitewide
+                        'spam'             =&gt; 'ham_only', // Spam status
+                );
+                $r = wp_parse_args( $args, $defaults );
+                extract( $r );
+
</ins><span class="cx">                 // Select conditions
</span><span class="cx">                 $select_sql = &quot;SELECT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name&quot;;
</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 &quot;in&quot; 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 &amp;&amp; empty( $max ) &amp;&amp; empty( $search_terms ) &amp;&amp; empty( $filter ) &amp;&amp; empty( $exclude ) &amp;&amp; empty( $in ) &amp;&amp; 'DESC' == $sort &amp;&amp; empty( $exclude ) &amp;&amp; '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'             =&gt; $page,
+                                'per_page'         =&gt; $per_page,
+                                'max'              =&gt; $max,
+                                'sort'             =&gt; $sort,
+                                'search_terms'     =&gt; $search_terms,
+                                'filter'           =&gt; $filter,
+                                'display_comments' =&gt; $display_comments,
+                                'show_hidden'      =&gt; $show_hidden,
+                                'spam'             =&gt; $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'             =&gt; $page,
+                        'per_page'         =&gt; $per_page,
+                        'max'              =&gt; $max,
+                        'sort'             =&gt; $sort,
+                        'search_terms'     =&gt; $search_terms,
+                        'filter'           =&gt; $filter,
+                        'display_comments' =&gt; $display_comments,
+                        'show_hidden'      =&gt; $show_hidden,
+                        'exclude'          =&gt; $exclude,
+                        'in'               =&gt; $in,
+                        'spam'             =&gt; $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( &amp;$activity, &amp;$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'             =&gt; $page,
+                'per_page'         =&gt; $per_page,
+                'max'              =&gt; $max,
+                'sort'             =&gt; $sort,
+                'display_comments' =&gt; $display_comments,
+                'show_hidden'      =&gt; $show_hidden,
+                'in'               =&gt; $activity_ids,
+                'spam'             =&gt; $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() &gt; 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 =&gt; 'page',
+                                1 =&gt; 'per_page',
+                                2 =&gt; 'max',
+                                3 =&gt; 'include',
+                                4 =&gt; 'sort',
+                                5 =&gt; 'filter',
+                                6 =&gt; 'search_terms',
+                                7 =&gt; 'display_comments',
+                                8 =&gt; 'show_hidden',
+                                9 =&gt; 'exclude',
+                                10 =&gt; 'in',
+                                11 =&gt; 'spam',
+                                12 =&gt; 'page_arg'
+                        );
+                        
+                        $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
+                }
+                
+                $defaults = array(
+                        'page'             =&gt; 1,
+                        'per_page'         =&gt; 20,
+                        'page_arg'         =&gt; 'acpage',
+                        'max'              =&gt; false,
+                        'sort'             =&gt; false,
+                        'include'          =&gt; false,
+                        'exclude'          =&gt; false,
+                        'in'               =&gt; false,
+                        'filter'           =&gt; false,
+                        'search_terms'     =&gt; false,
+                        'display_comments' =&gt; 'threaded',
+                        'show_hidden'      =&gt; false,
+                        'spam'             =&gt; 'ham_only',
+                );
+                $r = wp_parse_args( $args, $defaults );
+                extract( $r );
+                
</ins><span class="cx">                 $this-&gt;pag_page = isset( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : $page;
</span><span class="cx">                 $this-&gt;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 ) &amp;&amp; ( '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'             =&gt; $page,
+                'per_page'         =&gt; $per_page,
+                'page_arg'         =&gt; $page_arg,
+                'max'              =&gt; $max,
+                'sort'             =&gt; $sort,
+                'include'          =&gt; $include,
+                'exclude'          =&gt; $exclude,
+                'in'               =&gt; $in,
+                'filter'           =&gt; $filter,
+                'search_terms'     =&gt; $search_terms,
+                'display_comments' =&gt; $display_comments,
+                'show_hidden'      =&gt; $show_hidden,
+                'spam'             =&gt; $spam
+        );
</ins><span class="cx"> 
</span><del>-        return apply_filters( 'bp_has_activities', $activities_template-&gt;has_activities(), $activities_template );
</del><ins>+        $activities_template = new BP_Activity_Template( $template_args );
+
+        return apply_filters( 'bp_has_activities', $activities_template-&gt;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'  =&gt; 1,
+ *           'arg2' =&gt; false,
+ *           'arg3' =&gt; array(),
+ *           'arg4' =&gt; 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 =&gt; 'bar', // because $bar was the 0th parameter for foo()
+ *       1 =&gt; 'baz', // because $baz was the 1st parameter for foo()
+ *       2 =&gt; 'barry', // etc
+ *       3 =&gt; '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 =&gt; $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>