| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Get the attributes for the code editor |
| 5 |
* |
| 6 |
* @param array $override_atts Pass an array of attributes to override the saved ones |
| 7 |
* @param bool $json_encode Encode the data as JSON |
| 8 |
* |
| 9 |
* @return array|string Array if $json_encode is false, JSON string if it is true |
| 10 |
*/ |
| 11 |
function code_snippets_get_editor_atts( $override_atts, $json_encode ) { |
| 12 |
|
| 13 |
// default attributes for the CodeMirror editor |
| 14 |
$default_atts = array( |
| 15 |
'mode' => 'text/x-php', |
| 16 |
'matchBrackets' => true, |
| 17 |
'extraKeys' => array( 'Alt-F' => 'findPersistent' ), |
| 18 |
'gutters' => array( 'CodeMirror-lint-markers' ), |
| 19 |
'lint' => true, |
| 20 |
'viewportMargin' => 'Infinity' |
| 21 |
); |
| 22 |
|
| 23 |
// add relevant saved setting values to the default attributes |
| 24 |
$settings = code_snippets_get_settings(); |
| 25 |
$fields = code_snippets_get_settings_fields(); |
| 26 |
|
| 27 |
foreach ( $fields['editor'] as $field_id => $field ) { |
| 28 |
// the 'codemirror' setting field specifies the name of the attribute |
| 29 |
$default_atts[ $field['codemirror'] ] = $settings['editor'][ $field_id ]; |
| 30 |
} |
| 31 |
|
| 32 |
// merge the default attributes with the ones passed into the function |
| 33 |
$atts = wp_parse_args( $default_atts, $override_atts ); |
| 34 |
$atts = apply_filters( 'code_snippets_codemirror_atts', $atts ); |
| 35 |
|
| 36 |
// encode the attributes for display if requested |
| 37 |
if ( $json_encode ) { |
| 38 |
|
| 39 |
// JSON_UNESCAPED_SLASHES was added in PHP 5.4 |
| 40 |
if ( version_compare( phpversion(), '5.4.0', '>=' ) ) { |
| 41 |
$atts = json_encode( $atts, JSON_UNESCAPED_SLASHES ); |
| 42 |
} else { |
| 43 |
// Use a fallback for < 5.4 |
| 44 |
$atts = str_replace( '\\/', '/', json_encode( $atts ) ); |
| 45 |
} |
| 46 |
|
| 47 |
// Infinity is a constant and needs to be unquoted |
| 48 |
$atts = str_replace( '"Infinity"', 'Infinity', $atts ); |
| 49 |
} |
| 50 |
|
| 51 |
return $atts; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Register and load the CodeMirror library |
| 56 |
* |
| 57 |
* @uses wp_enqueue_style() to add the stylesheets to the queue |
| 58 |
* @uses wp_enqueue_script() to add the scripts to the queue |
| 59 |
*/ |
| 60 |
function code_snippets_enqueue_editor() { |
| 61 |
$url = plugin_dir_url( CODE_SNIPPETS_FILE ); |
| 62 |
$plugin_version = code_snippets()->version; |
| 63 |
|
| 64 |
/* Remove other CodeMirror styles */ |
| 65 |
wp_deregister_style( 'codemirror' ); |
| 66 |
wp_deregister_style( 'wpeditor' ); |
| 67 |
|
| 68 |
/* CodeMirror */ |
| 69 |
wp_enqueue_style( 'code-snippets-editor', $url . 'css/min/editor.css', array(), $plugin_version ); |
| 70 |
wp_enqueue_script( 'code-snippets-editor', $url . 'js/min/editor.js', array(), $plugin_version ); |
| 71 |
|
| 72 |
/* CodeMirror Theme */ |
| 73 |
$theme = code_snippets_get_setting( 'editor', 'theme' ); |
| 74 |
|
| 75 |
if ( 'default' !== $theme ) { |
| 76 |
|
| 77 |
wp_enqueue_style( |
| 78 |
'code-snippets-editor-theme-' . $theme, |
| 79 |
$url . "css/min/editor-themes/$theme.css", |
| 80 |
array( 'code-snippets-editor' ), $plugin_version |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Retrieve a list of the available CodeMirror themes |
| 87 |
* @return array the available themes |
| 88 |
*/ |
| 89 |
function code_snippets_get_available_themes() { |
| 90 |
static $themes = null; |
| 91 |
|
| 92 |
if ( ! is_null( $themes ) ) { |
| 93 |
return $themes; |
| 94 |
} |
| 95 |
|
| 96 |
$themes = array(); |
| 97 |
$themes_dir = plugin_dir_path( CODE_SNIPPETS_FILE ) . 'css/min/editor-themes/'; |
| 98 |
$theme_files = glob( $themes_dir . '*.css' ); |
| 99 |
|
| 100 |
foreach ( $theme_files as $i => $theme ) { |
| 101 |
$theme = str_replace( $themes_dir, '', $theme ); |
| 102 |
$theme = str_replace( '.css', '', $theme ); |
| 103 |
$themes[] = $theme; |
| 104 |
} |
| 105 |
|
| 106 |
return $themes; |
| 107 |
} |
| 108 |
|