Block.php
3 years ago
BlockFinder.php
5 years ago
DynamicData.php
4 years ago
HasOneRelationship.php
5 years ago
Integration.php
5 years ago
Utility.php
4 years ago
BlockFinder.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Support; |
| 4 | |
| 5 | class BlockFinder |
| 6 | { |
| 7 | protected $namespace; |
| 8 | |
| 9 | public function __construct($namespace = 'presto-player') |
| 10 | { |
| 11 | $this->namespace = $namespace; |
| 12 | } |
| 13 | |
| 14 | public function find() |
| 15 | { |
| 16 | $blocks = array(); |
| 17 | |
| 18 | /** |
| 19 | * Get an array of all of our post types, then we will |
| 20 | * remove any unwanted post types |
| 21 | */ |
| 22 | $post_types = get_post_types( |
| 23 | array( |
| 24 | 'public' => true, |
| 25 | 'show_ui' => true, |
| 26 | ) |
| 27 | ); |
| 28 | |
| 29 | array_push($post_types, 'wp_block'); |
| 30 | unset($post_types['attachment']); |
| 31 | |
| 32 | /** |
| 33 | * Get a list of all post ids |
| 34 | */ |
| 35 | $post_ids = []; |
| 36 | |
| 37 | foreach ($post_types as $key => $post_type) { |
| 38 | $posts = get_posts( |
| 39 | array( |
| 40 | 'posts_per_page' => -1, |
| 41 | 'post_type' => $post_type, |
| 42 | 'fields' => 'ids' |
| 43 | ) |
| 44 | ); |
| 45 | foreach ($posts as $id) { |
| 46 | array_push($post_ids, $id); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Loop through post IDs and get the blocks that are used. |
| 52 | */ |
| 53 | foreach ($post_ids as $post_ID) { |
| 54 | $post = get_post($post_ID); |
| 55 | |
| 56 | if (!has_blocks($post->post_content)) { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | $post_blocks = parse_blocks($post->post_content); |
| 61 | |
| 62 | foreach ($post_blocks as $block) { |
| 63 | $this->findBlocks($block, $blocks, $post); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $data = array( |
| 68 | 'blocks' => $blocks, |
| 69 | ); |
| 70 | |
| 71 | return $data; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Searches an array for a value. |
| 76 | * |
| 77 | * @param array $array - Array to search through. |
| 78 | * @param string $field - Key to search. |
| 79 | * @param string $value - Value to search in key. |
| 80 | * |
| 81 | * @return array/boolean |
| 82 | */ |
| 83 | function searchForBlockKey($array, $field, $value) |
| 84 | { |
| 85 | foreach ($array as $key => $val) { |
| 86 | if ($val[$field] === $value) { |
| 87 | return $key; |
| 88 | } |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | public function findBlocks($block, &$blocks, &$post, $nested_block_name = null) |
| 94 | { |
| 95 | |
| 96 | /** |
| 97 | * If the block name is blank, skip |
| 98 | */ |
| 99 | if (strlen($block['blockName']) === 0) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * If the block is reusable, skip |
| 105 | */ |
| 106 | if ('core/block' === $block['blockName']) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | foreach ($block['innerBlocks'] as $inner_block) { |
| 111 | $this->findBlocks($inner_block, $blocks, $post, $block['blockName']); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * If block is not in blocks array, push the |
| 116 | * blockName into the array. |
| 117 | */ |
| 118 | if (!in_array($block['blockName'], array_column($blocks, 'name'), true)) { |
| 119 | $block_array = array( |
| 120 | 'name' => $block['blockName'], |
| 121 | 'posts' => array(), |
| 122 | ); |
| 123 | |
| 124 | array_push($blocks, $block_array); |
| 125 | } |
| 126 | |
| 127 | $block_key = $this->searchForBlockKey($blocks, 'name', $block['blockName']); |
| 128 | |
| 129 | if (!in_array($post->ID, array_column($blocks[$block_key]['posts'], 'id'), true)) { |
| 130 | $blocks[$block_key]['posts'][] = array( |
| 131 | 'id' => $post->ID, |
| 132 | 'title' => $post->post_title, |
| 133 | 'count' => 1, |
| 134 | 'isReusable' => 'wp_block' === $post->post_type, |
| 135 | 'isNested' => $nested_block_name !== null, |
| 136 | 'nestedBlockType' => $nested_block_name, |
| 137 | 'postType' => $post->post_type, |
| 138 | 'post_url' => get_permalink($post->ID), |
| 139 | 'edit_url' => home_url('/wp-admin/post.php?post=' . $post->ID . '&action=edit'), |
| 140 | ); |
| 141 | } else { |
| 142 | $post_key = $this->searchForBlockKey($blocks[$block_key]['posts'], 'id', $post->ID); |
| 143 | $blocks[$block_key]['posts'][$post_key]['count']++; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 |