__( 'Displays a live stream of posts and comments.', 'p2020' ) ) ); // If the widget is not active, no need to do any enqueues if ( is_active_widget( false, false, self::WIDGET_ID_BASE, true ) ) { add_action( 'o2_templates', array( $this, 'activity_item_templates' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_scripts' ) ); } } public function activity_item_templates() { /* translators: %1$s is replaced by the author's display name */ $untitled_post_title = sprintf( __( 'Post by %1$s', 'p2020' ), '{{ data.author.displayName }}' ); /* translators: %1$s is replaced by the title of the post that the comment was made on */ $comment_title = sprintf( __( 'Comment on %1$s', 'p2020' ), '{{ data.title }}' ); /* translators: %1$s is replaced by the author's display name */ $untitled_comment_title = sprintf( __( 'Comment by %1$s', 'p2020' ), '{{ data.author.displayName }}' ); ?> query_vars, home_url( $wp->request ) ); $customizer_url = add_query_arg( 'return', rawurlencode( $current_url ), P2_Core\Navigation\Urls::get_customizer_url() ); $tags_menu = new \Automattic\P2\Themes\P2020\EllipsisMenu(); $tags_menu->add_item( __( 'Widget settings', 'p2020' ), esc_url( $customizer_url ) ); $widget_title .= $tags_menu->generate(); } // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable $widget_title .= $after_title; // phpcs:ignore WordPress.Security.EscapeOutput -- HTML from theme echo $widget_title; } $kind = esc_attr( $kind ); $number = esc_attr( $number ); echo "
'; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable, WordPress.Security.EscapeOutput.OutputNotEscaped echo $after_widget; do_action( 'p2_widgets_stats', self::WIDGET_ID_BASE ); } public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = wp_strip_all_tags( $new_instance['title'] ); $instance['kind'] = wp_strip_all_tags( $new_instance['kind'] ); $instance['number'] = wp_strip_all_tags( $new_instance['number'] ); return $instance; } public function form( $instance ) { $defaults = array( 'title' => __( 'Recent Activity', 'p2020' ), 'kind' => 'both', 'number' => 10 ); $instance = wp_parse_args( (array) $instance, $defaults ); $title_id = esc_attr( $this->get_field_id( 'title' ) ); $title_name = esc_attr( $this->get_field_name( 'title' ) ); $title_value = esc_attr( $instance['title'] ); $kind_id = esc_attr( $this->get_field_id( 'kind' ) ); $kind_name = esc_attr( $this->get_field_name( 'kind' ) ); $kind_value = esc_attr( $instance['kind'] ); $number_id = esc_attr( $this->get_field_id( 'number' ) ); $number_name = esc_attr( $this->get_field_name( 'number' ) ); $number_value = esc_attr( $instance['number'] ); echo ''; echo "'; echo ""; echo '
'; echo '';
echo "';
echo '
';
$kinds = array(
'comment' => __( 'Comments', 'p2020' ),
'post' => __( 'Posts', 'p2020' ),
'both' => __( 'Comments and Posts', 'p2020' ),
);
foreach ( (array) $kinds as $name => $label ) {
$checked = checked( $name, $kind_value, false );
echo "';
echo esc_html( $label );
echo '';
echo '
';
}
echo '
'; echo "'; echo ""; echo '
'; } } function p2020_activity_widget_footer_bootstrap() { if ( ! is_active_widget( false, false, Activity_Widget::WIDGET_ID_BASE ) ) { return; } // embed in the footer the most recent N comments and M posts based // on the widget settings $comment_count = 0; $post_count = 0; $settings_array = get_option( 'widget_p2020-activity-widget' ); if ( is_array( $settings_array ) ) { foreach ( (array) $settings_array as $settings ) { if ( is_array( $settings ) ) { if ( isset( $settings['kind'] ) && isset( $settings['number'] ) ) { $kind = $settings['kind']; $count = intval( $settings['number'] ); if ( 0 > $count ) { $count = 0; } if ( 'both' === $kind ) { $comment_count = max( $comment_count, $count ); $post_count = max( $post_count, $count ); } elseif ( 'comment' === $kind ) { $comment_count = max( $comment_count, $count ); } elseif ( 'post' === $kind ) { $post_count = max( $post_count, $count ); } } } } } $activity_bootstrap = array(); // Bootstrap comments, as needed based on widget settings if ( 0 < $comment_count ) { $comments = get_comments( array( 'status' => 'approve', 'number' => $comment_count, ) ); // instead of using get_fragment, which is terribly verbose, we use lighterweight emitters here foreach ( (array) $comments as $comment ) { $comment_id = $comment->comment_ID; $comment_post_id = $comment->comment_post_ID; $title = html_entity_decode( get_the_title( $comment->comment_post_ID ) ); $comment_bootstrap = array( 'unixtime' => strtotime( $comment->comment_date_gmt ), 'title' => $title, 'domRef' => '#comment-' . $comment_id, 'permalink' => get_permalink( $comment_post_id ) . '#comment-' . $comment_id, 'type' => 'comment', 'externalID' => $comment_id, 'postID' => $comment->comment_post_ID, ); $commentor = \o2_Fragment::get_comment_author_properties( $comment ); $activity_bootstrap[] = array_merge( $comment_bootstrap, $commentor ); } } // Bootstrap posts, as needed based on widget settings if ( 0 < $post_count ) { $posts = get_posts( array( 'post_status' => 'publish', 'numberposts' => $post_count, ) ); foreach ( (array) $posts as $post ) { $post_ID = $post->ID; $title = html_entity_decode( $post->post_title ); $post_bootstrap = array( 'unixtime' => strtotime( $post->post_date_gmt ), 'title' => $title, 'domRef' => '#post-' . $post_ID, 'permalink' => get_permalink( $post_ID ), 'type' => 'post', 'externalID' => $post_ID, ); $poster = \o2_Fragment::get_post_user_properties( $post ); $activity_bootstrap[] = array_merge( $post_bootstrap, $poster ); } } echo "\n"; // split this and the widgets init out into a separate class. maybe rename the containing directory to simply activity-comments } add_action( 'wp_footer', __NAMESPACE__ . '\p2020_activity_widget_footer_bootstrap' );