-
Notifications
You must be signed in to change notification settings - Fork 0
/
anything.php
174 lines (156 loc) · 5 KB
/
anything.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
<?php
/**
* Plugin Name: Anything Block
* Plugin URI: https://wplemon.com
* Author: Ari Stathopoulos
* Author URI: http://aristath.github.io
* Version: 1.0.3
* Description: Print any kind of data, any way you want it.
* Text Domain: anything-block
*
* @package anything-block
* @category Core
* @author Ari Stathopoulos
* @copyright Copyright (c) 2020, Ari Stathopoulos
* @license https://opensource.org/licenses/MIT
* @since 1.0.0
*/
/**
* Register the block.
*
* @since 1.0.0
* @return void
*/
function wplemon_anything_block_init() {
wp_register_script(
'anything-block',
plugins_url( 'block.js', __FILE__ ),
array( 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-editor', 'wp-server-side-render', 'wp-data' ),
filemtime( __DIR__ . '/block.js' ),
false
);
register_block_type(
'wplemon/anything',
array(
'attributes' => array(
'htmlData' => array(
'type' => 'text',
'default' => '',
),
'dataSourceName' => array( // Backwards-compatibility.
'type' => 'text',
'default' => '',
),
'dataSource' => array( // Backwards-compatibility.
'type' => 'text',
'default' => 'anything',
),
),
'editor_script' => 'anything-block',
'render_callback' => 'wplemon_anything_block_render_callback',
)
);
}
/**
* Render callback for dynamic block.
*
* @since 1.0.0
* @param array $atts The block attributes.
* @param string $content The block content.
* @return string
*/
function wplemon_anything_block_render_callback( $atts, $content ) {
if ( ! isset( $atts['dataSource'] ) || ! isset( $atts['htmlData'] ) ) {
return '';
}
$value = '';
$html = $atts['htmlData'];
if ( isset( $atts['dataSource'] ) && 'anything' !== $atts['dataSource'] ) { // Backwards-compatibility.
if ( isset( $atts['dataSourceName'] ) && '' !== $atts['dataSourceName'] ) {
$html = str_replace( '{data}', "{data.{$atts['dataSource']}.{$atts['dataSourceName']}}", $html );
} else {
$html = str_replace( '{data.', "{data.{$atts['dataSource']}.", $html );
}
}
$value = array(
'setting' => json_decode( wp_json_encode( wp_load_alloptions() ), true ),
'themeMod' => json_decode( wp_json_encode( get_theme_mods() ), true ),
'post' => json_decode( wp_json_encode( get_post( get_the_ID() ) ), true ),
);
$value['post']['meta'] = json_decode( wp_json_encode( get_post_meta( get_the_ID() ) ), true );
$string_parts = wplemon_anything_get_string_parts( $html );
foreach ( $string_parts as $string_part ) {
$search = str_replace( 'anythingData', 'data', $string_part );
$replace = wplemon_anything_get_part_value( $string_part, $value );
if ( 'data' !== $search && is_string( $replace ) ) {
$html = str_replace(
'{' . $search . '}',
$replace,
$html
);
}
}
return $html;
}
add_action( 'init', 'wplemon_anything_block_init' );
/**
* Get search strings from our HTML.
*
* @since 1.1.0
* @param string $html The HTML.
* @return array
*/
function wplemon_anything_get_string_parts( $html ) {
$the_parts = array();
$parts = explode( '{data', $html );
foreach ( $parts as $part ) {
$the_parts[] = 'anythingData' . explode( '}', $part )[0];
}
return $the_parts;
}
/**
* Get the value of a part.
*
* @since 1.1.0
* @param string $part The part we're looking for.
* @param array $values Where we're looking for the part.
* @return string
*/
function wplemon_anything_get_part_value( $part, $values ) {
if ( is_string( $values ) || is_bool( $values ) || is_numeric( $values ) ) {
return (string) $values;
}
$values = (array) $values;
$part = str_replace( 'anythingData.', '', $part );
if ( isset( $values[ $part ] ) && ! is_array( $values[ $part ] ) && ! is_object( $values[ $part ] ) ) {
return $values[ $part ];
}
if ( false !== strpos( $part, '.' ) ) {
$fragment = explode( '.', $part )[0];
if ( isset( $values[ $fragment ] ) ) {
return wplemon_anything_get_part_value( str_replace( "$fragment.", '', $part ), $values[ $fragment ] );
}
}
if ( ! is_array( $values ) ) {
return $values;
}
if ( current_user_can( 'update_core' ) ) {
$debug = '<table style="font-size:0.6em;background:#fef8ee;color:#333;border:1px solid #f0b849;white-space:pre-wrap" border="1">';
$debug .= '<thead><tr><td>' . esc_html__( 'Name', 'anything-block' ) . '</td><td>' . esc_html__( 'Type', 'anything-block' ) . '</td><td>' . esc_html__( 'Value', 'anything-block' ) . '</td></tr</thead>';
foreach ( $values as $key => $val ) {
$debug .= '<tr>';
$debug .= "<td style='vertical-align:top'><code>$key</code></td>";
$debug .= '<td style="vertical-align:top"><code>' . gettype( $val ) . '</td>';
if ( is_array( $val ) || is_object( $val ) ) {
$val = (array) $val;
$debug .= '<td style="vertical-align:top"><ul><li><code>' . implode( '</code></li><li><code>', array_keys( $val ) ) . '</code></li></ul></td>';
} else {
$debug .= "<td style='vertical-align:top'><code>$val</code></td>";
}
$debug .= '</tr>';
}
$debug .= '</table>';
return $debug;
}
return '';
}