| @@ -1,8 +1,9 @@ | ||
| 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 | 9 | * Register the shortcodes |
| @@ -107,80 +108,10 @@ | ||
| 107 | 108 | if ( 0 === $excerpt_length ) { |
| 108 | 109 | $excerpt_length = ACTIVITYPUB_EXCERPT_LENGTH; |
| 109 | 110 | } |
| 110 | 111 | |
| 111 | - $excerpt = \get_post_field( 'post_excerpt', $item ); | |
| 112 | + $excerpt = generate_post_summary( $item, $excerpt_length ); | |
| 112 | 113 | |
| 113 | - if ( '' === $excerpt ) { | |
| 114 | - | |
| 115 | - $content = \get_post_field( 'post_content', $item ); | |
| 116 | - | |
| 117 | - // An empty string will make wp_trim_excerpt do stuff we do not want. | |
| 118 | - if ( '' !== $content ) { | |
| 119 | - $excerpt = \strip_shortcodes( $content ); | |
| 120 | - | |
| 121 | - /** This filter is documented in wp-includes/post-template.php */ | |
| 122 | - $excerpt = \apply_filters( 'the_content', $excerpt ); | |
| 123 | - $excerpt = \str_replace( ']]>', ']]>', $excerpt ); | |
| 124 | - } | |
| 125 | - } | |
| 126 | - | |
| 127 | - // Strip out any remaining tags. | |
| 128 | - $excerpt = \wp_strip_all_tags( $excerpt ); | |
| 129 | - | |
| 130 | - /** This filter is documented in wp-includes/formatting.php */ | |
| 131 | - $excerpt_more = \apply_filters( 'excerpt_more', ' [...]' ); | |
| 132 | - $excerpt_more_len = strlen( $excerpt_more ); | |
| 133 | - | |
| 134 | - // We now have a excerpt, but we need to check it's length, it may be longer than we want for two reasons: | |
| 135 | - // | |
| 136 | - // * The user has entered a manual excerpt which is longer that what we want. | |
| 137 | - // * No manual excerpt exists so we've used the content which might be longer than we want. | |
| 138 | - // | |
| 139 | - // Either way, let's trim it up if we need too. Also, don't forget to take into account the more indicator | |
| 140 | - // as part of the total length. | |
| 141 | - // | |
| 142 | - | |
| 143 | - // Setup a variable to hold the current excerpts length. | |
| 144 | - $current_excerpt_length = strlen( $excerpt ); | |
| 145 | - | |
| 146 | - // Setup a variable to keep track of our target length. | |
| 147 | - $target_excerpt_length = $excerpt_length - $excerpt_more_len; | |
| 148 | - | |
| 149 | - // Setup a variable to keep track of the current max length. | |
| 150 | - $current_excerpt_max = $target_excerpt_length; | |
| 151 | - | |
| 152 | - // This is a loop since we can't calculate word break the string after 'the_excpert' filter has run (we would break | |
| 153 | - // all kinds of html tags), so we have to cut the excerpt down a bit at a time until we hit our target length. | |
| 154 | - while ( $current_excerpt_length > $target_excerpt_length && $current_excerpt_max > 0 ) { | |
| 155 | - // Trim the excerpt based on wordwrap() positioning. | |
| 156 | - // Note: we're using <br> as the linebreak just in case there are any newlines existing in the excerpt from the user. | |
| 157 | - // There won't be any <br> left after we've run wp_strip_all_tags() in the code above, so they're | |
| 158 | - // safe to use here. It won't be included in the final excerpt as the substr() will trim it off. | |
| 159 | - $excerpt = substr( $excerpt, 0, strpos( wordwrap( $excerpt, $current_excerpt_max, '<br>' ), '<br>' ) ); | |
| 160 | - | |
| 161 | - // If something went wrong, or we're in a language that wordwrap() doesn't understand, | |
| 162 | - // just chop it off and don't worry about breaking in the middle of a word. | |
| 163 | - if ( strlen( $excerpt ) > $excerpt_length - $excerpt_more_len ) { | |
| 164 | - $excerpt = substr( $excerpt, 0, $current_excerpt_max ); | |
| 165 | - } | |
| 166 | - | |
| 167 | - // Add in the more indicator. | |
| 168 | - $excerpt = $excerpt . $excerpt_more; | |
| 169 | - | |
| 170 | - // Run it through the excerpt filter which will add some html tags back in. | |
| 171 | - $excerpt_filtered = apply_filters( 'the_excerpt', $excerpt ); | |
| 172 | - | |
| 173 | - // Now set the current excerpt length to this new filtered length. | |
| 174 | - $current_excerpt_length = strlen( $excerpt_filtered ); | |
| 175 | - | |
| 176 | - // Check to see if we're over the target length. | |
| 177 | - if ( $current_excerpt_length > $target_excerpt_length ) { | |
| 178 | - // If so, remove 20 characters from the current max and run the loop again. | |
| 179 | - $current_excerpt_max = $current_excerpt_max - 20; | |
| 180 | - } | |
| 181 | - } | |
| 182 | - | |
| 183 | 114 | return \apply_filters( 'the_excerpt', $excerpt ); |
| 184 | 115 | } |
| 185 | 116 | |
| 186 | 117 | /** |
| @@ -207,23 +138,33 @@ | ||
| 207 | 138 | $atts, |
| 208 | 139 | $tag |
| 209 | 140 | ); |
| 210 | 141 | |
| 211 | - $content = \get_post_field( 'post_content', $item ); | |
| 142 | + $content = ''; | |
| 212 | 143 | |
| 213 | - if ( 'yes' === $atts['apply_filters'] ) { | |
| 214 | - $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 | + } | |
| 215 | 150 | } else { |
| 216 | - $content = do_blocks( $content ); | |
| 217 | - $content = wptexturize( $content ); | |
| 218 | - $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 ) ); | |
| 219 | 165 | } |
| 220 | 166 | |
| 221 | - // replace script and style elements | |
| 222 | - $content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content ); | |
| 223 | - $content = strip_shortcodes( $content ); | |
| 224 | - $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) ); | |
| 225 | - | |
| 226 | 167 | add_shortcode( 'ap_content', array( 'Activitypub\Shortcodes', 'content' ) ); |
| 227 | 168 | |
| 228 | 169 | return $content; |
| 229 | 170 | } |
| @@ -256,9 +197,9 @@ | ||
| 256 | 197 | return \esc_url( \get_permalink( $item->ID ) ); |
| 257 | 198 | } |
| 258 | 199 | |
| 259 | 200 | return \sprintf( |
| 260 | - '<a href="%1$s">%1$s</a>', | |
| 201 | + '<a href="%1$s" class="status-link unhandled-link">%1$s</a>', | |
| 261 | 202 | \esc_url( \get_permalink( $item->ID ) ) |
| 262 | 203 | ); |
| 263 | 204 | } |
| 264 | 205 | |
| @@ -290,9 +231,9 @@ | ||
| 290 | 231 | return \esc_url( \wp_get_shortlink( $item->ID ) ); |
| 291 | 232 | } |
| 292 | 233 | |
| 293 | 234 | return \sprintf( |
| 294 | - '<a href="%1$s">%1$s</a>', | |
| 235 | + '<a href="%1$s" class="status-link unhandled-link">%1$s</a>', | |
| 295 | 236 | \esc_url( \wp_get_shortlink( $item->ID ) ) |
| 296 | 237 | ); |
| 297 | 238 | } |
| 298 | 239 | |