| 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' ) ) { exit; } |
| 14 |
|
| 15 |
if ( ! class_exists( 'Charitable_User_Avatar' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Charitable_User_Avatar |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Charitable_User_Avatar { |
| 23 |
|
| 24 |
/* @var string */ |
| 25 |
const VERSION = '1.0.7'; |
| 26 |
|
| 27 |
/* @var string */ |
| 28 |
const DB_VERSION = '20150818'; |
| 29 |
|
| 30 |
/* @var string */ |
| 31 |
const NAME = 'Charitable User Avatar'; |
| 32 |
|
| 33 |
/* @var string */ |
| 34 |
const AUTHOR = 'Studio 164a'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Single class instance. |
| 38 |
* |
| 39 |
* @var Charitable_User_Avatar |
| 40 |
*/ |
| 41 |
private static $instance = null; |
| 42 |
|
| 43 |
/** |
| 44 |
* The root file of the plugin. |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
private $plugin_file; |
| 49 |
|
| 50 |
/** |
| 51 |
* The root directory of the plugin. |
| 52 |
* |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
private $directory_path; |
| 56 |
|
| 57 |
/** |
| 58 |
* The root directory of the plugin as a URL. |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
private $directory_url; |
| 63 |
|
| 64 |
/** |
| 65 |
* Store of registered objects. |
| 66 |
* |
| 67 |
* @var array |
| 68 |
*/ |
| 69 |
private $registry; |
| 70 |
|
| 71 |
/** |
| 72 |
* Create class instance. |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
* |
| 76 |
* @param string $plugin_file The core plugin file. |
| 77 |
*/ |
| 78 |
public function __construct() { |
| 79 |
// $this->plugin_file = $plugin_file; |
| 80 |
// $this->directory_path = plugin_dir_path( $plugin_file ); |
| 81 |
// $this->directory_url = plugin_dir_url( $plugin_file ); |
| 82 |
|
| 83 |
add_action( 'charitable_start', array( $this, 'start' ), 5 ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the original instance of this class. |
| 88 |
* |
| 89 |
* @since 1.0.0 |
| 90 |
* |
| 91 |
* @return Charitable_User_Avatar |
| 92 |
*/ |
| 93 |
public static function get_instance() { |
| 94 |
return self::$instance; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Run the startup sequence on the charitable_start hook. |
| 99 |
* |
| 100 |
* This is only ever executed once. |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function start() { |
| 107 |
// If we've already started (i.e. run this function once before), do not pass go. |
| 108 |
if ( $this->started() ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
// Set static instance. |
| 113 |
self::$instance = $this; |
| 114 |
|
| 115 |
// $this->load_dependencies(); |
| 116 |
|
| 117 |
$this->attach_hooks_and_filters(); |
| 118 |
|
| 119 |
// $this->maybe_start_ambassadors(); |
| 120 |
|
| 121 |
// $this->setup_licensing(); |
| 122 |
|
| 123 |
// $this->setup_i18n(); |
| 124 |
|
| 125 |
// Hook in here to do something when the plugin is first loaded. |
| 126 |
do_action( 'charitable_user_avatar_start', $this ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Include necessary files. |
| 131 |
* |
| 132 |
* @return void |
| 133 |
* @since 1.0.0 |
| 134 |
*/ |
| 135 |
private function load_dependencies() { |
| 136 |
require_once( $this->get_path( 'includes' ) . 'charitable-user-avatar-functions.php' ); |
| 137 |
require_once( $this->get_path( 'includes' ) . 'class-charitable-user-avatar-profile-form.php' ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Set up hook and filter callback functions. |
| 142 |
* |
| 143 |
* @return void |
| 144 |
* @since 1.0.0 |
| 145 |
*/ |
| 146 |
private function attach_hooks_and_filters() { |
| 147 |
// add_action( 'charitable_user_avatar_start', array( 'Charitable_User_Avatar_Profile_Form', 'start' ) ); |
| 148 |
add_filter( 'charitable_user_avatar', array( $this, 'get_user_avatar' ), 10, 3 ); |
| 149 |
add_filter( 'charitable_user_avatar_src', array( $this, 'get_user_avatar_src' ), 10, 3 ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Load up the Ambassadors integration if Ambassadors is installed. |
| 154 |
* |
| 155 |
* @return void |
| 156 |
* @since 1.0.2 |
| 157 |
*/ |
| 158 |
private function maybe_start_ambassadors() { |
| 159 |
if ( ! class_exists( 'Charitable_Ambassadors' ) ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
require_once( $this->get_path( 'includes' ) . 'ambassadors/class-charitable-user-avatar-campaign-form.php' ); |
| 164 |
|
| 165 |
add_action( 'charitable_user_avatar_start', array( 'Charitable_User_Avatar_Campaign_Form', 'start' ) ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Set up licensing for the extension. |
| 170 |
* |
| 171 |
* @return void |
| 172 |
* @since 1.0.0 |
| 173 |
*/ |
| 174 |
private function setup_licensing() { |
| 175 |
charitable_get_helper( 'licenses' )->register_licensed_product( |
| 176 |
Charitable_User_Avatar::NAME, |
| 177 |
Charitable_User_Avatar::AUTHOR, |
| 178 |
Charitable_User_Avatar::VERSION, |
| 179 |
$this->plugin_file |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Set up the internationalisation for the plugin. |
| 185 |
* |
| 186 |
* @return void |
| 187 |
* @since 1.1.0 |
| 188 |
*/ |
| 189 |
private function setup_i18n() { |
| 190 |
if ( class_exists( 'Charitable_i18n' ) ) { |
| 191 |
|
| 192 |
require_once( $this->get_path( 'includes' ) . 'i18n/class-charitable-user-avatar-i18n.php' ); |
| 193 |
|
| 194 |
Charitable_User_Avatar_i18n::get_instance(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Returns whether we are currently in the start phase of the plugin. |
| 200 |
* |
| 201 |
* @return bool |
| 202 |
* @since 1.0.0 |
| 203 |
*/ |
| 204 |
public function is_start() { |
| 205 |
return current_filter() == 'charitable_user_avatar_start'; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Returns whether the plugin has already started. |
| 210 |
* |
| 211 |
* @return bool |
| 212 |
* @since 1.0.0 |
| 213 |
*/ |
| 214 |
public function started() { |
| 215 |
return did_action( 'charitable_user_avatar_start' ) || current_filter() == 'charitable_user_avatar_start'; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Returns the plugin's version number. |
| 220 |
* |
| 221 |
* @return string |
| 222 |
* @since 1.0.0 |
| 223 |
*/ |
| 224 |
public function get_version() { |
| 225 |
return self::VERSION; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Returns plugin paths. |
| 230 |
* |
| 231 |
* @param string $path // If empty, returns the path to the plugin. |
| 232 |
* @param bool $absolute_path // If true, returns the file system path. If false, returns it as a URL. |
| 233 |
* @return string |
| 234 |
* @since 1.0.0 |
| 235 |
*/ |
| 236 |
public function get_path($type = '', $absolute_path = true ) { |
| 237 |
$base = $absolute_path ? $this->directory_path : $this->directory_url; |
| 238 |
|
| 239 |
switch( $type ) { |
| 240 |
case 'includes' : |
| 241 |
$path = $base . 'includes/'; |
| 242 |
break; |
| 243 |
|
| 244 |
case 'admin' : |
| 245 |
$path = $base . 'includes/admin/'; |
| 246 |
break; |
| 247 |
|
| 248 |
case 'templates' : |
| 249 |
$path = $base . 'templates/'; |
| 250 |
break; |
| 251 |
|
| 252 |
case 'assets' : |
| 253 |
$path = $base . 'assets/'; |
| 254 |
break; |
| 255 |
|
| 256 |
case 'directory' : |
| 257 |
$path = $base; |
| 258 |
break; |
| 259 |
|
| 260 |
default : |
| 261 |
$path = $this->plugin_file; |
| 262 |
} |
| 263 |
|
| 264 |
return $path; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Stores an object in the plugin's registry. |
| 269 |
* |
| 270 |
* @since 1.0.0 |
| 271 |
* |
| 272 |
* @param mixed $object |
| 273 |
* @return void |
| 274 |
*/ |
| 275 |
public function register_object( $object ) { |
| 276 |
if ( ! is_object( $object ) ) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
$class = get_class( $object ); |
| 281 |
|
| 282 |
$this->registry[ $class ] = $object; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Returns a registered object. |
| 287 |
* |
| 288 |
* @param string $class The type of class you want to retrieve. |
| 289 |
* @return mixed The object if its registered. Otherwise false. |
| 290 |
* @since 1.0.0 |
| 291 |
*/ |
| 292 |
public function get_object( $class ) { |
| 293 |
return isset( $this->registry[ $class ] ) ? $this->registry[ $class ] : false; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Return the user's avatar, or false if the user has not uploaded their own avatar. |
| 298 |
* |
| 299 |
* @since 1.0.0 |
| 300 |
* |
| 301 |
* @param string|false $avatar The default avatar to use if one hasn't been set. |
| 302 |
* @param Charitable_User $user The Charitable_User object. |
| 303 |
* @return string |
| 304 |
*/ |
| 305 |
public function get_user_avatar( $avatar, Charitable_User $user ) { |
| 306 |
|
| 307 |
$custom_avatar = get_user_meta( $user->ID, 'avatar', true ); |
| 308 |
|
| 309 |
if ( empty( $custom_avatar ) ) { |
| 310 |
return $avatar; |
| 311 |
} |
| 312 |
|
| 313 |
if ( is_null( get_post( $custom_avatar ) ) ) { |
| 314 |
return $avatar; |
| 315 |
} |
| 316 |
|
| 317 |
return $custom_avatar; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Return the src of the user's avatar, or false if they have not uploaded their own. |
| 322 |
* |
| 323 |
* @param string|false $avatar_src |
| 324 |
* @param Charitable_User $user |
| 325 |
* @param string $size |
| 326 |
* @return string|false |
| 327 |
* @since 1.0.0 |
| 328 |
*/ |
| 329 |
public function get_user_avatar_src( $avatar_src, Charitable_User $user, $size = '' ) { |
| 330 |
$avatar_attachment_id = $this->get_user_avatar( $avatar_src, $user ); |
| 331 |
|
| 332 |
if ( $avatar_attachment_id ) { |
| 333 |
$attachment_src = wp_get_attachment_image_src( $avatar_attachment_id, $size ); |
| 334 |
|
| 335 |
if ( $attachment_src ) { |
| 336 |
$avatar_src = $attachment_src[ 0 ]; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return $avatar_src; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Throw error on object clone. |
| 345 |
* |
| 346 |
* This class is specifically designed to be instantiated once. You can retrieve the instance using charitable() |
| 347 |
* |
| 348 |
* @since 1.0.0 |
| 349 |
* @return void |
| 350 |
*/ |
| 351 |
public function __clone() { |
| 352 |
charitable_get_deprecated()->doing_it_wrong( |
| 353 |
__FUNCTION__, |
| 354 |
__( 'Cheatin’ huh?', 'charitable-user-avatar' ), |
| 355 |
'1.0.0' |
| 356 |
); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Disable unserializing of the class. |
| 361 |
* |
| 362 |
* @since 1.0.0 |
| 363 |
* @return void |
| 364 |
*/ |
| 365 |
public function __wakeup() { |
| 366 |
charitable_get_deprecated()->doing_it_wrong( |
| 367 |
__FUNCTION__, |
| 368 |
__( 'Cheatin’ huh?', 'charitable-user-avatar' ), |
| 369 |
'1.0.0' |
| 370 |
); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
endif; |
| 375 |
|