forked from Automattic/Post-Meta-Inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-meta-inspector.php
123 lines (106 loc) · 3.38 KB
/
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
<?php
/**
* Plugin Name: Post Meta Inspector
* Plugin URI: http://wordpress.org/extend/plugins/post-meta-inspector/
* Description: Peer inside your post meta. Admins can view post meta for any post from a simple meta box.
* Author: Daniel Bachhuber, Automattic
* Version: 1.1.1
* Author URI: http://automattic.com/
*/
define( 'POST_META_INSPECTOR_VERSION', '1.1.1' );
class Post_Meta_Inspector
{
private static $instance;
public $view_cap;
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Post_Meta_Inspector;
self::setup_actions();
}
return self::$instance;
}
private function __construct() {
/** Do nothing **/
}
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() {
$this->view_cap = apply_filters( 'pmi_view_cap', 'manage_options' );
if ( ! current_user_can( $this->view_cap ) || ! apply_filters( 'pmi_show_post_type', '__return_true', get_post_type() ) )
return;
add_meta_box( 'post-meta-inspector', __( 'Post Meta Inspector', 'post-meta-inspector' ), array( self::$instance, 'post_meta_inspector' ), get_post_type() );
}
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">' . __( '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 _e( 'Key', 'post-meta-inspector' ); ?></th>
<th class="value-column"><?php _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 );
$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; ?><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
}
}
function Post_Meta_Inspector() {
return Post_Meta_Inspector::instance();
}
add_action( 'plugins_loaded', 'Post_Meta_Inspector' );