Skip to content

Commit b51a2b3

Browse files
committed
refactor: move settings boot to core config
1 parent e69f43d commit b51a2b3

10 files changed

Lines changed: 699 additions & 47 deletions

config/settings.php

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
<?php
2+
/**
3+
* Settings page schema.
4+
*
5+
* @package SrbTransLatin
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
defined( 'ABSPATH' ) || exit;
11+
12+
$locale = \get_locale();
13+
$disable_permalinks = \in_array( $locale, array( 'sr_RS', 'bs_BA' ), true );
14+
$navigation_menus = \get_registered_nav_menus();
15+
$has_navigation_menus = 0 < \count( $navigation_menus );
16+
17+
return array(
18+
'page' => array(
19+
'option_name' => \STL\Common\Settings\Settings_Schema::OPTION_NAME,
20+
'slug' => \STL\Common\Settings\Settings_Schema::PAGE_SLUG,
21+
'page_title' => \__( 'Latinisation', 'srbtranslatin' ),
22+
'menu_title' => \__( 'Settings', 'default' ),
23+
'parent_slug' => 'options-general.php',
24+
'capability' => 'manage_options',
25+
),
26+
'tabs' => array(
27+
array(
28+
'id' => 'general',
29+
'title' => \__( 'General', 'srbtranslatin' ),
30+
'icon' => 'dashicons-admin-generic',
31+
),
32+
array(
33+
'id' => 'menu',
34+
'title' => \__( 'Menus', 'srbtranslatin' ),
35+
'icon' => 'dashicons-menu',
36+
),
37+
array(
38+
'id' => 'media',
39+
'title' => \__( 'Media', 'srbtranslatin' ),
40+
'icon' => 'dashicons-format-image',
41+
),
42+
array(
43+
'id' => 'advanced',
44+
'title' => \__( 'Advanced', 'default' ),
45+
'icon' => 'dashicons-admin-tools',
46+
),
47+
),
48+
'sections' => array(
49+
array(
50+
'id' => 'general',
51+
'title' => \_x( 'General settings', 'section name', 'srbtranslatin' ),
52+
'description' => \__( 'General settings control main functionality of the plugin', 'srbtranslatin' ),
53+
'tab' => 'general',
54+
),
55+
array(
56+
'id' => 'menu',
57+
'title' => \_x( 'Navigation menu settings', 'section name', 'srbtranslatin' ),
58+
'description' => \__( 'Menu settings control extending and tweaking the script selector in theme menus', 'srbtranslatin' ),
59+
'tab' => 'menu',
60+
),
61+
array(
62+
'id' => 'media',
63+
'title' => \_x( 'File and Media settings', 'section name', 'srbtranslatin' ),
64+
'description' => \__( 'File and media settings control filename transliteration and media saving', 'srbtranslatin' ),
65+
'tab' => 'media',
66+
),
67+
array(
68+
'id' => 'advanced',
69+
'title' => \_x( 'Advanced settings', 'section name', 'srbtranslatin' ),
70+
'description' => \__( 'Advanced settings control permalink and search settings', 'srbtranslatin' ),
71+
'tab' => 'advanced',
72+
),
73+
),
74+
'fields' => array(
75+
array(
76+
'id' => 'enabled_scripts',
77+
'type' => 'buttons_group',
78+
'title' => \__( 'Enabled scripts', 'srbtranslatin' ),
79+
'section' => 'general',
80+
'extras' => array(
81+
'default' => 'both',
82+
'description' => \__( 'Cyrillic and Latin', 'srbtranslatin' ),
83+
'options' => array(
84+
'cir' => 'Ћ ' . \__( 'Cyrillic', 'srbtranslatin' ),
85+
'lat' => 'Ć ' . \__( 'Latin', 'srbtranslatin' ),
86+
'both' => 'Ć Ћ ' . \__( 'Both', 'srbtranslatin' ),
87+
),
88+
),
89+
),
90+
array(
91+
'id' => 'default_script',
92+
'type' => 'select',
93+
'title' => \__( 'Default script', 'srbtranslatin' ),
94+
'section' => 'general',
95+
'extras' => array(
96+
'default' => 'cir',
97+
'description' => \__( 'Default script used for the website if user did not select a script', 'srbtranslatin' ),
98+
'options' => array(
99+
'cir' => \__( 'Cyrillic', 'srbtranslatin' ),
100+
'lat' => \__( 'Latin', 'srbtranslatin' ),
101+
),
102+
'conditions' => array(
103+
'rules' => array(
104+
array(
105+
'field' => 'enabled_scripts',
106+
'operator' => '=',
107+
'value' => 'both',
108+
),
109+
),
110+
),
111+
),
112+
),
113+
array(
114+
'id' => 'url_param',
115+
'type' => 'text',
116+
'title' => \__( 'URL Parameter', 'srbtranslatin' ),
117+
'section' => 'general',
118+
'extras' => array(
119+
'default' => 'pismo',
120+
'description' => \__( 'URL parameter used for script selector', 'srbtranslatin' ),
121+
),
122+
),
123+
array(
124+
'id' => 'menu_warning',
125+
'type' => 'description',
126+
'title' => '',
127+
'section' => 'menu',
128+
'extras' => array(
129+
'description' => ! $has_navigation_menus
130+
? '<strong>' . \__( 'Options in this section are disabled because you do not have any navigation menus registered', 'srbtranslatin' ) . '</strong>'
131+
: '',
132+
'html_attributes' => array(
133+
'class' => 'notice inline notice-warning',
134+
),
135+
),
136+
),
137+
array(
138+
'id' => 'extend',
139+
'type' => 'checkbox',
140+
'title' => \__( 'Extend navigation menu', 'srbtranslatin' ),
141+
'section' => 'menu',
142+
'extras' => array(
143+
'default' => true,
144+
'description' => \__( 'Adds a script selector to the navigation menu', 'srbtranslatin' ),
145+
'html_attributes' => array(
146+
'disabled' => ! $has_navigation_menus,
147+
),
148+
),
149+
),
150+
array(
151+
'id' => 'extend_menu',
152+
'type' => 'select',
153+
'title' => \__( 'Navigation menu to extend', 'srbtranslatin' ),
154+
'section' => 'menu',
155+
'extras' => array(
156+
'default' => '',
157+
'description' => \__( 'Select the navigation menu you want to extend', 'srbtranslatin' ),
158+
'options' => \array_merge(
159+
array( '' => \__( 'Select a menu', 'srbtranslatin' ) ),
160+
$navigation_menus,
161+
),
162+
'html_attributes' => array(
163+
'disabled' => ! $has_navigation_menus,
164+
),
165+
),
166+
),
167+
array(
168+
'id' => 'selector_type',
169+
'type' => 'select',
170+
'title' => \__( 'Selector type', 'srbtranslatin' ),
171+
'section' => 'menu',
172+
'extras' => array(
173+
'default' => 'submenu',
174+
'description' => \__( 'Choose the type of the script selector', 'srbtranslatin' ),
175+
'options' => array(
176+
'submenu' => \__( 'Submenu', 'srbtranslatin' ),
177+
'inline' => \__( 'Inline', 'srbtranslatin' ),
178+
),
179+
'html_attributes' => array(
180+
'disabled' => ! $has_navigation_menus,
181+
),
182+
),
183+
),
184+
array(
185+
'id' => 'menu_title',
186+
'type' => 'text',
187+
'title' => \__( 'Menu item title', 'srbtranslatin' ),
188+
'section' => 'menu',
189+
'extras' => array(
190+
'default' => \__( 'Script', 'srbtranslatin' ),
191+
'description' => \__( 'Title of the menu item', 'srbtranslatin' ),
192+
'html_attributes' => array(
193+
'disabled' => ! $has_navigation_menus,
194+
),
195+
),
196+
),
197+
array(
198+
'id' => 'transliterate_uploads',
199+
'type' => 'checkbox',
200+
'title' => \__( 'Transliterate uploads', 'srbtranslatin' ),
201+
'section' => 'media',
202+
'extras' => array(
203+
'default' => true,
204+
'description' => \__( 'Transliterate filenames on upload', 'srbtranslatin' ),
205+
),
206+
),
207+
array(
208+
'id' => 'separate_uploads',
209+
'type' => 'checkbox',
210+
'title' => \__( 'Script specific filenames', 'srbtranslatin' ),
211+
'section' => 'media',
212+
'extras' => array(
213+
'default' => true,
214+
'description' => \__( 'Check this box if you want to have separate filenames for each script', 'srbtranslatin' ),
215+
),
216+
),
217+
array(
218+
'id' => 'filename_separator',
219+
'type' => 'text',
220+
'title' => \__( 'Filename separator', 'srbtranslatin' ),
221+
'section' => 'media',
222+
'extras' => array(
223+
'default' => '-',
224+
'description' => \__( 'Separator used for script specific filenames', 'srbtranslatin' ),
225+
'html_attributes' => array(
226+
'class' => 'small-text',
227+
),
228+
),
229+
),
230+
array(
231+
'id' => 'transliteration_method',
232+
'type' => 'select',
233+
'title' => \__( 'Transliteration method', 'srbtranslatin' ),
234+
'section' => 'media',
235+
'extras' => array(
236+
'default' => 'website',
237+
'description' => \__( 'Choose if you want to limit the script specific filenames on the entire website, or in content only', 'srbtranslatin' ),
238+
'options' => array(
239+
'website' => \__( 'Entire website', 'srbtranslatin' ),
240+
'content' => \__( 'Content only', 'srbtranslatin' ),
241+
),
242+
),
243+
),
244+
array(
245+
'id' => 'fix_permalinks',
246+
'type' => 'checkbox',
247+
'title' => \__( 'Fix Permalinks', 'srbtranslatin' ),
248+
'section' => 'advanced',
249+
'extras' => array(
250+
'default' => false,
251+
'description' => $disable_permalinks
252+
? \sprintf(
253+
\__( 'Fixes permalinks for cyrillic scripts. This option is currently disabled because your current locale is set to %s which will automatically change permalinks.', 'srbtranslatin' ),
254+
$locale,
255+
)
256+
: \__( 'Fixes permalinks for cyrillic scripts', 'srbtranslatin' ),
257+
'html_attributes' => array(
258+
'disabled' => $disable_permalinks,
259+
),
260+
),
261+
),
262+
array(
263+
'id' => 'fix_search',
264+
'type' => 'checkbox',
265+
'title' => \__( 'Fix Search', 'srbtranslatin' ),
266+
'section' => 'advanced',
267+
'extras' => array(
268+
'default' => true,
269+
'description' => \__( 'Enables searching cyrillic content via latin script', 'srbtranslatin' ),
270+
),
271+
),
272+
array(
273+
'id' => 'fix_ajax',
274+
'type' => 'checkbox',
275+
'title' => \__( 'Fix Ajax', 'srbtranslatin' ),
276+
'section' => 'advanced',
277+
'extras' => array(
278+
'default' => false,
279+
'description' => \__( 'Transliterates ajax calls', 'srbtranslatin' ),
280+
),
281+
),
282+
array(
283+
'id' => 'fix_titles',
284+
'type' => 'checkbox',
285+
'title' => \__( 'Fix Titles', 'srbtranslatin' ),
286+
'section' => 'advanced',
287+
'extras' => array(
288+
'default' => false,
289+
'description' => \__( 'Fixes titles for cyrillic scripts', 'srbtranslatin' ),
290+
),
291+
),
292+
),
293+
);

src/App.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
hook: 'plugins_loaded',
2020
priority: 50,
2121
imports: array(
22+
Core\Core_Module::class,
2223
Admin\Admin_Module::class,
2324
Install\Install_Module::class,
2425
Translit\Translit_Module::class,

src/Modules/Admin/Admin_Module.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22

33
namespace STL\Admin;
44

5-
use STL\Admin\Services\Settings_Page_Registrar;
6-
use STL\Admin\Settings\Admin_Context;
7-
use STL\Admin\Settings\Definitions\Advanced_Tab_Definition;
8-
use STL\Admin\Settings\Definitions\General_Tab_Definition;
9-
use STL\Admin\Settings\Definitions\Media_Tab_Definition;
10-
use STL\Admin\Settings\Definitions\Menu_Tab_Definition;
11-
use STL\Admin\Settings\Settings_Page_Definition;
12-
use STL\Admin\Settings\WordPress_Admin_Context;
13-
use WPTechnix\WP_Settings_Builder\Settings_Builder;
14-
use XWP\DI\Decorators\Action;
155
use XWP\DI\Decorators\Module;
166

177
/**
@@ -22,41 +12,4 @@ class Admin_Module {
2212
public static function can_initialize(): bool {
2313
return \is_admin();
2414
}
25-
26-
/**
27-
* @return array<string,mixed>
28-
*/
29-
public static function configure(): array {
30-
return array(
31-
Admin_Context::class => \DI\autowire( WordPress_Admin_Context::class ),
32-
Advanced_Tab_Definition::class => \DI\autowire(),
33-
General_Tab_Definition::class => \DI\autowire(),
34-
Media_Tab_Definition::class => \DI\autowire(),
35-
Menu_Tab_Definition::class => \DI\autowire(),
36-
Settings_Builder::class => \DI\factory(
37-
static fn(): Settings_Builder => new Settings_Builder(),
38-
),
39-
Settings_Page_Definition::class => \DI\factory(
40-
static fn(
41-
General_Tab_Definition $general,
42-
Menu_Tab_Definition $menu,
43-
Media_Tab_Definition $media,
44-
Advanced_Tab_Definition $advanced,
45-
): Settings_Page_Definition => new Settings_Page_Definition(
46-
array( $general, $menu, $media, $advanced ),
47-
),
48-
),
49-
Settings_Page_Registrar::class => \DI\autowire(),
50-
);
51-
}
52-
53-
/**
54-
* Registers the settings page.
55-
*
56-
* @param Settings_Page_Registrar $registrar Settings page registrar instance. Injected by the container.
57-
*/
58-
#[Action( tag: 'wp_loaded', priority: 100, invoke: Action::INV_PROXIED, args: 0 )]
59-
public function register_settings( Settings_Page_Registrar $registrar ): void {
60-
$registrar->register();
61-
}
6215
}

src/Modules/Core/Core_Module.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,27 @@
22

33
namespace STL\Core;
44

5+
use WPTechnix\WP_Settings_Builder\Settings_Builder;
6+
use XWP\DI\Decorators\Action;
57
use XWP\DI\Decorators\Module;
68

79
#[Module( container: 'stl', hook: 'srbtranslatin_loaded', priority: 0, imports: array(), handlers: array() )]
810
class Core_Module {
11+
/**
12+
* @return array<string,mixed>
13+
*/
14+
public static function configure(): array {
15+
return array(
16+
Settings_Builder::class => \DI\factory(
17+
static fn(): Settings_Builder => new Settings_Builder(),
18+
),
19+
Settings_Page_Config::class => \DI\autowire( File_Settings_Page_Config::class ),
20+
Settings_Page_Handler::class => \DI\autowire(),
21+
);
22+
}
23+
24+
#[Action( tag: 'init', priority: 0, invoke: Action::INV_PROXIED, args: 0 )]
25+
public function register_settings_page( Settings_Page_Handler $handler ): void {
26+
$handler->boot();
27+
}
928
}

0 commit comments

Comments
 (0)