<!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][13181] trunk/src/bp-activity: Generate a richer content for "no-content" activities</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/13181">13181</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/13181","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-12-12 02:58:38 +0000 (Sun, 12 Dec 2021)</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'>Generate a richer content for "no-content" activities

Create the "generated-content" support for the following activity types: 'new_member', 'new_avatar', 'friendship_created', 'created_group', 'joined_group' & 'updated_profile'.

- Introduce the `bp_activity_type_part()` function to return the name of the activity type template part.
- edit the `bp_activity_has_content()` function to generate a richer content for activity types supporting the feature. For such activities the `$activities_template` global is extended adding a `generated_content` property to each activity in the loop having a type supporting the "generated-content" feature.
- Introduce the `bp_activity_generated_content_part()` template tag which use the current activity `generated_content` property to output the requested information passed as the function parameter.

Props vapvarun, oztaser

See <a href="http://buddypress.trac.wordpress.org/ticket/8581">#8581</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpactivitybpactivityfunctionsphp">trunk/src/bp-activity/bp-activity-functions.php</a></li>
<li><a href="#trunksrcbpactivitybpactivitytemplatephp">trunk/src/bp-activity/bp-activity-template.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpactivitybpactivityfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-activity/bp-activity-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-activity/bp-activity-functions.php   2021-12-11 19:40:10 UTC (rev 13180)
+++ trunk/src/bp-activity/bp-activity-functions.php     2021-12-12 02:58:38 UTC (rev 13181)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -709,6 +709,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $retval = true;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                        break;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               /**
+                * Does this activity type support `generated-content`?
+                */
+               case 'generated-content' :
+                       /*
+                        * @todo `bp_activity_set_action()` should be improved to include a supports
+                        * argument or best we should create a `bp_register_activity_type()` function
+                        * to mimic the way WordPress registers post types. For now we'll use a non
+                        * extendable workaround.
+                        */
+                       $activity_types = array( 'new_member', 'new_avatar' );
+
+                       if ( bp_is_active( 'friends' ) ) {
+                               array_push( $activity_types, 'friendship_created' );
+                       }
+
+                       if ( bp_is_active( 'groups' ) ) {
+                               array_push( $activity_types, 'created_group', 'joined_group' );
+                       }
+
+                       if ( bp_is_active( 'xprofile' ) ) {
+                               array_push( $activity_types, 'updated_profile' );
+                       }
+
+                       $retval = in_array( $activity_type, $activity_types, true );
+                       break;
</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">        return $retval;
</span></span></pre></div>
<a id="trunksrcbpactivitybpactivitytemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-activity/bp-activity-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-activity/bp-activity-template.php    2021-12-11 19:40:10 UTC (rev 13180)
+++ trunk/src/bp-activity/bp-activity-template.php      2021-12-12 02:58:38 UTC (rev 13181)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -889,6 +889,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return apply_filters( 'bp_get_activity_type', $activities_template->activity->type );
</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">+/**
+ * Return the activity type template part name.
+ *
+ * @since 10.0.0
+ *
+ * @global object $activities_template {@link BP_Activity_Template}
+ *
+ * @return string The activity type template part name.
+ */
+function bp_activity_type_part() {
+       global $activities_template;
+
+       $name = '';
+       if ( isset( $activities_template->activity->type ) && $activities_template->activity->type ) {
+               $name = str_replace( '_', '-', $activities_template->activity->type );
+       }
+
+       return $name;
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Output the activity action name.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1384,7 +1404,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Output the activity content body.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 1.2.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function bp_activity_content_body() {
</span><span class="cx" style="display: block; padding: 0 10px">        echo bp_get_activity_content_body();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1422,6 +1441,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Does the activity have content?
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 1.2.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 10.0.0 Generate a richer content for activity types supporting the feature.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global object $activities_template {@link BP_Activity_Template}
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1430,14 +1450,225 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function bp_activity_has_content() {
</span><span class="cx" style="display: block; padding: 0 10px">        global $activities_template;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! empty( $activities_template->activity->content ) ) {
-               return true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $has_content = ! empty( $activities_template->activity->content );
+       if ( ! $has_content ) {
+               $activity_type = bp_get_activity_type();
+
+               if ( bp_activity_type_supports( $activity_type, 'generated-content' ) ) {
+                       $bp                = buddypress();
+                       $generated_content = new stdClass();
+                       $activity          = $activities_template->activity;
+                       $user_id           = $activity->user_id;
+
+                       // Set generated content properties.
+                       if ( 'new_avatar' === $activity_type ) {
+                               $avatars = bp_avatar_get_version( $user_id, 'user', bp_get_activity_date_recorded() );
+
+                               if ( $avatars && 1 === count( $avatars ) ) {
+                                       $avatar            = reset( $avatars );
+                                       $historical_avatar = trailingslashit( $avatar->parent_dir_url ) . $avatar->name;
+
+                                       // Add historical avatar to the current activity.
+                                       $generated_content->user_profile_photo = array(
+                                               'value'             => $historical_avatar,
+                                               'sanitize_callback' => 'esc_url',
+                                       );
+
+                                       // Do not use a generated content.
+                               } else {
+                                       return false;
+                               }
+                       }
+
+                       if ( in_array( $activity_type, array( 'new_member', 'friendship_created', 'updated_profile' ), true ) ) {
+                               if ( 'friendship_created' === $activity_type ) {
+                                       $user_id = $activity->secondary_item_id;
+                               }
+
+                               if ( isset( $bp->avatar->show_avatars ) && $bp->avatar->show_avatars ) {
+                                       $generated_content->user_profile_photo = array(
+                                               'value'             => bp_core_fetch_avatar(
+                                                       array(
+                                                               'item_id' => $user_id,
+                                                               'object'  => 'user',
+                                                               'type'    => 'full',
+                                                               'width'   => bp_core_avatar_full_width(),
+                                                               'height'  => bp_core_avatar_full_height(),
+                                                               'html'    => false,
+                                                       )
+                                               ),
+                                               'sanitize_callback' => 'esc_url',
+                                       );
+                               }
+                       }
+
+                       // Set common generated content properties.
+                       if ( in_array( $activity_type, array( 'new_avatar', 'new_member', 'friendship_created', 'updated_profile' ), true ) ) {
+                               $generated_content->user_url = array(
+                                       'value'             => bp_core_get_user_domain( $user_id ),
+                                       'sanitize_callback' => 'esc_url',
+                               );
+
+                               $generated_content->user_display_name = array(
+                                       'value'             => bp_core_get_user_displayname( $user_id ),
+                                       'sanitize_callback' => 'esc_html',
+                               );
+
+                               $generated_content->user_mention_name = array(
+                                       'value'             => bp_activity_get_user_mentionname( $user_id ),
+                                       'sanitize_callback' => 'esc_html',
+                               );
+
+                               $generated_content->user_mention_url = array(
+                                       'value'             => wp_nonce_url(
+                                               add_query_arg(
+                                                       array(
+                                                               'r' => $generated_content->user_mention_name['value'],
+                                                       ),
+                                                       bp_get_activity_directory_permalink()
+                                               )
+                                       ),
+                                       'sanitize_callback' => 'esc_url',
+                               );
+
+                               if ( bp_displayed_user_use_cover_image_header() ) {
+                                       $generated_content->user_cover_image = array(
+                                               'value'             => bp_attachments_get_attachment(
+                                                       'url',
+                                                       array(
+                                                               'object_dir' => 'members',
+                                                               'item_id'    => $user_id,
+                                                       )
+                                               ),
+                                               'sanitize_callback' => 'esc_url',
+                                       );
+                               }
+                       }
+
+                       if ( 'created_group' === $activity_type || 'joined_group' === $activity_type ) {
+                               $group = bp_get_group( $activity->item_id );
+
+                               if ( isset( $bp->avatar->show_avatars ) && $bp->avatar->show_avatars && ! bp_disable_group_avatar_uploads() ) {
+                                       $generated_content->group_profile_photo = array(
+                                               'value'             => bp_core_fetch_avatar(
+                                                       array(
+                                                               'item_id' => $group->id,
+                                                               'object'  => 'group',
+                                                               'type'    => 'full',
+                                                               'width'   => bp_core_avatar_full_width(),
+                                                               'height'  => bp_core_avatar_full_height(),
+                                                               'html'    => false,
+                                                       )
+                                               ),
+                                               'sanitize_callback' => 'esc_url',
+                                       );
+                               }
+
+                               $generated_content->group_url = array(
+                                       'value'             => bp_get_group_permalink( $group ),
+                                       'sanitize_callback' => 'esc_url',
+                               );
+
+                               $generated_content->group_name = array(
+                                       'value'             => bp_get_group_name( $group ),
+                                       'sanitize_callback' => 'esc_html',
+                               );
+
+                               if ( bp_group_use_cover_image_header() ) {
+                                       $generated_content->group_cover_image = array(
+                                               'value'             => bp_get_group_cover_url( $group ),
+                                               'sanitize_callback' => 'esc_url',
+                                       );
+                               }
+                       }
+
+                       // Update the corresponding entry into the activities template global.
+                       if ( get_object_vars( $generated_content ) ) {
+                               $activity_id    = $activities_template->activity->id;
+                               $activity_index = 0;
+
+                               // Find the activity index.
+                               while ( (int) $activities_template->activities[ $activity_index ]->id !== (int) $activity_id ) {
+                                       $activity_index++;
+                               }
+
+                               // Add the generated content object.
+                               $activities_template->activities[ $activity_index ]->generated_content = $generated_content;
+                               $has_content = true;
+                       }
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $has_content;
</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">+ * Does this property has been generated?
+ *
+ * @since 10.0.0
+ *
+ * @param string $property The name of the property to check into the generated content.
+ * @return bool            True if the property is not empty. False otherwise.
+ */
+function bp_activity_has_generated_content_part( $property = '' ) {
+       return bp_activity_get_generated_content_part( $property, 'boolean' );
+}
+
+/**
+ * Outputs a property of the activity generated content.
+ *
+ * @since 10.0.0
+ *
+ * @param string $property The name of the property to check into the generated content.
+ */
+function bp_activity_generated_content_part( $property = '' ) {
+       echo bp_activity_get_generated_content_part( $property );
+}
+
+       /**
+        * Returns the property of the activity generated content.
+        *
+        * @since 10.0.0
+        *
+        * @param string $property The name of the property to check into the generated content.
+        * @param string $return   Whether to return the property value or a boolean to check it exists.
+        * @return bool|string     A boolean when requested, false if there is no value, the HTML output otherwise.
+        */
+       function bp_activity_get_generated_content_part( $property = '', $return = '' ) {
+               global $activities_template;
+
+               if ( ! isset( $activities_template->activity->generated_content->{$property} ) ) {
+                       return false;
+               }
+
+               $content_part = $activities_template->activity->generated_content->{$property};
+
+               if ( ! isset( $content_part['value'] ) || ! $content_part['value'] ) {
+                       return false;
+               }
+
+               if ( 'boolean' === $return ) {
+                       return true;
+               }
+
+               /**
+                * Filter here to edit the generated content part.
+                *
+                * @since 10.0.0
+                *
+                * @param string $value    The generated content part.
+                * @param string $property The property the content part is attached to.
+                */
+               $value = apply_filters( 'bp_activity_get_generated_content_part', $content_part['value'], $property );
+
+               if ( isset( $content_part['sanitize_callback'] ) && $content_part['sanitize_callback'] ) {
+                       return call_user_func( $content_part['sanitize_callback'], $value );
+               }
+
+               return $value;
+       }
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Output the activity content.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 1.0.0
</span></span></pre>
</div>
</div>

</body>
</html>