PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.3
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
← All changes | class/class-mainwp-client-handler.php +389 -569 5.2.24.3 View file →
@@ -13,621 +13,441 @@
13 13 * @package MainWP\Dashboard
14 14 *
15 15 * @uses \MainWP\Dashboard\MainWP_Client_Handler
16 16 */
17 -class MainWP_Client_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
17 +class MainWP_Client_Handler {
18 18
19 - /**
20 - * Private static variable to hold the single instance of the class.
21 - *
22 - * @static
23 - *
24 - * @var mixed Default null
25 - */
26 - private static $instance = null;
19 + /**
20 + * Private static variable to hold the single instance of the class.
21 + *
22 + * @static
23 + *
24 + * @var mixed Default null
25 + */
26 + private static $instance = null;
27 27
28 - /**
29 - * Method instance()
30 - *
31 - * Create a public static instance.
32 - *
33 - * @static
34 - * @return MainWP_Client_Handler
35 - */
36 - public static function instance() {
37 - if ( null === static::$instance ) {
38 - static::$instance = new self();
39 - }
40 - return static::$instance;
41 - }
28 + /**
29 + * Method instance()
30 + *
31 + * Create a public static instance.
32 + *
33 + * @static
34 + * @return MainWP_Client_Handler
35 + */
36 + public static function instance() {
37 + if ( null == self::$instance ) {
38 + self::$instance = new self();
39 + }
40 + return self::$instance;
41 + }
42 42
43 43
44 - /**
45 - * Get default client fields.
46 - *
47 - * Get default client fields.
48 - */
49 - public static function get_default_client_fields() {
44 + /**
45 + * Get default client fields.
46 + *
47 + * Get default client fields.
48 + */
49 + public static function get_default_client_fields() {
50 50
51 - return array(
52 - 'client.name' => array(
53 - 'title' => esc_html__( 'Client Name (Required)', 'mainwp' ),
54 - 'desc' => esc_html__( 'Displays the Client name', 'mainwp' ),
55 - 'db_field' => 'name',
56 - ),
57 - 'client.email' => array(
58 - 'title' => esc_html__( 'Client email', 'mainwp' ),
59 - 'desc' => esc_html__( 'Displays the client email', 'mainwp' ),
60 - 'db_field' => 'client_email',
61 - ),
62 - 'client.phone' => array(
63 - 'title' => esc_html__( 'Client phone', 'mainwp' ),
64 - 'desc' => esc_html__( 'Displays the client phone', 'mainwp' ),
65 - 'db_field' => 'client_phone',
66 - ),
67 - 'client.facebook' => array(
68 - 'title' => esc_html__( 'Client Facebook', 'mainwp' ),
69 - 'desc' => esc_html__( 'Displays the client Facebook', 'mainwp' ),
70 - 'db_field' => 'client_facebook',
71 - ),
72 - 'client.twitter' => array(
73 - 'title' => esc_html__( 'Client X', 'mainwp' ),
74 - 'desc' => esc_html__( 'Displays the client X', 'mainwp' ),
75 - 'db_field' => 'client_twitter',
76 - ),
77 - 'client.instagram' => array(
78 - 'title' => esc_html__( 'Client Instagram', 'mainwp' ),
79 - 'desc' => esc_html__( 'Displays the client Instagram', 'mainwp' ),
80 - 'db_field' => 'client_instagram',
81 - ),
82 - 'client.linkedin' => array(
83 - 'title' => esc_html__( 'Client LinkedIn', 'mainwp' ),
84 - 'desc' => esc_html__( 'Displays the client LinkedIn', 'mainwp' ),
85 - 'db_field' => 'client_linkedin',
86 - ),
87 - 'client.contact.address.1' => array(
88 - 'title' => esc_html__( 'Client address 1', 'mainwp' ),
89 - 'desc' => esc_html__( 'Displays the client contact address 1', 'mainwp' ),
90 - 'db_field' => 'address_1',
91 - ),
92 - 'client.contact.address.2' => array(
93 - 'title' => esc_html__( 'Client address 2', 'mainwp' ),
94 - 'desc' => esc_html__( 'Displays the client contact address 2', 'mainwp' ),
95 - 'db_field' => 'address_2',
96 - ),
97 - 'client.city' => array(
98 - 'title' => esc_html__( 'Client city', 'mainwp' ),
99 - 'desc' => esc_html__( 'Displays the client city', 'mainwp' ),
100 - 'db_field' => 'city',
101 - ),
102 - 'client.state' => array(
103 - 'title' => esc_html__( 'Client state', 'mainwp' ),
104 - 'desc' => esc_html__( 'Displays the client state', 'mainwp' ),
105 - 'db_field' => 'state',
106 - ),
107 - 'client.zip' => array(
108 - 'title' => esc_html__( 'Client ZIP', 'mainwp' ),
109 - 'desc' => esc_html__( 'Displays the client ZIP code', 'mainwp' ),
110 - 'db_field' => 'zip',
111 - ),
112 - 'client.country' => array(
113 - 'title' => esc_html__( 'Client country', 'mainwp' ),
114 - 'desc' => esc_html__( 'Displays the client country', 'mainwp' ),
115 - 'db_field' => 'country',
116 - ),
117 - 'client.note' => array(
118 - 'title' => esc_html__( 'Client notes', 'mainwp' ),
119 - 'desc' => esc_html__( 'Displays the client note', 'mainwp' ),
120 - 'db_field' => 'note',
121 - ),
122 - 'client.suspended' => array(
123 - 'title' => esc_html__( 'Client status', 'mainwp' ),
124 - 'desc' => esc_html__( 'Displays the client suspended state', 'mainwp' ),
125 - 'db_field' => 'suspended',
126 - ),
127 - 'client.created' => array(
128 - 'title' => esc_html__( 'Added on', 'mainwp' ),
129 - 'desc' => esc_html__( 'Displays the client added on', 'mainwp' ),
130 - 'tooltip' => esc_html__( 'Set the date your client was added to your MainWP Dashboard.', 'mainwp' ),
131 - 'db_field' => 'created',
132 - ),
133 - );
134 - }
51 + return array(
52 + 'client.name' => array(
53 + 'title' => __( 'Client Name', 'mainwp' ),
54 + 'desc' => __( 'Displays the Client name', 'mainwp' ),
55 + 'db_field' => 'name',
56 + ),
57 + 'client.email' => array(
58 + 'title' => __( 'Client contact email', 'mainwp' ),
59 + 'desc' => __( 'Displays the client email', 'mainwp' ),
60 + 'db_field' => 'client_email',
61 + ),
62 + 'client.phone' => array(
63 + 'title' => __( 'Client contact phone', 'mainwp' ),
64 + 'desc' => __( 'Displays the client phone', 'mainwp' ),
65 + 'db_field' => 'client_phone',
66 + ),
67 + 'client.facebook' => array(
68 + 'title' => __( 'Client Facebook', 'mainwp' ),
69 + 'desc' => __( 'Displays the client Facebook', 'mainwp' ),
70 + 'db_field' => 'client_facebook',
71 + ),
72 + 'client.twitter' => array(
73 + 'title' => __( 'Client Twitter', 'mainwp' ),
74 + 'desc' => __( 'Displays the client Twitter', 'mainwp' ),
75 + 'db_field' => 'client_twitter',
76 + ),
77 + 'client.instagram' => array(
78 + 'title' => __( 'Client Instagram', 'mainwp' ),
79 + 'desc' => __( 'Displays the client Instagram', 'mainwp' ),
80 + 'db_field' => 'client_instagram',
81 + ),
82 + 'client.linkedin' => array(
83 + 'title' => __( 'Client LinkedIn', 'mainwp' ),
84 + 'desc' => __( 'Displays the client LinkedIn', 'mainwp' ),
85 + 'db_field' => 'client_linkedin',
86 + ),
87 + 'client.contact.address.1' => array(
88 + 'title' => __( 'Client address 1', 'mainwp' ),
89 + 'desc' => __( 'Displays the client contact address 1', 'mainwp' ),
90 + 'db_field' => 'address_1',
91 + ),
92 + 'client.contact.address.2' => array(
93 + 'title' => __( 'Client address 2', 'mainwp' ),
94 + 'desc' => __( 'Displays the client contact address 2', 'mainwp' ),
95 + 'db_field' => 'address_2',
96 + ),
97 + 'client.city' => array(
98 + 'title' => __( 'Client city', 'mainwp' ),
99 + 'desc' => __( 'Displays the client city', 'mainwp' ),
100 + 'db_field' => 'city',
101 + ),
102 + 'client.state' => array(
103 + 'title' => __( 'Client state', 'mainwp' ),
104 + 'desc' => __( 'Displays the client state', 'mainwp' ),
105 + 'db_field' => 'state',
106 + ),
107 + 'client.zip' => array(
108 + 'title' => __( 'Client ZIP', 'mainwp' ),
109 + 'desc' => __( 'Displays the client ZIP code', 'mainwp' ),
110 + 'db_field' => 'zip',
111 + ),
112 + 'client.country' => array(
113 + 'title' => __( 'Client country', 'mainwp' ),
114 + 'desc' => __( 'Displays the client country', 'mainwp' ),
115 + 'db_field' => 'country',
116 + ),
117 + 'client.note' => array(
118 + 'title' => __( 'Client notes', 'mainwp' ),
119 + 'desc' => __( 'Displays the client note', 'mainwp' ),
120 + 'db_field' => 'note',
121 + ),
122 + 'client.suspended' => array(
123 + 'title' => __( 'Client status', 'mainwp' ),
124 + 'desc' => __( 'Displays the client suspended state', 'mainwp' ),
125 + 'db_field' => 'suspended',
126 + ),
127 + );
128 + }
135 129
136 - /**
137 - * Get default client fields.
138 - *
139 - * Get default client fields.
140 - */
141 - public static function get_mini_default_client_fields() {
142 130
143 - return array(
144 - 'client.name' => array(
145 - 'title' => esc_html__( 'Client Name (Required)', 'mainwp' ),
146 - 'desc' => esc_html__( 'Displays the Client name', 'mainwp' ),
147 - 'db_field' => 'name',
148 - ),
149 - 'client.email' => array(
150 - 'title' => esc_html__( 'Client email', 'mainwp' ),
151 - 'desc' => esc_html__( 'Displays the client email', 'mainwp' ),
152 - 'db_field' => 'client_email',
153 - ),
154 - 'client.phone' => array(
155 - 'title' => esc_html__( 'Client phone', 'mainwp' ),
156 - 'desc' => esc_html__( 'Displays the client phone', 'mainwp' ),
157 - 'db_field' => 'client_phone',
158 - ),
159 - );
160 - }
131 + /**
132 + * Get default contact fields.
133 + *
134 + * Get default contact fields.
135 + */
136 + public static function get_default_contact_fields() {
137 + return array(
138 + 'client.contact.name' => array(
139 + 'title' => __( 'Client contact name', 'mainwp' ),
140 + 'desc' => __( 'Displays the client contact name', 'mainwp' ),
141 + 'db_field' => 'contact_name',
142 + ),
143 + 'contact.role' => array(
144 + 'title' => __( 'Contact contact role', 'mainwp' ),
145 + 'desc' => __( 'Displays the contact role', 'mainwp' ),
146 + 'db_field' => 'contact_role',
147 + ),
148 + 'contact.email' => array(
149 + 'title' => __( 'Contact contact email', 'mainwp' ),
150 + 'desc' => __( 'Displays the contact email', 'mainwp' ),
151 + 'db_field' => 'contact_email',
152 + ),
153 + 'contact.phone' => array(
154 + 'title' => __( 'Contact contact phone', 'mainwp' ),
155 + 'desc' => __( 'Displays the contact phone', 'mainwp' ),
156 + 'db_field' => 'contact_phone',
157 + ),
158 + 'contact.facebook' => array(
159 + 'title' => __( 'Contact Facebook', 'mainwp' ),
160 + 'desc' => __( 'Displays the contact Facebook', 'mainwp' ),
161 + 'db_field' => 'facebook',
162 + ),
163 + 'contact.twitter' => array(
164 + 'title' => __( 'Contact Twitter', 'mainwp' ),
165 + 'desc' => __( 'Displays the contact Twitter', 'mainwp' ),
166 + 'db_field' => 'twitter',
167 + ),
168 + 'contact.instagram' => array(
169 + 'title' => __( 'Contact Instagram', 'mainwp' ),
170 + 'desc' => __( 'Displays the contact Instagram', 'mainwp' ),
171 + 'db_field' => 'instagram',
172 + ),
173 + 'contact.linkedin' => array(
174 + 'title' => __( 'Contact LinkedIn', 'mainwp' ),
175 + 'desc' => __( 'Displays the contact LinkedIn', 'mainwp' ),
176 + 'db_field' => 'linkedin',
177 + ),
178 + );
179 + }
161 180
162 181
163 - /**
164 - * Get default contact fields.
165 - *
166 - * Get default contact fields.
167 - */
168 - public static function get_default_contact_fields() {
169 - return array(
170 - 'client.contact.name' => array(
171 - 'title' => esc_html__( 'Contact name (Required)', 'mainwp' ),
172 - 'desc' => esc_html__( 'Displays the client contact name', 'mainwp' ),
173 - 'db_field' => 'contact_name',
174 - ),
175 - 'contact.email' => array(
176 - 'title' => esc_html__( 'Contact email (Required)', 'mainwp' ),
177 - 'desc' => esc_html__( 'Displays the contact email', 'mainwp' ),
178 - 'db_field' => 'contact_email',
179 - ),
180 - 'contact.role' => array(
181 - 'title' => esc_html__( 'Contact role', 'mainwp' ),
182 - 'desc' => esc_html__( 'Displays the contact role', 'mainwp' ),
183 - 'db_field' => 'contact_role',
184 - ),
185 - 'contact.phone' => array(
186 - 'title' => esc_html__( 'Contact phone', 'mainwp' ),
187 - 'desc' => esc_html__( 'Displays the contact phone', 'mainwp' ),
188 - 'db_field' => 'contact_phone',
189 - ),
190 - 'contact.facebook' => array(
191 - 'title' => esc_html__( 'Contact Facebook', 'mainwp' ),
192 - 'desc' => esc_html__( 'Displays the contact Facebook', 'mainwp' ),
193 - 'db_field' => 'facebook',
194 - ),
195 - 'contact.twitter' => array(
196 - 'title' => esc_html__( 'Contact X', 'mainwp' ),
197 - 'desc' => esc_html__( 'Displays the contact X', 'mainwp' ),
198 - 'db_field' => 'twitter',
199 - ),
200 - 'contact.instagram' => array(
201 - 'title' => esc_html__( 'Contact Instagram', 'mainwp' ),
202 - 'desc' => esc_html__( 'Displays the contact Instagram', 'mainwp' ),
203 - 'db_field' => 'instagram',
204 - ),
205 - 'contact.linkedin' => array(
206 - 'title' => esc_html__( 'Contact LinkedIn', 'mainwp' ),
207 - 'desc' => esc_html__( 'Displays the contact LinkedIn', 'mainwp' ),
208 - 'db_field' => 'linkedin',
209 - ),
210 - );
211 - }
182 + /**
183 + * Method get_compatible_tokens().
184 + *
185 + * Get compatible tokens.
186 + */
187 + public static function get_compatible_tokens() {
188 + return array(
189 + 'client.contact.name' => 'client.contact.name',
190 + 'client.email' => 'contact.email',
191 + 'client.phone' => 'contact.phone',
192 + );
193 + }
194 + /**
195 + * Method rest_api_add_client().
196 + *
197 + * Rest API add client.
198 + *
199 + * @param array $data fields array.
200 + * @param bool $edit Is edit.
201 + *
202 + * $data fields.
203 + * 'email'.
204 + * 'name'.
205 + * 'facebook'.
206 + * 'twitter'.
207 + * 'instagram'.
208 + * 'address_1'.
209 + * 'address_2'.
210 + * 'city'.
211 + * 'zip'.
212 + * 'state'.
213 + * 'note'.
214 + * 'selected_sites'.
215 + * 'client_id'. - to edit client.
216 + *
217 + * @return mixed Results.
218 + */
219 + public static function rest_api_add_client( $data, $edit = false ) { // phpcs:ignore -- comlex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
212 220
213 - /**
214 - * Get default contact fields.
215 - *
216 - * Get default contact fields.
217 - */
218 - public static function get_mini_default_contact_fields() {
219 - return array(
220 - 'client.contact.name' => array(
221 - 'title' => esc_html__( 'Contact name (Required)', 'mainwp' ),
222 - 'desc' => esc_html__( 'Displays the client contact name', 'mainwp' ),
223 - 'db_field' => 'contact_name',
224 - ),
225 - 'contact.email' => array(
226 - 'title' => esc_html__( 'Contact email (Required)', 'mainwp' ),
227 - 'desc' => esc_html__( 'Displays the contact email', 'mainwp' ),
228 - 'db_field' => 'contact_email',
229 - ),
230 - 'contact.role' => array(
231 - 'title' => esc_html__( 'Contact role', 'mainwp' ),
232 - 'desc' => esc_html__( 'Displays the contact role', 'mainwp' ),
233 - 'db_field' => 'contact_role',
234 - ),
235 - );
236 - }
221 + $params = array();
237 222
223 + if ( $edit ) {
238 224
239 - /**
240 - * Method get_compatible_tokens().
241 - *
242 - * Get compatible tokens.
243 - */
244 - public static function get_compatible_tokens() {
245 - return array(
246 - 'client.contact.name' => 'client.contact.name',
247 - 'client.email' => 'contact.email',
248 - 'client.phone' => 'contact.phone',
249 - );
250 - }
251 - /**
252 - * Method rest_api_add_client().
253 - *
254 - * Rest API add client.
255 - *
256 - * @param array $data fields array.
257 - * @param bool $edit Is edit.
258 - *
259 - * $data fields.
260 - * 'client_email'.
261 - * 'name'.
262 - * 'client_facebook'.
263 - * 'client_twitter'.
264 - * 'client_instagram'.
265 - * 'client_linkedin'.
266 - * 'address_1'.
267 - * 'address_2'.
268 - * 'city'.
269 - * 'zip'.
270 - * 'state'.
271 - * 'note'.
272 - * 'selected_sites'.
273 - * 'client_id'. - to edit client.
274 - *
275 - * @throws \Exception Error message.
276 - * @return mixed Results.
277 - */
278 - public static function rest_api_add_client( $data, $edit = false ) { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
225 + $client_id = isset( $data['client_id'] ) ? intval( $data['client_id'] ) : 0;
279 226
280 - if ( empty( $data ) || ! is_array( $data ) ) {
281 - return array( 'error' => esc_html__( 'Invalid Client data. Please try again.', 'mainwp' ) );
282 - }
227 + if ( empty( $client_id ) ) { // edit client.
228 + return array(
229 + 'error' => __( 'Client ID field is required! Please enter a Client ID.', 'mainwp' ),
230 + );
231 + }
283 232
284 - $params = array();
233 + $params['client_id'] = $client_id;
285 234
286 - try {
235 + if ( isset( $data['name'] ) ) {
236 + $params['name'] = sanitize_text_field( wp_unslash( $data['name'] ) );
237 + }
287 238
288 - if ( $edit ) {
239 + if ( isset( $data['address_1'] ) ) {
240 + $params['address_1'] = sanitize_text_field( wp_unslash( $data['address_1'] ) );
241 + }
242 + if ( isset( $data['address_2'] ) ) {
243 + $params['address_2'] = sanitize_text_field( wp_unslash( $data['address_2'] ) );
244 + }
245 + if ( isset( $data['city'] ) ) {
246 + $params['city'] = sanitize_text_field( wp_unslash( $data['city'] ) );
247 + }
248 + if ( isset( $data['zip'] ) ) {
249 + $params['zip'] = sanitize_text_field( wp_unslash( $data['zip'] ) );
250 + }
251 + if ( isset( $data['state'] ) ) {
252 + $params['state'] = sanitize_text_field( wp_unslash( $data['state'] ) );
253 + }
289 254
290 - $client_id = isset( $data['client_id'] ) ? intval( $data['client_id'] ) : 0;
255 + if ( isset( $data['country'] ) ) {
256 + $params['country'] = sanitize_text_field( wp_unslash( $data['country'] ) );
257 + }
291 258
292 - if ( empty( $client_id ) ) { // edit client.
293 - return array(
294 - 'error' => esc_html__( 'Client ID field is required! Please enter a Client ID.', 'mainwp' ),
295 - );
296 - }
259 + if ( isset( $data['note'] ) ) {
260 + $params['note'] = sanitize_text_field( wp_unslash( $data['note'] ) );
261 + }
297 262
298 - $params['client_id'] = $client_id;
263 + MainWP_DB_Client::instance()->update_client( $params );
299 264
300 - if ( isset( $data['name'] ) && ! empty( trim( $data['name'] ) ) ) {
301 - $params['name'] = sanitize_text_field( wp_unslash( $data['name'] ) );
302 - }
265 + if ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) {
266 + $selected_sites = array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) );
267 + MainWP_DB_Client::instance()->update_selected_sites_for_client( $client_id, $selected_sites );
268 + }
303 269
304 - if ( isset( $data['address_1'] ) ) {
305 - $params['address_1'] = sanitize_text_field( wp_unslash( $data['address_1'] ) );
306 - }
307 - if ( isset( $data['address_2'] ) ) {
308 - $params['address_2'] = sanitize_text_field( wp_unslash( $data['address_2'] ) );
309 - }
310 - if ( isset( $data['city'] ) ) {
311 - $params['city'] = sanitize_text_field( wp_unslash( $data['city'] ) );
312 - }
313 - if ( isset( $data['zip'] ) ) {
314 - $params['zip'] = sanitize_text_field( wp_unslash( $data['zip'] ) );
315 - }
316 - if ( isset( $data['state'] ) ) {
317 - $params['state'] = sanitize_text_field( wp_unslash( $data['state'] ) );
318 - }
270 + if ( isset( $data['custom_fields'] ) && is_array( $data['custom_fields'] ) ) {
271 + $custom_fields = array_map( 'sanitize_text_field', wp_unslash( $data['custom_fields'] ) );
272 + foreach ( $custom_fields as $field ) {
273 + $fie_name = isset( $field['field_name'] ) ? sanitize_text_field( wp_unslash( $data['field_name'] ) ) : '';
274 + $fie_value = isset( $field['field_value'] ) ? sanitize_text_field( wp_unslash( $data['field_value'] ) ) : '';
275 + $fie_desc = isset( $field['field_desc'] ) ? sanitize_text_field( wp_unslash( $data['field_desc'] ) ) : '';
319 276
320 - if ( isset( $data['country'] ) ) {
321 - $params['country'] = sanitize_text_field( wp_unslash( $data['country'] ) );
322 - }
277 + if ( ! empty( $fie_name ) && ! empty( $fie_value ) ) {
278 + $get_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, 0 );
279 + if ( $get_gen_field ) { // it is general field.
280 + if ( ! empty( $fie_desc ) ) {
281 + MainWP_DB_Client::instance()->update_client_field(
282 + $get_gen_field->field_id,
283 + array(
284 + 'field_desc' => $fie_desc,
285 + )
286 + );
287 + }
288 + MainWP_DB_Client::instance()->update_client_field_value( $get_gen_field->field_id, $fie_value, $client_id ); // add or update general field value for the client.
289 + } else {
290 + $indi_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, $client_id );
291 + if ( $indi_gen_field ) { // it is individual field.
323 292
324 - if ( isset( $data['note'] ) ) {
325 - $params['note'] = sanitize_text_field( wp_unslash( $data['note'] ) );
326 - }
293 + if ( ! empty( $fie_desc ) ) {
294 + MainWP_DB_Client::instance()->update_client_field(
295 + $indi_gen_field->field_id,
296 + array(
297 + 'field_desc' => $fie_desc,
298 + 'client_id' => $client_id,
299 + )
300 + );
301 + }
302 + MainWP_DB_Client::instance()->update_client_field_value( $indi_gen_field->field_id, $fie_value, $client_id ); // add or update individual field value for the client.
303 + }
304 + }
305 + }
306 + }
307 + }
327 308
328 - if ( isset( $data['client_email'] ) ) {
329 - $params['client_email'] = sanitize_text_field( wp_unslash( $data['client_email'] ) );
330 - }
309 + return array(
310 + 'success' => true,
311 + 'clientid' => $client_id,
312 + );
331 313
332 - if ( isset( $data['client_phone'] ) ) {
333 - $params['client_phone'] = sanitize_text_field( wp_unslash( $data['client_phone'] ) );
334 - }
314 + } else {
335 315
336 - if ( isset( $data['client_facebook'] ) ) {
337 - $params['client_facebook'] = sanitize_text_field( wp_unslash( $data['client_facebook'] ) );
338 - }
316 + $params['name'] = isset( $data['name'] ) ? sanitize_text_field( wp_unslash( $data['name'] ) ) : '';
339 317
340 - if ( isset( $data['client_twitter'] ) ) {
341 - $params['client_twitter'] = sanitize_text_field( wp_unslash( $data['client_twitter'] ) );
342 - }
318 + if ( empty( $params['name'] ) ) {
319 + return array(
320 + 'error' => __( 'Client name field is required! Please enter a Client name.', 'mainwp' ),
321 + );
322 + }
343 323
344 - if ( isset( $data['client_instagram'] ) ) {
345 - $params['client_instagram'] = sanitize_text_field( wp_unslash( $data['client_instagram'] ) );
346 - }
324 + $params['address_1'] = isset( $data['address_1'] ) ? sanitize_text_field( wp_unslash( $data['address_1'] ) ) : '';
325 + $params['address_2'] = isset( $data['address_2'] ) ? sanitize_text_field( wp_unslash( $data['address_2'] ) ) : '';
326 + $params['city'] = isset( $data['city'] ) ? sanitize_text_field( wp_unslash( $data['city'] ) ) : '';
327 + $params['zip'] = isset( $data['zip'] ) ? sanitize_text_field( wp_unslash( $data['zip'] ) ) : '';
328 + $params['state'] = isset( $data['state'] ) ? sanitize_text_field( wp_unslash( $data['state'] ) ) : '';
329 + $params['country'] = isset( $data['country'] ) ? sanitize_text_field( wp_unslash( $data['country'] ) ) : '';
330 + $params['note'] = isset( $data['note'] ) ? sanitize_text_field( wp_unslash( $data['note'] ) ) : '';
347 331
348 - if ( isset( $data['client_linkedin'] ) ) {
349 - $params['client_linkedin'] = sanitize_text_field( wp_unslash( $data['client_linkedin'] ) );
350 - }
332 + $inserted = MainWP_DB_Client::instance()->update_client( $params );
351 333
352 - if ( isset( $data['created'] ) && is_numeric( $data['created'] ) ) {
353 - $params['created'] = intval( $data['created'] );
354 - }
334 + if ( empty( $inserted ) ) {
335 + return array(
336 + 'error' => __( 'Undefined error. Please try again.', 'mainwp' ),
337 + );
338 + }
355 339
356 - MainWP_DB_Client::instance()->update_client( $params, true );
340 + $selected_sites = ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) ) : array();
357 341
358 - if ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) {
359 - $selected_sites = array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) );
360 - MainWP_DB_Client::instance()->update_selected_sites_for_client( $client_id, $selected_sites );
361 - }
342 + MainWP_DB_Client::instance()->update_selected_sites_for_client( $inserted->client_id, $selected_sites );
362 343
363 - if ( isset( $data['custom_fields'] ) && is_array( $data['custom_fields'] ) ) {
364 - $custom_fields = wp_unslash( $data['custom_fields'] );
365 - foreach ( $custom_fields as $field ) {
366 - $fie_name = isset( $field['field_name'] ) ? sanitize_text_field( wp_unslash( $field['field_name'] ) ) : '';
367 - $fie_value = isset( $field['field_value'] ) ? sanitize_text_field( wp_unslash( $field['field_value'] ) ) : '';
368 - $fie_desc = isset( $field['field_desc'] ) ? sanitize_text_field( wp_unslash( $field['field_desc'] ) ) : '';
369 - if ( ! empty( $fie_name ) && ! empty( $fie_value ) ) {
370 - $get_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, 0 );
371 - if ( $get_gen_field ) { // it is general field.
372 - if ( ! empty( $fie_desc ) ) {
373 - MainWP_DB_Client::instance()->update_client_field(
374 - $get_gen_field->field_id,
375 - array(
376 - 'field_desc' => $fie_desc,
377 - )
378 - );
379 - }
380 - MainWP_DB_Client::instance()->update_client_field_value( $get_gen_field->field_id, $fie_value, $client_id ); // add or update general field value for the client.
381 - } else {
382 - $indi_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, $client_id );
383 - if ( $indi_gen_field ) { // it is individual field.
344 + return array(
345 + 'success' => true,
346 + 'clientid' => $inserted->client_id,
347 + );
348 + }
349 + }
384 350
385 - if ( ! empty( $fie_desc ) ) {
386 - MainWP_DB_Client::instance()->update_client_field(
387 - $indi_gen_field->field_id,
388 - array(
389 - 'field_desc' => $fie_desc,
390 - 'client_id' => $client_id,
391 - )
392 - );
393 - }
394 - MainWP_DB_Client::instance()->update_client_field_value( $indi_gen_field->field_id, $fie_value, $client_id ); // add or update individual field value for the client.
395 - }
396 - }
397 - }
398 - }
399 - }
400 351
401 - return array(
402 - 'success' => true,
403 - 'clientid' => $client_id,
404 - );
352 + /**
353 + * Method get_website_client_tokens_data()
354 + *
355 + * Get website client tokens.
356 + *
357 + * @param int $websiteid Website ID.
358 + *
359 + * @return mixed $result Result of tokens.
360 + */
361 + public function get_website_client_tokens_data( $websiteid = false ) { // phpcs:ignore -- comlex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
362 + if ( empty( $websiteid ) ) {
363 + return false;
364 + }
365 + $website = MainWP_DB::instance()->get_website_by_id( $websiteid );
366 + if ( empty( $website ) ) {
367 + return false;
368 + }
405 369
406 - } else {
370 + $client_tokens = array();
407 371
408 - $params['name'] = isset( $data['name'] ) ? sanitize_text_field( wp_unslash( $data['name'] ) ) : '';
372 + $client_tokens['client.site.url'] = $website->url;
373 + $client_tokens['client.site.name'] = $website->name;
409 374
410 - if ( empty( $params['name'] ) ) {
411 - return array(
412 - 'error' => esc_html__( 'Client name field is required! Please enter a Client name.', 'mainwp' ),
413 - );
414 - }
375 + // init values.
376 + $default_client_fields = self::get_default_client_fields();
377 + foreach ( $default_client_fields as $tok_name => $field ) {
378 + $client_tokens[ $tok_name ] = ''; // the tokens with empty value will be removed from the report.
379 + }
380 + $contact_fields = self::get_default_contact_fields();
381 + if ( ! empty( $contact_fields ) && is_array( $contact_fields ) ) {
382 + foreach ( $contact_fields as $tok_name => $field ) {
383 + $client_tokens[ $tok_name ] = ''; // the tokens with empty value will be removed from the report.
384 + }
385 + }
415 386
416 - $params['address_1'] = isset( $data['address_1'] ) ? sanitize_text_field( wp_unslash( $data['address_1'] ) ) : '';
417 - $params['address_2'] = isset( $data['address_2'] ) ? sanitize_text_field( wp_unslash( $data['address_2'] ) ) : '';
418 - $params['city'] = isset( $data['city'] ) ? sanitize_text_field( wp_unslash( $data['city'] ) ) : '';
419 - $params['zip'] = isset( $data['zip'] ) ? sanitize_text_field( wp_unslash( $data['zip'] ) ) : '';
420 - $params['state'] = isset( $data['state'] ) ? sanitize_text_field( wp_unslash( $data['state'] ) ) : '';
421 - $params['country'] = isset( $data['country'] ) ? sanitize_text_field( wp_unslash( $data['country'] ) ) : '';
422 - $params['note'] = isset( $data['note'] ) ? sanitize_text_field( wp_unslash( $data['note'] ) ) : '';
423 - $params['client_email'] = isset( $data['client_email'] ) ? sanitize_text_field( wp_unslash( $data['client_email'] ) ) : '';
424 - $params['client_phone'] = isset( $data['client_phone'] ) ? sanitize_text_field( wp_unslash( $data['client_phone'] ) ) : '';
425 - $params['client_facebook'] = isset( $data['client_facebook'] ) ? sanitize_text_field( wp_unslash( $data['client_facebook'] ) ) : '';
426 - $params['client_twitter'] = isset( $data['client_twitter'] ) ? sanitize_text_field( wp_unslash( $data['client_twitter'] ) ) : '';
427 - $params['client_instagram'] = isset( $data['client_instagram'] ) ? sanitize_text_field( wp_unslash( $data['client_instagram'] ) ) : '';
428 - $params['client_linkedin'] = isset( $data['client_linkedin'] ) ? sanitize_text_field( wp_unslash( $data['client_linkedin'] ) ) : '';
429 - $params['created'] = time();
387 + $custom_tokens_fields = MainWP_DB_Client::instance()->get_client_fields( true, $website->client_id );
388 + if ( ! empty( $custom_tokens_fields ) && is_array( $custom_tokens_fields ) ) {
389 + foreach ( $custom_tokens_fields as $fields ) {
390 + $client_tokens[ $fields->field_name ] = ''; // the tokens with empty value will be removed from the report.
391 + }
392 + }
430 393
431 - $inserted = MainWP_DB_Client::instance()->update_client( $params, true );
394 + if ( empty( $website->client_id ) ) {
395 + return $client_tokens;
396 + }
432 397
433 - if ( empty( $inserted ) ) {
434 - return array(
435 - 'error' => esc_html__( 'Undefined error. Please try again.', 'mainwp' ),
436 - );
437 - }
398 + $clientid = $website->client_id;
438 399
439 - $selected_sites = ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) ) : array();
400 + $client_info = MainWP_DB_Client::instance()->get_wp_client_by( 'client_id', $clientid );
440 401
441 - MainWP_DB_Client::instance()->update_selected_sites_for_client( $inserted->client_id, $selected_sites );
402 + if ( empty( $client_info ) ) {
403 + return $client_tokens;
404 + }
442 405
443 - return array(
444 - 'success' => true,
445 - 'clientid' => $inserted->client_id,
446 - );
447 - }
448 - } catch ( \Exception $e ) {
449 - return array( 'error' => $e->getMessage() );
450 - }
451 - }
406 + if ( ! empty( $custom_tokens_fields ) && is_array( $custom_tokens_fields ) ) {
407 + foreach ( $custom_tokens_fields as $fields ) {
408 + $client_tokens[ $fields->field_name ] = $fields->field_value;
409 + }
410 + }
452 411
412 + foreach ( $default_client_fields as $tok_name => $field ) {
413 + $db_field = $field['db_field'];
414 + if ( property_exists( $client_info, $db_field ) ) {
415 + $client_tokens[ $tok_name ] = $client_info->{$db_field};
416 + }
417 + }
453 418
454 - /**
455 - * Method get_website_client_tokens_data()
456 - *
457 - * Get website client tokens.
458 - *
459 - * @param int $websiteid Website ID.
460 - *
461 - * @return mixed $result Result of tokens.
462 - */
463 - public function get_website_client_tokens_data( $websiteid = false ) { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
464 - if ( empty( $websiteid ) ) {
465 - return false;
466 - }
467 - $website = MainWP_DB::instance()->get_website_by_id( $websiteid );
468 - if ( empty( $website ) ) {
469 - return false;
470 - }
419 + if ( ! empty( $contact_fields ) && is_array( $contact_fields ) ) {
420 + foreach ( $contact_fields as $tok_name => $field ) {
421 + $db_field = isset( $field['db_field'] ) ? $field['db_field'] : '';
422 + $val = '' != $db_field && property_exists( $client_info, $db_field ) ? $client_info->{$db_field} : false;
423 + if ( false !== $val ) {
424 + $client_tokens[ $tok_name ] = $val;
425 + }
426 + }
427 + }
428 + $client_tokens = apply_filters( 'mainwp_clients_website_client_tokens', $client_tokens, $websiteid, $clientid );
429 + return $client_tokens;
430 + }
471 431
472 - $client_tokens = array();
473 -
474 - $client_tokens['client.site.url'] = $website->url;
475 - $client_tokens['client.site.name'] = $website->name;
476 -
477 - // init values.
478 - $default_client_fields = static::get_default_client_fields();
479 - foreach ( $default_client_fields as $tok_name => $field ) {
480 - $client_tokens[ $tok_name ] = ''; // the tokens with empty value will be removed from the report.
481 - }
482 - $contact_fields = static::get_default_contact_fields();
483 - if ( ! empty( $contact_fields ) && is_array( $contact_fields ) ) {
484 - foreach ( $contact_fields as $tok_name => $field ) {
485 - $client_tokens[ $tok_name ] = ''; // the tokens with empty value will be removed from the report.
486 - }
487 - }
488 -
489 - $custom_tokens_fields = MainWP_DB_Client::instance()->get_client_fields( true, $website->client_id );
490 - if ( ! empty( $custom_tokens_fields ) && is_array( $custom_tokens_fields ) ) {
491 - foreach ( $custom_tokens_fields as $fields ) {
492 - $client_tokens[ $fields->field_name ] = ''; // the tokens with empty value will be removed from the report.
493 - }
494 - }
495 -
496 - if ( empty( $website->client_id ) ) {
497 - return $client_tokens;
498 - }
499 -
500 - $clientid = $website->client_id;
501 -
502 - $client_info = MainWP_DB_Client::instance()->get_wp_client_by( 'client_id', $clientid );
503 -
504 - if ( empty( $client_info ) ) {
505 - return $client_tokens;
506 - }
507 -
508 - if ( ! empty( $custom_tokens_fields ) && is_array( $custom_tokens_fields ) ) {
509 - foreach ( $custom_tokens_fields as $fields ) {
510 - $client_tokens[ $fields->field_name ] = $fields->field_value;
511 - }
512 - }
513 -
514 - foreach ( $default_client_fields as $tok_name => $field ) {
515 - $db_field = $field['db_field'];
516 - if ( property_exists( $client_info, $db_field ) ) {
517 - $client_tokens[ $tok_name ] = $client_info->{$db_field};
518 - }
519 - }
520 -
521 - if ( ! empty( $contact_fields ) && is_array( $contact_fields ) ) {
522 - foreach ( $contact_fields as $tok_name => $field ) {
523 - $db_field = isset( $field['db_field'] ) ? $field['db_field'] : '';
524 - $val = '' !== $db_field && property_exists( $client_info, $db_field ) ? $client_info->{$db_field} : false;
525 - if ( false !== $val ) {
526 - $client_tokens[ $tok_name ] = $val;
527 - }
528 - }
529 - }
530 - $client_tokens = apply_filters( 'mainwp_clients_website_client_tokens', $client_tokens, $websiteid, $clientid );
531 - return $client_tokens;
532 - }
533 -
534 - /**
535 - * Method get_client_contact_image()
536 - *
537 - * Get client contact image.
538 - *
539 - * @param array $item client|contact array data.
540 - * @param string $type type of image.
541 - * @param string $what image for what.
542 - */
543 - public static function get_client_contact_image( $item, $type = 'client', $what = 'default' ) {
544 -
545 - if ( empty( $item ) ) {
546 - return '';
547 - }
548 -
549 - $dirs = MainWP_System_Utility::get_mainwp_dir( 'client-images', true );
550 -
551 - if ( 'client' === $type ) {
552 - $image_path = $item['image'];
553 - $icon_info = $item['selected_icon_info'];
554 - } else {
555 - $image_path = $item['contact_image'];
556 - $icon_info = $item['contact_icon_info'];
557 - }
558 -
559 - if ( 'uploaded_icon' === $what ) {
560 - $full_url = '';
561 - if ( ! empty( $image_path ) && file_exists( $dirs[0] . $image_path ) ) {
562 - $full_url = $dirs[1] . $image_path;
563 - }
564 - return $full_url; // return path to uploaded icon.
565 - }
566 -
567 - $img_cls = 'ui mini circular image';
568 - if ( 'card' === $what ) {
569 - $img_cls = 'ui medium circular image';
570 - }
571 -
572 - $icon_wrapper_attr = ' class="' . $img_cls . '" style="height:auto;display:inline-block;" ';
573 - if ( 'display_edit' === $what ) {
574 - $icon_wrapper_attr = ' id="mainwp_add_edit_client_upload_custom_icon" style="width:32px;height:auto;display:inline-block;" class="' . esc_attr( $img_cls ) . '" ';
575 - if ( 'contact' === $type ) {
576 - $icon_wrapper_attr = ' style="width:32px;height:auto;display:inline-block;" class="mainwp_add_edit_contact_upload_custom_icon ' . esc_attr( $img_cls ) . '" ';
577 - }
578 - }
579 -
580 - $img = '';
581 -
582 - if ( ! empty( $image_path ) && file_exists( $dirs[0] . $image_path ) ) {
583 - $full_url = $dirs[1] . $image_path;
584 - $img = '<img ' . $icon_wrapper_attr . ' src="' . esc_attr( $full_url ) . '" alt="' . esc_attr( $item['name'] ) . '">';
585 - } elseif ( ! empty( $icon_info ) ) {
586 - $img = MainWP_Client::get_cust_client_icon( $icon_info, 'display', $what );
587 - }
588 - return $img;
589 - }
590 -
591 -
592 - /**
593 - * Method get_client_image_url()
594 - *
595 - * Get client icon URL.
596 - *
597 - * @param string $image_path Client image url path.
598 - *
599 - * @return mixed $full_url Full image URL.
600 - */
601 - public static function get_client_image_url( $image_path ) {
602 - $full_url = '';
603 - if ( ! empty( $image_path ) ) {
604 - if ( false !== stripos( $image_path, 'assets/images/demo/clients/' ) || false !== stripos( $image_path, 'assets/images/demo/contacts/' ) ) {
605 - $full_url = MAINWP_PLUGIN_URL . $image_path;
606 - } else {
607 - $dirs = MainWP_System_Utility::get_mainwp_dir( 'client-images', true );
608 - if ( file_exists( $dirs[0] . $image_path ) ) {
609 - $full_url = $dirs[1] . $image_path;
610 - } else {
611 - $full_url = '';
612 - }
613 - }
614 - }
615 - return $full_url;
616 - }
617 -
618 - /**
619 - * Method show_notice_existed_contact_emails()
620 - */
621 - public static function show_notice_existed_contact_emails() {
622 - $existed_emails = MainWP_Utility::get_flash_message( 'contact_existed_emails' );
623 - if ( ! empty( $existed_emails ) ) {
624 - $existed_emails = esc_html( $existed_emails );
625 - $existed_emails = str_replace( '|', '<br/>', $existed_emails );
626 - ?>
627 - <div class="ui yellow message">
628 - <?php printf( esc_html__( 'Existed contact emails.%sPlease try again.', 'mainwp' ), '<br/>' . $existed_emails . '</br>' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
629 - </div>
630 - <?php
631 - }
632 - }
432 + /**
433 + * Method get_favico_url()
434 + *
435 + * Get Child Site favicon URL.
436 + *
437 + * @param string $image_path Client image url path.
438 + *
439 + * @return mixed $full_url Full image URL.
440 + */
441 + public static function get_client_image_url( $image_path ) {
442 + $full_url = '';
443 + if ( ! empty( $image_path ) ) {
444 + $dirs = MainWP_System_Utility::get_mainwp_dir( 'client-images', true );
445 + if ( file_exists( $dirs[0] . $image_path ) ) {
446 + $full_url = $dirs[1] . $image_path;
447 + } else {
448 + $full_url = '';
449 + }
450 + }
451 + return $full_url;
452 + }
633 453 }