Skip to content

Commit

Permalink
fix #65 import
Browse files Browse the repository at this point in the history
  • Loading branch information
cabelotaina committed Dec 28, 2016
1 parent 4b7057f commit bfefb21
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 4 deletions.
116 changes: 116 additions & 0 deletions classes/views/class-jaiminho-view-subscribers-csvimport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

// Prevent loading this file directly
if ( !defined('SENDPRESS_VERSION') ) {
header('HTTP/1.0 403 Forbidden');
die;
}

class Jaiminho_View_Subscribers_Csvimport extends Jaiminho_View_Subscribers {


// function save(){
// //$this->security_check();
// $uploadfiles = $_FILES['uploadfiles'];
// if (is_array($uploadfiles)) {
// foreach ($uploadfiles['name'] as $key => $value) {
//
// // look only for uploded files
// if ($uploadfiles['error'][$key] == 0) {
//
// $filetmp = $uploadfiles['tmp_name'][$key];
//
// //clean filename and extract extension
// $filename = $uploadfiles['name'][$key];
//
// // get file info
// // @fixme: wp checks the file extension....
// $filetype = wp_check_filetype( basename( $filename ), null );
// $filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
// $filename = $filetitle . '.' . $filetype['ext'];
// $upload_dir = wp_upload_dir();
// if( $filetype['ext'] != 'csv' ){
// SendPress_Admin::redirect('Subscribers_Csvimport',array('listID'=> SPNL()->validate->_int( 'listID' )));
// }
//
// /**
// * Check if the filename already exist in the directory and rename the
// * file if necessary
// */
// $i = 0;
// while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
// $filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
// $i++;
// }
// $filedest = $upload_dir['path'] . '/' . $filename;
//
// $filedest = str_replace('\\','/', $filedest);
// /**
// * Check write permissions
// */
// if ( !is_writeable( $upload_dir['path'] ) ) {
// SendPress_Option::set('import_error', true);
// //$this->_error = true;
// //$this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
// //return;
// }
//
// /**
// * Save temporary file to uploads dir
// */
// if ( !@move_uploaded_file($filetmp, $filedest) ){
// SendPress_Option::set('import_error', true);
// //$this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
// //continue;
// }
//
// update_post_meta(SPNL()->validate->_int( 'listID' ),'csv_import',$filedest);
// if(SendPress_Option::get('import_error', false) == false ){
// SendPress_Admin::redirect('Subscribers_Csvprep',array('listID'=> SPNL()->validate->_int( 'listID' )));
// }
// /*
// $attachment = array(
// 'post_mime_type' => $filetype['type'],
// 'post_title' => $filetitle,
// 'post_content' => '',
// 'post_status' => 'inherit'
// );
//
// $attach_id = wp_insert_attachment( $attachment, $filedest );
// require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
// $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
// wp_update_attachment_metadata( $attach_id, $attach_data );
// */
// }
// }
// }
// }

function html() { ?>
<?php
if( SendPress_Option::get('import_error', false) == true ) { ?>
<div class="alert alert-danger">
<?php _e('We had a problem saving your upload','sendpress'); ?>.
</div>
<?php } ?>
<div id="taskbar" class="lists-dashboard rounded group">
<h2><?php _e('Import CSV to ','sendpress'); ?><?php echo get_the_title(SPNL()->validate->_int( 'listID' )); ?></h2>
</div>
<div class="boxer">
<div class="boxer-inner">
<form method="post" action="<?php echo esc_url( admin_url('admin.php') ); ?>" enctype="multipart/form-data" >
<input type="hidden" name="action" value="import">
<input type="hidden" name="listID" value="<?php echo SPNL()->validate->_int( 'listID' ); ?>" />
<input type="file" name="uploadfiles" id="uploadfiles" size="35" class="uploadfiles" />
<button class="button-primary" type="submit" name="uploadfile" id="uploadfile_btn" >Upload</button>
<?php
SendPress_Option::set('import_error', false);
SendPress_Data::nonce_field(); ?>
</form>
</div>
</div>
<?php

}

}
127 changes: 127 additions & 0 deletions classes/views/class-jaiminho-view-subscribers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

// Prevent loading this file directly
if ( !defined('SENDPRESS_VERSION') ) {
header('HTTP/1.0 403 Forbidden');
die;
}

class Jaiminho_View_Subscribers extends SendPress_View {
function _get_called_class(){
return "Jaiminho_View_Subscribers";
}

function admin_init(){
add_action('load-sendpress_page_sp-subscribers',array($this,'screen_options'));
//add_action('sendpress-subscribers-sub-menu', array('SendPress_View_Subscribers','default_header'));
}

function delete_list(){
SendPress_Data::delete_list( SPNL()->validate->_int('listID') );
wp_redirect( esc_url_raw( admin_url('admin.php?page='.SPNL()->validate->page() ) ) );
}

function delete_lists_bulk(){
$list_delete = SPNL()->validate->_int_array('list');
foreach ($list_delete as $listID) {
SendPress_Data::delete_list( SPNL()->validate->int($listID));
}
wp_redirect( esc_url_raw( admin_url('admin.php?page='.SPNL()->validate->page() ) ) );
}


function export_list(){
//$this->security_check();
$l = SPNL()->validate->_int('listID');
if( $l > 0 ){
$items = SendPress_Data::export_subscirbers( $l );
header("Content-type:text/octect-stream");
header("Content-Disposition:attachment;filename=sendpress.csv");
print "email,firstname,lastname,status \n";
foreach($items as $user) {
print $user->email . ",". $user->firstname.",". $user->lastname.",". $user->status."\n" ;
}
}
exit;
}

function sub_menu(){
?>
<div class="navbar navbar-default" >
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only"><?php _e('Toggle navigation','sendpress'); ?></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>

</button>
<a class="navbar-brand" href="#"><?php _e('Subscribers','sendpress'); ?></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li <?php if(! SPNL()->validate->_isset('view') ){ ?>class="active"<?php } ?> >
<a href="<?php echo SendPress_Admin::link('Subscribers'); ?>"><i class="icon-list "></i> <?php _e('Lists','sendpress'); ?></a>
</li>
<?php do_action('sendpress-add-submenu-item', SPNL() );?>
<li <?php if(SPNL()->validate->_string('view') === 'all'){ ?>class="active"<?php } ?> >
<a href="<?php echo SendPress_Admin::link('Subscribers_All'); ?>"><i class="icon-user "></i> <?php _e('All Subscribers','sendpress'); ?></a>
</li>
<?php if(SendPress_Option::get('beta')){ ?>
<li <?php if(SPNL()->validate->_string('view') === 'custom'){ ?>class="active"<?php } ?> >
<a href="<?php echo SendPress_Admin::link('Subscribers_Custom'); ?>"><i class="icon-list "></i> <?php _e('Custom Fields','sendpress'); ?></a>
</li>
<?php } ?>

</ul>
</div>
</div>

<?php

do_action('sendpress-subscribers-sub-menu');
}

function screen_options(){

$screen = get_current_screen();

$args = array(
'label' => __('Lists per page', 'sendpress'),
'default' => 10,
'option' => 'sendpress_lists_per_page'
);
add_screen_option( 'per_page', $args );
}

function default_header(){
?>

<?php
}

function html() {
SendPress_Tracking::event('Lists Tab');
//Create an instance of our package class...
$testListTable = new SendPress_Lists_Table();
//Fetch, prepare, sort, and filter our data...
$testListTable->prepare_items();

?>
<!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
<div id="button-area">
<a class="btn btn-primary btn-large" href="?page=<?php echo SPNL()->validate->page(); ?>&view=listcreate"><?php _e('Create List','sendpress'); ?></a>
</div>
<h2><?php _e('Lists','sendpress'); ?></h2>
<form id="sendpress-lists" method="get">
<!-- For plugins, we also need to ensure that the form posts back to our current page -->
<input type="hidden" name="page" value="<?php echo SPNL()->validate->page() ?>" />
<!-- Now we can render the completed list table -->
<?php $testListTable->display(); ?>
<?php wp_nonce_field($this->_nonce_value); ?>
</form>
<?php
}

}
SendPress_Admin::add_cap('Subscribers','sendpress_subscribers');
68 changes: 64 additions & 4 deletions jaiminho.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
require_once( ABSPATH . '/wp-content/plugins/jaiminho/classes/views/class-jaiminho-view-emails-send-tempclone.php' );
require_once( ABSPATH . '/wp-content/plugins/jaiminho/classes/views/class-jaiminho-view-settings-account.php' );
require_once( ABSPATH . '/wp-content/plugins/jaiminho/classes/views/class-jaiminho-view-reports.php' );
require_once( ABSPATH . '/wp-content/plugins/jaiminho/classes/views/class-jaiminho-view-subscribers.php' );
require_once( ABSPATH . '/wp-content/plugins/jaiminho/classes/views/class-jaiminho-view-subscribers-csvimport.php' );
require_once( ABSPATH . '/wp-content/plugins/jaiminho/classes/class-jaiminho-sender-redelivre.php' );
require_once( ABSPATH . '/wp-content/plugins/jaiminho/classes/class-jaiminho-sender-network.php' );
require_once( ABSPATH . '/wp-content/plugins/jaiminho/classes/class-jaiminho-sender-gmail.php' );
Expand Down Expand Up @@ -92,14 +94,70 @@ public function Init()
}
add_action( 'tgmpa_register', array( $this , 'jaiminho_register_required_plugins' ) );
remove_action( 'init' , array( SPNL() , 'toplevel_page_sp-overview' ) );
//add_filter( 'sendpress_notices', '__return_empty_string' );
add_action( 'sendpress_notices', array( $this, 'jaiminho_notices' ) );
add_action( 'init', array( $this, 'frame_it_up' ), 20 );
add_action('admin_enqueue_scripts', array( $this, 'load_admin_script') );
add_action( 'admin_action_export', array($this,'export_report') );
add_action( 'admin_action_import', array($this,'save_import') );
add_filter( 'mce_buttons_2', array($this,'mce_buttons') );
}

function save_import(){
$uploadfiles = $_FILES['uploadfiles'];
if (is_array($uploadfiles)) {

if ($uploadfiles['error']== 0) {

$filetmp = $uploadfiles['tmp_name'];

$filename = $uploadfiles['name'];

// @fixme: wp checks the file extension....
$filetype = wp_check_filetype( $filename, array('csv' => 'text/csv') );
$filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
$filename = $filetitle . '.' . $filetype['ext'];
var_dump($filetype);
var_dump(basename( $filename ));
$upload_dir = wp_upload_dir();
if( $filetype['ext'] != 'csv' ){
SendPress_Admin::redirect('Subscribers_Csvimport',array('listID'=> SPNL()->validate->_int( 'listID' )));
}

/**
* Check if the filename already exist in the directory and rename the
* file if necessary
*/
$i = 0;
while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
$filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
$i++;
}
$filedest = $upload_dir['path'] . '/' . $filename;

$filedest = str_replace('\\','/', $filedest);
/**
* Check write permissions
*/
if ( !is_writeable( $upload_dir['path'] ) ) {
SendPress_Option::set('import_error', true);
}

/**
* Save temporary file to uploads dir
*/
if ( !@move_uploaded_file($filetmp, $filedest) ){
SendPress_Option::set('import_error', true);
var_dump("Aqui não entra");
}
var_dump(SPNL()->validate->_int( 'listID' ));
var_dump($filedest);
update_post_meta(SPNL()->validate->_int( 'listID' ),'csv_import',$filedest);
if(SendPress_Option::get('import_error', false) == false ){
SendPress_Admin::redirect('Subscribers_Csvprep',array('listID'=> SPNL()->validate->_int( 'listID' )));
}
}
}
}
function mce_buttons( $buttons ) {
array_unshift( $buttons, 'fontselect' );
array_unshift( $buttons, 'fontsizeselect' );
Expand Down Expand Up @@ -379,11 +437,9 @@ public function jaiminho_network_settings()
public function jaiminho_fix_tables_html()
{

//var_dump(isset($_POST['fix_tables']));
global $wpdb;
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

//$wpdb->hide_errors();

$collate = '';
if ( $wpdb->has_cap( 'collation' ) ) {
Expand Down Expand Up @@ -1060,6 +1116,10 @@ public function jaiminho_get_view_class($page, $current_view, $emails_credits, $
return "Jaiminho_View_Emails_Tempclone";
case "SendPress_View_Reports":
return "Jaiminho_View_Reports";
case "SendPress_View_Subscribers":
return "Jaiminho_View_Subscribers";
case "SendPress_View_Subscribers_Csvimport":
return "Jaiminho_View_Subscribers_Csvimport";
case "SendPress_View_Subscribers_Listcreate":
wp_enqueue_script('jaiminho_disable');
return $view_class;
Expand Down

0 comments on commit bfefb21

Please sign in to comment.