-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnutrifox.php
129 lines (123 loc) · 3.85 KB
/
nutrifox.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
<?php
/**
* Plugin Name: Nutrifox WP Connector
* Plugin URI: https://nutrifox.com/
* Description: Embed Nutrifox recipes in WordPress in one moment or less.
* Author: Pinch of Yum
* Author URI: http://pinchofyum.com/
* Text Domain: nutrifox
* Domain Path: /languages
* Version: 0.2.0
* GitHub Plugin URI: https://github.com/pinchofyum/nutrifox-plugin
*
* @package Nutrifox
*/
/**
* Register the Nutrifox shortcode and oEmbed handler
*/
function nutrifox_action_init() {
add_shortcode( 'nutrifox', 'nutrifox_shortcode' );
wp_embed_register_handler( 'nutrifox', '#^https?://nutrifox\.(?<tld>com|test)/(embed/label|recipes)/(?P<id>\d+)(/edit)?#', 'nutrifox_shortcode' );
if ( function_exists( 'register_block_type' ) ) {
$time = filemtime( dirname( __FILE__ ) . '/assets/js/nutrifox-resize.js' );
wp_register_script(
'nutrifox-resize',
plugins_url( 'assets/js/nutrifox-resize.js?r=' . (int) $time, __FILE__ )
);
$time = 0;
if ( file_exists( dirname( __FILE__ ) . '/assets/dist/block-editor.build.js' ) ) {
$time = filemtime( dirname( __FILE__ ) . '/assets/dist/block-editor.build.js' );
}
wp_register_script(
'nutrifox-block-editor',
plugins_url( 'assets/dist/block-editor.build.js?r=' . (int) $time, __FILE__ ),
array(
'nutrifox-resize',
'wp-blocks',
'wp-editor',
'wp-element',
'wp-components',
'wp-data',
'wp-i18n',
)
);
register_block_type(
'nutrifox/nutrifox',
array(
'attributes' => array(
'id' => array(
'type' => 'number',
),
'url' => array(
'type' => 'string',
),
),
'editor_script' => 'nutrifox-block-editor',
'render_callback' => 'nutrifox_shortcode',
)
);
}
}
add_action( 'init', 'nutrifox_action_init' );
/**
* Render the Nutrifox shortcode
*
* @param array $attr Shortcode attributes.
*/
function nutrifox_shortcode( $attr ) {
if ( empty( $attr['id'] ) && empty( $attr['url'] ) ) {
return '';
}
$attr = array_merge(
array(
'tld' => 'com',
),
$attr
);
if ( ! empty( $attr['url'] ) ) {
if ( is_numeric( $attr['url'] ) ) {
$attr['id'] = $attr['url'];
} else {
preg_match( '#^https?://nutrifox\.(?<tld>com|test)/(embed/label|recipes)/(?P<id>\d+)(/edit)?#', $attr['url'], $matches );
if ( empty( $matches ) ) {
if ( current_user_can( 'edit_posts' ) ) {
return __( 'Invalid Nutrifox URL provided.', 'nutrifox' );
}
return '';
}
$attr['id'] = $matches['id'];
$attr['tld'] = $matches['tld'];
}
}
$nutrifox_id = (int) $attr['id'];
$nutrifox_iframe_url = sprintf( 'http://nutrifox.%s/embed/label/%d', $attr['tld'], $nutrifox_id );
$nutrifox_resize_script = file_get_contents( dirname( __FILE__ ) . '/assets/js/nutrifox-resize.js' );
ob_start(); ?>
<script type="text/javascript">
<?php echo $nutrifox_resize_script; ?>
</script>
<iframe id="<?php echo esc_attr( 'nutrifox-label-' . $nutrifox_id ); ?>" src="<?php echo esc_url( $nutrifox_iframe_url ); ?>" style="width:100%;border-width:0;"></iframe>
<?php
return trim( ob_get_clean() );
}
/**
* Transform Nutrifox embed codes into their shortcode
*
* @param string $content Content to search through.
* @return string
*/
function nutrifox_filter_content_save_pre( $content ) {
if ( false === stripos( $content, 'nutrifox-label' ) ) {
return $content;
}
// $content comes through slashed
$needle = '#<div class=\\\"nutrifox-label.+data-recipe-id=\\\"([^"]+)\\\".+\n*<script[^>]+src=\\\"https://nutrifox\.com/embed\.js[^>]+></script>#';
if ( preg_match_all( $needle, $content, $matches ) ) {
$replacements = array();
foreach ( $matches[0] as $key => $value ) {
$content = str_replace( $value, '[nutrifox id=\"' . (int) $matches[1][ $key ] . '\"]', $content );
}
}
return $content;
}
add_action( 'content_save_pre', 'nutrifox_filter_content_save_pre' );