-
Notifications
You must be signed in to change notification settings - Fork 3
/
ojsimport-admin.php
executable file
·197 lines (148 loc) · 6.22 KB
/
ojsimport-admin.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
<?php
/*
Copyright (c) 2012, Matthew Crider
Code based on SMW Import plugin by Christoph Herbst (http://wordpress.org/extend/plugins/smw-import/).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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 General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once('ojsimport.php');
// Hook for adding admin menus
add_action('admin_menu', 'ojsimport_add_pages');
add_action('ojsimport_import_all_event', 'ojsimport_import_all' );
register_activation_hook( __FILE__, 'ojsimport_on_activation' );
function ojsimport_on_activation() {
// check existing data sources
$datasources = get_option('ojsimport_data_sources',array());
if ( empty($datasources) ){
// add default data source
$datasources[] = dirname(__FILE__) . '/example_data.json';
update_option( 'ojsimport_data_sources', $datasources );
}
}
function ojsimport_import_all(){
ojsimport::import_all();
}
// action function for above hook
function ojsimport_add_pages() {
$title = 'ojs Import';
$slug = 'ojsimport';
// Add a new submenu under Tools:
add_management_page( __($title,'menu-ojsimport'), __($title,'menu-ojsimport'), 'manage_options',$slug, 'ojsimport_tools_page');
// Add a new submenu under Settings:
add_options_page(__($title,'menu-ojsimport'), __($title,'menu-ojsimport'), 'manage_options', $slug, 'ojsimport_settings_page');
}
// mt_tools_page() displays the page content for the Test Tools submenu
function ojsimport_tools_page() {
//must check that the user has the required capability
if (!current_user_can('manage_options'))
{
wp_die( __('You do not have sufficient permissions to access this page.') );
}
echo "<h2>" . __( 'ojs Import', 'menu-ojsimport' ) . "</h2>";
$hidden_field_name = 'ojsimport_submit_hidden';
// See if the user has posted us some information
// If they did, this hidden field will be set to 'Y'
if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
$class = "updated";
if ( $_POST['Import'] ){
$ret = ojsimport::import_all();
if ( is_wp_error($ret) ){
$message = $ret->get_error_message();
$class = "error";
}else $message = 'Successfully imported.'."</br>".$ret;
}else if ( $_POST['Delete'] ){
$ret = ojsimport::delete_all_imported();
if ( is_wp_error($ret) ){
$message = $ret->get_error_message();
$class = "error";
}else $message = 'successfully deleted all imported posts.';
}
// Put the result message on the screen
?>
<div id="message" class="<?php echo $class ?>"><p><strong><?php _e($message, 'menu-ojsimport' ); ?></strong></p></div>
<?php
}
// tools form
?>
<form name="form1" method="post" action="">
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
<p class="submit">
<input type="submit" name="Import" class="button-primary" value="<?php esc_attr_e('Import from ojs') ?>" />
<input type="submit" name="Delete" class="button-secondary" value="<?php esc_attr_e('Delete all imported') ?>" />
</p>
</form>
</div>
<?php
}
// ojs_import_page() displays the page content for the Test tools submenu
function ojsimport_settings_page() {
global $events_option_name, $news_option_name, $press_option_name, $images_page_option_name;
//must check that the user has the required capability
if (!current_user_can('manage_options'))
{
wp_die( __('You do not have sufficient permissions to access this page.') );
}
// variables for the field and option names
$hidden_field_name = 'ojsimport_submit_hidden';
// Read in existing option value from database
$datasources_opt = get_option('ojsimport_data_sources',array());
// See if the user has posted us some information
// If they did, this hidden field will be set to 'Y'
if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
// Read their posted value
if ( $_POST['Submit'] ){
foreach( $datasources_opt as $key => $opt )
$datasources_opt[$key] = $_POST[ 'ojsimport_data_source'.$key ];
// Save the posted value in the database
update_option( 'ojsimport_data_sources', $datasources_opt );
$message = __('settings saved.', 'menu-ojsimport' );
}else if ( $_POST['NewSource'] ){
// add new data source
$datasources_opt[] = '';
update_option('ojsimport_data_sources',$datasources_opt);
$message = __('New data source added.', 'menu-ojsimport' );
}else if ( $_POST['RemoveSource'] ){
// remove last data source
unset($datasources_opt[count($datasources_opt)-1]);
update_option('ojsimport_data_sources',$datasources_opt);
$message = __('Data source removed.', 'menu-ojsimport' );
}
// Put an settings updated message on the screen
?>
<div class="updated"><p><strong><?php echo($message); ?></strong></p></div>
<?php
}
// Now display the settings editing screen
echo '<div class="wrap">';
// header
echo "<h2>" . __( 'ojs Import Settings', 'menu-ojsimport' ) . "</h2>";
// settings form
?>
<form name="form1" method="post" action="">
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
<?php foreach ( $datasources_opt as $key => $opt ){ ?>
<p><?php echo(($key+1).'.'); _e("Data source:", 'menu-ojsimport' ); ?>
<input type="text" name="ojsimport_data_source<?php echo $key; ?>" value="<?php echo $opt; ?>" size="80">
</p>
<?php } ?>
<hr />
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
<input type="submit" name="NewSource" class="button-secondary" value="<?php esc_attr_e('Add new data source') ?>" />
<?php if ( count($datasources_opt) > 0 ){ ?>
<input type="submit" name="RemoveSource" class="button-secondary" value="<?php esc_attr_e('Remove last data source') ?>" />
<?php } ?>
</p>
</form>
</div>
<?php
}
?>