-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbed-iSpring.php
More file actions
284 lines (261 loc) · 9.34 KB
/
Embed-iSpring.php
File metadata and controls
284 lines (261 loc) · 9.34 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/*
Plugin Name: Embed iSpring
Plugin URI: http://theblackchimp.com
description: Plugin to embed iSpring software generated HTML5 PPT's
Version: 1.0
Author: Harsh
Author URI: http://theblackchimp.com
License: GPL2
*/
defined('ABSPATH') or die('No script kiddies please!');
register_activation_hook(__FILE__, 'tbc_ispring_on_EI_activate');
function tbc_ispring_on_EI_activate()
{
global $wpdb;
$create_table_query = "
CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ispring_data` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`file_name` varchar(100) NOT NULL,
`file_path` text NOT NULL,
PRIMARY KEY (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($create_table_query);
}
function tbc_ispring_embedder_settings_page()
{
add_menu_page('Upload iSpring PPT', 'iSpring Embedder', 'manage_options', 'ispring-embedder', 'tbc_ispring_option_page_content');
}
add_action('admin_menu', 'tbc_ispring_embedder_settings_page');
function tbc_ispring_option_page_content()
{
?>
<style>
table {
border-collapse: collapse;
width: 90%;
text-align: center;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #fff
}
th {
background-color: #4dcadd;
color: white;
}
.ispring_form div {
padding-top: 10px;
}
.notice-error, .notice-updated {
margin-left: 0px !important;
}
</style>
<div>
<h2>Upload iSpring converted file </h2>
<?php
$notices = tbc_ispring_save_user_data();
tbc_ispring_display_Notices($notices);
?>
<form class="ispring_form" method="post" enctype="multipart/form-data">
<div>
<span><input type="text" id="file_title_name" required="" name="file_name" value=""
placeholder="Title"/></span>
</div>
<div>
<span><input type="file" id="ispring_file" required="" name="zip_file" value=""/></span>
</div>
</table>
<?php
submit_button('Upload File', 'primary', 'submit_ispring_form');
?>
</form>
</div>
<?php
tbc_ispring_display_uploaded_files_list();
}
function tbc_ispring_save_user_data()
{
if (!isset($_POST['submit_ispring_form']) && !empty($_GET['delete_ppt'])) {
if (is_numeric($_GET['delete_ppt'])) {
$notices = tbc_ispring_delete_ppt_by_id($_GET['delete_ppt']);
return $notices;
}
return;
}
if (isset($_POST['submit_ispring_form']) && !empty($_POST['submit_ispring_form'])) {
$error = new WP_Error();
if ($_FILES["zip_file"]["name"]) {
$filename = sanitize_file_name($_FILES["zip_file"]["name"]);
$source = $_FILES["zip_file"]["tmp_name"];
$type = sanitize_mime_type($_FILES["zip_file"]["type"]);
$title = $_POST['file_name'];
$name = explode(".", $filename);
$accepted_types = array(
'application/zip',
'application/x-zip-compressed',
'multipart/x-zip',
'application/x-compressed'
);
if (empty($title)) {
$error->add('error', 'Title Field cannot be empty.');
return $error;
}
foreach ($accepted_types as $mime_type) {
if ($mime_type == $type) {
$type_okay = true;
break;
}
}
if (!$type_okay) {
$error->add('error', 'The file you are trying to upload is not a .zip file type. Please try again.');
return $error;
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if (!$continue) {
$error->add('error', 'The file you are trying to upload is not a .zip file. Please try again.');
return $error;
}
$uploads = wp_upload_dir();
$target_path = $uploads['basedir'] . "/iSpring_embedder";
if (!is_dir($target_path))
wp_mkdir_p($target_path);
$target_file = $target_path . '/' . $filename;
if (move_uploaded_file($source, $target_file)) {
$zip = new ZipArchive;
if ($zip->open($target_file) == TRUE) {
$ext_folder = $ext_folder = explode("/",$zip->getNameIndex(0))[0];
$zip->extractTo($target_path);
$zip->close();
unlink($target_file);
}
$folder_name = '/ppt_' . time();
$folderUpdated = rename($target_path . '/' . $ext_folder, $target_path . $folder_name);
if ($folderUpdated) {
$path = 'iSpring_embedder' . $folder_name;
tbc_ispring_save_data_to_db($title, $path);
$error->add('success', 'File Uploaded Successfully.');
return $error;
}
} else {
$error->add('error', 'There was a with upload. Please try again.');
return $error;
}
}
}
}
function tbc_ispring_save_data_to_db($title, $path)
{
global $wpdb;
$title = sanitize_text_field($title);
$id = $wpdb->insert($wpdb->prefix . 'ispring_data', array(
'file_name' => $title,
'file_path' => $path
), array(
'%s',
'%s'
));
return $id;
}
function tbc_ispring_display_uploaded_files_list()
{
$data = tbc_ispring_get_ppt_data();
if (empty($data)) {
echo "No Files to display. Please Upload files to display here";
} else {
echo "<table><tr><th>Sr. No.</th><th>Name</th><th>Shortcode</th><th>Action</th></tr>";
$i = 1;
foreach ($data as $file):
$onClickJs = 'return confirm("Are you sure you want to delete?")';
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo esc_html($file->file_name); ?></td>
<td>[ppt_embedder id="<?php echo $file->id; ?>"]</td>
<td><a href=" <?php menu_page_url('ispring-embedder'); ?>&delete_ppt=<?php echo esc_html($file->id); ?>"
Onclick="<?php echo esc_js($onClickJs); ?>">Delete</a></td>
</tr>
<?php
$i++;
endforeach;
?>
</table>
</br>
<div>
<p>You can set dimensions of the <i>iframe</i> by adding width and height parameters to shortcode. </p>
<p>For Example : <strong>[ppt_embedder id='1' width='800px' height='300px']</strong></p>
</div>
<?php
}
}
add_shortcode('ppt_embedder', 'tbc_ispring_process_ppt_embedder_shortcode');
function tbc_ispring_process_ppt_embedder_shortcode($atts)
{
$atts = shortcode_atts(array(
'id' => '',
'width' => '100%',
'height' => '555px'
), $atts);
$uploads = wp_upload_dir();
if (!empty($atts['id']) && is_numeric($atts['id'])) {
$data = tbc_ispring_get_ppt_data($atts['id']);
if (count($data) >= 1) {
return '<iframe style="width:' . esc_attr($atts['width']) . '; height:' . esc_attr($atts['height']) . '"; src="' . $uploads['baseurl'] . '/' . $data[0]->file_path . '/index.html"> </iframe>';
} else {
return "Oops !! the files have been deleted.";
}
}
}
function tbc_ispring_get_ppt_data($id = '')
{
global $wpdb;
$id = esc_sql($id);
$query = 'SELECT * FROM ' . $wpdb->prefix . 'ispring_data';
if (!empty($id))
$query = 'SELECT * FROM ' . $wpdb->prefix . 'ispring_data where id=' . $id;
$data = $wpdb->get_results($query);
return $data;
}
function tbc_ispring_display_Notices($error)
{
if (is_wp_error($error)) {
$class = 'notice notice-error';
if (isset($error->errors['success']))
$class = 'updated notice-updated';
printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($error->get_error_message()));
}
}
function tbc_ispring_delete_ppt_by_id($id = '')
{
if (isset($id)) {
$id = esc_sql($id);
$data = tbc_ispring_get_ppt_data($id);
if (count($data) >= 1) {
global $wpdb;
$deleted = $wpdb->delete($wpdb->prefix . 'ispring_data', array(
'id' => $id
));
if ($deleted) {
$basedir = wp_upload_dir();
$path = $basedir['basedir'] . '/' . $data[0]->file_path;
tbc_ispring_removeDirectory($path);
$notice = new WP_Error('success', 'Deleted file Successfully.');
return $notice;
}
}
}
}
function tbc_ispring_removeDirectory($path)
{
$files = glob($path . '/*');
foreach ($files as $file) {
is_dir($file) ? tbc_ispring_removeDirectory($file) : unlink($file);
}
rmdir($path);
return;
}
?>