| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* UsersWP user title widget. |
| 8 |
* |
| 9 |
* @since 1.1.2 |
| 10 |
*/ |
| 11 |
class UWP_User_Title_Widget extends WP_Super_Duper { |
| 12 |
|
| 13 |
/** |
| 14 |
* Register the user title widget with WordPress. |
| 15 |
* |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
|
| 19 |
|
| 20 |
$options = array( |
| 21 |
'textdomain' => 'userswp', |
| 22 |
'block-icon' => 'admin-site', |
| 23 |
'block-category' => 'widgets', |
| 24 |
'block-keywords' => "['userswp','user']", |
| 25 |
'class_name' => __CLASS__, |
| 26 |
'base_id' => 'uwp_user_title', |
| 27 |
'name' => __( 'UWP > User Title', 'userswp' ), |
| 28 |
'no_wrap' => true, |
| 29 |
'widget_ops' => array( |
| 30 |
'classname' => 'uwp-user-title', |
| 31 |
'description' => esc_html__( 'Displays user name.', 'userswp' ), |
| 32 |
), |
| 33 |
'arguments' => array( |
| 34 |
'tag' => array( |
| 35 |
'title' => __( 'Header Tag:', 'userswp' ), |
| 36 |
'desc' => __( 'Header tag for the user title.', 'userswp' ), |
| 37 |
'type' => 'select', |
| 38 |
'options' => array( |
| 39 |
"h1" => "h1", |
| 40 |
"h2" => "h2", |
| 41 |
"h3" => "h3", |
| 42 |
"h4" => "h4", |
| 43 |
"h5" => "h5", |
| 44 |
"h6" => "h6", |
| 45 |
), |
| 46 |
'default' => 'h2', |
| 47 |
'desc_tip' => true, |
| 48 |
), |
| 49 |
'user_id' => array( |
| 50 |
'title' => __( 'User ID:', 'userswp' ), |
| 51 |
'desc' => __( 'Leave blank to use current user ID or use post_author for current post author ID. For profile page it will take displayed user ID. Input specific user ID for other pages.', 'userswp' ), |
| 52 |
'type' => 'text', |
| 53 |
'desc_tip' => true, |
| 54 |
'default' => '', |
| 55 |
'advanced' => true |
| 56 |
), |
| 57 |
) |
| 58 |
|
| 59 |
); |
| 60 |
|
| 61 |
|
| 62 |
parent::__construct( $options ); |
| 63 |
} |
| 64 |
|
| 65 |
public function output( $args = array(), $widget_args = array(), $content = '' ) { |
| 66 |
|
| 67 |
global $post; |
| 68 |
|
| 69 |
$defaults = array( |
| 70 |
'tag' => 'h2', |
| 71 |
); |
| 72 |
|
| 73 |
$args = wp_parse_args( $args, $defaults ); |
| 74 |
|
| 75 |
$args = apply_filters( 'uwp_widget_user_title_args', $args, $widget_args, $this ); |
| 76 |
|
| 77 |
$allowed_tags = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ); |
| 78 |
$tag = in_array( $args['tag'], $allowed_tags ) ? esc_attr( $args['tag'] ) : 'h2'; |
| 79 |
$title_tag = empty( $args['tag'] ) ? 'h2' : apply_filters( 'uwp_widget_user_title_tag', $tag, $args, $widget_args, $this ); |
| 80 |
|
| 81 |
if ( 'post_author' == $args['user_id'] && $post instanceof WP_Post ) { |
| 82 |
$user = get_userdata( $post->post_author ); |
| 83 |
$args['user_id'] = $post->post_author; |
| 84 |
} else if ( isset( $args['user_id'] ) && (int) $args['user_id'] > 0 ) { |
| 85 |
$user = get_userdata( (int) $args['user_id'] ); |
| 86 |
} else { |
| 87 |
$user = uwp_get_displayed_user(); |
| 88 |
} |
| 89 |
|
| 90 |
if ( empty( $args['user_id'] ) && ! empty( $user->ID ) ) { |
| 91 |
$args['user_id'] = $user->ID; |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! $user ) { |
| 95 |
return ''; |
| 96 |
} |
| 97 |
|
| 98 |
ob_start(); |
| 99 |
|
| 100 |
do_action( 'uwp_user_title', $user, $title_tag ); |
| 101 |
|
| 102 |
return ob_get_clean(); |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
} |