PluginProbe
ActivityPub / 3.2.5
ActivityPub v3.2.5
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
← All changes | includes/class-shortcodes.php +41 -92 1.0.53.2.5 View file →
@@ -1,22 +1,29 @@
1 1 <?php
2 2 namespace Activitypub;
3 3
4 4 use function Activitypub\esc_hashtag;
5 +use function Activitypub\generate_post_summary;
5 6
6 7 class Shortcodes {
7 8 /**
8 - * Class constructor, registering WordPress then Shortcodes
9 + * Register the shortcodes
9 10 */
10 - public static function init() {
11 - // do not load on admin pages
12 - if ( is_admin() ) {
13 - return;
11 + public static function register() {
12 + foreach ( get_class_methods( self::class ) as $shortcode ) {
13 + if ( 'init' !== $shortcode ) {
14 + add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) );
15 + }
14 16 }
17 + }
15 18
19 + /**
20 + * Unregister the shortcodes
21 + */
22 + public static function unregister() {
16 23 foreach ( get_class_methods( self::class ) as $shortcode ) {
17 24 if ( 'init' !== $shortcode ) {
18 - add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) );
25 + remove_shortcode( 'ap_' . $shortcode );
19 26 }
20 27 }
21 28 }
22 29
@@ -101,80 +108,10 @@
101 108 if ( 0 === $excerpt_length ) {
102 109 $excerpt_length = ACTIVITYPUB_EXCERPT_LENGTH;
103 110 }
104 111
105 - $excerpt = \get_post_field( 'post_excerpt', $item );
112 + $excerpt = generate_post_summary( $item, $excerpt_length );
106 113
107 - if ( '' === $excerpt ) {
108 -
109 - $content = \get_post_field( 'post_content', $item );
110 -
111 - // An empty string will make wp_trim_excerpt do stuff we do not want.
112 - if ( '' !== $content ) {
113 - $excerpt = \strip_shortcodes( $content );
114 -
115 - /** This filter is documented in wp-includes/post-template.php */
116 - $excerpt = \apply_filters( 'the_content', $excerpt );
117 - $excerpt = \str_replace( ']]>', ']]&gt;', $excerpt );
118 - }
119 - }
120 -
121 - // Strip out any remaining tags.
122 - $excerpt = \wp_strip_all_tags( $excerpt );
123 -
124 - /** This filter is documented in wp-includes/formatting.php */
125 - $excerpt_more = \apply_filters( 'excerpt_more', ' [...]' );
126 - $excerpt_more_len = strlen( $excerpt_more );
127 -
128 - // We now have a excerpt, but we need to check it's length, it may be longer than we want for two reasons:
129 - //
130 - // * The user has entered a manual excerpt which is longer that what we want.
131 - // * No manual excerpt exists so we've used the content which might be longer than we want.
132 - //
133 - // Either way, let's trim it up if we need too. Also, don't forget to take into account the more indicator
134 - // as part of the total length.
135 - //
136 -
137 - // Setup a variable to hold the current excerpts length.
138 - $current_excerpt_length = strlen( $excerpt );
139 -
140 - // Setup a variable to keep track of our target length.
141 - $target_excerpt_length = $excerpt_length - $excerpt_more_len;
142 -
143 - // Setup a variable to keep track of the current max length.
144 - $current_excerpt_max = $target_excerpt_length;
145 -
146 - // This is a loop since we can't calculate word break the string after 'the_excpert' filter has run (we would break
147 - // all kinds of html tags), so we have to cut the excerpt down a bit at a time until we hit our target length.
148 - while ( $current_excerpt_length > $target_excerpt_length && $current_excerpt_max > 0 ) {
149 - // Trim the excerpt based on wordwrap() positioning.
150 - // Note: we're using <br> as the linebreak just in case there are any newlines existing in the excerpt from the user.
151 - // There won't be any <br> left after we've run wp_strip_all_tags() in the code above, so they're
152 - // safe to use here. It won't be included in the final excerpt as the substr() will trim it off.
153 - $excerpt = substr( $excerpt, 0, strpos( wordwrap( $excerpt, $current_excerpt_max, '<br>' ), '<br>' ) );
154 -
155 - // If something went wrong, or we're in a language that wordwrap() doesn't understand,
156 - // just chop it off and don't worry about breaking in the middle of a word.
157 - if ( strlen( $excerpt ) > $excerpt_length - $excerpt_more_len ) {
158 - $excerpt = substr( $excerpt, 0, $current_excerpt_max );
159 - }
160 -
161 - // Add in the more indicator.
162 - $excerpt = $excerpt . $excerpt_more;
163 -
164 - // Run it through the excerpt filter which will add some html tags back in.
165 - $excerpt_filtered = apply_filters( 'the_excerpt', $excerpt );
166 -
167 - // Now set the current excerpt length to this new filtered length.
168 - $current_excerpt_length = strlen( $excerpt_filtered );
169 -
170 - // Check to see if we're over the target length.
171 - if ( $current_excerpt_length > $target_excerpt_length ) {
172 - // If so, remove 20 characters from the current max and run the loop again.
173 - $current_excerpt_max = $current_excerpt_max - 20;
174 - }
175 - }
176 -
177 114 return \apply_filters( 'the_excerpt', $excerpt );
178 115 }
179 116
180 117 /**
@@ -201,23 +138,33 @@
201 138 $atts,
202 139 $tag
203 140 );
204 141
205 - $content = \get_post_field( 'post_content', $item );
142 + $content = '';
206 143
207 - if ( 'yes' === $atts['apply_filters'] ) {
208 - $content = \apply_filters( 'the_content', $content );
144 + if ( 'attachment' === $item->post_type ) {
145 + // get title of attachment with fallback to alt text.
146 + $content = wp_get_attachment_caption( $item->ID );
147 + if ( empty( $content ) ) {
148 + $content = get_post_meta( $item->ID, '_wp_attachment_image_alt', true );
149 + }
209 150 } else {
210 - $content = do_blocks( $content );
211 - $content = wptexturize( $content );
212 - $content = wp_filter_content_tags( $content );
151 + $content = \get_post_field( 'post_content', $item );
152 +
153 + if ( 'yes' === $atts['apply_filters'] ) {
154 + $content = \apply_filters( 'the_content', $content );
155 + } else {
156 + $content = do_blocks( $content );
157 + $content = wptexturize( $content );
158 + $content = wp_filter_content_tags( $content );
159 + }
160 +
161 + // replace script and style elements
162 + $content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
163 + $content = strip_shortcodes( $content );
164 + $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
213 165 }
214 166
215 - // replace script and style elements
216 - $content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
217 - $content = strip_shortcodes( $content );
218 - $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
219 -
220 167 add_shortcode( 'ap_content', array( 'Activitypub\Shortcodes', 'content' ) );
221 168
222 169 return $content;
223 170 }
@@ -250,9 +197,9 @@
250 197 return \esc_url( \get_permalink( $item->ID ) );
251 198 }
252 199
253 200 return \sprintf(
254 - '<a href="%1$s">%1$s</a>',
201 + '<a href="%1$s" class="status-link unhandled-link">%1$s</a>',
255 202 \esc_url( \get_permalink( $item->ID ) )
256 203 );
257 204 }
258 205
@@ -284,9 +231,9 @@
284 231 return \esc_url( \wp_get_shortlink( $item->ID ) );
285 232 }
286 233
287 234 return \sprintf(
288 - '<a href="%1$s">%1$s</a>',
235 + '<a href="%1$s" class="status-link unhandled-link">%1$s</a>',
289 236 \esc_url( \wp_get_shortlink( $item->ID ) )
290 237 );
291 238 }
292 239
@@ -383,9 +330,10 @@
383 330 if ( ! $item ) {
384 331 return '';
385 332 }
386 333
387 - $name = \get_the_author_meta( 'display_name', $item->post_author );
334 + $author_id = \get_post_field( 'post_author', $item->ID );
335 + $name = \get_the_author_meta( 'display_name', $author_id );
388 336
389 337 if ( ! $name ) {
390 338 return '';
391 339 }
@@ -408,9 +356,10 @@
408 356 if ( ! $item ) {
409 357 return '';
410 358 }
411 359
412 - $url = \get_the_author_meta( 'user_url', $item->post_author );
360 + $author_id = \get_post_field( 'post_author', $item->ID );
361 + $url = \get_the_author_meta( 'user_url', $author_id );
413 362
414 363 if ( ! $url ) {
415 364 return '';
416 365 }