Skip to content

Commit

Permalink
Provide a block theme for the Friends page
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Nov 19, 2024
1 parent 6d6ef66 commit c384a15
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 1 deletion.
31 changes: 30 additions & 1 deletion includes/class-friends.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,36 @@ public static function on_frontend() {
}

$pagename_parts = explode( '/', trim( $pagename, '/' ) );
return count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[0];
if ( count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[0] ) {
return true;
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( implode( '/', array_slice( $pagename_parts, 0, 2 ) ) === 'wp-admin/customize.php' && isset( $_REQUEST['url'] ) ) {
$pagename_parts = explode( '/', trim( str_replace( '://', '', wp_unslash( $_REQUEST['url'] ) ), '/' ) );
if ( count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[1] ) {
return true;
}
}

if ( implode( '/', array_slice( $pagename_parts, 0, 2 ) ) === 'wp-admin/site-editor.php' && isset( $_REQUEST['postId'] ) ) {
$pagename_parts = explode( '//', wp_unslash( $_REQUEST['postId'] ) );
if ( count( $pagename_parts ) > 0 && 'friends' === $pagename_parts[0] ) {
return true;
}
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

if ( implode( '/', array_slice( $pagename_parts, 0, 5 ) ) === 'wp-json/wp/v2/templates/friends' ) {
return true;
}

if ( implode( '/', array_slice( $pagename_parts, 0, 5 ) ) === 'wp-json/wp/v2/template-parts/friends' ) {
return true;
}

return false;
}

/**
Expand Down
60 changes: 60 additions & 0 deletions includes/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public function __construct( Friends $friends ) {
* Register the WordPress hooks
*/
private function register_hooks() {
register_theme_directory( __DIR__ . '/../themes' );

add_filter( 'pre_get_posts', array( $this, 'friend_posts_query' ), 2 );
add_filter( 'post_type_link', array( $this, 'friend_post_link' ), 10, 2 );
add_filter( 'friends_header_widget_title', array( $this, 'header_widget_title' ) );
Expand All @@ -100,6 +102,8 @@ private function register_hooks() {
add_action( 'the_post', array( $this, 'the_post' ), 10, 2 );
add_action( 'parse_query', array( $this, 'parse_query' ) );
add_filter( 'body_class', array( $this, 'add_body_class' ) );
add_filter( 'stylesheet', array( $this, 'stylesheet' ) );
add_filter( 'block_type_metadata_settings', array( $this, 'block_type_metadata_settings' ), 15 );

add_filter( 'friends_override_author_name', array( $this, 'override_author_name' ), 10, 3 );
add_filter( 'friends_friend_posts_query_viewable', array( $this, 'expose_opml' ), 10, 2 );
Expand Down Expand Up @@ -291,11 +295,61 @@ public function parse_query( $query ) {
public function add_body_class( $classes ) {
if ( $this->friends->on_frontend() ) {
$classes[] = 'friends-page';
$classes[] = 'off-canvas';
$classes[] = 'off-canvas-sidebar-show';
}

return $classes;
}

public function stylesheet( $stylesheet ) {
if ( ! Friends::on_frontend() ) {
return $stylesheet;
}

return 'friends';
}

public function block_type_metadata_settings( $settings ) {
if ( ! Friends::on_frontend() || ! isset( $settings['name'] ) ) {
return $settings;
}
if ( 'core/post-author-name' === $settings['name'] ) {
$settings['render_callback'] = function ( $attributes, $content, $block ) {
if ( isset( $block->context['postId'] ) ) {
$author = User::get_post_author( get_post( $block->context['postId'] ) );
} else {
return '';
}
if ( empty( $author ) ) {
return '';
}

$author_name = $author->display_name;
$override_author_name = apply_filters( 'friends_override_author_name', '', $author_name, $block->context['postId'] );
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
$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 );
}

if ( $override_author_name && trim( str_replace( $override_author_name, '', $author_name ) ) === $author_name ) {
$author_name .= '' . esc_html( $override_author_name );
}

$classes = array();
if ( isset( $attributes['textAlign'] ) ) {
$classes[] = 'has-text-align-' . $attributes['textAlign'];
}
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
$classes[] = 'has-link-color';
}
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );

return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $author_name );
};
}
return $settings;
}

/**
* Gets the minimal query variables.
*
Expand Down Expand Up @@ -813,6 +867,10 @@ public function template_override( $template ) {
return Friends::template_loader()->get_template_part( $this->template, null, $args, false );
}

if ( wp_is_block_theme() ) {
return $template;
}

Check failure on line 873 in includes/class-frontend.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Whitespace found at end of line

Check failure on line 873 in includes/class-frontend.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Whitespace found at end of line
$args['frontend_default_view'] = get_option( 'friends_frontend_default_view', 'expanded' );
$args['blocks-everywhere'] = false;

Expand Down Expand Up @@ -1300,6 +1358,8 @@ public function friend_posts_query( $query ) {
$query->is_friends_page = true;
$query->is_singular = false;
$query->is_single = false;
$query->is_category = false;
$query->is_archive = false;
$query->queried_object = null;
$query->queried_object_id = null;
$post_types = apply_filters( 'friends_frontend_post_types', array() );
Expand Down
1 change: 1 addition & 0 deletions themes/friends/parts/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
footer
1 change: 1 addition & 0 deletions themes/friends/parts/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-author-name {"fontSize":"large"} /-->
9 changes: 9 additions & 0 deletions themes/friends/parts/sidebar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading"><a href="/friends/">Friends</a></h1>
<!-- /wp:heading -->

<!-- wp:legacy-widget {"idBase":"friends-widget-stats", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->
<!-- wp:legacy-widget {"idBase":"friends-widget-refresh", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->
<!-- wp:legacy-widget {"idBase":"friends-widget-post-formats", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->
<!-- wp:legacy-widget {"idBase":"friends-widget-friend-list", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->
<!-- wp:legacy-widget {"idBase":"friends-widget-friend-request", "instance": {"encoded":"YTowOnt9", "hash":"992105660ae8de77cb5a8a24bf061d0f"}} /-->
11 changes: 11 additions & 0 deletions themes/friends/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*!
* Theme Name: Friends
* Theme URI: https://wordpress.org/plugins/friends/
* Author: akirk
* Author URI: https://alex.kirk.at
* Description: Theme for the Friends Plugin.
* Version: 1.0
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: friends
*/
17 changes: 17 additions & 0 deletions themes/friends/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- wp:columns {"style":{"spacing":{"padding":{"top":"40px"}}}} -->
<div class="wp-block-columns" style="padding-top:40px"><!-- wp:column {"width":"33.33%"} -->
<div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:template-part {"slug":"sidebar","theme":"friends","tagName":"sidebar","area":"sidebar"} /--></div>
<!-- /wp:column -->

<!-- wp:column {"width":"66.66%"} -->
<div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:template-part {"slug":"header","theme":"friends","tagName":"header","area":"header"} /-->

<!-- 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"}} -->
<div class="wp-block-query alignwide posts columns"><!-- wp:post-template {"className":"translator-exclude"} -->
<!-- wp:post-author-name /--> <!-- wp:post-title {"isLink":true}} /-->
<!-- /wp:post-template --></div>
<!-- /wp:query -->

<!-- wp:template-part {"slug":"footer","theme":"friends","tagName":"footer","area":"footer"} /--></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->
31 changes: 31 additions & 0 deletions themes/friends/theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 3,
"settings": {
"appearanceTools": true
},
"styles": {
"color": {
"background": "var(--wp--preset--color--base)",
"text": "var(--wp--preset--color--contrast)"
}

},
"templateParts": [
{
"area": "header",
"name": "header",
"title": "Header"
},
{
"area": "footer",
"name": "footer",
"title": "Footer"
},
{
"area": "uncategorized",
"name": "sidebar",
"title": "Sidebar"
}
]
}

0 comments on commit c384a15

Please sign in to comment.