Skip to content

Commit c384a15

Browse files
committed
Provide a block theme for the Friends page
1 parent 6d6ef66 commit c384a15

File tree

8 files changed

+160
-1
lines changed

8 files changed

+160
-1
lines changed

includes/class-friends.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,36 @@ public static function on_frontend() {
797797
}
798798

799799
$pagename_parts = explode( '/', trim( $pagename, '/' ) );
800-
return count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[0];
800+
if ( count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[0] ) {
801+
return true;
802+
}
803+
// phpcs:disable WordPress.Security.NonceVerification.Recommended
804+
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
805+
if ( implode( '/', array_slice( $pagename_parts, 0, 2 ) ) === 'wp-admin/customize.php' && isset( $_REQUEST['url'] ) ) {
806+
$pagename_parts = explode( '/', trim( str_replace( '://', '', wp_unslash( $_REQUEST['url'] ) ), '/' ) );
807+
if ( count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[1] ) {
808+
return true;
809+
}
810+
}
811+
812+
if ( implode( '/', array_slice( $pagename_parts, 0, 2 ) ) === 'wp-admin/site-editor.php' && isset( $_REQUEST['postId'] ) ) {
813+
$pagename_parts = explode( '//', wp_unslash( $_REQUEST['postId'] ) );
814+
if ( count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[0] ) {
815+
return true;
816+
}
817+
}
818+
// phpcs:enable WordPress.Security.NonceVerification.Recommended
819+
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
820+
821+
if ( implode( '/', array_slice( $pagename_parts, 0, 5 ) ) === 'wp-json/wp/v2/templates/friends' ) {
822+
return true;
823+
}
824+
825+
if ( implode( '/', array_slice( $pagename_parts, 0, 5 ) ) === 'wp-json/wp/v2/template-parts/friends' ) {
826+
return true;
827+
}
828+
829+
return false;
801830
}
802831

803832
/**

includes/class-frontend.php

+60
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public function __construct( Friends $friends ) {
7474
* Register the WordPress hooks
7575
*/
7676
private function register_hooks() {
77+
register_theme_directory( __DIR__ . '/../themes' );
78+
7779
add_filter( 'pre_get_posts', array( $this, 'friend_posts_query' ), 2 );
7880
add_filter( 'post_type_link', array( $this, 'friend_post_link' ), 10, 2 );
7981
add_filter( 'friends_header_widget_title', array( $this, 'header_widget_title' ) );
@@ -100,6 +102,8 @@ private function register_hooks() {
100102
add_action( 'the_post', array( $this, 'the_post' ), 10, 2 );
101103
add_action( 'parse_query', array( $this, 'parse_query' ) );
102104
add_filter( 'body_class', array( $this, 'add_body_class' ) );
105+
add_filter( 'stylesheet', array( $this, 'stylesheet' ) );
106+
add_filter( 'block_type_metadata_settings', array( $this, 'block_type_metadata_settings' ), 15 );
103107

104108
add_filter( 'friends_override_author_name', array( $this, 'override_author_name' ), 10, 3 );
105109
add_filter( 'friends_friend_posts_query_viewable', array( $this, 'expose_opml' ), 10, 2 );
@@ -291,11 +295,61 @@ public function parse_query( $query ) {
291295
public function add_body_class( $classes ) {
292296
if ( $this->friends->on_frontend() ) {
293297
$classes[] = 'friends-page';
298+
$classes[] = 'off-canvas';
299+
$classes[] = 'off-canvas-sidebar-show';
294300
}
295301

296302
return $classes;
297303
}
298304

305+
public function stylesheet( $stylesheet ) {
306+
if ( ! Friends::on_frontend() ) {
307+
return $stylesheet;
308+
}
309+
310+
return 'friends';
311+
}
312+
313+
public function block_type_metadata_settings( $settings ) {
314+
if ( ! Friends::on_frontend() || ! isset( $settings['name'] ) ) {
315+
return $settings;
316+
}
317+
if ( 'core/post-author-name' === $settings['name'] ) {
318+
$settings['render_callback'] = function ( $attributes, $content, $block ) {
319+
if ( isset( $block->context['postId'] ) ) {
320+
$author = User::get_post_author( get_post( $block->context['postId'] ) );
321+
} else {
322+
return '';
323+
}
324+
if ( empty( $author ) ) {
325+
return '';
326+
}
327+
328+
$author_name = $author->display_name;
329+
$override_author_name = apply_filters( 'friends_override_author_name', '', $author_name, $block->context['postId'] );
330+
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
331+
$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 );
332+
}
333+
334+
if ( $override_author_name && trim( str_replace( $override_author_name, '', $author_name ) ) === $author_name ) {
335+
$author_name .= '' . esc_html( $override_author_name );
336+
}
337+
338+
$classes = array();
339+
if ( isset( $attributes['textAlign'] ) ) {
340+
$classes[] = 'has-text-align-' . $attributes['textAlign'];
341+
}
342+
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
343+
$classes[] = 'has-link-color';
344+
}
345+
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
346+
347+
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $author_name );
348+
};
349+
}
350+
return $settings;
351+
}
352+
299353
/**
300354
* Gets the minimal query variables.
301355
*
@@ -813,6 +867,10 @@ public function template_override( $template ) {
813867
return Friends::template_loader()->get_template_part( $this->template, null, $args, false );
814868
}
815869

870+
if ( wp_is_block_theme() ) {
871+
return $template;
872+
}
873+
816874
$args['frontend_default_view'] = get_option( 'friends_frontend_default_view', 'expanded' );
817875
$args['blocks-everywhere'] = false;
818876

@@ -1300,6 +1358,8 @@ public function friend_posts_query( $query ) {
13001358
$query->is_friends_page = true;
13011359
$query->is_singular = false;
13021360
$query->is_single = false;
1361+
$query->is_category = false;
1362+
$query->is_archive = false;
13031363
$query->queried_object = null;
13041364
$query->queried_object_id = null;
13051365
$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)