forked from Automattic/node-red-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.node-red-wp-shortcodes.php
54 lines (47 loc) · 1.14 KB
/
class.node-red-wp-shortcodes.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
<?php
// Bail if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
die( 'Cheatin\', eh?' );
}
/**
* Node Red WordPress shortcodes
*
* @package node-red-wp
* @since 0.0.1
*/
class Node_Red_WP_Shortcodes {
/**
* Class constructor
*/
function __construct() {
add_shortcode( 'nodered_data', array( $this, 'shortcode__data' ) );
}
/**
* Data shortcode
*
* Returns the markup for a live data point on the front end
*
* @param array $atts Shortcode attributes
*
* @uses Node_Red_WP::init()
* @uses Node_Red_WP->data
* @uses Node_Red_WP->data->get()
* @uses esc_attr()
* @uses esc_html()
*
* @return string Formatted shortcode markup
*/
function shortcode__data( $atts ) {
// Get the data for the specified key
$val = Node_Red_WP::init()->data->get( $atts['key'] );
// Default the data value when it's not set
$val = null !== $val ? $val : '[undefined]';
// Create the shortcode markup
return sprintf( '<h2 class="entry-title">%s</h2><span class="nrwp-data nrwp-data-%s" data-key="%s">%s</span>',
esc_attr( $atts['title'] ),
esc_attr( $atts['key'] ),
esc_attr( $atts['key'] ),
esc_html( $val )
);
}
}