-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacf-inflextor.php
222 lines (166 loc) · 4.8 KB
/
acf-inflextor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
Plugin Name: ACF Inflextor - Flexible Content Inspector
Plugin URI: https://briteweb.com
Description: Shows you which pages each layout in a Flexible Content field is used on
Version: 1.0.0
Author: Briteweb
Author URI: http://briteweb.com
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists('ACF_Inflextor') ) {
class ACF_Inflextor {
public function __construct() {
//if ( ! class_exists( 'ACF' ) ) return;
add_action( 'admin_menu', function() {
if ( ! class_exists( 'ACF' ) ) return;
add_submenu_page(
'edit.php?post_type=acf-field-group',
'Inflextor',
'Inflextor',
'manage_options',
'inflextor',
[ $this, 'admin_page' ]
);
}, 100);
}
public function get_fields() {
$all_groups = acf_get_field_groups();
$fields = [];
foreach ( $all_groups as $group ) {
$group_fields = acf_get_fields( $group );
foreach ( $group_fields as $field ) {
if ( $field['type'] == 'flexible_content' ) {
$fields[] = [
'field' => $field,
'group' => $group
];
}
}
}
//$this->pre($fields);
return $fields;
}
public function get_layouts() {
$key = $_GET['key'];
$field = acf_get_field( $key );
//$this->pre($field);
$field_name = $field['name'];
$all_posts = get_posts( [
'post_type' => 'any',
'posts_per_page' => -1,
'orderby' => 'post_title',
'order' => 'asc',
'meta_key' => $field_name
] );
$posts = [];
foreach ( $all_posts as $post ) {
$fields = get_field( $field_name, $post->ID );
if ( empty( $fields ) ) continue;
$post_layouts = [];
foreach ( $fields as $f ) {
$post_layouts[] = $f['acf_fc_layout'];
}
if ( empty( $post_layouts ) ) continue;
$post->layouts = $post_layouts;
$posts[] = $post;
}
$layouts = [];
foreach ( $field['layouts'] as $layout ) {
$layout_posts = [];
foreach ( $posts as $post ) {
if ( in_array( $layout['name'], $post->layouts ) ) $layout_posts[] = $post;
}
$layouts[] = [
'key' => $layout['key'],
'name' => $layout['name'],
'label' => $layout['label'],
'posts' => $layout_posts
];
}
//$this->pre($layouts);
return $layouts;
}
public function list_posts() {
$key = $_GET['key'];
$field = acf_get_field( $key );
$layouts = $this->get_layouts();
//$this->pre($layouts);
?>
<div class="wrap acf-settings-wrap">
<h1>ACF Inflextor</h1>
<h3>Showing all layouts for <?php echo $field['label']; ?></h3>
<?php foreach( $layouts as $layout ) : ?>
<div class="acf-box">
<div class="title">
<h3><?php echo $layout['label']; ?></h3>
</div>
<div class="inner">
<table class="wp-list-table widefat fixed striped">
<thead>
<th>Post</th>
<th>Type</th>
</thead>
<tbody>
<?php foreach ( $layout['posts'] as $post ) : ?>
<tr>
<td><a href=""><strong><?php echo $post->post_title; ?></strong></a></td>
<td><?php echo $post->post_type; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php endforeach; ?>
</div>
<?php
}
public function list_layouts() {
$fields = $this->get_fields();
?>
<div class="wrap acf-settings-wrap">
<h1>ACF Inflextor</h1>
<div class="acf-box">
<div class="title">
<h3>Flexible Content Layouts</h3>
</div>
<div class="inner">
<table class="wp-list-table widefat fixed striped">
<thead>
<th>Field</th>
<th>Group</th>
</thead>
<tbody>
<?php foreach ( $fields as $field ) : ?>
<tr>
<td><a href="<?php echo admin_url( 'edit.php?post_type=acf-field-group&page=inflextor&key=' . $field['field']['key'] ); ?>"><strong><?php echo $field['field']['label']; ?></strong></a></td>
<td><a href=""><?php echo $field['group']['title']; ?></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php
}
public function admin_page() {
if ( isset( $_GET['key'] ) && !empty( $_GET['key'] ) ) {
$this->list_posts();
} else {
$this->list_layouts();
}
}
public function pre( $arr ) {
echo '<pre>';
var_dump( $arr );
echo '</pre>';
}
}
new ACF_Inflextor();
}