Project

General

Profile

Feature #12056 » cac-update-xprofile-college-names.php

Boone Gorges, 2019-11-14 03:30 PM

 
1
<?php
2

    
3
$field = xprofile_get_field( 2 );
4

    
5
$map = [
6
	'CUNY Graduate Center ' => 'CUNY Graduate Center',
7
	'CUNY Graduate School of Journalism' => 'Craig Newmark Graduate School of Journalism',
8
	'City College' => 'City College of New York',
9
	'Macaulay Honors College ' => 'Macaulay Honors College',
10
	'NYC College of Technology' => 'New York City College of Technology',
11
	'New Community College at CUNY' => 'Guttman Community College',
12
	'Stella and Charles Guttman Community College' => 'Guttman Community College',
13
];
14

    
15
global $wpdb, $bp;
16
foreach ( $map as $old => $new ) {
17
	$like = '%' . $wpdb->esc_like( $old ) . '%';
18
	$user_ids = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->profile->table_name_data} WHERE field_id = 2 and value LIKE %s", $like ) );
19

    
20
	foreach ( $user_ids as $user_id ) {
21
		$current = xprofile_get_field_data( 2, $user_id );
22

    
23
		if ( is_array( $current ) ) {
24
			foreach ( $current as &$value ) {
25
				if ( $old === $value ) {
26
					$value = $new;
27
				}
28
			}
29
		} else {
30
			$current = $new;
31
		}
32

    
33
		xprofile_set_field_data( 2, $user_id, $current );
34
	}
35

    
36
	// Positions.
37
	$term = get_term_by( 'name', $old, 'cacap_position_college' );
38
	if ( $term ) {
39
		// Don't bother with the ones that are just about trailing spaces.
40
		if ( trim( $old ) !== $new ) {
41
			$updated = wp_update_term(
42
				$term->term_id,
43
				'cacap_position_college',
44
				[
45
					'name' => $new,
46
				]
47
			);
48
		}
49
	}
50
}
(2-2/2)