| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Settings; |
| 4 |
|
| 5 |
/** |
| 6 |
* Groups settings fields into tabs for display. |
| 7 |
* |
| 8 |
* Storage is deliberately left alone: every value stays in the section it has |
| 9 |
* always lived in, so `get_setting( 'general', … )` keeps working and nobody's |
| 10 |
* saved settings are orphaned. This class only decides which tab a field is |
| 11 |
* drawn under, and in what order. |
| 12 |
* |
| 13 |
* The tabs are grouped by what someone came to the page to do, rather than by |
| 14 |
* which release the setting arrived in. General had grown large enough that |
| 15 |
* whole features were sitting in the same flat list as small preferences. |
| 16 |
* |
| 17 |
* The map below names every field across both editions. Anything not present |
| 18 |
* in the running installation is skipped, and a tab left with nothing in it does not |
| 19 |
* appear at all, so the free plugin shows fewer tabs than Pro without needing a |
| 20 |
* separate map. |
| 21 |
*/ |
| 22 |
class Settings_Layout { |
| 23 |
|
| 24 |
/** |
| 25 |
* Retrieve every tab, in display order, before availability is considered. |
| 26 |
* |
| 27 |
* @return array<string, string> Tab identifier keyed to its label. |
| 28 |
*/ |
| 29 |
public static function get_tabs(): array { |
| 30 |
$tabs = [ |
| 31 |
'editing' => __( 'Editing', 'code-snippets' ), |
| 32 |
'running' => __( 'Running', 'code-snippets' ), |
| 33 |
'insights' => __( 'Insights', 'code-snippets' ), |
| 34 |
'library' => __( 'Library', 'code-snippets' ), |
| 35 |
'interface' => __( 'Interface', 'code-snippets' ), |
| 36 |
'advanced' => __( 'Advanced', 'code-snippets' ), |
| 37 |
]; |
| 38 |
|
| 39 |
return apply_filters( 'code_snippets_settings_tabs', $tabs ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Retrieve the contents of every tab. |
| 44 |
* |
| 45 |
* Each entry is a `[ storage section, field identifier ]` pair. The storage |
| 46 |
* section is the one the value is saved under and must not change; only the |
| 47 |
* tab it appears in does. |
| 48 |
* |
| 49 |
* @return array<string, array<int, array{0: string, 1: string}>> |
| 50 |
*/ |
| 51 |
public static function get_tab_contents(): array { |
| 52 |
$contents = [ |
| 53 |
'editing' => [ |
| 54 |
[ 'general', 'enable_tags' ], |
| 55 |
[ 'general', 'enable_description' ], |
| 56 |
[ 'general', 'visual_editor_rows' ], |
| 57 |
[ 'editor', 'theme' ], |
| 58 |
[ 'editor', 'keymap' ], |
| 59 |
[ 'editor', 'font_size' ], |
| 60 |
[ 'editor', 'indent_with_tabs' ], |
| 61 |
[ 'editor', 'tab_size' ], |
| 62 |
[ 'editor', 'indent_unit' ], |
| 63 |
[ 'editor', 'line_numbers' ], |
| 64 |
[ 'editor', 'code_folding' ], |
| 65 |
[ 'editor', 'wrap_lines' ], |
| 66 |
[ 'editor', 'auto_close_brackets' ], |
| 67 |
[ 'editor', 'highlight_active_line' ], |
| 68 |
[ 'editor', 'highlight_selection_matches' ], |
| 69 |
], |
| 70 |
'running' => [ |
| 71 |
[ 'general', 'enable_flat_files' ], |
| 72 |
[ 'general', 'minify_output' ], |
| 73 |
[ 'general', 'activate_by_default' ], |
| 74 |
], |
| 75 |
'insights' => [ |
| 76 |
[ 'general', 'enable_performance_tracker' ], |
| 77 |
[ 'general', 'enable_security_scan' ], |
| 78 |
[ 'general', 'rescan_all_snippets' ], |
| 79 |
], |
| 80 |
'library' => [ |
| 81 |
[ 'general', 'max_revisions' ], |
| 82 |
[ 'general', 'preserve_on_delete' ], |
| 83 |
[ 'general', 'delete_revision_default' ], |
| 84 |
[ 'general', 'connect_cloud' ], |
| 85 |
], |
| 86 |
'interface' => [ |
| 87 |
[ 'general', 'list_order' ], |
| 88 |
[ 'general', 'disable_prism' ], |
| 89 |
[ 'general', 'enable_admin_bar' ], |
| 90 |
[ 'general', 'admin_bar_snippet_limit' ], |
| 91 |
[ 'general', 'hide_upgrade_menu' ], |
| 92 |
], |
| 93 |
'advanced' => [ |
| 94 |
[ 'permissions', 'role_capabilities' ], |
| 95 |
[ 'version-switch', 'version_switcher' ], |
| 96 |
[ 'version-switch', 'refresh_versions' ], |
| 97 |
[ 'version-switch', 'version_warning' ], |
| 98 |
[ 'debug', 'reset_caches' ], |
| 99 |
[ 'debug', 'database_update' ], |
| 100 |
[ 'general', 'complete_uninstall' ], |
| 101 |
[ 'general', 'enable_feedback_reporter' ], |
| 102 |
], |
| 103 |
]; |
| 104 |
|
| 105 |
return apply_filters( 'code_snippets_settings_tab_contents', $contents ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Retrieve the fields of a tab that exist and should be shown. |
| 110 |
* |
| 111 |
* @param string $tab_id Tab identifier. |
| 112 |
* @param array<string, array<string, mixed>>|null $settings Current setting values. |
| 113 |
* |
| 114 |
* @return array<int, array{0: string, 1: string}> Storage section and field pairs. |
| 115 |
*/ |
| 116 |
public static function get_visible_fields( string $tab_id, ?array $settings = null ): array { |
| 117 |
$contents = self::get_tab_contents(); |
| 118 |
|
| 119 |
if ( empty( $contents[ $tab_id ] ) ) { |
| 120 |
return []; |
| 121 |
} |
| 122 |
|
| 123 |
$definitions = Settings_Fields::get_field_definitions(); |
| 124 |
$settings = null === $settings ? get_settings_values() : $settings; |
| 125 |
$visible = []; |
| 126 |
|
| 127 |
foreach ( $contents[ $tab_id ] as $entry ) { |
| 128 |
list( $section_id, $field_id ) = $entry; |
| 129 |
|
| 130 |
// Fields belonging to the other edition simply are not there. |
| 131 |
if ( ! isset( $definitions[ $section_id ][ $field_id ] ) ) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! should_render_setting_field( $definitions[ $section_id ][ $field_id ], $settings ) ) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
$visible[] = $entry; |
| 140 |
} |
| 141 |
|
| 142 |
return $visible; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Retrieve the tabs that have something to show. |
| 147 |
* |
| 148 |
* @return array<string, string> Tab identifier keyed to its label. |
| 149 |
*/ |
| 150 |
public static function get_available_tabs(): array { |
| 151 |
$settings = get_settings_values(); |
| 152 |
|
| 153 |
return array_filter( |
| 154 |
self::get_tabs(), |
| 155 |
fn( $tab_id ) => self::get_visible_fields( $tab_id, $settings ), |
| 156 |
ARRAY_FILTER_USE_KEY |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Retrieve the headings that break a tab into labeled groups. |
| 162 |
* |
| 163 |
* Keyed by tab, then by the field the heading is drawn above. A heading |
| 164 |
* whose field is absent is skipped along with it. |
| 165 |
* |
| 166 |
* @return array<string, array<string, string>> |
| 167 |
*/ |
| 168 |
public static function get_group_headings(): array { |
| 169 |
return [ |
| 170 |
'editing' => [ |
| 171 |
'enable_tags' => __( 'Snippet fields', 'code-snippets' ), |
| 172 |
'theme' => __( 'Code editor', 'code-snippets' ), |
| 173 |
], |
| 174 |
'running' => [ |
| 175 |
'enable_flat_files' => __( 'Execution', 'code-snippets' ), |
| 176 |
], |
| 177 |
'insights' => [ |
| 178 |
'enable_performance_tracker' => __( 'Performance', 'code-snippets' ), |
| 179 |
'enable_security_scan' => __( 'Security', 'code-snippets' ), |
| 180 |
], |
| 181 |
'library' => [ |
| 182 |
'max_revisions' => __( 'Revisions', 'code-snippets' ), |
| 183 |
'connect_cloud' => __( 'Cloud', 'code-snippets' ), |
| 184 |
], |
| 185 |
'interface' => [ |
| 186 |
'list_order' => __( 'Snippets list', 'code-snippets' ), |
| 187 |
'enable_admin_bar' => __( 'Admin bar', 'code-snippets' ), |
| 188 |
'hide_upgrade_menu' => __( 'Notices', 'code-snippets' ), |
| 189 |
], |
| 190 |
'advanced' => [ |
| 191 |
'role_capabilities' => __( 'Access', 'code-snippets' ), |
| 192 |
'version_switcher' => __( 'Maintenance', 'code-snippets' ), |
| 193 |
'reset_caches' => __( 'Maintenance', 'code-snippets' ), |
| 194 |
], |
| 195 |
]; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Retrieve replacement descriptions for fields whose current wording names |
| 200 |
* the setting without saying what it is for. |
| 201 |
* |
| 202 |
* @return array<string, string> Field identifier keyed to its description. |
| 203 |
*/ |
| 204 |
public static function get_descriptions(): array { |
| 205 |
return [ |
| 206 |
'enable_flat_files' => __( 'Run snippets from files on disk instead of the database. Faster on most sites, and it lets you keep snippets in version control.', 'code-snippets' ), |
| 207 |
'minify_output' => __( 'Strip whitespace from CSS and JavaScript snippets before they reach the browser, so pages load faster.', 'code-snippets' ), |
| 208 |
'enable_performance_tracker' => __( 'Measure how long each snippet takes to run. Timings appear on the snippets list, so you can find the slow one without guessing.', 'code-snippets' ), |
| 209 |
'enable_security_scan' => __( 'Check PHP snippets for risky patterns. Anything worth a second look is flagged against the snippet on the list.', 'code-snippets' ), |
| 210 |
'max_revisions' => __( 'Saves a copy each time a snippet changes, so you can compare versions and roll back a bad edit.', 'code-snippets' ), |
| 211 |
'preserve_on_delete' => __( 'Keep the revision history after a snippet is deleted, so it can still be recovered.', 'code-snippets' ), |
| 212 |
'enable_admin_bar' => __( 'Jump to any snippet from anywhere in the admin, and see which ones ran on the page you are looking at.', 'code-snippets' ), |
| 213 |
'reset_caches' => __( 'Clears stored snippet data. Worth doing before switching to an older version of the plugin.', 'code-snippets' ), |
| 214 |
]; |
| 215 |
} |
| 216 |
} |
| 217 |
|