Project

General

Profile

Bug #23494 » sync-positions-to-college.php

Boone Gorges, 2025-09-18 10:46 AM

 
1
<?php
2

    
3
global $wpdb;
4

    
5
$cv_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'cac-cv'" );
6

    
7
foreach ( $cv_ids as $cv_id ) {
8
	$cv_post = get_post( $cv_id );
9
	if ( ! $cv_post || ( ! $cv_post instanceof WP_Post ) ) {
10
		continue;
11
	}
12

    
13
	$blocks = parse_blocks( $cv_post->post_content );
14
	if ( empty( $blocks ) ) {
15
		continue;
16
	}
17

    
18
	$position_blocks = \CAC\CVEditor\Blocks\CVPositions\find_position_blocks( $blocks );
19
	if ( ! $position_blocks ) {
20
		continue;
21
	}
22

    
23
	$user_id = $cv_post->post_author;
24

    
25
	$existing = xprofile_get_field_data( 2, $user_id, true );
26

    
27
	$all_campuses = cac_get_cuny_campuses();
28
	$campus_names = array_map(
29
		function ( $position ) use ( $all_campuses ) {
30
			if ( empty( $position['campus'] ) ) {
31
				return '';
32
			}
33

    
34
			$find = wp_filter_object_list(
35
				$all_campuses,
36
				[
37
					'full_name' => $position['campus'],
38
				]
39
			);
40

    
41
			return ! empty( $find ) ? $position['campus'] : '';
42

    
43
		},
44
		$position_blocks
45
	);
46

    
47
	$campus_names = array_filter( $campus_names );
48
	$campus_names = array_unique( $campus_names );
49

    
50
	// Both are empty - nothing to do.
51
	if ( empty( $existing ) && empty( $campus_names ) ) {
52
		continue;
53
	}
54

    
55
	$existing_array = (array) $existing;
56
	$update_value   = array_filter( array_unique( array_merge( $existing_array, $campus_names ) ) );
57

    
58
	// This mostly covers cases where the user has College value but no Positions in CV.
59
	if ( $existing_array == $update_value ) {
60
		continue;
61
	}
62

    
63
	WP_CLI::log( sprintf(
64
		'Changing user %d from "%s" to "%s"',
65
		$user_id,
66
		print_r( $existing, true ),
67
		implode( ', ', $update_value )
68
	) );
69

    
70
	/*
71
	$field           = new \BP_XProfile_ProfileData();
72
	$field->field_id = 2;
73
	$field->user_id  = $user_id
74
	$field->value    = maybe_serialize( $update_value );
75
	$field->save();
76
	*/
77
}
(1-1/2)