-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoptions.php
293 lines (244 loc) · 7.84 KB
/
options.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
<?php
// PHP 5.3 and later:
namespace BotaPressao;
class SettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_settings_page()
{
// These will be under "Tools"
add_management_page(
__('Importar politicos de um csv','botapressao'),
__('Importar politicos de um csv','botapressao'),
'import',
'botapressao-import-file',
array($this, 'create_admin_page')
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'botapressao_plugin_options', array() );
?>
<div class="wrap">
<h2><?php _e('Configurações do Plugin BotaPressao de Cultura', 'botapressaodecultura') ?></h2>
<form id="botapressao_csv_import_form" method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'botapressao_option_group' );
do_settings_sections( 'botapressao-setting-admin' );
echo '<b>'.__('Arquivo a importar:', 'botapressao').'</b>';
echo '<input id="botapressao-import-csv" '
. 'name="botapressao-import-csv" type="file">';
submit_button("Importar Csv", 'secundary', 'importcsv' );
submit_button();
//<input type="hidden" name="action" value="ImportarCsv" >
?>
</form>
<div id="result">
</div>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'botapressao_option_group', // Option group
'botapressao_plugin_options', // Option name
array( $this, 'sanitize' ) // Sanitize
);
if(array_key_exists('page', $_REQUEST) && $_REQUEST['page'] == 'botapressao-import-file')
{
$path = plugin_dir_url(__FILE__) ;
wp_register_script('botapressao_options_scripts', $path . '/js/botapressao_options_scripts.js', array('jquery'));
wp_enqueue_script('botapressao_options_scripts');
wp_localize_script( 'botapressao_options_scripts', 'botapressao_options_scripts_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' )) );
}
add_action( 'wp_ajax_ImportarCsv', array($this, 'ImportarCsv_callback') );
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
$new_input = array();
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
_e('Configurações personalizadas do Plugin: Bota Pressão', 'botapressaodecultura');
}
public function fetch_remote_file( $url, $post ) {
global $url_remap;
// extract the file name and extension from the url
$file_name = basename( $url );
// get placeholder file in the upload dir with a unique, sanitized filename
$upload = wp_upload_bits( $file_name, 0, '',
array_key_exists('upload_date', $post) ? $post['upload_date'] : null );
if ( $upload['error'] )
return new WP_Error( 'upload_dir_error', $upload['error'] );
// fetch the remote url and write it to the placeholder file
$headers = wp_get_http( $url, $upload['file'] );
// request failed
if ( ! $headers ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
}
// make sure the fetch was successful
if ( $headers['response'] != '200' ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) );
}
$filesize = filesize( $upload['file'] );
if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
}
if ( 0 == $filesize ) {
@unlink( $upload['file'] );
return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
}
// keep track of the old and new urls so we can substitute them later
$url_remap[$url] = $upload['url'];
return $upload;
}
protected $logfilename = 'csv_import.log';
public static function log($msn, $print_r = false)
{
if($print_r)
{
print_r($msn);
file_put_contents("/tmp/csv_import.log", print_r($msn, true), FILE_APPEND);
}
else
{
echo $msn;
$msn = str_replace("<br/>", "\n", $msn);
$msn = str_replace("<br>", "\n", $msn);
file_put_contents("/tmp/csv_import.log", $msn, FILE_APPEND);
}
}
public static function newLog()
{
file_put_contents("/tmp/csv_import.log", date('Y-m-d').'\n');
}
public function ImportarCsv_callback()
{
SettingsPage::newLog();
echo '<div id="result">';
//print_r($_POST);print_r($_FILES);die();
if (!array_key_exists('file-0', $_FILES)
|| !array_key_exists('tmp_name', $_FILES['file-0'])
|| $_FILES['file-0']['error'])
{
_e('CSV inválido', 'botapressao');
die;
}
$file = fopen($_FILES['file-0']['tmp_name'], 'r');
$debug = false;
$begin = 1;
$header_size = 1; //Header size
$header_n = 1; // Header slugs on
$id_column = 0;
$last_column = 9;
$sep = ','; // field csv separator
$ids = array();
ini_set("memory_limit", "2048M");
set_time_limit(0);
$names = array();
$coords = array();
for ($i = 0; $i < $header_size; $i++) // first n lines has header
{
$row = fgetcsv( $file, 0, $sep);
if ($row === false)
{
_e('CSV inválido', 'botapressao');
die;
}
$names[$i] = $row;
}
for ($i = 0; $i < $begin; $i++) // move pointer to begin of data
{
$row = fgetcsv( $file, 0, $sep);
}
SettingsPage::log('<pre>');
$row = fgetcsv( $file, 0, $sep);
$i = 0;
do
{
if(count($ids) > 0) // have ids limit
{
while ($row !== false && !in_array($row[$id_column], $ids)) // locate next valid id
{
$row = fgetcsv( $file, 0, $sep);
}
if($row === false) break;
}
$post = array(
'post_author' => 1, //The user ID number of the author.
'post_content' => '',
'post_title' => $row[0], //The title of your post.
'post_type' => 'politicos',
'post_status' => 'publish'
);
$post_id = 0;
if(!$debug) $post_id = wp_insert_post($post);
foreach( $names as $name )
{
foreach ($name as $key => $value){
if($debug)
{
self::log("update_post_meta($post_id, ".$value." ,".$row[$key].");<br/>");
}
else
{
//var_dump( wp_upload_dir()['baseurl']);
//echo "(" . $value . ": " . $row[$key] . "<br>";
$aux = wp_upload_dir();
if ($value=="title"){ continue;}
else if($value=="politico_picture"){
update_post_meta($post_id, $value ,$aux['baseurl']."/politicos/".$row[$key]);
}
else{
update_post_meta($post_id, $value ,$row[$key]);
}
}
}
}
$row = fgetcsv( $file, 0, $sep);
$i++;
} while ($row !== false);// && $i < 10);
SettingsPage::log('</pre>');
fclose ( $file );
echo '</div>';
die();
}
}
if( is_admin() )
$botapressao_settings_page = new \BotaPressao\SettingsPage();