| @@ -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,84 +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 ( 'attachment' === $item->post_type ) { | |
| 114 | - // get title of attachment with fallback to alt text. | |
| 115 | - $content = wp_get_attachment_caption( $item->ID ); | |
| 116 | - if ( empty( $content ) ) { | |
| 117 | - $content = get_post_meta( $item->ID, '_wp_attachment_image_alt', true ); | |
| 118 | - } | |
| 119 | - } elseif ( '' === $excerpt ) { | |
| 120 | - $content = \get_post_field( 'post_content', $item ); | |
| 121 | - | |
| 122 | - // An empty string will make wp_trim_excerpt do stuff we do not want. | |
| 123 | - if ( '' !== $content ) { | |
| 124 | - $excerpt = \strip_shortcodes( $content ); | |
| 125 | - | |
| 126 | - /** This filter is documented in wp-includes/post-template.php */ | |
| 127 | - $excerpt = \apply_filters( 'the_content', $excerpt ); | |
| 128 | - $excerpt = \str_replace( ']]>', ']]>', $excerpt ); | |
| 129 | - } | |
| 130 | - } | |
| 131 | - | |
| 132 | - // Strip out any remaining tags. | |
| 133 | - $excerpt = \wp_strip_all_tags( $excerpt ); | |
| 134 | - | |
| 135 | - $excerpt_more = \apply_filters( 'activitypub_excerpt_more', ' […]' ); | |
| 136 | - $excerpt_more_len = strlen( $excerpt_more ); | |
| 137 | - | |
| 138 | - // We now have a excerpt, but we need to check it's length, it may be longer than we want for two reasons: | |
| 139 | - // | |
| 140 | - // * The user has entered a manual excerpt which is longer that what we want. | |
| 141 | - // * No manual excerpt exists so we've used the content which might be longer than we want. | |
| 142 | - // | |
| 143 | - // Either way, let's trim it up if we need too. Also, don't forget to take into account the more indicator | |
| 144 | - // as part of the total length. | |
| 145 | - // | |
| 146 | - | |
| 147 | - // Setup a variable to hold the current excerpts length. | |
| 148 | - $current_excerpt_length = strlen( $excerpt ); | |
| 149 | - | |
| 150 | - // Setup a variable to keep track of our target length. | |
| 151 | - $target_excerpt_length = $excerpt_length - $excerpt_more_len; | |
| 152 | - | |
| 153 | - // Setup a variable to keep track of the current max length. | |
| 154 | - $current_excerpt_max = $target_excerpt_length; | |
| 155 | - | |
| 156 | - // This is a loop since we can't calculate word break the string after 'the_excpert' filter has run (we would break | |
| 157 | - // all kinds of html tags), so we have to cut the excerpt down a bit at a time until we hit our target length. | |
| 158 | - while ( $current_excerpt_length > $target_excerpt_length && $current_excerpt_max > 0 ) { | |
| 159 | - // Trim the excerpt based on wordwrap() positioning. | |
| 160 | - // Note: we're using <br> as the linebreak just in case there are any newlines existing in the excerpt from the user. | |
| 161 | - // There won't be any <br> left after we've run wp_strip_all_tags() in the code above, so they're | |
| 162 | - // safe to use here. It won't be included in the final excerpt as the substr() will trim it off. | |
| 163 | - $excerpt = substr( $excerpt, 0, strpos( wordwrap( $excerpt, $current_excerpt_max, '<br>' ), '<br>' ) ); | |
| 164 | - | |
| 165 | - // If something went wrong, or we're in a language that wordwrap() doesn't understand, | |
| 166 | - // just chop it off and don't worry about breaking in the middle of a word. | |
| 167 | - if ( strlen( $excerpt ) > $excerpt_length - $excerpt_more_len ) { | |
| 168 | - $excerpt = substr( $excerpt, 0, $current_excerpt_max ); | |
| 169 | - } | |
| 170 | - | |
| 171 | - // Add in the more indicator. | |
| 172 | - $excerpt = $excerpt . $excerpt_more; | |
| 173 | - | |
| 174 | - // Run it through the excerpt filter which will add some html tags back in. | |
| 175 | - $excerpt_filtered = apply_filters( 'the_excerpt', $excerpt ); | |
| 176 | - | |
| 177 | - // Now set the current excerpt length to this new filtered length. | |
| 178 | - $current_excerpt_length = strlen( $excerpt_filtered ); | |
| 179 | - | |
| 180 | - // Check to see if we're over the target length. | |
| 181 | - if ( $current_excerpt_length > $target_excerpt_length ) { | |
| 182 | - // If so, remove 20 characters from the current max and run the loop again. | |
| 183 | - $current_excerpt_max = $current_excerpt_max - 20; | |
| 184 | - } | |
| 185 | - } | |
| 186 | - | |
| 187 | 114 | return \apply_filters( 'the_excerpt', $excerpt ); |
| 188 | 115 | } |
| 189 | 116 | |
| 190 | 117 | /** |
| @@ -270,9 +197,9 @@ | ||
| 270 | 197 | return \esc_url( \get_permalink( $item->ID ) ); |
| 271 | 198 | } |
| 272 | 199 | |
| 273 | 200 | return \sprintf( |
| 274 | - '<a href="%1$s">%1$s</a>', | |
| 201 | + '<a href="%1$s" class="status-link unhandled-link">%1$s</a>', | |
| 275 | 202 | \esc_url( \get_permalink( $item->ID ) ) |
| 276 | 203 | ); |
| 277 | 204 | } |
| 278 | 205 | |
| @@ -304,9 +231,9 @@ | ||
| 304 | 231 | return \esc_url( \wp_get_shortlink( $item->ID ) ); |
| 305 | 232 | } |
| 306 | 233 | |
| 307 | 234 | return \sprintf( |
| 308 | - '<a href="%1$s">%1$s</a>', | |
| 235 | + '<a href="%1$s" class="status-link unhandled-link">%1$s</a>', | |
| 309 | 236 | \esc_url( \wp_get_shortlink( $item->ID ) ) |
| 310 | 237 | ); |
| 311 | 238 | } |
| 312 | 239 | |