| 1 |
<?php |
| 2 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 |
namespace Yoast\WP\SEO\Schema_Aggregator\Application\Enhancement; |
| 4 |
|
| 5 |
/** |
| 6 |
* The abstract schema enhancer. |
| 7 |
*/ |
| 8 |
abstract class Abstract_Schema_Enhancer { |
| 9 |
|
| 10 |
/** |
| 11 |
* Trims content to a maximum length, attempting to break at word boundaries. |
| 12 |
* |
| 13 |
* @param int $max_length Maximum length of content. |
| 14 |
* @param string $content The content to trim. |
| 15 |
* |
| 16 |
* @return string The trimmed content. |
| 17 |
*/ |
| 18 |
protected function trim_content_to_max_length( int $max_length, string $content ): string { |
| 19 |
if ( $max_length > 0 && \strlen( $content ) > $max_length ) { |
| 20 |
$content = \substr( $content, 0, $max_length ); |
| 21 |
$last_space = \strrpos( $content, ' ' ); |
| 22 |
if ( $last_space !== false && $last_space > ( $max_length * 0.9 ) ) { |
| 23 |
$content = \substr( $content, 0, $last_space ); |
| 24 |
} |
| 25 |
$content .= '...'; |
| 26 |
} |
| 27 |
|
| 28 |
return $content; |
| 29 |
} |
| 30 |
} |
| 31 |
|