| 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 |
$settings = code_snippets_get_settings(); |
| 13 |
$settings = $settings['editor']; |
| 14 |
|
| 15 |
$fields = code_snippets_get_settings_fields(); |
| 16 |
$fields = $fields['editor']; |
| 17 |
|
| 18 |
$saved_atts = array( |
| 19 |
'matchBrackets' => true, |
| 20 |
); |
| 21 |
|
| 22 |
foreach ( $fields as $field_id => $field ) { |
| 23 |
$saved_atts[ $field['codemirror'] ] = $settings[ $field_id ]; |
| 24 |
} |
| 25 |
|
| 26 |
$atts = wp_parse_args( $override_atts, $saved_atts ); |
| 27 |
$atts = apply_filters( 'code_snippets_codemirror_atts', $atts ); |
| 28 |
|
| 29 |
if ( $json_encode ) { |
| 30 |
|
| 31 |
/* JSON_UNESCAPED_SLASHES was added in PHP 5.4 */ |
| 32 |
if ( version_compare( phpversion(), '5.4.0', '>=' ) ) { |
| 33 |
$atts = json_encode( $atts, JSON_UNESCAPED_SLASHES ); |
| 34 |
} else { |
| 35 |
/* Use a fallback for < 5.4 */ |
| 36 |
$atts = str_replace( '\\/', '/', json_encode( $atts ) ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return $atts; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Registers and loads the CodeMirror library |
| 45 |
* |
| 46 |
* @uses wp_enqueue_style() to add the stylesheets to the queue |
| 47 |
* @uses wp_enqueue_script() to add the scripts to the queue |
| 48 |
*/ |
| 49 |
function code_snippets_enqueue_codemirror() { |
| 50 |
$codemirror_version = '5.41.0'; |
| 51 |
$url = plugin_dir_url( CODE_SNIPPETS_FILE ); |
| 52 |
|
| 53 |
/* Remove other CodeMirror styles */ |
| 54 |
wp_deregister_style( 'codemirror' ); |
| 55 |
wp_deregister_style( 'wpeditor' ); |
| 56 |
|
| 57 |
/* CodeMirror */ |
| 58 |
wp_enqueue_style( |
| 59 |
'code-snippets-codemirror', |
| 60 |
$url . 'css/min/codemirror.css', |
| 61 |
false, $codemirror_version |
| 62 |
); |
| 63 |
|
| 64 |
wp_enqueue_script( |
| 65 |
'code-snippets-codemirror', |
| 66 |
$url . 'js/min/codemirror.js', |
| 67 |
false, $codemirror_version |
| 68 |
); |
| 69 |
|
| 70 |
/* CodeMirror Theme */ |
| 71 |
$theme = code_snippets_get_setting( 'editor', 'theme' ); |
| 72 |
|
| 73 |
if ( 'default' !== $theme ) { |
| 74 |
|
| 75 |
wp_enqueue_style( |
| 76 |
'code-snippets-codemirror-theme-' . $theme, |
| 77 |
$url . "css/min/cmthemes/$theme.css", |
| 78 |
array( 'code-snippets-codemirror' ), |
| 79 |
$codemirror_version |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Retrieve a list of the available CodeMirror themes |
| 86 |
* @return array the available themes |
| 87 |
*/ |
| 88 |
function code_snippets_get_available_themes() { |
| 89 |
static $themes = null; |
| 90 |
|
| 91 |
if ( ! is_null( $themes ) ) { |
| 92 |
return $themes; |
| 93 |
} |
| 94 |
|
| 95 |
$themes = array(); |
| 96 |
$themes_dir = plugin_dir_path( CODE_SNIPPETS_FILE ) . 'css/min/cmthemes/'; |
| 97 |
$theme_files = glob( $themes_dir . '*.css' ); |
| 98 |
|
| 99 |
foreach ( $theme_files as $i => $theme ) { |
| 100 |
$theme = str_replace( $themes_dir, '', $theme ); |
| 101 |
$theme = str_replace( '.css', '', $theme ); |
| 102 |
$themes[] = $theme; |
| 103 |
} |
| 104 |
|
| 105 |
return $themes; |
| 106 |
} |
| 107 |
|