| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file handles the editor preview setting |
| 5 |
* |
| 6 |
* @since 2.0 |
| 7 |
* @package Code_Snippets |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Load the CSS and JavaScript for the editor preview field |
| 12 |
*/ |
| 13 |
function code_snippets_editor_settings_preview_assets() { |
| 14 |
$plugin = code_snippets(); |
| 15 |
|
| 16 |
// Enqueue scripts for the editor preview |
| 17 |
code_snippets_enqueue_editor(); |
| 18 |
|
| 19 |
// Enqueue all editor themes |
| 20 |
$themes = code_snippets_get_available_themes(); |
| 21 |
|
| 22 |
foreach ( $themes as $theme ) { |
| 23 |
if ( 'default' !== $theme ) { |
| 24 |
wp_enqueue_style( |
| 25 |
'code-snippets-editor-theme-' . $theme, |
| 26 |
plugins_url( "css/min/editor-themes/$theme.css", $plugin->file ), |
| 27 |
array( 'code-snippets-editor' ), $plugin->version |
| 28 |
); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// Enqueue the menu scripts |
| 33 |
wp_enqueue_script( |
| 34 |
'code-snippets-settings-menu', |
| 35 |
plugins_url( 'js/min/settings.js', $plugin->file ), |
| 36 |
array( 'code-snippets-editor' ), $plugin->version, true |
| 37 |
); |
| 38 |
|
| 39 |
// Extract the CodeMirror-specific editor settings |
| 40 |
$setting_fields = code_snippets_get_settings_fields(); |
| 41 |
$editor_fields = array(); |
| 42 |
|
| 43 |
foreach ( $setting_fields['editor'] as $name => $field ) { |
| 44 |
if ( empty( $field['codemirror'] ) ) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
$editor_fields[] = array( |
| 49 |
'name' => $name, |
| 50 |
'type' => $field['type'], |
| 51 |
'codemirror' => addslashes( $field['codemirror'] ), |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
// Pass the saved options to the external JavaScript file |
| 56 |
$inline_script = 'var code_snippets_editor_atts = ' . code_snippets_get_editor_atts( array(), true ) . ';'; |
| 57 |
$inline_script .= "\n" . 'var code_snippets_editor_settings = ' . wp_json_encode( $editor_fields ) . ';'; |
| 58 |
|
| 59 |
wp_add_inline_script( 'code-snippets-settings-menu', $inline_script, 'before' ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Render a theme select field |
| 64 |
* |
| 65 |
* @param array $atts |
| 66 |
*/ |
| 67 |
function code_snippets_codemirror_theme_select_field( $atts ) { |
| 68 |
|
| 69 |
$saved_value = code_snippets_get_setting( $atts['section'], $atts['id'] ); |
| 70 |
|
| 71 |
echo '<select name="code_snippets_settings[editor][theme]">'; |
| 72 |
|
| 73 |
// print a dropdown entry for each theme |
| 74 |
foreach ( code_snippets_get_available_themes() as $theme ) { |
| 75 |
|
| 76 |
// skip mobile themes |
| 77 |
if ( 'ambiance-mobile' === $theme ) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
printf( |
| 82 |
'<option value="%s"%s>%s</option>', |
| 83 |
$theme, |
| 84 |
selected( $theme, $saved_value, false ), |
| 85 |
ucwords( str_replace( '-', ' ', $theme ) ) |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
echo '</select>'; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Render the editor preview setting |
| 94 |
*/ |
| 95 |
function code_snippets_settings_editor_preview() { |
| 96 |
echo '<div id="code_snippets_editor_preview"></div>'; |
| 97 |
} |
| 98 |
|