Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Code_Editor | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
| setup | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| init | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 | |||
| enqueue_block_editor_assets | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Code Editors |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types = 1 ); |
| 9 | |
| 10 | namespace Automattic\Jetpack; |
| 11 | |
| 12 | /** |
| 13 | * Code Editor class. |
| 14 | * |
| 15 | * Add improved code editors functionality. The "code editor" view in the post and site editors and |
| 16 | * the "Additional CSS" panel found in various places in the site editor are improved with features |
| 17 | * like syntax highlighting and autocompletion. |
| 18 | */ |
| 19 | abstract class Code_Editor { |
| 20 | const MODULE_PREFIX = '@a8cCodeEditor/'; |
| 21 | |
| 22 | /** |
| 23 | * Set up the feature. |
| 24 | * |
| 25 | * This is the main entry point for the code editor feature. |
| 26 | */ |
| 27 | public static function setup() { |
| 28 | add_action( 'init', array( __CLASS__, 'init' ) ); |
| 29 | add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_assets' ) ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Init hook. |
| 34 | */ |
| 35 | public static function init() { |
| 36 | // The asset file should always be present, however on CI it is not built. |
| 37 | // If it were ever absent in a production build, that's unexpected and the |
| 38 | // feature would be unavailable. |
| 39 | $asset_path = plugin_dir_path( __FILE__ ) . '../../build-module/assets.php'; |
| 40 | if ( ! file_exists( $asset_path ) ) { |
| 41 | return; |
| 42 | } |
| 43 | $asset_manifest = include $asset_path; |
| 44 | |
| 45 | $modules = array( |
| 46 | 'code-editor/code-editor.js' => self::MODULE_PREFIX . 'code-editor', |
| 47 | 'codemirror/codemirror.js' => self::MODULE_PREFIX . 'codemirror-bundle', |
| 48 | 'site-additional-css/site-additional-css.js' => self::MODULE_PREFIX . 'site-additional-css', |
| 49 | ); |
| 50 | /** |
| 51 | * Module data keyed by module ID. |
| 52 | * |
| 53 | * @var array<string, array{dependencies: array, version: string, type: string, src: string}> $asset_manifest |
| 54 | */ |
| 55 | $module_assets = array(); |
| 56 | foreach ( $modules as $path => $module_id ) { |
| 57 | if ( ! isset( $asset_manifest[ $path ] ) ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $module_assets[ $module_id ] = $asset_manifest[ $path ]; |
| 62 | $module_assets[ $module_id ]['module_id'] = $module_id; |
| 63 | $module_assets[ $module_id ]['src'] = plugins_url( '../../build-module/' . $path, __FILE__ ); |
| 64 | } |
| 65 | |
| 66 | foreach ( $module_assets as $module_id => $asset_manifest ) { |
| 67 | wp_register_script_module( |
| 68 | $module_id, |
| 69 | $asset_manifest['src'], |
| 70 | $asset_manifest['dependencies'], |
| 71 | $asset_manifest['version'] |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Enqueue block editor assets hook. |
| 78 | */ |
| 79 | public static function enqueue_block_editor_assets() { |
| 80 | $user_prefers_syntax_highlight = 'false' !== wp_get_current_user()->syntax_highlighting; |
| 81 | |
| 82 | /** |
| 83 | * Filter to enable or disable the improved code editor. |
| 84 | * |
| 85 | * This feature enhances the "code editor" views in the Post and Site Editors. |
| 86 | */ |
| 87 | $should_load_code_editor = apply_filters( 'jetpack_mu_wpcom_should_load_code_editor', $user_prefers_syntax_highlight ); |
| 88 | if ( $should_load_code_editor ) { |
| 89 | wp_enqueue_script_module( self::MODULE_PREFIX . 'code-editor' ); |
| 90 | |
| 91 | // Enqueue code editor script dependencies. |
| 92 | wp_enqueue_script( 'wp-i18n' ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Filter to enable or disable the improved CSS editor. |
| 97 | * |
| 98 | * This feature enhances the "Additional CSS" panel in the Site Editor. |
| 99 | */ |
| 100 | $should_load_css_editor = apply_filters( 'jetpack_mu_wpcom_should_load_css_editor', $user_prefers_syntax_highlight ); |
| 101 | if ( $should_load_css_editor ) { |
| 102 | wp_enqueue_script_module( self::MODULE_PREFIX . 'site-additional-css' ); |
| 103 | // The additional CSS panel has no script dependencies. |
| 104 | } |
| 105 | } |
| 106 | } |