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 Deals
6+ * @author KestutisIT
7+ * @copyright KestutisIT
8+ * @license MIT License. See Legal/License.txt for details.
9+ */
10+ namespace Deals \Controllers \Admin ;
11+ use Deals \Models \Configuration \ConfigurationInterface ;
12+ use Deals \Models \Language \LanguageInterface ;
13+ use Deals \Models \Node \LinkedNode ;
14+ use Deals \Models \Node \LinkedNodesObserver ;
15+ use Deals \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 sanitization 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 ->getText ('DATATABLES_LANG ' ).'.json ' ;
42+ if (is_readable ($ this ->conf ->getRouting ()->get3rdPartyAssetsPath ($ dataTablesRelPath .$ dataTablesLangFilename )) === FALSE )
43+ {
44+ $ dataTablesLangFilename = 'English.json ' ;
45+ }
46+
47+ $ pluginVars = array (
48+ // NOTE: As this is a JS context, we should use 'esc_js' instead of 'esc_url' even for URL JS var,
49+ // See for more information: https://wordpress.stackexchange.com/a/13580/45227
50+ 'DATATABLES_LANG_URL ' => esc_js ($ this ->conf ->getRouting ()->get3rdPartyAssetsURL ($ dataTablesRelURL .$ dataTablesLangFilename , TRUE )),
51+ );
52+ $ pluginLang = array (
53+ 'LANG_DEAL_DELETING_DIALOG_TEXT ' => $ this ->lang ->escJS ('LANG_DEAL_DELETING_DIALOG_TEXT ' ),
54+ );
55+
56+ if (static ::$ mandatoryPlainJSInitialized === FALSE )
57+ {
58+ static ::$ mandatoryPlainJSInitialized = TRUE ;
59+ ?>
60+ <script type="text/javascript">var DealsVars;</script>
61+ <script type="text/javascript">var DealsLang;</script>
62+ <?php
63+ }
64+ ?>
65+ <script type="text/javascript">DealsVars = <?= json_encode ($ pluginVars , JSON_FORCE_OBJECT );?> ;</script>
66+ <script type="text/javascript">DealsLang = <?= json_encode ($ pluginLang , JSON_FORCE_OBJECT );?> ;</script>
67+ <?php
68+ }
69+
70+ public function registerScripts ()
71+ {
72+ if (defined ('SCRIPT_DEBUG ' ) && SCRIPT_DEBUG )
73+ {
74+ // Debug scripts
75+
76+ // 1. Datatables with Responsive support
77+ wp_register_script ('datatables-jquery-datatables ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/js/jquery.dataTables.js ' ));
78+ wp_register_script ('datatables-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/js/dataTables.jqueryui.js ' ));
79+ wp_register_script ('datatables-responsive-datatables ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/js/dataTables.responsive.js ' ));
80+ wp_register_script ('datatables-responsive-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/js/responsive.jqueryui.js ' ));
81+ } else
82+ {
83+ // Regular scripts
84+
85+ // 1. Datatables with Responsive support
86+ wp_register_script ('datatables-jquery-datatables ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/js/jquery.dataTables.min.js ' ));
87+ wp_register_script ('datatables-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/js/dataTables.jqueryui.min.js ' ));
88+ wp_register_script ('datatables-responsive-datatables ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/js/dataTables.responsive.min.js ' ));
89+ wp_register_script ('datatables-responsive-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/js/responsive.jqueryui.min.js ' ));
90+ }
91+
92+ // 2. jQuery validate
93+ wp_register_script ('jquery-validate ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('jquery-validation/jquery.validate.js ' ));
94+
95+ // 3. NS Admin script
96+ wp_register_script ($ this ->conf ->getPluginHandlePrefix ().'admin ' , $ this ->conf ->getRouting ()->getAdminJS_URL ('DealsAdmin.js ' ), array (), '1.0 ' , TRUE );
97+ }
98+
99+ public function registerStyles ()
100+ {
101+ // Register 3rd party styles for further use (register even it the file is '' - WordPress will process that as needed)
102+ if (defined ('SCRIPT_DEBUG ' ) && SCRIPT_DEBUG )
103+ {
104+ // Debug style
105+
106+ // 1. Font-Awesome styles
107+ wp_register_style ('font-awesome ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('font-awesome/css/font-awesome.css ' ));
108+
109+ // 2. Modern tabs styles
110+ wp_register_style ('modern-tabs ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('ModernTabs/ModernTabs.css ' ));
111+
112+ // 3. jQuery UI theme (currently used for DataTables)
113+ wp_register_style ('jquery-ui-theme ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('jquery-ui/themes/custom/jquery-ui.css ' ));
114+
115+ // 4. Datatables with Responsive support
116+ wp_register_style ('datatables-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/css/dataTables.jqueryui.css ' ));
117+ wp_register_style ('datatables-responsive-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/css/responsive.jqueryui.css ' ));
118+ } else
119+ {
120+ // Regular style
121+
122+ // 1. Font-Awesome styles
123+ wp_register_style ('font-awesome ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('font-awesome/css/font-awesome.min.css ' ));
124+
125+ // 2. Modern tabs styles
126+ wp_register_style ('modern-tabs ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('ModernTabs/ModernTabs.css ' ));
127+
128+ // 3. jQuery UI theme (currently used for DataTables)
129+ wp_register_style ('jquery-ui-theme ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('jquery-ui/themes/custom/jquery-ui.min.css ' ));
130+
131+ // 4. Datatables with Responsive support
132+ wp_register_style ('datatables-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/DataTables-1.10.18/css/dataTables.jqueryui.min.css ' ));
133+ wp_register_style ('datatables-responsive-jqueryui ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('DataTables/Responsive-2.2.2/css/responsive.jqueryui.min.css ' ));
134+ }
135+
136+ // 5. jQuery Validate
137+ wp_register_style ('jquery-validate ' , $ this ->conf ->getRouting ()->get3rdPartyAssetsURL ('jquery-validation/jquery.validate.css ' ));
138+
139+ // 6. Plugin style
140+ wp_register_style ($ this ->conf ->getPluginURL_Prefix ().'admin ' , $ this ->conf ->getRouting ()->getAdminCSS_URL ('Admin.css ' ));
141+ }
142+ }
0 commit comments