-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
37 lines (35 loc) · 1.13 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
# From https://piszek.com/2022/08/15/wordpress-github-markdown/#
wp_embed_register_handler(
'github_readme_md',
'&https?:\/\/github\.com\/([a-zA-Z-_0-9/]+)\/([a-zA-Z]+)\.md&i',
__NAMESPACE__ . '\artpi_github_markdown_handler'
);
function artpi_github_markdown_handler( $matches, $attr, $url, $rawattr ) {
$url = str_replace(
[ 'github.com', '/blob' ],
[ 'raw.githubusercontent.com', '' ],
$matches[0]
);
$transient_key = 'gh_' . md5( $url );
$content = get_transient( $transient_key );
if ( ! $content ) {
$request = wp_remote_get( $url );
if ( is_wp_error( $request ) ) {
return false;
}
$content = wp_remote_retrieve_body( $request );
if( ! $content ) {
return false;
}
require_once __DIR__ . '/Parsedown.php'; // You will need to download Parsedown https://github.com/erusev/parsedown
$md_parser = new \Parsedown();
$content = $md_parser->text( $content );
if( ! $content ) {
return false;
}
$content = "<div class='github_readme_md'>$content</div>";
set_transient( $transient_key, $content, 3600 );
}
return apply_filters( 'embed_github_readme_md', $content, $matches, $attr, $url, $rawattr );
}