Skip to content

Instantly share code, notes, and snippets.

@gicolek
Created July 28, 2014 09:10
Show Gist options
  • Save gicolek/5fbca020237dee06ac70 to your computer and use it in GitHub Desktop.
Save gicolek/5fbca020237dee06ac70 to your computer and use it in GitHub Desktop.
LS DB
<?php
require_once(__DIR__ . '/class-like-saveit-db-installer.php');
class Like_Saveit_Db extends Like_Saveit_DB_Installer {
/**
* Members Table Name
*/
protected static $m_tbl_name = 'tbl_Member_Master';
/**
* Contact Table Name
*/
protected static $c_tbl_name = 'tbl_Contact_Reqeusts';
/**
* Tell A Friend Table Name
*/
protected static $f_tbl_name = 'tbl_Tell_A_Friend';
/**
* Reset Pass Tabl Name
*/
protected static $r_tbl_name = 'tbl_Reset_Pass';
/**
* Market Data Table Name (for not logged in)
*/
protected static $markdata_tbl_name = 'tbl_MarketData';
/**
* Market Data Table Name (for logged in)
*/
protected static $mark_data_tbl_name = 'tbl_Market_Data';
/**
* Setup all database tables required by the plugin
*/
public static function setup_db_tables() {
$m_tbl_name = self::$m_tbl_name;
parent::setup_m_table( $m_tbl_name );
$c_tbl_name = self::$c_tbl_name;
parent::setup_c_tbl( $c_tbl_name );
$f_tbl_name = self::$f_tbl_name;
parent::setup_f_tbl( $f_tbl_name );
$markdata_tbl_name = self::$markdata_tbl_name;
parent::setup_markdata_tbl( $markdata_tbl_name );
$mark_data_tbl_name = self::$mark_data_tbl_name;
parent::setup_mark_data_tbl( $mark_data_tbl_name );
}
/**
* Utility to get the user data
*
* @param int $user_id
*/
public static function get_user_data($user_id) {
global $wpdb;
$wpdb->show_errors();
$m_tbl_name = self::$m_tbl_name;
$user = $wpdb->get_row( "SELECT * FROM {$m_tbl_name} WHERE Member_ID = '{$user_id}';", ARRAY_A );
// if the query returned some rows return true
if ( $wpdb->num_rows > 0 ) {
return $user;
} else {
return false;
}
}
/**
* Utility to check if the user is active
*
* @global db $wpdb
* @param string $email
* @return boolean
*/
public static function is_user_active($email) {
global $wpdb;
$m_tbl_name = self::$m_tbl_name;
$user = $wpdb->get_results( "SELECT Activated FROM {$m_tbl_name} WHERE Email_Address = {$email};", OBJECT_K );
// if the user was activated return true
if ( $user['Activated'] == 1 ) {
return true;
} else {
return false;
}
}
/**
* Utility to check if the user exists by email
*
* @global db $wpdb
* @param string $email
* @return boolean
*/
public static function if_user_exists($email) {
global $wpdb;
$wpdb->show_errors();
$m_tbl_name = self::$m_tbl_name;
$user = $wpdb->get_row( "SELECT Member_ID FROM {$m_tbl_name} WHERE Email_Address = '{$email}';", ARRAY_A );
// if the query returned some rows return true
if ( $wpdb->num_rows > 0 ) {
return $user['Member_ID'];
} else {
return false;
}
}
/**
* Utility to check if the user exists by username
*
* @global db $wpdb
* @param string $email
* @return boolean
*/
public static function if_username_exists($username) {
global $wpdb;
$wpdb->show_errors();
$m_tbl_name = self::$m_tbl_name;
$user = $wpdb->get_row( "SELECT Member_ID FROM {$m_tbl_name} WHERE User_Name = '{$username}';", ARRAY_A );
// if the query returned some rows return true
if ( $wpdb->num_rows > 0 ) {
return $user['Member_ID'];
} else {
return false;
}
}
public static function login_user($login, $password) {
global $wpdb;
$m_tbl_name = self::$m_tbl_name;
$password = md5( $password );
$user = $wpdb->get_row( "SELECT Member_ID, User_Name FROM {$m_tbl_name} WHERE User_Name='{$login}' AND Password='{$password}';", ARRAY_A );
// if the query returned some rows return true
if ( $wpdb->num_rows > 0 ) {
return array( 'id' => $user['Member_ID'], 'login' => $user['User_Name'] );
} else {
return false;
}
}
/**
* Utility for first time user registration
*/
public static function register_user($un, $email, $pc, $pass) {
global $wpdb;
$wpdb->show_errors();
$insert = $wpdb->insert( self::$m_tbl_name, array(
'User_name' => $un,
'Email_Address' => $email,
'First_Name' => ' ',
'Last_Name' => ' ',
'Add_Line1' => ' ',
'Add_Line2' => ' ',
'Suburb' => ' ',
'Post_Code' => $pc,
'Activated' => 0,
'Password' => md5( $pass ),
'Sumbmit_IP' => $_SERVER['REMOTE_ADDR'],
)
);
return $insert;
}
/**
* Utility for user profile update
*/
public static function update_user($email, $fn, $ln, $add, $add2, $pc, $sub, $age, $pn, $pn2, $mn, $extra) {
global $wpdb;
$m_tbl_name = self::$m_tbl_name;
$updated = $wpdb->update(
self::$m_tbl_name, array(
'Email_Address' => $email,
'First_Name' => $fn,
'Last_Name' => $ln,
'Add_Line1' => $add,
'Add_Line2' => $add2,
'Post_Code' => $pc,
'Suburb' => $sub,
'Age_Group' => $age,
'Mobile_Number' => $mn,
'Phone_Number' => $pn . ',' . $pn2,
'Extra' => $extra ? 1 : 0
), array( 'Email_Address' => $email ), array(
'%s'
), array( '%s' )
);
// has the user been updated
if ( $updated ) {
return true;
} else {
return false;
}
}
/**
* Activate a user who's not been activate yet
* @global db $wpdb
* @param string $email
* @return boolean
*/
public static function activate_user($email) {
if ( !self::is_user_active( $email ) ) {
global $wpdb;
$m_tbl_name = self::$m_tbl_name;
$activated = $wpdb->update(
self::$m_tbl_name, array(
'Activated' => 1 // integer (number)
), array( 'Email_Address' => $email ), array(
'%d'
), array( '%s' )
);
// has he been activated or has something went wrong
if ( $activated ) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Reset user's password
*
* @global db $wpdb
* @param string $email
* @return boolean
*/
public static function reset_user_pass($user_id, $password) {
global $wpdb;
$wpdb->show_errors();
// get the table name
$m_tbl_name = self::$m_tbl_name;
// update the user query with a new password
$updated = $wpdb->update(
self::$m_tbl_name, //
array( 'Password' => md5( $password ) ), //
array( 'Member_ID' => $user_id ), //
array( '%s' ), array( '%d' )
//
);
// if everything's ok return true
if ( $updated ) {
return true;
} else {
// something went wrong most probably the same password was used
return false;
}
}
/**
* Check user's password
*
* @global db $wpdb
* @param string $email
* @return boolean
*/
public static function check_user_password($user_id, $password) {
global $wpdb;
$m_tbl_name = self::$m_tbl_name;
// haschode the password
$password = md5( $password );
$wpdb->get_row( "SELECT Member_ID FROM {$m_tbl_name} WHERE Member_ID = '{$user_id}' and Password = '{$password}'", ARRAY_A );
if ( $wpdb->num_rows > 0 ) {
return true;
} else {
return false;
}
}
/**
* Populate the temporary password table with temporary id
*
* @param int $user_id
* @param int $uniqid
*/
public static function save_temp_id($user_id, $uniqid) {
global $wpdb;
$wpdb->show_errors();
$insert = $wpdb->insert( self::$r_tbl_name, array(
'User_ID' => $user_id,
'Pass_String' => $uniqid,
)
);
return $insert;
}
/**
* Check if the temporary reset id can be found in the table
*/
public static function check_temp_id($user_id, $uniqid) {
global $wpdb;
$wpdb->show_errors();
$r_tbl_name = self::$r_tbl_name;
$wpdb->get_row( "SELECT User_ID FROM {$r_tbl_name} WHERE User_ID = '{$user_id}' and Pass_String = '{$uniqid}'", ARRAY_A );
if ( $wpdb->num_rows > 0 ) {
return true;
} else {
return false;
}
}
public static function update_contact_table($email, $fn, $ln, $msg) {
global $wpdb;
//$wpdb->show_errors();
$insert = $wpdb->insert( self::$c_tbl_name, array(
'Email_Address' => $email,
'First_Name' => $fn,
'Last_Name' => $ln,
'Message' => $msg,
'Submit_IP' => $_SERVER['REMOTE_ADDR'],
)
);
return $insert;
}
public static function update_tell_table($id, $email) {
global $wpdb;
//$wpdb->show_errors();
$insert = $wpdb->insert( self::$f_tbl_name, array(
'Send_Member_ID' => $id,
'Friend_Email' => $email,
'Submit_IP' => $_SERVER['REMOTE_ADDR'],
)
);
return $insert;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment