-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderer.php
104 lines (92 loc) · 3.71 KB
/
Renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* The render class.
*
* This class defines all code necessary for rendering the block.
*
* NOTES:
* 1. This file in concidered as safe. No user checks are needed.
*/
namespace PatternFriend;
if ( ! defined( 'ABSPATH' ) ) { exit; }
class Renderer {
/**
* Add the device visibility wrapper to the block content.
*
* @param string $block_content The block content.
* @param array $block The block.
* @return string The block content.
*/
public static function device_visibility_wrapper($block_content, $block) {
// Collect attributes.
$attributes = $block['attrs'];
$hide_on_mobile = isset($attributes['pattern_friend_hide_on_mobile']) ? $attributes['pattern_friend_hide_on_mobile'] : false;
$hide_on_tablet = isset($attributes['pattern_friend_hide_on_tablet']) ? $attributes['pattern_friend_hide_on_tablet'] : false;
$hide_on_computer = isset($attributes['pattern_friend_hide_on_computer']) ? $attributes['pattern_friend_hide_on_computer'] : false;
/*
* Wrap the block content with the visibility classes based on the attributes,
* if any of the attributes are set to true.
*/
if ($hide_on_mobile || $hide_on_tablet || $hide_on_computer) {
$wrapperElementClasses = '';
if ($hide_on_mobile) $wrapperElementClasses .= ' pf-hide-on-mobile';
if ($hide_on_tablet) $wrapperElementClasses .= ' pf-hide-on-tablet';
if ($hide_on_computer) $wrapperElementClasses .= ' pf-hide-on-desktop';
$block_content = '<div class="' . esc_attr($wrapperElementClasses) . '">' . $block_content . '</div>';
}
// Return the block content.
return $block_content;
}
/**
* Attend the hidable groups.
*
* Note that groups includes groups, rows, and stacks.
*
* Wrap each block with a div element that can be hidden, assign it an id.
*
* @param string $block_content The block content.
* @param array $block The block.
* @return string The block content.
*/
public static function hidable_group($block_content, $block) {
// Get attributes.
$attributes = $block['attrs'];
// Check if the block is of typ "core/group".
if ($block['blockName'] !== 'core/group') {
return $block_content;
}
// Check if the block is hidable.
$hidable_group = isset($attributes['pattern_friend_hidable_group']) ? $attributes['pattern_friend_hidable_group'] : false;
// If the block is hidable, wrap it with a div element.
if ($hidable_group && isset($attributes['pattern_friend_id'])) {
$block_content = '<div class="pf-hidable" data-block-id="' . $attributes['pattern_friend_id'] . '">' . $block_content . '</div>';
}
// Return the block content.
return $block_content;
}
/**
* Attend the group hiding buttons.
*
* Assigns the button to a javascript function that hides the group
* and stores the group id in the choosen way.
*/
public static function group_hiding_button($block_content, $block) {
// Get attributes.
$attributes = $block['attrs'];
// Check if the block is of type "core/button".
if ($block['blockName'] !== 'core/button') {
return $block_content;
}
// Check if the button is a group hiding button.
$hidable_group_button = isset($attributes['pattern_friend_hidable_group_button']) ? $attributes['pattern_friend_hidable_group_button'] : false;
// If the button is a group hiding button, put it inside a wrapper bound to a onClick function.
if ($hidable_group_button) {
// Get duration.
$duration = isset($attributes['pattern_friend_hide_duration']) ? $attributes['pattern_friend_hide_duration'] : 24;
// Wrap the button with a div element.
$block_content = '<div class="pf-hidable-group-button" onclick="patternFriend.hide(this, ' . $duration . ')">' . $block_content . '</div>';
}
// Return the block content.
return $block_content;
}
}