Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
13.64% |
3 / 22 |
|
0.00% |
0 / 3 |
CRAP | n/a |
0 / 0 |
|
| wpcom_delay_repost_pingbacks | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
4.68 | |||
| wpcom_send_delayed_repost_pingbacks | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| wpcom_wp_embed_urls | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Delay the pingbacks an Atomic post sends to the posts it embeds. |
| 4 | * |
| 5 | * WordPress.com drops a repost's pingback once it has recorded the repost, which happens a few minutes after publishing. |
| 6 | * |
| 7 | * @package automattic/jetpack-mu-wpcom |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Hold back pingbacks to posts embedded with a WordPress embed block, and send them a few minutes later. |
| 12 | * |
| 13 | * @param string[] $post_links Links to ping, passed by reference. |
| 14 | * @param string[] $pung Links already pinged, passed by reference. |
| 15 | * @param int $post_id Post ID. |
| 16 | */ |
| 17 | function wpcom_delay_repost_pingbacks( &$post_links, &$pung, $post_id ) { |
| 18 | $held = array_intersect( $post_links, wpcom_wp_embed_urls( parse_blocks( (string) get_post_field( 'post_content', $post_id ) ) ) ); |
| 19 | if ( ! $held ) { |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | $post_links = array_values( array_diff( $post_links, $held ) ); |
| 24 | |
| 25 | $args = array( (int) $post_id ); |
| 26 | if ( ! wp_next_scheduled( 'wpcom_send_delayed_repost_pingbacks', $args ) ) { |
| 27 | wp_schedule_single_event( time() + 3 * MINUTE_IN_SECONDS, 'wpcom_send_delayed_repost_pingbacks', $args ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Send the pingbacks wpcom_delay_repost_pingbacks() held back. Core skips links it has already pinged. |
| 33 | * |
| 34 | * @param int $post_id Post ID. |
| 35 | */ |
| 36 | function wpcom_send_delayed_repost_pingbacks( $post_id ) { |
| 37 | $post = get_post( $post_id ); |
| 38 | if ( ! $post || 'publish' !== $post->post_status ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | remove_action( 'pre_ping', 'wpcom_delay_repost_pingbacks' ); |
| 43 | pingback( $post->post_content, $post ); |
| 44 | add_action( 'pre_ping', 'wpcom_delay_repost_pingbacks', 10, 3 ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * URLs of the WordPress embed blocks in $blocks, inner blocks included. |
| 49 | * |
| 50 | * @param array $blocks Parsed blocks. |
| 51 | * @return string[] |
| 52 | */ |
| 53 | function wpcom_wp_embed_urls( $blocks ) { |
| 54 | $urls = array(); |
| 55 | foreach ( $blocks as $block ) { |
| 56 | if ( 'core/embed' === $block['blockName'] && 'wp-embed' === ( $block['attrs']['type'] ?? '' ) && ! empty( $block['attrs']['url'] ) ) { |
| 57 | $urls[] = $block['attrs']['url']; |
| 58 | } |
| 59 | $urls = array_merge( $urls, wpcom_wp_embed_urls( $block['innerBlocks'] ) ); |
| 60 | } |
| 61 | |
| 62 | return $urls; |
| 63 | } |
| 64 | |
| 65 | // Simple sites drop the pingback before sending it instead. |
| 66 | if ( ( new Automattic\Jetpack\Status\Host() )->is_woa_site() ) { |
| 67 | add_action( 'pre_ping', 'wpcom_delay_repost_pingbacks', 10, 3 ); |
| 68 | add_action( 'wpcom_send_delayed_repost_pingbacks', 'wpcom_send_delayed_repost_pingbacks' ); |
| 69 | } |