|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Publish to Apple News: \Apple_Exporter\Components\Footnotes class |
| 4 | + * |
| 5 | + * @package Apple_News |
| 6 | + * @subpackage Apple_Exporter\Components |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Apple_Exporter\Components; |
| 10 | + |
| 11 | +/** |
| 12 | + * A translation of the WordPress Footnotes block. |
| 13 | + * |
| 14 | + * @since 0.2.5 |
| 15 | + */ |
| 16 | +class Footnotes extends Component { |
| 17 | + |
| 18 | + /** |
| 19 | + * Look for node matches for this component. |
| 20 | + * |
| 21 | + * @param \DOMElement $node The node to examine for matches. |
| 22 | + * @access public |
| 23 | + * @return \DOMElement|null The node on success, or null on no match. |
| 24 | + */ |
| 25 | + public static function node_matches( $node ) { |
| 26 | + if ( |
| 27 | + 'ol' === $node->nodeName && |
| 28 | + self::node_has_class( $node, 'wp-block-footnotes' ) |
| 29 | + ) { |
| 30 | + return $node; |
| 31 | + } |
| 32 | + |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Register all specs for the component. |
| 38 | + * |
| 39 | + * @access public |
| 40 | + */ |
| 41 | + public function register_specs() { |
| 42 | + $this->register_spec( |
| 43 | + 'footnotes-json', |
| 44 | + __( 'Footnotes JSON', 'apple-news' ), |
| 45 | + [ |
| 46 | + 'role' => 'container', |
| 47 | + 'layout' => 'body-layout', |
| 48 | + 'components' => '#components#', |
| 49 | + ] |
| 50 | + ); |
| 51 | + |
| 52 | + $this->register_spec( |
| 53 | + 'footnote-json', |
| 54 | + __( 'Footnote JSON', 'apple-news' ), |
| 55 | + [ |
| 56 | + 'role' => 'text', |
| 57 | + 'text' => '#text#', |
| 58 | + 'identifier' => '#identifier#', |
| 59 | + ] |
| 60 | + ); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Build the component. |
| 66 | + * |
| 67 | + * @param string $html The HTML to parse into text for processing. |
| 68 | + * @access protected |
| 69 | + */ |
| 70 | + protected function build( $html ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 71 | + preg_match_all( '/<li.*?>.*?<\/li>/', $html, $matches ); |
| 72 | + $items = $matches[0]; |
| 73 | + |
| 74 | + // convert each list item to a paragraph with a number added. |
| 75 | + foreach ( $items as $key => $item ) { |
| 76 | + $count = $key + 1; |
| 77 | + $text = preg_replace( |
| 78 | + '/<li(.*?)>(.*?)<\/li>/', |
| 79 | + "<p$1>${count}. $2</p>", |
| 80 | + $item |
| 81 | + ); |
| 82 | + preg_match( '/id="(.*?)"/', $text, $matches ); |
| 83 | + $id = $matches[1] ?? null; |
| 84 | + $components[] = [ |
| 85 | + 'role' => 'body', |
| 86 | + 'text' => $text, |
| 87 | + 'format' => 'html', |
| 88 | + 'identifier' => $id, |
| 89 | + ]; |
| 90 | + } |
| 91 | + $this->register_json( |
| 92 | + 'footnotes-json', |
| 93 | + [ |
| 94 | + '#components#' => $components, |
| 95 | + ] |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
0 commit comments