| @@ -6,8 +6,13 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace MainWP\Dashboard; |
| 9 | 9 | |
| 10 | +// Exit if accessed directly. | |
| 11 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | + exit; | |
| 13 | +} | |
| 14 | + | |
| 10 | 15 | /** |
| 11 | 16 | * Class MainWP_Client_Handler |
| 12 | 17 | * |
| 13 | 18 | * @package MainWP\Dashboard |
| @@ -49,11 +54,12 @@ | ||
| 49 | 54 | public static function get_default_client_fields() { |
| 50 | 55 | |
| 51 | 56 | return array( |
| 52 | 57 | 'client.name' => array( |
| 53 | - 'title' => esc_html__( 'Client Name (Required)', 'mainwp' ), | |
| 58 | + 'title' => esc_html__( 'Client Name', 'mainwp' ), | |
| 54 | 59 | 'desc' => esc_html__( 'Displays the Client name', 'mainwp' ), |
| 55 | 60 | 'db_field' => 'name', |
| 61 | + 'required' => true, | |
| 56 | 62 | ), |
| 57 | 63 | 'client.email' => array( |
| 58 | 64 | 'title' => esc_html__( 'Client email', 'mainwp' ), |
| 59 | 65 | 'desc' => esc_html__( 'Displays the client email', 'mainwp' ), |
| @@ -69,10 +75,10 @@ | ||
| 69 | 75 | 'desc' => esc_html__( 'Displays the client Facebook', 'mainwp' ), |
| 70 | 76 | 'db_field' => 'client_facebook', |
| 71 | 77 | ), |
| 72 | 78 | 'client.twitter' => array( |
| 73 | - 'title' => esc_html__( 'Client Twitter', 'mainwp' ), | |
| 74 | - 'desc' => esc_html__( 'Displays the client Twitter', 'mainwp' ), | |
| 79 | + 'title' => esc_html__( 'Client X', 'mainwp' ), | |
| 80 | + 'desc' => esc_html__( 'Displays the client X', 'mainwp' ), | |
| 75 | 81 | 'db_field' => 'client_twitter', |
| 76 | 82 | ), |
| 77 | 83 | 'client.instagram' => array( |
| 78 | 84 | 'title' => esc_html__( 'Client Instagram', 'mainwp' ), |
| @@ -126,9 +132,8 @@ | ||
| 126 | 132 | ), |
| 127 | 133 | 'client.created' => array( |
| 128 | 134 | 'title' => esc_html__( 'Added on', 'mainwp' ), |
| 129 | 135 | '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 | 136 | 'db_field' => 'created', |
| 132 | 137 | ), |
| 133 | 138 | ); |
| 134 | 139 | } |
| @@ -141,21 +146,24 @@ | ||
| 141 | 146 | public static function get_mini_default_client_fields() { |
| 142 | 147 | |
| 143 | 148 | return array( |
| 144 | 149 | 'client.name' => array( |
| 145 | - 'title' => esc_html__( 'Client Name (Required)', 'mainwp' ), | |
| 150 | + 'title' => esc_html__( 'Client Name', 'mainwp' ), | |
| 146 | 151 | 'desc' => esc_html__( 'Displays the Client name', 'mainwp' ), |
| 147 | 152 | 'db_field' => 'name', |
| 153 | + 'required' => true, | |
| 148 | 154 | ), |
| 149 | 155 | 'client.email' => array( |
| 150 | 156 | 'title' => esc_html__( 'Client email', 'mainwp' ), |
| 151 | 157 | 'desc' => esc_html__( 'Displays the client email', 'mainwp' ), |
| 152 | 158 | 'db_field' => 'client_email', |
| 159 | + 'required' => false, | |
| 153 | 160 | ), |
| 154 | 161 | 'client.phone' => array( |
| 155 | 162 | 'title' => esc_html__( 'Client phone', 'mainwp' ), |
| 156 | 163 | 'desc' => esc_html__( 'Displays the client phone', 'mainwp' ), |
| 157 | 164 | 'db_field' => 'client_phone', |
| 165 | + 'required' => false, | |
| 158 | 166 | ), |
| 159 | 167 | ); |
| 160 | 168 | } |
| 161 | 169 | |
| @@ -167,16 +175,18 @@ | ||
| 167 | 175 | */ |
| 168 | 176 | public static function get_default_contact_fields() { |
| 169 | 177 | return array( |
| 170 | 178 | 'client.contact.name' => array( |
| 171 | - 'title' => esc_html__( 'Contact name (Required)', 'mainwp' ), | |
| 179 | + 'title' => esc_html__( 'Contact name', 'mainwp' ), | |
| 172 | 180 | 'desc' => esc_html__( 'Displays the client contact name', 'mainwp' ), |
| 173 | 181 | 'db_field' => 'contact_name', |
| 182 | + 'required' => true, | |
| 174 | 183 | ), |
| 175 | 184 | 'contact.email' => array( |
| 176 | - 'title' => esc_html__( 'Contact email (Required)', 'mainwp' ), | |
| 185 | + 'title' => esc_html__( 'Contact email', 'mainwp' ), | |
| 177 | 186 | 'desc' => esc_html__( 'Displays the contact email', 'mainwp' ), |
| 178 | 187 | 'db_field' => 'contact_email', |
| 188 | + 'required' => true, | |
| 179 | 189 | ), |
| 180 | 190 | 'contact.role' => array( |
| 181 | 191 | 'title' => esc_html__( 'Contact role', 'mainwp' ), |
| 182 | 192 | 'desc' => esc_html__( 'Displays the contact role', 'mainwp' ), |
| @@ -192,10 +202,10 @@ | ||
| 192 | 202 | 'desc' => esc_html__( 'Displays the contact Facebook', 'mainwp' ), |
| 193 | 203 | 'db_field' => 'facebook', |
| 194 | 204 | ), |
| 195 | 205 | 'contact.twitter' => array( |
| 196 | - 'title' => esc_html__( 'Contact Twitter', 'mainwp' ), | |
| 197 | - 'desc' => esc_html__( 'Displays the contact Twitter', 'mainwp' ), | |
| 206 | + 'title' => esc_html__( 'Contact X', 'mainwp' ), | |
| 207 | + 'desc' => esc_html__( 'Displays the contact X', 'mainwp' ), | |
| 198 | 208 | 'db_field' => 'twitter', |
| 199 | 209 | ), |
| 200 | 210 | 'contact.instagram' => array( |
| 201 | 211 | 'title' => esc_html__( 'Contact Instagram', 'mainwp' ), |
| @@ -217,21 +227,24 @@ | ||
| 217 | 227 | */ |
| 218 | 228 | public static function get_mini_default_contact_fields() { |
| 219 | 229 | return array( |
| 220 | 230 | 'client.contact.name' => array( |
| 221 | - 'title' => esc_html__( 'Contact name (Required)', 'mainwp' ), | |
| 231 | + 'title' => esc_html__( 'Contact name', 'mainwp' ), | |
| 222 | 232 | 'desc' => esc_html__( 'Displays the client contact name', 'mainwp' ), |
| 223 | 233 | 'db_field' => 'contact_name', |
| 234 | + 'required' => true, | |
| 224 | 235 | ), |
| 225 | 236 | 'contact.email' => array( |
| 226 | - 'title' => esc_html__( 'Contact email (Required)', 'mainwp' ), | |
| 237 | + 'title' => esc_html__( 'Contact email', 'mainwp' ), | |
| 227 | 238 | 'desc' => esc_html__( 'Displays the contact email', 'mainwp' ), |
| 228 | 239 | 'db_field' => 'contact_email', |
| 240 | + 'required' => true, | |
| 229 | 241 | ), |
| 230 | 242 | 'contact.role' => array( |
| 231 | 243 | 'title' => esc_html__( 'Contact role', 'mainwp' ), |
| 232 | 244 | 'desc' => esc_html__( 'Displays the contact role', 'mainwp' ), |
| 233 | 245 | 'db_field' => 'contact_role', |
| 246 | + 'required' => false, | |
| 234 | 247 | ), |
| 235 | 248 | ); |
| 236 | 249 | } |
| 237 | 250 | |
| @@ -276,168 +289,180 @@ | ||
| 276 | 289 | * @return mixed Results. |
| 277 | 290 | */ |
| 278 | 291 | 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. |
| 279 | 292 | |
| 293 | + if ( empty( $data ) || ! is_array( $data ) ) { | |
| 294 | + return array( 'error' => esc_html__( 'Invalid Client data. Please try again.', 'mainwp' ) ); | |
| 295 | + } | |
| 296 | + | |
| 280 | 297 | $params = array(); |
| 281 | 298 | |
| 282 | - if ( $edit ) { | |
| 299 | + try { | |
| 283 | 300 | |
| 284 | - $client_id = isset( $data['client_id'] ) ? intval( $data['client_id'] ) : 0; | |
| 301 | + if ( $edit ) { | |
| 285 | 302 | |
| 286 | - if ( empty( $client_id ) ) { // edit client. | |
| 287 | - return array( | |
| 288 | - 'error' => esc_html__( 'Client ID field is required! Please enter a Client ID.', 'mainwp' ), | |
| 289 | - ); | |
| 290 | - } | |
| 303 | + $client_id = isset( $data['client_id'] ) ? intval( $data['client_id'] ) : 0; | |
| 291 | 304 | |
| 292 | - $params['client_id'] = $client_id; | |
| 305 | + if ( empty( $client_id ) ) { // edit client. | |
| 306 | + return array( | |
| 307 | + 'error' => esc_html__( 'Client ID field is required! Please enter a Client ID.', 'mainwp' ), | |
| 308 | + ); | |
| 309 | + } | |
| 293 | 310 | |
| 294 | - if ( isset( $data['name'] ) ) { | |
| 295 | - $params['name'] = sanitize_text_field( wp_unslash( $data['name'] ) ); | |
| 296 | - } | |
| 311 | + $params['client_id'] = $client_id; | |
| 297 | 312 | |
| 298 | - if ( isset( $data['address_1'] ) ) { | |
| 299 | - $params['address_1'] = sanitize_text_field( wp_unslash( $data['address_1'] ) ); | |
| 300 | - } | |
| 301 | - if ( isset( $data['address_2'] ) ) { | |
| 302 | - $params['address_2'] = sanitize_text_field( wp_unslash( $data['address_2'] ) ); | |
| 303 | - } | |
| 304 | - if ( isset( $data['city'] ) ) { | |
| 305 | - $params['city'] = sanitize_text_field( wp_unslash( $data['city'] ) ); | |
| 306 | - } | |
| 307 | - if ( isset( $data['zip'] ) ) { | |
| 308 | - $params['zip'] = sanitize_text_field( wp_unslash( $data['zip'] ) ); | |
| 309 | - } | |
| 310 | - if ( isset( $data['state'] ) ) { | |
| 311 | - $params['state'] = sanitize_text_field( wp_unslash( $data['state'] ) ); | |
| 312 | - } | |
| 313 | + if ( isset( $data['name'] ) && ! empty( trim( $data['name'] ) ) ) { | |
| 314 | + $params['name'] = sanitize_text_field( wp_unslash( $data['name'] ) ); | |
| 315 | + } | |
| 313 | 316 | |
| 314 | - if ( isset( $data['country'] ) ) { | |
| 315 | - $params['country'] = sanitize_text_field( wp_unslash( $data['country'] ) ); | |
| 316 | - } | |
| 317 | + if ( isset( $data['address_1'] ) ) { | |
| 318 | + $params['address_1'] = sanitize_text_field( wp_unslash( $data['address_1'] ) ); | |
| 319 | + } | |
| 320 | + if ( isset( $data['address_2'] ) ) { | |
| 321 | + $params['address_2'] = sanitize_text_field( wp_unslash( $data['address_2'] ) ); | |
| 322 | + } | |
| 323 | + if ( isset( $data['city'] ) ) { | |
| 324 | + $params['city'] = sanitize_text_field( wp_unslash( $data['city'] ) ); | |
| 325 | + } | |
| 326 | + if ( isset( $data['zip'] ) ) { | |
| 327 | + $params['zip'] = sanitize_text_field( wp_unslash( $data['zip'] ) ); | |
| 328 | + } | |
| 329 | + if ( isset( $data['state'] ) ) { | |
| 330 | + $params['state'] = sanitize_text_field( wp_unslash( $data['state'] ) ); | |
| 331 | + } | |
| 317 | 332 | |
| 318 | - if ( isset( $data['note'] ) ) { | |
| 319 | - $params['note'] = sanitize_text_field( wp_unslash( $data['note'] ) ); | |
| 320 | - } | |
| 333 | + if ( isset( $data['country'] ) ) { | |
| 334 | + $params['country'] = sanitize_text_field( wp_unslash( $data['country'] ) ); | |
| 335 | + } | |
| 321 | 336 | |
| 322 | - if ( isset( $data['client_email'] ) ) { | |
| 323 | - $params['client_email'] = sanitize_text_field( wp_unslash( $data['client_email'] ) ); | |
| 324 | - } | |
| 337 | + if ( isset( $data['note'] ) ) { | |
| 338 | + $params['note'] = sanitize_text_field( wp_unslash( $data['note'] ) ); | |
| 339 | + } | |
| 325 | 340 | |
| 326 | - if ( isset( $data['client_phone'] ) ) { | |
| 327 | - $params['client_phone'] = sanitize_text_field( wp_unslash( $data['client_phone'] ) ); | |
| 328 | - } | |
| 341 | + if ( isset( $data['client_email'] ) ) { | |
| 342 | + $params['client_email'] = sanitize_text_field( wp_unslash( $data['client_email'] ) ); | |
| 343 | + } | |
| 329 | 344 | |
| 330 | - if ( isset( $data['client_facebook'] ) ) { | |
| 331 | - $params['client_facebook'] = sanitize_text_field( wp_unslash( $data['client_facebook'] ) ); | |
| 332 | - } | |
| 345 | + if ( isset( $data['client_phone'] ) ) { | |
| 346 | + $params['client_phone'] = sanitize_text_field( wp_unslash( $data['client_phone'] ) ); | |
| 347 | + } | |
| 333 | 348 | |
| 334 | - if ( isset( $data['client_twitter'] ) ) { | |
| 335 | - $params['client_twitter'] = sanitize_text_field( wp_unslash( $data['client_twitter'] ) ); | |
| 336 | - } | |
| 349 | + if ( isset( $data['client_facebook'] ) ) { | |
| 350 | + $params['client_facebook'] = sanitize_text_field( wp_unslash( $data['client_facebook'] ) ); | |
| 351 | + } | |
| 337 | 352 | |
| 338 | - if ( isset( $data['client_instagram'] ) ) { | |
| 339 | - $params['client_instagram'] = sanitize_text_field( wp_unslash( $data['client_instagram'] ) ); | |
| 340 | - } | |
| 353 | + if ( isset( $data['client_twitter'] ) ) { | |
| 354 | + $params['client_twitter'] = sanitize_text_field( wp_unslash( $data['client_twitter'] ) ); | |
| 355 | + } | |
| 341 | 356 | |
| 342 | - if ( isset( $data['client_linkedin'] ) ) { | |
| 343 | - $params['client_linkedin'] = sanitize_text_field( wp_unslash( $data['client_linkedin'] ) ); | |
| 344 | - } | |
| 357 | + if ( isset( $data['client_instagram'] ) ) { | |
| 358 | + $params['client_instagram'] = sanitize_text_field( wp_unslash( $data['client_instagram'] ) ); | |
| 359 | + } | |
| 345 | 360 | |
| 346 | - if ( isset( $data['created'] ) && is_numeric( $data['created'] ) ) { | |
| 347 | - $params['created'] = intval( $data['created'] ); | |
| 348 | - } | |
| 361 | + if ( isset( $data['client_linkedin'] ) ) { | |
| 362 | + $params['client_linkedin'] = sanitize_text_field( wp_unslash( $data['client_linkedin'] ) ); | |
| 363 | + } | |
| 349 | 364 | |
| 350 | - MainWP_DB_Client::instance()->update_client( $params, true ); | |
| 365 | + if ( isset( $data['created'] ) && is_numeric( $data['created'] ) ) { | |
| 366 | + $params['created'] = intval( $data['created'] ); | |
| 367 | + } | |
| 351 | 368 | |
| 352 | - if ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) { | |
| 353 | - $selected_sites = array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) ); | |
| 354 | - MainWP_DB_Client::instance()->update_selected_sites_for_client( $client_id, $selected_sites ); | |
| 355 | - } | |
| 369 | + MainWP_DB_Client::instance()->update_client( $params, true ); | |
| 356 | 370 | |
| 357 | - if ( isset( $data['custom_fields'] ) && is_array( $data['custom_fields'] ) ) { | |
| 358 | - $custom_fields = wp_unslash( $data['custom_fields'] ); | |
| 359 | - foreach ( $custom_fields as $field ) { | |
| 360 | - $fie_name = isset( $field['field_name'] ) ? sanitize_text_field( wp_unslash( $field['field_name'] ) ) : ''; | |
| 361 | - $fie_value = isset( $field['field_value'] ) ? sanitize_text_field( wp_unslash( $field['field_value'] ) ) : ''; | |
| 362 | - $fie_desc = isset( $field['field_desc'] ) ? sanitize_text_field( wp_unslash( $field['field_desc'] ) ) : ''; | |
| 363 | - if ( ! empty( $fie_name ) && ! empty( $fie_value ) ) { | |
| 364 | - $get_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, 0 ); | |
| 365 | - if ( $get_gen_field ) { // it is general field. | |
| 366 | - if ( ! empty( $fie_desc ) ) { | |
| 367 | - MainWP_DB_Client::instance()->update_client_field( | |
| 368 | - $get_gen_field->field_id, | |
| 369 | - array( | |
| 370 | - 'field_desc' => $fie_desc, | |
| 371 | - ) | |
| 372 | - ); | |
| 373 | - } | |
| 374 | - 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. | |
| 375 | - } else { | |
| 376 | - $indi_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, $client_id ); | |
| 377 | - if ( $indi_gen_field ) { // it is individual field. | |
| 371 | + if ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) { | |
| 372 | + $selected_sites = array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) ); | |
| 373 | + MainWP_DB_Client::instance()->update_selected_sites_for_client( $client_id, $selected_sites ); | |
| 374 | + } | |
| 378 | 375 | |
| 376 | + if ( isset( $data['custom_fields'] ) && is_array( $data['custom_fields'] ) ) { | |
| 377 | + $custom_fields = wp_unslash( $data['custom_fields'] ); | |
| 378 | + foreach ( $custom_fields as $field ) { | |
| 379 | + $fie_name = isset( $field['field_name'] ) ? sanitize_text_field( wp_unslash( $field['field_name'] ) ) : ''; | |
| 380 | + $fie_value = isset( $field['field_value'] ) ? sanitize_text_field( wp_unslash( $field['field_value'] ) ) : ''; | |
| 381 | + $fie_desc = isset( $field['field_desc'] ) ? sanitize_text_field( wp_unslash( $field['field_desc'] ) ) : ''; | |
| 382 | + if ( ! empty( $fie_name ) && ! empty( $fie_value ) ) { | |
| 383 | + $get_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, 0 ); | |
| 384 | + if ( $get_gen_field ) { // it is general field. | |
| 379 | 385 | if ( ! empty( $fie_desc ) ) { |
| 380 | 386 | MainWP_DB_Client::instance()->update_client_field( |
| 381 | - $indi_gen_field->field_id, | |
| 387 | + $get_gen_field->field_id, | |
| 382 | 388 | array( |
| 383 | 389 | 'field_desc' => $fie_desc, |
| 384 | - 'client_id' => $client_id, | |
| 385 | 390 | ) |
| 386 | 391 | ); |
| 387 | 392 | } |
| 388 | - 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. | |
| 393 | + 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. | |
| 394 | + } else { | |
| 395 | + $indi_gen_field = MainWP_DB_Client::instance()->get_client_fields_by( 'field_name', $fie_name, $client_id ); | |
| 396 | + if ( $indi_gen_field ) { // it is individual field. | |
| 397 | + | |
| 398 | + if ( ! empty( $fie_desc ) ) { | |
| 399 | + MainWP_DB_Client::instance()->update_client_field( | |
| 400 | + $indi_gen_field->field_id, | |
| 401 | + array( | |
| 402 | + 'field_desc' => $fie_desc, | |
| 403 | + 'client_id' => $client_id, | |
| 404 | + ) | |
| 405 | + ); | |
| 406 | + } | |
| 407 | + 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. | |
| 408 | + } | |
| 389 | 409 | } |
| 390 | 410 | } |
| 391 | 411 | } |
| 392 | 412 | } |
| 393 | - } | |
| 394 | 413 | |
| 395 | - return array( | |
| 396 | - 'success' => true, | |
| 397 | - 'clientid' => $client_id, | |
| 398 | - ); | |
| 414 | + return array( | |
| 415 | + 'success' => true, | |
| 416 | + 'client_id' => $client_id, | |
| 417 | + 'clientid' => $client_id, // Legacy key for backwards compatibility. | |
| 418 | + ); | |
| 399 | 419 | |
| 400 | - } else { | |
| 420 | + } else { | |
| 401 | 421 | |
| 402 | - $params['name'] = isset( $data['name'] ) ? sanitize_text_field( wp_unslash( $data['name'] ) ) : ''; | |
| 422 | + $params['name'] = isset( $data['name'] ) ? sanitize_text_field( wp_unslash( $data['name'] ) ) : ''; | |
| 403 | 423 | |
| 404 | - if ( empty( $params['name'] ) ) { | |
| 405 | - return array( | |
| 406 | - 'error' => esc_html__( 'Client name field is required! Please enter a Client name.', 'mainwp' ), | |
| 407 | - ); | |
| 408 | - } | |
| 424 | + if ( empty( $params['name'] ) ) { | |
| 425 | + return array( | |
| 426 | + 'error' => esc_html__( 'Client name field is required! Please enter a Client name.', 'mainwp' ), | |
| 427 | + ); | |
| 428 | + } | |
| 409 | 429 | |
| 410 | - $params['address_1'] = isset( $data['address_1'] ) ? sanitize_text_field( wp_unslash( $data['address_1'] ) ) : ''; | |
| 411 | - $params['address_2'] = isset( $data['address_2'] ) ? sanitize_text_field( wp_unslash( $data['address_2'] ) ) : ''; | |
| 412 | - $params['city'] = isset( $data['city'] ) ? sanitize_text_field( wp_unslash( $data['city'] ) ) : ''; | |
| 413 | - $params['zip'] = isset( $data['zip'] ) ? sanitize_text_field( wp_unslash( $data['zip'] ) ) : ''; | |
| 414 | - $params['state'] = isset( $data['state'] ) ? sanitize_text_field( wp_unslash( $data['state'] ) ) : ''; | |
| 415 | - $params['country'] = isset( $data['country'] ) ? sanitize_text_field( wp_unslash( $data['country'] ) ) : ''; | |
| 416 | - $params['note'] = isset( $data['note'] ) ? sanitize_text_field( wp_unslash( $data['note'] ) ) : ''; | |
| 417 | - $params['client_email'] = isset( $data['client_email'] ) ? sanitize_text_field( wp_unslash( $data['client_email'] ) ) : ''; | |
| 418 | - $params['client_phone'] = isset( $data['client_phone'] ) ? sanitize_text_field( wp_unslash( $data['client_phone'] ) ) : ''; | |
| 419 | - $params['client_facebook'] = isset( $data['client_facebook'] ) ? sanitize_text_field( wp_unslash( $data['client_facebook'] ) ) : ''; | |
| 420 | - $params['client_twitter'] = isset( $data['client_twitter'] ) ? sanitize_text_field( wp_unslash( $data['client_twitter'] ) ) : ''; | |
| 421 | - $params['client_instagram'] = isset( $data['client_instagram'] ) ? sanitize_text_field( wp_unslash( $data['client_instagram'] ) ) : ''; | |
| 422 | - $params['client_linkedin'] = isset( $data['client_linkedin'] ) ? sanitize_text_field( wp_unslash( $data['client_linkedin'] ) ) : ''; | |
| 430 | + $params['address_1'] = isset( $data['address_1'] ) ? sanitize_text_field( wp_unslash( $data['address_1'] ) ) : ''; | |
| 431 | + $params['address_2'] = isset( $data['address_2'] ) ? sanitize_text_field( wp_unslash( $data['address_2'] ) ) : ''; | |
| 432 | + $params['city'] = isset( $data['city'] ) ? sanitize_text_field( wp_unslash( $data['city'] ) ) : ''; | |
| 433 | + $params['zip'] = isset( $data['zip'] ) ? sanitize_text_field( wp_unslash( $data['zip'] ) ) : ''; | |
| 434 | + $params['state'] = isset( $data['state'] ) ? sanitize_text_field( wp_unslash( $data['state'] ) ) : ''; | |
| 435 | + $params['country'] = isset( $data['country'] ) ? sanitize_text_field( wp_unslash( $data['country'] ) ) : ''; | |
| 436 | + $params['note'] = isset( $data['note'] ) ? sanitize_text_field( wp_unslash( $data['note'] ) ) : ''; | |
| 437 | + $params['client_email'] = isset( $data['client_email'] ) ? sanitize_text_field( wp_unslash( $data['client_email'] ) ) : ''; | |
| 438 | + $params['client_phone'] = isset( $data['client_phone'] ) ? sanitize_text_field( wp_unslash( $data['client_phone'] ) ) : ''; | |
| 439 | + $params['client_facebook'] = isset( $data['client_facebook'] ) ? sanitize_text_field( wp_unslash( $data['client_facebook'] ) ) : ''; | |
| 440 | + $params['client_twitter'] = isset( $data['client_twitter'] ) ? sanitize_text_field( wp_unslash( $data['client_twitter'] ) ) : ''; | |
| 441 | + $params['client_instagram'] = isset( $data['client_instagram'] ) ? sanitize_text_field( wp_unslash( $data['client_instagram'] ) ) : ''; | |
| 442 | + $params['client_linkedin'] = isset( $data['client_linkedin'] ) ? sanitize_text_field( wp_unslash( $data['client_linkedin'] ) ) : ''; | |
| 443 | + $params['created'] = time(); | |
| 423 | 444 | |
| 424 | - $inserted = MainWP_DB_Client::instance()->update_client( $params ); | |
| 445 | + $inserted = MainWP_DB_Client::instance()->update_client( $params, true ); | |
| 425 | 446 | |
| 426 | - if ( empty( $inserted ) ) { | |
| 447 | + if ( empty( $inserted ) ) { | |
| 448 | + return array( | |
| 449 | + 'error' => esc_html__( 'Undefined error. Please try again.', 'mainwp' ), | |
| 450 | + ); | |
| 451 | + } | |
| 452 | + | |
| 453 | + $selected_sites = ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) ) : array(); | |
| 454 | + | |
| 455 | + MainWP_DB_Client::instance()->update_selected_sites_for_client( $inserted->client_id, $selected_sites ); | |
| 456 | + | |
| 427 | 457 | return array( |
| 428 | - 'error' => esc_html__( 'Undefined error. Please try again.', 'mainwp' ), | |
| 458 | + 'success' => true, | |
| 459 | + 'client_id' => $inserted->client_id, | |
| 460 | + 'clientid' => $inserted->client_id, // Legacy key for backwards compatibility. | |
| 429 | 461 | ); |
| 430 | 462 | } |
| 431 | - | |
| 432 | - $selected_sites = ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) ) : array(); | |
| 433 | - | |
| 434 | - MainWP_DB_Client::instance()->update_selected_sites_for_client( $inserted->client_id, $selected_sites ); | |
| 435 | - | |
| 436 | - return array( | |
| 437 | - 'success' => true, | |
| 438 | - 'clientid' => $inserted->client_id, | |
| 439 | - ); | |
| 463 | + } catch ( \Exception $e ) { | |
| 464 | + return array( 'error' => $e->getMessage() ); | |
| 440 | 465 | } |
| 441 | 466 | } |
| 442 | 467 | |
| 443 | 468 | |
| @@ -529,9 +554,9 @@ | ||
| 529 | 554 | * @param array $item client|contact array data. |
| 530 | 555 | * @param string $type type of image. |
| 531 | 556 | * @param string $what image for what. |
| 532 | 557 | */ |
| 533 | - public static function get_client_contact_image( $item, $type = 'client', $what = 'default' ) { | |
| 558 | + public static function get_client_contact_image( $item, $type = 'client', $what = 'default' ) { // phpcs:ignore -- NOSONAR - complex | |
| 534 | 559 | |
| 535 | 560 | if ( empty( $item ) ) { |
| 536 | 561 | return ''; |
| 537 | 562 | } |
| @@ -553,18 +578,22 @@ | ||
| 553 | 578 | } |
| 554 | 579 | return $full_url; // return path to uploaded icon. |
| 555 | 580 | } |
| 556 | 581 | |
| 557 | - $img_cls = 'ui mini circular image'; | |
| 582 | + $img_cls = 'ui avatar image'; | |
| 558 | 583 | if ( 'card' === $what ) { |
| 559 | 584 | $img_cls = 'ui medium circular image'; |
| 560 | 585 | } |
| 561 | 586 | |
| 587 | + if ( 'small' === $what ) { | |
| 588 | + $img_cls = 'ui small circular image'; | |
| 589 | + } | |
| 590 | + | |
| 562 | 591 | $icon_wrapper_attr = ' class="' . $img_cls . '" style="height:auto;display:inline-block;" '; |
| 563 | 592 | if ( 'display_edit' === $what ) { |
| 564 | - $icon_wrapper_attr = ' id="mainwp_add_edit_client_upload_custom_icon" style="width:32px;height:auto;display:inline-block;" class="' . esc_attr( $img_cls ) . '" '; | |
| 593 | + $icon_wrapper_attr = ' id="mainwp_add_edit_client_upload_custom_icon" style="width:28px;height:auto;display:inline-block;" class="' . esc_attr( $img_cls ) . '" '; | |
| 565 | 594 | if ( 'contact' === $type ) { |
| 566 | - $icon_wrapper_attr = ' style="width:32px;height:auto;display:inline-block;" class="mainwp_add_edit_contact_upload_custom_icon ' . esc_attr( $img_cls ) . '" '; | |
| 595 | + $icon_wrapper_attr = ' style="width:28px;height:auto;display:inline-block;" class="mainwp_add_edit_contact_upload_custom_icon ' . esc_attr( $img_cls ) . '" '; | |
| 567 | 596 | } |
| 568 | 597 | } |
| 569 | 598 | |
| 570 | 599 | $img = ''; |
| @@ -569,10 +598,11 @@ | ||
| 569 | 598 | |
| 570 | 599 | $img = ''; |
| 571 | 600 | |
| 572 | 601 | if ( ! empty( $image_path ) && file_exists( $dirs[0] . $image_path ) ) { |
| 573 | - $full_url = $dirs[1] . $image_path; | |
| 574 | - $img = '<img ' . $icon_wrapper_attr . ' src="' . esc_attr( $full_url ) . '" alt="' . esc_attr( $item['name'] ) . '">'; | |
| 602 | + $full_url = $dirs[1] . $image_path; | |
| 603 | + $image_name = ! empty( $item['contact_name'] ) ? $item['contact_name'] : ''; | |
| 604 | + $img = '<img ' . $icon_wrapper_attr . ' src="' . esc_attr( $full_url ) . '" alt="' . esc_attr( $image_name ) . '">'; | |
| 575 | 605 | } elseif ( ! empty( $icon_info ) ) { |
| 576 | 606 | $img = MainWP_Client::get_cust_client_icon( $icon_info, 'display', $what ); |
| 577 | 607 | } |
| 578 | 608 | return $img; |
| @@ -577,9 +607,45 @@ | ||
| 577 | 607 | } |
| 578 | 608 | return $img; |
| 579 | 609 | } |
| 580 | 610 | |
| 611 | + /** | |
| 612 | + * Method get_client_avatar() | |
| 613 | + * | |
| 614 | + * Returns client avatar image. | |
| 615 | + * | |
| 616 | + * @param array $item client|contact array data. | |
| 617 | + * @param string $type type of image. | |
| 618 | + */ | |
| 619 | + public static function get_client_avatar( $item, $type = 'client' ) { // phpcs:ignore -- NOSONAR - complex | |
| 581 | 620 | |
| 621 | + if ( empty( $item ) ) { | |
| 622 | + return ''; | |
| 623 | + } | |
| 624 | + | |
| 625 | + $dirs = MainWP_System_Utility::get_mainwp_dir( 'client-images', true ); | |
| 626 | + | |
| 627 | + if ( 'client' === $type ) { | |
| 628 | + $image_path = $item['image']; | |
| 629 | + $icon_info = $item['selected_icon_info']; | |
| 630 | + } else { | |
| 631 | + $image_path = $item['contact_image']; | |
| 632 | + $icon_info = $item['contact_icon_info']; | |
| 633 | + } | |
| 634 | + | |
| 635 | + $img = ''; | |
| 636 | + | |
| 637 | + if ( ! empty( $image_path ) && file_exists( $dirs[0] . $image_path ) ) { | |
| 638 | + $full_url = $dirs[1] . $image_path; | |
| 639 | + $image_name = ! empty( $item['contact_name'] ) ? $item['contact_name'] : ''; | |
| 640 | + $img = '<img class="ui avatar image" src="' . esc_attr( $full_url ) . '" alt="' . esc_attr( $image_name ) . '">'; | |
| 641 | + } elseif ( ! empty( $icon_info ) ) { | |
| 642 | + $img = MainWP_Client::get_cust_client_icon( $icon_info, 'display' ); | |
| 643 | + } | |
| 644 | + return $img; | |
| 645 | + } | |
| 646 | + | |
| 647 | + | |
| 582 | 648 | /** |
| 583 | 649 | * Method get_client_image_url() |
| 584 | 650 | * |
| 585 | 651 | * Get client icon URL. |
| @@ -614,9 +680,12 @@ | ||
| 614 | 680 | $existed_emails = esc_html( $existed_emails ); |
| 615 | 681 | $existed_emails = str_replace( '|', '<br/>', $existed_emails ); |
| 616 | 682 | ?> |
| 617 | 683 | <div class="ui yellow message"> |
| 618 | - <?php printf( esc_html__( 'Existed contact emails.%sPlease try again.', 'mainwp' ), '<br/>' . $existed_emails . '</br>' ); // phpcs:ignore WordPress.Security.EscapeOutput ?> | |
| 684 | + <?php | |
| 685 | + /* translators: %s: Line break followed by existing email addresses */ | |
| 686 | + printf( esc_html__( 'Existed contact emails.%sPlease try again.', 'mainwp' ), '<br/>' . $existed_emails . '</br>' ); // phpcs:ignore WordPress.Security.EscapeOutput | |
| 687 | + ?> | |
| 619 | 688 | </div> |
| 620 | 689 | <?php |
| 621 | 690 | } |
| 622 | 691 | } |