Skip to content

Instantly share code, notes, and snippets.

@adicahyaludin
Created November 4, 2022 03:55
Show Gist options
  • Save adicahyaludin/394bdf6965dcca2d5bb5c82ed7a4ab03 to your computer and use it in GitHub Desktop.
Save adicahyaludin/394bdf6965dcca2d5bb5c82ed7a4ab03 to your computer and use it in GitHub Desktop.
<?php
namespace Tokenz\Model;
/**
* User_Verification classes
* @since 1.0.0
*/
class User_Verification {
private $wpdb;
private $table_name;
public function __construct() {
global $wpdb;
$this->wpdb = $wpdb;
$this->table_name = $this->wpdb->prefix . "tokenz_user_verification";
}
public function create_table() {
$charset_collate = $this->wpdb->get_charset_collate();
$sql = "CREATE TABLE $this->table_name (
id int(11) NOT NULL AUTO_INCREMENT,
login varchar(255) NOT NULL,
type varchar(255),
status boolean DEFAULT 0,
created_date DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_date DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
maybe_create_table( $table_name, $sql );
}
public function create( $data = [] ) {
$values = array();
$place_holders = array();
$sql = "INSERT INTO $this->table_name (
login, type, status, created_date, updated_date
)
VALUES ";
foreach ( $data as $key => $item ) :
$_item = wp_parse_args( $item, [
'login' => '',
'type' => 'email',
'status' => 0,
'created_date' => date('Y-m-d H:i:s'),
'updated_date' => date('Y-m-d H:i:s'),
] );
array_push(
$values,
$_item['login'],
$_item['type'],
$_item['status'],
$_item['created_date'],
$_item['updated_date']
);
$place_holders[] = "('%s','%s','%d','%s','%s')";
endforeach;
$sql .= implode( ', ', $place_holders );
$sql_clean = $this->wpdb->prepare( "$sql ", $values );
$create = $this->wpdb->query( $sql_clean );
return $create;
}
public function read( $wheres = [] ) {
$args = wp_parse_args( $wheres, [
'start' => 0,
'length' => 10,
'order' => 'DESC',
'orderby' => 'created_date',
'user_id' => '',
'post_id' => '',
] );
$response = [];
$sql = "SELECT *
FROM $this->table_name
";
$sql_total = "SELECT COUNT(*)
FROM $this->table_name
";
$sql_wheres = [];
if ( !empty( $args['login'] ) ) :
$sql_wheres[] = " login = ".$args['login']." ";
endif;
if ( !empty( $args['type'] ) ) :
$sql_wheres[] = " type = ".$args['type']." ";
endif;
if ( !empty( $args['status'] ) ) :
$sql_wheres[] = " status = ".intval($args['status'])." ";
endif;
if ( $sql_wheres ) :
$sql_where = ' WHERE '.implode(' AND ', $sql_wheres );
$sql .= $sql_where;
$sql_total .= $sql_where;
endif;
$total = $this->wpdb->get_var( $sql_total );
$response['total'] = intval($total);
$sql .= " ORDER BY ".$args['orderby']." ".$args['order']." ";
if ( $args['length'] != -1 ) :
$sql .= " LIMIT ".intval($args['length'])." OFFSET ".intval($args['start'])."";
endif;
$results = $this->wpdb->get_results( $sql );
$response['data'] = $results;
return $response;
}
public function update( $data = [], $wheres = [] ) {
$sql = "UPDATE $this->table_name ";
$set_data = [];
foreach ( $data as $key => $item ) :
$set_data[] = $key." = '".$item."'";
endforeach;
if ( $set_data ) :
$sql .= " SET ".implode( ', ', $set_data );
endif;
$sql_wheres = [];
foreach ( $wheres as $key => $where ) :
$sql_wheres[] = $key." = '".$where."'";
endforeach;
if ( $sql_wheres ) :
$sql .= " WHERE ".implode(' AND ', $sql_wheres );
endif;
$update = $wpdb->query( $sql );
return $update;
}
public function delete( $wheres = [] ) {
$sql = "DELETE FROM $this->table_name ";
$sql_wheres = [];
foreach ( $wheres as $key => $where ) :
$sql_wheres[] = $key." = '".$where."'";
endforeach;
if ( $sql_wheres ) :
$sql .= " WHERE ".implode(' AND ', $sql_wheres );
endif;
$delete = $wpdb->query( $sql );
return $delete;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment