Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/22/2022 09:45:12 AM (4 years ago)
Author:
imath
Message:

Stop using the post link into the new_blog_post activity action

Now the post link is included into the activity content (see [13305]), displaying it into the activity action is no more needed.
This commit also :

  • introduces a new function to get a site's URL and Name out of BP Blogs meta which avoids some code duplication in new_blog, new_blog_post and new_blog_comment activity action format callbacks.
  • improves the new_blog_post activity content layout when the corresponding post's image is not the featured one.
  • properly deprecates pre-2.0 activity action filters.

Props teeboy4real

Closes https://github.com/buddypress/buddypress/pull/27
Fixes #8052

File:
1 edited

Legend:

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

    r13108 r13342  
    116116
    117117/**
     118 * Returns the blog URL and name which relates to a post or comment activity.
     119 *
     120 * @since 11.0.0
     121 *
     122 * @param BP_Activity_Activity $activity The activity object.
     123 * @return array The blog URL and name which relates to a post or comment activity.
     124 */
     125function bp_blogs_activity_get_site_link_meta( $activity = null ) {
     126        $attributes = array(
     127                'blog_url'  => '',
     128                'blog_name' => '',
     129        );
     130
     131        if ( ! isset( $activity->item_id, $activity->component ) || ! $activity->item_id || 'blogs' !== $activity->component ) {
     132                return $attributes;
     133        }
     134
     135        if ( isset( $activity->blog_url ) ) {
     136                $attributes['blog_url'] = $activity->blog_url;
     137        } else {
     138                $blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' );
     139
     140                if ( ! $blog_url ) {
     141                        $blog_url = get_home_url( $activity->item_id );
     142                        bp_blogs_update_blogmeta( $activity->item_id, 'url', $blog_url );
     143                } else {
     144                        $attributes['blog_url'] = $blog_url;
     145                }
     146        }
     147
     148        if ( isset( $activity->blog_name ) ) {
     149                $attributes['blog_name'] = $activity->blog_name;
     150        } else {
     151                $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' );
     152
     153                if ( ! $blog_name ) {
     154                        $blog_name = get_blog_option( $activity->item_id, 'blogname' );
     155                        bp_blogs_update_blogmeta( $activity->item_id, 'name', $blog_name );
     156                } else {
     157                        $attributes['blog_name'] = $blog_name;
     158                }
     159        }
     160
     161        return $attributes;
     162}
     163
     164/**
    118165 * Format 'new_blog' activity actions.
    119166 *
     
    122169 * @param string $action   Static activity action.
    123170 * @param object $activity Activity data object.
    124  * @return string
     171 * @return string Constructed activity action.
    125172 */
    126173function bp_blogs_format_activity_action_new_blog( $action, $activity ) {
    127         $blog_url  = bp_blogs_get_blogmeta( $activity->item_id, 'url' );
    128         $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' );
     174        list( $blog_url, $blog_name ) = array_values( bp_blogs_activity_get_site_link_meta( $activity ) );
    129175
    130176        $action = sprintf(
     
    143189
    144190                if ( isset( $recorded_blog ) ) {
    145                         $action = apply_filters( 'bp_blogs_activity_created_blog_action', $action, $recorded_blog, $blog_name, bp_blogs_get_blogmeta( $activity->item_id, 'description' ) );
     191                        $blog_description = bp_blogs_get_blogmeta( $activity->item_id, 'description' );
     192                        $action           = apply_filters_deprecated( 'bp_blogs_activity_created_blog_action', array( $action, $recorded_blog, $blog_name, $blog_description ), '2.0.0', 'bp_blogs_format_activity_action_new_blog' );
    146193                }
    147194        }
     
    168215 */
    169216function bp_blogs_format_activity_action_new_blog_post( $action, $activity ) {
    170         $blog_url  = bp_blogs_get_blogmeta( $activity->item_id, 'url' );
    171         $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' );
    172 
    173         if ( empty( $blog_url ) || empty( $blog_name ) ) {
    174                 $blog_url  = get_home_url( $activity->item_id );
    175                 $blog_name = get_blog_option( $activity->item_id, 'blogname' );
    176 
    177                 bp_blogs_update_blogmeta( $activity->item_id, 'url', $blog_url );
    178                 bp_blogs_update_blogmeta( $activity->item_id, 'name', $blog_name );
    179         }
    180 
    181         /**
    182          * When the post is published we are faking an activity object
    183          * to which we add 2 properties :
    184          * - the post url
    185          * - the post title
    186          * This is done to build the 'post link' part of the activity
    187          * action string.
    188          * NB: in this case the activity has not yet been created.
    189          */
    190         if ( isset( $activity->post_url ) ) {
    191                 $post_url = $activity->post_url;
    192 
    193         /**
    194          * The post_url property is not set, we need to build the url
    195          * thanks to the post id which is also saved as the secondary
    196          * item id property of the activity object.
    197          */
    198         } else {
    199                 $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) );
    200         }
    201 
    202         // Should be the case when the post has just been published.
    203         if ( isset( $activity->post_title ) ) {
    204                 $post_title = $activity->post_title;
    205 
    206         // If activity already exists try to get the post title from activity meta.
    207         } else if ( ! empty( $activity->id ) ) {
    208                 $post_title = bp_activity_get_meta( $activity->id, 'post_title' );
    209         }
    210 
    211         /**
    212          * In case the post was published without a title
    213          * or the activity meta was not found.
    214          */
    215         if ( empty( $post_title ) ) {
    216                 // Defaults to no title.
    217                 $post_title = __( '(no title)', 'buddypress' );
    218 
    219                 switch_to_blog( $activity->item_id );
    220 
    221                 $post = get_post( $activity->secondary_item_id );
    222                 if ( is_a( $post, 'WP_Post' ) ) {
    223                         // Does the post have a title ?
    224                         if ( ! empty( $post->post_title ) ) {
    225                                 $post_title = $post->post_title;
    226                         }
    227 
    228                         // Make sure the activity exists before saving the post title in activity meta.
    229                         if ( ! empty( $activity->id ) ) {
    230                                 bp_activity_update_meta( $activity->id, 'post_title', $post_title );
    231                         }
    232                 }
    233 
    234                 restore_current_blog();
    235         }
    236 
    237         // Build the 'post link' part of the activity action string.
    238         $post_link = '<a href="' . esc_url( $post_url ) . '">' . esc_html( $post_title ) . '</a>';
    239 
    240217        $user_link = bp_core_get_userlink( $activity->user_id );
    241218
    242219        // Build the complete activity action string.
    243220        if ( is_multisite() ) {
     221                list( $blog_url, $blog_name ) = array_values( bp_blogs_activity_get_site_link_meta( $activity ) );
     222
    244223                $action = sprintf(
    245                         /* translators: 1: the activity user link. 2: the post link. 3: the blog link. */
    246                         esc_html_x( '%1$s wrote a new post, %2$s, on the site %3$s', '`new_blog_post` activity action', 'buddypress' ),
     224                        /* translators: 1: the activity user link. 2: the blog link. */
     225                        esc_html_x( '%1$s wrote a new post on the site %2$s', 'Multisite `new_blog_post` activity action', 'buddypress' ),
    247226                        $user_link,
    248                         $post_link,
    249227                        '<a href="' . esc_url( $blog_url ) . '">' . esc_html( $blog_name ) . '</a>'
    250228                );
    251229        } else {
    252230                $action = sprintf(
    253                         /* translators: 1: the activity user link. 2: the post link. */
    254                         esc_html_x( '%1$s wrote a new post, %2$s', '`new_blog_post` activity action', 'buddypress' ),
    255                         $user_link,
    256                         $post_link
     231                        /* translators: 1: the activity user link. */
     232                        esc_html_x( '%s wrote a new post', '`new_blog_post` activity action', 'buddypress' ),
     233                        $user_link
    257234                );
    258235        }
     
    265242
    266243                if ( ! empty( $post ) && ! is_wp_error( $post ) ) {
    267                         $action = apply_filters( 'bp_blogs_activity_new_post_action', $action, $post, $post_url );
     244                        $post_url = add_query_arg( 'p', $post->ID, trailingslashit( get_home_url( $activity->item_id ) ) );
     245                        $action   = apply_filters_deprecated( 'bp_blogs_activity_new_post_action', array( $action, $post, $post_url ), '2.0.0', 'bp_blogs_format_activity_action_new_blog_post' );
    268246                }
    269247        }
     
    301279         * NB: in this case the activity has not yet been created.
    302280         */
    303 
    304         $blog_url = false;
    305 
    306         // Try to get the blog url from the activity object.
    307         if ( isset( $activity->blog_url ) ) {
    308                 $blog_url = $activity->blog_url;
    309         } else {
    310                 $blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' );
    311         }
    312 
    313         $blog_name = false;
    314 
    315         // Try to get the blog name from the activity object.
    316         if ( isset( $activity->blog_name ) ) {
    317                 $blog_name = $activity->blog_name;
    318         } else {
    319                 $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' );
    320         }
    321 
    322         if ( empty( $blog_url ) || empty( $blog_name ) ) {
    323                 $blog_url  = get_home_url( $activity->item_id );
    324                 $blog_name = get_blog_option( $activity->item_id, 'blogname' );
    325 
    326                 bp_blogs_update_blogmeta( $activity->item_id, 'url', $blog_url );
    327                 bp_blogs_update_blogmeta( $activity->item_id, 'name', $blog_name );
    328         }
    329 
    330         $post_url = false;
     281        list( $blog_url, $blog_name ) = array_values( bp_blogs_activity_get_site_link_meta( $activity ) );
     282        $post_url                     = false;
    331283
    332284        // Try to get the post url from the activity object.
     
    402354
    403355                if ( ! empty( $comment ) && ! is_wp_error( $comment ) ) {
    404                         $action = apply_filters( 'bp_blogs_activity_new_comment_action', $action, $comment, $post_url . '#' . $activity->secondary_item_id );
     356                        $action = apply_filters_deprecated( 'bp_blogs_activity_new_comment_action', array( $action, $comment, $post_url . '#' . $activity->secondary_item_id ), '2.0.0', 'bp_blogs_format_activity_action_new_blog_comment' );
    405357                }
    406358        }
Note: See TracChangeset for help on using the changeset viewer.