This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
forked from nextgenthemes/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prism.php
executable file
·348 lines (248 loc) · 8.4 KB
/
prism.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* @package Prism Syntax Highlighter for WordPress
* @author Nicolas Jonas
* @license GPL-3.0
* @link http://nextgenthemes.com/plugins/prism
* @copyright Copyright (c) 2015 Nicolas Jonas
*
* @wordpress-plugin
* Plugin Name: Prism Syntax Highlighter for WordPress
* Plugin URI: http://nextgenthemes.com/plugins/prism
* Description: Most minimalistic yet most configurabale Prismjs integration plugin, includes shortcode for custom field content (detached)
* Version: 1.1.1
* Author: Nicolas Jonas
* Author URI: https://nextgenthemes.com
* License: GPL-3.0
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* GitHub Plugin URI: https://github.com/nextgenthemes/prism
*
* WordPress-Plugin-Boilerplate: v2.6.1 (Only parts of it)
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die( 'NSA spyware installed, thank you!' );
}
add_action( 'plugins_loaded', array( 'Prism', 'get_instance' ) );
class Prism {
protected static $instance = null;
const PRISM_VERSION = '2016-10-02';
private function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'register_styles' ), 0 );
add_filter( 'mce_css', array( $this, 'plugin_editor_style' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ), 0 );
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_load_prism' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_load_prism' ) );
add_action( 'admin_head', array( $this, 'print_admin_css' ) );
add_action( 'media_buttons', array( $this, 'add_media_button' ), 11 );
add_action( 'admin_footer', array( $this, 'print_admin_javascript' ) );
add_shortcode( 'prism', array( $this, 'shortcode' ) );
add_filter( 'mce_buttons_2', array( $this, 'mce_add_buttons' ) );
add_filter( 'tiny_mce_before_init', array( $this, 'filter_tiny_mce_before_init' ) );
}
public function register_styles() {
$upload_dir = wp_upload_dir();
$cssfile_dir = trailingslashit( $upload_dir['basedir'] ) . 'prism/prism.css';
$cssfile_url = trailingslashit( $upload_dir['baseurl'] ) . 'prism/prism.css';
if ( is_file( $cssfile_dir ) ) {
wp_register_style( 'prism', $cssfile_url, array(), filemtime( $cssfile_dir ) );
} else {
wp_register_style( 'prism', plugins_url( 'prism.css', __FILE__ ), array(), self::PRISM_VERSION );
}
}
public function plugin_editor_style( $mce_css ){
$mce_css .= ', ' . plugins_url( 'prism.css', __FILE__ );
return $mce_css;
}
public function register_scripts() {
$upload_dir = wp_upload_dir();
$jsfile_dir = trailingslashit( $upload_dir['basedir'] ) . 'prism/prism.js';
$jsfile_url = trailingslashit( $upload_dir['baseurl'] ) . 'prism/prism.js';
if ( is_file( $jsfile_dir ) ) {
wp_register_script( 'prism', $jsfile_url, array(), filemtime( $jsfile_dir ), true );
} else {
wp_register_script( 'prism', plugins_url( 'prism.js', __FILE__ ), array(), self::PRISM_VERSION, true );
}
}
public function maybe_load_prism() {
global $post, $wp_query;
$post_contents = '';
if ( is_singular() ) {
$post_contents = $post->post_content;
} else {
$post_ids = wp_list_pluck( $wp_query->posts, 'ID' );
foreach ( $post_ids as $post_id ) {
$post_contents .= get_post_field( 'post_content', $post_id );
}
}
if ( strpos( $post_contents, '<code class="language-' ) !== false ) {
wp_enqueue_style( 'prism' );
wp_enqueue_script( 'prism' );
}
}
public function admin_load_prism() {
#wp_enqueue_style( 'prism' );
$this->register_scripts();
wp_enqueue_script( 'prism' );
}
public function shortcode( $atts, $content = null ) {
$pairs = array(
'field' => false,
'url' => false,
'post_id' => false,
//* <code>
'language' => 'none',
//* <pre>
'id' => false,
'class' => false,
'data_src' => false,
'data_start' => false,
'data_line' => false,
'data_line_offset' => false,
'data_manual' => false,
);
$atts = shortcode_atts( $pairs, $atts, 'prism' );
$pre_attr = array(
'id' => ( $atts['id'] ) ? $atts['id'] : $atts['field'],
'class' => $atts['class'],
'data-src' => esc_url( $atts['data_src'] ),
'data-start' => $atts['data_start'],
'data-line' => $atts['data_line'],
'data-line-offset' => $atts['data_line_offset'],
'data-manual' => $atts['data_manual'],
);
$code_attr = array(
'class' => 'language-' . $atts['language'],
);
if ( $atts['url'] ) {
if ( false === filter_var( $atts['url'], FILTER_VALIDATE_URL ) ) {
return sprintf( '<p><strong>Prism Shortcode Error:</strong> URL <code>%s</code> is invalid </p>', esc_html( $atts['url'] ) );
}
$response = wp_remote_get( esc_url( $atts['url'] ) );
if ( is_wp_error( $response ) ) {
return sprintf( '<p><strong>Prism Shortcode Error:</strong> could not get remote content. WP_Error message:<br>%s</p>', esc_html( $response->get_error_message() ) );
} elseif( 200 != $response['response']['code'] ) {
return sprintf( '<p><strong>Prism Shortcode Error:</strong> could not get remote content. HTTP response code %s</p>', esc_html( $response['response']['code'] ) );
}
wp_enqueue_style( 'prism' );
wp_enqueue_script( 'prism' );
return sprintf(
'<pre %s><code %s>%s</code></pre>',
$this->parse_attr( $pre_attr ),
$this->parse_attr( $code_attr ),
esc_html( $response['body'] )
);
}
if ( $atts['data_src'] ) {
wp_enqueue_style( 'prism' );
wp_enqueue_script( 'prism' );
$pre_attr['class'] .= " language-{$atts['language']}";
return sprintf( '<pre %s></pre>', $this->parse_attr( $pre_attr ) );
}
if ( ! $atts['field'] ) {
return '<p><strong>Prism Shortcode Error:</strong> field, url, data_src is missing</p>';
}
global $post;
$from_post = ( $atts['post_id'] ) ? $atts['post_id'] : $post->ID;
$field_content = get_post_meta( $from_post, $atts['field'], true );
if ( empty( $field_content ) ) {
return '<p><strong>Prism Shortcode Error:</strong> Custom field not set or empty</p>';
}
wp_enqueue_style( 'prism' );
wp_enqueue_script( 'prism' );
return sprintf(
'<pre %s><code %s>%s</code></pre>',
$this->parse_attr( $pre_attr ),
$this->parse_attr( $code_attr ),
esc_html( $field_content )
);
}
public function parse_attr( $attr = array() ) {
$out = '';
foreach ( $attr as $key => $value ) {
if ( 'data-manual' == $key && false !== $value ) {
$out .= ' data-manual';
continue;
}
if ( empty( $value ) ) {
continue;
}
$out .= sprintf( ' %s="%s"', esc_html( $key ), esc_attr( $value ) );
}
return trim( $out );
}
public function add_media_button() {
?>
<a id="prism-shortcode" class="button add_media" title="Prism Shortcode Snippet">
<span class="wp-media-buttons-icon prism-icon"></span> Prism
</a>
<?php
}
public function print_admin_css() {
?>
<style>
#prism-shortcode {
padding-left: 1px;
}
.prism-icon:before {
content: "\f499" !important;
}
</style>
<?php
}
public function print_admin_javascript() {
?>
<script>
(function ( $ ) {
"use strict";
$( '#prism-shortcode' ).click( function( event ) {
event.preventDefault();
send_to_editor( '[prism field="" language=""]' );
} );
}(jQuery));
</script>
<?php
}
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
function filter_tiny_mce_before_init( $settings ) {
$languages = array(
'Bash',
'CSS',
'JavaScript',
'Markup',
'PHP',
'SCSS',
);
$style_formats[] = array(
'title' => "<code>",
'inline' => 'code'
);
foreach ( $languages as $lang ) {
$lang_lowercase = strtolower( $lang );
$style_formats[] = array(
'title' => "$lang <pre>",
'block' => 'pre',
'classes' => "language-$lang_lowercase",
);
$style_formats[] = array(
'title' => "$lang <code>",
'inline' => 'code',
'classes' => "language-$lang_lowercase",
);
}
$settings['style_formats'] = json_encode( $style_formats );
$settings['style_formats_merge'] = false;
#$settings['block_formats'] = 'Paragraph=p;Heading 3=h3;Heading 4=h4;CSS Code=pre';
return $settings;
}
function mce_add_buttons( $buttons ) {
array_splice( $buttons, 1, 0, 'styleselect' );
return $buttons;
}
}