| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file handles the editor preview setting |
| 4 |
* |
| 5 |
* @since 2.0.0 |
| 6 |
* @package Code_Snippets |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Code_Snippets\Settings; |
| 10 |
|
| 11 |
use function Code_Snippets\Utils\get_editor_themes; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class for handling the editor preview setting. |
| 15 |
* |
| 16 |
* @package Code_Snippets\Settings |
| 17 |
*/ |
| 18 |
class Editor_Preview { |
| 19 |
|
| 20 |
/** |
| 21 |
* Retrieve the list of code editor themes. |
| 22 |
* |
| 23 |
* @return array<string, string> List of editor themes. |
| 24 |
*/ |
| 25 |
public static function get_editor_theme_list(): array { |
| 26 |
$themes = [ |
| 27 |
'default' => __( 'Default', 'code-snippets' ), |
| 28 |
]; |
| 29 |
|
| 30 |
foreach ( get_editor_themes() as $theme ) { |
| 31 |
if ( '-mobile' === substr( $theme, -7 ) ) { |
| 32 |
continue; |
| 33 |
} |
| 34 |
|
| 35 |
$themes[ $theme ] = ucwords( str_replace( '-', ' ', $theme ) ); |
| 36 |
} |
| 37 |
|
| 38 |
return $themes; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Render the editor preview setting. |
| 43 |
*/ |
| 44 |
public function render() { |
| 45 |
$settings = get_settings_values(); |
| 46 |
$settings = $settings['editor']; |
| 47 |
|
| 48 |
$indent_unit = absint( $settings['indent_unit'] ); |
| 49 |
$tab_size = absint( $settings['tab_size'] ); |
| 50 |
|
| 51 |
$n_tabs = $settings['indent_with_tabs'] ? floor( $indent_unit / $tab_size ) : 0; |
| 52 |
$n_spaces = $settings['indent_with_tabs'] ? $indent_unit % $tab_size : $indent_unit; |
| 53 |
|
| 54 |
$indent = str_repeat( "\t", $n_tabs ) . str_repeat( ' ', $n_spaces ); |
| 55 |
|
| 56 |
$code = "add_filter( 'admin_footer_text', function ( \$text ) {\n\n" . |
| 57 |
$indent . "\$site_name = get_bloginfo( 'name' );\n\n" . |
| 58 |
$indent . '$text = "Thank you for visiting $site_name.";' . "\n" . |
| 59 |
$indent . 'return $text;' . "\n" . |
| 60 |
"} );\n"; |
| 61 |
|
| 62 |
echo '<textarea id="code_snippets_editor_preview">', esc_textarea( $code ), '</textarea>'; |
| 63 |
} |
| 64 |
} |
| 65 |
|