| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* UsersWP user post counts widget. |
| 8 |
*/ |
| 9 |
class UWP_User_Post_Counts_Widget extends WP_Super_Duper { |
| 10 |
|
| 11 |
/** |
| 12 |
* Register the user post counts widget with WordPress. |
| 13 |
* |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
|
| 17 |
|
| 18 |
$options = array( |
| 19 |
'textdomain' => 'userswp', |
| 20 |
'block-icon' => 'admin-site', |
| 21 |
'block-category' => 'widgets', |
| 22 |
'block-keywords' => "['userswp','user']", |
| 23 |
'class_name' => __CLASS__, |
| 24 |
'base_id' => 'uwp_user_post_counts', |
| 25 |
'name' => __( 'UWP > User Post Counts', 'userswp' ), |
| 26 |
'no_wrap' => true, |
| 27 |
'block-wrap' => '', |
| 28 |
'widget_ops' => array( |
| 29 |
'classname' => 'uwp-user-post-counts bsui', |
| 30 |
'description' => esc_html__( 'Display user post, comments and other custom post type counts.', 'userswp' ), |
| 31 |
), |
| 32 |
'arguments' => array( |
| 33 |
'title' => array( |
| 34 |
'title' => __( 'Title', 'userswp' ), |
| 35 |
'desc' => __( 'Enter widget title.', 'userswp' ), |
| 36 |
'type' => 'text', |
| 37 |
'desc_tip' => true, |
| 38 |
'default' => '', |
| 39 |
'advanced' => false |
| 40 |
), |
| 41 |
'disable_greedy' => array( |
| 42 |
'title' => __('Disable Greedy Menu', 'userswp'), |
| 43 |
'desc' => __('Greedy menu prevents a large menu falling onto another line by adding a dropdown select.', 'userswp'), |
| 44 |
'type' => 'checkbox', |
| 45 |
'desc_tip' => true, |
| 46 |
'value' => '1', |
| 47 |
'default' => '', |
| 48 |
'advanced' => true, |
| 49 |
), |
| 50 |
) |
| 51 |
|
| 52 |
); |
| 53 |
|
| 54 |
|
| 55 |
parent::__construct( $options ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* The Super block output function. |
| 60 |
* |
| 61 |
* @param array $args |
| 62 |
* @param array $widget_args |
| 63 |
* @param string $content |
| 64 |
* |
| 65 |
* @return mixed|string|bool |
| 66 |
*/ |
| 67 |
public function output( $args = array(), $widget_args = array(), $content = '' ) { |
| 68 |
global $_uwp_user_post_counts; |
| 69 |
|
| 70 |
$user = uwp_get_displayed_user(); |
| 71 |
|
| 72 |
if ( ! $user ) { |
| 73 |
return ''; |
| 74 |
} |
| 75 |
|
| 76 |
ob_start(); |
| 77 |
|
| 78 |
$output = ""; |
| 79 |
$counts = array(); |
| 80 |
$user_id = $user->ID; |
| 81 |
|
| 82 |
if ( is_user_logged_in() && $user_id == get_current_user_id() ) { |
| 83 |
|
| 84 |
$post_types = uwp_get_option( 'login_user_post_counts_cpts' ); |
| 85 |
$post_types = apply_filters( 'uwp_login_user_count_cpts', $post_types, $user_id ); |
| 86 |
|
| 87 |
} else { |
| 88 |
|
| 89 |
$post_types = uwp_get_option( 'user_post_counts_cpts' ); |
| 90 |
$post_types = apply_filters( 'uwp_user_count_cpts', $post_types, $user_id ); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
if ( ! empty( $post_types ) ) { |
| 95 |
$_uwp_user_post_counts = true; |
| 96 |
|
| 97 |
foreach ( $post_types as $cpt ) { |
| 98 |
$post_type = get_post_type_object( $cpt ); |
| 99 |
$count = count_user_posts( $user_id, $cpt ); |
| 100 |
|
| 101 |
if ( $post_type && $count ) { |
| 102 |
$counts[ $cpt ] = array( |
| 103 |
'name' => $post_type->labels->name, |
| 104 |
'singular_name' => $post_type->labels->singular_name, |
| 105 |
'count' => $count |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
$_uwp_user_post_counts = false; |
| 111 |
} |
| 112 |
|
| 113 |
$counts = apply_filters( 'uwp_get_user_post_counts', $counts, $user_id ); |
| 114 |
$greedy_menu_class = empty($args['disable_greedy']) ? 'greedy' : ''; |
| 115 |
|
| 116 |
if ( is_uwp_profile_page() ) { |
| 117 |
|
| 118 |
if ( ! empty( $counts ) ) { |
| 119 |
$output .= '<nav class="navbar navbar-expand-xl navbar-light bg-white p-xl-0 '.esc_attr($greedy_menu_class).'">'; |
| 120 |
$output .= '<div class="w-100 justify-content-center">'; |
| 121 |
$output .= '<ul class="navbar-nav flex-wrap m-0">'; |
| 122 |
$class = " pl-0"; |
| 123 |
foreach ( $counts as $cpt => $post_type ) { |
| 124 |
$post_count_text = $post_type['count'] > 1 ? esc_attr( $post_type['name'] ) . '<span class="badge badge-dark ml-1">' . esc_attr( $post_type['count'] ) . '</span>' : esc_attr( $post_type['singular_name'] ) . '<span class="badge badge-dark ml-1">' . esc_attr( $post_type['count'] ) . '</span>'; |
| 125 |
$output .= '<li class="nav-item"><span class="nav-link pr-0'.$class.'"><span class="badge badge-white text-muted'.$class.'">' . $post_count_text . '</span></span></li>' . " \n"; |
| 126 |
$class = ''; |
| 127 |
} |
| 128 |
$output .= '</ul>'; |
| 129 |
$output .= '</div>'; |
| 130 |
$output .= '</nav>'; |
| 131 |
} |
| 132 |
|
| 133 |
} else { |
| 134 |
if ( ! empty( $counts ) ) { |
| 135 |
$class = " pl-0"; |
| 136 |
foreach ( $counts as $cpt => $post_type ) { |
| 137 |
$post_count_text = $post_type['count'] > 1 ? esc_attr( $post_type['name'] ) . '<span class="badge badge-dark ml-1">' . esc_attr( $post_type['count'] ) . '</span>' : esc_attr( $post_type['singular_name'] ) . '<span class="badge badge-dark ml-1">' . esc_attr( $post_type['count'] ) . '</span>'; |
| 138 |
$output .= '<span class="badge badge-white nav-link text-muted pr-0'.$class.'">' . $post_count_text . '</span>' . " \n"; |
| 139 |
$class = ''; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 145 |
|
| 146 |
return ob_get_clean(); |
| 147 |
} |
| 148 |
} |