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

222 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 'appearance' => $user_state['appearance'],
83 'history' => $user_state['history'],
84 'searchHistory' => $user_state['searchHistory'],
85 'shouldShowLabels' => false, /* Disabled */
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 'cloudUrl' => FL_ASSISTANT_CLOUD_URL,
110 'cloudWpUrl' => FL_ASSISTANT_WP_API_URL,
111 'contentTypes' => $this->posts->get_types(),
112 'contentStatus' => $this->posts->get_stati(),
113 'currentPageView' => $this->site->get_current_view(),
114 'currentUser' => $current_user,
115 'defaultAppName' => 'fl-dashboard',
116 'emptyTrashDays' => EMPTY_TRASH_DAYS,
117 'isShowingAdminBar' => is_admin_bar_showing(),
118 'isAdmin' => is_admin(),
119 'isSiteAdmin' => is_super_admin(),
120 'mockup' => Mockup::get(),
121 'nonce' => [
122 'api' => wp_create_nonce( 'wp_rest' ),
123 'reply' => wp_create_nonce( 'replyto-comment' ),
124 'replyUnfiltered' => wp_create_nonce( 'unfiltered-html-comment' ),
125 'updates' => wp_create_nonce( 'updates' ),
126 ],
127 'pluginURL' => FL_ASSISTANT_URL,
128 'taxonomies' => $this->posts->get_taxononies(),
129 'userRoles' => $this->users->get_roles(),
130 ];
131 }
132
133 /**
134 * Check if the frontend scripts/styles should be enqueued
135 */
136 public function should_enqueue() {
137
138 // Users must be logged in.
139 if ( ! is_user_logged_in() ) {
140 return false;
141 }
142
143 // Don't show Assistant in customizer.
144 if ( is_customize_preview() ) {
145 return false;
146 }
147
148 // Don't show in admin iframes.
149 if ( defined( 'IFRAME_REQUEST' ) && IFRAME_REQUEST ) {
150 return false;
151 }
152
153 // User is logged in, and the current request is not customizer or an iframe
154 $state = UserState::get();
155
156 // Don't show Assistant in the WP Admin if the user has turned it off.
157 if ( is_admin() && ! $state['shouldShowInAdmin'] ) {
158 return false;
159 }
160
161 // There is no read-only assistant (for now). Users must be able to edit.
162 if ( ! current_user_can( 'edit_others_posts' ) ) {
163 return false;
164 }
165
166 return true;
167 }
168
169 /**
170 * Enqueue all scripts and styles
171 */
172 public function enqueue() {
173
174 $url = FL_ASSISTANT_URL;
175 $ver = FL_ASSISTANT_VERSION;
176
177 wp_register_script( 'fl-fluid', $url . 'build/fl-assistant-fluid.bundle.js', [ 'react', 'react-dom', 'lodash' ], $ver, false );
178 wp_register_style( 'fl-fluid', $url . 'build/fl-assistant-fluid.bundle.css', [], $ver, null );
179
180 if ( $this->should_enqueue() ) {
181
182 $config = $this->generate_frontend_config();
183 $state = $this->generate_initial_state();
184
185 // API - loaded in header
186 $js_deps = [
187 'fl-fluid',
188 'wp-i18n',
189 'wp-keycodes',
190 'wp-dom-ready',
191 'wp-components',
192 'wp-date',
193 ];
194
195 wp_enqueue_style( 'fl-assistant-system', $url . 'build/fl-assistant-system.bundle.css', [ 'fl-fluid', 'wp-components' ], $ver, null );
196 wp_enqueue_script( 'fl-assistant-system', $url . 'build/fl-assistant-system.bundle.js', $js_deps, $ver, false );
197
198 wp_localize_script( 'fl-assistant-system', 'FL_ASSISTANT_CONFIG', $config );
199 wp_localize_script( 'fl-assistant-system', 'FL_ASSISTANT_INITIAL_STATE', $state );
200
201 // Apps - loaded in header
202 wp_enqueue_script( 'fl-assistant-apps', $url . 'build/fl-assistant-apps.bundle.js', $js_deps, $ver, false );
203
204 // UI Render - loaded in footer
205 wp_enqueue_style( 'fl-assistant-render', $url . 'build/fl-assistant-render.bundle.css', [], $ver, null );
206 wp_enqueue_script( 'fl-assistant-render', $url . 'build/fl-assistant-render.bundle.js', $js_deps, $ver, true );
207
208 // WordPress Media Uploader
209 wp_enqueue_media();
210
211 // Drip email script
212 wp_enqueue_script( 'fl-assistant-drip', 'https://tag.getdrip.com/3112548.js', [], $ver, false );
213
214 do_action( 'fl_assistant_enqueue' );
215 }
216 }
217
218 public function __invoke() {
219 $this->enqueue();
220 }
221 }
222