Created
July 28, 2014 09:09
-
-
Save gicolek/d68f78164bfbb7c2091b to your computer and use it in GitHub Desktop.
LikeSaveIt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(__DIR__ . '/includes/class-like-saveit-db.php'); | |
require_once(__DIR__ . '/includes/class-session-db.php'); | |
/** | |
* Plugin Name. | |
* | |
* @package Plugin_Name | |
* @author Your Name <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://example.com | |
* @copyright 2014 Your Name or Company Name | |
*/ | |
/** | |
* Plugin class. This class should ideally be used to work with the | |
* public-facing side of the WordPress site. | |
* | |
* If you're interested in introducing administrative or dashboard | |
* functionality, then refer to `class-plugin-name-admin.php` | |
* | |
* @TODO: Rename this class to a proper name for your plugin. | |
* | |
* @package Plugin_Name | |
* @author Your Name <[email protected]> | |
*/ | |
class Like_Saveit extends Like_Saveit_Db { | |
/** | |
* Plugin version, used for cache-busting of style and script file references. | |
* | |
* @since 1.0.0 | |
* | |
* @var string | |
*/ | |
const VERSION = '1.0.0'; | |
/** | |
* Members Table Name | |
*/ | |
protected static $m_tbl_name = 'tbl_Member_Master'; | |
protected static $c_tbl_name = 'tbl_Contact_Reqeusts'; | |
/** | |
* @TODO - Rename "plugin-name" to the name of your plugin | |
* | |
* Unique identifier for your plugin. | |
* | |
* | |
* The variable name is used as the text domain when internationalizing strings | |
* of text. Its value should match the Text Domain file header in the main | |
* plugin file. | |
* | |
* @since 1.0.0 | |
* | |
* @var string | |
*/ | |
protected $plugin_slug = 'like-saveit'; | |
/** | |
* Instance of this class. | |
* | |
* @since 1.0.0 | |
* | |
* @var object | |
*/ | |
protected static $instance = null; | |
/** | |
* Initialize the plugin by setting localization and loading public scripts | |
* and styles. | |
* | |
* @since 1.0.0 | |
*/ | |
public function __construct() { | |
// Load plugin text domain | |
add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); | |
add_action( 'init', array( __CLASS__, 'start_session' ), 1 ); | |
add_action( 'init', array( __CLASS__, 'login_handler' ), 3 ); | |
add_action( 'init', array( __CLASS__, 'logout_handler' ), 2 ); | |
add_action( 'init', array( __CLASS__, 'forgot_p_handler' ) ); | |
add_action( 'init', array( __CLASS__, 'reset_p_handler' ) ); | |
add_action( 'init', array( __CLASS__, 'user_confirmation_activation' ) ); | |
// Activate plugin when new blog is added | |
add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); | |
} | |
/** | |
* Return the plugin slug. | |
* | |
* @since 1.0.0 | |
* | |
* @return Plugin slug variable. | |
*/ | |
public function get_plugin_slug() { | |
return $this->plugin_slug; | |
} | |
/** | |
* Return an instance of this class. | |
* | |
* @since 1.0.0 | |
* | |
* @return object A single instance of this class. | |
*/ | |
public static function get_instance() { | |
// If the single instance hasn't been set, set it now. | |
if ( null == self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
/** | |
* Fired when the plugin is activated. | |
* | |
* @since 1.0.0 | |
* | |
* @param boolean $network_wide True if WPMU superadmin uses | |
* "Network Activate" action, false if | |
* WPMU is disabled or plugin is | |
* activated on an individual blog. | |
*/ | |
public static function activate($network_wide) { | |
if ( function_exists( 'is_multisite' ) && is_multisite() ) { | |
if ( $network_wide ) { | |
// Get all blog ids | |
$blog_ids = self::get_blog_ids(); | |
foreach ( $blog_ids as $blog_id ) { | |
switch_to_blog( $blog_id ); | |
self::single_activate(); | |
restore_current_blog(); | |
} | |
} else { | |
self::single_activate(); | |
} | |
} else { | |
self::single_activate(); | |
} | |
} | |
/** | |
* Fired when the plugin is deactivated. | |
* | |
* @since 1.0.0 | |
* | |
* @param boolean $network_wide True if WPMU superadmin uses | |
* "Network Deactivate" action, false if | |
* WPMU is disabled or plugin is | |
* deactivated on an individual blog. | |
*/ | |
public static function deactivate($network_wide) { | |
if ( function_exists( 'is_multisite' ) && is_multisite() ) { | |
if ( $network_wide ) { | |
// Get all blog ids | |
$blog_ids = self::get_blog_ids(); | |
foreach ( $blog_ids as $blog_id ) { | |
switch_to_blog( $blog_id ); | |
self::single_deactivate(); | |
restore_current_blog(); | |
} | |
} else { | |
self::single_deactivate(); | |
} | |
} else { | |
self::single_deactivate(); | |
} | |
} | |
/** | |
* Fired when a new site is activated with a WPMU environment. | |
* | |
* @since 1.0.0 | |
* | |
* @param int $blog_id ID of the new blog. | |
*/ | |
public function activate_new_site($blog_id) { | |
if ( 1 !== did_action( 'wpmu_new_blog' ) ) { | |
return; | |
} | |
switch_to_blog( $blog_id ); | |
self::single_activate(); | |
restore_current_blog(); | |
} | |
/** | |
* Get all blog ids of blogs in the current network that are: | |
* - not archived | |
* - not spam | |
* - not deleted | |
* | |
* @since 1.0.0 | |
* | |
* @return array|false The blog ids, false if no matches. | |
*/ | |
private static function get_blog_ids() { | |
global $wpdb; | |
// get an array of blog ids | |
$sql = "SELECT blog_id FROM $wpdb->blogs | |
WHERE archived = '0' AND spam = '0' | |
AND deleted = '0'"; | |
return $wpdb->get_col( $sql ); | |
} | |
/** | |
* Fired for each blog when the plugin is activated. | |
* | |
* @since 1.0.0 | |
*/ | |
private static function single_activate() { | |
self::setup_db_tables(); | |
} | |
/** | |
* Fired for each blog when the plugin is deactivated. | |
* | |
* @since 1.0.0 | |
*/ | |
private static function single_deactivate() { | |
// disable the tables? | |
// @TODO: Define deactivation functionality here | |
} | |
public static function setup_db_tables() { | |
parent::setup_db_tables(); | |
} | |
/** | |
* Load the plugin text domain for translation. | |
* | |
* @since 1.0.0 | |
*/ | |
public function load_plugin_textdomain() { | |
$domain = $this->plugin_slug; | |
$locale = apply_filters( 'plugin_locale', get_locale(), $domain ); | |
load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' ); | |
load_plugin_textdomain( $domain, FALSE, basename( plugin_dir_path( dirname( __FILE__ ) ) ) . '/languages/' ); | |
} | |
/** | |
* Session start handler | |
*/ | |
public static function start_session() { | |
if ( !session_id() ) { | |
session_start(); | |
} | |
} | |
/** | |
* Handle the login functionality | |
*/ | |
public static function login_handler() { | |
if ( isset( $_POST['login-n'] ) and isset( $_POST['login-p'] ) ) { | |
$user = parent::login_user( esc_attr( $_POST['login-n'] ), $_POST['login-p'] ); | |
// if the user logged in successfully setup the $_SESSION variables | |
if ( $user ) { | |
$_SESSION['id'] = $user['id']; | |
$_SESSION['login'] = $user['login']; | |
} else { | |
echo 'Either username or password specified were wrong!'; | |
} | |
} | |
} | |
/** | |
* Check if the user is logged in and return his id | |
*/ | |
public static function is_user_logged_in() { | |
if ( isset( $_SESSION['id'] ) ) { | |
return intval( $_SESSION['id'] ); | |
} | |
return 0; | |
} | |
/** | |
* Handler the logout functionality | |
*/ | |
public static function logout_handler() { | |
if ( isset( $_GET['logout'] ) ) { | |
if ( session_id() ) { | |
session_destroy(); | |
wp_safe_redirect( home_url() ); | |
exit; | |
} | |
} | |
} | |
/** | |
* Handler the logout functionality | |
*/ | |
public static function forgot_p_handler() { | |
// has the form been submitted? | |
if ( isset( $_GET['forg-n'] ) ) { | |
$email = sanitize_email( $_GET['forg-n'] ); | |
// check if the user for the specified email exists | |
$user_id = parent::if_user_exists( $email ); | |
if ( $user_id ) { | |
$uniqid = uniqid( $user_id . '_' ); | |
$msg = "Lorem ipsum dolor sit amet \n"; | |
$link = get_field( 'm_chp', 'option' ); | |
// encode the user id | |
$link .= '?id=' . base64_encode( $user_id ); | |
$link .= '&uid=' . $uniqid; | |
$msg .= $link; | |
if ( parent::save_temp_id( $user_id, $uniqid ) ) { | |
wp_mail( $email, 'Like Save IT reset password link.', $msg ); | |
wp_safe_redirect( get_field( 'm_chp', 'option' ) ); | |
exit; | |
} | |
} else { | |
add_action( 'user_notification', function() { | |
?> | |
<h2>Wrong email specified.</h2> | |
<?php | |
} ); | |
} | |
} | |
} | |
/** | |
* Utility to check if the user is trying to recover the pass to gain access | |
*/ | |
public static function is_recovery_pass() { | |
if ( isset( $_GET['id'] ) and isset( $_GET['uid'] ) ) { | |
$user_id = base64_decode( $_GET['id'] ); | |
$uniqid = sanitize_key( $_GET['uid'] ); | |
if ( parent::check_temp_id( $user_id, $uniqid ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
/** | |
* Handler the password reset functionality | |
*/ | |
public static function reset_p_handler() { | |
// check if the two parameters are set | |
// TODO or check if the user is logged in | |
if ( isset( $_GET['id'] ) and isset( $_GET['uid'] ) ) { | |
$user_id = base64_decode( $_GET['id'] ); | |
$uniqid = sanitize_key( $_GET['uid'] ); | |
if ( parent::check_temp_id( $user_id, $uniqid ) ) { | |
add_action( 'user_notification', function() { | |
?> | |
<h2>Please reset your password below.</h2> | |
<?php | |
} ); | |
} else { | |
add_action( 'user_notification', function() { | |
?> | |
<h2>Invalid reset link or the link has expired.</h2> | |
<?php | |
} ); | |
} | |
} else if ( false ) { | |
// check if the user is logged in | |
} else { | |
// redirect - the page should not be visible | |
} | |
} | |
/** | |
* Search the GET paremetere | |
* for the activation link and | |
* activate the user if specified | |
* | |
* @hook init | |
*/ | |
public static function user_confirmation_activation() { | |
if ( isset( $_GET['activate'] ) ) { | |
$email = sanitize_email( base64_decode( $_GET['activate'] ) ); | |
// depending on the user activation redirect user to the update profile page | |
if ( parent::activate_user( $email ) ) { | |
wp_safe_redirect( 'http://localhost/likesaveit/wp/members-area-update-profile/?activated=true' ); | |
exit; | |
} else { | |
wp_safe_redirect( 'http://localhost/likesaveit/wp/members-area-update-profile/?expired=true' ); | |
exit; | |
} | |
} | |
} | |
/** | |
* Print user profile notifications | |
*/ | |
public static function user_profile_notification() { | |
if ( isset( $_GET['activated'] ) ) { | |
?> | |
<h2>You're profile has been successfully activated.</h2> | |
<p>Please complete the remaining fields to continue using the site.</p> | |
<br /> | |
<?php | |
} | |
if ( isset( $_GET['expired'] ) ) { | |
?> | |
<h2>You're profile has already been activated.</h2> | |
<br /> | |
<?php | |
} | |
} | |
/** | |
* Register and enqueue public-facing style sheet. | |
* | |
* @since 1.0.0 | |
*/ | |
public function enqueue_styles() { | |
wp_enqueue_style( $this->plugin_slug . '-plugin-styles', plugins_url( 'assets/css/public.css', __FILE__ ), array(), self::VERSION ); | |
} | |
/** | |
* Register and enqueues public-facing JavaScript files. | |
* | |
* @since 1.0.0 | |
*/ | |
public function enqueue_scripts() { | |
wp_enqueue_script( $this->plugin_slug . '-plugin-script', plugins_url( 'assets/js/public.js', __FILE__ ), array( 'jquery' ), self::VERSION ); | |
} | |
/** | |
* NOTE: Actions are points in the execution of a page or process | |
* lifecycle that WordPress fires. | |
* | |
* Actions: http://codex.wordpress.org/Plugin_API#Actions | |
* Reference: http://codex.wordpress.org/Plugin_API/Action_Reference | |
* | |
* @since 1.0.0 | |
*/ | |
public function action_method_name() { | |
// @TODO: Define your action hook callback here | |
} | |
/** | |
* NOTE: Filters are points of execution in which WordPress modifies data | |
* before saving it or sending it to the browser. | |
* | |
* Filters: http://codex.wordpress.org/Plugin_API#Filters | |
* Reference: http://codex.wordpress.org/Plugin_API/Filter_Reference | |
* | |
* @since 1.0.0 | |
*/ | |
public function filter_method_name() { | |
// @TODO: Define your filter hook callback here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment