Completed
Branch Gutenberg/form-system (1dc3a2)
by
unknown
50:13 queued 41:39
created

EventAttendees::parseAttributes()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 10
nop 1
dl 0
loc 26
rs 8.8817
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\entities\editor\blocks;
4
5
use DomainException;
6
use EE_Error;
7
use EEM_Registration;
8
use EventEspresso\core\domain\entities\editor\Block;
9
use EventEspresso\core\domain\entities\editor\CoreBlocksAssetManager;
10
use EventEspresso\core\domain\services\blocks\EventAttendeesBlockRenderer;
11
use EventEspresso\core\services\request\RequestInterface;
12
13
/**
14
 * Class EventAttendees
15
 * Returns a list of people that have registered for the specified event
16
 *
17
 * @package EventEspresso\core\domain\entities\editor\blocks\common
18
 * @author  Brent Christensen
19
 * @since   $VID:$
20
 */
21
class EventAttendees extends Block
22
{
23
24
    const BLOCK_TYPE = 'event-attendees';
25
26
    /**
27
     * @var EventAttendeesBlockRenderer $renderer
28
     */
29
    protected $renderer;
30
31
32
    /**
33
     * EventAttendees constructor.
34
     *
35
     * @param CoreBlocksAssetManager      $block_asset_manager
36
     * @param RequestInterface            $request
37
     * @param EventAttendeesBlockRenderer $renderer
38
     */
39
    public function __construct(
40
        CoreBlocksAssetManager $block_asset_manager,
41
        RequestInterface $request,
42
        EventAttendeesBlockRenderer $renderer
43
    ) {
44
        parent::__construct($block_asset_manager, $request);
45
        $this->renderer= $renderer;
46
    }
47
48
49
    /**
50
     * Perform any early setup required by the block
51
     * including setting the block type and supported post types
52
     *
53
     * @return void
54
     */
55
    public function initialize()
56
    {
57
        $this->setBlockType(self::BLOCK_TYPE);
58
        $this->setSupportedRoutes(
59
            array(
60
                'EventEspresso\core\domain\entities\route_match\specifications\admin\EspressoStandardPostTypeEditor',
61
                'EventEspresso\core\domain\entities\route_match\specifications\admin\WordPressPostTypeEditor',
62
                'EventEspresso\core\domain\entities\route_match\specifications\frontend\EspressoBlockRenderer',
63
                'EventEspresso\core\domain\entities\route_match\specifications\frontend\AnyFrontendRequest'
64
            )
65
        );
66
        $EVT_ID = $this->request->getRequestParam('page') === 'espresso_events'
67
            ? $this->request->getRequestParam('post', 0)
68
            : 0;
69
        $this->setAttributes(
70
            array(
71
                'eventId'           => array(
72
                    'type'    => 'number',
73
                    'default' => $EVT_ID,
74
                ),
75
                'datetimeId'        => array(
76
                    'type'    => 'number',
77
                    'default' => 0,
78
                ),
79
                'ticketId'          => array(
80
                    'type'    => 'number',
81
                    'default' => 0,
82
                ),
83
                'status'            => array(
84
                    'type'    => 'string',
85
                    'default' => EEM_Registration::status_id_approved,
86
                ),
87
                'limit'             => array(
88
                    'type'    => 'number',
89
                    'default' => 10,
90
                ),
91
                'order' => array(
92
                    'type' => 'string',
93
                    'default' => 'ASC'
94
                ),
95
                'orderBy' => array(
96
                    'type' => 'string',
97
                    'default' => 'lastThenFirstName',
98
                ),
99
                'showGravatar'      => array(
100
                    'type'    => 'boolean',
101
                    'default' => false,
102
                ),
103
                'avatarClass' => array(
104
                    'type' => 'string',
105
                    'default' => 'contact',
106
                ),
107
                'avatarSize' => array(
108
                    'type' => 'number',
109
                    'default' => 24,
110
                ),
111
                'displayOnArchives' => array(
112
                    'type'    => 'boolean',
113
                    'default' => false,
114
                ),
115
            )
116
        );
117
        $this->setDynamic();
118
    }
119
120
121
    /**
122
     * Returns an array where the key corresponds to the incoming attribute name from the WP block
123
     * and the value corresponds to the attribute name for the existing EspressoEventAttendees shortcode
124
     *
125
     * @since $VID:$
126
     * @return array
127
     */
128
    private function getAttributesMap()
129
    {
130
        return array(
131
            'eventId'           => 'absint',
132
            'datetimeId'        => 'absint',
133
            'ticketId'          => 'absint',
134
            'status'            => 'sanitize_text_field',
135
            'limit'             => 'intval',
136
            'showGravatar'      => 'bool',
137
            'avatarClass'       => 'sanitize_text_field',
138
            'avatarSize'        => 'absint',
139
            'displayOnArchives' => 'bool',
140
            'order' => 'sanitize_text_field',
141
            'orderBy' => 'sanitize_text_field',
142
        );
143
    }
144
145
146
    /**
147
     * Sanitizes attributes.
148
     *
149
     * @param array $attributes
150
     * @return array
151
     */
152
    private function sanitizeAttributes(array $attributes)
153
    {
154
        $sanitized_attributes = array();
155
        foreach ($attributes as $attribute => $value) {
156
            $convert = $this->getAttributesMap();
157
            if (isset($convert[ $attribute ])) {
158
                $sanitize = $convert[ $attribute ];
159
                if ($sanitize === 'bool') {
160
                    $sanitized_attributes[ $attribute ] = filter_var(
161
                        $value,
162
                        FILTER_VALIDATE_BOOLEAN
163
                    );
164
                } else {
165
                    $sanitized_attributes[ $attribute ] = $sanitize($value);
166
                }
167
                // don't pass along attributes with a 0 value
168
                if ($sanitized_attributes[ $attribute ] === 0) {
169
                    unset($sanitized_attributes[ $attribute ]);
170
                }
171
            }
172
        }
173
        return $attributes;
174
    }
175
176
177
    /**
178
     * Returns the rendered HTML for the block
179
     *
180
     * @param array $attributes
181
     * @return string
182
     * @throws DomainException
183
     * @throws EE_Error
184
     */
185
    public function renderBlock(array $attributes = array())
186
    {
187
        $attributes = $this->sanitizeAttributes($attributes);
188
        return (is_archive() || is_front_page() || is_home()) && ! $attributes['displayOnArchives']
189
            ? ''
190
            : $this->renderer->render($attributes);
191
    }
192
}
193