Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRY: Register a block template for a new /rsvp endpoint #73

Draft
wants to merge 1 commit into
base: fix/704
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gatherpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Author URI: https://gatherpress.org/
* Version: 0.31.0-alpha
* Requires PHP: 7.4
* Requires Plugins: gutenberg
* Requires at least: 6.4
* Text Domain: gatherpress
* Domain Path: /languages
Expand Down
66 changes: 66 additions & 0 deletions includes/core/classes/class-rsvp-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use GatherPress\Core\Endpoints\Endpoint_Template;
use GatherPress\Core\Endpoints\Posttype_Single_Endpoint;
use GatherPress\Core\Traits\Singleton;

/**
Expand Down Expand Up @@ -52,7 +54,16 @@ protected function __construct() {
*/
protected function setup_hooks(): void {
add_action( 'init', array( $this, 'register_taxonomy' ) );
add_action( 'init', array( $this, 'register_block_template' ) );
add_filter( 'get_comments_number', array( $this, 'adjust_comments_number' ), 10, 2 );
add_action(
sprintf(
'registered_post_type_%s',
'gatherpress_event'
),
array( $this, 'setup_endpoint' ),
);
add_filter( 'show_admin_bar', '__return_false' ); // @todo DEBUG DEMO ONLY !!!
}

/**
Expand Down Expand Up @@ -100,4 +111,59 @@ public function adjust_comments_number( int $comments_number, int $post_id ): in

return $comment_count['approved'] ?? 0;
}

public function register_block_template(): void {
// the namespace is not allowed to contain "_" as it is validated against: '/^[a-z0-9-]+\/\/[a-z0-9-]+$/'
// @see /wp-content/plugins/gutenberg/lib/compat/wordpress-6.7/class-wp-block-templates-registry.php lines 53ff
// maybe open an issue about that missmatching error message
\wp_register_block_template( 'gatherpress//rsvp', [
'title' => __( 'RSVP', 'gatherpress' ),
'description' => __( 'A RSVP block template .', 'gatherpress' ),
'content' => self::get_template_content( 'endpoints/rsvp.php' ),
] );
}

public static function get_template_content( $template ) {
ob_start();
// include __DIR__ . "/templates/{$template}";
include sprintf(
'%s/includes/templates/%s',
GATHERPRESS_CORE_PATH,
$template
);
return ob_get_clean();
}

public function setup_endpoint(): void {

new Posttype_Single_Endpoint(
array(
new Endpoint_Template( 'rsvp', array( $this, 'get_rsvp_template' ) ),
),
'rsvp'
);
}


/**
* Returns the template for the RSVP.
*
* This method provides the template file to be used for ....
*
* By adding a file with the same name to your themes root folder
* or your themes `/templates` folder, this template will be used
* with priority over the default template provided by GatherPress.
*
* @since 1.0.0
*
* @return array An array containing:
* - 'file_name': the file name of the template to be loaded from the theme. Will load defaults from the plugin if theme files do not exist.
* - 'dir_path': (Optional) Absolute path to some template directory outside of the theme folder.
*/
public function get_rsvp_template(): array {
return array(
// 'file_name' => Utility::prefix_key( 'rsvp.php' ),
'file_name' => 'rsvp.php',
);
}
}
29 changes: 29 additions & 0 deletions includes/templates/endpoints/rsvp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Template for GatherPress RSVPs
*
* This template is used to render ... to the browser.
*
* @package GatherPress\Core
* @since 1.0.0
*/

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

?>

<?php if ( ! is_user_logged_in() && ! is_admin() ) : ?>
<!-- wp:paragraph -->
<p>You must Login to RSVP to events.</p>
<!-- /wp:paragraph -->

<!-- wp:loginout /-->
<?php endif; ?>

<?php if ( is_user_logged_in() ) : ?>
<!-- wp:paragraph -->
<p>Fortunately the RSVP block knowes about the queried event, here on the endpoint.</p>
<!-- /wp:paragraph -->
<!-- wp:gatherpress/rsvp /-->
<?php endif; ?>
Loading