PluginProbe ʕ •ᴥ•ʔ
Presto Player / 1.10.2
Presto Player v1.10.2
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 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