NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reformat-blocks.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. function get_ids_from_gallery_block($blocks){
  3. $parsed_blocks = array();
  4. foreach ($blocks as $block) {
  5. if($block[blockName] === "core/gallery") {
  6. // Fix for new style gallery blocks
  7. if(count($block[attrs][ids]) < 1) {
  8. $gallery_imgs_ids = [];
  9. foreach($block[innerBlocks] as $inner) {
  10. $inner_id = $inner[attrs][id];
  11. array_push($gallery_imgs_ids, $inner_id);
  12. }
  13. $block[attrs][ids] = $gallery_imgs_ids;
  14. }
  15. array_push($parsed_blocks, $block[attrs]);
  16. } elseif ($block[blockName] === "core/image") {
  17. $ids = [];
  18. $galleryFormat = [];
  19. array_push($ids, $block[attrs][id]);
  20. $galleryFormat[ids] = $ids;
  21. $galleryFormat[columns] = 1;
  22. $galleryFormat[linkTo] = 'none';
  23. array_push($parsed_blocks, $galleryFormat);
  24. }
  25. }
  26. return $parsed_blocks;
  27. }
  28. function get_images_from_content($content) {
  29. $parse_images = array();
  30. $dom = new DOMDocument();
  31. @ $dom->loadHTML($content);
  32. $images = $dom->getElementsByTagName('img');
  33. foreach ($images as $image) {
  34. if($image->getAttribute('data-id')) {
  35. $id = $image->getAttribute('data-id');
  36. } else {
  37. $class_pieces = explode("-", $image->getAttribute('class'));
  38. $id = end($class_pieces);
  39. }
  40. // Format for lightbox wants an object
  41. $parse_images[$id] = [
  42. 'src' => $image->getAttribute('src'),
  43. 'title' => $image->getAttribute('alt'),
  44. ];
  45. }
  46. return $parse_images;
  47. }
  48. ?>