Automattic\WooCommerce\Blocks\BlockTypes
ProductSummary::generate_summary
Generates the summary text from a string of text. It's not ideal but allows keeping the editor and frontend consistent.
NOTE: If editing, keep it in sync with generateSummary function from plugins/woocommerce/client/blocks/assets/js/base/components/summary/utils.ts!
Once HTML API allow for HTML manipulation both functions (PHP and JS) should be updated to solution fully respecting the word count. https://github.com/woocommerce/woocommerce/issues/52835
Метод класса: ProductSummary{}
Хуков нет.
Возвращает
Строку. Generated summary.
Использование
// private - только в коде основоного (родительского) класса $result = $this->generate_summary( $source, $max_length );
- $source(строка) (обязательный)
- Source text.
- $max_length(int) (обязательный)
- Limit number of items returned if text has multiple paragraphs.
Код ProductSummary::generate_summary() ProductSummary::generate summary WC 10.5.2
private function generate_summary( $source, $max_length ) {
$count_type = wp_get_word_count_type();
$source_with_paragraphs = wpautop( $source );
$source_word_count = $this->count_text( $source_with_paragraphs, $count_type );
if ( $source_word_count <= $max_length ) {
return $source_with_paragraphs;
}
$first_paragraph = $this->get_first_paragraph( $source_with_paragraphs );
$first_paragraph_word_count = $this->count_text( $first_paragraph, $count_type );
if ( $first_paragraph_word_count <= $max_length ) {
return $first_paragraph;
}
if ( 'words' === $count_type ) {
return wpautop( wp_trim_words( $first_paragraph, $max_length ) );
}
return $this->trim_characters( $first_paragraph, $max_length, $count_type ) . '…';
}