/** * Render the editor preview setting */ function code_snippets_settings_editor_preview() { $example_content = "\nfunction example_custom_admin_footer_text( \$text ) {\n\treturn 'Thank you for visiting <a href=\"' . get_home_url() . '\">' . get_bloginfo( 'name' ) . '</a>.';\n}\n\nadd_filter( 'admin_footer_text', 'example_custom_admin_footer_text' );"; $atts = array('mode' => 'text/x-php', 'value' => $example_content); ?> <div style="max-width: 800px" id="code_snippets_editor_preview"></div> <script> (function () { 'use strict'; // Load CodeMirror var atts = <?php echo code_snippets_get_editor_atts($atts, true); ?> ; var editor = CodeMirror(document.getElementById('code_snippets_editor_preview'), atts); // Dynamically change editor settings <?php /* Retrieve editor settings */ $fields = code_snippets_get_settings_fields(); $fields = $fields['editor']; foreach ($fields as $setting => $field) { /* Only output settings which have a CodeMirror attribute */ if (empty($field['codemirror'])) { continue; } $att_name = $field['codemirror']; switch ($field['type']) { case 'codemirror_theme_select': ?> document.querySelector('select[name="code_snippets_settings[editor][<?php echo $setting; ?> ]"]').onchange = function () { editor.setOption('<?php echo $att_name; ?> ', this.options[this.selectedIndex].value); }; <?php break; case 'checkbox': ?> document.querySelector('input[name="code_snippets_settings[editor][<?php echo $setting; ?> ]"]').onchange = function () { editor.setOption('<?php echo $att_name; ?> ', this.checked); }; <?php break; case 'number': ?> document.querySelector('input[name="code_snippets_settings[editor][<?php echo $setting; ?> ]"]').onchange = function () { editor.setOption( '<?php echo $att_name; ?> ', this.value); }; <?php break; } } ?> }()); </script> <?php }
_e('Settings', 'code-snippets'); ?> </h3> <table class="form-table"> <?php do_action('code_snippets/admin/single/settings', $snippet); ?> </table> <?php } ?> </form> </div> <script> /** * Loads CodeMirror on the snippet editor */ (function() { var atts = <?php $atts = array('mode' => 'text/x-php'); echo code_snippets_get_editor_atts($atts, true); ?> ; CodeMirror.fromTextArea(document.getElementById('snippet_code'), atts); })(); </script>
function code_snippets_settings_editor_preview() { $example_content = "\nfunction example_custom_admin_footer_text( \$text ) {\n\treturn 'Thank you for visiting <a href=\"' . get_home_url() . '\">' . get_bloginfo( 'name' ) . '</a>.';\n}\n\nadd_filter( 'admin_footer_text', 'example_custom_admin_footer_text' );"; $atts = array('mode' => 'text/x-php', 'value' => $example_content); ?> <div style="max-width: 800px" id="code_snippets_editor_preview"></div> <script> (function( $ ) { 'use strict'; $(function() { // Load CodeMirror var atts = <?php echo code_snippets_get_editor_atts($atts, true); ?> ; var editor = CodeMirror(document.getElementById('code_snippets_editor_preview'), atts); // Dynamically change editor settings <?php $fields = code_snippets_get_settings_fields(); $fields = $fields['editor']; $types = wp_list_pluck($fields, 'type', 'id'); $codemirror_atts = wp_list_pluck($fields, 'codemirror', 'id'); foreach ($codemirror_atts as $setting => $att_name) { switch ($types[$setting]) { case 'codemirror_theme_select': ?> $( 'select[name="code_snippets_settings[editor][<?php echo $setting; ?> ]"]' ).change( function () { editor.setOption( '<?php echo $att_name; ?> ', $(this).val() ); } ); <?php break; case 'checkbox': ?> $( 'input[name="code_snippets_settings[editor][<?php echo $setting; ?> ]"]' ).change( function () { editor.setOption( '<?php echo $att_name; ?> ', $(this).is(':checked') ); } ); <?php break; case 'number': ?> $( 'input[name="code_snippets_settings[editor][<?php echo $setting; ?> ]"]' ).change( function () { editor.setOption( '<?php echo $att_name; ?> ', $(this).val() ); } ); <?php break; } } ?> }); }(jQuery)); </script> <?php }