Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
10.20% |
5 / 49 |
|
0.00% |
0 / 6 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_insert_shared_post_data | |
15.62% |
5 / 32 |
|
0.00% |
0 / 1 |
46.44 | |||
| wpcomsh_get_embed_content | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| wpcomsh_get_image_content | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| wpcomsh_get_text_content | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| wpcomsh_filter_title | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| wpcomsh_filter_content | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Adds fallback behavior for non-Gutenframed sites to be able to use the 'Share Post' functionality from WPCOM Reader. |
| 4 | * |
| 5 | * @package share-post |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Saves shared post data. |
| 10 | * |
| 11 | * @param int $target_post_id Post ID. |
| 12 | * @param ?WP_Post $post Post object. |
| 13 | * @param bool $update Whether this is an update. |
| 14 | * |
| 15 | * @return int|void|WP_Error |
| 16 | */ |
| 17 | function wpcomsh_insert_shared_post_data( $target_post_id, $post = null, $update = false ) { |
| 18 | |
| 19 | // There are cases when plugins trigger insert hooks without arguments, we just return in this case. |
| 20 | if ( null === $post ) { |
| 21 | return $target_post_id; |
| 22 | } |
| 23 | |
| 24 | // This `$update` check avoids infinite loops of trying to update our updated post. |
| 25 | if ( $update ) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | // Abort this function if we are not in a post sharing context. |
| 30 | $is_post_share = isset( $_GET['is_post_share'] ) && $_GET['is_post_share']; // phpcs:ignore WordPress.Security |
| 31 | if ( ! $is_post_share ) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | // phpcs:disable WordPress.Security |
| 36 | $title = $_GET['title']; |
| 37 | $image = $_GET['image']; |
| 38 | $text = $_GET['text']; |
| 39 | $url = $_GET['url']; |
| 40 | $embed = strtok( $_GET['embed'], '?' ); |
| 41 | $link = sprintf( |
| 42 | '<a href="%1$s">%2$s</a>', |
| 43 | esc_url( $url ), |
| 44 | sanitize_text_field( $title ) |
| 45 | ); |
| 46 | $content = ''; |
| 47 | // phpcs:enable |
| 48 | |
| 49 | if ( $embed ) { |
| 50 | $content .= wpcomsh_get_embed_content( $embed ); |
| 51 | } |
| 52 | |
| 53 | if ( $image ) { |
| 54 | $content .= wpcomsh_get_image_content( $image, $text, $link ); |
| 55 | } |
| 56 | |
| 57 | if ( $text ) { |
| 58 | $content .= wpcomsh_get_text_content( $text, $link ); |
| 59 | } |
| 60 | |
| 61 | $data = array( |
| 62 | 'ID' => $target_post_id, |
| 63 | 'post_title' => $title, |
| 64 | 'post_content' => $content, |
| 65 | ); |
| 66 | |
| 67 | // Required to satisfy get_default_post_to_edit(), which has these filters after post creation. |
| 68 | add_filter( 'default_title', 'wpcomsh_filter_title', 10, 2 ); |
| 69 | add_filter( 'default_content', 'wpcomsh_filter_content', 10, 2 ); |
| 70 | |
| 71 | return wp_update_post( $data ); |
| 72 | } |
| 73 | add_action( 'wp_insert_post', 'wpcomsh_insert_shared_post_data', 10, 3 ); |
| 74 | |
| 75 | /** |
| 76 | * Generate the embed content for our sharing post. |
| 77 | * |
| 78 | * @param string $embed URL of our embed. |
| 79 | * @return string Generated content for the sharing post. |
| 80 | */ |
| 81 | function wpcomsh_get_embed_content( $embed ) { |
| 82 | return sprintf( |
| 83 | '<!-- wp:embed {"url":"%s"} --><figure><div class="wp-block-embed__wrapper">%s</div></figure><!-- /wp:embed -->', |
| 84 | esc_url( $embed ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Generate the image content for our sharing post. |
| 90 | * |
| 91 | * @param string $image URL of our image. |
| 92 | * @param string $text Content of the shared post. |
| 93 | * @param string $link Permalink to the shared post. |
| 94 | * @return string Generated content for the sharing post. |
| 95 | */ |
| 96 | function wpcomsh_get_image_content( $image, $text, $link ) { |
| 97 | return sprintf( |
| 98 | '<!-- wp:image --><figure class="wp-block-image"><img src="%1$s" alt=""/>%2$s</figure><!-- /wp:image -->', |
| 99 | esc_url( $image ), |
| 100 | empty( $text ) ? '<figcaption>' . $link . '</figcaption>' : null // $link is escaped on initialization |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Generate the text content for our sharing post. |
| 106 | * |
| 107 | * @param string $text Content of the shared post. |
| 108 | * @param string $link Permalink to the shared post. |
| 109 | * @return string Generated content for the sharing post. |
| 110 | */ |
| 111 | function wpcomsh_get_text_content( $text, $link ) { |
| 112 | return sprintf( |
| 113 | '<!-- wp:quote --><blockquote class="wp-block-quote"><p>%1$s</p><cite>%2$s</cite></blockquote><!-- /wp:quote -->', |
| 114 | wp_kses_post( $text ), |
| 115 | $link // Escaped on initialization. |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Update the target post's title. |
| 121 | * |
| 122 | * @param string $post_title Post title determined by `get_default_post_to_edit()`. |
| 123 | * @param WP_Post $post Post object of newly-inserted post. |
| 124 | * @return string Updated post title from source post. |
| 125 | */ |
| 126 | function wpcomsh_filter_title( $post_title, $post ) { |
| 127 | return $post->post_title; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Update the target post's content (`post_content`). |
| 132 | * |
| 133 | * @param string $post_content Post content determined by `get_default_post_to_edit()`. |
| 134 | * @param WP_Post $post Post object of newly-inserted post. |
| 135 | * @return string Updated post content from source post. |
| 136 | */ |
| 137 | function wpcomsh_filter_content( $post_content, $post ) { |
| 138 | return $post->post_content; |
| 139 | } |