Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therajumandapati/1aff94b1e869773f29eadb5cd3a73e7e to your computer and use it in GitHub Desktop.
Save therajumandapati/1aff94b1e869773f29eadb5cd3a73e7e to your computer and use it in GitHub Desktop.
Extract recipe data from WPZoom Recipe plugin
$post = get_post(2930);
$blocks = parse_blocks( $post->post_content );
foreach ( $blocks as $block ) {
if( 'wpzoom-recipe-card/block-recipe-card' === $block['blockName']) {
$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' => $block['attrs']['keywords']
];
foreach($block['attrs']['details'] as $detail) {
if(array_key_exists("jsonLabel", $detail)) {
switch($detail['jsonLabel']) {
case 'Servings':
$recipe['servings'] = [
'value' => $detail['jsonValue'],
'unit' => $detail['jsonUnit']
];
break;
case 'Prep time':
$recipe['preptime'] = [
'value' => $detail['jsonValue'],
'unit' => $detail['jsonUnit']
];
break;
case 'Cooking time':
$recipe['cooktime'] = [
'value' => $detail['jsonValue'],
'unit' => $detail['jsonUnit']
];
break;
case 'Total time':
$recipe['totaltime'] = [
'value' => $detail['jsonValue'],
'unit' => $detail['jsonUnit']
];
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'] = $block['attrs']['notes'];
return $recipe;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment