-
Notifications
You must be signed in to change notification settings - Fork 16
/
class-post-meta-inspector.php
160 lines (147 loc) · 3.62 KB
/
class-post-meta-inspector.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
<?php
/**
* Main plugin container
*
* @package post-meta-inspector
*/
/**
* Post Meta Inspector
*/
class Post_Meta_Inspector {
/**
* Post_Meta_Inspector class
*
* @var object
*/
private static $instance;
/**
* Does user have the cap to view post meta?
*
* @var bool
*/
public $view_cap;
/**
* Kick off the instance.
*
* @return object
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Post_Meta_Inspector();
self::setup_actions();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
/** Do nothing */
}
/**
* Setup on init, add the metaboxes
*
* @return void
*/
private static function setup_actions() {
add_action( 'init', array( self::$instance, 'action_init' ) );
add_action( 'add_meta_boxes', array( self::$instance, 'action_add_meta_boxes' ) );
}
/**
* Init i18n files
*/
public function action_init() {
load_plugin_textdomain( 'post-meta-inspector', false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
/**
* Add the post meta box to view post meta if the user has permissions to
*/
public function action_add_meta_boxes() {
$post_type = get_post_type();
$this->view_cap = apply_filters( 'pmi_view_cap', 'manage_options' );
if ( empty( $post_type ) || ! current_user_can( $this->view_cap ) || ! apply_filters( 'pmi_show_post_type', '__return_true', $post_type ) ) {
return;
}
add_meta_box( 'post-meta-inspector', __( 'Post Meta Inspector', 'post-meta-inspector' ), array( self::$instance, 'post_meta_inspector' ), get_post_type() );
}
/**
* Output the post meta in metabox.
*
* @return void
*/
public function post_meta_inspector() {
$toggle_length = apply_filters( 'pmi_toggle_long_value_length', 0 );
$toggle_length = max( intval( $toggle_length ), 0 );
$toggle_el = '<a href="javascript:void(0);" class="pmi_toggle">' . esc_html__( 'Click to show…', 'post-meta-inspector' ) . '</a>';
?>
<style>
#post-meta-inspector table {
text-align: left;
width: 100%;
}
#post-meta-inspector table .key-column {
display: inline-block;
width: 20%;
}
#post-meta-inspector table .value-column {
display: inline-block;
width: 79%;
}
#post-meta-inspector code {
word-wrap: break-word;
}
</style>
<?php $custom_fields = get_post_meta( get_the_ID() ); ?>
<table>
<thead>
<tr>
<th class="key-column"><?php esc_html_e( 'Key', 'post-meta-inspector' ); ?></th>
<th class="value-column"><?php esc_html_e( 'Value', 'post-meta-inspector' ); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ( $custom_fields as $key => $values ) :
if ( apply_filters( 'pmi_ignore_post_meta_key', false, $key ) ) {
continue;
}
?>
<?php foreach ( $values as $value ) : ?>
<?php
$value = var_export( $value, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
$toggled = $toggle_length && strlen( $value ) > $toggle_length;
?>
<tr>
<td class="key-column"><?php echo esc_html( $key ); ?></td>
<td class="value-column">
<?php
if ( $toggled ) {
echo $toggle_el; // phpcs:ignore -- escaped earlier
}
?>
<code
<?php
if ( $toggled ) {
echo ' style="display: none;"';
}
?>
>
<?php echo esc_html( $value ); ?>
</code>
</td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>
<script>
jQuery(document).ready(function() {
jQuery('.pmi_toggle').click( function(e){
jQuery('+ code', this).show();
jQuery(this).hide();
});
});
</script>
<?php
}
}