| 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, $user_id = null ){ |
| 129 |
$key = '_templately_' . $key; |
| 130 |
|
| 131 |
if( $user_id === null ) { |
| 132 |
$user_id = $this->user_id(); |
| 133 |
} |
| 134 |
|
| 135 |
$updated = update_user_meta( $user_id, $key, $value ); |
| 136 |
|
| 137 |
if( $key === '_templately_api_key' && $updated ) { |
| 138 |
$this->has_api = true; |
| 139 |
} |
| 140 |
|
| 141 |
return $updated; |
| 142 |
} |
| 143 |
/** |
| 144 |
* Get optional usermeta or option data |
| 145 |
* |
| 146 |
* @param string $key |
| 147 |
* @param mixed $default |
| 148 |
* @return mixed |
| 149 |
*/ |
| 150 |
public function get( $key, $default = false, $user_id = null ){ |
| 151 |
$key = '_templately_' . $key; |
| 152 |
$_user_meta = get_user_meta( is_null( $user_id ) ? $this->user_id() : $user_id, $key, true ); |
| 153 |
return ! empty( $_user_meta ) ? $_user_meta : $default; |
| 154 |
} |
| 155 |
/** |
| 156 |
* Remove options data or usermeta |
| 157 |
* |
| 158 |
* @param string $key |
| 159 |
* @return Options |
| 160 |
*/ |
| 161 |
public function remove( $key ){ |
| 162 |
$key = '_templately_' . $key; |
| 163 |
$updated = delete_user_meta( $this->user_id(), $key ); // delete user meta |
| 164 |
|
| 165 |
if( $key === '_templately_api_key' && $updated ) { |
| 166 |
$this->has_api = false; |
| 167 |
} |
| 168 |
|
| 169 |
return $this; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get option data |
| 174 |
* |
| 175 |
* @since 2.0.1 |
| 176 |
* |
| 177 |
* @param string $key |
| 178 |
* @param mixed $default |
| 179 |
* |
| 180 |
* @return mixed |
| 181 |
*/ |
| 182 |
public function get_option( $key, $default = false ){ |
| 183 |
return get_option( $key, $default ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Update option data |
| 188 |
* |
| 189 |
* @since 2.0.1 |
| 190 |
* |
| 191 |
* @param string $key |
| 192 |
* @param mixed $value |
| 193 |
* @param string $autoload |
| 194 |
* |
| 195 |
* @return bool |
| 196 |
*/ |
| 197 |
public function update_option( $key, $value, $autoload = 'no' ){ |
| 198 |
return update_option( $key, $value, $autoload ); |
| 199 |
} |
| 200 |
} |