PluginProbe
Assistant – Every Day Productivity Apps / 0.6.0
Assistant – Every Day Productivity Apps v0.6.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.6.0, at backend/src/Hooks/Actions/OnEnqueueScripts.php

295 lines 8.0 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 'shouldReduceMotion' => false, /* Disabled */
81 'shouldShowLabels' => false, /* Disabled */
82 'appOrder' => $user_state['appOrder'],
83 'appearance' => $user_state['appearance'],
84 'history' => $user_state['history'],
85 'searchHistory' => $user_state['searchHistory'],
86 'window' => $user_state['window'],
87 'isAppHidden' => $user_state['isAppHidden'] ? true : false,
88 'hasSubscribed' => isset( $user_state['hasSubscribed'] ) && $user_state['hasSubscribed'] ? true : false,
89 ];
90 }
91
92 /**
93 *
94 * Compiles an array of all assistant data.
95 *
96 * NOTE: Kept in alphabetical order.
97 *
98 * @return array
99 * @throws \Exception
100 */
101 public function generate_frontend_config() {
102
103 $current_user = $this->users->current( $this->user_transform );
104
105 return [
106 'adminURLs' => $this->site->get_admin_urls(),
107 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
108 'apiRoot' => esc_url_raw( get_rest_url() ),
109 'contentTypes' => $this->posts->get_types(),
110 'contentStatus' => $this->posts->get_stati(),
111 'currentPageView' => $this->site->get_current_view(),
112 'currentUser' => $current_user,
113 'defaultAppName' => 'fl-home',
114 'emptyTrashDays' => EMPTY_TRASH_DAYS,
115 'isShowingAdminBar' => is_admin_bar_showing(),
116 'isAdmin' => is_admin(),
117 'isSiteAdmin' => is_super_admin(),
118 'isLocalhost' => $this->site->is_local(),
119 'mockup' => Mockup::get(),
120 'nonce' => [
121 'api' => wp_create_nonce( 'wp_rest' ),
122 'reply' => wp_create_nonce( 'replyto-comment' ),
123 'replyUnfiltered' => wp_create_nonce( 'unfiltered-html-comment' ),
124 'updates' => wp_create_nonce( 'updates' ),
125 ],
126 'pluginURL' => FL_ASSISTANT_URL,
127 'taxonomies' => $this->posts->get_taxononies(),
128 'userRoles' => $this->users->get_roles(),
129 'cloudConfig' => [
130 'apiUrl' => FL_ASSISTANT_CLOUD_URL,
131 'appUrl' => FL_ASSISTANT_CLOUD_APP_URL,
132 'pusherKey' => FL_ASSISTANT_PUSHER_KEY,
133 'pusherCluster' => FL_ASSISTANT_PUSHER_CLUSTER,
134 ],
135 'embedInBB' => FL_ASST_SUPPORTS_BB,
136 ];
137 }
138
139 /**
140 * Check if the frontend scripts/styles should be enqueued
141 */
142 public function should_enqueue() {
143
144 // Users must be logged in.
145 if ( ! is_user_logged_in() ) {
146 return false;
147 }
148
149 // Don't show Assistant in customizer.
150 if ( is_customize_preview() ) {
151 return false;
152 }
153
154 // Don't show in admin iframes.
155 if ( defined( 'IFRAME_REQUEST' ) && IFRAME_REQUEST ) {
156 return false;
157 }
158
159 // Don't show in post previews.
160 if ( isset( $_GET['fl_asst_post_preview'] ) ) {
161 return false;
162 }
163
164 // User is logged in, and the current request is not customizer or an iframe
165 $state = UserState::get();
166
167 // Don't show Assistant in the WP Admin if the user has turned it off.
168 if ( is_admin() && ! $state['shouldShowInAdmin'] ) {
169 return false;
170 }
171
172 // There is no read-only assistant (for now). Users must be able to edit.
173 if ( ! current_user_can( 'edit_others_posts' ) ) {
174 return false;
175 }
176
177 return true;
178 }
179
180 public function register_vendors() {
181
182 $url = FL_ASSISTANT_URL;
183 $ver = FL_ASSISTANT_VERSION;
184
185 // Bundled vendors
186 // These vendors are special in that they attache a global reference to themselves on the FL.vendors object.
187 // classnames
188 wp_register_script( 'classnames', $url . 'build/vendor-classnames.js', [], $ver, false );
189
190 // redux
191 wp_register_script( 'redux', $url . 'build/vendor-redux.js', [], $ver, false );
192
193 // react-router-dom
194 wp_register_script( 'react-router-dom', $url . 'build/vendor-react-router-dom.js', [ 'react' ], $ver, false );
195
196 // framer-motion
197 wp_register_script( 'framer-motion', $url . 'build/vendor-framer-motion.js', [ 'react' ], $ver, false );
198
199 // react-laag
200 wp_register_script( 'react-laag', $url . 'build/vendor-react-laag.js', [ 'react' ], $ver, false );
201
202 // HTML2Canvas
203 wp_register_script( 'html2canvas', $url . 'build/vendor-html2canvas.js', [], $ver, false );
204
205 // @beaverbuilder/app-core
206 wp_register_script( 'bb-app-core', $url . 'build/vendor-bb-app-core.js', [ 'react', 'redux' ], $ver, false );
207
208 // @beaverbuilder/cloud
209 wp_register_script( 'bb-cloud', $url . 'build/vendor-bb-cloud.js', [ 'react' ], $ver, false );
210
211 // @beaverbuilder/icons
212 wp_register_script( 'bb-icons', $url . 'build/vendor-bb-icons.js', [ 'react' ], $ver, false );
213
214 // @beaverbuilder/fluid
215 $fluid_deps = [
216 'react',
217 'react-dom',
218 'redux',
219 'react-router-dom',
220 'classnames',
221 'framer-motion',
222 'react-laag',
223 'wp-i18n',
224 'bb-icons'
225 ];
226 wp_register_script( 'bb-fluid', $url . 'build/vendor-bb-fluid.js', $fluid_deps, $ver, false );
227 wp_register_style( 'bb-fluid', $url . 'build/vendor-bb-fluid.css', [], $ver, null );
228
229 // @beaverbuilder/forms
230 wp_register_script( 'bb-forms', $url . 'build/vendor-bb-forms.js', [ 'bb-fluid' ], $ver, false );
231 wp_register_style( 'bb-forms', $url . 'build/vendor-bb-forms.css', [ 'bb-fluid' ], $ver, null );
232
233 }
234
235 /**
236 * Enqueue all scripts and styles
237 */
238 public function enqueue() {
239
240 self::register_vendors();
241
242 $url = FL_ASSISTANT_URL;
243 $ver = FL_ASSISTANT_VERSION;
244
245 if ( $this->should_enqueue() ) {
246
247 $config = $this->generate_frontend_config();
248 $state = $this->generate_initial_state();
249
250 // API - loaded in header
251 $js_deps = [
252 'wp-i18n',
253 'wp-keycodes',
254 'wp-dom-ready',
255 'wp-components',
256 'wp-date',
257 'bb-fluid',
258 'bb-forms',
259 'bb-app-core',
260 'bb-cloud',
261 'bb-icons'
262 ];
263 $css_deps = [
264 'bb-fluid',
265 'bb-forms',
266 'wp-components'
267 ];
268
269 // System - loaded in header
270 wp_enqueue_style( 'fl-assistant', $url . 'build/system.css', $css_deps, $ver, null );
271 wp_enqueue_script( 'fl-assistant', $url . 'build/system.js', $js_deps, $ver, false );
272
273 wp_localize_script( 'fl-assistant', 'FL_ASSISTANT_CONFIG', $config );
274 wp_localize_script( 'fl-assistant', 'FL_ASSISTANT_INITIAL_STATE', $state );
275
276 // Apps - loaded in header
277 wp_enqueue_script( 'fl-assistant-apps', $url . 'build/apps.js', [ 'fl-assistant', 'html2canvas' ], $ver, false );
278 wp_enqueue_style( 'fl-assistant-apps', $url . 'build/apps.css', [ 'fl-assistant' ], $ver, null );
279
280 // Render - loaded in footer
281 wp_enqueue_style( 'fl-assistant-render', $url . 'build/render.css', [ 'fl-assistant' ], $ver, null );
282 wp_enqueue_script( 'fl-assistant-render', $url . 'build/render.js', [ 'fl-assistant' ], $ver, true );
283
284 // WordPress Media Uploader
285 wp_enqueue_media();
286
287 do_action( 'fl_assistant_enqueue' );
288 }
289 }
290
291 public function __invoke() {
292 $this->enqueue();
293 }
294 }
295