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

feat(correction-blocks): Correction box & Loop item + Template #3787

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
7 changes: 7 additions & 0 deletions includes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ final class Blocks {
*/
public static function init() {
require_once NEWSPACK_ABSPATH . 'src/blocks/reader-registration/index.php';

if ( wp_is_block_theme() && class_exists( 'Newspack\Corrections' ) && defined( 'NEWSPACK_CORRECTIONS_ENABLED' ) && NEWSPACK_CORRECTIONS_ENABLED ) {
require_once NEWSPACK_ABSPATH . 'src/blocks/correction-box/class-correction-box-block.php';
require_once NEWSPACK_ABSPATH . 'src/blocks/correction-item/class-correction-item-block.php';
}

\add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_block_editor_assets' ] );
}

Expand Down Expand Up @@ -47,6 +53,7 @@ public static function enqueue_block_editor_assets() {
'reader_activation_url' => Reader_Activation::get_setting( 'terms_url' ),
'has_recaptcha' => Recaptcha::can_use_captcha(),
'recaptcha_url' => admin_url( 'admin.php?page=newspack-connections-wizard' ),
'corrections_enabled' => wp_is_block_theme() && class_exists( 'Newspack\Corrections' ) && defined( 'NEWSPACK_CORRECTIONS_ENABLED' ) && NEWSPACK_CORRECTIONS_ENABLED,
]
);
\wp_enqueue_style(
Expand Down
77 changes: 77 additions & 0 deletions includes/corrections/class-corrections.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public static function init() {
add_action( 'wp_enqueue_scripts', [ __CLASS__, 'wp_enqueue_scripts' ] );
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'wp_enqueue_scripts' ] );
add_action( 'rest_api_init', [ __CLASS__, 'register_rest_routes' ] );
add_action( 'admin_init', [ __CLASS__, 'register_corrections_block_patterns' ] );
add_action( 'init', [ __CLASS__, 'register_corrections_template' ] );
}

/**
Expand Down Expand Up @@ -545,5 +547,80 @@ public static function corrections_customize_register( $wp_customize ) {
)
);
}

/**
* Registers the block template.
*/
public static function register_corrections_block_patterns() {
if ( ! class_exists( 'WP_Block_Patterns_Registry' ) || ! wp_is_block_theme() ) {
return;
}

$category = 'newspack-corrections';

\register_block_pattern_category(
$category,
[
'label' => __( 'Newspack Corrections', 'newspack-plugin' ),
]
);

\register_block_pattern(
'newspack/corrections-loop',
[
'categories' => [ $category ],
'title' => __( 'Corrections Loop', 'newspack-plugin' ),
'description' => __( 'A block pattern for displaying an archive of corrections.', 'newspack-plugin' ),
'content' => self::get_corrections_pattern_content( 'corrections-loop' ),
'keywords' => [ __( 'corrections', 'newspack-plugin' ), __( 'archive', 'newspack-plugin' ), __( 'loop', 'newspack-plugin' ) ],
]
);
}

/**
* Retrieves the block template content.
*
* @param string $pattern The pattern name.
*
* @return string The template content.
*/
private static function get_corrections_pattern_content( $pattern ) {
ob_start();
include_once __DIR__ . "/../templates/block-patterns/corrections/{$pattern}.php";
$content = ob_get_clean();
return $content;
}

/**
* Registers the block template.
*/
public static function register_corrections_template() {
if ( ! class_exists( 'WP_Block_Templates_Registry' ) || ! wp_is_block_theme() ) {
return;
}

\register_block_template(
'newspack//corrections-archive',
[
'title' => __( 'Corrections Archive', 'newspack-plugin' ),
'description' => __( 'A block template for displaying an archive of corrections.', 'newspack-plugin' ),
'content' => self::get_corrections_template_content( 'corrections-archive' ),
]
);
}

/**
* Retrieves the block template content.
*
* @param string $template The template name.
*
* @return string The template content.
*/
private static function get_corrections_template_content( $template ) {
ob_start();
include_once __DIR__ . "/../templates/corrections/{$template}.php";
$content = ob_get_clean();
return $content;
}
}
Corrections::init();
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Correction Loop Block Pattern.
*
* @package Newspack
*/

?>

<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:query {"queryId":0,"query":{"perPage":20,"pages":0,"offset":0,"postType":"newspack_correction","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false,"taxQuery":null,"parents":[]},"tagName":"main","layout":{"type":"constrained"}} -->
<main class="wp-block-query">

<!-- wp:group {"style":{"spacing":{"blockGap":"var:preset|spacing|30","margin":{"bottom":"var:preset|spacing|80"}}},"layout":{"type":"flex","orientation":"vertical"}} -->
<div class="wp-block-group" style="margin-bottom:var(--wp--preset--spacing--80)">
<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading"><?php _e( 'Corrections & Clarifications', 'newspack-plugin' ); ?></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><?php _e( 'We are committed to truthful, transparent, and accurate reporting. When we make a mistake, we correct it promptly and note the change clearly. Clarifications, when needed, provide additional context.', 'newspack-plugin' ); ?></p>
<!-- /wp:paragraph -->
</div>
<!-- /wp:group -->

<!-- wp:post-template {"layout":{"type":"default"}} -->
<!-- wp:newspack/correction-item /-->
<!-- /wp:post-template -->

<!-- wp:group {"metadata":{"name":"Pagination"},"className":"wp-block-query-pagination__container","style":{"spacing":{"margin":{"top":"var:preset|spacing|80"},"blockGap":"var:preset|spacing|30"}},"layout":{"type":"default"}} -->
<div class="wp-block-group wp-block-query-pagination__container" style="margin-top:var(--wp--preset--spacing--80)">
<!-- wp:separator {"className":"is-style-wide","style":{"spacing":{"margin":{"bottom":"var:preset|spacing|30"}}}} -->
<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-bottom:var(--wp--preset--spacing--30)"/>
<!-- /wp:separator -->

<!-- wp:query-pagination {"paginationArrow":"arrow","showLabel":false,"layout":{"type":"flex","justifyContent":"space-between"}} -->
<!-- wp:query-pagination-previous /-->

<!-- wp:query-pagination-numbers {"midSize":1} /-->

<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination -->
</div>
<!-- /wp:group -->

</main>
<!-- /wp:query -->
</div>
<!-- /wp:group -->
70 changes: 70 additions & 0 deletions includes/templates/corrections/corrections-archive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Correction Archive template.
*
* @package Newspack
*/

?>

<!-- wp:paragraph -->
<p><?php _e( 'Replace this with your Header', 'newspack-plugin' ); ?></p>
<!-- /wp:paragraph -->

<!-- wp:query {"queryId":0,"query":{"perPage":20,"pages":0,"offset":0,"postType":"newspack_correction","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false,"taxQuery":null,"parents":[]},"tagName":"main","layout":{"type":"constrained"}} -->
<main class="wp-block-query">

<!-- wp:group {"style":{"spacing":{"blockGap":"var:preset|spacing|30","margin":{"bottom":"var:preset|spacing|80"}}},"layout":{"type":"flex","orientation":"vertical"}} -->
<div class="wp-block-group" style="margin-bottom:var(--wp--preset--spacing--80)">
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading"><?php _e( 'Corrections & Clarifications', 'newspack-plugin' ); ?></h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><?php _e( 'We are committed to truthful, transparent, and accurate reporting. When we make a mistake, we correct it promptly and note the change clearly. Clarifications, when needed, provide additional context.', 'newspack-plugin' ); ?></p>
<!-- /wp:paragraph -->
</div>
<!-- /wp:group -->

<!-- wp:columns {"align":"wide","className":"is-style-first-col-to-second newspack-grid"} -->
<div class="wp-block-columns alignwide is-style-first-col-to-second newspack-grid">
<!-- wp:column {"width":"50%","metadata":{"name":"Post"}} -->
<div class="wp-block-column" style="flex-basis:50%">
<!-- wp:post-template {"layout":{"type":"default"}} -->
<!-- wp:newspack/correction-item /-->
<!-- /wp:post-template -->
</div>
<!-- /wp:column -->

<!-- wp:column {"width":"25%"} -->
<div class="wp-block-column" style="flex-basis:25%"></div>
<!-- /wp:column -->

<!-- wp:column {"width":"25%"} -->
<div class="wp-block-column" style="flex-basis:25%"></div>
<!-- /wp:column -->
</div>
<!-- /wp:columns -->

<!-- wp:group {"metadata":{"name":"Pagination"},"className":"wp-block-query-pagination__container","style":{"spacing":{"margin":{"top":"var:preset|spacing|80"},"blockGap":"var:preset|spacing|30"}},"layout":{"type":"default"}} -->
<div class="wp-block-group wp-block-query-pagination__container" style="margin-top:var(--wp--preset--spacing--80)">
<!-- wp:separator {"className":"is-style-wide","style":{"spacing":{"margin":{"bottom":"var:preset|spacing|30"}}}} -->
<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-bottom:var(--wp--preset--spacing--30)"/>
<!-- /wp:separator -->

<!-- wp:query-pagination {"paginationArrow":"arrow","showLabel":false,"layout":{"type":"flex","justifyContent":"space-between"}} -->
<!-- wp:query-pagination-previous /-->

<!-- wp:query-pagination-numbers {"midSize":1} /-->

<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination -->
</div>
<!-- /wp:group -->

</main>
<!-- /wp:query -->

<!-- wp:paragraph -->
<p><?php _e( 'Replace this with your Footer', 'newspack-plugin' ); ?></p>
<!-- /wp:paragraph -->
43 changes: 43 additions & 0 deletions src/blocks/correction-box/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "newspack/correction-box",
"category": "newspack",
"attributes": {},
"supports": {
"html": false,
"__experimentalBorder": {
"color": true,
"radius": true,
"style": true,
"width": true
},
"color": {
"link": true,
"__experimentalDefaultControls": {
"background": true,
"text": true
}
},
"spacing": {
"margin": true,
"padding": true
},
"shadow": true,
"typography": {
"fontSize": true,
"lineHeight": true,
"textAlign": true,
"__experimentalFontFamily": true,
"__experimentalTextDecoration": true,
"__experimentalFontStyle": true,
"__experimentalFontWeight": true,
"__experimentalLetterSpacing": true,
"__experimentalTextTransform": true,
"__experimentalWritingMode": true,
"__experimentalDefaultControls": {
"fontSize": true
}
}
},
"usesContext": [ "postId" ],
"textdomain": "newspack-plugin"
}
98 changes: 98 additions & 0 deletions src/blocks/correction-box/class-correction-box-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Correction Box Block.
*
* @package Newspack
*/

namespace Newspack\Blocks\Correction_Box;

use Newspack\Corrections;

defined( 'ABSPATH' ) || exit;

/**
* Corrections Box class.
*/
final class Correction_Box_Block {
/**
* Initializes the block.
*/
public static function init() {
add_action( 'init', [ __CLASS__, 'register_block' ] );
}

/**
* Registers the block.
*/
public static function register_block() {
register_block_type_from_metadata(
__DIR__ . '/block.json',
[
'render_callback' => [ __CLASS__, 'render_block' ],
'uses_context' => [ 'postId' ],
]
);
}

/**
* Block render callback.
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param object $block The block.
*
* @return string The block HTML.
*/
public static function render_block( array $attributes, string $content, $block ) {
$post_id = $block->context['postId'] ?? null;

if ( empty( $post_id ) ) {
return '';
}

// Fetch corrections.
$corrections = Corrections::get_corrections( $post_id );

if ( empty( $corrections ) ) {
return '';
}

$block_wrapper_attributes = get_block_wrapper_attributes(
[
'class' => 'correction-box',
]
);
$corrections_archive_url = get_post_type_archive_link( Corrections::POST_TYPE );

ob_start();
?>
<div <?php echo $block_wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<?php
foreach ( $corrections as $correction ) :
$correction_content = $correction->post_content;
$correction_date = \get_the_date( get_option( 'date_format' ), $correction->ID );
$correction_time = \get_the_time( get_option( 'time_format' ), $correction->ID );
$correction_heading = sprintf(
'%s, %s %s:',
Corrections::get_correction_type( $correction->ID ),
$correction_date,
$correction_time
);
?>
<p class="correction">
<a class="correction-title" href="<?php echo esc_url( $corrections_archive_url ); ?>">
<?php echo esc_html( $correction_heading ); ?>
</a>
<span class="correction-content">
<?php echo esc_html( $correction_content ); ?>
</span>
</p>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}
}

Correction_Box_Block::init();
Loading