-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
250 lines (203 loc) · 6.56 KB
/
functions.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* Functions.
*/
function fjh_textdomain()
{
return 'find-junk-html';
}
function fjh_get_all_needles()
{
$needles = [
[
'code' => '<h1',
'tag' => 'h1',
'desc' => 'The h1 tag is used by the theme for the page title.',
],
[
'code' => '<div',
'tag' => 'div',
'desc' => 'The div tag is rarely required, and often part of an old page builder. When in doubt, ask the theme developer.',
],
[
'code' => '<span',
'tag' => 'span',
'desc' => 'The span tag is rarely required, and often part of an old page builder. When in doubt, ask the theme developer.',
],
[
'code' => '<b',
'tag' => 'b',
'desc' => 'The b tag has been superseded by the strong tag. Used for strong importance. Not for its bold look.',
],
[
'code' => '<i',
'tag' => 'i',
'desc' => 'The i tag has been superseded by the em tag. Used for emphasis. Not for its italic look.',
],
[
'code' => ' style=',
'tag' => 'style',
'desc' => 'The style tag is almost always a relic from a previous website. The theme should take care of styling.',
],
];
return apply_filters( 'fjh_needles', $needles );
}
function fjh_get_active_needles()
{
$needles = fjh_get_all_needles();
$junk = get_option( 'fjh_options' )['fjh_junk'] ?? [];
$active_needles = array_filter( $needles, function ( $item ) use ( $junk ) {
return in_array( $item['tag'], $junk );
});
return $active_needles;
}
function fjh_find_junk_posts()
{
global $wpdb;
$needles = fjh_get_active_needles();
if ( empty( $needles ) )
return [];
$junk_posts = [];
foreach ( $needles as $needle )
{
$posts = $wpdb->get_results( "SELECT ID FROM {$wpdb->prefix}posts WHERE ( post_content LIKE '%".$needle['code']."%' OR post_excerpt LIKE '%".$needle['code']."%' ) AND post_status = 'publish' AND post_type NOT IN ( 'revision', 'oembed_cache' )" );
if ( !empty( $posts ) )
{
foreach ( $posts as $post )
{
if ( isset( $junk_posts[ $post->ID ] ) && in_array( $needle['tag'], $junk_posts[ $post->ID ] ) )
continue;
$junk_posts[ $post->ID ][] = $needle['tag'];
}
}
$postmetas = $wpdb->get_results( "SELECT post_id FROM {$wpdb->prefix}postmeta pm INNER JOIN {$wpdb->prefix}posts p ON pm.post_id = p.ID WHERE pm.meta_value LIKE '%".$needle['code']."%' AND p.post_status = 'publish' AND p.post_type NOT IN ( 'revision', 'oembed_cache' )" );
if ( !empty( $postmetas ) )
{
foreach ( $postmetas as $postmeta )
{
if ( isset( $junk_posts[ $postmeta->post_id ] ) && in_array( $needle['tag'], $junk_posts[ $postmeta->post_id ] ) )
continue;
$junk_posts[ $postmeta->post_id ][] = $needle['tag'];
}
}
}
krsort( $junk_posts );
return $junk_posts;
}
function fjh_nav()
{
$pages = [
[
'link' => admin_url( 'options-general.php?page=fjh-options' ),
'title' => __( 'Settings', fjh_textdomain() ),
],
[
'link' => admin_url( 'tools.php?page=fjh' ),
'title' => __( 'Find Junk HTML', fjh_textdomain() ),
],
];
?>
<link rel="stylesheet" href="<?=plugins_url( 'find-junk-html.min.css', __FILE__ );?>" />
<div class="fjh-acf-admin-toolbar">
<h2><i class="fjh-acf-tab-icon dashicons dashicons-trash"></i> Find Junk HTML</h2>
<?php
foreach ( $pages as $page )
{
$is_active = strpos( $page['link'], $_SERVER['REQUEST_URI'] ) !== false ? 'is-active' : '';
?>
<a class="fjh-acf-tab <?=$is_active;?>" href="<?=$page['link'];?>"><?=$page['title'];?></a>
<?php
}
?>
</div>
<?php
}
/**
* Page: FJH.
*/
function fjh_page_fjh()
{
$junk_posts = fjh_find_junk_posts();
?>
<div class="wrap fjh-wrapper">
<h1><?php _e( 'Find Junk HTML', fjh_textdomain() ); ?></h1>
<?php if ( !empty( $junk_posts ) ): ?>
<table class="wp-list-table widefat fixed striped" style="width: auto; margin-top: 16px;">
<thead>
<tr>
<td><?php _e( 'ID', fjh_textdomain() ); ?></td>
<td><?php _e( 'Type', fjh_textdomain() ); ?></td>
<td><?php _e( 'Title', fjh_textdomain() ); ?></td>
<td><?php _e( 'Junk', fjh_textdomain() ); ?></td>
</tr>
</thead>
<tbody>
<?php foreach ( $junk_posts as $post_id => $tags ): ?>
<tr>
<td><?=$post_id;?></td>
<td><?=get_post_type_object( get_post_type( $post_id ) )->labels->singular_name;?></td>
<td><a href="<?=get_edit_post_link( $post_id );?>" title="<?php _e( 'Edit this post', fjh_textdomain() ); ?>"><?=get_the_title( $post_id );?></a></td>
<td><?=implode( ' ', $tags );?></td>
</tr>
<?php endforeach; // $junk_posts ?>
</tbody>
</table>
<?php else: // $junk_posts is empty ?>
<p><?php _e( 'No HTML junk was found in your posts. Nice!', fjh_textdomain() ); ?></p>
<?php endif; // $junk_posts ?>
</div><!-- wrap -->
<?php
}
/**
* Page: Options.
*/
function fjh_page_options()
{
?>
<div class="wrap fjh-wrapper">
<h1><?php _e( 'Settings', fjh_textdomain() ); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields('fjh_options');
do_settings_sections('fjh_needles');
submit_button();
?>
</form>
</div><!-- wrap -->
<?php
}
/**
* Callbacks.
*/
function fjh_needles_cb( $args )
{
echo '<p>'.__( 'What do you consider junk?', fjh_textdomain() ).'</p>';
}
function fjh_junk_cb()
{
echo fjh_form_field_checkbox_group( 'fjh_options', 'fjh_junk' );
}
/**
* Form field HTML.
*/
function fjh_form_field_checkbox_group( $options, $field_name )
{
if ( !$options || !$field_name )
return;
$needles = fjh_get_all_needles();
if ( empty( $needles ) )
return;
$values = get_option( $options )[ $field_name ] ?? [];
foreach ( $needles as $needle )
{
$checked = in_array( $needle['tag'], $values ) ? 'checked' : '';
?>
<div class="checkbox">
<label for="label-<?=$needle['tag'];?>" title="<?=$needle['desc'] ?? '';?>">
<input type="checkbox" name="<?=$options;?>[<?=$field_name;?>][]" id="label-<?=$needle['tag'];?>" value="<?=$needle['tag'];?>" <?=$checked;?>>
<?=$needle['tag'];?>
</label>
</div>
<?php
}
}