1
+ <?php
2
+
3
+ /**
4
+ * The admin-specific functionality of the plugin.
5
+ *
6
+ * @link https://www.grocrm.com/
7
+ * @since 1.0.0
8
+ *
9
+ * @package Grocrm
10
+ * @subpackage Grocrm/admin
11
+ */
12
+
13
+ /**
14
+ * The admin-specific functionality of the plugin.
15
+ *
16
+ * Defines the plugin name, version, and two examples hooks for how to
17
+ * enqueue the admin-specific stylesheet and JavaScript.
18
+ *
19
+ * @package Grocrm
20
+ * @subpackage Grocrm/admin
21
+ * @author Gro CRM <support@grocrm.com>
22
+ */
23
+ class Grocrm_Admin {
24
+
25
+ /**
26
+ * The ID of this plugin.
27
+ *
28
+ * @since 1.0.0
29
+ * @access private
30
+ * @var string $plugin_name The ID of this plugin.
31
+ */
32
+ private $ plugin_name ;
33
+
34
+ /**
35
+ * The version of this plugin.
36
+ *
37
+ * @since 1.0.0
38
+ * @access private
39
+ * @var string $version The current version of this plugin.
40
+ */
41
+ private $ version ;
42
+
43
+ private $ login_error ;
44
+
45
+ /**
46
+ * Initialize the class and set its properties.
47
+ *
48
+ * @since 1.0.0
49
+ * @param string $plugin_name The name of this plugin.
50
+ * @param string $version The version of this plugin.
51
+ */
52
+ public function __construct ( $ plugin_name , $ version ) {
53
+
54
+ $ this ->plugin_name = $ plugin_name ;
55
+ $ this ->version = $ version ;
56
+ }
57
+
58
+ /**
59
+ * Register the stylesheets for the admin area.
60
+ *
61
+ * @since 1.0.0
62
+ */
63
+ public function enqueue_styles () {
64
+
65
+ if ( 'settings_page_grocrm ' == get_current_screen () -> id ) {
66
+ wp_enqueue_style ( $ this ->plugin_name , plugin_dir_url ( __FILE__ ) . 'css/grocrm-admin.css ' , array (), $ this ->version , 'all ' );
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Register the JavaScript for the admin area.
72
+ *
73
+ * @since 1.0.0
74
+ */
75
+ public function enqueue_scripts () {
76
+ /*
77
+ if ( 'settings_page_grocrm' == get_current_screen() -> id ) {
78
+ wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/grocrm-admin.js', array( 'jquery' ), $this->version, false );
79
+ }
80
+ */
81
+ }
82
+
83
+ public function add_plugin_admin_menu () {
84
+ add_options_page ( "Gro CRM Setup " , "Gro CRM " , "manage_options " , $ this ->plugin_name , [$ this , "display_plugin_setup_page " ]);
85
+ }
86
+
87
+ public function add_action_links ($ links ) {
88
+ $ settings_link = [
89
+ '<a href=" ' . admin_url ( 'options-general.php?page= ' . $ this ->plugin_name ) . '"> ' . __ ('Settings ' , $ this ->plugin_name ) . '</a> ' ,
90
+ ];
91
+
92
+ return array_merge ($ settings_link , $ links );
93
+ }
94
+
95
+ public function display_plugin_setup_page () {
96
+ include_once 'partials/grocrm-admin-display.php ' ;
97
+ }
98
+
99
+ public function add_admin_body_class ($ classes ) {
100
+
101
+ if ( 'settings_page_grocrm ' == get_current_screen () -> id ) {
102
+
103
+ return "$ classes grocrm-page " ;
104
+ }
105
+
106
+ return $ classes ;
107
+ }
108
+
109
+ public function request_handler () {
110
+
111
+ if (isset ($ _POST ['grocrm_action ' ])) {
112
+ switch ($ _POST ['grocrm_action ' ]) {
113
+ case 'login ' :
114
+ $ this ->login ();
115
+ break ;
116
+ case 'logout ' :
117
+ $ this ->logout ();
118
+ break ;
119
+ case 'update ' :
120
+ $ this ->update_settings ();
121
+ break ;
122
+ }
123
+ }
124
+ }
125
+
126
+ // Helper functions
127
+
128
+ private function login () {
129
+
130
+ if (!class_exists ('GroCRM_API ' )) {
131
+ $ path = plugin_dir_path (__FILE__ );
132
+ require_once ($ path . '../api/grocrm.php ' );
133
+ }
134
+
135
+ $ apiKey = sanitize_text_field ($ _POST ['grocrm_api_key ' ]);
136
+ $ grocrm_api = new GroCRM_API ($ apiKey );
137
+
138
+ try {
139
+
140
+ $ user = $ grocrm_api ->getUser ();
141
+ update_option ("grocrm_user " , $ user );
142
+ $ this ->update_text_field ('grocrm_api_key ' );
143
+ $ this ->login_error = null ;
144
+
145
+ } catch (Exception $ e ) {
146
+ if ($ e ->getCode () == 401 ) {
147
+ $ this ->login_error = esc_html_e ( "Whoops, those credentials are not correct! " , "grocrm " );
148
+ } else {
149
+ $ this ->login_error = esc_html_e ( "An error has occured, please try again later " , "grocrm " );
150
+ }
151
+ }
152
+ }
153
+
154
+ private function logout () {
155
+ delete_option ("grocrm_api_key " );
156
+ delete_option ("grocrm_user " );
157
+ }
158
+
159
+ private function update_settings () {
160
+
161
+ $ this ->update_text_field ('grocrm_header ' , true );
162
+ $ this ->update_text_field ('grocrm_subheader ' , true );
163
+ $ this ->update_text_field ('grocrm_submit ' );
164
+ $ this ->update_text_field ('grocrm_default_type ' );
165
+
166
+
167
+ $ defaultTags = preg_replace ('/\s+/ ' , '' , sanitize_text_field ($ _POST ["grocrm_default_tags " ]));
168
+ update_option ( "grocrm_default_tags " , $ defaultTags );
169
+
170
+ $ this ->update_checkbox ('grocrm_css_disabled ' );
171
+
172
+ $ this ->update_fields ();
173
+ }
174
+
175
+ private function update_text_field ($ name , $ allowsHTML = false ) {
176
+ if ($ allowsHTML ) {
177
+ $ content = stripslashes ($ _POST [$ name ]);
178
+ $ content = str_replace ("\r\n" ,"<br/> " , $ content );
179
+ update_option ($ name , $ content );
180
+ } else {
181
+ update_option ($ name , sanitize_text_field ($ _POST [$ name ]));
182
+ }
183
+ }
184
+
185
+ private function update_checkbox ($ name ) {
186
+
187
+ if (isset ($ _POST [$ name ]) && !empty ($ _POST [$ name ])) {
188
+ update_option ($ name , true );
189
+ } else {
190
+ update_option ($ name , false );
191
+ }
192
+ }
193
+
194
+ private function update_fields () {
195
+
196
+ $ field_keys = [];
197
+
198
+ foreach (grocrm_fields () as $ key => $ value ) {
199
+
200
+ if ($ value ['required ' ]) {
201
+ $ field_keys [] = $ key ;
202
+ continue ;
203
+ }
204
+
205
+ if (isset ($ _POST [$ key ]) && !empty ($ _POST [$ key ])) {
206
+ $ field_keys [] = $ key ;
207
+ }
208
+ }
209
+
210
+ update_option ("grocrm_field_keys " , $ field_keys );
211
+ }
212
+ }
0 commit comments