Completed
Branch EDTR/master (5c103b)
by
unknown
10:13 queued 38s
created

EventEditor::loadScriptsStyles()   B

Complexity

Conditions 9
Paths 53

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 53
nop 0
dl 0
loc 29
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\admin\events\editor;
4
5
use DomainException;
6
use EE_Admin_Config;
7
use EE_Error;
8
use EE_Event;
9
use EventEspresso\core\domain\entities\admin\GraphQLData\CurrentUser;
10
use EventEspresso\core\domain\entities\admin\GraphQLData\GeneralSettings;
11
use EventEspresso\core\domain\services\assets\EspressoEditorAssetManager;
12
use EventEspresso\core\exceptions\InvalidDataTypeException;
13
use EventEspresso\core\exceptions\InvalidInterfaceException;
14
use EventEspresso\core\exceptions\ModelConfigurationException;
15
use EventEspresso\core\exceptions\UnexpectedEntityException;
16
use EventEspresso\core\services\assets\JedLocaleData;
17
use InvalidArgumentException;
18
use ReflectionException;
19
use WP_Post;
20
use WPGraphQL\Router;
21
22
/**
23
 * Class EventEditor
24
 * Description
25
 *
26
 * @package EventEspresso\core\domain\services\admin\events\editor
27
 * @author  Manzoor Wani,  Brent Christensen
28
 * @since   $VID:$
29
 */
30
class EventEditor
31
{
32
33
    /**
34
     * @var EE_Admin_Config
35
     */
36
    protected $admin_config;
37
38
    /**
39
     * @var CurrentUser $current_user
40
     */
41
    protected $current_user;
42
43
    /**
44
     * @var EE_Event
45
     */
46
    protected $event;
47
48
    /**
49
     * @var EventEditorGraphQLData
50
     */
51
    protected $event_editor_gql_data;
52
53
    /**
54
     * @var GeneralSettings $general_settings
55
     */
56
    protected $general_settings;
57
58
    /**
59
     * @var JedLocaleData $jed_locale
60
     */
61
    private $jed_locale;
62
63
64
    /**
65
     * EventEditor constructor.
66
     *
67
     * @param EE_Admin_Config $admin_config
68
     * @param CurrentUser $current_user
69
     * @param EE_Event        $event
70
     * @param EventEditorGraphQLData $event_editor_gql_data
71
     * @param GeneralSettings $general_settings
72
     * @param JedLocaleData   $jed_locale
73
     */
74
    public function __construct(
75
        EE_Admin_Config $admin_config,
76
        CurrentUser $current_user,
77
        EE_Event $event,
78
        EventEditorGraphQLData $event_editor_gql_data,
79
        GeneralSettings $general_settings,
80
        JedLocaleData $jed_locale
81
    ) {
82
        $this->admin_config = $admin_config;
83
        $this->current_user = $current_user;
84
        $this->event = $event;
85
        $this->event_editor_gql_data = $event_editor_gql_data;
86
        $this->general_settings = $general_settings;
87
        $this->jed_locale = $jed_locale;
88
        add_action('admin_enqueue_scripts', [$this, 'loadScriptsStyles']);
89
    }
90
91
92
    /**
93
     * @throws EE_Error
94
     * @throws InvalidArgumentException
95
     * @throws InvalidDataTypeException
96
     * @throws InvalidInterfaceException
97
     * @throws ModelConfigurationException
98
     * @throws ReflectionException
99
     * @throws UnexpectedEntityException
100
     * @throws DomainException
101
     * @since $VID:$
102
     */
103
    public function loadScriptsStyles()
104
    {
105
        if ($this->admin_config->useAdvancedEditor()) {
106
            $eventId = $this->event instanceof EE_Event ? $this->event->ID() : 0;
107
            if (! $eventId) {
108
                global $post;
109
                $eventId = isset($_REQUEST['post']) ? absint($_REQUEST['post']) : 0;
110
                // if there's no event ID but there IS a WP Post... then use the Post ID
111
                $use_post_id = $eventId === 0 && $post instanceof WP_Post && $post->post_type === 'espresso_events';
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
112
                $eventId = $use_post_id ? $post->ID : $eventId;
113
            }
114
            if ($eventId) {
115
                $data = $this->getEditorData($eventId);
116
                $data = wp_json_encode($data);
117
                add_action(
118
                    'admin_footer',
119
                    static function () use ($data) {
120
                        wp_add_inline_script(
121
                            EspressoEditorAssetManager::JS_HANDLE_EDITOR,
122
                            "
123
var eeEditorData={$data};
124
",
125
                            'before'
126
                        );
127
                    }
128
                );
129
            }
130
        }
131
    }
132
133
134
    /**
135
     * @param int $eventId
136
     * @return array
137
     * @throws EE_Error
138
     * @throws ReflectionException
139
     * @since $VID:$
140
     */
141
    protected function getEditorData($eventId)
142
    {
143
        $event = $this->event_editor_gql_data->getData($eventId);
144
        $event['dbId'] = $eventId;
145
146
        $graphqlEndpoint = class_exists('WPGraphQL') ? trailingslashit(site_url()) . Router::$route : '';
147
        $graphqlEndpoint = esc_url($graphqlEndpoint);
148
149
        $currentUser = $this->current_user->getData();
150
151
        $generalSettings = $this->general_settings->getData();
152
153
        $i18n = $this->jed_locale->getData('event_espresso');
154
155
        $assetsUrl = EE_PLUGIN_DIR_URL . 'assets/dist/';
156
157
        return compact('event', 'graphqlEndpoint', 'currentUser', 'generalSettings', 'i18n', 'assetsUrl');
158
    }
159
}
160