Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
9 / 12 |
|
0.00% |
0 / 1 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\Extensions\Sharing_Buttons\render_block_implementation | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
5.03 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Sharing Buttons block render implementation. |
| 4 | * |
| 5 | * Loaded lazily from sharing-buttons.php only when the block is rendered, to keep |
| 6 | * the render body out of the eager front-end PHP/opcache footprint. |
| 7 | * |
| 8 | * @package automattic/jetpack |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\Extensions\Sharing_Buttons; |
| 12 | |
| 13 | use Automattic\Jetpack\Status\Request; |
| 14 | use WP_HTML_Tag_Processor; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit( 0 ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Sharing Buttons block registration/dependency declaration. |
| 22 | * |
| 23 | * @param array $attr Array containing the Sharing Buttons block attributes. |
| 24 | * @param string $content String containing the Sharing Buttons block content. |
| 25 | * |
| 26 | * @return string |
| 27 | */ |
| 28 | function render_block_implementation( $attr, $content ) { |
| 29 | // Render nothing in other contexts than frontend (i.e. feed, emails, API, etc.). |
| 30 | if ( ! Request::is_frontend() ) { |
| 31 | return ''; |
| 32 | } |
| 33 | |
| 34 | // save.jsx hard-codes this id, typo included; site CSS may target it, so only repeats get a new one. |
| 35 | static $list_id_taken = false; |
| 36 | $list_id = 'jetpack-sharing-serivces-list'; |
| 37 | |
| 38 | $list = new WP_HTML_Tag_Processor( $content ); |
| 39 | if ( $list->next_tag( 'ul' ) && $list_id === $list->get_attribute( 'id' ) ) { |
| 40 | if ( $list_id_taken ) { |
| 41 | $list->set_attribute( 'id', wp_unique_id( $list_id . '-' ) ); |
| 42 | } |
| 43 | $list_id_taken = true; |
| 44 | } |
| 45 | |
| 46 | return $list->get_updated_html(); |
| 47 | } |