| 1 |
<?php |
| 2 |
/** |
| 3 |
* UsersWP tools functions |
| 4 |
* |
| 5 |
* All UsersWP tools related functions can be found here. |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @author GeoDirectory Team <info@wpgeodirectory.com> |
| 9 |
*/ |
| 10 |
class UsersWP_Tools { |
| 11 |
|
| 12 |
/** |
| 13 |
* Constructor. |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
add_filter('uwp_load_db_language', array($this,'load_custom_field_translation') ); |
| 17 |
add_filter('uwp_load_db_language', array($this,'load_uwp_options_text_translation') ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Fixes usermeta table |
| 22 |
* |
| 23 |
* @package userswp |
| 24 |
* |
| 25 |
*/ |
| 26 |
public function fix_usermeta_table() { |
| 27 |
uwp_create_tables(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Wraps message |
| 32 |
* |
| 33 |
* @package userswp |
| 34 |
* |
| 35 |
* @param string $message Message to wrap |
| 36 |
* @param string $class Class for wrapper |
| 37 |
* |
| 38 |
* @return string |
| 39 |
* |
| 40 |
*/ |
| 41 |
public function tools_wrap_error_message($message, $class) { |
| 42 |
ob_start(); |
| 43 |
?> |
| 44 |
<div class="notice inline notice-<?php echo esc_attr( $class ); ?> notice-alt"> |
| 45 |
<p><?php echo wp_kses_post( $message ); ?></p> |
| 46 |
</div> |
| 47 |
<?php |
| 48 |
$html = ob_get_clean(); |
| 49 |
return $html; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Fixes field columns |
| 54 |
* |
| 55 |
* @package userswp |
| 56 |
* |
| 57 |
* @return object|bool |
| 58 |
* |
| 59 |
*/ |
| 60 |
public function fix_field_columns() { |
| 61 |
global $wpdb; |
| 62 |
$errors = new WP_Error(); |
| 63 |
|
| 64 |
$form_type = 'account'; |
| 65 |
$table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 66 |
$fields = $wpdb->get_results($wpdb->prepare("SELECT htmlvar_name FROM " . $table_name . " WHERE form_type = %s ORDER BY sort_order ASC", array($form_type))); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 67 |
$meta_table = get_usermeta_table_prefix() . 'uwp_usermeta'; |
| 68 |
|
| 69 |
$excluded = uwp_get_excluded_fields(); |
| 70 |
|
| 71 |
if (!empty($fields)) { |
| 72 |
foreach ($fields as $field) { |
| 73 |
$htmlvar_name = $field->htmlvar_name; |
| 74 |
if (in_array($htmlvar_name, $excluded)) { |
| 75 |
continue; |
| 76 |
} |
| 77 |
$is_exists = uwp_column_exist($meta_table, $htmlvar_name); |
| 78 |
if (!$is_exists) { |
| 79 |
$meta_field_add = $this->uwp_sql_datatype_from_field($field); |
| 80 |
$add_result = uwp_add_column_if_not_exist($meta_table, $htmlvar_name, $meta_field_add); |
| 81 |
if ($add_result === false) { |
| 82 |
$errors->add('creation_failed', __('Column creation failed, you may have too many columns or the default value does not match with field data type.', 'userswp')); |
| 83 |
return $errors; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns columns for field |
| 93 |
* |
| 94 |
* @package userswp |
| 95 |
* |
| 96 |
* @return array |
| 97 |
* |
| 98 |
*/ |
| 99 |
public function get_field_columns() { |
| 100 |
global $wpdb; |
| 101 |
$form_type = 'account'; |
| 102 |
$table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 103 |
$fields = $wpdb->get_results($wpdb->prepare("SELECT htmlvar_name FROM " . $table_name . " WHERE form_type = %s ORDER BY sort_order ASC", array($form_type))); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 104 |
|
| 105 |
$excluded = uwp_get_excluded_fields(); |
| 106 |
|
| 107 |
$columns = array(); |
| 108 |
|
| 109 |
if (!empty($fields)) { |
| 110 |
foreach ($fields as $field) { |
| 111 |
$htmlvar_name = $field->htmlvar_name; |
| 112 |
if (in_array($htmlvar_name, $excluded)) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
$columns[] = $htmlvar_name; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return $columns; |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Fixes users meta |
| 125 |
* |
| 126 |
* @package userswp |
| 127 |
* |
| 128 |
* @param int $step Step for processing |
| 129 |
* |
| 130 |
*/ |
| 131 |
public function fix_usermeta($step) { |
| 132 |
|
| 133 |
global $wpdb; |
| 134 |
|
| 135 |
|
| 136 |
$items_per_page = apply_filters('tools_process_fix_usermeta_per_page', 10, $step); |
| 137 |
$offset = (int) $step * $items_per_page; |
| 138 |
|
| 139 |
$user_ids = $wpdb->get_col( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 140 |
"SELECT $wpdb->users.ID FROM $wpdb->users LIMIT %d OFFSET %d", |
| 141 |
$items_per_page, $offset )); |
| 142 |
|
| 143 |
$users_count = count_users(); |
| 144 |
$total_users = $users_count['total_users']; |
| 145 |
|
| 146 |
$max_step = ceil($total_users / $items_per_page) - 1; |
| 147 |
$percent = (($step + 1)/ ($max_step+1)) * 100; |
| 148 |
|
| 149 |
$columns = $this->get_field_columns(); |
| 150 |
|
| 151 |
//we got all the IDs, now loop through them to get individual IDs |
| 152 |
$count = 0; |
| 153 |
$message = ''; |
| 154 |
$table_sync_error = false; |
| 155 |
$done = false; |
| 156 |
$error = false; |
| 157 |
|
| 158 |
if ($step == 0) { |
| 159 |
$this->fix_usermeta_table(); |
| 160 |
|
| 161 |
$output = $this->fix_field_columns(); |
| 162 |
if (is_wp_error($output)) { |
| 163 |
$table_sync_error = true; |
| 164 |
$error = true; |
| 165 |
$message = $this->tools_wrap_error_message($output->get_error_message(), 'error'); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (!$table_sync_error) { |
| 170 |
foreach ( $user_ids as $user_id ) { |
| 171 |
|
| 172 |
// get user info by calling get_userdata() on each id |
| 173 |
$user_data = get_userdata($user_id); |
| 174 |
$usermeta = get_user_meta( $user_id, 'uwp_usermeta', true ); |
| 175 |
|
| 176 |
foreach ($columns as $column) { |
| 177 |
switch ($column) { |
| 178 |
case "username": |
| 179 |
$value = $user_data->user_login; |
| 180 |
break; |
| 181 |
case "email": |
| 182 |
$value = $user_data->user_email; |
| 183 |
break; |
| 184 |
case "first_name": |
| 185 |
$value = $user_data->first_name; |
| 186 |
break; |
| 187 |
case "last_name": |
| 188 |
$value = $user_data->last_name; |
| 189 |
break; |
| 190 |
case "bio": |
| 191 |
$value = $user_data->description; |
| 192 |
break; |
| 193 |
default: |
| 194 |
if ($usermeta === false) { |
| 195 |
$value = false; |
| 196 |
} else { |
| 197 |
$value = isset( $usermeta[ $column ] ) ? $usermeta[ $column ] : false; |
| 198 |
} |
| 199 |
|
| 200 |
} |
| 201 |
if ($value !== false) { |
| 202 |
uwp_update_usermeta($user_id, $column, $value); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
$count++; |
| 207 |
} |
| 208 |
|
| 209 |
if ($step >= $max_step) { |
| 210 |
$done = true; |
| 211 |
$message = __("Processed Successfully", 'userswp'); |
| 212 |
$message = $this->tools_wrap_error_message($message, 'success'); |
| 213 |
} else { |
| 214 |
$done = false; |
| 215 |
$step = $step + 1; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
$output = array( |
| 220 |
'done' => $done, |
| 221 |
'error' => $error, |
| 222 |
'message' => $message, |
| 223 |
'step' => $step, |
| 224 |
'percent' => intval($percent) |
| 225 |
); |
| 226 |
echo json_encode($output); |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Exports DB texts for translation. |
| 232 |
* |
| 233 |
* @package userswp |
| 234 |
* |
| 235 |
* @param int $step Step for processing |
| 236 |
* |
| 237 |
* @return bool |
| 238 |
* |
| 239 |
*/ |
| 240 |
public function export_db_texts($step){ |
| 241 |
|
| 242 |
$error = false; |
| 243 |
$percent = 100; |
| 244 |
|
| 245 |
$wp_filesystem = UsersWP_Files::uwp_init_filesystem(); |
| 246 |
|
| 247 |
$language_file = USERSWP_PATH . 'db-language.php'; |
| 248 |
|
| 249 |
if ( is_file( $language_file ) && ! is_writable( $language_file ) ) { |
| 250 |
return false; |
| 251 |
} // Not possible to create. |
| 252 |
|
| 253 |
if ( ! is_file( $language_file ) && ! is_writable( dirname( $language_file ) ) ) { |
| 254 |
return false; |
| 255 |
} // Not possible to create. |
| 256 |
|
| 257 |
$contents_strings = array(); |
| 258 |
|
| 259 |
/** |
| 260 |
* Filter the language string from database to translate via po editor |
| 261 |
* |
| 262 |
* @since 1.2.2 |
| 263 |
* |
| 264 |
* @param array $contents_strings Array of strings. |
| 265 |
*/ |
| 266 |
$contents_strings = apply_filters( 'uwp_load_db_language', $contents_strings ); |
| 267 |
|
| 268 |
$contents_strings = array_unique( $contents_strings ); |
| 269 |
|
| 270 |
$contents_head = array(); |
| 271 |
$contents_head[] = "<?php"; |
| 272 |
$contents_head[] = "/**"; |
| 273 |
$contents_head[] = " * Translate language string stored in database. Ex: Custom Fields"; |
| 274 |
$contents_head[] = " *"; |
| 275 |
$contents_head[] = " * @package userswp"; |
| 276 |
$contents_head[] = " * @since ".USERSWP_VERSION; |
| 277 |
$contents_head[] = " */"; |
| 278 |
$contents_head[] = ""; |
| 279 |
|
| 280 |
$contents_foot = array(); |
| 281 |
$contents_foot[] = ""; |
| 282 |
$contents_foot[] = ""; |
| 283 |
|
| 284 |
$contents = implode( PHP_EOL, $contents_head ); |
| 285 |
|
| 286 |
if ( ! empty( $contents_strings ) ) { |
| 287 |
foreach ( $contents_strings as $string ) { |
| 288 |
if ( is_scalar( $string ) && $string != '' ) { |
| 289 |
$string = str_replace( "'", "\'", $string ); |
| 290 |
|
| 291 |
do_action( 'uwp_language_file_add_string', $string ); |
| 292 |
|
| 293 |
$contents .= PHP_EOL . "__('" . $string . "', 'userswp');"; |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
$contents .= implode( PHP_EOL, $contents_foot ); |
| 299 |
|
| 300 |
if ( ! $wp_filesystem->put_contents( $language_file, $contents, FS_CHMOD_FILE ) ) { |
| 301 |
return false; |
| 302 |
} // Failure; could not write file. |
| 303 |
|
| 304 |
$done = true; |
| 305 |
$message = __("Processed Successfully", 'userswp'); |
| 306 |
$message = $this->tools_wrap_error_message($message, 'success'); |
| 307 |
|
| 308 |
$output = array( |
| 309 |
'done' => $done, |
| 310 |
'error' => $error, |
| 311 |
'message' => $message, |
| 312 |
'step' => $step, |
| 313 |
'percent' => intval($percent) |
| 314 |
); |
| 315 |
echo json_encode($output); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Get the custom fields texts for translation |
| 320 |
* |
| 321 |
* @since 1.2.2 |
| 322 |
* @package userswp |
| 323 |
* |
| 324 |
* @global object $wpdb WordPress database abstraction object. |
| 325 |
* |
| 326 |
* @param array $translation_texts Array of text strings. |
| 327 |
* |
| 328 |
* @return array Translation texts. |
| 329 |
*/ |
| 330 |
public function load_custom_field_translation( $translation_texts = array() ) { |
| 331 |
global $wpdb; |
| 332 |
|
| 333 |
$table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 334 |
// Custom fields table |
| 335 |
$sql = "SELECT site_title, form_label, help_text, required_msg, default_value, option_values, validation_msg FROM " . $table_name . " where form_type = 'account'"; |
| 336 |
$rows = $wpdb->get_results( $sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 337 |
|
| 338 |
if ( ! empty( $rows ) ) { |
| 339 |
foreach ( $rows as $row ) { |
| 340 |
if ( ! empty( $row->site_title ) ) { |
| 341 |
$translation_texts[] = stripslashes_deep( $row->site_title ); |
| 342 |
} |
| 343 |
|
| 344 |
if ( ! empty( $row->form_label ) ) { |
| 345 |
$translation_texts[] = stripslashes_deep( $row->form_label ); |
| 346 |
} |
| 347 |
|
| 348 |
if ( ! empty( $row->help_text ) ) { |
| 349 |
$translation_texts[] = stripslashes_deep( $row->help_text ); |
| 350 |
} |
| 351 |
|
| 352 |
if ( ! empty( $row->required_msg ) ) { |
| 353 |
$translation_texts[] = stripslashes_deep( $row->required_msg ); |
| 354 |
} |
| 355 |
|
| 356 |
if ( ! empty( $row->validation_msg ) ) { |
| 357 |
$translation_texts[] = stripslashes_deep( $row->validation_msg ); |
| 358 |
} |
| 359 |
|
| 360 |
if ( ! empty( $row->default_value ) ) { |
| 361 |
$translation_texts[] = stripslashes_deep( $row->default_value ); |
| 362 |
} |
| 363 |
|
| 364 |
if ( ! empty( $row->placeholder_value ) ) { |
| 365 |
$translation_texts[] = stripslashes_deep( $row->placeholder_value ); |
| 366 |
} |
| 367 |
|
| 368 |
if ( ! empty( $row->option_values ) ) { |
| 369 |
$option_values = uwp_string_values_to_options( stripslashes_deep( $row->option_values ) ); |
| 370 |
|
| 371 |
if ( ! empty( $option_values ) ) { |
| 372 |
foreach ( $option_values as $option_value ) { |
| 373 |
if ( ! empty( $option_value['label'] ) ) { |
| 374 |
$translation_texts[] = $option_value['label']; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
$translation_texts = ! empty( $translation_texts ) ? array_unique( $translation_texts ) : $translation_texts; |
| 383 |
|
| 384 |
return $translation_texts; |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Get the userswp notification subject & content texts for translation. |
| 389 |
* |
| 390 |
* @since 1.2.2 |
| 391 |
* @package userswp |
| 392 |
* |
| 393 |
* @param array $translation_texts Array of text strings. |
| 394 |
* @return array Translation texts. |
| 395 |
*/ |
| 396 |
public function load_uwp_options_text_translation($translation_texts = array()) { |
| 397 |
$translation_texts = !empty( $translation_texts ) && is_array( $translation_texts ) ? $translation_texts : array(); |
| 398 |
|
| 399 |
$uwp_options = array( |
| 400 |
'register_link_title', |
| 401 |
'forgot_link_title', |
| 402 |
'login_link_title', |
| 403 |
'profile_link_title', |
| 404 |
'account_link_title', |
| 405 |
'author_box_content', |
| 406 |
'author_box_content_bootstrap', |
| 407 |
'email_name', |
| 408 |
'email_footer_text', |
| 409 |
'registration_activate_email_subject', |
| 410 |
'registration_activate_email_content', |
| 411 |
'registration_success_email_subject', |
| 412 |
'registration_success_email_content', |
| 413 |
'forgot_password_email_subject', |
| 414 |
'forgot_password_email_content', |
| 415 |
'change_password_email_subject', |
| 416 |
'change_password_email_content', |
| 417 |
'reset_password_email_subject', |
| 418 |
'reset_password_email_content', |
| 419 |
'account_update_email_subject', |
| 420 |
'account_update_email_content', |
| 421 |
'account_delete_email_subject', |
| 422 |
'account_delete_email_content', |
| 423 |
'registration_success_email_subject_admin', |
| 424 |
'registration_success_email_content_admin', |
| 425 |
'account_delete_email_subject_admin', |
| 426 |
'account_delete_email_content_admin', |
| 427 |
'wp_new_user_notification_email_subject', |
| 428 |
'wp_new_user_notification_email_content', |
| 429 |
'wp_new_user_notification_email_subject_admin', |
| 430 |
'wp_new_user_notification_email_content_admin', |
| 431 |
'account_new_email_activation_email', |
| 432 |
'account_new_email_activation_email_subject', |
| 433 |
'account_new_email_activation_email_content', |
| 434 |
); |
| 435 |
|
| 436 |
/** |
| 437 |
* Filters the userswp option names that requires to add for translation. |
| 438 |
* |
| 439 |
* @since 1.2.2 |
| 440 |
* @package userswp |
| 441 |
* |
| 442 |
* @param array $uwp_options Array of option names. |
| 443 |
*/ |
| 444 |
$uwp_options = apply_filters('uwp_options_for_translation', $uwp_options); |
| 445 |
$uwp_options = array_unique($uwp_options); |
| 446 |
|
| 447 |
if (!empty($uwp_options)) { |
| 448 |
foreach ($uwp_options as $uwp_option) { |
| 449 |
if ($uwp_option != '' && $option_value = uwp_get_option($uwp_option)) { |
| 450 |
$option_value = is_string($option_value) ? stripslashes_deep($option_value) : ''; |
| 451 |
|
| 452 |
if ($option_value != '' && !in_array($option_value, $translation_texts)) { |
| 453 |
$translation_texts[] = stripslashes_deep($option_value); |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
$translation_texts = !empty($translation_texts) ? array_unique($translation_texts) : $translation_texts; |
| 460 |
|
| 461 |
return $translation_texts; |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Returns SQL data type for field |
| 466 |
* |
| 467 |
* @package userswp |
| 468 |
* |
| 469 |
* @param object $field Field object |
| 470 |
* |
| 471 |
* @return string |
| 472 |
* |
| 473 |
*/ |
| 474 |
public function uwp_sql_datatype_from_field($field) { |
| 475 |
|
| 476 |
switch ($field->field_type) { |
| 477 |
case 'checkbox': |
| 478 |
$data_type = 'TINYINT'; |
| 479 |
|
| 480 |
$meta_field_add = $data_type . "( 1 ) NOT NULL "; |
| 481 |
if ((int)$field->default_value === 1) { |
| 482 |
$meta_field_add .= " DEFAULT '1'"; |
| 483 |
} |
| 484 |
break; |
| 485 |
case 'multiselect': |
| 486 |
case 'select': |
| 487 |
$data_type = 'VARCHAR'; |
| 488 |
$op_size = '500'; |
| 489 |
$meta_field_add = $data_type . "( $op_size ) NULL "; |
| 490 |
if ($field->default_value != '') { |
| 491 |
$meta_field_add .= " DEFAULT '" . $field->default_value . "'"; |
| 492 |
} |
| 493 |
break; |
| 494 |
case 'textarea': |
| 495 |
case 'html': |
| 496 |
case 'url': |
| 497 |
case 'file': |
| 498 |
$data_type = 'TEXT'; |
| 499 |
$meta_field_add = $data_type . " NULL "; |
| 500 |
break; |
| 501 |
case 'datepicker': |
| 502 |
$data_type = 'DATE'; |
| 503 |
$meta_field_add = $data_type . " NULL "; |
| 504 |
break; |
| 505 |
case 'time': |
| 506 |
$data_type = 'TIME'; |
| 507 |
$meta_field_add = $data_type . " NULL "; |
| 508 |
break; |
| 509 |
default: |
| 510 |
$data_type = $field->data_type; |
| 511 |
$decimal_point = $field->decimal_point; |
| 512 |
$default_value = $field->default_value; |
| 513 |
if ($data_type != 'VARCHAR' && $data_type != '') { |
| 514 |
$meta_field_add = $data_type . " NULL "; |
| 515 |
|
| 516 |
if ($data_type == 'FLOAT' && $decimal_point > 0) { |
| 517 |
$meta_field_add = "DECIMAL(11, " . (int)$decimal_point . ") NULL "; |
| 518 |
} |
| 519 |
|
| 520 |
if (is_numeric($default_value) && $default_value != '') { |
| 521 |
$meta_field_add .= " DEFAULT '" . $default_value . "'"; |
| 522 |
} |
| 523 |
} else { |
| 524 |
$meta_field_add = " VARCHAR( 254 ) NULL "; |
| 525 |
|
| 526 |
if ($default_value != '') { |
| 527 |
$meta_field_add .= " DEFAULT '" . $default_value . "'"; |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
return $meta_field_add; |
| 533 |
} |
| 534 |
|
| 535 |
/** |
| 536 |
* Process add or remove dummy users |
| 537 |
* |
| 538 |
* @package userswp |
| 539 |
* |
| 540 |
* @param int $step Current step |
| 541 |
* @param string $type Action type add or remove |
| 542 |
* |
| 543 |
*/ |
| 544 |
public function uwp_tools_process_dummy_users($step, $type = 'add') { |
| 545 |
$items_per_page = apply_filters('tools_process_dummy_users_per_page', 10, $step, $type); |
| 546 |
$offset = (int) $step * $items_per_page; |
| 547 |
$message = ''; |
| 548 |
$error = false; |
| 549 |
$percent = 100; |
| 550 |
$max_step = 0; |
| 551 |
if ('add' == $type) { |
| 552 |
$users_data = $this->uwp_dummy_users_data(); |
| 553 |
$total_users = count($users_data); |
| 554 |
$max_step = ceil($total_users / $items_per_page) - 1; |
| 555 |
$percent = (($step + 1)/ ($max_step+1)) * 100; |
| 556 |
$dummy_users = array_slice($users_data, $offset, $items_per_page, true); |
| 557 |
foreach ( $dummy_users as $user ) { |
| 558 |
if ( username_exists( $user['login'] ) ) { |
| 559 |
continue; |
| 560 |
} |
| 561 |
$name = explode( ' ', $user['display_name'] ); |
| 562 |
$user_id = wp_insert_user( array( |
| 563 |
'user_login' => $user['login'], |
| 564 |
'user_pass' => $user['pass'], |
| 565 |
'first_name' => isset( $name[0] ) ? $name[0] : '', |
| 566 |
'last_name' => isset( $name[1] ) ? $name[1] : '', |
| 567 |
'display_name' => $user['display_name'], |
| 568 |
'user_email' => $user['email'], |
| 569 |
'user_registered' => uwp_get_random_date( 45, 1 ), |
| 570 |
) ); |
| 571 |
update_user_meta( $user_id, 'uwp_dummy_user', '1' ); |
| 572 |
} |
| 573 |
} |
| 574 |
if ('remove' == $type) { |
| 575 |
$dummy_users = get_users( array( 'meta_key' => 'uwp_dummy_user', 'meta_value' => '1', 'fields' => array( 'ID' )) ); |
| 576 |
$max_step = $step; |
| 577 |
foreach ( $dummy_users as $user ) { |
| 578 |
wp_delete_user($user->ID); |
| 579 |
} |
| 580 |
} |
| 581 |
if ($step >= $max_step) { |
| 582 |
$done = true; |
| 583 |
$message = __("Processed Successfully", 'userswp'); |
| 584 |
$message = $this->tools_wrap_error_message($message, 'success'); |
| 585 |
} else { |
| 586 |
$done = false; |
| 587 |
$step = $step + 1; |
| 588 |
} |
| 589 |
$output = array( |
| 590 |
'done' => $done, |
| 591 |
'error' => $error, |
| 592 |
'message' => $message, |
| 593 |
'step' => $step, |
| 594 |
'percent' => intval($percent) |
| 595 |
); |
| 596 |
echo json_encode($output); |
| 597 |
|
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Get a site specific password for dummy users. |
| 602 |
* |
| 603 |
* @return string |
| 604 |
*/ |
| 605 |
private static function get_dummy_user_passowrd(){ |
| 606 |
return substr(hash( 'SHA256', AUTH_KEY . site_url() ), 0, 15); |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* Returns array of dummy users |
| 611 |
* |
| 612 |
* @package userswp |
| 613 |
* |
| 614 |
* @return array |
| 615 |
* |
| 616 |
*/ |
| 617 |
public function uwp_dummy_users_data() { |
| 618 |
|
| 619 |
return array( |
| 620 |
0 => array( |
| 621 |
'login' => 'antawn', |
| 622 |
'pass' => self::get_dummy_user_passowrd(), |
| 623 |
'display_name' => 'Antawn Jamison', |
| 624 |
'email' => 'uwp.dummy.user+1@gmail.com', |
| 625 |
), |
| 626 |
1 => array( |
| 627 |
'login' => 'chynna', |
| 628 |
'pass' => self::get_dummy_user_passowrd(), |
| 629 |
'display_name' => 'Chynna Phillips', |
| 630 |
'email' => 'uwp.dummy.user+2@gmail.com', |
| 631 |
), |
| 632 |
2 => array( |
| 633 |
'login' => 'kiki', |
| 634 |
'pass' => self::get_dummy_user_passowrd(), |
| 635 |
'display_name' => 'Kiki Cuyler', |
| 636 |
'email' => 'uwp.dummy.user+3@gmail.com', |
| 637 |
), |
| 638 |
3 => array( |
| 639 |
'login' => 'malivai', |
| 640 |
'pass' => self::get_dummy_user_passowrd(), |
| 641 |
'display_name' => 'MaliVai Washington', |
| 642 |
'email' => 'uwp.dummy.user+4@gmail.com', |
| 643 |
), |
| 644 |
4 => array( |
| 645 |
'login' => 'matraca', |
| 646 |
'pass' => self::get_dummy_user_passowrd(), |
| 647 |
'display_name' => 'Matraca Berg', |
| 648 |
'email' => 'uwp.dummy.user+5@gmail.com', |
| 649 |
), |
| 650 |
5 => array( |
| 651 |
'login' => 'ron', |
| 652 |
'pass' => self::get_dummy_user_passowrd(), |
| 653 |
'display_name' => 'Ron Faucheux', |
| 654 |
'email' => 'uwp.dummy.user+6@gmail.com', |
| 655 |
), |
| 656 |
6 => array( |
| 657 |
'login' => 'michellie', |
| 658 |
'pass' => self::get_dummy_user_passowrd(), |
| 659 |
'display_name' => 'Michellie Jones', |
| 660 |
'email' => 'uwp.dummy.user+7@gmail.com', |
| 661 |
), |
| 662 |
7 => array( |
| 663 |
'login' => 'monta', |
| 664 |
'pass' => self::get_dummy_user_passowrd(), |
| 665 |
'display_name' => 'Monta Ellis', |
| 666 |
'email' => 'uwp.dummy.user+8@gmail.com', |
| 667 |
), |
| 668 |
8 => array( |
| 669 |
'login' => 'picabo', |
| 670 |
'pass' => self::get_dummy_user_passowrd(), |
| 671 |
'display_name' => 'Picabo Street', |
| 672 |
'email' => 'uwp.dummy.user+9@gmail.com', |
| 673 |
), |
| 674 |
9 => array( |
| 675 |
'login' => 'ralph', |
| 676 |
'pass' => self::get_dummy_user_passowrd(), |
| 677 |
'display_name' => 'Ralph Fiennes', |
| 678 |
'email' => 'uwp.dummy.user+10@gmail.com', |
| 679 |
), |
| 680 |
10 => array( |
| 681 |
'login' => 'seamus', |
| 682 |
'pass' => self::get_dummy_user_passowrd(), |
| 683 |
'display_name' => 'Seamus', |
| 684 |
'email' => 'uwp.dummy.user+11@gmail.com', |
| 685 |
), |
| 686 |
11 => array( |
| 687 |
'login' => 'shan', |
| 688 |
'pass' => self::get_dummy_user_passowrd(), |
| 689 |
'display_name' => 'Shan Foster', |
| 690 |
'email' => 'uwp.dummy.user+12@gmail.com', |
| 691 |
), |
| 692 |
12 => array( |
| 693 |
'login' => 'siobhan', |
| 694 |
'pass' => self::get_dummy_user_passowrd(), |
| 695 |
'display_name' => 'Siobhan', |
| 696 |
'email' => 'uwp.dummy.user+13@gmail.com', |
| 697 |
), |
| 698 |
13 => array( |
| 699 |
'login' => 'stephen', |
| 700 |
'pass' => self::get_dummy_user_passowrd(), |
| 701 |
'display_name' => 'Stephen Curry', |
| 702 |
'email' => 'uwp.dummy.user+14@gmail.com', |
| 703 |
), |
| 704 |
14 => array( |
| 705 |
'login' => 'wynonna', |
| 706 |
'pass' => self::get_dummy_user_passowrd(), |
| 707 |
'display_name' => 'Wynonna Judd', |
| 708 |
'email' => 'uwp.dummy.user+15@gmail.com', |
| 709 |
), |
| 710 |
15 => array( |
| 711 |
'login' => 'john', |
| 712 |
'pass' => self::get_dummy_user_passowrd(), |
| 713 |
'display_name' => 'John Caius', |
| 714 |
'email' => 'uwp.dummy.user+16@gmail.com', |
| 715 |
), |
| 716 |
16 => array( |
| 717 |
'login' => 'thomas', |
| 718 |
'pass' => self::get_dummy_user_passowrd(), |
| 719 |
'display_name' => 'Thomas Carew', |
| 720 |
'email' => 'uwp.dummy.user+17@gmail.com', |
| 721 |
), |
| 722 |
17 => array( |
| 723 |
'login' => 'jason', |
| 724 |
'pass' => self::get_dummy_user_passowrd(), |
| 725 |
'display_name' => 'Jason Chaffetz', |
| 726 |
'email' => 'uwp.dummy.user+18@gmail.com', |
| 727 |
), |
| 728 |
18 => array( |
| 729 |
'login' => 'mamah', |
| 730 |
'pass' => self::get_dummy_user_passowrd(), |
| 731 |
'display_name' => 'Mamah Cheney', |
| 732 |
'email' => 'uwp.dummy.user+19@gmail.com', |
| 733 |
), |
| 734 |
19 => array( |
| 735 |
'login' => 'cecelia', |
| 736 |
'pass' => self::get_dummy_user_passowrd(), |
| 737 |
'display_name' => 'Cecelia Cichan ', |
| 738 |
'email' => 'uwp.dummy.user+20@gmail.com', |
| 739 |
), |
| 740 |
20 => array( |
| 741 |
'login' => 'dan', |
| 742 |
'pass' => self::get_dummy_user_passowrd(), |
| 743 |
'display_name' => 'Dan Cortese ', |
| 744 |
'email' => 'uwp.dummy.user+21@gmail.com', |
| 745 |
), |
| 746 |
21 => array( |
| 747 |
'login' => 'vernon', |
| 748 |
'pass' => self::get_dummy_user_passowrd(), |
| 749 |
'display_name' => 'Vernon Dahmer', |
| 750 |
'email' => 'uwp.dummy.user+22@gmail.com', |
| 751 |
), |
| 752 |
22 => array( |
| 753 |
'login' => 'andre', |
| 754 |
'pass' => self::get_dummy_user_passowrd(), |
| 755 |
'display_name' => 'Andre Dubus', |
| 756 |
'email' => 'uwp.dummy.user+23@gmail.com', |
| 757 |
), |
| 758 |
23 => array( |
| 759 |
'login' => 'justin', |
| 760 |
'pass' => self::get_dummy_user_passowrd(), |
| 761 |
'display_name' => 'Justin Duchscherer', |
| 762 |
'email' => 'uwp.dummy.user+24@gmail.com', |
| 763 |
), |
| 764 |
24 => array( |
| 765 |
'login' => 'keir', |
| 766 |
'pass' => self::get_dummy_user_passowrd(), |
| 767 |
'display_name' => 'Keir Dullea ', |
| 768 |
'email' => 'uwp.dummy.user+25@gmail.com', |
| 769 |
), |
| 770 |
); |
| 771 |
|
| 772 |
} |
| 773 |
|
| 774 |
/** |
| 775 |
* Outputs tools form |
| 776 |
* |
| 777 |
* @package userswp |
| 778 |
* |
| 779 |
*/ |
| 780 |
public static function output() { |
| 781 |
ob_start(); |
| 782 |
?> |
| 783 |
<div class="wrap userswp"> |
| 784 |
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
| 785 |
<table class="uwp-tools-table widefat"> |
| 786 |
<tbody> |
| 787 |
|
| 788 |
<?php if (defined('USERSWP_VERSION')) { |
| 789 |
do_action('uwp_tools_output_start'); |
| 790 |
?> |
| 791 |
<tr> |
| 792 |
<th> |
| 793 |
<strong class="tool-name"><?php esc_html_e('Clear version numbers', 'userswp');?></strong> |
| 794 |
<p class="tool-description"><?php esc_html_e('This will force install/upgrade functions to run.', 'userswp');?></p> |
| 795 |
</th> |
| 796 |
<td class="run-tool"> |
| 797 |
<input type="button" value="<?php esc_attr_e('Run', 'userswp');?>" class="button-primary uwp_diagnosis_button" data-diagnose="clear_version_numbers"/> |
| 798 |
</td> |
| 799 |
</tr> |
| 800 |
|
| 801 |
<tr> |
| 802 |
<td colspan="2" class="has-pbar"> |
| 803 |
<div id="uwp_diagnose_pb_clear_version_numbers" class="uwp-pb-wrapper"> |
| 804 |
<div class="progressBar" style="display: none;"><div></div></div> |
| 805 |
</div> |
| 806 |
<div id="uwp_diagnose_clear_version_numbers"></div> |
| 807 |
</td> |
| 808 |
</tr> |
| 809 |
|
| 810 |
<tr> |
| 811 |
<th> |
| 812 |
<strong class="tool-name"><?php esc_html_e('Fix User Data', 'userswp');?></strong> |
| 813 |
<p class="tool-description"><?php esc_html_e('Fixes User Data if you were using the Beta version.', 'userswp');?></p> |
| 814 |
</th> |
| 815 |
<td class="run-tool"> |
| 816 |
<input type="button" value="<?php esc_attr_e('Run', 'userswp');?>" class="button-primary uwp_diagnosis_button" data-diagnose="fix_user_data"/> |
| 817 |
</td> |
| 818 |
</tr> |
| 819 |
|
| 820 |
<tr> |
| 821 |
<td colspan="2" class="has-pbar"> |
| 822 |
<div id="uwp_diagnose_pb_fix_user_data" class="uwp-pb-wrapper"> |
| 823 |
<div class="progressBar" style="display: none;"><div></div></div> |
| 824 |
</div> |
| 825 |
<div id="uwp_diagnose_fix_user_data"></div> |
| 826 |
</td> |
| 827 |
</tr> |
| 828 |
|
| 829 |
<tr> |
| 830 |
<th> |
| 831 |
<strong class="tool-name"><?php esc_html_e('DB text translation', 'userswp');?></strong> |
| 832 |
<p class="tool-description"><?php esc_html_e('This tool will collect any texts stored in the DB and put them in the file db-language.php so they can then be used to translate them by translations tools.', 'userswp');?></p> |
| 833 |
</th> |
| 834 |
<td class="run-tool"> |
| 835 |
<input type="button" value="<?php esc_attr_e('Run', 'userswp');?>" class="button-primary uwp_diagnosis_button" data-diagnose="export_db_texts"/> |
| 836 |
</td> |
| 837 |
</tr> |
| 838 |
|
| 839 |
<tr> |
| 840 |
<td colspan="2" class="has-pbar"> |
| 841 |
<div id="uwp_diagnose_pb_export_db_texts" class="uwp-pb-wrapper"> |
| 842 |
<div class="progressBar" style="display: none;"><div></div></div> |
| 843 |
</div> |
| 844 |
<div id="uwp_diagnose_export_db_texts"></div> |
| 845 |
</td> |
| 846 |
</tr> |
| 847 |
|
| 848 |
<tr> |
| 849 |
<th> |
| 850 |
<strong class="tool-name"><?php esc_html_e('Dummy Users', 'userswp');?></strong> |
| 851 |
<p class="tool-description"><?php esc_html_e('Dummy Users for Testing. Password for all dummy users:', 'userswp'); echo " " . esc_html( self::get_dummy_user_passowrd() );?></p> |
| 852 |
</th> |
| 853 |
<td class="run-tool"> |
| 854 |
<?php |
| 855 |
$dummy_users = get_users( array( 'meta_key' => 'uwp_dummy_user', 'meta_value' => '1', 'fields' => array( 'ID' ) ) ); |
| 856 |
$total_dummy_users = !empty( $dummy_users ) ? count($dummy_users) : 0; |
| 857 |
?> |
| 858 |
<input style="display: <?php echo ( $total_dummy_users > 0 ) ? 'none' :'block'; ?>" type="button" value="<?php esc_attr_e('Create', 'userswp');?>" class="button-primary uwp_diagnosis_button uwp_add_dummy_users_button" data-diagnose="add_dummy_users"/> |
| 859 |
<input style="display: <?php echo ( $total_dummy_users > 0 ) ? 'block' :'none'; ?>" type="button" value="<?php esc_attr_e('Remove', 'userswp');?>" class="button-primary uwp_diagnosis_button uwp_remove_dummy_users_button" data-diagnose="remove_dummy_users"/> |
| 860 |
</td> |
| 861 |
</tr> |
| 862 |
|
| 863 |
<tr> |
| 864 |
<td colspan="2" class="has-pbar"> |
| 865 |
<div id="uwp_diagnose_pb_add_dummy_users" class="uwp-pb-wrapper"> |
| 866 |
<div class="progressBar" style="display: none;"><div></div></div> |
| 867 |
</div> |
| 868 |
<div id="uwp_diagnose_add_dummy_users"></div> |
| 869 |
</td> |
| 870 |
</tr> |
| 871 |
|
| 872 |
<tr> |
| 873 |
<td colspan="2" class="has-pbar"> |
| 874 |
<div id="uwp_diagnose_pb_remove_dummy_users" class="uwp-pb-wrapper"> |
| 875 |
<div class="progressBar" style="display: none;"><div></div></div> |
| 876 |
</div> |
| 877 |
<div id="uwp_diagnose_remove_dummy_users"></div> |
| 878 |
</td> |
| 879 |
</tr> |
| 880 |
|
| 881 |
<?php |
| 882 |
do_action('uwp_tools_output_end'); |
| 883 |
} ?> |
| 884 |
|
| 885 |
</tbody> |
| 886 |
</table> |
| 887 |
</div> |
| 888 |
|
| 889 |
<script type="text/javascript"> |
| 890 |
(function( $, window, undefined ) { |
| 891 |
$(document).ready(function () { |
| 892 |
$('.uwp_diagnosis_button').click(function (e) { |
| 893 |
e.preventDefault(); |
| 894 |
var type = $(this).data('diagnose'); |
| 895 |
$(this).hide(); |
| 896 |
jQuery("#uwp_diagnose_add_dummy_users,#uwp_diagnose_remove_dummy_users").html(''); |
| 897 |
$("#uwp_diagnose_pb_" + type).find('.progressBar').show().progressbar({value: 0}); |
| 898 |
uwp_process_diagnose_step( 0, type ); |
| 899 |
|
| 900 |
}); |
| 901 |
}); |
| 902 |
}( jQuery, window )); |
| 903 |
|
| 904 |
function uwp_process_diagnose_step(step, type) { |
| 905 |
jQuery.ajax({ |
| 906 |
url: ajaxurl, |
| 907 |
type: 'POST', |
| 908 |
dataType: 'json', |
| 909 |
data: { |
| 910 |
action: 'uwp_process_diagnosis', |
| 911 |
step: step, |
| 912 |
type: type, |
| 913 |
security: '<?php echo esc_js( wp_create_nonce('uwp_process_diagnosis') ); ?>', |
| 914 |
}, |
| 915 |
beforeSend: function() {}, |
| 916 |
success: function(response, textStatus, xhr) { |
| 917 |
if(response.done === true || response.error === true ) { |
| 918 |
tools_progress(response.percent, type); |
| 919 |
setTimeout(function(){ |
| 920 |
jQuery("#uwp_diagnose_pb_" + type).find('.progressBar').hide(); |
| 921 |
jQuery("#uwp_diagnose_" + type).html(response.message); |
| 922 |
if( 'add_dummy_users' === type ) { |
| 923 |
jQuery('.uwp_remove_dummy_users_button').show(); |
| 924 |
jQuery('.uwp_add_dummy_users_button').hide(); |
| 925 |
} else{ |
| 926 |
jQuery('.uwp_add_dummy_users_button').show(); |
| 927 |
jQuery('.uwp_remove_dummy_users_button').hide(); |
| 928 |
} |
| 929 |
}, 1500); |
| 930 |
} else { |
| 931 |
tools_progress(response.percent, type); |
| 932 |
setTimeout(function(){ |
| 933 |
uwp_process_diagnose_step(parseInt( response.step ), type) |
| 934 |
}, 500); |
| 935 |
} |
| 936 |
}, |
| 937 |
error: function(xhr, textStatus, errorThrown) { |
| 938 |
alert(textStatus); |
| 939 |
} |
| 940 |
}); // end of ajax |
| 941 |
|
| 942 |
|
| 943 |
function tools_progress(percent, type) { |
| 944 |
$element = jQuery("#uwp_diagnose_pb_" + type).find('.progressBar'); |
| 945 |
var progressBarWidth = percent * $element.width() / 100; |
| 946 |
$element.find('div').animate({ width: progressBarWidth }, 500).html(percent + "% "); |
| 947 |
} |
| 948 |
} |
| 949 |
</script> |
| 950 |
<?php |
| 951 |
|
| 952 |
echo ob_get_clean(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 953 |
} |
| 954 |
|
| 955 |
/** |
| 956 |
* Process diagnosis AJAX call |
| 957 |
*/ |
| 958 |
public function uwp_process_diagnosis_ajax() { |
| 959 |
|
| 960 |
if (!is_user_logged_in()) { |
| 961 |
return; |
| 962 |
} |
| 963 |
|
| 964 |
check_ajax_referer( 'uwp_process_diagnosis', 'security' ); |
| 965 |
|
| 966 |
$type = strip_tags(esc_sql($_POST['type'])); |
| 967 |
$step = isset($_POST['step']) ? strip_tags(esc_sql($_POST['step'])) : 0; |
| 968 |
|
| 969 |
|
| 970 |
if (!current_user_can('manage_options')) { |
| 971 |
return; |
| 972 |
} |
| 973 |
|
| 974 |
$this->uwp_process_diagnosis($type, $step); |
| 975 |
|
| 976 |
die(); |
| 977 |
|
| 978 |
} |
| 979 |
|
| 980 |
/** |
| 981 |
* Process diagnosis step |
| 982 |
* |
| 983 |
* @package userswp |
| 984 |
* |
| 985 |
* @param string $type Action type |
| 986 |
* @param int $step Current step |
| 987 |
* |
| 988 |
*/ |
| 989 |
public function uwp_process_diagnosis($type, $step) { |
| 990 |
switch ($type) { |
| 991 |
case 'clear_version_numbers': |
| 992 |
$this->clear_version_numbers(); |
| 993 |
break; |
| 994 |
case 'fix_user_data': |
| 995 |
$this->fix_usermeta($step); |
| 996 |
break; |
| 997 |
case 'export_db_texts': |
| 998 |
$this->export_db_texts($step); |
| 999 |
break; |
| 1000 |
case 'add_dummy_users': |
| 1001 |
$this->uwp_tools_process_dummy_users($step, 'add'); |
| 1002 |
break; |
| 1003 |
case 'remove_dummy_users': |
| 1004 |
$this->uwp_tools_process_dummy_users($step, 'remove'); |
| 1005 |
break; |
| 1006 |
default : |
| 1007 |
do_action('uwp_process_diagnosis', $type, $step); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
/** |
| 1013 |
* Clear version numbers so install/upgrade functions will run. |
| 1014 |
*/ |
| 1015 |
public function clear_version_numbers(){ |
| 1016 |
delete_option( 'uwp_db_version' ); |
| 1017 |
do_action( 'uwp_clear_version_numbers'); // used by addons to clear their version numbers. |
| 1018 |
$message = aui()->alert(array( |
| 1019 |
'type'=>'success', |
| 1020 |
'content'=> __( 'Version numbers cleared. Install/upgrade functions will run on next page load.', 'userswp' ) |
| 1021 |
) |
| 1022 |
); |
| 1023 |
$output = array( |
| 1024 |
'done' => true, |
| 1025 |
'message' => "<div class='bsui'>".$message."</div>", |
| 1026 |
); |
| 1027 |
echo json_encode($output); |
| 1028 |
} |
| 1029 |
|
| 1030 |
public static function setup_menu( $menu_id = '',$menu_location = '' ) { |
| 1031 |
|
| 1032 |
$menu_id = sanitize_title_with_dashes($menu_id); |
| 1033 |
$menu_location = sanitize_title_with_dashes($menu_location); |
| 1034 |
|
| 1035 |
// confirm the sidebar_id is valid |
| 1036 |
if(!$menu_id && !$menu_location){ |
| 1037 |
return new WP_Error( 'uwp-wizard-setup-menu', __( "The menu is not valid.", "userswp" ) ); |
| 1038 |
} |
| 1039 |
|
| 1040 |
$items_added = 0; |
| 1041 |
$items_exist= 0; |
| 1042 |
|
| 1043 |
if($menu_id){ |
| 1044 |
|
| 1045 |
$menu_exists = wp_get_nav_menu_object( $menu_id ); |
| 1046 |
|
| 1047 |
if(!$menu_exists){ |
| 1048 |
return new WP_Error( 'uwp-wizard-setup-menu', __( "The menu is not valid.", "userswp" ) ); |
| 1049 |
} |
| 1050 |
|
| 1051 |
$current_menu_items = wp_get_nav_menu_items( $menu_id ); |
| 1052 |
|
| 1053 |
$current_menu_titles = array(); |
| 1054 |
// get a list of current slugs so we don't add things twice. |
| 1055 |
if(!empty($current_menu_items)){ |
| 1056 |
foreach($current_menu_items as $current_menu_item){ |
| 1057 |
if(!empty($current_menu_item->post_name)){ |
| 1058 |
$current_menu_titles[] = $current_menu_item->title; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
$uwp_menus = new UsersWP_Menus(); |
| 1064 |
$uwp_menu_items = $uwp_menus->get_endpoints(); |
| 1065 |
|
| 1066 |
if(!empty($uwp_menu_items)){ |
| 1067 |
foreach($uwp_menu_items as $menu_item_type){ |
| 1068 |
if(!empty($menu_item_type)){ |
| 1069 |
|
| 1070 |
$menu_item_type = array_map('wp_setup_nav_menu_item', $menu_item_type); |
| 1071 |
|
| 1072 |
foreach($menu_item_type as $menu_item){ |
| 1073 |
|
| 1074 |
if(!empty($current_menu_titles) && (in_array($menu_item->title,$current_menu_titles) || in_array(str_replace(" page",'',$menu_item->title),$current_menu_titles))){ |
| 1075 |
$items_exist++; continue 2; |
| 1076 |
} |
| 1077 |
|
| 1078 |
// setup standard menu stuff |
| 1079 |
$menu_item->{'menu-item-object-id'} = $menu_item->object_id; |
| 1080 |
$menu_item->{'menu-item-object'} = $menu_item->object; |
| 1081 |
$menu_item->{'menu-item-type'} = $menu_item->type; |
| 1082 |
$menu_item->{'menu-item-status'} = 'publish'; |
| 1083 |
$menu_item->{'menu-item-classes'} = !empty($menu_item->classes) ? implode(" ",$menu_item->classes) : 'uwp-menu-item'; |
| 1084 |
if($menu_item->type=='custom'){ |
| 1085 |
$menu_item->{'menu-item-url'} = $menu_item->url; |
| 1086 |
$menu_item->{'menu-item-title'} = $menu_item->title; |
| 1087 |
} |
| 1088 |
|
| 1089 |
wp_update_nav_menu_item($menu_id, 0, $menu_item); |
| 1090 |
$items_added++; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
} elseif($menu_location){ |
| 1097 |
|
| 1098 |
$menuname = "UsersWP Menu"; |
| 1099 |
|
| 1100 |
$menu_exists = wp_get_nav_menu_object( $menuname ); |
| 1101 |
|
| 1102 |
// If it doesn't exist, let's create it. |
| 1103 |
if( !$menu_exists) { |
| 1104 |
$menu_id = wp_create_nav_menu( $menuname ); |
| 1105 |
|
| 1106 |
$locations = get_theme_mod( 'nav_menu_locations' ); |
| 1107 |
|
| 1108 |
if($menu_id){ |
| 1109 |
$locations[$menu_location] = $menu_id; |
| 1110 |
set_theme_mod('nav_menu_locations', $locations); |
| 1111 |
return self::setup_menu($menu_id); |
| 1112 |
} |
| 1113 |
|
| 1114 |
}else{ |
| 1115 |
return new WP_Error( 'uwp-wizard-setup-menu', __( "Menu already exists.", "userswp" ) ); |
| 1116 |
} |
| 1117 |
|
| 1118 |
} |
| 1119 |
|
| 1120 |
if($items_added == 0 && $items_exist > 0){ |
| 1121 |
return __( 'Menu items already exist, none added.' , 'userswp' ); |
| 1122 |
}elseif($items_added > 0){ |
| 1123 |
return __( 'Menu items added successfully.' , 'userswp' ); |
| 1124 |
}else{ |
| 1125 |
return __( 'Something went wrong, you can manually add items in Appearance > Menus' , 'userswp' ); |
| 1126 |
} |
| 1127 |
|
| 1128 |
} |
| 1129 |
|
| 1130 |
} |