PluginProbe ʕ •ᴥ•ʔ
Presto Player / 4.3.1
Presto Player v4.3.1
4.3.1 4.3.0 4.2.4 4.2.3 4.2.2 4.2.0 4.2.1 trunk 1.10.0 1.10.1 1.10.2 1.11.0 1.12.0 1.13.0 1.14.0 1.14.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.13 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3-beta1 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.0-beta1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1.0 3.1.1 3.1.2 3.1.3 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4
presto-player / inc / Support / BlockFinder.php
presto-player / inc / Support Last commit date
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