Completed
Branch Gutenberg/event-attendees-bloc... (613332)
by
unknown
90:11 queued 75:35
created

EventAttendees::initialize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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