| 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 |
* @param string $hook The current page hook |
| 14 |
*/ |
| 15 |
function code_snippets_editor_settings_preview_assets( $hook ) { |
| 16 |
|
| 17 |
/* Only load on the settings page */ |
| 18 |
if ( code_snippets()->get_menu_hook( 'settings' ) !== $hook ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
/* Enqueue scripts for the editor preview */ |
| 23 |
code_snippets_enqueue_codemirror(); |
| 24 |
|
| 25 |
/* Enqueue ALL themes */ |
| 26 |
$themes = code_snippets_get_available_themes(); |
| 27 |
|
| 28 |
foreach ( $themes as $theme ) { |
| 29 |
|
| 30 |
wp_enqueue_style( |
| 31 |
'code-snippets-codemirror-theme-' . $theme, |
| 32 |
plugins_url( "css/min/cmthemes/$theme.css", CODE_SNIPPETS_FILE ), |
| 33 |
array( 'code-snippets-codemirror' ) |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
wp_enqueue_script( 'jquery' ); |
| 38 |
} |
| 39 |
|
| 40 |
add_action( 'admin_enqueue_scripts', 'code_snippets_editor_settings_preview_assets' ); |
| 41 |
|
| 42 |
/** |
| 43 |
* Render a theme select field |
| 44 |
*/ |
| 45 |
function code_snippets_codemirror_theme_select_field( $atts ) { |
| 46 |
|
| 47 |
$saved_value = code_snippets_get_setting( $atts['section'], $atts['id'] ); |
| 48 |
|
| 49 |
echo '<select name="code_snippets_settings[editor][theme]">'; |
| 50 |
echo '<option value="default"' . selected( 'default', $saved_value, false ) . '>Default</option>'; |
| 51 |
|
| 52 |
/* Fetch all theme CSS files */ |
| 53 |
$themes_dir = plugin_dir_path( CODE_SNIPPETS_FILE ) . 'css/min/cmthemes/'; |
| 54 |
$themes = glob( $themes_dir . '*.css' ); |
| 55 |
|
| 56 |
/* Print dropdown entry for each theme */ |
| 57 |
foreach ( $themes as $theme ) { |
| 58 |
|
| 59 |
/* Extract theme name from path */ |
| 60 |
$theme = str_replace( $themes_dir, '', $theme ); |
| 61 |
$theme = str_replace( '.css', '', $theme ); |
| 62 |
|
| 63 |
/* Skip mobile themes */ |
| 64 |
if ( 'ambiance-mobile' === $theme ) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
printf( |
| 68 |
'<option value="%s"%s>%s</option>', |
| 69 |
$theme, |
| 70 |
selected( $theme, $saved_value, false ), |
| 71 |
ucwords( str_replace( '-', ' ', $theme ) ) |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
echo '</select>'; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Render the editor preview setting |
| 80 |
*/ |
| 81 |
function code_snippets_settings_editor_preview() { |
| 82 |
|
| 83 |
$example_content = " |
| 84 |
function example_custom_admin_footer_text( \$text ) { |
| 85 |
return 'Thank you for visiting <a href=\"' . get_home_url() . '\">' . get_bloginfo( 'name' ) . '</a>.'; |
| 86 |
} |
| 87 |
|
| 88 |
add_filter( 'admin_footer_text', 'example_custom_admin_footer_text' ); |
| 89 |
|
| 90 |
"; |
| 91 |
|
| 92 |
$atts = array( |
| 93 |
'mode' => 'text/x-php', |
| 94 |
'value' => $example_content, |
| 95 |
); |
| 96 |
|
| 97 |
?> |
| 98 |
|
| 99 |
<div style="max-width: 800px" id="code_snippets_editor_preview"></div> |
| 100 |
|
| 101 |
<script> |
| 102 |
(function () { |
| 103 |
'use strict'; |
| 104 |
|
| 105 |
// Load CodeMirror |
| 106 |
var atts = []; |
| 107 |
atts = <?php echo code_snippets_get_editor_atts( $atts, true ); ?>; |
| 108 |
atts['viewportMargin'] = Infinity; |
| 109 |
|
| 110 |
var editor = CodeMirror(document.getElementById('code_snippets_editor_preview'), atts); |
| 111 |
|
| 112 |
// Dynamically change editor settings |
| 113 |
<?php |
| 114 |
|
| 115 |
/* Retrieve editor settings */ |
| 116 |
$fields = code_snippets_get_settings_fields(); |
| 117 |
$fields = $fields['editor']; |
| 118 |
|
| 119 |
foreach ( $fields as $setting => $field ) { |
| 120 |
|
| 121 |
/* Only output settings which have a CodeMirror attribute */ |
| 122 |
if ( empty( $field['codemirror'] ) ) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
$att_name = addslashes( $field['codemirror'] ); |
| 127 |
|
| 128 |
switch ( $field['type'] ) { |
| 129 |
|
| 130 |
case 'codemirror_theme_select': |
| 131 |
?> |
| 132 |
document.querySelector('select[name="code_snippets_settings[editor][<?php echo $setting; ?>]"]').onchange = function () { |
| 133 |
editor.setOption('<?php echo $att_name; ?>', this.options[this.selectedIndex].value); |
| 134 |
}; |
| 135 |
|
| 136 |
<?php break; |
| 137 |
|
| 138 |
case 'checkbox': |
| 139 |
?> |
| 140 |
document.querySelector('input[name="code_snippets_settings[editor][<?php echo $setting; ?>]"]').onchange = function () { |
| 141 |
editor.setOption('<?php echo $att_name; ?>', this.checked); |
| 142 |
}; |
| 143 |
|
| 144 |
<?php break; |
| 145 |
case 'number': |
| 146 |
?> |
| 147 |
document.querySelector('input[name="code_snippets_settings[editor][<?php echo $setting; ?>]"]').onchange = function () { |
| 148 |
editor.setOption('<?php echo $att_name; ?>', this.value); |
| 149 |
}; |
| 150 |
|
| 151 |
<?php break; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
?> |
| 156 |
}()); |
| 157 |
</script> |
| 158 |
|
| 159 |
<?php |
| 160 |
} |
| 161 |
|