| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Utils; |
| 4 |
|
| 5 |
use function get_option; |
| 6 |
use function update_option; |
| 7 |
use function get_user_meta; |
| 8 |
use function update_user_meta; |
| 9 |
use function get_current_user_id; |
| 10 |
|
| 11 |
class Options extends Base { |
| 12 |
/** |
| 13 |
* Currest User Id |
| 14 |
* @var integer |
| 15 |
*/ |
| 16 |
private $current_user = 0; |
| 17 |
|
| 18 |
/** |
| 19 |
* User has any api? |
| 20 |
* @var boolean |
| 21 |
*/ |
| 22 |
private $has_api = false; |
| 23 |
|
| 24 |
/** |
| 25 |
* Autometically invoked and setup the properties. |
| 26 |
*/ |
| 27 |
public function __construct(){ |
| 28 |
$this->current_user = get_current_user_id(); |
| 29 |
$this->has_api = ! empty( $this->get( 'api_key', '', $this->current_user ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the current user ID. |
| 34 |
* @return int |
| 35 |
*/ |
| 36 |
public function current_user_id(){ |
| 37 |
return $this->current_user; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Can a user link another templately account in a setup?. |
| 42 |
* @return boolean |
| 43 |
*/ |
| 44 |
public function link_account() { |
| 45 |
return $this->whoami() === 'link' && ! $this->has_api; |
| 46 |
} |
| 47 |
|
| 48 |
public function unlink_account() { |
| 49 |
return ( $this->whoami() === 'link' || $this->whoami() === 'local' ) && $this->has_api; |
| 50 |
} |
| 51 |
/** |
| 52 |
* Get determined who am I. |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
public function whoami(){ |
| 56 |
$_whoami = 'local'; |
| 57 |
|
| 58 |
if( $this->is_global() > 0 && $this->is_global() === $this->current_user ) { |
| 59 |
$_whoami = 'global'; |
| 60 |
} |
| 61 |
|
| 62 |
if( $this->is_global() > 0 && $this->is_global() !== $this->current_user ) { |
| 63 |
$_whoami = 'link'; |
| 64 |
} |
| 65 |
|
| 66 |
if( $this->is_global() == 0 ) { |
| 67 |
$_whoami = 'local'; |
| 68 |
} |
| 69 |
|
| 70 |
return $_whoami; |
| 71 |
} |
| 72 |
/** |
| 73 |
* Get user id determine dynamically |
| 74 |
* @return integer |
| 75 |
*/ |
| 76 |
private function user_id(){ |
| 77 |
$_whoami = $this->whoami(); |
| 78 |
// Helper::log( '_whoami: ' . $_whoami ); |
| 79 |
|
| 80 |
if( ! empty( $_SERVER['REQUEST_URI'] ) ) { |
| 81 |
$parse_uri = explode( '/', substr( $_SERVER['REQUEST_URI'], 0, strpos( $_SERVER['REQUEST_URI'], '?' ) ) ); |
| 82 |
if( $_whoami === 'link' && array_pop( $parse_uri ) === 'login' ) { |
| 83 |
return $this->current_user; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if( $_whoami === 'link' && $this->has_api ) { |
| 88 |
return $this->current_user; |
| 89 |
} |
| 90 |
|
| 91 |
return $_whoami === 'local' ? $this->current_user : $this->is_global(); |
| 92 |
} |
| 93 |
/** |
| 94 |
* Globally logged in and the User ID of globally logged-in user. |
| 95 |
* @return integer |
| 96 |
*/ |
| 97 |
public function is_global(){ |
| 98 |
return intval( get_option('_templately_global_login', 0) ); |
| 99 |
} |
| 100 |
/** |
| 101 |
* Set global login flag |
| 102 |
* @return boolean |
| 103 |
*/ |
| 104 |
public static function set_global_login() { |
| 105 |
return update_option('_templately_global_login', get_current_user_id(), 'no'); |
| 106 |
} |
| 107 |
/** |
| 108 |
* Remove global login flag |
| 109 |
* @return boolean |
| 110 |
*/ |
| 111 |
public function remove_global_login() { |
| 112 |
return delete_option( '_templately_global_login' ); |
| 113 |
} |
| 114 |
|
| 115 |
public function is_globally_signed() { |
| 116 |
return $this->whoami() !== 'local'; |
| 117 |
} |
| 118 |
public function signed_as_global() { |
| 119 |
return $this->current_user === $this->is_global(); |
| 120 |
} |
| 121 |
/** |
| 122 |
* Set optional usermeta or option data |
| 123 |
* |
| 124 |
* @param string $key |
| 125 |
* @param mixed $value |
| 126 |
* @return boolean |
| 127 |
*/ |
| 128 |
public function set( $key, $value ){ |
| 129 |
$key = '_templately_' . $key; |
| 130 |
$updated = update_user_meta( $this->user_id(), $key, $value ); |
| 131 |
|
| 132 |
if( $key === '_templately_api_key' && $updated ) { |
| 133 |
$this->has_api = true; |
| 134 |
} |
| 135 |
|
| 136 |
return $updated; |
| 137 |
} |
| 138 |
/** |
| 139 |
* Get optional usermeta or option data |
| 140 |
* |
| 141 |
* @param string $key |
| 142 |
* @param mixed $default |
| 143 |
* @return mixed |
| 144 |
*/ |
| 145 |
public function get( $key, $default = false, $user_id = null ){ |
| 146 |
$key = '_templately_' . $key; |
| 147 |
$_user_meta = get_user_meta( is_null( $user_id ) ? $this->user_id() : $user_id, $key, true ); |
| 148 |
return ! empty( $_user_meta ) ? $_user_meta : $default; |
| 149 |
} |
| 150 |
/** |
| 151 |
* Remove options data or usermeta |
| 152 |
* |
| 153 |
* @param string $key |
| 154 |
* @return Options |
| 155 |
*/ |
| 156 |
public function remove( $key ){ |
| 157 |
$key = '_templately_' . $key; |
| 158 |
$updated = delete_user_meta( $this->user_id(), $key ); // delete user meta |
| 159 |
|
| 160 |
if( $key === '_templately_api_key' && $updated ) { |
| 161 |
$this->has_api = false; |
| 162 |
} |
| 163 |
|
| 164 |
return $this; |
| 165 |
} |
| 166 |
} |