Completed
Branch FET-9222-rest-api-writes (d21e32)
by
unknown
132:12 queued 120:09
created

ShortcodesManager   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 210
Duplicated Lines 8.57 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 18
loc 210
rs 10
c 1
b 0
f 0
wmc 23
lcom 1
cbo 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getShortcodes() 0 7 2
A loadShortcodesCollection() 0 21 1
B registerShortcodes() 13 25 4
B addShortcodes() 0 20 5
B wpHead() 5 21 6
A parseContentForShortcodes() 0 15 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace EventEspresso\core\services\shortcodes;
3
4
use DomainException;
5
use EventEspresso\core\domain\EnqueueAssetsInterface;
6
// use EventEspresso\core\domain\SetHooksInterface;
7
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay;
8
use EventEspresso\core\exceptions\InvalidClassException;
9
use EventEspresso\core\exceptions\InvalidDataTypeException;
10
use EventEspresso\core\exceptions\InvalidEntityException;
11
use EventEspresso\core\exceptions\InvalidFilePathException;
12
use EventEspresso\core\exceptions\InvalidIdentifierException;
13
use EventEspresso\core\exceptions\InvalidInterfaceException;
14
use EventEspresso\core\services\collections\CollectionDetails;
15
use EventEspresso\core\services\collections\CollectionInterface;
16
use EventEspresso\core\services\collections\CollectionLoader;
17
use Exception;
18
19
defined('EVENT_ESPRESSO_VERSION') || exit;
20
21
22
23
/**
24
 * Class ShortcodesManager
25
 * Loads a Collection of ShortcodeInterface classes
26
 * then retrieves shortcode tags and calls add_shortcode() for each
27
 * ensures assets are registered and enqueued at the appropriate time
28
 *
29
 * @package       Event Espresso
30
 * @author        Brent Christensen
31
 * @since         4.9.26
32
 */
33
class ShortcodesManager
34
{
35
36
    /**
37
     * @var LegacyShortcodesManager $legacy_shortcodes_manager
38
     */
39
    private $legacy_shortcodes_manager;
40
41
    /**
42
     * @var ShortcodeInterface[] $shortcodes
43
     */
44
    private $shortcodes;
45
46
47
48
    /**
49
     * ShortcodesManager constructor
50
     *
51
     * @param LegacyShortcodesManager $legacy_shortcodes_manager
52
     */
53
    public function __construct(LegacyShortcodesManager $legacy_shortcodes_manager) {
54
        $this->legacy_shortcodes_manager = $legacy_shortcodes_manager;
55
        // assemble a list of installed and active shortcodes
56
        add_action(
57
            'AHEE__EE_System__register_shortcodes_modules_and_widgets',
58
            array($this, 'registerShortcodes'),
59
            999
60
        );
61
        //  call add_shortcode() for all installed shortcodes
62
        add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'addShortcodes'));
63
        // check content for shortcodes the old way
64
        add_action('parse_query', array($this->legacy_shortcodes_manager, 'initializeShortcodes'), 5);
65
        // check content for shortcodes the NEW more efficient way
66
        add_action('wp_head', array($this, 'wpHead'), 0);
67
    }
68
69
70
71
    /**
72
     * @return CollectionInterface|ShortcodeInterface[]
73
     * @throws InvalidIdentifierException
74
     * @throws InvalidInterfaceException
75
     * @throws InvalidFilePathException
76
     * @throws InvalidEntityException
77
     * @throws InvalidDataTypeException
78
     * @throws InvalidClassException
79
     */
80
    public function getShortcodes()
81
    {
82
        if ( ! $this->shortcodes instanceof CollectionInterface) {
83
            $this->shortcodes = $this->loadShortcodesCollection();
84
        }
85
        return $this->shortcodes;
86
    }
87
88
89
90
    /**
91
     * @return CollectionInterface|ShortcodeInterface[]
92
     * @throws InvalidIdentifierException
93
     * @throws InvalidInterfaceException
94
     * @throws InvalidFilePathException
95
     * @throws InvalidEntityException
96
     * @throws InvalidDataTypeException
97
     * @throws InvalidClassException
98
     */
99
    protected function loadShortcodesCollection()
100
    {
101
        $loader = new CollectionLoader(
102
            new CollectionDetails(
103
            // collection name
104
                'shortcodes',
105
                // collection interface
106
                'EventEspresso\core\services\shortcodes\ShortcodeInterface',
107
                // FQCNs for classes to add (all classes within that namespace will be loaded)
108
                array('EventEspresso\core\domain\entities\shortcodes'),
109
                // filepaths to classes to add
110
                array(),
111
                // filemask to use if parsing folder for files to add
112
                '',
113
                // what to use as identifier for collection entities
114
                // using CLASS NAME prevents duplicates (works like a singleton)
115
                CollectionDetails::ID_CLASS_NAME
116
            )
117
        );
118
        return $loader->getCollection();
119
    }
120
121
122
123
    /**
124
     * @return void
125
     * @throws DomainException
126
     * @throws InvalidInterfaceException
127
     * @throws InvalidIdentifierException
128
     * @throws InvalidFilePathException
129
     * @throws InvalidEntityException
130
     * @throws InvalidDataTypeException
131
     * @throws InvalidClassException
132
     */
133
    public function registerShortcodes()
134
    {
135
        try {
136
            $this->shortcodes = apply_filters(
137
                'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection',
138
                $this->getShortcodes()
139
            );
140 View Code Duplication
            if (! $this->shortcodes instanceof CollectionInterface) {
141
                throw new InvalidEntityException(
142
                    $this->shortcodes,
143
                    'CollectionInterface',
144
                    sprintf(
145
                        esc_html__(
146
                            'The "FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection" filter must return a Collection of EspressoShortcode objects. "%1$s" was received instead.',
147
                            'event_espresso'
148
                        ),
149
                        is_object($this->shortcodes) ? get_class($this->shortcodes) : gettype($this->shortcodes)
150
                    )
151
                );
152
            }
153
            $this->legacy_shortcodes_manager->registerShortcodes();
154
        } catch (Exception $exception) {
155
            new ExceptionStackTraceDisplay($exception);
156
        }
157
    }
158
159
160
161
    /**
162
     * @return void
163
     */
164
    public function addShortcodes()
165
    {
166
        try {
167
            // cycle thru shortcode folders
168
            foreach ($this->shortcodes as $shortcode) {
169
                /** @var ShortcodeInterface $shortcode */
170
                if ( $shortcode instanceof EnqueueAssetsInterface) {
171
                    add_action('wp_enqueue_scripts', array($shortcode, 'registerScriptsAndStylesheets'), 10);
172
                    add_action('wp_enqueue_scripts', array($shortcode, 'enqueueStylesheets'), 11);
173
                }
174
                // add_shortcode() if it has not already been added
175
                if ( ! shortcode_exists($shortcode->getTag())) {
176
                    add_shortcode($shortcode->getTag(), array($shortcode, 'processShortcodeCallback'));
177
                }
178
            }
179
            $this->legacy_shortcodes_manager->addShortcodes();
180
        } catch (Exception $exception) {
181
            new ExceptionStackTraceDisplay($exception);
182
        }
183
    }
184
185
186
187
    /**
188
     * callback for the "wp_head" hook point
189
     * checks posts for EE shortcodes, and initializes them,
190
     * then toggles filter switch that loads core default assets
191
     *
192
     * @return void
193
     */
194
    public function wpHead()
195
    {
196
        global $wp_query;
197
        if (empty($wp_query->posts)) {
198
            return;
199
        }
200
        $load_assets = false;
201
        // array of posts displayed in current request
202
        $posts = is_array($wp_query->posts) ? $wp_query->posts : array($wp_query->posts);
203
        foreach ($posts as $post) {
204
            // now check post content and excerpt for EE shortcodes
205
            $load_assets = $this->parseContentForShortcodes($post->post_content)
206
                ? true
207
                : $load_assets;
208
        }
209 View Code Duplication
        if ($load_assets) {
210
            $this->legacy_shortcodes_manager->registry()->REQ->set_espresso_page(true);
211
            add_filter('FHEE_load_css', '__return_true');
212
            add_filter('FHEE_load_js', '__return_true');
213
        }
214
    }
215
216
217
218
    /**
219
     * checks supplied content against list of shortcodes,
220
     * then initializes any found shortcodes, and returns true.
221
     * returns false if no shortcodes found.
222
     *
223
     * @param string $content
224
     * @return bool
225
     */
226
    public function parseContentForShortcodes($content)
227
    {
228
        $has_shortcode = false;
229
        if(empty($this->shortcodes)){
230
            return $has_shortcode;
231
        }
232
        foreach ($this->shortcodes as $shortcode) {
233
            /** @var ShortcodeInterface $shortcode */
234
            if (has_shortcode($content, $shortcode->getTag())) {
235
                $shortcode->initializeShortcode();
236
                $has_shortcode = true;
237
            }
238
        }
239
        return $has_shortcode;
240
    }
241
242
}
243
// End of file ShortcodesManager.php
244
// Location: EventEspresso\core\services\shortcodes/ShortcodesManager.php