Skip to content

Commit 0ed1072

Browse files
committed
Provide a block theme for the Friends page
1 parent 0cf9416 commit 0ed1072

File tree

8 files changed

+159
-1
lines changed

8 files changed

+159
-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

+59
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
}
@@ -284,11 +288,61 @@ public function parse_query( $query ) {
284288
public function add_body_class( $classes ) {
285289
if ( $this->friends->on_frontend() ) {
286290
$classes[] = 'friends-page';
291+
$classes[] = 'off-canvas';
292+
$classes[] = 'off-canvas-sidebar-show';
287293
}
288294

289295
return $classes;
290296
}
291297

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

858+
if ( wp_is_block_theme() ) {
859+
return $template;
860+
}
804861
if ( isset( $_GET['refresh'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
805862
add_filter( 'notify_about_new_friend_post', '__return_false', 999 );
806863
add_filter(
@@ -1283,6 +1340,8 @@ public function friend_posts_query( $query ) {
12831340
$query->is_friends_page = true;
12841341
$query->is_singular = false;
12851342
$query->is_single = false;
1343+
$query->is_category = false;
1344+
$query->is_archive = false;
12861345
$query->queried_object = null;
12871346
$query->queried_object_id = null;
12881347
$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)