-
Notifications
You must be signed in to change notification settings - Fork 1
/
embed.php
156 lines (103 loc) · 4.19 KB
/
embed.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
<?php
/*
* WP Feature Box
* Embed feature
*/
if(!class_exists('WP_Feature_Embed')) {
class WP_Feature_Embed extends WP_Feature_Box {
var $ajax_action = 'wp_feature_box_embed';
public function __construct() {
add_action('init', array($this, 'print_embed_script'));
add_action('init', array($this, 'get_embed'));
add_action('wp_ajax_' . $this->ajax_action, array($this, 'get_embed'));
add_action('wp_ajax_no_priv_' . $this->ajax_action, array($this, 'get_embed'));
add_filter('wp_feature_box_options', array($this, 'embed_options'), 1, 1);
add_filter('wp_feature_box_js_settings', array($this, 'get_embed_settings'));
}
public function get_embed() {
if(isset($_REQUEST['action']) && $_REQUEST['action'] == $this->ajax_action) {
if(!isset($_REQUEST['ids']) || !is_array($_REQUEST['ids']) || empty($_REQUEST['ids']))
$this->ajax_response(array('error' => __('Missing feature box IDs', 'wp-feature-box')));
$ids = $_REQUEST['ids'];
$embed = array();
if(count($ids) > 1) {
$embed['html'] = $this->get_feature_box_slider($ids);
} else {
$embed['html'] = $this->get_feature_box(array_shift($ids));
}
$this->ajax_response($embed);
}
}
public function embed_options($settings) {
$options = array(
'allow_embed' => 1,
'default_embed_text' => __('View more at', 'wp-feature-box') . ' ' . get_bloginfo('name'),
'default_embed_link' => home_url('/')
);
return array_merge($settings, $options);
}
public function ajax_response($response) {
header('Content-type: application/javascript');
echo $_REQUEST['callback'] . '(' . str_replace(array('\n', '\t', '\r'), '', json_encode($response)) . ');';
exit;
}
public function get_footer_text() {
$options = $this->get_options();
return '<footer class="wp-feature-box-footer"><p><a href="' . $options['default_embed_link'] . '" target="_blank" rel="external">' . $options['default_embed_text'] . '</a></p></footer>';
}
public function get_embed_tool() {
ob_start();
?>
<div class="wp-feature-box-embed-action">
<a class="embed-icon" href="#" title="<?php _e('Share', 'wp-feature-box'); ?>"><?php _e('Embed', 'wp-feature-box'); ?></a>
<div class="embed-box">
<div class="embed-box-content">
<p><?php _e('Copy and paste the code below to embed this content on your page', 'wp-feature-box'); ?></p>
<textarea></textarea>
<a class="close-embed-tool" href="#"><?php _e('Close', 'wp-feature-box'); ?></a>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
public function get_embed_settings($settings) {
$options = $this->get_options();
$embed_settings = array(
'allowEmbed' => intval($options['allow_embed']),
'embedTool' => $this->get_embed_tool(),
'action' => $this->ajax_action,
'css' => $this->get_dir() . 'css/feature-box.css',
'scripts' => array(
array(
'srcUrl' => $this->get_dir() . 'js/sly.min.js',
'varName' => 'Sly'
),
array(
'srcUrl' => $this->get_dir() . 'js/feature-box.min.js',
'varName' => 'wpFeatureBox'
)
),
'footer' => $this->get_footer_text()
);
return array_merge($settings, $embed_settings);
}
public function print_embed_script() {
header('Content-type: application/javascript');
if(isset($_REQUEST['wp_feature_embed'])) {
$options = $this->get_options();
if($options['allow_embed']) {
echo 'var wpFeatureBoxSettings = ' . str_replace(array('\r', '\n', '\t'), '', json_encode($this->get_feature_box_js_settings())) . ';';
echo file_get_contents($this->get_path() . '/js/embed.min.js');
exit;
} else {
echo "console.log('" . __('Embed is disabled on', 'wp-feature-box') . " " . get_bloginfo('name') . "');";
exit;
}
}
}
}
}
if(class_exists('WP_Feature_Embed')) {
$wp_feature_embed = new WP_Feature_Embed();
}