/** * Retrieve the default setting values * @return array */ function code_snippets_get_default_settings() { static $defaults; if (isset($defaults)) { return $defaults; } $defaults = array(); foreach (code_snippets_get_settings_fields() as $section_id => $fields) { $defaults[$section_id] = array(); foreach ($fields as $field_id => $field_atts) { $defaults[$section_id][$field_id] = $field_atts['default']; } } return $defaults; }
/** * Get the attributes for the code editor * * @param array $override_atts Pass an array of attributes to override the saved ones * @param bool $json_encode Encode the data as JSON * @return array|string Array if $json_encode is false, JSON string if it is true */ function code_snippets_get_editor_atts($override_atts, $json_encode) { $settings = code_snippets_get_settings(); $settings = $settings['editor']; $fields = code_snippets_get_settings_fields(); $fields = $fields['editor']; $saved_atts = array('matchBrackets' => true); foreach ($fields as $field_id => $field) { $saved_atts[$field['codemirror']] = $settings[$field_id]; } $atts = wp_parse_args($override_atts, $saved_atts); $atts = apply_filters('code_snippets_codemirror_atts', $atts); if ($json_encode) { /* JSON_UNESCAPED_SLASHES was added in PHP 5.4 */ if (version_compare(phpversion(), '5.4.0', '>=')) { $atts = json_encode($atts, JSON_UNESCAPED_SLASHES); } else { /* Use a fallback for < 5.4 */ $atts = str_replace('\\/', '/', json_encode($atts)); } } return $atts; }
/** * 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 }
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 }
/** * Validate the settings * @param array $input * @return array */ function code_snippets_settings_validate(array $input) { $settings = code_snippets_get_settings(); $settings_fields = code_snippets_get_settings_fields(); // Don't directly loop through $input as it does not include as deselected checkboxes foreach ($settings_fields as $section_id => $fields) { // Loop through fields foreach ($fields as $field) { $field_id = $field['id']; switch ($field['type']) { case 'checkbox': $settings[$section_id][$field_id] = isset($input[$section_id][$field_id]) && 'on' === $input[$section_id][$field_id]; break; case 'number': $settings[$section_id][$field_id] = absint($input[$section_id][$field_id]); break; default: $settings[$section_id][$field_id] = $input[$section_id][$field_id]; } } } /* Add an updated message */ add_settings_error('code-snippets-settings-notices', 'settings-saved', __('Settings saved.', 'code-snippets'), 'updated'); return $settings; }