Skip to:
Content

BuddyPress.org

Changeset 13181


Ignore:
Timestamp:
12/12/2021 02:58:38 AM (3 years ago)
Author:
imath
Message:

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 #8581

Location:
trunk/src/bp-activity
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-functions.php

    r13150 r13181  
    709709                $retval = true;
    710710            }
     711            break;
     712
     713        /**
     714         * Does this activity type support `generated-content`?
     715         */
     716        case 'generated-content' :
     717            /*
     718             * @todo `bp_activity_set_action()` should be improved to include a supports
     719             * argument or best we should create a `bp_register_activity_type()` function
     720             * to mimic the way WordPress registers post types. For now we'll use a non
     721             * extendable workaround.
     722             */
     723            $activity_types = array( 'new_member', 'new_avatar' );
     724
     725            if ( bp_is_active( 'friends' ) ) {
     726                array_push( $activity_types, 'friendship_created' );
     727            }
     728
     729            if ( bp_is_active( 'groups' ) ) {
     730                array_push( $activity_types, 'created_group', 'joined_group' );
     731            }
     732
     733            if ( bp_is_active( 'xprofile' ) ) {
     734                array_push( $activity_types, 'updated_profile' );
     735            }
     736
     737            $retval = in_array( $activity_type, $activity_types, true );
    711738            break;
    712739    }
  • trunk/src/bp-activity/bp-activity-template.php

    r13114 r13181  
    890890    }
    891891
     892/**
     893 * Return the activity type template part name.
     894 *
     895 * @since 10.0.0
     896 *
     897 * @global object $activities_template {@link BP_Activity_Template}
     898 *
     899 * @return string The activity type template part name.
     900 */
     901function bp_activity_type_part() {
     902    global $activities_template;
     903
     904    $name = '';
     905    if ( isset( $activities_template->activity->type ) && $activities_template->activity->type ) {
     906        $name = str_replace( '_', '-', $activities_template->activity->type );
     907    }
     908
     909    return $name;
     910}
     911
    892912    /**
    893913     * Output the activity action name.
     
    13851405 *
    13861406 * @since 1.2.0
    1387  *
    13881407 */
    13891408function bp_activity_content_body() {
     
    14231442 *
    14241443 * @since 1.2.0
     1444 * @since 10.0.0 Generate a richer content for activity types supporting the feature.
    14251445 *
    14261446 * @global object $activities_template {@link BP_Activity_Template}
     
    14311451    global $activities_template;
    14321452
    1433     if ( ! empty( $activities_template->activity->content ) ) {
    1434         return true;
    1435     }
    1436 
    1437     return false;
    1438 }
     1453    $has_content = ! empty( $activities_template->activity->content );
     1454    if ( ! $has_content ) {
     1455        $activity_type = bp_get_activity_type();
     1456
     1457        if ( bp_activity_type_supports( $activity_type, 'generated-content' ) ) {
     1458            $bp                = buddypress();
     1459            $generated_content = new stdClass();
     1460            $activity          = $activities_template->activity;
     1461            $user_id           = $activity->user_id;
     1462
     1463            // Set generated content properties.
     1464            if ( 'new_avatar' === $activity_type ) {
     1465                $avatars = bp_avatar_get_version( $user_id, 'user', bp_get_activity_date_recorded() );
     1466
     1467                if ( $avatars && 1 === count( $avatars ) ) {
     1468                    $avatar            = reset( $avatars );
     1469                    $historical_avatar = trailingslashit( $avatar->parent_dir_url ) . $avatar->name;
     1470
     1471                    // Add historical avatar to the current activity.
     1472                    $generated_content->user_profile_photo = array(
     1473                        'value'             => $historical_avatar,
     1474                        'sanitize_callback' => 'esc_url',
     1475                    );
     1476
     1477                    // Do not use a generated content.
     1478                } else {
     1479                    return false;
     1480                }
     1481            }
     1482
     1483            if ( in_array( $activity_type, array( 'new_member', 'friendship_created', 'updated_profile' ), true ) ) {
     1484                if ( 'friendship_created' === $activity_type ) {
     1485                    $user_id = $activity->secondary_item_id;
     1486                }
     1487
     1488                if ( isset( $bp->avatar->show_avatars ) && $bp->avatar->show_avatars ) {
     1489                    $generated_content->user_profile_photo = array(
     1490                        'value'             => bp_core_fetch_avatar(
     1491                            array(
     1492                                'item_id' => $user_id,
     1493                                'object'  => 'user',
     1494                                'type'    => 'full',
     1495                                'width'   => bp_core_avatar_full_width(),
     1496                                'height'  => bp_core_avatar_full_height(),
     1497                                'html'    => false,
     1498                            )
     1499                        ),
     1500                        'sanitize_callback' => 'esc_url',
     1501                    );
     1502                }
     1503            }
     1504
     1505            // Set common generated content properties.
     1506            if ( in_array( $activity_type, array( 'new_avatar', 'new_member', 'friendship_created', 'updated_profile' ), true ) ) {
     1507                $generated_content->user_url = array(
     1508                    'value'             => bp_core_get_user_domain( $user_id ),
     1509                    'sanitize_callback' => 'esc_url',
     1510                );
     1511
     1512                $generated_content->user_display_name = array(
     1513                    'value'             => bp_core_get_user_displayname( $user_id ),
     1514                    'sanitize_callback' => 'esc_html',
     1515                );
     1516
     1517                $generated_content->user_mention_name = array(
     1518                    'value'             => bp_activity_get_user_mentionname( $user_id ),
     1519                    'sanitize_callback' => 'esc_html',
     1520                );
     1521
     1522                $generated_content->user_mention_url = array(
     1523                    'value'             => wp_nonce_url(
     1524                        add_query_arg(
     1525                            array(
     1526                                'r' => $generated_content->user_mention_name['value'],
     1527                            ),
     1528                            bp_get_activity_directory_permalink()
     1529                        )
     1530                    ),
     1531                    'sanitize_callback' => 'esc_url',
     1532                );
     1533
     1534                if ( bp_displayed_user_use_cover_image_header() ) {
     1535                    $generated_content->user_cover_image = array(
     1536                        'value'             => bp_attachments_get_attachment(
     1537                            'url',
     1538                            array(
     1539                                'object_dir' => 'members',
     1540                                'item_id'    => $user_id,
     1541                            )
     1542                        ),
     1543                        'sanitize_callback' => 'esc_url',
     1544                    );
     1545                }
     1546            }
     1547
     1548            if ( 'created_group' === $activity_type || 'joined_group' === $activity_type ) {
     1549                $group = bp_get_group( $activity->item_id );
     1550
     1551                if ( isset( $bp->avatar->show_avatars ) && $bp->avatar->show_avatars && ! bp_disable_group_avatar_uploads() ) {
     1552                    $generated_content->group_profile_photo = array(
     1553                        'value'             => bp_core_fetch_avatar(
     1554                            array(
     1555                                'item_id' => $group->id,
     1556                                'object'  => 'group',
     1557                                'type'    => 'full',
     1558                                'width'   => bp_core_avatar_full_width(),
     1559                                'height'  => bp_core_avatar_full_height(),
     1560                                'html'    => false,
     1561                            )
     1562                        ),
     1563                        'sanitize_callback' => 'esc_url',
     1564                    );
     1565                }
     1566
     1567                $generated_content->group_url = array(
     1568                    'value'             => bp_get_group_permalink( $group ),
     1569                    'sanitize_callback' => 'esc_url',
     1570                );
     1571
     1572                $generated_content->group_name = array(
     1573                    'value'             => bp_get_group_name( $group ),
     1574                    'sanitize_callback' => 'esc_html',
     1575                );
     1576
     1577                if ( bp_group_use_cover_image_header() ) {
     1578                    $generated_content->group_cover_image = array(
     1579                        'value'             => bp_get_group_cover_url( $group ),
     1580                        'sanitize_callback' => 'esc_url',
     1581                    );
     1582                }
     1583            }
     1584
     1585            // Update the corresponding entry into the activities template global.
     1586            if ( get_object_vars( $generated_content ) ) {
     1587                $activity_id    = $activities_template->activity->id;
     1588                $activity_index = 0;
     1589
     1590                // Find the activity index.
     1591                while ( (int) $activities_template->activities[ $activity_index ]->id !== (int) $activity_id ) {
     1592                    $activity_index++;
     1593                }
     1594
     1595                // Add the generated content object.
     1596                $activities_template->activities[ $activity_index ]->generated_content = $generated_content;
     1597                $has_content = true;
     1598            }
     1599        }
     1600    }
     1601
     1602    return $has_content;
     1603}
     1604
     1605/**
     1606 * Does this property has been generated?
     1607 *
     1608 * @since 10.0.0
     1609 *
     1610 * @param string $property The name of the property to check into the generated content.
     1611 * @return bool            True if the property is not empty. False otherwise.
     1612 */
     1613function bp_activity_has_generated_content_part( $property = '' ) {
     1614    return bp_activity_get_generated_content_part( $property, 'boolean' );
     1615}
     1616
     1617/**
     1618 * Outputs a property of the activity generated content.
     1619 *
     1620 * @since 10.0.0
     1621 *
     1622 * @param string $property The name of the property to check into the generated content.
     1623 */
     1624function bp_activity_generated_content_part( $property = '' ) {
     1625    echo bp_activity_get_generated_content_part( $property );
     1626}
     1627
     1628    /**
     1629     * Returns the property of the activity generated content.
     1630     *
     1631     * @since 10.0.0
     1632     *
     1633     * @param string $property The name of the property to check into the generated content.
     1634     * @param string $return   Whether to return the property value or a boolean to check it exists.
     1635     * @return bool|string     A boolean when requested, false if there is no value, the HTML output otherwise.
     1636     */
     1637    function bp_activity_get_generated_content_part( $property = '', $return = '' ) {
     1638        global $activities_template;
     1639
     1640        if ( ! isset( $activities_template->activity->generated_content->{$property} ) ) {
     1641            return false;
     1642        }
     1643
     1644        $content_part = $activities_template->activity->generated_content->{$property};
     1645
     1646        if ( ! isset( $content_part['value'] ) || ! $content_part['value'] ) {
     1647            return false;
     1648        }
     1649
     1650        if ( 'boolean' === $return ) {
     1651            return true;
     1652        }
     1653
     1654        /**
     1655         * Filter here to edit the generated content part.
     1656         *
     1657         * @since 10.0.0
     1658         *
     1659         * @param string $value    The generated content part.
     1660         * @param string $property The property the content part is attached to.
     1661         */
     1662        $value = apply_filters( 'bp_activity_get_generated_content_part', $content_part['value'], $property );
     1663
     1664        if ( isset( $content_part['sanitize_callback'] ) && $content_part['sanitize_callback'] ) {
     1665            return call_user_func( $content_part['sanitize_callback'], $value );
     1666        }
     1667
     1668        return $value;
     1669    }
    14391670
    14401671/**
Note: See TracChangeset for help on using the changeset viewer.