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

223 lines 5.7 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 FLBuilderModel;
11
12 /**
13 * Class OnEnqueueScripts
14 * @package FL\Assistant\Hooks\Actions
15 *
16 */
17 class OnEnqueueScripts {
18
19 /**
20 * @var UsersRepository
21 */
22 protected $users;
23 /**
24 * @var PostsRepository
25 */
26 protected $posts;
27
28 /**
29 * @var Site
30 */
31 protected $site;
32
33 /**
34 * @var UserTransformer
35 */
36 protected $user_transform;
37
38
39 /**
40 * OnEnqueueScripts constructor.
41 *
42 * @param UsersRepository $users
43 * @param PostsRepository $posts
44 * @param Site $site
45 * @param UserTransformer $user_transform
46 */
47 public function __construct(
48 UsersRepository $users,
49 PostsRepository $posts,
50 Site $site,
51 UserTransformer $user_transform
52 ) {
53
54 $this->users = $users;
55 $this->posts = $posts;
56 $this->site = $site;
57 $this->user_transform = $user_transform;
58 }
59
60
61 /**
62 * @return array
63 * @throws \Exception
64 */
65 public function generate_initial_state() {
66 if ( ! is_user_logged_in() ) {
67 return [];
68 }
69
70 $user_state = UserState::get();
71
72 // Always show in admin bar and hidden in wp-admin.
73 if ( is_admin() ) {
74 $user_state['window']['isHidden'] = true;
75 $user_state['window']['hiddenAppearance'] = 'admin_bar';
76 }
77
78 return [
79 'appOrder' => $user_state['appOrder'],
80 'shouldReduceMotion' => false, /* Disabled */
81
82 /* New UI Props */
83 'appearance' => $user_state['appearance'],
84 'history' => $user_state['history'],
85 'searchHistory' => $user_state['searchHistory'],
86 'shouldShowLabels' => false, /* Disabled */
87 'window' => $user_state['window'],
88 ];
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 'contentTypes' => $this->posts->get_types(),
111 'contentStatus' => $this->posts->get_stati(),
112 'currentPageView' => $this->site->get_current_view(),
113 'currentUser' => $current_user,
114 'defaultAppName' => 'fl-dashboard',
115 'emptyTrashDays' => EMPTY_TRASH_DAYS,
116 'isShowingAdminBar' => is_admin_bar_showing(),
117 'isAdmin' => is_admin(),
118 'nonce' => [
119 'api' => wp_create_nonce( 'wp_rest' ),
120 'reply' => wp_create_nonce( 'replyto-comment' ),
121 'replyUnfiltered' => wp_create_nonce( 'unfiltered-html-comment' ),
122 'updates' => wp_create_nonce( 'updates' ),
123 ],
124 'pluginURL' => FL_ASSISTANT_URL,
125 'taxonomies' => $this->posts->get_taxononies(),
126 'userRoles' => $this->users->get_roles(),
127
128 /*
129 'integrations' => [
130 'yoastSEO' => [
131 'isActive' => is_plugin_active('wordpress-seo/wp-seo.php')
132 ]
133 ],*/
134 ];
135 }
136
137 /**
138 * Check if the frontend scripts/styles should be enqueued
139 */
140 public function should_enqueue() {
141
142 // Users must be logged in.
143 if ( ! is_user_logged_in() ) {
144 return false;
145 }
146
147 // Don't show Assistant in customizer.
148 if ( is_customize_preview() ) {
149 return false;
150 }
151
152 // Don't show in admin iframes.
153 if ( defined( 'IFRAME_REQUEST' ) && IFRAME_REQUEST ) {
154 return false;
155 }
156
157 // User is logged in, and the current request is not customizer or an iframe
158 $state = UserState::get();
159
160 // Don't show Assistant in the WP Admin if the user has turned it off.
161 if ( is_admin() && ! $state['shouldShowInAdmin'] ) {
162 return false;
163 }
164
165 // There is no read-only assistant (for now). Users must be able to edit.
166 if ( ! current_user_can( 'edit_others_posts' ) ) {
167 return false;
168 }
169
170 return true;
171 }
172
173 /**
174 * Enqueue all scripts and styles
175 */
176 public function enqueue() {
177
178 $url = FL_ASSISTANT_URL;
179 $ver = FL_ASSISTANT_VERSION;
180
181 wp_register_script( 'fl-fluid', $url . 'build/fl-assistant-fluid.bundle.js', [ 'react', 'react-dom' ], $ver, false );
182 wp_register_style( 'fl-fluid', $url . 'build/fl-assistant-fluid.bundle.css', [], $ver, null );
183 wp_enqueue_media();
184 if ( $this->should_enqueue() ) {
185
186 $config = $this->generate_frontend_config();
187 $state = $this->generate_initial_state();
188
189 // API - loaded in header
190 $js_deps = [
191 'fl-fluid',
192 'lodash',
193 'heartbeat',
194 'wp-i18n',
195 'wp-keycodes',
196 'wp-dom-ready',
197 'wp-components',
198 'wp-date',
199 ];
200
201 wp_enqueue_style( 'fl-assistant-system', $url . 'build/fl-assistant-system.bundle.css', [ 'fl-fluid', 'wp-components' ], $ver, null );
202 wp_enqueue_script( 'fl-assistant-system', $url . 'build/fl-assistant-system.bundle.js', $js_deps, $ver, false );
203
204 wp_localize_script( 'fl-assistant-system', 'FL_ASSISTANT_CONFIG', $config );
205 wp_localize_script( 'fl-assistant-system', 'FL_ASSISTANT_INITIAL_STATE', $state );
206
207 // Apps - loaded in header
208 wp_enqueue_style( 'fl-assistant-apps', $url . 'build/fl-assistant-apps.bundle.css', [], $ver, null );
209 wp_enqueue_script( 'fl-assistant-apps', $url . 'build/fl-assistant-apps.bundle.js', $js_deps, $ver, false );
210
211 // UI Render - loaded in footer
212 wp_enqueue_style( 'fl-assistant-render', $url . 'build/fl-assistant-render.bundle.css', [], $ver, null );
213 wp_enqueue_script( 'fl-assistant-render', $url . 'build/fl-assistant-render.bundle.js', $js_deps, $ver, true );
214
215 do_action( 'fl_assistant_enqueue' );
216 }
217 }
218
219 public function __invoke() {
220 $this->enqueue();
221 }
222 }
223