-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImportExport.php
433 lines (327 loc) · 16.7 KB
/
ImportExport.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
/**
*
* Copyright (c) 2017 MPAT Consortium , All rights reserved.
* Fraunhofer FOKUS, Fincons Group, Telecom ParisTech, IRT, Lacaster University, Leadin, RBB, Mediaset
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Aurelien David https://github.com/aureliendavid/
* Jean-Claude Dufourd ([email protected])
* Jean-Philippe Ruijs https://github.com/jeanphilipperuijs/
**/
/*
* Plugin Name: MPAT ImportExport
* Plugin URI: https://github.com/aureliendavid/mpat-importexport/
* Description: Import and export MPAT pages and layout
* Version: 1.0.beta
* Author: Aurelien David
* Author URI: https://github.com/aureliendavid/
* License: GPL2
* Text Domain: mpat-importexport
* Domain Path: /languages
*/
namespace MPAT\ImportExport;
require "mpatie_export.php";
require "mpatie_import.php";
class ImportExport {
public $message;
function import_export_init() {
load_plugin_textdomain('mpat-importexport', false, basename( dirname( __FILE__ ) ) . '/languages' );
add_menu_page('MPAT ImportExport', __('ImportExport', 'mpat-importexport'), 'edit_pages', 'MPAT_ImportExport', array(&$this, 'display'), 'dashicons-download');
// added option for gz compression
add_option('mpatImportExport', '');
add_settings_section(
'importexport_settings_section',
__('Import Export', "mpat-importexport"),
function(){
_e('Use compression during import and export', "mpat-newpage-wizard");
},
'general'
);
add_settings_field(
'mpatImportExport',
__('Compression', "mpat-newpage-wizard"),
function($args){
$option = get_option($args[0],'');
$cmp = strcmp($option , "on");
$chk = ($cmp == 0) ? 'checked' : '';
echo '<input type="checkbox" id="'. $args[0] .'" name="'. $args[0] .'" '. $chk .' />';
},
'general',
'importexport_settings_section',
array('mpatImportExport')
);
register_setting('general', 'mpatImportExport','esc_attr');
//register_setting('mpatImportExport-settings-group', 'mpatImportExport' );
}
function import_export_onload() {
$zipped = get_option('mpatImportExport') == 'on';
if ( isset($_GET['page']) && strtolower($_GET['page']) === "mpat_importexport" )
{
$action = isset($_GET['action']) ? $_GET['action'] : "";
switch ($action) {
case 'export':
handle_export($zipped);
exit();
break;
case 'import':
$this->message = handle_import($zipped);
break;
default:
break;
}
}
}
function display() {
$zipped = "";
if(get_option('mpatImportExport') == 'on'){
$zipped="checked";
}
wp_enqueue_style('mpat-importexport', plugin_dir_url(__FILE__) . 'mpat_import_export.css');
?>
<h2><?php _e('MPAT Importer/Exporter for','mpat-importexport'); ?> <?php echo bloginfo('name'); ?></h2>
<script src="<?php echo plugin_dir_url(__FILE__); ?>mpat_import_export.js" > </script>
<div style="" >
<!-- <details id="options">
<summary><?php _e('Options','mpat-importexport'); ?></summary>
<form method="post" action="./options.php">
<?php settings_fields( 'mpatImportExport-settings-group' ); ?>
<?php do_settings_sections( 'mpatImportExport-settings-group' ); ?>
<table class="form-table" style="width: 300px; border-width: 1px;">
<tr>
<td>
<td><?php _e('Use compression during import and export','mpat-importexport') ?></td>
<td><input type="checkbox" name="mpatImportExport" <?php echo $zipped ?> /></td>
</tr>
<tr>
<td colspan="2">
<?php submit_button(); ?>
</td>
</tr>
</table>
</form>
</details>-->
</div>
<div id="toolbarzone">
<div id="importzone" class="iexport-toolbar">
<div style="display: inline-block;">
<form action="<?php echo str_replace("//","/", $_SERVER['REQUEST_URI']) . '&action=import'; ?>"
id="page-form" method="post" enctype="multipart/form-data" target="_top" >
<input id="page-fileinput" type="file" name="page" style="display: none;" />
<button type="button" id="page-btn"
style="display:inline;vertical-align:center;"
title="<?php _e('Import new pages', 'mpat-importexport'); ?>">
<?php _e('Import', 'mpat-importexport'); ?><span class='dashicons dashicons-plus'></span>
</button>
</form>
</div>
<div style="display: inline-block;">
<form action="<?php echo str_replace("//","/", $_SERVER['REQUEST_URI']) . '&action=import'; ?>"
id="layout-form" method="post" enctype="multipart/form-data" target="_top" >
<input id="layout-fileinput" type="file" name="layout" style="display: none;" />
<button type="button" id="layout-btn" style="display:inline;vertical-align:center;"
title="<?php _e('Import new layouts', 'mpat-importexport'); ?>">
<?php _e('Import Layouts', 'mpat-importexport'); ?>
<span class='dashicons dashicons-welcome-add-page'></span>
</button>
</form>
</div>
</div>
<span> </span>
<div id="exporttb" class="iexport-toolbar">
<button onClick="fake_submit(this.id);" id="btn-exportall"
title="<?php _e('Export all pages', 'mpat-importexport'); ?>">
<?php _e('Export ALL', 'mpat-importexport'); ?>
<span class='dashicons dashicons-media-archive'></span>
</button>
<!-- <button onClick="fake_submit(this.id);" disabled id="btn-addmedia"
title="<?php _e('Export selected pages and media', 'mpat-importexport'); ?>">
<?php _e('Exp. Selected w. Media', 'mpat-importexport'); ?>
<span class='dashicons dashicons-media-video'></span>
</button> -->
<button onClick="fake_submit(this.id);" disabled id="btn-exportpages"
title="<?php _e('Export selected pages', 'mpat-importexport'); ?>">
<?php _e('Exp. Selected', 'mpat-importexport'); ?>
<span class='dashicons dashicons-media-document'></span>
</button>
<button onClick="fake_submit(this.id);" disabled id="btn-exportlayouts"
title="<?php _e('Export layouts of selected pages', 'mpat-importexport'); ?>">
<?php _e('Exp. Layouts', 'mpat-importexport'); ?>
<span class='dashicons dashicons-media-default'></span>
</button>
</div>
<?php
if ($this->message && !empty($this->message)) {
echo "<div id='msgzone' >\n";
echo $this->message ;
echo "\n</div>\n";
}
?>
</div>
<div id="exportzone">
<form style="display: inline; margin: 0; padding: 0;" method="post" action="<?php echo str_replace("//","/", $_SERVER['REQUEST_URI']) . '&action=export'; ?>" id="exportform" >
<div id="exporttb" class="iexport-toolbar">
<button type="submit" hidden="1" id="frm-btn-exportall" name="exportall" value="1" ></span></button>
<!-- <button type="submit" hidden="1" id="frm-btn-addmedia" name="addmedia" value="1" ></button> -->
<button type="submit" hidden="1" id="frm-btn-exportpages" name="exportpages" value="1" ></button>
<button type="submit" hidden="1" id="frm-btn-exportlayouts" name="exportlayouts" value="1"></button>
</div>
<details open>
<summary><?php _e('Page export settings', 'mpat-importexport'); ?></summary>
<br />
<input type='checkbox' name='chk_addmedia' id='chk_addmedia' checked>
<label for='chk_addmedia'><?php _e('Include local media', 'mpat-importexport'); ?></label>
<br />
<input type='checkbox' name='tl_options' id='tl_options'>
<label for='tl_options'><?php _e('Include timeline options (dsmcc, timeline_scenario)', 'mpat-importexport'); ?></label>
<br />
<input type='checkbox' name='custom_css' id='custom_css'>
<label for='custom_css'><?php _e('Include custom styles', 'mpat-importexport'); ?></label>
</details>
<br />
<table class="iexport-table">
<thead>
<tr>
<td><input type="checkbox" onchange="checkAll(this);"></td>
<td><?php _e('Title', 'mpat-importexport'); ?></td>
<td><?php _e('Id', 'mpat-importexport'); ?></td>
<td><?php _e('Layout', 'mpat-importexport'); ?></td>
<td><?php _e('Map', 'mpat-importexport'); ?></td>
<!-- <td><?php _e('Export', 'mpat-importexport'); ?></td> -->
</tr>
</thead>
<tbody>
<?php
$all = ImportExport::getAll();
$first_model = false;
echo "<tr><td colspan='100'><b>".translate('Pages', 'mpat-importexport')."</b></td></tr>";
foreach ($all as $o) {
if (isset($o['page'])) {
$id = $o['page']['ID'];
if (!$first_model) {
if ($o['page']['post_type'] == 'page_model') {
echo "<tr><td colspan='100'><b>".translate('Page Models', 'mpat-importexport')."</b></td></tr>";
$first_model = true;
}
}
echo "<tr>";
echo "<td><input type='checkbox' name='pageid[]' value='$id'></td>\n";
echo "<td>" . $o['page']['post_title'] . "</td>";
echo "<td>$id</td>";
$lname = "";
if (isset($o['page_layout'])) {
$lid = $o['page_layout']['ID'];
$lname = $o['page_layout']['post_title'];
echo "<td>" . $lname . "</td>\n";
echo "<td><canvas id='canvas-$id' width='128' height='72' /></td>\n";
//$zones = $o['page_layout']['meta']['mpat_content']['layout'];
//$content = $o['page']['meta']['mpat_content']['content'];
if (isset($o['page_layout']['meta']['mpat_content']['layout']))
$zones = $o['page_layout']['meta']['mpat_content']['layout'];
else
$zones = array();
if (isset($o['page']['meta']['mpat_content']['content']))
$content = $o['page']['meta']['mpat_content']['content'];
else
$content = array();
echo "<script>\n";
echo "var ctx = window.getCtx('$id');\n";
foreach ($zones as $z) {
$type = reset($content[ $z['i'] ])['type'];
$text = strtoupper($type[0]);
echo "window.drawComponent(ctx, $z[x], $z[y], $z[w], $z[h], '$text');\n";
}
echo "</script>\n";
/* echo "<td><div id='exportbtns'>";
echo "<a href='".str_replace("//","/", $_SERVER['REQUEST_URI'])."&action=export&pageid=$id&addmedia=1' title='".__("Page + Media", "mpat-importexport") ."'><span class='dashicons dashicons-media-video'></span></a> ";
echo "<a href='".str_replace("//","/", $_SERVER['REQUEST_URI'])."&action=export&pageid=$id' title='".__("Page only","mpat-importexport")."'><span class='dashicons dashicons-media-document'></span></a> ";
echo "<a href='".str_replace("//","/", $_SERVER['REQUEST_URI'])."&action=export&layoutid=$lid' title='".__("Layout only","mpat-importexport").'><span class='dashicons dashicons-media-default'></span></a>";
echo "</div></td>";*/
}
echo "</tr>";
}
}
?>
</tbody>
</table>
</form>
</div>
<?php
}
static function getFullUrl($url) {
// absolute url with same protocol
if (substr($url, 0, 2) == "//") {
$url = $_SERVER['REQUEST_SCHEME'] . $url;
}
// absolute url with same protocol/domain/port
// maybe use SERVER_NAME / SERVER_PORT instead ? or WP_DOMAIN ?
else if ( $url[0] == '/' ) {
$url = $_SERVER['WP_HOME'] . $url;
}
// relative url from current site
else if (!strpos($url, "://")) {
$url = $_SERVER['WP_SITEURL'] . $url;
}
return $url;
}
static function getLayoutFromObject($layout) {
$layout = $layout->to_array();
if ($layout['post_status'] == 'publish' || $layout['post_status'] == 'draft') {
$meta = get_post_meta($layout['ID'], 'mpat_content', true);
$layout['meta'] = array('mpat_content' => $meta);
return array( "page_layout" => $layout );
}
return null;
}
static function addPages(&$main_pages, &$pages) {
foreach ($pages as $page) {
$page = $page->to_array();
$meta = get_post_meta($page['ID'], 'mpat_content', true);
$page['meta'] = array('mpat_content' => $meta);
$o = array(
"page" => $page,
);
//attach layout
if (isset($meta["layoutId"]))
{
$layout = get_post( $meta["layoutId"] );
if ($layout && $layout->post_type == "page_layout") {
if ($l = ImportExport::getLayoutFromObject($layout))
$o += $l;
}
}
array_push($main_pages, $o);
}
}
static function getAll($ids=null) {
$main_pages = array();
if ($ids) {
$pages = get_pages( array('include' => $ids, 'post_status' => array('publish', 'draft')) );
$models = get_posts( array('post_type' => 'page_model', 'include' => $ids, 'post_status' => array('publish', 'draft')) );
}
else {
$pages = get_pages( array('post_status' => array('publish', 'draft')) );
$models = get_posts( array('post_type' => 'page_model', 'post_status' => array('publish', 'draft')) );
}
ImportExport::addPages($main_pages, $pages);
ImportExport::addPages($main_pages, $models);
return $main_pages;
}
}
$ie = new ImportExport();
add_action("admin_menu", array(&$ie, "import_export_init"));
add_action('wp_loaded', array(&$ie,"import_export_onload"));
?>