PluginProbe
Assistant – Every Day Productivity Apps / 0.5.0
Assistant – Every Day Productivity Apps v0.5.0
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
assistant / backend / src / Hooks / Actions / OnEnqueueScripts.php

OnEnqueueScripts.php in Assistant – Every Day Productivity Apps 0.5.0, at backend/src/Hooks/Actions/OnEnqueueScripts.php

225 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FL\Assistant\Hooks\Actions;
4
5 use FL\Assistant\Data\Repository\PostsRepository;
6 use FL\Assistant\Data\Repository\UsersRepository;
7 use FL\Assistant\Data\Site;
8 use FL\Assistant\Data\Transformers\UserTransformer;
9 use FL\Assistant\Data\UserState;
10 use FL\Assistant\Data\Mockup;
11 use FLBuilderModel;
12
13 /**
14 * Class OnEnqueueScripts
15 * @package FL\Assistant\Hooks\Actions
16 *
17 */
18 class OnEnqueueScripts {
19
20 /**
21 * @var UsersRepository
22 */
23 protected $users;
24 /**
25 * @var PostsRepository
26 */
27 protected $posts;
28
29 /**
30 * @var Site
31 */
32 protected $site;
33
34 /**
35 * @var UserTransformer
36 */
37 protected $user_transform;
38
39
40 /**
41 * OnEnqueueScripts constructor.
42 *
43 * @param UsersRepository $users
44 * @param PostsRepository $posts
45 * @param Site $site
46 * @param UserTransformer $user_transform
47 */
48 public function __construct(
49 UsersRepository $users,
50 PostsRepository $posts,
51 Site $site,
52 UserTransformer $user_transform
53 ) {
54
55 $this->users = $users;
56 $this->posts = $posts;
57 $this->site = $site;
58 $this->user_transform = $user_transform;
59 }
60
61
62 /**
63 * @return array
64 * @throws \Exception
65 */
66 public function generate_initial_state() {
67 if ( ! is_user_logged_in() ) {
68 return [];
69 }
70
71 $user_state = UserState::get();
72
73 // Always show in admin bar and hidden in wp-admin.
74 if ( is_admin() ) {
75 $user_state['window']['isHidden'] = true;
76 $user_state['window']['hiddenAppearance'] = 'admin_bar';
77 }
78
79 return [
80 'appOrder' => $user_state['appOrder'],
81 'shouldReduceMotion' => false, /* Disabled */
82
83 /* New UI Props */
84 'appearance' => $user_state['appearance'],
85 'history' => $user_state['history'],
86 'searchHistory' => $user_state['searchHistory'],
87 'shouldShowLabels' => false, /* Disabled */
88 'window' => $user_state['window'],
89 'isAppHidden' => $user_state['isAppHidden'] ? true : false,
90 'hasSubscribed' => isset( $user_state['hasSubscribed'] ) && $user_state['hasSubscribed'] ? true : false,
91 ];
92
93 }
94
95 /**
96 *
97 * Compiles an array of all assistant data.
98 *
99 * NOTE: Kept in alphabetical order.
100 *
101 * @return array
102 * @throws \Exception
103 */
104 public function generate_frontend_config() {
105
106 $current_user = $this->users->current( $this->user_transform );
107
108 return [
109 'adminURLs' => $this->site->get_admin_urls(),
110 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
111 'apiRoot' => esc_url_raw( get_rest_url() ),
112 'cloudUrl' => FL_ASSISTANT_CLOUD_URL,
113 'cloudWpUrl' => FL_ASSISTANT_WP_API_URL,
114 'contentTypes' => $this->posts->get_types(),
115 'contentStatus' => $this->posts->get_stati(),
116 'currentPageView' => $this->site->get_current_view(),
117 'currentUser' => $current_user,
118 'defaultAppName' => 'fl-dashboard',
119 'emptyTrashDays' => EMPTY_TRASH_DAYS,
120 'isShowingAdminBar' => is_admin_bar_showing(),
121 'isAdmin' => is_admin(),
122 'isSiteAdmin' => is_super_admin(),
123 'mockup' => Mockup::get(),
124 'nonce' => [
125 'api' => wp_create_nonce( 'wp_rest' ),
126 'reply' => wp_create_nonce( 'replyto-comment' ),
127 'replyUnfiltered' => wp_create_nonce( 'unfiltered-html-comment' ),
128 'updates' => wp_create_nonce( 'updates' ),
129 ],
130 'pluginURL' => FL_ASSISTANT_URL,
131 'taxonomies' => $this->posts->get_taxononies(),
132 'userRoles' => $this->users->get_roles(),
133 ];
134 }
135
136 /**
137 * Check if the frontend scripts/styles should be enqueued
138 */
139 public function should_enqueue() {
140
141 // Users must be logged in.
142 if ( ! is_user_logged_in() ) {
143 return false;
144 }
145
146 // Don't show Assistant in customizer.
147 if ( is_customize_preview() ) {
148 return false;
149 }
150
151 // Don't show in admin iframes.
152 if ( defined( 'IFRAME_REQUEST' ) && IFRAME_REQUEST ) {
153 return false;
154 }
155
156 // User is logged in, and the current request is not customizer or an iframe
157 $state = UserState::get();
158
159 // Don't show Assistant in the WP Admin if the user has turned it off.
160 if ( is_admin() && ! $state['shouldShowInAdmin'] ) {
161 return false;
162 }
163
164 // There is no read-only assistant (for now). Users must be able to edit.
165 if ( ! current_user_can( 'edit_others_posts' ) ) {
166 return false;
167 }
168
169 return true;
170 }
171
172 /**
173 * Enqueue all scripts and styles
174 */
175 public function enqueue() {
176
177 $url = FL_ASSISTANT_URL;
178 $ver = FL_ASSISTANT_VERSION;
179
180 wp_register_script( 'fl-fluid', $url . 'build/fl-assistant-fluid.bundle.js', [ 'react', 'react-dom', 'lodash' ], $ver, false );
181 wp_register_style( 'fl-fluid', $url . 'build/fl-assistant-fluid.bundle.css', [], $ver, null );
182
183 if ( $this->should_enqueue() ) {
184
185 $config = $this->generate_frontend_config();
186 $state = $this->generate_initial_state();
187
188 // API - loaded in header
189 $js_deps = [
190 'fl-fluid',
191 'wp-i18n',
192 'wp-keycodes',
193 'wp-dom-ready',
194 'wp-components',
195 'wp-date',
196 ];
197
198 wp_enqueue_style( 'fl-assistant-system', $url . 'build/fl-assistant-system.bundle.css', [ 'fl-fluid', 'wp-components' ], $ver, null );
199 wp_enqueue_script( 'fl-assistant-system', $url . 'build/fl-assistant-system.bundle.js', $js_deps, $ver, false );
200
201 wp_localize_script( 'fl-assistant-system', 'FL_ASSISTANT_CONFIG', $config );
202 wp_localize_script( 'fl-assistant-system', 'FL_ASSISTANT_INITIAL_STATE', $state );
203
204 // Apps - loaded in header
205 wp_enqueue_script( 'fl-assistant-apps', $url . 'build/fl-assistant-apps.bundle.js', $js_deps, $ver, false );
206
207 // UI Render - loaded in footer
208 wp_enqueue_style( 'fl-assistant-render', $url . 'build/fl-assistant-render.bundle.css', [], $ver, null );
209 wp_enqueue_script( 'fl-assistant-render', $url . 'build/fl-assistant-render.bundle.js', $js_deps, $ver, true );
210
211 // WordPress Media Uploader
212 wp_enqueue_media();
213
214 // Drip email script
215 wp_enqueue_script( 'fl-assistant-drip', 'https://tag.getdrip.com/3112548.js', [], $ver, false );
216
217 do_action( 'fl_assistant_enqueue' );
218 }
219 }
220
221 public function __invoke() {
222 $this->enqueue();
223 }
224 }
225