syntax-highlighting-code-block
Last commit date
build
5 years ago
vendor
5 years ago
LICENSE
6 years ago
editor-styles.css
6 years ago
readme.txt
5 years ago
style.css
6 years ago
syntax-highlighting-code-block.php
5 years ago
syntax-highlighting-code-block.php
669 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: Syntax-highlighting Code Block (with Server-side Rendering) |
| 4 | * Plugin URI: https://github.com/westonruter/syntax-highlighting-code-block |
| 5 | * Description: Extending the Code block with syntax highlighting rendered on the server, thus being AMP-compatible and having faster frontend performance. |
| 6 | * Version: 1.2.3 |
| 7 | * Author: Weston Ruter |
| 8 | * Author URI: https://weston.ruter.net/ |
| 9 | * License: GPL2 |
| 10 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 11 | * Text Domain: syntax-highlighting-code-block |
| 12 | * Requires PHP: 5.6 |
| 13 | * |
| 14 | * @package Syntax_Highlighting_Code_Block |
| 15 | */ |
| 16 | |
| 17 | namespace Syntax_Highlighting_Code_Block; |
| 18 | |
| 19 | use Exception; |
| 20 | use WP_Block_Type_Registry; |
| 21 | use WP_Error; |
| 22 | use WP_Customize_Manager; |
| 23 | use WP_Styles; |
| 24 | |
| 25 | const PLUGIN_VERSION = '1.2.3'; |
| 26 | |
| 27 | const BLOCK_NAME = 'core/code'; |
| 28 | |
| 29 | const DEVELOPMENT_MODE = false; |
| 30 | |
| 31 | const OPTION_NAME = 'syntax_highlighting'; |
| 32 | |
| 33 | const DEFAULT_THEME = 'default'; |
| 34 | |
| 35 | const BLOCK_STYLE_FILTER = 'syntax_highlighting_code_block_style'; |
| 36 | |
| 37 | const HIGHLIGHTED_LINE_BACKGROUND_COLOR_FILTER = 'syntax_highlighted_line_background_color'; |
| 38 | |
| 39 | const FRONTEND_STYLE_HANDLE = 'syntax-highlighting-code-block'; |
| 40 | |
| 41 | /** |
| 42 | * Add a tint to an RGB color and make it lighter. |
| 43 | * |
| 44 | * @param float[] $rgb_array An array representing an RGB color. |
| 45 | * @param float $tint How much of a tint to apply; a number between 0 and 1. |
| 46 | * @return float[] The new color as an RGB array. |
| 47 | */ |
| 48 | function add_tint_to_rgb( $rgb_array, $tint ) { |
| 49 | return [ |
| 50 | 'r' => $rgb_array['r'] + ( 255 - $rgb_array['r'] ) * $tint, |
| 51 | 'g' => $rgb_array['g'] + ( 255 - $rgb_array['g'] ) * $tint, |
| 52 | 'b' => $rgb_array['b'] + ( 255 - $rgb_array['b'] ) * $tint, |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the relative luminance of a color. |
| 58 | * |
| 59 | * @link https://en.wikipedia.org/wiki/Relative_luminance |
| 60 | * |
| 61 | * @param float[] $rgb_array An array representing an RGB color. |
| 62 | * @return float A value between 0 and 100 representing the luminance of a color. |
| 63 | * The closer to to 100, the higher the luminance is; i.e. the lighter it is. |
| 64 | */ |
| 65 | function get_relative_luminance( $rgb_array ) { |
| 66 | return 0.2126 * ( $rgb_array['r'] / 255 ) + |
| 67 | 0.7152 * ( $rgb_array['g'] / 255 ) + |
| 68 | 0.0722 * ( $rgb_array['b'] / 255 ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check whether or not a given RGB array is considered a "dark theme." |
| 73 | * |
| 74 | * @param float[] $rgb_array The RGB array to test. |
| 75 | * @return bool True if the theme's background has a "dark" luminance. |
| 76 | */ |
| 77 | function is_dark_theme( $rgb_array ) { |
| 78 | return get_relative_luminance( $rgb_array ) <= 0.6; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Convert an RGB array to hexadecimal representation. |
| 83 | * |
| 84 | * @param float[] $rgb_array The RGB array to convert. |
| 85 | * @return string A hexadecimal representation. |
| 86 | */ |
| 87 | function get_hex_from_rgb( $rgb_array ) { |
| 88 | return sprintf( |
| 89 | '#%02X%02X%02X', |
| 90 | $rgb_array['r'], |
| 91 | $rgb_array['g'], |
| 92 | $rgb_array['b'] |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the default highlighted line background color. |
| 98 | * |
| 99 | * In a dark theme, the background color is decided by adding a 15% tint to the |
| 100 | * color. |
| 101 | * |
| 102 | * In a light theme, a default light blue is used. |
| 103 | * |
| 104 | * @param string $theme_name The theme name to get a color for. |
| 105 | * @return string A hexadecimal value. |
| 106 | */ |
| 107 | function get_default_line_background_color( $theme_name ) { |
| 108 | require_highlight_php_functions(); |
| 109 | |
| 110 | $theme_rgb = \HighlightUtilities\getThemeBackgroundColor( $theme_name ); |
| 111 | |
| 112 | if ( is_dark_theme( $theme_rgb ) ) { |
| 113 | return get_hex_from_rgb( |
| 114 | add_tint_to_rgb( $theme_rgb, 0.15 ) |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | return '#ddf6ff'; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get an array of all the options tied to this plugin. |
| 123 | * |
| 124 | * @return array |
| 125 | */ |
| 126 | function get_plugin_options() { |
| 127 | $options = \get_option( OPTION_NAME, [] ); |
| 128 | |
| 129 | $theme_name = isset( $options['theme_name'] ) ? $options['theme_name'] : DEFAULT_THEME; |
| 130 | return array_merge( |
| 131 | [ |
| 132 | 'theme_name' => DEFAULT_THEME, |
| 133 | 'highlighted_line_background_color' => get_default_line_background_color( $theme_name ), |
| 134 | ], |
| 135 | $options |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the single, specified plugin option. |
| 141 | * |
| 142 | * @param string $option_name The plugin option name. |
| 143 | * @return string|null |
| 144 | */ |
| 145 | function get_plugin_option( $option_name ) { |
| 146 | $options = get_plugin_options(); |
| 147 | if ( array_key_exists( $option_name, $options ) ) { |
| 148 | return $options[ $option_name ]; |
| 149 | } |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Require the highlight.php functions file. |
| 155 | */ |
| 156 | function require_highlight_php_functions() { |
| 157 | if ( DEVELOPMENT_MODE ) { |
| 158 | require_once __DIR__ . '/vendor/scrivo/highlight.php/HighlightUtilities/functions.php'; |
| 159 | } else { |
| 160 | require_once __DIR__ . '/vendor/scrivo/highlight-php/HighlightUtilities/functions.php'; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Initialize plugin. |
| 166 | * |
| 167 | * As of Gutenberg 8.3, this must run after `init` priority 10, because at that point the core blocks are registered |
| 168 | * server-side via `gutenberg_reregister_core_block_types()`. |
| 169 | * |
| 170 | * @see gutenberg_reregister_core_block_types() |
| 171 | * @see https://github.com/WordPress/gutenberg/issues/2751 |
| 172 | * @see https://github.com/WordPress/gutenberg/pull/22491 |
| 173 | */ |
| 174 | function init() { |
| 175 | if ( ! function_exists( 'register_block_type' ) ) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | if ( DEVELOPMENT_MODE && ! file_exists( __DIR__ . '/build/index.asset.php' ) ) { |
| 180 | add_action( 'admin_notices', __NAMESPACE__ . '\print_build_required_admin_notice' ); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | $attributes = [ |
| 185 | 'language' => [ |
| 186 | 'type' => 'string', |
| 187 | 'default' => '', |
| 188 | ], |
| 189 | 'highlightedLines' => [ |
| 190 | 'type' => 'string', |
| 191 | 'default' => '', |
| 192 | ], |
| 193 | 'showLineNumbers' => [ |
| 194 | 'type' => 'boolean', |
| 195 | 'default' => false, |
| 196 | ], |
| 197 | 'wrapLines' => [ |
| 198 | 'type' => 'boolean', |
| 199 | 'default' => false, |
| 200 | ], |
| 201 | ]; |
| 202 | |
| 203 | $registry = WP_Block_Type_Registry::get_instance(); |
| 204 | |
| 205 | if ( $registry->is_registered( BLOCK_NAME ) ) { |
| 206 | $block = $registry->get_registered( BLOCK_NAME ); |
| 207 | $block->render_callback = __NAMESPACE__ . '\render_block'; |
| 208 | $block->attributes = array_merge( $block->attributes, $attributes ); |
| 209 | } else { |
| 210 | register_block_type( |
| 211 | BLOCK_NAME, |
| 212 | [ |
| 213 | 'render_callback' => __NAMESPACE__ . '\render_block', |
| 214 | 'attributes' => $attributes, |
| 215 | ] |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_editor_assets' ); |
| 220 | } |
| 221 | add_action( 'init', __NAMESPACE__ . '\init', 100 ); |
| 222 | |
| 223 | /** |
| 224 | * Print admin notice when plugin installed from source but no build being performed. |
| 225 | */ |
| 226 | function print_build_required_admin_notice() { |
| 227 | ?> |
| 228 | <div class="notice notice-error"> |
| 229 | <p> |
| 230 | <strong><?php esc_html_e( 'Syntax-highlighting Code Block', 'syntax-highlighting-code-block' ); ?>:</strong> |
| 231 | <?php |
| 232 | echo wp_kses_post( |
| 233 | sprintf( |
| 234 | /* translators: %s is the command to run */ |
| 235 | __( 'Unable to initialize plugin due to being installed from source without running a build. Please run %s', 'syntax-highlighting-code-block' ), |
| 236 | '<code>composer install && npm install && npm run build</code>' |
| 237 | ) |
| 238 | ); |
| 239 | ?> |
| 240 | </p> |
| 241 | </div> |
| 242 | <?php |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Enqueue assets for editor. |
| 247 | */ |
| 248 | function enqueue_editor_assets() { |
| 249 | $script_handle = 'syntax-highlighting-code-block-scripts'; |
| 250 | $script_path = '/build/index.js'; |
| 251 | $style_handle = 'syntax-highlighting-code-block-styles'; |
| 252 | $style_path = '/editor-styles.css'; |
| 253 | $script_asset = require __DIR__ . '/build/index.asset.php'; |
| 254 | $in_footer = true; |
| 255 | |
| 256 | wp_enqueue_style( |
| 257 | $style_handle, |
| 258 | plugins_url( $style_path, __FILE__ ), |
| 259 | [], |
| 260 | SCRIPT_DEBUG ? filemtime( plugin_dir_path( __FILE__ ) . $style_path ) : PLUGIN_VERSION |
| 261 | ); |
| 262 | |
| 263 | wp_enqueue_script( |
| 264 | $script_handle, |
| 265 | plugins_url( $script_path, __FILE__ ), |
| 266 | $script_asset['dependencies'], |
| 267 | $script_asset['version'], |
| 268 | $in_footer |
| 269 | ); |
| 270 | |
| 271 | wp_set_script_translations( $script_handle, 'syntax-highlighting-code-block' ); |
| 272 | |
| 273 | $block = WP_Block_Type_Registry::get_instance()->get_registered( BLOCK_NAME ); |
| 274 | $data = [ |
| 275 | 'name' => BLOCK_NAME, |
| 276 | 'attributes' => $block->attributes, |
| 277 | 'deprecated' => [ |
| 278 | 'selectedLines' => $block->attributes['highlightedLines'], |
| 279 | 'showLines' => $block->attributes['showLineNumbers'], |
| 280 | ], |
| 281 | ]; |
| 282 | wp_add_inline_script( |
| 283 | $script_handle, |
| 284 | sprintf( 'const syntaxHighlightingCodeBlockType = %s;', wp_json_encode( $data ) ), |
| 285 | 'before' |
| 286 | ); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Register assets for the frontend. |
| 291 | * |
| 292 | * Asset(s) will only be enqueued if needed. |
| 293 | * |
| 294 | * @param WP_Styles $styles Styles. |
| 295 | */ |
| 296 | function register_styles( WP_Styles $styles ) { |
| 297 | if ( has_filter( BLOCK_STYLE_FILTER ) ) { |
| 298 | /** |
| 299 | * Filters the style used for the code syntax block. |
| 300 | * |
| 301 | * The string returned must correspond to the filenames found at <https://github.com/scrivo/highlight.php/tree/master/styles>, |
| 302 | * minus the file extension. |
| 303 | * |
| 304 | * This filter takes precedence over any settings set in the database as an option. Additionally, if this filter |
| 305 | * is provided, then a theme selector will not be provided in Customizer. |
| 306 | * |
| 307 | * @since 1.0.0 |
| 308 | * @param string $style Style. |
| 309 | */ |
| 310 | $style = apply_filters( BLOCK_STYLE_FILTER, DEFAULT_THEME ); |
| 311 | } else { |
| 312 | $style = get_plugin_option( 'theme_name' ); |
| 313 | } |
| 314 | |
| 315 | $default_style_path = sprintf( |
| 316 | 'vendor/scrivo/%s/styles/%s.css', |
| 317 | DEVELOPMENT_MODE ? 'highlight.php' : 'highlight-php', |
| 318 | 0 === validate_file( $style ) ? $style : DEFAULT_THEME |
| 319 | ); |
| 320 | $styles->add( |
| 321 | FRONTEND_STYLE_HANDLE, |
| 322 | plugins_url( $default_style_path, __FILE__ ), |
| 323 | [], |
| 324 | SCRIPT_DEBUG ? filemtime( plugin_dir_path( __FILE__ ) . $default_style_path ) : PLUGIN_VERSION |
| 325 | ); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Get styles. |
| 330 | * |
| 331 | * @param array $attributes Attributes. |
| 332 | * @return string Attributes. |
| 333 | */ |
| 334 | function get_styles( $attributes ) { |
| 335 | if ( is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { |
| 336 | return ''; |
| 337 | } |
| 338 | |
| 339 | static $added_inline_style = false; |
| 340 | static $added_highlighted_color_style = false; |
| 341 | |
| 342 | if ( ! wp_style_is( FRONTEND_STYLE_HANDLE, 'registered' ) ) { |
| 343 | register_styles( wp_styles() ); |
| 344 | } |
| 345 | |
| 346 | // Print stylesheet now that we know it will be needed. Note that the stylesheet is not being enqueued at the |
| 347 | // wp_enqueue_scripts action because this could result in the stylesheet being printed when it would never be used. |
| 348 | // When a stylesheet is printed in the body it has the additional benefit of not being render-blocking. When |
| 349 | // a stylesheet is printed the first time, subsequent calls to wp_print_styles() will no-op. |
| 350 | ob_start(); |
| 351 | wp_print_styles( FRONTEND_STYLE_HANDLE ); |
| 352 | $styles = trim( ob_get_clean() ); |
| 353 | |
| 354 | // Include line-number styles if requesting to show lines. |
| 355 | if ( ! $added_inline_style ) { |
| 356 | $styles .= sprintf( '<style>%s</style>', file_get_contents( __DIR__ . '/style.css' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 357 | |
| 358 | $added_inline_style = true; |
| 359 | } |
| 360 | |
| 361 | if ( ! $added_highlighted_color_style && ! empty( $attributes['highlightedLines'] ) ) { |
| 362 | if ( has_filter( HIGHLIGHTED_LINE_BACKGROUND_COLOR_FILTER ) ) { |
| 363 | /** |
| 364 | * Filters the background color of a highlighted line. |
| 365 | * |
| 366 | * This filter takes precedence over any settings set in the database as an option. Additionally, if this filter |
| 367 | * is provided, then a color selector will not be provided in Customizer. |
| 368 | * |
| 369 | * @param string $rgb_color An RGB hexadecimal (with the #) to be used as the background color of a highlighted line. |
| 370 | * |
| 371 | * @since 1.1.5 |
| 372 | */ |
| 373 | $line_color = apply_filters( HIGHLIGHTED_LINE_BACKGROUND_COLOR_FILTER, get_default_line_background_color( DEFAULT_THEME ) ); |
| 374 | } else { |
| 375 | $line_color = get_plugin_option( 'highlighted_line_background_color' ); |
| 376 | } |
| 377 | |
| 378 | $styles .= "<style>.hljs > mark.shcb-loc { background-color: $line_color; }</style>"; |
| 379 | |
| 380 | $added_highlighted_color_style = true; |
| 381 | } |
| 382 | |
| 383 | return $styles; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Inject class names and styles into the |
| 388 | * |
| 389 | * @param string $pre_start_tag The `<pre>` start tag. |
| 390 | * @param string $code_start_tag The `<code>` start tag. |
| 391 | * @param array $attributes Attributes. |
| 392 | * @return string Injected markup. |
| 393 | */ |
| 394 | function inject_markup( $pre_start_tag, $code_start_tag, $attributes ) { |
| 395 | $added_classes = 'hljs'; |
| 396 | |
| 397 | if ( $attributes['language'] ) { |
| 398 | $added_classes .= " language-{$attributes['language']}"; |
| 399 | } |
| 400 | |
| 401 | if ( $attributes['showLineNumbers'] || $attributes['highlightedLines'] ) { |
| 402 | $added_classes .= ' shcb-code-table'; |
| 403 | } |
| 404 | |
| 405 | if ( $attributes['showLineNumbers'] ) { |
| 406 | $added_classes .= ' shcb-line-numbers'; |
| 407 | } |
| 408 | |
| 409 | if ( $attributes['wrapLines'] ) { |
| 410 | $added_classes .= ' shcb-wrap-lines'; |
| 411 | } |
| 412 | |
| 413 | $code_start_tag = preg_replace( |
| 414 | '/(<code[^>]*class=["\'])/', |
| 415 | '$1 ' . esc_attr( $added_classes ), |
| 416 | $code_start_tag, |
| 417 | 1, |
| 418 | $count |
| 419 | ); |
| 420 | if ( 0 === $count ) { |
| 421 | $code_start_tag = preg_replace( |
| 422 | '/(?<=<code\b)/', |
| 423 | sprintf( ' class="%s"', esc_attr( $added_classes ) ), |
| 424 | $code_start_tag, |
| 425 | 1 |
| 426 | ); |
| 427 | } |
| 428 | |
| 429 | return $pre_start_tag . get_styles( $attributes ) . '<div>' . $code_start_tag; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Render code block. |
| 434 | * |
| 435 | * @param array $attributes Attributes. |
| 436 | * @param string $content Content. |
| 437 | * @return string Highlighted content. |
| 438 | */ |
| 439 | function render_block( $attributes, $content ) { |
| 440 | $pattern = '(?P<pre_start_tag><pre\b[^>]*?>)(?P<code_start_tag><code\b[^>]*?>)'; |
| 441 | $pattern .= '(?P<content>.*)'; |
| 442 | $pattern .= '</code></pre>'; |
| 443 | |
| 444 | if ( ! preg_match( '#^\s*' . $pattern . '\s*$#s', $content, $matches ) ) { |
| 445 | return $content; |
| 446 | } |
| 447 | |
| 448 | // Migrate legacy attribute names. |
| 449 | if ( isset( $attributes['selectedLines'] ) ) { |
| 450 | $attributes['highlightedLines'] = $attributes['selectedLines']; |
| 451 | unset( $attributes['selectedLines'] ); |
| 452 | } |
| 453 | if ( isset( $attributes['showLines'] ) ) { |
| 454 | $attributes['showLineNumbers'] = $attributes['showLines']; |
| 455 | unset( $attributes['showLines'] ); |
| 456 | } |
| 457 | |
| 458 | $end_tags = '</code></div></pre>'; |
| 459 | |
| 460 | /** |
| 461 | * Filters the list of languages that are used for auto-detection. |
| 462 | * |
| 463 | * @param string[] $auto_detect_language Auto-detect languages. |
| 464 | */ |
| 465 | $auto_detect_languages = apply_filters( 'syntax_highlighting_code_block_auto_detect_languages', [] ); |
| 466 | |
| 467 | $transient_key = 'syntax-highlighted-' . md5( wp_json_encode( $attributes ) . implode( '', $auto_detect_languages ) . $matches['content'] . PLUGIN_VERSION ); |
| 468 | $highlighted = get_transient( $transient_key ); |
| 469 | |
| 470 | if ( ! DEVELOPMENT_MODE && $highlighted && isset( $highlighted['content'] ) ) { |
| 471 | return inject_markup( $matches['pre_start_tag'], $matches['code_start_tag'], $highlighted['attributes'] ) . $highlighted['content'] . $end_tags; |
| 472 | } |
| 473 | |
| 474 | try { |
| 475 | if ( ! class_exists( '\Highlight\Autoloader' ) ) { |
| 476 | if ( DEVELOPMENT_MODE ) { |
| 477 | require_once __DIR__ . '/vendor/scrivo/highlight.php/Highlight/Autoloader.php'; |
| 478 | } else { |
| 479 | require_once __DIR__ . '/vendor/scrivo/highlight-php/Highlight/Autoloader.php'; |
| 480 | } |
| 481 | spl_autoload_register( 'Highlight\Autoloader::load' ); |
| 482 | } |
| 483 | |
| 484 | $highlighter = new \Highlight\Highlighter(); |
| 485 | if ( ! empty( $auto_detect_languages ) ) { |
| 486 | $highlighter->setAutodetectLanguages( $auto_detect_languages ); |
| 487 | } |
| 488 | |
| 489 | $language = $attributes['language']; |
| 490 | $content = html_entity_decode( $matches['content'], ENT_QUOTES ); |
| 491 | |
| 492 | // Convert from Prism.js languages names. |
| 493 | if ( 'clike' === $language ) { |
| 494 | $language = 'cpp'; |
| 495 | } elseif ( 'git' === $language ) { |
| 496 | $language = 'diff'; // Best match. |
| 497 | } elseif ( 'markup' === $language ) { |
| 498 | $language = 'xml'; |
| 499 | } |
| 500 | |
| 501 | if ( $language ) { |
| 502 | $r = $highlighter->highlight( $language, $content ); |
| 503 | } else { |
| 504 | $r = $highlighter->highlightAuto( $content ); |
| 505 | } |
| 506 | $attributes['language'] = $r->language; |
| 507 | |
| 508 | $content = $r->value; |
| 509 | if ( $attributes['showLineNumbers'] || $attributes['highlightedLines'] ) { |
| 510 | require_highlight_php_functions(); |
| 511 | |
| 512 | $highlighted_lines = parse_highlighted_lines( $attributes['highlightedLines'] ); |
| 513 | $lines = \HighlightUtilities\splitCodeIntoArray( $content ); |
| 514 | $content = ''; |
| 515 | |
| 516 | // We need to wrap the line of code twice in order to let out `white-space: pre` CSS setting to be respected |
| 517 | // by our `table-row`. |
| 518 | foreach ( $lines as $i => $line ) { |
| 519 | $tag_name = in_array( $i, $highlighted_lines, true ) ? 'mark' : 'span'; |
| 520 | $content .= "<$tag_name class='shcb-loc'><span>$line\n</span></$tag_name>"; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if ( ! DEVELOPMENT_MODE ) { |
| 525 | set_transient( $transient_key, compact( 'content', 'attributes' ), MONTH_IN_SECONDS ); |
| 526 | } |
| 527 | |
| 528 | return inject_markup( $matches['pre_start_tag'], $matches['code_start_tag'], $attributes ) . $content . $end_tags; |
| 529 | } catch ( Exception $e ) { |
| 530 | return sprintf( |
| 531 | '<!-- %s(%s): %s -->%s', |
| 532 | get_class( $e ), |
| 533 | $e->getCode(), |
| 534 | str_replace( '--', '', $e->getMessage() ), |
| 535 | $content |
| 536 | ); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Parse the highlighted line syntax from the front-end and return an array of highlighted line numbers. |
| 542 | * |
| 543 | * @param string $highlighted_lines The highlighted line syntax. |
| 544 | * @return int[] |
| 545 | */ |
| 546 | function parse_highlighted_lines( $highlighted_lines ) { |
| 547 | $highlighted_line_numbers = []; |
| 548 | |
| 549 | if ( ! $highlighted_lines || empty( trim( $highlighted_lines ) ) ) { |
| 550 | return $highlighted_line_numbers; |
| 551 | } |
| 552 | |
| 553 | $ranges = explode( ',', preg_replace( '/\s/', '', $highlighted_lines ) ); |
| 554 | |
| 555 | foreach ( $ranges as $chunk ) { |
| 556 | if ( strpos( $chunk, '-' ) !== false ) { |
| 557 | $range = explode( '-', $chunk ); |
| 558 | |
| 559 | if ( count( $range ) === 2 ) { |
| 560 | for ( $i = (int) $range[0]; $i <= (int) $range[1]; $i++ ) { |
| 561 | $highlighted_line_numbers[] = $i - 1; |
| 562 | } |
| 563 | } |
| 564 | } else { |
| 565 | $highlighted_line_numbers[] = (int) $chunk - 1; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | return $highlighted_line_numbers; |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * Validate the given stylesheet name against available stylesheets. |
| 574 | * |
| 575 | * @param WP_Error $validity Validator object. |
| 576 | * @param string $input Incoming theme name. |
| 577 | * @return mixed |
| 578 | */ |
| 579 | function validate_theme_name( $validity, $input ) { |
| 580 | require_highlight_php_functions(); |
| 581 | |
| 582 | $themes = \HighlightUtilities\getAvailableStyleSheets(); |
| 583 | |
| 584 | if ( ! in_array( $input, $themes, true ) ) { |
| 585 | $validity->add( 'invalid_theme', __( 'Unrecognized theme', 'syntax-highlighting-code-block' ) ); |
| 586 | } |
| 587 | |
| 588 | return $validity; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Add plugin settings to Customizer. |
| 593 | * |
| 594 | * @param WP_Customize_Manager $wp_customize The Customizer object. |
| 595 | */ |
| 596 | function customize_register( $wp_customize ) { |
| 597 | if ( has_filter( BLOCK_STYLE_FILTER ) && has_filter( HIGHLIGHTED_LINE_BACKGROUND_COLOR_FILTER ) ) { |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | require_highlight_php_functions(); |
| 602 | |
| 603 | if ( ! has_filter( BLOCK_STYLE_FILTER ) ) { |
| 604 | $themes = \HighlightUtilities\getAvailableStyleSheets(); |
| 605 | sort( $themes ); |
| 606 | $choices = array_combine( $themes, $themes ); |
| 607 | |
| 608 | $wp_customize->add_setting( |
| 609 | 'syntax_highlighting[theme_name]', |
| 610 | [ |
| 611 | 'type' => 'option', |
| 612 | 'default' => DEFAULT_THEME, |
| 613 | 'validate_callback' => __NAMESPACE__ . '\validate_theme_name', |
| 614 | ] |
| 615 | ); |
| 616 | $wp_customize->add_control( |
| 617 | 'syntax_highlighting[theme_name]', |
| 618 | [ |
| 619 | 'type' => 'select', |
| 620 | 'section' => 'colors', |
| 621 | 'label' => __( 'Syntax Highlighting Theme', 'syntax-highlighting-code-block' ), |
| 622 | 'description' => __( 'Preview the theme by navigating to a page with a code block to see the different themes in action.', 'syntax-highlighting-code-block' ), |
| 623 | 'choices' => $choices, |
| 624 | ] |
| 625 | ); |
| 626 | } |
| 627 | |
| 628 | if ( ! has_filter( HIGHLIGHTED_LINE_BACKGROUND_COLOR_FILTER ) ) { |
| 629 | $wp_customize->add_setting( |
| 630 | 'syntax_highlighting[highlighted_line_background_color]', |
| 631 | [ |
| 632 | 'type' => 'option', |
| 633 | 'sanitize_callback' => 'sanitize_hex_color', |
| 634 | ] |
| 635 | ); |
| 636 | $wp_customize->add_control( |
| 637 | new \WP_Customize_Color_Control( |
| 638 | $wp_customize, |
| 639 | 'syntax_highlighting[highlighted_line_background_color]', |
| 640 | [ |
| 641 | 'section' => 'colors', |
| 642 | 'settings' => 'syntax_highlighting[highlighted_line_background_color]', |
| 643 | 'label' => __( 'Highlighted Line Color', 'syntax-highlighting-code-block' ), |
| 644 | 'description' => __( 'The background color of a highlighted line.', 'syntax-highlighting-code-block' ), |
| 645 | ] |
| 646 | ) |
| 647 | ); |
| 648 | } |
| 649 | } |
| 650 | add_action( 'customize_register', __NAMESPACE__ . '\customize_register' ); |
| 651 | |
| 652 | /** |
| 653 | * Override the post value for the highlighted line background color when the theme has been highlighted. |
| 654 | * |
| 655 | * This is an unfortunate workaround for the Customizer not respecting dynamic updates to the default setting value. |
| 656 | * |
| 657 | * @todo What's missing is dynamically changing the default value of the highlighted_line_background_color control based on the selected theme. |
| 658 | * |
| 659 | * @param WP_Customize_Manager $wp_customize Customize manager. |
| 660 | */ |
| 661 | function override_highlighted_line_background_color_post_value( WP_Customize_Manager $wp_customize ) { |
| 662 | $highlighted_line_background_color_setting = $wp_customize->get_setting( 'syntax_highlighting[highlighted_line_background_color]' ); |
| 663 | if ( $highlighted_line_background_color_setting && ! $highlighted_line_background_color_setting->post_value() ) { |
| 664 | $highlighted_line_background_color_setting->default = get_default_line_background_color( get_plugin_option( 'theme_name' ) ); // This has no effect. |
| 665 | $wp_customize->set_post_value( $highlighted_line_background_color_setting->id, $highlighted_line_background_color_setting->default ); |
| 666 | } |
| 667 | } |
| 668 | add_action( 'customize_preview_init', __NAMESPACE__ . '\override_highlighted_line_background_color_post_value' ); |
| 669 |