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

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

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