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