-
Notifications
You must be signed in to change notification settings - Fork 56
feat(content-gate): add countdown block #4176
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
Changes from all commits
bcbaa98
66abff5
716f5fa
64565b4
50c2267
68e9bbd
5b19478
618127d
fdd706f
88445ce
e62f54d
009cbde
68df86c
4202b80
492e931
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -686,6 +686,29 @@ public static function get_restricted_post_excerpt( $post ) { | |
| $content = explode( '<!--more-->', $content )[0]; | ||
| } else { | ||
| $count = (int) get_post_meta( $gate_post_id, 'visible_paragraphs', true ); | ||
| // Remove all spaces. | ||
| $content = preg_replace( '/\s+/', ' ', $content ); | ||
| /** | ||
| * Filter the list of blocks to exclude from the excerpt. | ||
| * | ||
| * @param array $excluded_blocks Array of blocks to exclude. i.e. [ 'core/image', 'newspack/content-gate-countdown' ]. | ||
| * | ||
| * @return array | ||
| */ | ||
| $excluded_blocks = apply_filters( 'newspack_memberships_excerpt_excluded_blocks', [ 'newspack/content-gate-countdown' ] ); | ||
| // Remove unwanted blocks from the content. | ||
| foreach ( $excluded_blocks as $block ) { | ||
| [ $category, $name ] = explode( '/', $block ); | ||
| if ( ! $category || ! $name ) { | ||
| continue; | ||
| } | ||
chickenn00dle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ( 'core' === $category ) { | ||
| $regex = $name; | ||
| } else { | ||
| $regex = "$category\/$name"; | ||
| } | ||
| $content = preg_replace( "/<!-- wp:$regex {?.*?}? -->.*?<!-- \/wp:$regex -->/s", '', $content ); | ||
chickenn00dle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't filter this, any inner paragraph blocks within the content gate block will appear in the excerpt despite us explicitly returning an empty string in the render callback. |
||
| // Split into paragraphs. | ||
| $content = explode( '</p>', $content ); | ||
| // Extract the first $x paragraphs only. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { SVG, Path } from '@wordpress/primitives'; | ||
|
|
||
| const countdown = ( | ||
| <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
| <Path d="M19 6.5H5a.5.5 0 0 0-.5.5v10a.5.5 0 0 0 .5.5h14a.5.5 0 0 0 .5-.5V7a.5.5 0 0 0-.5-.5ZM5 5h14a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2Z" /> | ||
| <Path d="M13.202 15V9.4h1.008l2.392 3.616V9.4h1.024V15h-.84l-2.56-3.84V15h-1.024ZM9.147 15.68 11.29 9.4h1l-2.144 6.28h-1ZM6.91 15v-4.152a2.303 2.303 0 0 1-.332.176 1.63 1.63 0 0 1-.372.112l-.088-.864a1.18 1.18 0 0 0 .384-.172c.141-.09.272-.197.392-.32.12-.125.205-.252.256-.38h.784V15H6.91Z" /> | ||
| </SVG> | ||
| ); | ||
|
|
||
| export default countdown; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| { | ||
| "$schema": "https://schemas.wp.org/trunk/block.json", | ||
| "apiVersion": 3, | ||
| "name": "newspack/content-gate-countdown", | ||
| "category": "newspack", | ||
| "attributes": { | ||
| "text": { | ||
| "type": "string", | ||
| "default": "" | ||
| } | ||
| }, | ||
| "supports": { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the design, it seems that the block should also support
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Added in c9ed5ab |
||
| "align": [ "wide", "full" ], | ||
| "html": false, | ||
| "color": { | ||
| "link": true, | ||
| "__experimentalDefaultControls": { | ||
| "background": true, | ||
| "text": 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 | ||
| } | ||
| } | ||
| }, | ||
| "textdomain": "newspack-plugin" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| <?php | ||
| /** | ||
| * Content Gate Countdown Block | ||
| * | ||
| * @package Newspack | ||
| */ | ||
|
|
||
| namespace Newspack; | ||
|
|
||
| defined( 'ABSPATH' ) || exit; | ||
|
|
||
| use Newspack\Memberships; | ||
| use Newspack\Memberships\Metering; | ||
|
|
||
| /** | ||
| * Content Gate Countdown Block class. | ||
| */ | ||
| class Content_Gate_Countdown_Block { | ||
| /** | ||
| * Initialize the block. | ||
| */ | ||
| public static function init() { | ||
| add_action( 'init', [ __CLASS__, 'register_block' ] ); | ||
| add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_scripts' ] ); | ||
| } | ||
|
|
||
| /** | ||
| * Enqueue block scripts and styles. | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function enqueue_scripts() { | ||
| if ( ! Memberships::is_active() || ! is_singular() ) { | ||
| return; | ||
| } | ||
| wp_enqueue_script( | ||
| 'newspack-content-gate-countdown-block', | ||
| \Newspack\Newspack::plugin_url() . '/dist/content-gate-countdown-block.js', | ||
| [ 'wp-i18n', 'newspack-memberships-gate-metering' ], | ||
| NEWSPACK_PLUGIN_VERSION, | ||
| true | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Register the block. | ||
| */ | ||
| public static function register_block() { | ||
| register_block_type_from_metadata( | ||
| __DIR__ . '/block.json', | ||
| [ | ||
| 'render_callback' => [ __CLASS__, 'render_block' ], | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Block render callback. | ||
| * | ||
| * @param array $attributes The block attributes. | ||
| * @param string $content The block content. | ||
| * | ||
| * @return string The block HTML. | ||
| */ | ||
| public static function render_block( array $attributes, string $content ) { | ||
| if ( ! Metering::is_metering() || ! Memberships::is_post_restricted() ) { | ||
| return ''; | ||
| } | ||
| $total_views = Metering::get_total_metered_views( \is_user_logged_in() ); | ||
| if ( false === $total_views ) { | ||
| return ''; | ||
| } | ||
| $views = Metering::get_metered_views( get_current_user_id() ); | ||
| $countdown = sprintf( | ||
| /* translators: 1: current number of metered views, 2: total metered views. */ | ||
| __( '%1$d/%2$d', 'newspack-plugin' ), | ||
| $views, | ||
| $total_views | ||
| ); | ||
| $text = isset( $attributes['text'] ) ? esc_html( $attributes['text'] ) : ''; | ||
| if ( empty( $text ) ) { | ||
| $text = sprintf( | ||
| /* translators: %s - metered content period (week, month, etc. */ | ||
| __( | ||
| 'free articles this %s', | ||
| 'newspack-plugin' | ||
| ), | ||
| Metering::get_metering_period() | ||
| ); | ||
| } | ||
| $block_wrapper_attributes = get_block_wrapper_attributes( | ||
| [ | ||
| 'class' => 'newspack-content-gate-countdown__wrapper', | ||
| ] | ||
| ); | ||
| $block_content = "<div $block_wrapper_attributes> | ||
| <div class='newspack-content-gate-countdown__content'> | ||
| <div class='newspack-content-gate-countdown__text'> | ||
| <span class='newspack-content-gate-countdown__countdown'>$countdown</span> | ||
| <p>$text</p> | ||
| </div> | ||
| $content | ||
| </div> | ||
| </div>"; | ||
chickenn00dle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return $block_content; | ||
| } | ||
| } | ||
|
|
||
| Content_Gate_Countdown_Block::init(); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is superfluous as we're already in the
Newspacknamespace.We can keep
Newspack\Memberships\Metering, but I don't think it's bad to useMemberships\Meteringin the code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels cleaner to use just
Meteringin the code imo, so I'll keep it as is. Happy to change it if you feel strongly about it though!