repeat-visitor.php
59 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Repeat Visitor Block |
| 4 | * |
| 5 | * @since 7.2.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Repeat_Visitor; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit( 0 ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Registers the block for use in Gutenberg |
| 21 | * This is done via an action so that we can disable |
| 22 | * registration if we need to. |
| 23 | */ |
| 24 | function register_block() { |
| 25 | Blocks::jetpack_register_block( |
| 26 | __DIR__, |
| 27 | array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
| 28 | ); |
| 29 | } |
| 30 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 31 | |
| 32 | /** |
| 33 | * Repeat Visitor block dependency declaration. |
| 34 | * |
| 35 | * @param array $attributes Array containing the block attributes. |
| 36 | * @param string $content String containing the block content. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | function render_block( $attributes, $content ) { |
| 41 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 42 | |
| 43 | $classes = Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attributes ); |
| 44 | |
| 45 | $count = isset( $_COOKIE['jp-visit-counter'] ) ? (int) $_COOKIE['jp-visit-counter'] : 0; |
| 46 | $criteria = isset( $attributes['criteria'] ) ? $attributes['criteria'] : 'after-visits'; |
| 47 | $threshold = isset( $attributes['threshold'] ) ? (int) $attributes['threshold'] : 3; |
| 48 | |
| 49 | if ( |
| 50 | ( 'after-visits' === $criteria && $count >= $threshold ) || |
| 51 | ( 'before-visits' === $criteria && $count < $threshold ) |
| 52 | ) { |
| 53 | return $content; |
| 54 | } |
| 55 | |
| 56 | // return an empty div so that view script increments the visit counter in the cookie. |
| 57 | return '<div class="' . esc_attr( $classes ) . '"></div>'; |
| 58 | } |
| 59 |