| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gallery |
| 4 |
* |
| 5 |
* @package Powerkit |
| 6 |
* @subpackage Extensions |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Init module |
| 11 |
*/ |
| 12 |
class Powerkit_User extends Powerkit_Module { |
| 13 |
|
| 14 |
/** |
| 15 |
* Register module |
| 16 |
*/ |
| 17 |
public function register() { |
| 18 |
$this->name = 'user'; |
| 19 |
$this->slug = 'user'; |
| 20 |
$this->type = 'extension'; |
| 21 |
$this->category = 'basic'; |
| 22 |
$this->public = false; |
| 23 |
$this->enabled = true; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Initialize module |
| 28 |
*/ |
| 29 |
public function initialize() { |
| 30 |
// Helpers Functions for the module. |
| 31 |
require_once dirname( __FILE__ ) . '/helpers/helper-powerkit-user.php'; |
| 32 |
|
| 33 |
// Filters. |
| 34 |
add_filter( 'init', array( $this, 'powerkit_guest_support_meta' ) ); |
| 35 |
add_filter( 'pre_get_avatar', array( $this, 'powerkit_guest_support_avatar' ), 10, 3 ); |
| 36 |
add_filter( 'author_link', array( $this, 'powerkit_guest_support_link' ), 9999, 3 ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Add support meta info for guest authors. |
| 41 |
*/ |
| 42 |
public function powerkit_guest_support_meta() { |
| 43 |
$fields = array( |
| 44 |
'user_url', |
| 45 |
'url', |
| 46 |
'display_name', |
| 47 |
'first_name', |
| 48 |
'last_name', |
| 49 |
'user_email', |
| 50 |
'website', |
| 51 |
'description', |
| 52 |
); |
| 53 |
|
| 54 |
foreach ( $fields as $field ) { |
| 55 |
/** |
| 56 |
* Filters the value of the requested user metadata. |
| 57 |
* |
| 58 |
* @param string $value The value of the metadata. |
| 59 |
* @param int $user_id The user ID for the value. |
| 60 |
* @param int|bool $original The original user ID, as passed to the function. |
| 61 |
*/ |
| 62 |
add_filter( 'get_the_author_' . $field, function( $value, $user_id, $original = null ) use ( $field ) { |
| 63 |
if ( powerkit_is_guest( $user_id ) && empty( $value ) ) { |
| 64 |
$guest_value = powerkit_get_guest_meta( $field, $user_id ); |
| 65 |
if ( $guest_value ) { |
| 66 |
$value = $guest_value; |
| 67 |
} |
| 68 |
} |
| 69 |
return $value; |
| 70 |
}, 10, 3 ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Add support avatar for guest authors. |
| 76 |
* |
| 77 |
* @param string $link The URL to the author's page. |
| 78 |
* @param int $author_id The author's id. |
| 79 |
* @param string $author_nicename The author's nice name. |
| 80 |
*/ |
| 81 |
public function powerkit_guest_support_link( $link, $author_id, $author_nicename ) { |
| 82 |
global $wp_rewrite; |
| 83 |
|
| 84 |
if ( powerkit_is_guest( $author_id ) ) { |
| 85 |
$guest = powerkit_get_guest_meta( 'user_login', $author_id ); |
| 86 |
|
| 87 |
if ( $guest ) { |
| 88 |
$link = $wp_rewrite->get_author_permastruct(); |
| 89 |
|
| 90 |
$link = str_replace( '%author%', $guest, $link ); |
| 91 |
|
| 92 |
$link = home_url( user_trailingslashit( $link ) ); |
| 93 |
} |
| 94 |
} |
| 95 |
return $link; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Add support avatar for guest authors. |
| 100 |
* |
| 101 |
* @param string $avatar HTML for the user's avatar. Default null. |
| 102 |
* @param mixed $id_or_email The Gravatar to retrieve. |
| 103 |
* @param array $args Arguments passed to get_avatar_url(), after processing. |
| 104 |
*/ |
| 105 |
public function powerkit_guest_support_avatar( $avatar, $id_or_email, $args ) { |
| 106 |
if ( powerkit_is_guest( $id_or_email ) && empty( $avatar ) ) { |
| 107 |
$guest_avatar = coauthors_get_avatar( (object) array( |
| 108 |
'ID' => $id_or_email, |
| 109 |
'type' => 'guest-author', |
| 110 |
), $args['size'] ); |
| 111 |
|
| 112 |
if ( $guest_avatar ) { |
| 113 |
$avatar = $guest_avatar; |
| 114 |
} |
| 115 |
} |
| 116 |
return $avatar; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
new Powerkit_User(); |
| 121 |
|