first commit
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* Gallery
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Extensions
|
||||
*/
|
||||
|
||||
if ( class_exists( 'Powerkit_Module' ) ) {
|
||||
/**
|
||||
* Init module
|
||||
*/
|
||||
class Powerkit_User extends Powerkit_Module {
|
||||
|
||||
/**
|
||||
* Register module
|
||||
*/
|
||||
public function register() {
|
||||
$this->name = 'user';
|
||||
$this->slug = 'user';
|
||||
$this->type = 'extension';
|
||||
$this->category = 'basic';
|
||||
$this->public = false;
|
||||
$this->enabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize module
|
||||
*/
|
||||
public function initialize() {
|
||||
// Helpers Functions for the module.
|
||||
require_once dirname( __FILE__ ) . '/helpers/helper-powerkit-user.php';
|
||||
|
||||
// Filters.
|
||||
add_filter( 'init', array( $this, 'powerkit_guest_support_meta' ) );
|
||||
add_filter( 'pre_get_avatar', array( $this, 'powerkit_guest_support_avatar' ), 10, 3 );
|
||||
add_filter( 'author_link', array( $this, 'powerkit_guest_support_link' ), 9999, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add support meta info for guest authors.
|
||||
*/
|
||||
public function powerkit_guest_support_meta() {
|
||||
$fields = array(
|
||||
'user_url',
|
||||
'url',
|
||||
'display_name',
|
||||
'first_name',
|
||||
'last_name',
|
||||
'user_email',
|
||||
'website',
|
||||
'description',
|
||||
);
|
||||
|
||||
foreach ( $fields as $field ) {
|
||||
/**
|
||||
* Filters the value of the requested user metadata.
|
||||
*
|
||||
* @param string $value The value of the metadata.
|
||||
* @param int $user_id The user ID for the value.
|
||||
* @param int|bool $original The original user ID, as passed to the function.
|
||||
*/
|
||||
add_filter( 'get_the_author_' . $field, function( $value, $user_id, $original = null ) use ( $field ) {
|
||||
if ( powerkit_is_guest( $user_id ) && empty( $value ) ) {
|
||||
$guest_value = powerkit_get_guest_meta( $field, $user_id );
|
||||
if ( $guest_value ) {
|
||||
$value = $guest_value;
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}, 10, 3 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add support avatar for guest authors.
|
||||
*
|
||||
* @param string $link The URL to the author's page.
|
||||
* @param int $author_id The author's id.
|
||||
* @param string $author_nicename The author's nice name.
|
||||
*/
|
||||
public function powerkit_guest_support_link( $link, $author_id, $author_nicename ) {
|
||||
global $wp_rewrite;
|
||||
|
||||
if ( powerkit_is_guest( $author_id ) ) {
|
||||
$guest = powerkit_get_guest_meta( 'user_login', $author_id );
|
||||
|
||||
if ( $guest ) {
|
||||
$link = $wp_rewrite->get_author_permastruct();
|
||||
|
||||
$link = str_replace( '%author%', $guest, $link );
|
||||
|
||||
$link = home_url( user_trailingslashit( $link ) );
|
||||
}
|
||||
}
|
||||
return $link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add support avatar for guest authors.
|
||||
*
|
||||
* @param string $avatar HTML for the user's avatar. Default null.
|
||||
* @param mixed $id_or_email The Gravatar to retrieve.
|
||||
* @param array $args Arguments passed to get_avatar_url(), after processing.
|
||||
*/
|
||||
public function powerkit_guest_support_avatar( $avatar, $id_or_email, $args ) {
|
||||
if ( powerkit_is_guest( $id_or_email ) && empty( $avatar ) ) {
|
||||
$guest_avatar = coauthors_get_avatar( (object) array(
|
||||
'ID' => $id_or_email,
|
||||
'type' => 'guest-author',
|
||||
), $args['size'] );
|
||||
|
||||
if ( $guest_avatar ) {
|
||||
$avatar = $guest_avatar;
|
||||
}
|
||||
}
|
||||
return $avatar;
|
||||
}
|
||||
}
|
||||
|
||||
new Powerkit_User();
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
/**
|
||||
* Helpers to empower authors and users
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules/Helper
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check enabled CoAuthors.
|
||||
*/
|
||||
function powerkit_coauthors_enabled() {
|
||||
return class_exists( 'CoAuthors_Plus' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve list of guests.
|
||||
*/
|
||||
function powerkit_get_guests() {
|
||||
|
||||
$list = wp_cache_get( 'powerkit_get_guests' );
|
||||
|
||||
if ( ! $list ) {
|
||||
|
||||
$list = array();
|
||||
|
||||
$query = new WP_Query();
|
||||
|
||||
$guests = $query->query( array(
|
||||
'post_type' => 'guest-author',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
) );
|
||||
|
||||
foreach ( $guests as $guest ) {
|
||||
array_push( $list, $guest );
|
||||
}
|
||||
|
||||
wp_cache_set( 'powerkit_get_guests', $list, 'powerkit', 1 );
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether a user is a guest.
|
||||
*
|
||||
* @param int $id The id of guest.
|
||||
*/
|
||||
function powerkit_is_guest( $id ) {
|
||||
if ( ! powerkit_coauthors_enabled() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! is_numeric( $id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$guests = powerkit_get_guests();
|
||||
|
||||
foreach ( $guests as $guest ) {
|
||||
if ( (int) $id === (int) $guest->ID ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve guest info by a given field.
|
||||
*
|
||||
* @param string $field The field of guest.
|
||||
* @param int $id The id of guest.
|
||||
*/
|
||||
function powerkit_get_guest_meta( $field, $id ) {
|
||||
$field = str_replace( array( 'user_url', 'url' ), 'website', $field );
|
||||
|
||||
return get_post_meta( $id, 'cap-' . $field, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve list of users and guests.
|
||||
*/
|
||||
function powerkit_get_users() {
|
||||
|
||||
$users = wp_cache_get( 'powerkit_get_users' );
|
||||
|
||||
if ( ! $users ) {
|
||||
$args = array(
|
||||
'orderby' => 'display_name',
|
||||
'capability' => array( 'edit_posts' ),
|
||||
);
|
||||
|
||||
// Capability queries were only introduced in WP 5.9.
|
||||
if ( version_compare( $GLOBALS['wp_version'], '5.9', '<' ) ) {
|
||||
$args['who'] = 'authors';
|
||||
unset( $args['capability'] );
|
||||
}
|
||||
|
||||
$users = get_users( apply_filters( 'powerkit_get_users_args', $args ) );
|
||||
|
||||
if ( powerkit_coauthors_enabled() ) {
|
||||
|
||||
$guests = powerkit_get_guests();
|
||||
|
||||
foreach ( $guests as $guest ) {
|
||||
$guest->first_name = powerkit_get_guest_meta( 'first_name', $guest->ID );
|
||||
$guest->last_name = powerkit_get_guest_meta( 'last_name', $guest->ID );
|
||||
$guest->user_email = powerkit_get_guest_meta( 'user_email', $guest->ID );
|
||||
$guest->website = powerkit_get_guest_meta( 'website', $guest->ID );
|
||||
|
||||
$user = (object) array(
|
||||
'ID' => $guest->ID,
|
||||
'display_name' => $guest->post_title,
|
||||
'user_registered' => $guest->post_date,
|
||||
'user_login' => $guest->first_name,
|
||||
'user_nicename' => $guest->last_name,
|
||||
'user_email' => $guest->user_email,
|
||||
'user_url' => $guest->website,
|
||||
'user_pass' => '',
|
||||
'user_activation_key' => '',
|
||||
'user_status' => 0,
|
||||
);
|
||||
|
||||
array_push( $users, $user );
|
||||
}
|
||||
}
|
||||
|
||||
wp_cache_set( 'powerkit_get_users', $users, 'powerkit', 1 );
|
||||
}
|
||||
|
||||
usort( $users, function( $a, $b ) {
|
||||
if ( function_exists( 'mb_strtolower' ) ) {
|
||||
return strcmp( mb_strtolower( $a->display_name ), mb_strtolower( $b->display_name ) );
|
||||
} else {
|
||||
return strcmp( strtolower( $a->display_name ), strtolower( $b->display_name ) );
|
||||
}
|
||||
} );
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the user's belonging to the post.
|
||||
*
|
||||
* @param int $author_id The ID of author.
|
||||
* @param int $post_id The ID of post.
|
||||
*/
|
||||
function powerkit_check_post_author( $author_id, $post_id ) {
|
||||
|
||||
if ( (string) get_post_field( 'post_author', $post_id ) === (string) $author_id ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( powerkit_coauthors_enabled() ) {
|
||||
$coauthors = (array) get_coauthors();
|
||||
|
||||
foreach ( $coauthors as $coauthor ) {
|
||||
if ( isset( $coauthor->ID ) && (string) $coauthor->ID === (string) $author_id ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user