Completed
Push — updates/infinity-vanilla-js ( fda899...9466ff )
by
unknown
07:55
created

repeat-visitor.php ➔ render_block()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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