| 1 |
<?php |
| 2 |
/** |
| 3 |
* Markdown handling |
| 4 |
* |
| 5 |
* Handles all Markdown operations and rendering. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 2.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\System; |
| 13 |
|
| 14 |
use cebe\markdownparser\GithubMarkdown; |
| 15 |
|
| 16 |
/** |
| 17 |
* Define the Markdown functionality. |
| 18 |
* |
| 19 |
* Handles all Markdown operations and rendering. |
| 20 |
* |
| 21 |
* @package System |
| 22 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 23 |
* @since 2.0.0 |
| 24 |
*/ |
| 25 |
class Markdown { |
| 26 |
|
| 27 |
/** |
| 28 |
* Get the changelog. |
| 29 |
* |
| 30 |
* @param string $file The filename. |
| 31 |
* @param array $attributes 'style' => 'markdown', 'html'. |
| 32 |
* 'mode' => 'raw', 'clean'. |
| 33 |
* @return string The output of the shortcode, ready to print. |
| 34 |
* @since 2.0.0 |
| 35 |
*/ |
| 36 |
public function get_shortcode( $file, $attributes ) { |
| 37 |
$_attributes = shortcode_atts( |
| 38 |
[ |
| 39 |
'style' => 'html', |
| 40 |
'mode' => 'clean', |
| 41 |
], |
| 42 |
$attributes |
| 43 |
); |
| 44 |
$style = $_attributes['style']; |
| 45 |
$mode = $_attributes['mode']; |
| 46 |
$error = decalog_esc_html__( 'Sorry, unable to find or read the specified file.', 'decalog' ); |
| 47 |
$result = esc_html( $error ); |
| 48 |
$changelog = DECALOG_PLUGIN_DIR . $file; |
| 49 |
if ( file_exists( $changelog ) ) { |
| 50 |
try { |
| 51 |
// phpcs:ignore |
| 52 |
$content = file_get_contents( $changelog ); |
| 53 |
if ( $content ) { |
| 54 |
switch ( $style ) { |
| 55 |
case 'html': |
| 56 |
$result = '<div class="markdown">' . $this->html_from_markdown( $content, ( 'clean' === $mode ) ) . '</div>'; |
| 57 |
break; |
| 58 |
default: |
| 59 |
$result = esc_html( $content ); |
| 60 |
} |
| 61 |
} |
| 62 |
} catch ( \Exception $e ) { |
| 63 |
$result = esc_html( $error ); |
| 64 |
} |
| 65 |
} |
| 66 |
return $result; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the changelog. |
| 71 |
* |
| 72 |
* @param string $content The filename. |
| 73 |
* @param array $attributes 'style' => 'markdown', 'html'. |
| 74 |
* 'mode' => 'raw', 'clean'. |
| 75 |
* @return string The output of the inline, ready to print. |
| 76 |
*/ |
| 77 |
public function get_inline( $content, $attributes ) { |
| 78 |
$_attributes = shortcode_atts( |
| 79 |
[ |
| 80 |
'style' => 'html', |
| 81 |
'mode' => 'clean', |
| 82 |
], |
| 83 |
$attributes |
| 84 |
); |
| 85 |
$style = $_attributes['style']; |
| 86 |
$mode = $_attributes['mode']; |
| 87 |
if ( $content ) { |
| 88 |
switch ( $style ) { |
| 89 |
case 'html': |
| 90 |
$result = '<div class="markdown">' . $this->html_from_markdown( $content, ( 'clean' === $mode ) ) . '</div>'; |
| 91 |
break; |
| 92 |
default: |
| 93 |
$result = esc_html( $content ); |
| 94 |
} |
| 95 |
} |
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Format a changelog in html. |
| 101 |
* |
| 102 |
* @param string $content The raw changelog in markdown. |
| 103 |
* @param boolean $clean Optional. Should the output be cleaned?. |
| 104 |
* @return string The converted changelog, ready to print. |
| 105 |
* @since 2.0.0 |
| 106 |
*/ |
| 107 |
private function html_from_markdown( $content, $clean = false ) { |
| 108 |
$markdown = new GithubMarkdown(); |
| 109 |
$markdown->html5 = true; |
| 110 |
$markdown->enableNewlines = true; |
| 111 |
$result = $markdown->parse( $content ); |
| 112 |
if ( $clean ) { |
| 113 |
$result = preg_replace( '/<h1>.*<\/h1>/iU', '', $result ); |
| 114 |
for ( $i = 8; $i > 1; $i-- ) { |
| 115 |
$result = str_replace( [ '<h' . $i . '>', '</h' . $i . '>' ], [ '<h' . (string) ( $i + 1 ) . '>', '</h' . (string) ( $i + 1 ) . '>' ], $result ); |
| 116 |
} |
| 117 |
$result = preg_replace_callback( |
| 118 |
'/<h([0-9])>(.*)<\/h([0-9])>/iU', |
| 119 |
function( $matches ) { |
| 120 |
return '<h' . $matches[1] . ' id="' . sanitize_title_with_dashes( $matches[2] ) . '">' . $matches[2] . '</h' . $matches[3] . '>'; |
| 121 |
}, |
| 122 |
$result |
| 123 |
); |
| 124 |
} |
| 125 |
return wp_kses( |
| 126 |
$result, |
| 127 |
[ |
| 128 |
'a' => [ |
| 129 |
'href' => [], |
| 130 |
'title' => [], |
| 131 |
'rel' => [], |
| 132 |
], |
| 133 |
'blockquote' => [ 'cite' => [] ], |
| 134 |
'br' => [], |
| 135 |
'p' => [], |
| 136 |
'code' => [ 'class' => [] ], |
| 137 |
'pre' => [], |
| 138 |
'em' => [], |
| 139 |
'strong' => [], |
| 140 |
'ul' => [], |
| 141 |
'ol' => [], |
| 142 |
'li' => [], |
| 143 |
'h3' => [ 'id' => [] ], |
| 144 |
'h4' => [ ], |
| 145 |
'h5' => [ ], |
| 146 |
] |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|