Completed
Branch Gutenberg/event-attendees-bloc... (683e90)
by
unknown
39:01 queued 25:57
created

EventAttendees::renderBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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