diff --git a/includes/class-friends.php b/includes/class-friends.php index 28761942..4b6b387e 100644 --- a/includes/class-friends.php +++ b/includes/class-friends.php @@ -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; } /** diff --git a/includes/class-frontend.php b/includes/class-frontend.php index ec0da2ab..0c9c82ec 100644 --- a/includes/class-frontend.php +++ b/includes/class-frontend.php @@ -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' ) ); @@ -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 ); @@ -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( '%3$s', $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( '