-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwordpress-datascript.php
More file actions
155 lines (125 loc) · 4.56 KB
/
wordpress-datascript.php
File metadata and controls
155 lines (125 loc) · 4.56 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
<?php
/*
Plugin Name: WordPress DataScript
Plugin URI:
Description: A plugin that help you to put data changes of wordpress in post/page/cagegory in scripting
Author: Tran Dang Khoa
Author URI: excitingthing.com
Version: 0.6.1
Text Domain: wordpress-datascript
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
if(!function_exists('wp_get_current_user')) {
require_once(ABSPATH . "wp-includes/pluggable.php");
}
if(!function_exists('wp_insert_category')) {
require_once(ABSPATH . "wp-admin/includes/taxonomy.php");
}
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'WPDataScript' ) ) :
$wp_rewrite = new WP_Rewrite();
/**
* WordPress DataScript class to handle the data script loading and processing
*/
class WPDataScript {
private $errors = array();
private $success = false;
private $dataScript;
private function includes() {
include_once( 'includes/class-ds-command.php' );
include_once( 'includes/class-command-factory.php' );
include_once( 'includes/class-add-page-command.php' );
include_once( 'includes/class-update-page-command.php');
include_once( 'includes/class-add-category-command.php' );
include_once( 'includes/class-update-category-command.php');
}
/**
* WPDataScript Constructor.
* @access public
* @return
*/
public function __construct() {
$this->includes();
if(isset($_POST['wp-datascript-action']) && $_POST['wp-datascript-action'] == "process" ){
$this->processSubmitData();
}
add_action('admin_menu', array($this, 'addToolsMenu'));
}
public function processDataScript($dataScript) {
$commandData = json_decode($dataScript, true);
if($commandData == null) {
$this->errors[] = "Invalid JSON data";
return;
}
foreach($commandData as $command) {
$cmdName = $command['cmd'];
$actionCommand = DSCommandFactory::getCommand($cmdName);
if(!empty($actionCommand)) {
$result = $actionCommand->execute($command);
$commandErrors = $actionCommand->getErrors();
if(!$result && !empty($commandErrors)) {
$this->errors[] = $commandErrors[0];
}
} else {
$this->errors[] = "Command [".$cmdName."] is not supported";
}
}
$this->success = empty($this->errors);
}
protected function processSubmitData() {
$this->dataScript = $_POST['datascript'];
$this->processDataScript($this->dataScript);
}
public function addToolsMenu(){
add_menu_page( "Wordpress Datascript",
"Wordpress DataScript",
"manage_options",
'wp-datascript',
array($this, 'wpDataScriptCallBack'));
}
public function wpDataScriptCallBack() { ?>
<div class="wrap"><div id="icon-tools" class="icon32"></div>
<h2>Wordpress DataScript</h2>
</div>
<div id="submit-script-form">
<form action="" method="post" id="datascript-form">
<?php
if(isset($_POST['wp-datascript-action'])) {
?>
<textarea id="datascript" name="datascript" rows="25" cols="80"><?php echo $this->dataScript; ?></textarea>
<?php
if(!empty($this->errors)) {
?>
<p>
<span>
<?php echo $this->errors[0]; ?>
</span>
</p>
<?php
} else { ?>
<p>
<span>
Commands run successfully
</span>
</p>
<?php }
} else {
?>
<textarea id="datascript" name="datascript" rows="25" cols="80"></textarea>
<?php
}
?>
<p class="submit">
<input type="hidden" name="wp-datascript-action" value="process"/>
<input type="submit" class="button-primary" name="Submit" value="Submit"/>
</p>
</form>
</div>
<?php
}
}
$GLOBALS['WPDataScript'] = new WPDataScript();
endif;
?>