Created
January 12, 2023 03:46
-
-
Save therajumandapati/5d97cbe6a0012331a9f9fd305ef94bbc to your computer and use it in GitHub Desktop.
Migrates the recipe data from WPZoom recipe block to ACF blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$post_ids = [3011, 2883, 2930, 2912, 2904, 2801, 2640, 2515, 2480, 1505, 1118, 1011, 1098]; | |
function convertRecipeData($post_id) { | |
if($post_id === null) return; | |
echo "============ START POST ID: " . $post_id . " =============\n"; | |
echo "Getting post...\n"; | |
$post = get_post($post_id); | |
if($post === null) { | |
echo "No post found...\n"; | |
echo "============ END POST ID: " . $post_id . " =============\n"; | |
return; | |
}; | |
function getRecipe($post) { | |
$blocks = parse_blocks( $post->post_content ); | |
echo "Parsing blocks...\n"; | |
foreach ( $blocks as $block ) { | |
if( 'wpzoom-recipe-card/block-recipe-card' === $block['blockName']) { | |
echo "Found recipe card...\n"; | |
$recipe = [ | |
'title' => $block['attrs']['recipeTitle'], | |
'summary' => array_key_exists('jsonSummary', $block['attrs']) ? $block['attrs']['jsonSummary']: null, | |
'course' => array_key_exists('course', $block['attrs']) ? $block['attrs']['course']: null, | |
'keywords' => array_key_exists('keywords', $block['attrs']) ? $block['attrs']['keywords']: [] | |
]; | |
foreach($block['attrs']['details'] as $detail) { | |
if(array_key_exists("label", $detail)) { | |
switch($detail['label']) { | |
case 'Servings': | |
$recipe['servings'] = [ | |
'value' => $detail['value'], | |
'unit' => $detail['unit'] | |
]; | |
break; | |
case 'Prep time': | |
$recipe['preptime'] = [ | |
'value' => $detail['value'], | |
'unit' => $detail['unit'] | |
]; | |
break; | |
case 'Cooking time': | |
$recipe['cooktime'] = [ | |
'value' => $detail['value'], | |
'unit' => $detail['unit'] | |
]; | |
break; | |
case 'Total time': | |
$recipe['totaltime'] = [ | |
'value' => $detail['value'], | |
'unit' => $detail['unit'] | |
]; | |
break; | |
} | |
} | |
} | |
$ingredients = []; | |
$currentIngredientGroup = 'DefaultGroup'; | |
$ingredients[$currentIngredientGroup] = []; | |
foreach($block['attrs']['ingredients'] as $ingredient) { | |
if($ingredient['isGroup'] === true) { | |
$currentIngredientGroup = $ingredient['jsonName']; | |
$ingredients[$currentIngredientGroup] = []; | |
} else { | |
array_push($ingredients[$currentIngredientGroup], $ingredient['jsonName']); | |
} | |
} | |
$recipe['ingredients'] = $ingredients; | |
$steps = []; | |
$currentStepGroup = 'DefaultGroup'; | |
$steps[$currentStepGroup] = []; | |
foreach($block['attrs']['steps'] as $step) { | |
if($step['isGroup'] === true) { | |
$currentStepGroup = $step['jsonText']; | |
$steps[$currentStepGroup] = []; | |
} else { | |
array_push($steps[$currentStepGroup], $step['jsonText']); | |
} | |
} | |
$recipe['steps'] = $steps; | |
$recipe['notes'] = array_key_exists('notes', $block['attrs']) ? $block['attrs']['notes'] : null; | |
return $recipe; | |
} | |
} | |
} | |
function generateAcfRecipe($recipe) { | |
$acf_recipe_data = [ | |
"recipe"=> true, | |
"title"=> $recipe['title'], | |
"summary"=> $recipe['summary'], | |
"keywords"=> array_map(function($keyword) { | |
return (object) [ | |
'keyword' => $keyword | |
]; | |
}, $recipe['keywords']), | |
"notes"=> array_key_exists('notes', $recipe) ? $recipe['notes'] : null | |
]; | |
// Add prep time | |
if(array_key_exists('preptime', $recipe)) { | |
$acf_recipe_data['prep_time'] = (object) [ | |
"display_prep_time"=> true, | |
"prep_time_label"=> "Prep time", | |
"prep_time_value"=> $recipe['preptime']['value'], | |
"prep_time_units"=> $recipe['preptime']['unit'], | |
]; | |
} | |
// Add cook time | |
if(array_key_exists('cooktime', $recipe)) { | |
$acf_recipe_data['cook_time'] = (object) [ | |
"display_cook_time"=> true, | |
"cook_time_label"=> "Cook time", | |
"cook_time_value"=> $recipe['cooktime']['value'], | |
"cook_time_units"=> $recipe['cooktime']['unit'], | |
]; | |
} | |
// add total time | |
if(array_key_exists('totaltime', $recipe)) { | |
$acf_recipe_data['total_time'] = (object) [ | |
"display_total_time"=> true, | |
"total_time_label"=> "Total time", | |
"total_time_value"=> $recipe['totaltime']['value'], | |
"total_time_units"=> $recipe['totaltime']['unit'], | |
]; | |
} | |
// add servings | |
if(array_key_exists('servings', $recipe)) { | |
$acf_recipe_data['servings'] = (object) [ | |
"servings_label"=> "Servings", | |
"servings_value"=> $recipe['servings']['value'], | |
"servings_units"=> $recipe['servings']['unit'], | |
]; | |
} | |
// Add Ingredients | |
if(array_key_exists('ingredients', $recipe)) { | |
foreach($recipe['ingredients'] as $i_group_name => $i_group_list) { | |
if(count($i_group_list) === 0) continue; | |
$acf_recipe_data['ingredients'][] = (object) [ | |
"ingredients_group"=> (object) [ | |
"ingredients_group_name" => $i_group_name, | |
"ingredients" => array_map(function($ingredient) { | |
return (object) [ | |
"ingredient" => $ingredient | |
]; | |
}, $i_group_list), | |
] | |
]; | |
} | |
} | |
// Add Steps | |
// Add Ingredients | |
if(array_key_exists('steps', $recipe)) { | |
foreach($recipe['steps'] as $s_group_name => $s_group_list) { | |
if(count($s_group_list) === 0) continue; | |
$acf_recipe_data['steps'][] = (object) [ | |
"steps_group"=> (object) [ | |
"steps_group_name" => $s_group_name, | |
"steps" => array_map(function($step) { | |
return (object) [ | |
"step" => $step | |
]; | |
}, $s_group_list), | |
] | |
]; | |
} | |
} | |
return $acf_recipe_data; | |
} | |
function updatePostWithACFRecipeData($post_id, $generated_acf_recipe_data) { | |
$response = wp_remote_post( "https://ENTERYOURENDPOINT/wp-json/wp/v2/posts/$post_id?per_page=3&sticky=true&status=publish", array( | |
'headers' => array( | |
'Authorization' => 'Basic ENTERYOURAUTH', | |
'Content-Type' => 'application/json; charset=utf-8', | |
), | |
'body' => json_encode([ | |
'id' => $post_id, | |
'acf' => $generated_acf_recipe_data | |
]), | |
) ); | |
echo json_encode([ | |
'id' => $post_id, | |
'acf' => $generated_acf_recipe_data | |
]); | |
if ( ! is_wp_error( $response ) ) { | |
// The request went through successfully, check the response code against | |
// what we're expecting | |
if ( 200 == wp_remote_retrieve_response_code( $response ) ) { | |
echo "SUCCESSFULLY UPDATED...\n"; | |
} else { | |
// The response code was not what we were expecting, record the message | |
$error_message = wp_remote_retrieve_response_message( $response ); | |
echo "FAILED TO UPDATE...\n"; | |
echo($error_message); | |
echo "\n"; | |
} | |
} else { | |
// There was an error making the request | |
$error_message = $response->get_error_message(); | |
echo "FAILED TO UPDATE...\n"; | |
echo($error_message); | |
echo "\n"; | |
} | |
} | |
$recipe = getRecipe($post); | |
if($recipe) { | |
echo "Got recipe data...\n"; | |
} else { | |
echo "No recipe card block found. Exiting...\n"; | |
echo "============ END POST ID: " . $post_id . " =============\n"; | |
return; | |
} | |
// ACF Data | |
// $acf_prep_time = update_field('prep_time', $post_id, []); | |
// $acf_cook_time = get_field('cook_time', $post_id); | |
// $acf_total_time = get_field('total_time', $post_id); | |
// $acf_servings = get_field('servings', $post_id); | |
// $acf_ingredients = get_field('ingredients', $post_id); | |
// $acf_steps = get_field('steps', $post_id); | |
echo "Converting to ACF format...\n"; | |
$generated_acf_recipe_data = generateAcfRecipe($recipe); | |
echo "Completed converting to ACF format...\n"; | |
echo "Calling API to update post...\n"; | |
updatePostWithACFRecipeData($post_id, $generated_acf_recipe_data); | |
echo "Completed calling API to update post...\n"; | |
// return generateAcfRecipe($recipe); | |
echo "============ END POST ID: " . $post_id . " =============\n"; | |
} | |
convertRecipeData($post_ids[12]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment