Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a dashboard widget #346

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions friends-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,7 @@ span#friends_enable_retention_number_line {
width: .8rem;
z-index: 1;
}

#friends-dashboard-widget .author-avatar {
vertical-align: middle;
}
23 changes: 23 additions & 0 deletions friends-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,29 @@ jQuery( function ( $ ) {
}
} );

setTimeout( function () {
if ( $( '#friends-dashboard-widget' ).length ) {
$( '#friends-dashboard-widget' ).append( ' <i class="friends-loading"></i>' );
$.post(
friends.ajax_url,
{
_ajax_nonce: $( '#friends-dashboard-widget' ).data( 'nonce' ),
action: 'friends_dashboard',
},
function ( response ) {
if ( response.success ) {
$( '#friends-dashboard-widget' ).html( response.data );
} else {
$( '#friends-dashboard-widget i' )
.removeClass( 'friends-loading' )
.addClass( 'dashicons dashicons-warning' )
.prop( 'title', response.data );
}
}
);
}
}, 500 );

setTimeout( function () {
if ( $( '#fetch-feeds' ).length ) {
$( '#fetch-feeds' ).append( ' <i class="friends-loading"></i>' );
Expand Down
2 changes: 1 addition & 1 deletion friends.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function validate_feed_rules( $rules ) {
* @return false|string URL or false on failure.
*/
function check_url( $url ) {
return check_url( $url );
return Friends::check_url( $url );
}

// Integrations.
Expand Down
47 changes: 47 additions & 0 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ private function register_hooks() {
add_action( 'remove_user_from_blog', array( $this, 'delete_user' ) );
add_action( 'tool_box', array( $this, 'toolbox_bookmarklets' ) );
add_action( 'dashboard_glance_items', array( $this, 'dashboard_glance_items' ) );
add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) );
add_action( 'wp_ajax_friends_dashboard', array( $this, 'ajax_friends_dashboard' ) );
add_filter( 'site_status_tests', array( $this, 'site_status_tests' ) );
add_filter( 'site_status_test_php_modules', array( $this, 'site_status_test_php_modules' ) );
add_filter( 'debug_information', array( $this, 'site_health_debug' ) );
Expand Down Expand Up @@ -3047,6 +3049,51 @@ public function dashboard_glance_items( $items ) {
return $items;
}

public function add_dashboard_widget() {
wp_add_dashboard_widget( 'friends_dashboard_widget', __( 'Friends: Latest Posts', 'friends' ), array( $this, 'render_dashboard_widget' ), null, null, 'column3' );
}

public function render_dashboard_widget() {
?>
<div id="friends-dashboard-widget" data-nonce="<?php echo esc_attr( wp_create_nonce( 'friends-dashboard' ) ); ?>">
</div>
<?php
}

public function ajax_friends_dashboard() {
check_ajax_referer( 'friends-dashboard' );

$any_friends = User_Query::all_associated_users();

ob_start();
if ( 0 === $any_friends->get_total() ) {
Friends::template_loader()->get_template_part(
'admin/dashboard-widget-welcome',
null,
array()
);

} else {
Friends::template_loader()->get_template_part(
'admin/dashboard-widget',
null,
array(
'posts' => get_posts(
array(
'post_type' => apply_filters( 'friends_frontend_post_types', array( 'post' ) ),
)
),
)
);
}
$data = ob_get_contents();
ob_end_clean();

wp_send_json_success(
$data
);
}

public function site_status_tests( $tests ) {
$tests['direct']['friends-roles'] = array(
'label' => __( 'Friend roles were created', 'friends' ),
Expand Down
6 changes: 5 additions & 1 deletion includes/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,10 @@ function ( $feed ) {
'post_format' => $this->post_format,
);

if ( isset( $_GET['welcome'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$args['show_welcome'] = true;
}

return Friends::template_loader()->get_template_part( 'frontend/index', $this->post_format, $args, false );
}

Expand Down Expand Up @@ -1159,7 +1163,7 @@ public function friend_posts_query( $query ) {
}

$page_id = get_query_var( 'page' );
// phpcs:disable WordPress.Security.NonceVerification.Recommended
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['share'] ) ) {
$share_hash = hash( 'crc32b', apply_filters( 'friends_share_salt', wp_salt( 'nonce' ), $page_id ) . $page_id );
if ( $_GET['share'] === $share_hash ) {
Expand Down
13 changes: 13 additions & 0 deletions templates/admin/dashboard-widget-welcome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* This template contains the Friends Dashboard Widget Welcome.
*
* @package Friends
*/

?>
<p><?php esc_html_e( "In this widget, you'd usually see your latest friends' posts. But since you haven't added any friends yet, these are your options:", 'friends' ); ?></p>
<ul>
<li><a href="<?php echo esc_url( home_url( '/friends/?welcome' ) ); ?>"><span class="ab-icon dashicons dashicons-groups"></span> <?php esc_html_e( 'Check out your Friends page to learn more', 'friends' ); ?></a></li>
<li><a href="<?php echo esc_url( self_admin_url( 'admin.php?page=add-friend' ) ); ?>"><span class="ab-icon dashicons dashicons-businessperson"></span> <?php esc_html_e( 'Add a new friend right away', 'friends' ); ?></a></li>
</ul>
47 changes: 47 additions & 0 deletions templates/admin/dashboard-widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* This template contains the Friends Dashboard Widget.
*
* @package Friends
*/

namespace Friends;

?><ul>
<?php
foreach ( $args['posts'] as $_post ) :
$friend_user = User::get_post_author( $_post );
$author_name = $args['friend_user']->display_name;
$override_author_name = apply_filters( 'friends_override_author_name', '', $author_name, $_post->ID );
$avatar = $friend_user->get_avatar_url();
if ( ! $avatar ) {
$avatar = get_avatar_url( $friend_user->ID );
}
$avatar = apply_filters( 'friends_author_avatar_url', $avatar, $friend_user, $_post->ID );

?>
<li>
<a href="<?php echo esc_attr( $friend_user->get_local_friends_page_url() ); ?>" class="author-avatar">
<img src="<?php echo esc_url( $avatar ); ?>" width="16" height="16" />
</a>
<a href="<?php echo esc_url( $friend_user->get_local_friends_page_url() ); ?>">
<strong><?php echo esc_html( $friend_user->display_name ); ?></strong>
<?php if ( $override_author_name && trim( str_replace( $override_author_name, '', $author_name ) ) === $author_name ) : ?>
– <?php echo esc_html( $override_author_name ); ?>
<?php endif; ?>
</a>:
<a href="<?php echo esc_url( $friend_user->get_local_friends_page_url( $_post->ID ) ); ?>">
<?php
if ( $_post->post_title ) {
echo esc_html( $_post->post_title );
} else {
echo esc_html( wp_trim_words( get_the_excerpt( $_post ), 18 ) );
}
?>
</a>
<a href="<?php echo esc_url( $_post->guid ); ?>" class="dashicons dashicons-external">
</a>
</li>
<?php endforeach; ?>
</ul>
<a href="<?php echo esc_url( home_url( '/friends/' ) ); ?>"><?php esc_html_e( 'Go to your friends page for all posts', 'friends' ); ?></a>
2 changes: 1 addition & 1 deletion templates/admin/settings-header.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}
if ( is_array( $_page ) ) {
$query = $_page;
$_page = $args['page'];
$_page = $_page['page'];
} else {
$query = array(
'page' => $_page,
Expand Down
8 changes: 5 additions & 3 deletions templates/frontend/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
$args
);

$show_welcome = isset( $args['show_welcome'] ) && $args['show_welcome'];

?>
<section class="posts columns <?php echo 'collapsed' === $args['frontend_default_view'] ? ' all-collapsed' : ''; ?>">
<?php
if ( ! have_posts() ) {
if ( $show_welcome || ! have_posts() ) {
?>
<div class="card columns col-12">
<div class="card-body">
<?php
if ( $args['friends']->frontend->post_format ) {
if ( ! $show_welcome && $args['friends']->frontend->post_format ) {
$post_formats = get_post_format_strings();

if ( $args['friend_user'] ) {
Expand Down Expand Up @@ -49,7 +51,7 @@
}
} else {
$any_friends = Friends\User_Query::all_associated_users();
if ( $any_friends->get_total() > 0 ) {
if ( ! $show_welcome && $any_friends->get_total() > 0 ) {
Friends\Friends::template_loader()->get_template_part( 'frontend/no-posts', $args['post_format'], $args );
} else {
Friends\Friends::template_loader()->get_template_part( 'frontend/no-friends', $args['post_format'], $args );
Expand Down
4 changes: 2 additions & 2 deletions templates/frontend/parts/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
* }, 10, 3 );
* ```
*/
$avatar = apply_filters( 'friends_author_avatar_url', $avatar, $friend_user, get_the_id() );
+ 45
?><header class="entry-header card-header columns">
<div class="avatar col-auto mr-2 translator-exclude">
<?php if ( in_array( get_post_type(), apply_filters( 'friends_frontend_post_types', array() ), true ) ) : ?>
<a href="<?php echo esc_attr( $friend_user->get_local_friends_page_url() ); ?>" class="author-avatar">
<?php echo get_avatar( $args['friend_user']->user_login, 36 ); ?>
<?php echo get_avatar( $args['friend_user']->ID, 36 ); ?>
</a>
<?php else : ?>
<a href="<?php echo esc_url( get_the_author_meta( 'url' ) ); ?>" class="author-avatar">
Expand Down
Loading