PluginProbe
Assistant – Every Day Productivity Apps / 0.1.0
Assistant – Every Day Productivity Apps v0.1.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 / classes / class-fl-assistant-data.php

class-fl-assistant-data.php in Assistant – Every Day Productivity Apps 0.1.0, at classes/class-fl-assistant-data.php

431 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Handles working with backend assistant data.
5 */
6 class FL_Assistant_Data {
7
8 /**
9 * Default state for the current user.
10 */
11 static public $default_user_state = array(
12 'activeApp' => 'fl-dashboard',
13 'appFrameSize' => 'normal',
14 'appOrder' => [],
15 'isShowingUI' => true,
16 'panelPosition' => 'end',
17 'shouldReduceMotion' => false,
18 );
19
20 /**
21 * Returns an array of all assistant data.
22 *
23 * NOTE: Kept in alphabetical order.
24 */
25 static public function get_all() {
26 $user_state = self::get_current_user_state();
27
28 /**
29 * This will hydrate the config object that can be
30 * accessed using the useConfig hook. If you need
31 * state, consider adding to the store instead.
32 */
33 $config = array(
34 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
35 'apiRoot' => esc_url_raw( get_rest_url() ),
36 'contentTypes' => self::get_post_types(),
37 'contentStatus' => self::get_post_stati(),
38 'currentPageView' => self::get_current_view(),
39 'currentUser' => self::get_current_user_data(),
40 'dashboardApp' => array(
41 'adminActions' => self::get_admin_actions(),
42 ),
43 'defaultAppName' => 'fl-dashboard',
44 'nonce' => array(
45 'api' => wp_create_nonce( 'wp_rest' ),
46 'reply' => wp_create_nonce( 'replyto-comment' ),
47 'replyUnfiltered' => wp_create_nonce( 'unfiltered-html-comment' ),
48 'updates' => wp_create_nonce( 'updates' ),
49 ),
50 'pluginURL' => FL_ASSISTANT_URL,
51 'taxonomies' => self::get_taxonomies(),
52 'userRoles' => self::get_user_roles(),
53 );
54
55 /**
56 * This will hydrate the redux store. Each key must
57 * have a reducer. If it doesn't need one, consider
58 * adding it to the config.
59 */
60 $state = array(
61 'activeApp' => $user_state['activeApp'],
62 'appFrameSize' => $user_state['appFrameSize'],
63 'appOrder' => $user_state['appOrder'],
64 'counts' => self::get_counts(),
65 'isShowingUI' => $user_state['isShowingUI'],
66 'panelPosition' => $user_state['panelPosition'],
67 'shouldReduceMotion' => $user_state['shouldReduceMotion'],
68 );
69
70 return array(
71 'config' => $config,
72 'state' => $state,
73 );
74 }
75
76 /**
77 * Get post type slugs and names.
78 */
79 static public function get_post_types() {
80 $data = [];
81 $types = get_post_types(
82 array(
83 'public' => true,
84 ), 'objects'
85 );
86
87 foreach ( $types as $slug => $type ) {
88 if ( ! current_user_can( $type->cap->edit_published_posts ) ) {
89 continue;
90 }
91 if ( 'attachment' === $slug ) {
92 continue;
93 }
94 $data[ $slug ] = esc_html( $type->labels->name );
95 }
96
97 return $data;
98 }
99
100 /**
101 * Get post status slugs and names.
102 */
103 static public function get_post_stati() {
104 $data = [];
105 $stati = get_post_stati( array(), 'objects' );
106
107 foreach ( $stati as $slug => $status ) {
108 $data[ $slug ] = esc_html( $status->label );
109 }
110
111 return $data;
112 }
113
114 /**
115 * Checks to see if Beaver Builder can be used
116 * to edit a post.
117 */
118 static public function bb_can_edit_post( $post_id ) {
119 $editable = false;
120 $post = get_post( $post_id );
121 $post_types = FLBuilderModel::get_post_types();
122 $user_can = current_user_can( 'edit_post', $post->ID );
123 $user_access = FLBuilderUserAccess::current_user_can( 'builder_access' );
124
125 if ( in_array( $post->post_type, $post_types ) && $user_can && $user_access ) {
126 $editable = true;
127 }
128
129 return (bool) apply_filters( 'fl_builder_is_post_editable', $editable );
130 }
131
132 /**
133 * Get taxonomy slugs and names.
134 */
135 static public function get_taxonomies() {
136 $data = [];
137 $types = self::get_post_types();
138
139 foreach ( $types as $type_slug => $type_name ) {
140 $taxonomies = get_object_taxonomies( $type_slug, 'objects' );
141 foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
142 if ( ! $taxonomy->public || ! $taxonomy->show_ui || 'post_format' == $taxonomy_slug ) {
143 continue;
144 }
145 $data[ $taxonomy_slug ] = $taxonomy->label;
146 }
147 }
148
149 return $data;
150 }
151
152 /**
153 * Get info about the current page view.
154 */
155 static public function get_current_view() {
156 $data = [];
157 $actions = [];
158 $intro = __( 'Currently Viewing', 'fl-assistant' );
159 $name = __( 'Untitled', 'fl-assistant' );
160
161 $obj = get_queried_object();
162 $data['queried_object'] = $obj;
163
164 if ( is_404() ) {
165 $name = __( 'Page Not Found (404)', 'fl-assistant' );
166
167 } elseif ( is_search() ) {
168
169 $intro = __( 'Currently Viewing Search Results For', 'fl-assistant' );
170 $name = get_search_query();
171
172 } elseif ( is_post_type_archive() ) {
173
174 $post_type = get_post_type_object( 'post' );
175 $intro = __( 'Currently Viewing Post Type Archive', 'fl-assistant' );
176 $name = $post_type->labels->singular;
177
178 } elseif ( is_tax() || is_category() || is_tag() ) {
179
180 $tax = get_taxonomy( $obj->taxonomy );
181 $labels = $tax->labels;
182
183 $intro = sprintf( esc_html__( 'Currently Viewing %s', 'fl-assistant' ), $labels->singular_name );
184 $name = $obj->name;
185
186 $actions[] = [
187 'label' => $labels->edit_item,
188 'href' => get_edit_term_link( $obj->term_id, $obj->taxonomy, null ),
189 'capability' => 'manage_categories',
190 ];
191
192 } elseif ( is_singular() || is_attachment() ) {
193
194 $post_type = get_post_type_object( get_post_type() );
195 $labels = $post_type->labels;
196 $post_type = $labels->singular_name;
197 $intro = sprintf( esc_html__( 'Currently Viewing %s', 'fl-assistant' ), $post_type );
198 $name = $obj->post_title;
199
200 if ( is_attachment() ) {
201 $meta = wp_get_attachment_metadata( $obj->ID );
202 $name = basename( $meta['file'] );
203 }
204
205 $actions[] = [
206 'label' => $labels->edit_item,
207 'href' => get_edit_post_link( $obj->ID, '' ),
208 'capability' => 'edit_pages',
209 ];
210
211 // Add Beaver Builder edit link
212 global $wp_the_query;
213 if ( class_exists( 'FLBuilderModel' ) ) {
214 if ( FLBuilderModel::is_post_editable() && is_object( $wp_the_query->post ) ) {
215
216 $enabled = get_post_meta( $wp_the_query->post->ID, '_fl_builder_enabled', true );
217
218 $actions[] = [
219 'label' => FLBuilderModel::get_branding(),
220 'href' => FLBuilderModel::get_edit_url( $wp_the_query->post->ID ),
221 'capability' => 'edit_pages',
222 'isEnabled' => $enabled,
223 ];
224 }
225 }
226 } elseif ( is_author() ) {
227
228 $intro = __( 'Currently Viewing Author', 'fl-assistant' );
229 $name = wp_get_current_user()->display_name;
230
231 } elseif ( is_front_page() ) {
232 $intro = __( 'Currently Viewing Post Archive', 'fl-assistant' );
233 $name = __( 'Latest Posts', 'fl-assistant' );
234 }
235
236 $data['intro'] = $intro;
237 $data['name'] = $name;
238 $data['actions'] = self::filter_actions_by_capability( $actions );
239
240 $theme = wp_get_theme();
241 $data['theme'] = [
242 'name' => $theme->get( 'Name' ),
243 'team' => $theme->get( 'Author' ),
244 'screenshot' => $theme->get_screenshot(),
245 'version' => $theme->get( 'Version' ),
246 ];
247
248 return $data;
249 }
250
251 /**
252 * Filter an array of actions by their capability
253 */
254 static public function filter_actions_by_capability( $actions = [], $exclude_unset = true ) {
255
256 foreach ( $actions as $i => $action ) {
257 $defaults = [
258 'label' => '',
259 'capability' => '',
260 ];
261 $action = wp_parse_args( $action, $defaults );
262 $cap = $action['capability'];
263
264 // Remove actions without a capability set
265 if ( $exclude_unset && ( '' === $cap || empty( $cap ) ) ) {
266 unset( $actions[ $i ] );
267 }
268 // Test capability
269 if ( is_string( $cap ) && ! current_user_can( $cap ) ) {
270 unset( $actions[ $i ] );
271 }
272
273 // Test array of capabilities
274 if ( is_array( $cap ) ) {
275 foreach ( $cap as $single_cap ) {
276 if ( ! current_user_can( $single_cap ) ) {
277 unset( $actions[ $i ] );
278 }
279 }
280 }
281 }
282
283 return $actions;
284 }
285
286 /**
287 * Get an action set for allowed admin links.
288 */
289 static public function get_admin_actions() {
290 $actions = [];
291 $customize_url = self::get_customize_url();
292
293 // Customize Link
294 if ( $customize_url ) {
295 $actions[] = [
296 'label' => __( 'Customize' ),
297 'href' => $customize_url,
298 'capability' => 'customize',
299 ];
300 }
301
302 // Your User Profile Link
303 $user_id = get_current_user_id();
304
305 if ( current_user_can( 'read' ) ) {
306 $profile_url = get_edit_profile_url( $user_id );
307 }
308 $actions[] = [
309 'label' => __( 'Your Profile' ),
310 'href' => $profile_url,
311 'capability' => 'read',
312 ];
313
314 // About Link
315 if ( current_user_can( 'read' ) ) {
316 $about_url = self_admin_url( 'about.php' );
317 }
318 $actions[] = [
319 'label' => __( 'About WordPress' ),
320 'href' => $about_url,
321 'capability' => 'read',
322 ];
323
324 return self::filter_actions_by_capability( $actions );
325 }
326
327 /**
328 * Get customize url
329 */
330 static public function get_customize_url() {
331 global $wp_customize;
332
333 // Don't show for users who can't access the customizer or when in the admin.
334 if ( ! current_user_can( 'customize' ) || is_admin() ) {
335 return;
336 }
337
338 // Don't show if the user cannot edit a given customize_changeset post currently being previewed.
339 if ( is_customize_preview() && $wp_customize->changeset_post_id() && ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->edit_post, $wp_customize->changeset_post_id() ) ) {
340 return;
341 }
342
343 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
344 if ( is_customize_preview() && $wp_customize->changeset_uuid() ) {
345 $current_url = remove_query_arg( 'customize_changeset_uuid', $current_url );
346 }
347
348 $customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
349 if ( is_customize_preview() ) {
350 $customize_url = add_query_arg( array( 'changeset_uuid' => $wp_customize->changeset_uuid() ), $customize_url );
351 }
352 return $customize_url;
353 }
354
355 /**
356 * Get the saved state for a user.
357 */
358 static public function get_user_state( $id ) {
359 $saved = get_user_meta( $id, 'fl_assistant_state', true );
360
361 return array_merge(
362 self::$default_user_state,
363 $saved ? (array) $saved : array()
364 );
365 }
366
367 /**
368 * Update the saved state for a user.
369 */
370 static public function update_user_state( $id, $state ) {
371 $saved = self::get_user_state( $id );
372
373 update_user_meta(
374 $id, 'fl_assistant_state', array_merge(
375 $saved,
376 $state ? (array) $state : array()
377 )
378 );
379 }
380
381 /**
382 * Get the saved state for the current user.
383 */
384 static public function get_current_user_state() {
385 return self::get_user_state( wp_get_current_user()->ID );
386 }
387
388 /**
389 * Get info about the current user.
390 */
391 static public function get_current_user_data() {
392 $user = wp_get_current_user();
393
394 return array(
395 'id' => $user->ID,
396 'name' => $user->display_name,
397 'capabilities' => $user->allcaps,
398 );
399 }
400
401 /**
402 * Get all user roles for the site.
403 */
404 static public function get_user_roles() {
405 if ( ! function_exists( 'get_editable_roles' ) ) {
406 require_once( ABSPATH . 'wp-admin/includes/user.php' );
407 }
408
409 $data = [];
410 $roles = get_editable_roles();
411
412 foreach ( $roles as $key => $role ) {
413 $data[] = [
414 'key' => $key,
415 'name' => $role['name'],
416 ];
417 }
418
419 return $data;
420 }
421
422 /**
423 * Returns an array of all counts to hydrate the store.
424 */
425 static public function get_counts() {
426 $request = new WP_REST_Request( 'GET', '/fl-assistant/v1/counts' );
427 $response = rest_do_request( $request );
428 return $response->get_data();
429 }
430 }
431