-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMF.php
80 lines (64 loc) · 2.74 KB
/
MF.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
add_action('dining_menu_updater', 'update_pods_from_github');
function update_pods_from_github()
{
//Url to JSON file hosted on GitHub: repository Menu-Updater, branch master, file Menu.json
$url = 'https://github.com/Web-Jose/Menu-Updater/blob/main/Menu.json';
// GitHub Personal Access Token
$token = '1234567890abcdef1234567890abcdef12345678';
// Set up the API headers
$args = array(
'headers' => array(
'Authorization' => 'token ' . $token
)
);
// Make the API request
$response = wp_remote_get($url, $args);
// Check for errors
if (is_wp_error($response)) {
error_log('Error in API request: ' . $response->get_error_message());
return;
}
// Get the JSON from the body of the response
$json_data = wp_remote_retrieve_body($response);
// Decode the JSON into an associative array
$menu_data = json_decode($json_data, true);
// Check for errors decoding the JSON
if (is_null($menu_data)) {
error_log('Error decoding JSON from API');
return;
}
error_log('menu_data: ' . print_r($menu_data, true));
// Loop through each day in the menu data (e.g., Sunday, Monday, Tuesday)
foreach ($menu_data as $day => $meals) {
// Loop through each meal type (e.g., Breakfast, Lunch, Dinner)
foreach ($meals as $meal_type => $items) {
if (is_array($items)) {
//Loop through each food type (e.g., Entrees, Sides, Desserts)
foreach ($items as $food_item) {
// Loop Through each food item (e.g., Chicken, Rice, Beans)
foreach ($food_item as $food_type => $item) {
// Get the Dining Hall Menu Pod
$DiningPod = pods('dining_hall_menu');
// Check if pod exists
if ($DiningPod->exists()) {
//Creating the field name
$field_name = strtolower($day . 's_' . $meal_type . '_' . str_replace(" ", "_", str_replace("é", "e", $food_type)));
// Save the value to the field name
$FieldValue = array(
$field_name => $item
);
// Update the pod
$DiningPod->save($FieldValue);
} else {
error_log('Pod does not exist: ' . $DiningPod);
continue;
}
}
}
} else {
error_log('$items is not an array on day ' . $day . ', meal_type ' . $meal_type);
}
}
}
}