PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-markdown.php

class-markdown.php in Sessions 2.3.1, at includes/system/class-markdown.php

121 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 POSessions\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 = esc_html__( 'Sorry, unable to find or read the specified file.', 'sessions' );
47 $result = esc_html( $error );
48 $changelog = POSE_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 * Format a changelog in html.
71 *
72 * @param string $content The raw changelog in markdown.
73 * @param boolean $clean Optional. Should the output be cleaned?.
74 * @return string The converted changelog, ready to print.
75 * @since 2.0.0
76 */
77 private function html_from_markdown( $content, $clean = false ) {
78 $markdown = new GithubMarkdown();
79 $markdown->html5 = true;
80 $markdown->enableNewlines = true;
81 $result = $markdown->parse( $content );
82 if ( $clean ) {
83 $result = preg_replace( '/<h1>.*<\/h1>/iU', '', $result );
84 for ( $i = 8; $i > 1; $i-- ) {
85 $result = str_replace( [ '<h' . $i . '>', '</h' . $i . '>' ], [ '<h' . (string) ( $i + 1 ) . '>', '</h' . (string) ( $i + 1 ) . '>' ], $result );
86 }
87 $result = preg_replace_callback(
88 '/<h([0-9])>(.*)<\/h([0-9])>/iU',
89 function( $matches ) {
90 return '<h' . $matches[1] . ' id="' . sanitize_title_with_dashes( $matches[2] ) . '">' . $matches[2] . '</h' . $matches[3] . '>';
91 },
92 $result
93 );
94 }
95 return wp_kses(
96 $result,
97 [
98 'a' => [
99 'href' => [],
100 'title' => [],
101 'rel' => [],
102 ],
103 'blockquote' => [ 'cite' => [] ],
104 'br' => [],
105 'p' => [],
106 'code' => [ 'class' => [] ],
107 'pre' => [],
108 'em' => [],
109 'strong' => [],
110 'ul' => [],
111 'ol' => [],
112 'li' => [],
113 'h3' => [ 'id' => [] ],
114 'h4' => [ ],
115 'h5' => [ ],
116 ]
117 );
118 }
119
120 }
121