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

444 lines 12.0 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 ) && $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_post_type_archive() ) {
194
195 $post_type = get_post_type_object( 'post' );
196 $intro = __( 'Currently Viewing Post Type Archive', 'fl-assistant' );
197 $name = $post_type->labels->singular_name;
198
199 } elseif ( is_tax() || is_category() || is_tag() ) {
200
201 $tax = get_taxonomy( $obj->taxonomy );
202 $labels = $tax->labels;
203
204 $intro = sprintf( esc_html__( 'Currently Viewing %s', 'fl-assistant' ), $labels->singular_name );
205 $name = $obj->name;
206
207 $actions[] = [
208 'label' => $labels->edit_item,
209 'href' => get_edit_term_link( $obj->term_id, $obj->taxonomy, null ),
210 'capability' => 'manage_categories',
211 ];
212
213 } elseif ( is_singular() || is_attachment() ) {
214
215 $post_type = get_post_type_object( get_post_type() );
216 $labels = $post_type->labels;
217 $post_type = $labels->singular_name;
218 $intro = sprintf( esc_html__( 'Currently Viewing %s', 'fl-assistant' ), $post_type );
219 $name = $obj->post_title;
220
221 if ( is_attachment() ) {
222 $meta = wp_get_attachment_metadata( $obj->ID );
223 $name = basename( $meta['file'] );
224 }
225
226 $actions[] = [
227 'label' => $labels->edit_item,
228 'href' => get_edit_post_link( $obj->ID, '' ),
229 'capability' => 'edit_pages',
230 ];
231
232 // Add Beaver Builder edit link
233 global $wp_the_query;
234 if ( class_exists( 'FLBuilderModel' ) ) {
235 if ( FLBuilderModel::is_post_editable() && is_object( $wp_the_query->post ) ) {
236
237 $enabled = get_post_meta( $wp_the_query->post->ID, '_fl_builder_enabled', true );
238
239 $actions[] = [
240 'label' => FLBuilderModel::get_branding(),
241 'href' => FLBuilderModel::get_edit_url( $wp_the_query->post->ID ),
242 'capability' => 'edit_pages',
243 'isEnabled' => $enabled,
244 ];
245 }
246 }
247 } elseif ( is_author() ) {
248
249 $intro = __( 'Currently Viewing Author', 'fl-assistant' );
250 $name = wp_get_current_user()->display_name;
251
252 } elseif ( is_front_page() ) {
253 $intro = __( 'Currently Viewing Post Archive', 'fl-assistant' );
254 $name = __( 'Latest Posts', 'fl-assistant' );
255 }
256
257 $data['intro'] = $intro;
258 $data['name'] = $name;
259 $data['actions'] = self::filter_actions_by_capability( $actions );
260
261 $theme = wp_get_theme();
262 $data['theme'] = [
263 'name' => $theme->get( 'Name' ),
264 'team' => $theme->get( 'Author' ),
265 'screenshot' => $theme->get_screenshot(),
266 'version' => $theme->get( 'Version' ),
267 ];
268
269 return $data;
270 }
271
272 /**
273 * Filter an array of actions by their capability
274 */
275 static public function filter_actions_by_capability( $actions = [], $exclude_unset = true ) {
276
277 foreach ( $actions as $i => $action ) {
278 $defaults = [
279 'label' => '',
280 'capability' => '',
281 ];
282 $action = wp_parse_args( $action, $defaults );
283 $cap = $action['capability'];
284
285 // Remove actions without a capability set
286 if ( $exclude_unset && ( '' === $cap || empty( $cap ) ) ) {
287 unset( $actions[ $i ] );
288 }
289 // Test capability
290 if ( is_string( $cap ) && ! current_user_can( $cap ) ) {
291 unset( $actions[ $i ] );
292 }
293
294 // Test array of capabilities
295 if ( is_array( $cap ) ) {
296 foreach ( $cap as $single_cap ) {
297 if ( ! current_user_can( $single_cap ) ) {
298 unset( $actions[ $i ] );
299 }
300 }
301 }
302 }
303
304 return $actions;
305 }
306
307 /**
308 * Get an action set for allowed admin links.
309 */
310 static public function get_admin_urls() {
311 $urls = [];
312
313 $urls['createPost'] = admin_url( 'post-new.php' );
314
315 $url = self::get_customize_url();
316 if ( $url ) {
317 $urls['customize'] = $url;
318 }
319
320 if ( current_user_can( 'switch_themes' ) ) {
321 $urls['switchThemes'] = '';
322 }
323
324 // Your User Profile
325 $user_id = get_current_user_id();
326 if ( current_user_can( 'read' ) ) {
327 $urls['userProfile'] = get_edit_profile_url( $user_id );
328 }
329
330 if ( current_user_can( 'create_users' ) ) {
331 $urls['createUser'] = admin_url( 'user-new.php' );
332 }
333
334 return $urls;
335 }
336
337 /**
338 * Get customize url
339 */
340 static public function get_customize_url() {
341 global $wp_customize;
342
343 // Don't show for users who can't access the customizer or when in the admin.
344 if ( ! current_user_can( 'customize' ) || is_admin() ) {
345 return;
346 }
347
348 // Don't show if the user cannot edit a given customize_changeset post currently being previewed.
349 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() ) ) {
350 return;
351 }
352
353 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
354 if ( is_customize_preview() && $wp_customize->changeset_uuid() ) {
355 $current_url = remove_query_arg( 'customize_changeset_uuid', $current_url );
356 }
357
358 $customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
359 if ( is_customize_preview() ) {
360 $customize_url = add_query_arg( array( 'changeset_uuid' => $wp_customize->changeset_uuid() ), $customize_url );
361 }
362 return $customize_url;
363 }
364
365 /**
366 * Get the saved state for a user.
367 */
368 static public function get_user_state( $id ) {
369 $saved = get_user_meta( $id, 'fl_assistant_state', true );
370
371 return array_merge(
372 self::$default_user_state,
373 $saved ? (array) $saved : array()
374 );
375 }
376
377 /**
378 * Update the saved state for a user.
379 */
380 static public function update_user_state( $id, $state ) {
381 $saved = self::get_user_state( $id );
382
383 update_user_meta(
384 $id, 'fl_assistant_state', array_merge(
385 $saved,
386 $state ? (array) $state : array()
387 )
388 );
389 }
390
391 /**
392 * Get the saved state for the current user.
393 */
394 static public function get_current_user_state() {
395 return self::get_user_state( wp_get_current_user()->ID );
396 }
397
398 /**
399 * Get info about the current user.
400 */
401 static public function get_current_user_data() {
402 $user = wp_get_current_user();
403 $request = new WP_REST_Request( 'GET', '/fl-assistant/v1/user/' . $user->ID );
404 $response = rest_do_request( $request );
405
406 return array_merge(
407 $response->get_data(),
408 array(
409 'capabilities' => $user->allcaps,
410 )
411 );
412 }
413
414 /**
415 * Get all user roles for the site.
416 */
417 static public function get_user_roles() {
418 if ( ! function_exists( 'get_editable_roles' ) ) {
419 require_once( ABSPATH . 'wp-admin/includes/user.php' );
420 }
421
422 $data = [];
423 $roles = get_editable_roles();
424
425 foreach ( $roles as $key => $role ) {
426 $data[] = [
427 'key' => $key,
428 'name' => $role['name'],
429 ];
430 }
431
432 return $data;
433 }
434
435 /**
436 * Returns an array of all counts to hydrate the store.
437 */
438 static public function get_counts() {
439 $request = new WP_REST_Request( 'GET', '/fl-assistant/v1/counts' );
440 $response = rest_do_request( $request );
441 return $response->get_data();
442 }
443 }
444