Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
corsac committed Jul 9, 2018
2 parents eea9f8d + 69e5d3e commit d9d907d
Showing 1 changed file with 233 additions and 2 deletions.
235 changes: 233 additions & 2 deletions dt-core/admin/menu/tabs/tab-featured-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function __construct()
{
add_action( 'dt_extensions_tab_menu', [ $this, 'add_tab' ], 10, 1 ); // use the priority setting to control load order
add_action( 'dt_extensions_tab_content', [ $this, 'content' ], 99, 1 );
//tools tab
add_action( 'dt_extensions_tools_tab_content', [ $this, 'content' ], 100, 1 );
parent::__construct();
} // End __construct()

Expand Down Expand Up @@ -61,6 +63,11 @@ function(data, status){
echo 'nav-tab-active';
}
echo '">' . esc_attr__( 'Featured Extensions', 'disciple_tools' ) . '</a>';
echo '<a href="' . esc_url( admin_url() ) . 'admin.php?page=dt_extensions&tab=tools" class="nav-tab ';
if ($tab == 'tools') {
echo 'nav-tab-active';
}
echo '">' . esc_attr__( 'Tools', 'disciple_tools' ) . '</a>';
}

public function content( $tab )
Expand All @@ -74,6 +81,18 @@ public function content( $tab )
// begin right column template
$this->template( 'right_column' );

// end columns template
$this->template( 'end' );
}
else if ( 'tools' == $tab ) {
// begin columns template
$this->template( 'begin' );

$this->tools_box_message();

// begin right column template
$this->template( 'right_column' );

// end columns template
$this->template( 'end' );
}
Expand Down Expand Up @@ -106,6 +125,218 @@ public function partial_array_search( $array, $find )
return -1;
}

//tools page
public function tools_box_message()
{
//check if it can run commands
$run = true;
//check for admin or multisite super admin
if ( ( is_multisite() && !is_super_admin() ) || ( !is_multisite() && !is_admin() ) ) {
$run = false;
}
//check for action of csv import
if ( isset( $_POST['csv_import_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['csv_import_nonce'] ) ), 'csv_import' ) && $run ) {
//@codingStandardsIgnoreLine
if ( isset( $_FILES["csv_file"] ) ) {
//@codingStandardsIgnoreLine
$file_parts = explode( ".", sanitize_text_field( wp_unslash( $_FILES["csv_file"]["name"] ) ) )[count( explode( ".", sanitize_text_field( wp_unslash( $_FILES["csv_file"]["name"] ) ) ) ) -1];
if ( $file_parts != 'csv') {
esc_html_e( "NOT CSV", 'disciple_tools' );
exit;
}
if ($_FILES["csv_file"]["error"] > 0) {
esc_html_e( "ERROR UPLOADING FILE", 'disciple_tools' );
exit;
}
//@codingStandardsIgnoreLine
$this->import_csv( $_FILES['csv_file'], sanitize_text_field( wp_unslash( $_POST['csv_del'] ) ), sanitize_text_field( wp_unslash( $_POST['csv_source'] ) ), sanitize_text_field( wp_unslash( $_POST['csv_assign'] ) ), sanitize_text_field( wp_unslash( $_POST['csv_header'] ) ) );
}
exit;
}
//check for varrification of data
if ( isset( $_POST['csv_correct_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['csv_correct_nonce'] ) ), 'csv_correct' ) && $run ) {
//@codingStandardsIgnoreLine
if ( isset( $_POST["csv_contacts"] ) ) {
//@codingStandardsIgnoreLine
$this->insert_contacts( unserialize( base64_decode( $_POST["csv_contacts"] ) ) );
}
exit;
}
?>
<h3><?php esc_html_e( "CSV IMPORT", 'disciple_tools' ) ?></h3>
<p><?php esc_html_e( "Format", 'disciple_tools' ) ?></p>
<p><?php esc_html_e( "name, phone, email, address, gender, initial_comment", 'disciple_tools' ) ?></p>
<form method="post" enctype="multipart/form-data">
<input type="file" name="csv_file" id="csv_file"> <br>
<input type="text" name="csv_del" value=',' size=2> <?php esc_html_e( "The CSV Delimiter", 'disciple_tools' ) ?> <br>
<select name="csv_header">
<option value=yes ><?php esc_html_e( "yes", 'disciple_tools' ) ?></option>
<option value=no ><?php esc_html_e( "no", 'disciple_tools' ) ?></option>
</select>
<?php esc_html_e( "Does the file have a header?", 'disciple_tools' ) ?> <br>
<select name="csv_source">
<?php
$site_custom_lists = dt_get_option( 'dt_site_custom_lists' );
foreach ( $site_custom_lists['sources'] as $key => $value ) {
if ( $value['enabled'] ) {
?> <option value=<?php echo esc_html( $key ); ?>><?php echo esc_html( $value['label'] ); ?></option> <?php
}
}
?>
</select>
<?php esc_html_e( "The Source of the Contacts", 'disciple_tools' ) ?> <br>
<select name="csv_assign">
<option value""></option>
<?php
$args = [
'role__not_in' => [ 'registered' ],
'fields' => [ 'ID', 'display_name' ],
'order' => 'ASC'
];
$users = get_users( $args );
foreach ( $users as $user) { ?>
<option value=<?php echo esc_html( $user->ID ); ?>><?php echo esc_html( $user->display_name ); ?></option>
<?php } ?>
</select>
<?php esc_html_e( "The User to Assign to", 'disciple_tools' ) ?>
<?php wp_nonce_field( 'csv_import', 'csv_import_nonce' ); ?>
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value=<?php esc_html_e( "Submit", 'disciple_tools' ) ?>></p>
</form>
<?php
}

public function import_csv( $file, $del = ';', $source = 'web', $assign = '', $header = "yes" ) {
$people = [];
//open file
$file_data = fopen( $file['tmp_name'], "r" );
//loop over array
while ( $row = fgetcsv( $file_data, 10000, $del ) ) {
foreach ($row as $index => $i) {
//$info = explode( $del, $data );
//chcek for data type
if ($assign != '') {
$fields["assigned_to"] = (int) $assign;
}
$fields["sources"] = [ "values" => array( [ "value" => $source ] ) ];
//foreach ($info as $index => $i) {
$i = str_replace( "\"", "", $i );
//checks for name
if ( $index == 0 ){
$fields['title'] = $i;
}
//checks for phone
else if ( $index == 1) {//preg_match('/^[0-9|(|)|\-|#|" "|+]*]*$/', $i) ) {
$fields['contact_phone'][] = [ "value" => $i ];
}
//checks for email
else if ( $index == 2) {//filter_var($i, FILTER_VALIDATE_EMAIL) ) {
$fields['contact_email'][] = [ "value" => $i ];
}
//checks for address
else if ( $index == 3) {
$fields['contact_address'][] = [ "value" => $i ];
}
//checks for gender
else if ( $index == 4) {
$i = strtolower( $i );
$i = substr( $i, 0, 1 );
$gender = "not-set";
if ($i == "m" ){
$gender = "male";
}
else if ($i == "f" ){
$gender = "female";
}
$fields['gender'] = $gender;
}
//checks for comments
else { //$index == count($info)-1 ) {
if ( $i != '' ) {
$fields["notes"][] = $i;
}
}
}
//add person
if ( $fields['title'] != '' && $fields['title'] != ' ' && $fields['title'] !== false ) {
$people[] = array( $fields );
unset( $fields['contact_email'] );
unset( $fields['contact_phone'] );
unset( $fields['contact_address'] );
unset( $fields['gender'] );
unset( $fields['sources'] );
unset( $fields['notes'] );
}
}
//close the file
fclose( $file_data );
//check for correct data
$pos = 0;
if ( $header == "yes" ) {
unset( $people[0] );
$pos = 1;
}
?>
<h3> <?php echo esc_html_e( "Is This Data In The Correct Fields?", 'disciple_tools' ); ?> </h3>
<p><?php esc_html_e( "Name", 'disciple_tools' ) ?>: <?php echo esc_html( $people[$pos][0]['title'] ) ?></p>
<p><?php esc_html_e( "Source", 'disciple_tools' ) ?>: <?php echo esc_html( $people[$pos][0]['sources']["values"][0]["value"] ) ?></p>
<p><?php esc_html_e( "Assigned To", 'disciple_tools' ) ?>: <?php echo ( isset( $people[$pos][0]['assigned_to'] ) && $people[$pos][0]['assigned_to'] != '' ) ? esc_html( get_user_by( 'id', $people[$pos][0]['assigned_to'] )->data->display_name ) : "Not Set" ?></p>
<p><?php esc_html_e( "Contact Phone", 'disciple_tools' ) ?>: <?php echo isset( $people[$pos][0]['contact_phone'][0]["value"] ) ? esc_html( $people[$pos][0]['contact_phone'][0]["value"] ) : "None" ?></p>
<p><?php esc_html_e( "Contact Email", 'disciple_tools' ) ?>: <?php echo isset( $people[$pos][0]['contact_email'][0]["value"] ) ? esc_html( $people[$pos][0]['contact_email'][0]["value"] ) : "None" ?></p>
<p><?php esc_html_e( "Contact Address", 'disciple_tools' ) ?>: <?php echo isset( $people[$pos][0]['contact_address'][0]["value"] ) ? esc_html( $people[$pos][0]['contact_address'][0]["value"] ) : "None" ?></p>
<p><?php esc_html_e( "Gender", 'disciple_tools' ) ?>: <?php echo isset( $people[$pos][0]['gender'] ) ? esc_html( $people[$pos][0]['gender'] ) : "None" ?></p>
<p><?php esc_html_e( "Notes", 'disciple_tools' ) ?>: <?php echo isset( $people[$pos][0]['notes'][0] ) ? esc_html( $people[$pos][0]['notes'][0] ) : "None" ?></p>
</p>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="csv_contacts" value="<?php echo esc_html( base64_encode( serialize( $people ) ) ); ?>">
<?php wp_nonce_field( 'csv_correct', 'csv_correct_nonce' ); ?>
<a href="/dt3/wp-admin/admin.php?page=dt_extensions&tab=tools" class="button button-primary"> <?php esc_html_e( "No", 'disciple_tools' ) ?> </a>
<input type="submit" name="submit" id="submit" style="background-color:#4CAF50; color:white" class="button" value=<?php esc_html_e( "Yes", 'disciple_tools' ) ?>>
</form>
<?php
}

private function insert_contacts( $contacts) {
?>
<script type="text/javascript">
jQuery(document).ajaxStop(function () {
jQuery("#back").show();
});
</script>
<?php
set_time_limit( 0 );
global $wpdb;
foreach ( $contacts as $num => $f ) {
$js_array = wp_json_encode( $f[0] );
$ret = 0;
?>
<script type="text/javascript">
jQuery.ajax({
type: "POST",
data: JSON.stringify(<?php /*@codingStandardsIgnoreLine*/ echo $js_array; ?>),
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "<?php echo esc_url_raw( rest_url() ); ?>" + `dt/v1/contact/create`,
beforeSend: function(xhr) {
xhr.setRequestHeader('X-WP-Nonce', "<?php /*@codingStandardsIgnoreLine*/ echo sanitize_text_field( wp_unslash( wp_create_nonce( 'wp_rest' ) ) ); ?>");
}
});
</script>
<?php
$wpdb->queries = [];
if ( !is_numeric( $ret ) ) {
break;
echo esc_html_e( "ERROR CREATING CONTACT", 'disciple_tools' );
}
}
$num = count( $contacts );
echo esc_html( sprintf( __( "Creating %s Contacts DO NOT LEAVE THE PAGE UNTIL THE BACK BUTTON APPEARS", 'disciple_tools' ), $num ) );
?>
<form id="back" method="post" enctype="multipart/form-data" hidden>
<a href="" class="button button-primary"> <?php esc_html_e( "Back", 'disciple_tools' ) ?> </a>
</form>
<?php
exit;
}
//main page
public function box_message()
{
Expand All @@ -131,7 +362,7 @@ public function box_message()
//get plugin data
$plugins = $this->get_plugins();
?>
<h3>Official DT Plugins</h3>
<h3><?php esc_html_e( "Official DT Plugins", 'disciple_tools' ) ?></h3>
<table class="widefat striped">
<thead>
<tr>
Expand Down Expand Up @@ -184,7 +415,7 @@ public function box_message()
?>
</tbody>
</table>
<h3>Recommended Plugins</h3>
<h3><?php esc_html_e( "Recommended Plugins", 'disciple_tools' ) ?></h3>
<p><?php echo esc_html__( 'look for the "Install" button on the bottom right of your screen after clicking the install button link', 'disciple_tools' ) ?></p>
<table class="widefat striped">
<thead>
Expand Down

0 comments on commit d9d907d

Please sign in to comment.