Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
36 / 39
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
About
97.30% covered (success)
97.30%
36 / 37
80.00% covered (warning)
80.00%
4 / 5
18
0.00% covered (danger)
0.00%
0 / 1
 get_content_type
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_content
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 to_markdown
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 get_page
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
 render
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * AboutSpec: /.well-known/about.md
4 *
5 * @link https://aboutspec.org/
6 *
7 * @package automattic/jetpack-publicize
8 */
9
10namespace Automattic\Jetpack\Publicize\Socialspec;
11
12use Automattic\Jetpack\Publicize\Lib\Markdown;
13
14if ( ! defined( 'ABSPATH' ) ) {
15    exit( 0 );
16}
17
18/**
19 * The About page as Markdown.
20 */
21class About implements Document {
22
23    /**
24     * Filters that add something of their own below the content: Sharing and Likes.
25     */
26    const ADDITIONS = array( 'sharing_show', 'wpl_is_likes_visible' );
27
28    /**
29     * The media type.
30     *
31     * @return string
32     */
33    public function get_content_type() {
34        return 'text/markdown';
35    }
36
37    /**
38     * The Markdown document: the title and the content of the About page.
39     *
40     * @return string|null Null if there is no public About page, or the page is empty.
41     */
42    public function get_content() {
43        $page = self::get_page();
44
45        if ( ! $page ) {
46            return null;
47        }
48
49        $html     = self::render( $page );
50        $title    = self::to_markdown( '<h1>' . get_the_title( $page ) . '</h1>' );
51        $markdown = self::to_markdown( $html );
52
53        // HTML is valid Markdown, so it is the fallback if it cannot be converted.
54        if ( '' === $markdown ) {
55            $markdown = trim( wp_kses_post( $html ) );
56        }
57
58        if ( '' !== $title ) {
59            $markdown = rtrim( $title . "\n\n" . $markdown );
60        }
61
62        return '' !== $markdown ? $markdown . "\n" : null;
63    }
64
65    /**
66     * Converts HTML to Markdown with the html-to-md library where the site has it, and with our converter otherwise.
67     *
68     * @link https://github.com/dmsnell/html-to-md
69     *
70     * @param string $html The HTML.
71     * @return string The Markdown, or an empty string if the HTML cannot be converted.
72     */
73    private static function to_markdown( $html ) {
74        $markdown = '';
75
76        if ( function_exists( 'wp_html_to_markdown' ) ) {
77            try {
78                // @phan-suppress-next-line PhanUndeclaredFunction -- From the html-to-md library, on sites that have it.
79                $markdown = trim( (string) wp_html_to_markdown( $html ) );
80            } catch ( \Throwable $e ) {
81                // The library throws on HTML it cannot handle.
82                $markdown = '';
83            }
84        }
85
86        return '' !== $markdown ? $markdown : trim( (string) Markdown::from_html( $html ) );
87    }
88
89    /**
90     * The About page, if everyone can see it: the page from the settings, or the one at /about.
91     *
92     * @return \WP_Post|null
93     */
94    private static function get_page() {
95        /**
96         * Filters the page published as /.well-known/about.md.
97         *
98         * @since $$next-version$$
99         *
100         * @param int|\WP_Post $page The page from the settings, or the page at /about until one is picked. 0 for none.
101         */
102        $page = apply_filters( 'jetpack_social_about_page', Socialspec::get_about_page_id() );
103
104        // get_post( 0 ) would return the global post.
105        $page = $page ? get_post( $page ) : null;
106
107        if ( ! $page instanceof \WP_Post || '' !== $page->post_password || ! is_post_publicly_viewable( $page ) ) {
108            return null;
109        }
110
111        return $page;
112    }
113
114    /**
115     * Renders the page content like the theme would, so access checks on the_content apply.
116     *
117     * Blocks and shortcodes may read the global post, so it is set up for the duration.
118     *
119     * @param \WP_Post $page The page.
120     * @return string
121     */
122    private static function render( \WP_Post $page ) {
123        $previous = $GLOBALS['post'] ?? null;
124
125        $GLOBALS['post'] = $page; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
126        setup_postdata( $page );
127
128        // A callback of its own, so removing it cannot remove another plugin's `__return_false`.
129        $hide = static function () {
130            return false;
131        };
132
133        foreach ( self::ADDITIONS as $hook ) {
134            add_filter( $hook, $hide, PHP_INT_MAX );
135        }
136
137        /** This filter is documented in wp-includes/post-template.php */
138        $html = (string) apply_filters( 'the_content', $page->post_content );
139
140        foreach ( self::ADDITIONS as $hook ) {
141            remove_filter( $hook, $hide, PHP_INT_MAX );
142        }
143
144        // In this order: wp_reset_postdata() sets the global post to the one of the main query.
145        wp_reset_postdata();
146        $GLOBALS['post'] = $previous; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
147
148        return $html;
149    }
150}