Skip to content

Commit 631f0c3

Browse files
committed
Provide a block theme for the Friends page
1 parent 199af50 commit 631f0c3

File tree

8 files changed

+161
-2
lines changed

8 files changed

+161
-2
lines changed

includes/class-friends.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,36 @@ public static function on_frontend() {
854854
}
855855

856856
$pagename_parts = explode( '/', trim( $pagename, '/' ) );
857-
return count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[0];
857+
if ( count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[0] ) {
858+
return true;
859+
}
860+
// phpcs:disable WordPress.Security.NonceVerification.Recommended
861+
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
862+
if ( implode( '/', array_slice( $pagename_parts, 0, 2 ) ) === 'wp-admin/customize.php' && isset( $_REQUEST['url'] ) ) {
863+
$pagename_parts = explode( '/', trim( str_replace( '://', '', wp_unslash( $_REQUEST['url'] ) ), '/' ) );
864+
if ( count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[1] ) {
865+
return true;
866+
}
867+
}
868+
869+
if ( implode( '/', array_slice( $pagename_parts, 0, 2 ) ) === 'wp-admin/site-editor.php' && isset( $_REQUEST['postId'] ) ) {
870+
$pagename_parts = explode( '//', wp_unslash( $_REQUEST['postId'] ) );
871+
if ( count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[0] ) {
872+
return true;
873+
}
874+
}
875+
// phpcs:enable WordPress.Security.NonceVerification.Recommended
876+
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
877+
878+
if ( implode( '/', array_slice( $pagename_parts, 0, 5 ) ) === 'wp-json/wp/v2/templates/friends' ) {
879+
return true;
880+
}
881+
882+
if ( implode( '/', array_slice( $pagename_parts, 0, 5 ) ) === 'wp-json/wp/v2/template-parts/friends' ) {
883+
return true;
884+
}
885+
886+
return false;
858887
}
859888

860889
/**

includes/class-frontend.php

+61-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function __construct( Friends $friends ) {
8787
* Register the WordPress hooks
8888
*/
8989
private function register_hooks() {
90+
register_theme_directory( __DIR__ . '/../themes' );
91+
9092
add_filter( 'pre_get_posts', array( $this, 'friend_posts_query' ), 2 );
9193
add_filter( 'post_type_link', array( $this, 'friend_post_link' ), 10, 2 );
9294
add_filter( 'friends_header_widget_title', array( $this, 'header_widget_title' ) );
@@ -118,6 +120,8 @@ private function register_hooks() {
118120
add_action( 'the_post', array( $this, 'the_post' ), 10, 2 );
119121
add_action( 'parse_query', array( $this, 'parse_query' ) );
120122
add_filter( 'body_class', array( $this, 'add_body_class' ) );
123+
add_filter( 'stylesheet', array( $this, 'stylesheet' ) );
124+
add_filter( 'block_type_metadata_settings', array( $this, 'block_type_metadata_settings' ), 15 );
121125

122126
add_filter( 'friends_override_author_name', array( $this, 'override_author_name' ), 10, 3 );
123127
add_filter( 'friends_friend_posts_query_viewable', array( $this, 'expose_opml' ), 10, 2 );
@@ -354,11 +358,61 @@ public function parse_query( $query ) {
354358
public function add_body_class( $classes ) {
355359
if ( $this->friends->on_frontend() ) {
356360
$classes[] = 'friends-page';
361+
$classes[] = 'off-canvas';
362+
$classes[] = 'off-canvas-sidebar-show';
357363
}
358364

359365
return $classes;
360366
}
361367

368+
public function stylesheet( $stylesheet ) {
369+
if ( ! Friends::on_frontend() ) {
370+
return $stylesheet;
371+
}
372+
373+
return 'friends';
374+
}
375+
376+
public function block_type_metadata_settings( $settings ) {
377+
if ( ! Friends::on_frontend() || ! isset( $settings['name'] ) ) {
378+
return $settings;
379+
}
380+
if ( 'core/post-author-name' === $settings['name'] ) {
381+
$settings['render_callback'] = function ( $attributes, $content, $block ) {
382+
if ( isset( $block->context['postId'] ) ) {
383+
$author = User::get_post_author( get_post( $block->context['postId'] ) );
384+
} else {
385+
return '';
386+
}
387+
if ( empty( $author ) ) {
388+
return '';
389+
}
390+
391+
$author_name = $author->display_name;
392+
$override_author_name = apply_filters( 'friends_override_author_name', '', $author_name, $block->context['postId'] );
393+
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
394+
$author_name = sprintf( '<a href="%1$s" target="%2$s" class="wp-block-post-author-name__link">%3$s</a>', $author->get_local_friends_page_url(), esc_attr( $attributes['linkTarget'] ), $author_name );
395+
}
396+
397+
if ( $override_author_name && trim( str_replace( $override_author_name, '', $author_name ) ) === $author_name ) {
398+
$author_name .= '' . esc_html( $override_author_name );
399+
}
400+
401+
$classes = array();
402+
if ( isset( $attributes['textAlign'] ) ) {
403+
$classes[] = 'has-text-align-' . $attributes['textAlign'];
404+
}
405+
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
406+
$classes[] = 'has-link-color';
407+
}
408+
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
409+
410+
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $author_name );
411+
};
412+
}
413+
return $settings;
414+
}
415+
362416
/**
363417
* Gets the minimal query variables.
364418
*
@@ -890,7 +944,11 @@ public function template_override( $template ) {
890944
return Friends::template_loader()->get_template_part( $this->template, null, $args, false );
891945
}
892946

893-
$args['frontend_default_view'] = get_user_option( 'friends_frontend_default_view', get_current_user_id() );
947+
if ( wp_is_block_theme() ) {
948+
return $template;
949+
}
950+
951+
$args['frontend_default_view'] = get_user_option( 'friends_frontend_default_view', 'expanded' );
894952
$args['blocks-everywhere'] = false;
895953

896954
if ( isset( $_GET['welcome'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -1436,6 +1494,8 @@ public function friend_posts_query( $query ) {
14361494
$query->is_friends_page = true;
14371495
$query->is_singular = false;
14381496
$query->is_single = false;
1497+
$query->is_category = false;
1498+
$query->is_archive = false;
14391499
$query->queried_object = null;
14401500
$query->queried_object_id = null;
14411501
$post_types = apply_filters( 'friends_frontend_post_types', array() );

themes/friends/parts/footer.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
footer

themes/friends/parts/header.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- wp:post-author-name {"fontSize":"large"} /-->

themes/friends/parts/sidebar.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- wp:heading {"level":1} -->
2+
<h1 class="wp-block-heading"><a href="/friends/">Friends</a></h1>
3+
<!-- /wp:heading -->
4+
5+
<!-- wp:legacy-widget {"idBase":"friends-widget-stats", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->
6+
<!-- wp:legacy-widget {"idBase":"friends-widget-refresh", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->
7+
<!-- wp:legacy-widget {"idBase":"friends-widget-post-formats", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->
8+
<!-- wp:legacy-widget {"idBase":"friends-widget-friend-list", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->
9+
<!-- wp:legacy-widget {"idBase":"friends-widget-friend-request", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->

themes/friends/style.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*!
2+
* Theme Name: Friends
3+
* Theme URI: https://wordpress.org/plugins/friends/
4+
* Author: akirk
5+
* Author URI: https://alex.kirk.at
6+
* Description: Theme for the Friends Plugin.
7+
* Version: 1.0
8+
* License: GNU General Public License v2 or later
9+
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
10+
* Text Domain: friends
11+
*/

themes/friends/templates/index.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- wp:columns {"style":{"spacing":{"padding":{"top":"40px"}}}} -->
2+
<div class="wp-block-columns" style="padding-top:40px"><!-- wp:column {"width":"33.33%"} -->
3+
<div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:template-part {"slug":"sidebar","theme":"friends","tagName":"sidebar","area":"sidebar"} /--></div>
4+
<!-- /wp:column -->
5+
6+
<!-- wp:column {"width":"66.66%"} -->
7+
<div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:template-part {"slug":"header","theme":"friends","tagName":"header","area":"header"} /-->
8+
9+
<!-- wp:query {"queryId":0,"query":{"perPage":10,"pages":0,"offset":"0","postType":"friend_post_cache","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true},"align":"wide","className":"posts columns","layout":{"type":"default"}} -->
10+
<div class="wp-block-query alignwide posts columns"><!-- wp:post-template {"className":"translator-exclude"} -->
11+
<!-- wp:post-author-name /--> <!-- wp:post-title {"isLink":true}} /-->
12+
<!-- /wp:post-template --></div>
13+
<!-- /wp:query -->
14+
15+
<!-- wp:template-part {"slug":"footer","theme":"friends","tagName":"footer","area":"footer"} /--></div>
16+
<!-- /wp:column --></div>
17+
<!-- /wp:columns -->

themes/friends/theme.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/theme.json",
3+
"version": 3,
4+
"settings": {
5+
"appearanceTools": true
6+
},
7+
"styles": {
8+
"color": {
9+
"background": "var(--wp--preset--color--base)",
10+
"text": "var(--wp--preset--color--contrast)"
11+
}
12+
13+
},
14+
"templateParts": [
15+
{
16+
"area": "header",
17+
"name": "header",
18+
"title": "Header"
19+
},
20+
{
21+
"area": "footer",
22+
"name": "footer",
23+
"title": "Footer"
24+
},
25+
{
26+
"area": "uncategorized",
27+
"name": "sidebar",
28+
"title": "Sidebar"
29+
}
30+
]
31+
}

0 commit comments

Comments
 (0)