| @@ -7,24 +7,43 @@ | ||
| 7 | 7 | * @since 1.0.0 |
| 8 | 8 | * @author GeoDirectory Team <info@wpgeodirectory.com> |
| 9 | 9 | */ |
| 10 | 10 | class UsersWP_Tools { |
| 11 | - | |
| 12 | - public function uwp_fix_usermeta_table() { | |
| 13 | - // If table not available it will be created else synced | |
| 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() { | |
| 14 | 27 | uwp_create_tables(); |
| 15 | - uwp101_create_tables(); | |
| 16 | 28 | } |
| 17 | 29 | |
| 18 | - public function uwp_fix_userdata($step) { | |
| 19 | - $this->uwp_fix_usermeta($step); | |
| 20 | - } | |
| 21 | - | |
| 22 | - public function uwp_tools_wrap_error_message($message, $class) { | |
| 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) { | |
| 23 | 42 | ob_start(); |
| 24 | 43 | ?> |
| 25 | - <div class="notice inline notice-<?php echo $class; ?> notice-alt"> | |
| 26 | - <p><?php echo $message; ?></p> | |
| 44 | + <div class="notice inline notice-<?php echo esc_attr( $class ); ?> notice-alt"> | |
| 45 | + <p><?php echo wp_kses_post( $message ); ?></p> | |
| 27 | 46 | </div> |
| 28 | 47 | <?php |
| 29 | 48 | $html = ob_get_clean(); |
| 30 | 49 | return $html; |
| @@ -29,16 +48,24 @@ | ||
| 29 | 48 | $html = ob_get_clean(); |
| 30 | 49 | return $html; |
| 31 | 50 | } |
| 32 | 51 | |
| 33 | - public function uwp_fix_field_columns() { | |
| 52 | + /** | |
| 53 | + * Fixes field columns | |
| 54 | + * | |
| 55 | + * @package userswp | |
| 56 | + * | |
| 57 | + * @return object|bool | |
| 58 | + * | |
| 59 | + */ | |
| 60 | + public function fix_field_columns() { | |
| 34 | 61 | global $wpdb; |
| 35 | 62 | $errors = new WP_Error(); |
| 36 | 63 | |
| 37 | 64 | $form_type = 'account'; |
| 38 | 65 | $table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 39 | - $fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s ORDER BY sort_order ASC", array($form_type))); | |
| 40 | - $meta_table = uwp_get_table_prefix() . 'uwp_usermeta'; | |
| 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'; | |
| 41 | 68 | |
| 42 | 69 | $excluded = uwp_get_excluded_fields(); |
| 43 | 70 | |
| 44 | 71 | if (!empty($fields)) { |
| @@ -60,13 +87,21 @@ | ||
| 60 | 87 | } |
| 61 | 88 | return true; |
| 62 | 89 | } |
| 63 | 90 | |
| 64 | - public function uwp_get_field_columns() { | |
| 91 | + /** | |
| 92 | + * Returns columns for field | |
| 93 | + * | |
| 94 | + * @package userswp | |
| 95 | + * | |
| 96 | + * @return array | |
| 97 | + * | |
| 98 | + */ | |
| 99 | + public function get_field_columns() { | |
| 65 | 100 | global $wpdb; |
| 66 | 101 | $form_type = 'account'; |
| 67 | 102 | $table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 68 | - $fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s ORDER BY sort_order ASC", array($form_type))); | |
| 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 | |
| 69 | 104 | |
| 70 | 105 | $excluded = uwp_get_excluded_fields(); |
| 71 | 106 | |
| 72 | 107 | $columns = array(); |
| @@ -84,27 +119,35 @@ | ||
| 84 | 119 | return $columns; |
| 85 | 120 | |
| 86 | 121 | } |
| 87 | 122 | |
| 88 | - public function uwp_fix_usermeta($step) { | |
| 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) { | |
| 89 | 132 | |
| 90 | 133 | global $wpdb; |
| 91 | 134 | |
| 92 | 135 | |
| 93 | - $items_per_page = 10; | |
| 136 | + $items_per_page = apply_filters('tools_process_fix_usermeta_per_page', 10, $step); | |
| 94 | 137 | $offset = (int) $step * $items_per_page; |
| 95 | -// $end = $offset + $items_per_page; | |
| 96 | 138 | |
| 97 | - $user_ids = $wpdb->get_col( $wpdb->prepare( | |
| 139 | + $user_ids = $wpdb->get_col( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 98 | 140 | "SELECT $wpdb->users.ID FROM $wpdb->users LIMIT %d OFFSET %d", |
| 99 | 141 | $items_per_page, $offset )); |
| 100 | 142 | |
| 101 | - $total_users = $this->uwp_tools_total_users_count(); | |
| 143 | + $users_count = count_users(); | |
| 144 | + $total_users = $users_count['total_users']; | |
| 102 | 145 | |
| 103 | 146 | $max_step = ceil($total_users / $items_per_page) - 1; |
| 104 | 147 | $percent = (($step + 1)/ ($max_step+1)) * 100; |
| 105 | 148 | |
| 106 | - $columns = $this->uwp_get_field_columns(); | |
| 149 | + $columns = $this->get_field_columns(); | |
| 107 | 150 | |
| 108 | 151 | //we got all the IDs, now loop through them to get individual IDs |
| 109 | 152 | $count = 0; |
| 110 | 153 | $message = ''; |
| @@ -112,15 +155,15 @@ | ||
| 112 | 155 | $done = false; |
| 113 | 156 | $error = false; |
| 114 | 157 | |
| 115 | 158 | if ($step == 0) { |
| 116 | - $this->uwp_fix_usermeta_table(); | |
| 159 | + $this->fix_usermeta_table(); | |
| 117 | 160 | |
| 118 | - $output = $this->uwp_fix_field_columns(); | |
| 161 | + $output = $this->fix_field_columns(); | |
| 119 | 162 | if (is_wp_error($output)) { |
| 120 | 163 | $table_sync_error = true; |
| 121 | 164 | $error = true; |
| 122 | - $message = $this->uwp_tools_wrap_error_message($output->get_error_message(), 'error'); | |
| 165 | + $message = $this->tools_wrap_error_message($output->get_error_message(), 'error'); | |
| 123 | 166 | } |
| 124 | 167 | } |
| 125 | 168 | |
| 126 | 169 | if (!$table_sync_error) { |
| @@ -127,29 +170,26 @@ | ||
| 127 | 170 | foreach ( $user_ids as $user_id ) { |
| 128 | 171 | |
| 129 | 172 | // get user info by calling get_userdata() on each id |
| 130 | 173 | $user_data = get_userdata($user_id); |
| 131 | - $first_name = get_user_meta( $user_id, 'first_name', true ); | |
| 132 | - $last_name = get_user_meta( $user_id, 'last_name', true ); | |
| 133 | - $bio = get_user_meta( $user_id, 'description', true ); | |
| 134 | 174 | $usermeta = get_user_meta( $user_id, 'uwp_usermeta', true ); |
| 135 | 175 | |
| 136 | 176 | foreach ($columns as $column) { |
| 137 | 177 | switch ($column) { |
| 138 | - case "uwp_account_username": | |
| 178 | + case "username": | |
| 139 | 179 | $value = $user_data->user_login; |
| 140 | 180 | break; |
| 141 | - case "uwp_account_email": | |
| 181 | + case "email": | |
| 142 | 182 | $value = $user_data->user_email; |
| 143 | 183 | break; |
| 144 | - case "uwp_account_first_name": | |
| 145 | - $value = $first_name; | |
| 184 | + case "first_name": | |
| 185 | + $value = $user_data->first_name; | |
| 146 | 186 | break; |
| 147 | - case "uwp_account_last_name": | |
| 148 | - $value = $last_name; | |
| 187 | + case "last_name": | |
| 188 | + $value = $user_data->last_name; | |
| 149 | 189 | break; |
| 150 | - case "uwp_account_bio": | |
| 151 | - $value = $bio; | |
| 190 | + case "bio": | |
| 191 | + $value = $user_data->description; | |
| 152 | 192 | break; |
| 153 | 193 | default: |
| 154 | 194 | if ($usermeta === false) { |
| 155 | 195 | $value = false; |
| @@ -162,29 +202,15 @@ | ||
| 162 | 202 | uwp_update_usermeta($user_id, $column, $value); |
| 163 | 203 | } |
| 164 | 204 | } |
| 165 | 205 | |
| 166 | - | |
| 167 | - //avatar and banner | |
| 168 | - $avatar_url = isset( $usermeta[ 'uwp_account_avatar_thumb' ] ) ? $usermeta[ 'uwp_account_avatar_thumb' ] : false; | |
| 169 | - if ($avatar_url !== false) { | |
| 170 | - uwp_update_usermeta($user_id, 'uwp_account_avatar_thumb', $avatar_url); | |
| 171 | - } | |
| 172 | - $banner_url = isset( $usermeta[ 'uwp_account_banner_thumb' ] ) ? $usermeta[ 'uwp_account_banner_thumb' ] : false; | |
| 173 | - if ($banner_url !== false) { | |
| 174 | - uwp_update_usermeta($user_id, 'uwp_account_banner_thumb', $banner_url); | |
| 175 | - } | |
| 176 | - | |
| 177 | - | |
| 178 | - | |
| 179 | - | |
| 180 | 206 | $count++; |
| 181 | 207 | } |
| 182 | 208 | |
| 183 | 209 | if ($step >= $max_step) { |
| 184 | 210 | $done = true; |
| 185 | - $message = __("Processed Successfully", 'uwp-tools'); | |
| 186 | - $message = $this->uwp_tools_wrap_error_message($message, 'success'); | |
| 211 | + $message = __("Processed Successfully", 'userswp'); | |
| 212 | + $message = $this->tools_wrap_error_message($message, 'success'); | |
| 187 | 213 | } else { |
| 188 | 214 | $done = false; |
| 189 | 215 | $step = $step + 1; |
| 190 | 216 | } |
| @@ -200,8 +226,252 @@ | ||
| 200 | 226 | echo json_encode($output); |
| 201 | 227 | |
| 202 | 228 | } |
| 203 | 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 | + */ | |
| 204 | 474 | public function uwp_sql_datatype_from_field($field) { |
| 205 | 475 | |
| 206 | 476 | switch ($field->field_type) { |
| 207 | 477 | case 'checkbox': |
| @@ -261,59 +531,71 @@ | ||
| 261 | 531 | |
| 262 | 532 | return $meta_field_add; |
| 263 | 533 | } |
| 264 | 534 | |
| 265 | - public function uwp_tools_total_users_count() { | |
| 266 | - global $wpdb; | |
| 267 | - $sort= "user_registered"; | |
| 268 | - $total_users = $wpdb->get_var( $wpdb->prepare( | |
| 269 | - "SELECT count(*) FROM $wpdb->users ORDER BY %s ASC" | |
| 270 | - , $sort )); | |
| 271 | - return $total_users; | |
| 272 | - } | |
| 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); | |
| 273 | 597 | |
| 274 | - public function uwp_tools_process_dummy_users() { | |
| 275 | - if (!current_user_can('manage_options')) { | |
| 276 | - return; | |
| 277 | - } | |
| 278 | - | |
| 279 | - if (!isset($_GET['uwp_dummy_users']) || empty($_GET['uwp_dummy_users'])) { | |
| 280 | - return; | |
| 281 | - } | |
| 282 | - | |
| 283 | - global $wpdb; | |
| 284 | - | |
| 285 | - if ($_GET['uwp_dummy_users'] == 'create') { | |
| 286 | - | |
| 287 | - $users_data = $this->uwp_dummy_users_data(); | |
| 288 | - foreach ( $users_data as $user ) { | |
| 289 | - if ( username_exists( $user['login'] ) ) { | |
| 290 | - continue; | |
| 291 | - } | |
| 292 | - $user_id = wp_insert_user( array( | |
| 293 | - 'user_login' => $user['login'], | |
| 294 | - 'user_pass' => $user['pass'], | |
| 295 | - 'display_name' => $user['display_name'], | |
| 296 | - 'user_email' => $user['email'], | |
| 297 | - 'user_registered' => uwp_get_random_date( 45, 1 ), | |
| 298 | - ) ); | |
| 299 | - $query[] = $wpdb->last_query; | |
| 300 | - | |
| 301 | - $name = explode( ' ', $user['display_name'] ); | |
| 302 | - update_user_meta( $user_id, 'first_name', $name[0] ); | |
| 303 | - update_user_meta( $user_id, 'uwp_dummy_user', '1' ); | |
| 304 | - update_user_meta( $user_id, 'last_name', isset( $name[1] ) ? $name[1] : '' ); | |
| 305 | - | |
| 306 | - $users[] = $user_id; | |
| 307 | - } | |
| 308 | - | |
| 309 | - wp_redirect(admin_url('users.php')); | |
| 310 | - exit(); | |
| 311 | - } | |
| 312 | - | |
| 313 | - if ($_GET['uwp_dummy_users'] == 'delete') { | |
| 314 | - //todo: add this feature | |
| 315 | - } | |
| 316 | 598 | } |
| 317 | 599 | |
| 318 | 600 | /** |
| 319 | 601 | * Get a site specific password for dummy users. |
| @@ -323,8 +605,16 @@ | ||
| 323 | 605 | private static function get_dummy_user_passowrd(){ |
| 324 | 606 | return substr(hash( 'SHA256', AUTH_KEY . site_url() ), 0, 15); |
| 325 | 607 | } |
| 326 | 608 | |
| 609 | + /** | |
| 610 | + * Returns array of dummy users | |
| 611 | + * | |
| 612 | + * @package userswp | |
| 613 | + * | |
| 614 | + * @return array | |
| 615 | + * | |
| 616 | + */ | |
| 327 | 617 | public function uwp_dummy_users_data() { |
| 328 | 618 | |
| 329 | 619 | return array( |
| 330 | 620 | 0 => array( |
| @@ -480,67 +770,58 @@ | ||
| 480 | 770 | ); |
| 481 | 771 | |
| 482 | 772 | } |
| 483 | 773 | |
| 484 | - /** | |
| 485 | - * Adds the tools settings page menu as submenu. | |
| 486 | - * | |
| 487 | - * @since 1.0.0 | |
| 488 | - * @package userswp | |
| 489 | - * | |
| 490 | - * @param callable $settings_page The function to be called to output the content for this page. | |
| 491 | - * | |
| 492 | - * @return void | |
| 493 | - */ | |
| 494 | - public function uwp_add_admin_tools_sub_menu($settings_page) { | |
| 495 | - | |
| 496 | - add_submenu_page( | |
| 497 | - "userswp", | |
| 498 | - __('Tools', 'uwp-tools'), | |
| 499 | - __('Tools', 'uwp-tools'), | |
| 500 | - 'manage_options', | |
| 501 | - 'uwp_tools', | |
| 502 | - $settings_page | |
| 503 | - ); | |
| 504 | - | |
| 505 | - } | |
| 506 | - | |
| 507 | - public function uwp_tools_main_tab_content() { | |
| 774 | + /** | |
| 775 | + * Outputs tools form | |
| 776 | + * | |
| 777 | + * @package userswp | |
| 778 | + * | |
| 779 | + */ | |
| 780 | + public static function output() { | |
| 781 | + ob_start(); | |
| 508 | 782 | ?> |
| 509 | - <table class="form-table gd-tools-table"> | |
| 783 | + <div class="wrap userswp"> | |
| 784 | + <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> | |
| 785 | + <table class="uwp-tools-table widefat"> | |
| 510 | 786 | <tbody> |
| 511 | - <tr> | |
| 512 | - <td><strong><?php _e('Tool', 'uwp-tools');?></strong></td> | |
| 513 | - <td><strong><?php _e('Description', 'uwp-tools');?></strong></td> | |
| 514 | - <td style="text-align: right"><strong><?php _e('Action', 'uwp-tools');?></strong></td> | |
| 515 | - </tr> | |
| 516 | 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> | |
| 517 | 800 | |
| 518 | - <?php if (defined('USERSWP_VERSION')) { ?> | |
| 519 | 801 | <tr> |
| 520 | - <td><?php _e('Fix User Data', 'uwp-tools');?></td> | |
| 521 | - <td> | |
| 522 | - <div style="margin-bottom: 10px"><?php _e('Fixes User Data if you were using the Beta version.', 'uwp-tools');?></div> | |
| 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> | |
| 523 | 807 | </td> |
| 524 | - <td style="text-align: right"> | |
| 525 | - <?php | |
| 526 | - $total_users = $this->uwp_tools_total_users_count(); | |
| 527 | - $items_per_page = 10; | |
| 528 | - if ($total_users > $items_per_page) { | |
| 529 | - $multiple = 'data-step="1"'; | |
| 530 | - } else { | |
| 531 | - $multiple = ""; | |
| 532 | - } | |
| 533 | - ?> | |
| 534 | - <input type="button" value="<?php _e('Run', 'uwp-tools');?>" | |
| 535 | - class="button-primary uwp_diagnosis_button" <?php echo $multiple; ?> data-diagnose="fix_user_data"/> | |
| 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"/> | |
| 536 | 817 | </td> |
| 537 | 818 | </tr> |
| 538 | 819 | |
| 539 | 820 | <tr> |
| 540 | - <td colspan="3"> | |
| 821 | + <td colspan="2" class="has-pbar"> | |
| 541 | 822 | <div id="uwp_diagnose_pb_fix_user_data" class="uwp-pb-wrapper"> |
| 542 | - <div id="progressBar" style="display: none;"><div></div></div> | |
| 823 | + <div class="progressBar" style="display: none;"><div></div></div> | |
| 543 | 824 | </div> |
| 544 | 825 | <div id="uwp_diagnose_fix_user_data"></div> |
| 545 | 826 | </td> |
| 546 | 827 | </tr> |
| @@ -545,26 +826,66 @@ | ||
| 545 | 826 | </td> |
| 546 | 827 | </tr> |
| 547 | 828 | |
| 548 | 829 | <tr> |
| 549 | - <td><?php _e('Create Dummy Users', 'uwp-tools');?></td> | |
| 550 | - <td> | |
| 551 | - <div><?php _e('Dummy Users will be created for Testing. You can delete them later. Password for all dummy users:', 'uwp-tools'); echo " ".self::get_dummy_user_passowrd();?></div> | |
| 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"/> | |
| 552 | 836 | </td> |
| 553 | - <td style="text-align: right"> | |
| 554 | - <?php | |
| 555 | - $dummy_users_create_url = add_query_arg( array( | |
| 556 | - 'uwp_dummy_users' => 'create', | |
| 557 | - )); | |
| 837 | + </tr> | |
| 558 | 838 | |
| 559 | - ?> | |
| 560 | - <a href="<?php echo $dummy_users_create_url; ?>" class="button-primary"><?php _e('Run', 'uwp-tools');?></a> | |
| 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> | |
| 561 | 845 | </td> |
| 562 | 846 | </tr> |
| 563 | - <?php } ?> | |
| 564 | 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 | + | |
| 565 | 885 | </tbody> |
| 566 | 886 | </table> |
| 887 | + </div> | |
| 567 | 888 | |
| 568 | 889 | <script type="text/javascript"> |
| 569 | 890 | (function( $, window, undefined ) { |
| 570 | 891 | $(document).ready(function () { |
| @@ -570,12 +891,11 @@ | ||
| 570 | 891 | $(document).ready(function () { |
| 571 | 892 | $('.uwp_diagnosis_button').click(function (e) { |
| 572 | 893 | e.preventDefault(); |
| 573 | 894 | var type = $(this).data('diagnose'); |
| 574 | - | |
| 575 | - $("#uwp_diagnose_pb_" + type).find('#progressBar').show().progressbar({value: 0}); | |
| 576 | - | |
| 577 | - // start the process | |
| 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}); | |
| 578 | 898 | uwp_process_diagnose_step( 0, type ); |
| 579 | 899 | |
| 580 | 900 | }); |
| 581 | 901 | }); |
| @@ -588,15 +908,26 @@ | ||
| 588 | 908 | dataType: 'json', |
| 589 | 909 | data: { |
| 590 | 910 | action: 'uwp_process_diagnosis', |
| 591 | 911 | step: step, |
| 592 | - type: type | |
| 912 | + type: type, | |
| 913 | + security: '<?php echo esc_js( wp_create_nonce('uwp_process_diagnosis') ); ?>', | |
| 593 | 914 | }, |
| 594 | 915 | beforeSend: function() {}, |
| 595 | 916 | success: function(response, textStatus, xhr) { |
| 596 | 917 | if(response.done === true || response.error === true ) { |
| 597 | - jQuery("#uwp_diagnose_pb_" + type).find('#progressBar').hide(); | |
| 598 | - jQuery("#uwp_diagnose_" + type).html(response.message); | |
| 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); | |
| 599 | 930 | } else { |
| 600 | 931 | tools_progress(response.percent, type); |
| 601 | 932 | setTimeout(function(){ |
| 602 | 933 | uwp_process_diagnose_step(parseInt( response.step ), type) |
| @@ -609,9 +940,9 @@ | ||
| 609 | 940 | }); // end of ajax |
| 610 | 941 | |
| 611 | 942 | |
| 612 | 943 | function tools_progress(percent, type) { |
| 613 | - $element = jQuery("#uwp_diagnose_pb_" + type).find('#progressBar'); | |
| 944 | + $element = jQuery("#uwp_diagnose_pb_" + type).find('.progressBar'); | |
| 614 | 945 | var progressBarWidth = percent * $element.width() / 100; |
| 615 | 946 | $element.find('div').animate({ width: progressBarWidth }, 500).html(percent + "% "); |
| 616 | 947 | } |
| 617 | 948 | } |
| @@ -616,16 +947,23 @@ | ||
| 616 | 947 | } |
| 617 | 948 | } |
| 618 | 949 | </script> |
| 619 | 950 | <?php |
| 951 | + | |
| 952 | + echo ob_get_clean(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 620 | 953 | } |
| 621 | 954 | |
| 622 | - function uwp_process_diagnosis_ajax() { | |
| 955 | + /** | |
| 956 | + * Process diagnosis AJAX call | |
| 957 | + */ | |
| 958 | + public function uwp_process_diagnosis_ajax() { | |
| 623 | 959 | |
| 624 | 960 | if (!is_user_logged_in()) { |
| 625 | 961 | return; |
| 626 | 962 | } |
| 627 | 963 | |
| 964 | + check_ajax_referer( 'uwp_process_diagnosis', 'security' ); | |
| 965 | + | |
| 628 | 966 | $type = strip_tags(esc_sql($_POST['type'])); |
| 629 | 967 | $step = isset($_POST['step']) ? strip_tags(esc_sql($_POST['step'])) : 0; |
| 630 | 968 | |
| 631 | 969 | |
| @@ -638,12 +976,155 @@ | ||
| 638 | 976 | die(); |
| 639 | 977 | |
| 640 | 978 | } |
| 641 | 979 | |
| 642 | - function uwp_process_diagnosis($type, $step) { | |
| 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) { | |
| 643 | 990 | switch ($type) { |
| 991 | + case 'clear_version_numbers': | |
| 992 | + $this->clear_version_numbers(); | |
| 993 | + break; | |
| 644 | 994 | case 'fix_user_data': |
| 645 | - $this->uwp_fix_userdata($step); | |
| 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); | |
| 646 | 1008 | } |
| 647 | 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 | + } | |
| 648 | 1129 | |
| 649 | 1130 | } |