@@ -21,6 +21,16 @@ function __construct() {
21
21
// manage user's last activity update.
22
22
add_action ( 'bp_activity_posted_update ' , array ( &$ this , 'manage_user_last_activity_update ' ), 999 , 3 );
23
23
add_action ( 'bp_groups_posted_update ' , array ( &$ this , 'bp_groups_posted_update ' ), 99 , 4 );
24
+
25
+ /**
26
+ * Filter to disable bp_activity_truncate_entry override function.
27
+ *
28
+ * @param boolean By default its enabled.
29
+ */
30
+ if ( apply_filters ( 'rtmedia_disable_truncate_entry_override ' , true ) ) {
31
+ // Code to show media with read more option.
32
+ add_filter ( 'bp_activity_truncate_entry ' , array ( $ this , 'bp_activity_truncate_entry ' ), 10 , 3 );
33
+ }
24
34
}
25
35
add_action ( 'bp_init ' , array ( $ this , 'non_threaded_comments ' ) );
26
36
add_action ( 'bp_activity_comment_posted ' , array ( $ this , 'comment_sync ' ), 10 , 2 );
@@ -64,6 +74,166 @@ function __construct() {
64
74
}
65
75
}
66
76
77
+ /**
78
+ * Show media even if the text is long with read more option.
79
+ *
80
+ * @param string $excerpt Excerpt of the activity text.
81
+ * @param string $text Actual text of activity.
82
+ * @param string $readmore Read more text.
83
+ *
84
+ * @return string Custom excerpt if conditions are match.
85
+ */
86
+ public function bp_activity_truncate_entry ( $ excerpt , $ text , $ readmore ) {
87
+ // Return if class doesn't exist.
88
+ if ( ! class_exists ( 'DOMDocument ' ) ) {
89
+ return $ excerpt ;
90
+ }
91
+
92
+ global $ activities_template ;
93
+
94
+ $ excerpt_length = bp_activity_get_excerpt_length ();
95
+ // Run the text through the excerpt function. If it's too short, the original text will be returned.
96
+ $ temp_excerpt = bp_create_excerpt ( $ text , $ excerpt_length , array () );
97
+ if ( strlen ( $ temp_excerpt ) >= strlen ( strip_shortcodes ( $ text ) ) ) {
98
+ return $ excerpt ;
99
+ }
100
+
101
+ // Get current activity id.
102
+ $ activity_id = bp_get_activity_id ();
103
+
104
+ // We need to separate text and rtMedia images, for this we need DOM manipulation.
105
+ $ dom = new DOMDocument ();
106
+ // DOMDocument gives error on html5 tags, so we need to disable errors.
107
+ libxml_use_internal_errors ( true );
108
+ $ dom ->loadHTML ( $ text );
109
+ // DOMDocument gives error on html5 tags, so we need to disable errors.
110
+ libxml_clear_errors ();
111
+ // We need to find div having rtmedia-activity-text class, but no direct method for it.
112
+ // So we need to iterate.
113
+ $ div_list = $ dom ->getElementsByTagName ( 'div ' );
114
+
115
+ // Return if no divs found.
116
+ if ( empty ( $ div_list ) ) {
117
+ return $ excerpt ;
118
+ }
119
+
120
+ // We're storing first div to create final markup.
121
+ // If we create markup from dom object, it'll create whole HTML which we don't want.
122
+ $ first_div = '' ;
123
+
124
+ foreach ( $ div_list as $ div ) {
125
+ // Set first div.
126
+ if ( empty ( $ first_div ) ) {
127
+ $ first_div = $ div ;
128
+ }
129
+
130
+ // We need div with class attribute.
131
+ if ( empty ( $ div ->attributes ) ) {
132
+ continue ;
133
+ }
134
+
135
+ $ atts = $ div ->attributes ;
136
+ // Check attributes by iterating them.
137
+ foreach ( $ atts as $ att ) {
138
+ if ( empty ( $ att ->name ) || empty ( $ att ->value ) ) {
139
+ continue ;
140
+ }
141
+
142
+ // Condition to find text div.
143
+ if ( 'class ' === $ att ->name && strpos ( $ att ->value , 'rtmedia-activity-text ' ) !== false ) {
144
+ // Create excerpt only on text and then set it to div text.
145
+ // We're using actual length / 2 to make space for image.
146
+ $ custom_excerpt = bp_create_excerpt ( $ div ->textContent , (int ) $ excerpt_length / 2 , array ( 'ending ' => '... ' ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Can't change property name.
147
+ $ div ->textContent = trim ( $ custom_excerpt ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Can't change property name.
148
+
149
+ // Show 4 images if text is less, else show 2 images.
150
+ $ images_to_show = 4 ;
151
+ if ( strlen ( $ div ->textContent ) > 20 ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Can't change property name.
152
+ $ images_to_show = 2 ;
153
+ }
154
+
155
+ // Set number of images to show in excerpt.
156
+ $ dom = $ this ->get_bp_activity_media_html ( $ dom , $ images_to_show );
157
+
158
+ // Code copied from buddypress.
159
+ $ id = ( ! empty ( $ activities_template ->activity ->current_comment ->id ) ? 'acomment-read-more- ' . $ activities_template ->activity ->current_comment ->id : 'activity-read-more- ' . $ activity_id );
160
+
161
+ // Get final HTML.
162
+ $ content = $ first_div ->ownerDocument ->saveHTML ( $ first_div ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Can't change property name.
163
+
164
+ // Append read more link and text.
165
+ $ return = sprintf ( '%1$s<span class="activity-read-more" id="%2$s"><a href="%3$s" rel="nofollow">%4$s</a></span> ' , $ content , $ id , bp_get_activity_thread_permalink (), $ readmore );
166
+
167
+ return $ return ;
168
+ }
169
+ }
170
+ }
171
+
172
+ return $ excerpt ;
173
+ }
174
+
175
+ /**
176
+ * Set number of images to show in activity excerpt.
177
+ *
178
+ * @param object $dom DOMDocument object for DOM manipulation.
179
+ * @param int $images_to_show Number of images to show.
180
+ *
181
+ * @return object Modified DOMDocument object.
182
+ */
183
+ private function get_bp_activity_media_html ( $ dom , $ images_to_show ) {
184
+ // Get media list which is inside <ul>.
185
+ $ ul_list = $ dom ->getElementsByTagName ( 'ul ' );
186
+
187
+ // Return if no ul element.
188
+ if ( empty ( $ ul_list ) ) {
189
+ return $ dom ;
190
+ }
191
+
192
+ // Iterate to find out media-list ul.
193
+ foreach ( $ ul_list as $ ul ) {
194
+ // We need ul having class 'rtm-activity-media-list'.
195
+ if ( empty ( $ ul ->attributes ) ) {
196
+ continue ;
197
+ }
198
+
199
+ // Iterate attributes.
200
+ foreach ( $ ul ->attributes as $ att ) {
201
+ if ( empty ( $ att ->name ) || empty ( $ att ->value ) ) {
202
+ continue ;
203
+ }
204
+
205
+ // Conditions to match required class.
206
+ if ( 'class ' === $ att ->name && strpos ( $ att ->value , 'rtm-activity-media-list ' ) !== false && count ( $ ul ->childNodes ) > 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Can't change property name.
207
+
208
+ // Number of li (images) allowed to show.
209
+ $ count = 1 ;
210
+ // Array where items to remove will be stored.
211
+ $ items_to_remove = array ();
212
+ // Iterate all children of ul which are images (li).
213
+ foreach ( $ ul ->childNodes as $ li ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Can't change property name.
214
+
215
+ // If max number of images reached, add li to items_to_remove array.
216
+ if ( $ count > $ images_to_show ) {
217
+ $ items_to_remove [] = $ li ;
218
+ }
219
+
220
+ $ count ++;
221
+ }
222
+
223
+ // Remove images.
224
+ foreach ( $ items_to_remove as $ item ) {
225
+ $ ul ->removeChild ( $ item );
226
+ }
227
+
228
+ return $ dom ;
229
+ }
230
+ }
231
+ }
232
+
233
+ return $ dom ;
234
+ }
235
+
236
+
67
237
/**
68
238
* For adding secondary avatar in the activity header.
69
239
*
0 commit comments