| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Settings; |
| 4 |
|
| 5 |
use function Code_Snippets\code_snippets; |
| 6 |
|
| 7 |
/** |
| 8 |
* Plugin settings fields definitions. |
| 9 |
* |
| 10 |
* This class defines the settings fields and their default values for the Code Snippets plugin. |
| 11 |
* It provides methods to retrieve the settings fields and default values. |
| 12 |
* |
| 13 |
* @package Code_Snippets\Settings |
| 14 |
*/ |
| 15 |
class Settings_Fields { |
| 16 |
|
| 17 |
/** |
| 18 |
* Instance of this class. |
| 19 |
* |
| 20 |
* @var Settings_Fields |
| 21 |
*/ |
| 22 |
private static Settings_Fields $instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* The settings fields definitions. |
| 26 |
* |
| 27 |
* @var array<string, array<string, array>> |
| 28 |
*/ |
| 29 |
private array $fields; |
| 30 |
|
| 31 |
/** |
| 32 |
* The default settings values. |
| 33 |
* |
| 34 |
* @var array<string, array<string, mixed>> |
| 35 |
*/ |
| 36 |
private array $defaults; |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor. |
| 40 |
* |
| 41 |
* Initializes the settings fields and default values. |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
$this->init_fields(); |
| 45 |
$this->init_defaults(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Retrieve the instance of this class. |
| 50 |
* |
| 51 |
* @return Settings_Fields |
| 52 |
*/ |
| 53 |
private static function get_instance(): Settings_Fields { |
| 54 |
if ( ! isset( self::$instance ) ) { |
| 55 |
self::$instance = new self(); |
| 56 |
} |
| 57 |
|
| 58 |
return self::$instance; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Retrieve the default setting values |
| 63 |
* |
| 64 |
* @return array<string, array<string, mixed>> |
| 65 |
*/ |
| 66 |
public static function get_default_values(): array { |
| 67 |
return self::get_instance()->defaults; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Retrieve the settings fields. |
| 72 |
* |
| 73 |
* @return array<string, array<string, array>> |
| 74 |
*/ |
| 75 |
public static function get_field_definitions(): array { |
| 76 |
return self::get_instance()->fields; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Initialise default settings values. |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
private function init_defaults() { |
| 85 |
$this->defaults = [ |
| 86 |
'general' => [ |
| 87 |
'activate_by_default' => true, |
| 88 |
'enable_tags' => true, |
| 89 |
'enable_description' => true, |
| 90 |
'visual_editor_rows' => 5, |
| 91 |
'list_order' => 'priority-asc', |
| 92 |
'disable_prism' => false, |
| 93 |
'hide_upgrade_menu' => false, |
| 94 |
'complete_uninstall' => false, |
| 95 |
'enable_flat_files' => false, |
| 96 |
'enable_admin_bar' => true, |
| 97 |
'admin_bar_snippet_limit' => 20, |
| 98 |
], |
| 99 |
'editor' => [ |
| 100 |
'indent_with_tabs' => true, |
| 101 |
'tab_size' => 4, |
| 102 |
'indent_unit' => 4, |
| 103 |
'font_size' => 14, |
| 104 |
'wrap_lines' => true, |
| 105 |
'code_folding' => true, |
| 106 |
'line_numbers' => true, |
| 107 |
'auto_close_brackets' => true, |
| 108 |
'highlight_selection_matches' => true, |
| 109 |
'highlight_active_line' => true, |
| 110 |
'keymap' => 'default', |
| 111 |
'theme' => 'default', |
| 112 |
], |
| 113 |
'version-switch' => [ |
| 114 |
'selected_version' => '', |
| 115 |
], |
| 116 |
'debug' => [ |
| 117 |
'enable_version_change' => false, |
| 118 |
], |
| 119 |
]; |
| 120 |
|
| 121 |
$this->defaults = apply_filters( 'code_snippets_settings_defaults', $this->defaults ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Initialise the settings fields values. |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
private function init_fields() { |
| 130 |
$this->fields = []; |
| 131 |
|
| 132 |
$this->fields['debug'] = [ |
| 133 |
'database_update' => [ |
| 134 |
'name' => __( 'Database Table Upgrade', 'code-snippets' ), |
| 135 |
'type' => 'action', |
| 136 |
'label' => __( 'Upgrade Database Table', 'code-snippets' ), |
| 137 |
'desc' => __( 'Use this button to manually upgrade the Code Snippets database table. This action will only affect the snippets table and should be used only when necessary.', 'code-snippets' ), |
| 138 |
], |
| 139 |
'reset_caches' => [ |
| 140 |
'name' => __( 'Reset Caches', 'code-snippets' ), |
| 141 |
'type' => 'action', |
| 142 |
'desc' => __( 'Use this button to manually clear snippets caches.', 'code-snippets' ), |
| 143 |
], |
| 144 |
'enable_version_change' => [ |
| 145 |
'name' => __( 'Version Change', 'code-snippets' ), |
| 146 |
'type' => 'checkbox', |
| 147 |
'label' => __( 'Enable the ability to switch or rollback versions of the Code Snippets core plugin.', 'code-snippets' ), |
| 148 |
], |
| 149 |
]; |
| 150 |
|
| 151 |
$this->fields['version-switch'] = [ |
| 152 |
'version_switcher' => [ |
| 153 |
'name' => __( 'Switch Version', 'code-snippets' ), |
| 154 |
'type' => 'callback', |
| 155 |
'render_callback' => [ '\\Code_Snippets\\Settings\\Version_Switch', 'render_version_switch_field' ], |
| 156 |
], |
| 157 |
'refresh_versions' => [ |
| 158 |
'name' => __( 'Refresh Versions', 'code-snippets' ), |
| 159 |
'type' => 'callback', |
| 160 |
'render_callback' => [ '\\Code_Snippets\\Settings\\Version_Switch', 'render_refresh_versions_field' ], |
| 161 |
], |
| 162 |
'version_warning' => [ |
| 163 |
'name' => '', |
| 164 |
'type' => 'callback', |
| 165 |
'render_callback' => [ '\\Code_Snippets\\Settings\\Version_Switch', 'render_version_switch_warning' ], |
| 166 |
], |
| 167 |
]; |
| 168 |
|
| 169 |
$this->fields['general'] = [ |
| 170 |
'activate_by_default' => [ |
| 171 |
'name' => __( 'Activate by Default', 'code-snippets' ), |
| 172 |
'type' => 'checkbox', |
| 173 |
'label' => __( "Make the 'Save and Activate' button the default action when saving a snippet.", 'code-snippets' ), |
| 174 |
], |
| 175 |
'enable_tags' => [ |
| 176 |
'name' => __( 'Enable Snippet Tags', 'code-snippets' ), |
| 177 |
'type' => 'checkbox', |
| 178 |
'label' => __( 'Show snippet tags on admin pages.', 'code-snippets' ), |
| 179 |
], |
| 180 |
'enable_description' => [ |
| 181 |
'name' => __( 'Enable Snippet Descriptions', 'code-snippets' ), |
| 182 |
'type' => 'checkbox', |
| 183 |
'label' => __( 'Show snippet descriptions on admin pages.', 'code-snippets' ), |
| 184 |
], |
| 185 |
'visual_editor_rows' => [ |
| 186 |
'name' => __( 'Description Editor Height', 'code-snippets' ), |
| 187 |
'type' => 'number', |
| 188 |
'label' => _x( 'rows', 'unit', 'code-snippets' ), |
| 189 |
'min' => 0, |
| 190 |
], |
| 191 |
'list_order' => [ |
| 192 |
'name' => __( 'Snippets List Order', 'code-snippets' ), |
| 193 |
'type' => 'select', |
| 194 |
'desc' => __( 'Default way to order snippets on the All Snippets admin menu.', 'code-snippets' ), |
| 195 |
'options' => [ |
| 196 |
'priority-asc' => __( 'Priority', 'code-snippets' ), |
| 197 |
'name-asc' => __( 'Name (A-Z)', 'code-snippets' ), |
| 198 |
'name-desc' => __( 'Name (Z-A)', 'code-snippets' ), |
| 199 |
'modified-desc' => __( 'Modified (latest first)', 'code-snippets' ), |
| 200 |
'modified-asc' => __( 'Modified (oldest first)', 'code-snippets' ), |
| 201 |
], |
| 202 |
], |
| 203 |
'disable_prism' => [ |
| 204 |
'name' => __( 'Disable Syntax Highlighter', 'code-snippets' ), |
| 205 |
'type' => 'checkbox', |
| 206 |
'label' => __( 'Disable syntax highlighting when displaying snippet code on the front-end.', 'code-snippets' ), |
| 207 |
], |
| 208 |
]; |
| 209 |
|
| 210 |
if ( ! code_snippets()->licensing->is_licensed() ) { |
| 211 |
$this->fields['general']['hide_upgrade_menu'] = [ |
| 212 |
'name' => __( 'Hide Upgrade Notices', 'code-snippets' ), |
| 213 |
'type' => 'checkbox', |
| 214 |
'label' => __( 'Hide notices inviting you to upgrade to Code Snippets Pro.', 'code-snippets' ), |
| 215 |
]; |
| 216 |
} |
| 217 |
|
| 218 |
if ( ! is_multisite() || is_main_site() ) { |
| 219 |
$this->fields['general']['complete_uninstall'] = [ |
| 220 |
'name' => __( 'Complete Uninstall', 'code-snippets' ), |
| 221 |
'type' => 'checkbox', |
| 222 |
'label' => __( 'When the plugin is deleted from the Plugins menu, also delete all snippets and plugin settings.', 'code-snippets' ), |
| 223 |
]; |
| 224 |
} |
| 225 |
|
| 226 |
$this->fields['general']['enable_admin_bar'] = [ |
| 227 |
'name' => __( 'Enable Admin Bar Menu', 'code-snippets' ), |
| 228 |
'type' => 'checkbox', |
| 229 |
'label' => __( 'Show a Snippets menu in the admin bar for quick access to snippets.', 'code-snippets' ), |
| 230 |
]; |
| 231 |
|
| 232 |
$this->fields['general']['admin_bar_snippet_limit'] = [ |
| 233 |
'name' => __( 'Admin Bar Snippets Per Page', 'code-snippets' ), |
| 234 |
'type' => 'number', |
| 235 |
'desc' => __( 'Number of snippets to show in the admin bar Active/Inactive menus before paginating.', 'code-snippets' ), |
| 236 |
'label' => __( 'snippets', 'code-snippets' ), |
| 237 |
'min' => 1, |
| 238 |
'max' => 100, |
| 239 |
'show_if' => [ |
| 240 |
'section' => 'general', |
| 241 |
'field' => 'enable_admin_bar', |
| 242 |
'value' => true, |
| 243 |
], |
| 244 |
]; |
| 245 |
|
| 246 |
$this->fields['editor'] = [ |
| 247 |
'indent_with_tabs' => [ |
| 248 |
'name' => __( 'Indent With Tabs', 'code-snippets' ), |
| 249 |
'type' => 'checkbox', |
| 250 |
'label' => __( 'Use hard tabs instead of spaces for indentation.', 'code-snippets' ), |
| 251 |
'codemirror' => 'indentWithTabs', |
| 252 |
], |
| 253 |
'tab_size' => [ |
| 254 |
'name' => __( 'Tab Size', 'code-snippets' ), |
| 255 |
'type' => 'number', |
| 256 |
'desc' => __( 'The width of a tab character.', 'code-snippets' ), |
| 257 |
'label' => _x( 'spaces', 'unit', 'code-snippets' ), |
| 258 |
'codemirror' => 'tabSize', |
| 259 |
'min' => 0, |
| 260 |
], |
| 261 |
'indent_unit' => [ |
| 262 |
'name' => __( 'Indent Unit', 'code-snippets' ), |
| 263 |
'type' => 'number', |
| 264 |
'desc' => __( 'The number of spaces to indent a block.', 'code-snippets' ), |
| 265 |
'label' => _x( 'spaces', 'unit', 'code-snippets' ), |
| 266 |
'codemirror' => 'indentUnit', |
| 267 |
'min' => 0, |
| 268 |
], |
| 269 |
'font_size' => [ |
| 270 |
'name' => __( 'Font Size', 'code-snippets' ), |
| 271 |
'type' => 'number', |
| 272 |
'label' => _x( 'px', 'unit', 'code-snippets' ), |
| 273 |
'codemirror' => 'fontSize', |
| 274 |
'min' => 8, |
| 275 |
'max' => 28, |
| 276 |
], |
| 277 |
'wrap_lines' => [ |
| 278 |
'name' => __( 'Wrap Lines', 'code-snippets' ), |
| 279 |
'type' => 'checkbox', |
| 280 |
'label' => __( 'Soft-wrap long lines of code instead of horizontally scrolling.', 'code-snippets' ), |
| 281 |
'codemirror' => 'lineWrapping', |
| 282 |
], |
| 283 |
'code_folding' => [ |
| 284 |
'name' => __( 'Code Folding', 'code-snippets' ), |
| 285 |
'type' => 'checkbox', |
| 286 |
'label' => __( 'Allow folding functions or other blocks into a single line.', 'code-snippets' ), |
| 287 |
'codemirror' => 'foldGutter', |
| 288 |
], |
| 289 |
'line_numbers' => [ |
| 290 |
'name' => __( 'Line Numbers', 'code-snippets' ), |
| 291 |
'type' => 'checkbox', |
| 292 |
'label' => __( 'Show line numbers to the left of the editor.', 'code-snippets' ), |
| 293 |
'codemirror' => 'lineNumbers', |
| 294 |
], |
| 295 |
'auto_close_brackets' => [ |
| 296 |
'name' => __( 'Auto Close Brackets', 'code-snippets' ), |
| 297 |
'type' => 'checkbox', |
| 298 |
'label' => __( 'Auto-close brackets and quotes when typed.', 'code-snippets' ), |
| 299 |
'codemirror' => 'autoCloseBrackets', |
| 300 |
], |
| 301 |
'highlight_selection_matches' => [ |
| 302 |
'name' => __( 'Highlight Selection Matches', 'code-snippets' ), |
| 303 |
'label' => __( 'Highlight all instances of a currently selected word.', 'code-snippets' ), |
| 304 |
'type' => 'checkbox', |
| 305 |
'codemirror' => 'highlightSelectionMatches', |
| 306 |
], |
| 307 |
'highlight_active_line' => [ |
| 308 |
'name' => __( 'Highlight Active Line', 'code-snippets' ), |
| 309 |
'label' => __( 'Highlight the line that is currently being edited.', 'code-snippets' ), |
| 310 |
'type' => 'checkbox', |
| 311 |
'codemirror' => 'styleActiveLine', |
| 312 |
], |
| 313 |
'keymap' => [ |
| 314 |
'name' => __( 'Keymap', 'code-snippets' ), |
| 315 |
'type' => 'select', |
| 316 |
'desc' => __( 'The set of keyboard shortcuts to use in the code editor.', 'code-snippets' ), |
| 317 |
'options' => [ |
| 318 |
'default' => __( 'Default', 'code-snippets' ), |
| 319 |
'vim' => __( 'Vim', 'code-snippets' ), |
| 320 |
'emacs' => __( 'Emacs', 'code-snippets' ), |
| 321 |
'sublime' => __( 'Sublime Text', 'code-snippets' ), |
| 322 |
], |
| 323 |
'codemirror' => 'keyMap', |
| 324 |
], |
| 325 |
'theme' => [ |
| 326 |
'name' => __( 'Theme', 'code-snippets' ), |
| 327 |
'type' => 'select', |
| 328 |
'options' => Editor_Preview::get_editor_theme_list(), |
| 329 |
'codemirror' => 'theme', |
| 330 |
], |
| 331 |
]; |
| 332 |
|
| 333 |
$this->fields = apply_filters( 'code_snippets_settings_fields', $this->fields ); |
| 334 |
} |
| 335 |
} |
| 336 |
|