| 1 |
<?php |
| 2 |
/** |
| 3 |
* Client Handler. |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class MainWP_Client_Handler |
| 12 |
* |
| 13 |
* @package MainWP\Dashboard |
| 14 |
* |
| 15 |
* @uses \MainWP\Dashboard\MainWP_Client_Handler |
| 16 |
*/ |
| 17 |
class MainWP_Client_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 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; |
| 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 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Get default client fields. |
| 46 |
* |
| 47 |
* Get default client fields. |
| 48 |
*/ |
| 49 |
public static function get_default_client_fields() { |
| 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 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get default client fields. |
| 138 |
* |
| 139 |
* Get default client fields. |
| 140 |
*/ |
| 141 |
public static function get_mini_default_client_fields() { |
| 142 |
|
| 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 |
} |
| 161 |
|
| 162 |
|
| 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 |
} |
| 212 |
|
| 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 |
} |
| 237 |
|
| 238 |
|
| 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. |
| 279 |
|
| 280 |
if ( empty( $data ) || ! is_array( $data ) ) { |
| 281 |
return array( 'error' => esc_html__( 'Invalid Client data. Please try again.', 'mainwp' ) ); |
| 282 |
} |
| 283 |
|
| 284 |
$params = array(); |
| 285 |
|
| 286 |
try { |
| 287 |
|
| 288 |
if ( $edit ) { |
| 289 |
|
| 290 |
$client_id = isset( $data['client_id'] ) ? intval( $data['client_id'] ) : 0; |
| 291 |
|
| 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 |
} |
| 297 |
|
| 298 |
$params['client_id'] = $client_id; |
| 299 |
|
| 300 |
if ( isset( $data['name'] ) && ! empty( trim( $data['name'] ) ) ) { |
| 301 |
$params['name'] = sanitize_text_field( wp_unslash( $data['name'] ) ); |
| 302 |
} |
| 303 |
|
| 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 |
} |
| 319 |
|
| 320 |
if ( isset( $data['country'] ) ) { |
| 321 |
$params['country'] = sanitize_text_field( wp_unslash( $data['country'] ) ); |
| 322 |
} |
| 323 |
|
| 324 |
if ( isset( $data['note'] ) ) { |
| 325 |
$params['note'] = sanitize_text_field( wp_unslash( $data['note'] ) ); |
| 326 |
} |
| 327 |
|
| 328 |
if ( isset( $data['client_email'] ) ) { |
| 329 |
$params['client_email'] = sanitize_text_field( wp_unslash( $data['client_email'] ) ); |
| 330 |
} |
| 331 |
|
| 332 |
if ( isset( $data['client_phone'] ) ) { |
| 333 |
$params['client_phone'] = sanitize_text_field( wp_unslash( $data['client_phone'] ) ); |
| 334 |
} |
| 335 |
|
| 336 |
if ( isset( $data['client_facebook'] ) ) { |
| 337 |
$params['client_facebook'] = sanitize_text_field( wp_unslash( $data['client_facebook'] ) ); |
| 338 |
} |
| 339 |
|
| 340 |
if ( isset( $data['client_twitter'] ) ) { |
| 341 |
$params['client_twitter'] = sanitize_text_field( wp_unslash( $data['client_twitter'] ) ); |
| 342 |
} |
| 343 |
|
| 344 |
if ( isset( $data['client_instagram'] ) ) { |
| 345 |
$params['client_instagram'] = sanitize_text_field( wp_unslash( $data['client_instagram'] ) ); |
| 346 |
} |
| 347 |
|
| 348 |
if ( isset( $data['client_linkedin'] ) ) { |
| 349 |
$params['client_linkedin'] = sanitize_text_field( wp_unslash( $data['client_linkedin'] ) ); |
| 350 |
} |
| 351 |
|
| 352 |
if ( isset( $data['created'] ) && is_numeric( $data['created'] ) ) { |
| 353 |
$params['created'] = intval( $data['created'] ); |
| 354 |
} |
| 355 |
|
| 356 |
MainWP_DB_Client::instance()->update_client( $params, true ); |
| 357 |
|
| 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 |
} |
| 362 |
|
| 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. |
| 384 |
|
| 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 |
|
| 401 |
return array( |
| 402 |
'success' => true, |
| 403 |
'clientid' => $client_id, |
| 404 |
); |
| 405 |
|
| 406 |
} else { |
| 407 |
|
| 408 |
$params['name'] = isset( $data['name'] ) ? sanitize_text_field( wp_unslash( $data['name'] ) ) : ''; |
| 409 |
|
| 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 |
} |
| 415 |
|
| 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(); |
| 430 |
|
| 431 |
$inserted = MainWP_DB_Client::instance()->update_client( $params, true ); |
| 432 |
|
| 433 |
if ( empty( $inserted ) ) { |
| 434 |
return array( |
| 435 |
'error' => esc_html__( 'Undefined error. Please try again.', 'mainwp' ), |
| 436 |
); |
| 437 |
} |
| 438 |
|
| 439 |
$selected_sites = ( isset( $data['selected_sites'] ) && is_array( $data['selected_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $data['selected_sites'] ) ) : array(); |
| 440 |
|
| 441 |
MainWP_DB_Client::instance()->update_selected_sites_for_client( $inserted->client_id, $selected_sites ); |
| 442 |
|
| 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 |
} |
| 452 |
|
| 453 |
|
| 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 |
} |
| 471 |
|
| 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 |
} |
| 633 |
} |
| 634 |
|