| 1 |
<?php |
| 2 |
/** |
| 3 |
* The main Charitable User Avatar class. |
| 4 |
* |
| 5 |
* The responsibility of this class is to load all the plugin's functionality. |
| 6 |
* |
| 7 |
* @package Charitable User Avatar |
| 8 |
* @copyright Copyright (c) 2017, Eric Daams |
| 9 |
* @license http://opensource.org/licenses/gpl-1.0.0.php GNU Public License |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; } |
| 15 |
|
| 16 |
if ( ! class_exists( 'Charitable_User_Avatar' ) ) : |
| 17 |
|
| 18 |
/** |
| 19 |
* Charitable_User_Avatar |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Charitable_User_Avatar { |
| 24 |
|
| 25 |
/* @var string */ |
| 26 |
const VERSION = '1.0.7'; |
| 27 |
|
| 28 |
/* @var string */ |
| 29 |
const DB_VERSION = '20150818'; |
| 30 |
|
| 31 |
/* @var string */ |
| 32 |
const NAME = 'Charitable User Avatar'; |
| 33 |
|
| 34 |
/* @var string */ |
| 35 |
const AUTHOR = 'WPCharitable'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Single class instance. |
| 39 |
* |
| 40 |
* @var Charitable_User_Avatar |
| 41 |
*/ |
| 42 |
private static $instance = null; |
| 43 |
|
| 44 |
/** |
| 45 |
* The root file of the plugin. |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
private $plugin_file; |
| 50 |
|
| 51 |
/** |
| 52 |
* The root directory of the plugin. |
| 53 |
* |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
private $directory_path; |
| 57 |
|
| 58 |
/** |
| 59 |
* The root directory of the plugin as a URL. |
| 60 |
* |
| 61 |
* @var string |
| 62 |
*/ |
| 63 |
private $directory_url; |
| 64 |
|
| 65 |
/** |
| 66 |
* Store of registered objects. |
| 67 |
* |
| 68 |
* @var array |
| 69 |
*/ |
| 70 |
private $registry; |
| 71 |
|
| 72 |
/** |
| 73 |
* Create class instance. |
| 74 |
* |
| 75 |
* @since 1.0.0 |
| 76 |
*/ |
| 77 |
public function __construct() { |
| 78 |
add_action( 'charitable_start', array( $this, 'start' ), 5 ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the original instance of this class. |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
* @return Charitable_User_Avatar |
| 87 |
*/ |
| 88 |
public static function get_instance() { |
| 89 |
return self::$instance; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Run the startup sequence on the charitable_start hook. |
| 94 |
* |
| 95 |
* This is only ever executed once. |
| 96 |
* |
| 97 |
* @since 1.0.0 |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function start() { |
| 102 |
// If we've already started (i.e. run this function once before), do not pass go. |
| 103 |
if ( $this->started() ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
// Set static instance. |
| 108 |
self::$instance = $this; |
| 109 |
|
| 110 |
$this->attach_hooks_and_filters(); |
| 111 |
|
| 112 |
// Hook in here to do something when the first loaded. |
| 113 |
do_action( 'charitable_user_avatar_start', $this ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Set up hook and filter callback functions. |
| 118 |
* |
| 119 |
* @return void |
| 120 |
* @since 1.0.0 |
| 121 |
*/ |
| 122 |
private function attach_hooks_and_filters() { |
| 123 |
add_filter( 'charitable_user_avatar', array( $this, 'get_user_avatar' ), 10, 3 ); |
| 124 |
add_filter( 'charitable_user_avatar_src', array( $this, 'get_user_avatar_src' ), 10, 3 ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns whether we are currently in the start phase of the plugin. |
| 129 |
* |
| 130 |
* @return bool |
| 131 |
* @since 1.0.0 |
| 132 |
*/ |
| 133 |
public function is_start() { |
| 134 |
return current_filter() == 'charitable_user_avatar_start'; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Returns whether the plugin has already started. |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
* @since 1.0.0 |
| 142 |
*/ |
| 143 |
public function started() { |
| 144 |
return did_action( 'charitable_user_avatar_start' ) || current_filter() == 'charitable_user_avatar_start'; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the plugin's version number. |
| 149 |
* |
| 150 |
* @return string |
| 151 |
* @since 1.0.0 |
| 152 |
*/ |
| 153 |
public function get_version() { |
| 154 |
return self::VERSION; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Returns plugin paths. |
| 159 |
* |
| 160 |
* @param string $type If empty, returns the path to the plugin. |
| 161 |
* @param bool $absolute_path If true, returns the file system path. If false, returns it as a URL. |
| 162 |
* @return string |
| 163 |
* @since 1.0.0 |
| 164 |
*/ |
| 165 |
public function get_path( $type = '', $absolute_path = true ) { |
| 166 |
$base = $absolute_path ? $this->directory_path : $this->directory_url; |
| 167 |
|
| 168 |
switch ( $type ) { |
| 169 |
case 'includes': |
| 170 |
$path = $base . 'includes/'; |
| 171 |
break; |
| 172 |
|
| 173 |
case 'admin': |
| 174 |
$path = $base . 'includes/admin/'; |
| 175 |
break; |
| 176 |
|
| 177 |
case 'templates': |
| 178 |
$path = $base . 'templates/'; |
| 179 |
break; |
| 180 |
|
| 181 |
case 'assets': |
| 182 |
$path = $base . 'assets/'; |
| 183 |
break; |
| 184 |
|
| 185 |
case 'directory': |
| 186 |
$path = $base; |
| 187 |
break; |
| 188 |
|
| 189 |
default: |
| 190 |
$path = $this->plugin_file; |
| 191 |
} |
| 192 |
|
| 193 |
return $path; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Stores an object in the plugin's registry. |
| 198 |
* |
| 199 |
* @since 1.0.0 |
| 200 |
* |
| 201 |
* @param mixed $object The object to store. |
| 202 |
* @return void |
| 203 |
*/ |
| 204 |
public function register_object( $object ) { |
| 205 |
if ( ! is_object( $object ) ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
$class = get_class( $object ); |
| 210 |
|
| 211 |
$this->registry[ $class ] = $object; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Returns a registered object. |
| 216 |
* |
| 217 |
* @param string $class The type of class you want to retrieve. |
| 218 |
* @return mixed The object if its registered. Otherwise false. |
| 219 |
* @since 1.0.0 |
| 220 |
*/ |
| 221 |
public function get_object( $class ) { |
| 222 |
return isset( $this->registry[ $class ] ) ? $this->registry[ $class ] : false; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Return the user's avatar, or false if the user has not uploaded their own avatar. |
| 227 |
* |
| 228 |
* @since 1.0.0 |
| 229 |
* |
| 230 |
* @param string|false $avatar The default avatar to use if one hasn't been set. |
| 231 |
* @param Charitable_User $user The Charitable_User object. |
| 232 |
* @return string |
| 233 |
*/ |
| 234 |
public function get_user_avatar( $avatar, Charitable_User $user ) { |
| 235 |
|
| 236 |
$custom_avatar = get_user_meta( $user->ID, 'avatar', true ); |
| 237 |
|
| 238 |
if ( empty( $custom_avatar ) ) { |
| 239 |
return $avatar; |
| 240 |
} |
| 241 |
|
| 242 |
if ( is_null( get_post( $custom_avatar ) ) ) { |
| 243 |
return $avatar; |
| 244 |
} |
| 245 |
|
| 246 |
return $custom_avatar; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Return the src of the user's avatar, or false if they have not uploaded their own. |
| 251 |
* |
| 252 |
* @param string|false $avatar_src |
| 253 |
* @param Charitable_User $user |
| 254 |
* @param string $size |
| 255 |
* @return string|false |
| 256 |
* @since 1.0.0 |
| 257 |
*/ |
| 258 |
public function get_user_avatar_src( $avatar_src, Charitable_User $user, $size = '' ) { |
| 259 |
$avatar_attachment_id = $this->get_user_avatar( $avatar_src, $user ); |
| 260 |
|
| 261 |
if ( $avatar_attachment_id ) { |
| 262 |
$attachment_src = wp_get_attachment_image_src( $avatar_attachment_id, $size ); |
| 263 |
|
| 264 |
if ( $attachment_src ) { |
| 265 |
$avatar_src = $attachment_src[0]; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return $avatar_src; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Throw error on object clone. |
| 274 |
* |
| 275 |
* This class is specifically designed to be instantiated once. You can retrieve the instance using charitable() |
| 276 |
* |
| 277 |
* @since 1.0.0 |
| 278 |
* @return void |
| 279 |
*/ |
| 280 |
public function __clone() { |
| 281 |
charitable_get_deprecated()->doing_it_wrong( |
| 282 |
__FUNCTION__, |
| 283 |
__( 'Cheatin’ huh?', 'charitable' ), |
| 284 |
'1.0.0' |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Disable unserializing of the class. |
| 290 |
* |
| 291 |
* @since 1.0.0 |
| 292 |
* @return void |
| 293 |
*/ |
| 294 |
public function __wakeup() { |
| 295 |
charitable_get_deprecated()->doing_it_wrong( |
| 296 |
__FUNCTION__, |
| 297 |
__( 'Cheatin’ huh?', 'charitable' ), |
| 298 |
'1.0.0' |
| 299 |
); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
endif; |
| 304 |
|