1+ <?php
2+ /**
3+ * Initializer class to load admin section
4+ * Final class cannot be inherited anymore. We use them when creating new instances
5+ * @package ExpandableFAQ
6+ * @author KestutisIT
7+ * @copyright KestutisIT
8+ * @license MIT License. See Legal/License.txt for details.
9+ */
10+ namespace ExpandableFAQ \Controllers \Admin ;
11+ use ExpandableFAQ \Models \Configuration \ConfigurationInterface ;
12+ use ExpandableFAQ \Models \Language \LanguageInterface ;
13+ use ExpandableFAQ \Models \Node \LinkedNode ;
14+ use ExpandableFAQ \Models \Node \LinkedNodesObserver ;
15+ use ExpandableFAQ \Models \Node \LinkedNodesTable ;
16+
17+ final class AssetController
18+ {
19+ private $ conf = NULL ;
20+ private $ lang = NULL ;
21+ private static $ mandatoryPlainJSInitialized = FALSE ;
22+
23+ public function __construct (ConfigurationInterface &$ paramConf , LanguageInterface &$ paramLang )
24+ {
25+ // Set class settings
26+ $ this ->conf = $ paramConf ;
27+ // Already sanitized before in it's constructor. Too much sanitation will kill the system speed
28+ $ this ->lang = $ paramLang ;
29+ }
30+
31+ /**
32+ * We use this method, because WP_LOCALIZE_SCRIPT does not do the great job,
33+ * and even the 'l10n_print_after' param is a backward-compatible feature, that has issues of initializing first or second count
34+ * NOTE: About dynamic properties:
35+ * https://stackoverflow.com/questions/11040472/how-to-check-if-object-property-exists-with-a-variable-holding-the-property-name/30148756
36+ */
37+ public function enqueueMandatoryPlainJS ()
38+ {
39+ $ dataTablesRelPath = 'DataTables ' .DIRECTORY_SEPARATOR .'Plugins ' .DIRECTORY_SEPARATOR .'i18n ' .DIRECTORY_SEPARATOR ;
40+ $ dataTablesRelURL = 'DataTables/Plugins/i18n/ ' ;
41+ $ dataTablesLangFilename = $ this ->lang ->getPrint ('DATATABLES_LANG ' ).'.json ' ;
42+ if (is_readable ($ this ->conf ->getRouting ()->get3rdPartyAssetsPath ($ dataTablesRelPath .$ dataTablesLangFilename )) === FALSE )
43+ {
44+ $ dataTablesLangFilename = 'English.json ' ;
45+ }
46+
47+ $ extVars = array (
48+ 'DATATABLES_LANG_URL ' => $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ($ dataTablesRelURL .$ dataTablesLangFilename , TRUE ),
49+ );
50+ $ extLang = array (
51+ 'LANG_FAQ_DELETING_DIALOG_TEXT ' => $ this ->lang ->getPrint ('LANG_FAQ_DELETING_DIALOG_TEXT ' ),
52+ );
53+
54+ if (static ::$ mandatoryPlainJSInitialized === FALSE )
55+ {
56+ static ::$ mandatoryPlainJSInitialized = TRUE ;
57+ ?>
58+ <script type="text/javascript">var <?= $ this ->conf ->getPluginJS_ClassPrefix ();?> Vars;</script>
59+ <script type="text/javascript">var <?= $ this ->conf ->getPluginJS_ClassPrefix ();?> Lang;</script>
60+ <?php
61+ }
62+ ?>
63+ <script type="text/javascript"><?= $ this ->conf ->getPluginJS_ClassPrefix ();?> Vars = <?= json_encode ($ extVars , JSON_FORCE_OBJECT );?> ;</script>
64+ <script type="text/javascript"><?= $ this ->conf ->getPluginJS_ClassPrefix ();?> Lang = <?= json_encode ($ extLang , JSON_FORCE_OBJECT );?> ;</script>
65+ <?php
66+ }
67+
68+ public function registerScripts ()
69+ {
70+ if (defined ('SCRIPT_DEBUG ' ) && SCRIPT_DEBUG )
71+ {
72+ // Debug scripts
73+
74+ // 1. Datatables with Responsive support
75+ wp_register_script ('datatables-jquery-datatables ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/js/jquery.dataTables.js ' ));
76+ wp_register_script ('datatables-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/js/dataTables.jqueryui.js ' ));
77+ wp_register_script ('datatables-responsive-datatables ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/js/dataTables.responsive.js ' ));
78+ wp_register_script ('datatables-responsive-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/js/responsive.jqueryui.js ' ));
79+ } else
80+ {
81+ // Regular scripts
82+
83+ // 1. Datatables with Responsive support
84+ wp_register_script ('datatables-jquery-datatables ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/js/jquery.dataTables.min.js ' ));
85+ wp_register_script ('datatables-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/js/dataTables.jqueryui.min.js ' ));
86+ wp_register_script ('datatables-responsive-datatables ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/js/dataTables.responsive.min.js ' ));
87+ wp_register_script ('datatables-responsive-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/js/responsive.jqueryui.min.js ' ));
88+ }
89+
90+ // 2. jQuery validate
91+ wp_register_script ('jquery-validate ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('jquery-validation/jquery.validate.js ' ));
92+
93+ // 3. NS Admin script
94+ $ filename = $ this ->conf ->getPluginJS_ClassPrefix ().'Admin.js ' ;
95+ wp_register_script ($ this ->conf ->getPluginHandlePrefix ().'admin ' , $ this ->conf ->getRouting ()->getAdminJS_URL ($ filename ), array (), '1.0 ' , TRUE );
96+ }
97+
98+ public function registerStyles ()
99+ {
100+ // Register 3rd party styles for further use (register even it the file is '' - WordPress will process that as needed)
101+ if (defined ('SCRIPT_DEBUG ' ) && SCRIPT_DEBUG )
102+ {
103+ // Debug style
104+
105+ // 1. Font-Awesome styles
106+ wp_register_style ('font-awesome ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('font-awesome/css/font-awesome.css ' ));
107+
108+ // 2. Modern tabs styles
109+ wp_register_style ('modern-tabs ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('ModernTabs/ModernTabs.css ' ));
110+
111+ // 3. jQuery UI theme (currently used for DataTables)
112+ wp_register_style ('jquery-ui-theme ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('jquery-ui/themes/custom/jquery-ui.css ' ));
113+
114+ // 4. Datatables with Responsive support
115+ wp_register_style ('datatables-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/css/dataTables.jqueryui.css ' ));
116+ wp_register_style ('datatables-responsive-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/css/responsive.jqueryui.css ' ));
117+ } else
118+ {
119+ // Regular style
120+
121+ // 1. Font-Awesome styles
122+ wp_register_style ('font-awesome ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('font-awesome/css/font-awesome.min.css ' ));
123+
124+ // 2. Modern tabs styles
125+ wp_register_style ('modern-tabs ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('ModernTabs/ModernTabs.css ' ));
126+
127+ // 3. jQuery UI theme (currently used for DataTables)
128+ wp_register_style ('jquery-ui-theme ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('jquery-ui/themes/custom/jquery-ui.min.css ' ));
129+
130+ // 4. Datatables with Responsive support
131+ wp_register_style ('datatables-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/css/dataTables.jqueryui.min.css ' ));
132+ wp_register_style ('datatables-responsive-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/css/responsive.jqueryui.min.css ' ));
133+ }
134+
135+ // 5. jQuery Validate
136+ wp_register_style ('jquery-validate ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('jquery-validation/jquery.validate.css ' ));
137+
138+ // 6. Plugin style
139+ wp_register_style ($ this ->conf ->getPluginURL_Prefix ().'admin ' , $ this ->conf ->getRouting ()->getAdminCSS_URL ('Admin.css ' ));
140+ }
141+ }
0 commit comments