PluginProbe
Assistant – Every Day Productivity Apps / 0.3.1
Assistant – Every Day Productivity Apps v0.3.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 / Data / Site.php

Site.php in Assistant – Every Day Productivity Apps 0.3.1, at backend/src/Data/Site.php

239 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace FL\Assistant\Data;
5
6
7 class Site {
8
9
10 /**
11 * Get info about the current page view.
12 */
13 public function get_current_view() {
14 $data = [];
15 $actions = [];
16 $intro = __( 'Currently Viewing', 'fl-assistant' );
17 $type = '';
18 $name = __( 'Untitled', 'fl-assistant' );
19
20 $obj = get_queried_object();
21 $data['queried_object'] = $obj;
22
23 if ( is_admin() ) {
24
25 $intro = __( 'Currently Viewing Admin Page', 'fl-assistant' );
26 $type = __( 'Admin Page', 'fl-assistant' );
27 $screen = get_current_screen();
28 $name = $screen->id;
29
30 } else {
31
32 if ( is_404() ) {
33 $name = __( 'Page Not Found', 'fl-assistant' );
34 $type = __( '404', 'fl-assistant' );
35
36 } elseif ( is_search() ) {
37
38 $intro = __( 'Currently Viewing Search Results For', 'fl-assistant' );
39 $type = __( 'Search Results For:', 'fl-assistant' );
40 $name = get_search_query();
41
42 } elseif ( is_date() ) {
43
44 $intro = __( 'Currently Viewing Date Archive', 'fl-assistant' );
45 $type = __( 'Date Archive', 'fl-assistant' );
46 $name = get_the_date();
47
48 } elseif ( is_post_type_archive() ) {
49
50 $post_type = get_post_type_object( 'post' );
51 $intro = __( 'Currently Viewing Post Type Archive', 'fl-assistant' );
52 $type = __( 'Post Type Archive', 'fl-assistant' );
53 $name = $post_type->labels->singular_name;
54
55 } elseif ( is_tax() || is_category() || is_tag() ) {
56
57 $tax = get_taxonomy( $obj->taxonomy );
58 $labels = $tax->labels;
59
60 $intro = sprintf( esc_html__( 'Currently Viewing %s', 'fl-assistant' ), $labels->singular_name );
61 $type = $labels->singular_name;
62 $name = $obj->name;
63
64 $actions[] = [
65 'label' => $labels->edit_item,
66 'href' => get_edit_term_link( $obj->term_id, $obj->taxonomy, null ),
67 'capability' => 'manage_categories',
68 ];
69
70 } elseif ( is_singular() || is_attachment() ) {
71
72 $post_type = get_post_type_object( get_post_type() );
73 $labels = $post_type->labels;
74 $post_type = $labels->singular_name;
75 $intro = sprintf( esc_html__( 'Currently Viewing %s', 'fl-assistant' ), $post_type );
76 $type = $post_type;
77 $name = $obj->post_title;
78
79 if ( is_attachment() ) {
80 $meta = wp_get_attachment_metadata( $obj->ID );
81 $name = basename( $meta['file'] );
82 }
83
84 $actions[] = [
85 'label' => $labels->edit_item,
86 'href' => get_edit_post_link( $obj->ID, '' ),
87 'capability' => 'edit_pages',
88 ];
89
90 // Add Beaver Builder edit link
91 global $wp_the_query;
92 if ( class_exists( '\FLBuilderModel' ) ) {
93 if ( \FLBuilderModel::is_post_editable() && is_object( $wp_the_query->post ) ) {
94
95 $enabled = get_post_meta( $wp_the_query->post->ID, '_fl_builder_enabled', true );
96
97 $actions[] = [
98 'label' => \FLBuilderModel::get_branding(),
99 'href' => \FLBuilderModel::get_edit_url( $wp_the_query->post->ID ),
100 'capability' => 'edit_pages',
101 'isEnabled' => $enabled,
102 ];
103 }
104 }
105 } elseif ( is_author() ) {
106
107 $intro = __( 'Currently Viewing Author', 'fl-assistant' );
108 $type = __( 'Author Archive', 'fl-assistant' );
109 $name = wp_get_current_user()->display_name;
110
111 } elseif ( is_front_page() ) {
112 $intro = __( 'Currently Viewing Post Archive', 'fl-assistant' );
113 $type = __( 'Post Archive', 'fl-assistant' );
114 $name = __( 'Latest Posts', 'fl-assistant' );
115 }
116 }
117
118 $data['intro'] = $intro;
119 $data['name'] = $name;
120 $data['type'] = $type;
121 $data['actions'] = $this->filter_actions_by_capability( $actions );
122
123 $theme = wp_get_theme();
124 $data['theme'] = [
125 'name' => $theme->get( 'Name' ),
126 'team' => $theme->get( 'Author' ),
127 'screenshot' => $theme->get_screenshot(),
128 'version' => $theme->get( 'Version' ),
129 ];
130
131 $default_icon = plugins_url('img/icon-128x128.jpg', FL_ASSISTANT_FILE );
132 $data['site'] = [
133 'test' => $default_icon,
134 'icon' => get_site_icon_url( 120, $default_icon ),
135 'title' => get_bloginfo('name'),
136 'description' => get_bloginfo('description'),
137 'homeURL' => home_url(),
138 ];
139
140 return $data;
141 }
142
143 /**
144 * Filter an array of actions by their capability
145 */
146 public function filter_actions_by_capability( $actions = [], $exclude_unset = true ) {
147
148 foreach ( $actions as $i => $action ) {
149 $defaults = [
150 'label' => '',
151 'capability' => '',
152 ];
153 $action = wp_parse_args( $action, $defaults );
154 $cap = $action['capability'];
155
156 // Remove actions without a capability set
157 if ( $exclude_unset && ( '' === $cap || empty( $cap ) ) ) {
158 unset( $actions[ $i ] );
159 }
160 // Test capability
161 if ( is_string( $cap ) && ! current_user_can( $cap ) ) {
162 unset( $actions[ $i ] );
163 }
164
165 // Test array of capabilities
166 if ( is_array( $cap ) ) {
167 foreach ( $cap as $single_cap ) {
168 if ( ! current_user_can( $single_cap ) ) {
169 unset( $actions[ $i ] );
170 }
171 }
172 }
173 }
174
175 return $actions;
176 }
177
178 /**
179 * Get an action set for allowed admin links.
180 */
181 public function get_admin_urls() {
182 $urls = [];
183
184 $urls['dashboard'] = admin_url();
185
186 $urls['createPost'] = admin_url( 'post-new.php' );
187
188 $url = static::get_customize_url();
189 if ( $url ) {
190 $urls['customize'] = $url;
191 }
192
193 if ( current_user_can( 'switch_themes' ) ) {
194 $urls['switchThemes'] = '';
195 }
196
197 // Your User Profile
198 $user_id = get_current_user_id();
199 if ( current_user_can( 'read' ) ) {
200 $urls['userProfile'] = get_edit_profile_url( $user_id );
201 }
202
203 if ( current_user_can( 'create_users' ) ) {
204 $urls['createUser'] = admin_url( 'user-new.php' );
205 }
206
207 return $urls;
208 }
209
210 /**
211 * Get customize url
212 */
213 public function get_customize_url() {
214 global $wp_customize;
215
216 // Don't show for users who can't access the customizer or when in the admin.
217 if ( ! current_user_can( 'customize' ) || is_admin() ) {
218 return;
219 }
220
221 // Don't show if the user cannot edit a given customize_changeset post currently being previewed.
222 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() ) ) {
223 return;
224 }
225
226 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
227 if ( is_customize_preview() && $wp_customize->changeset_uuid() ) {
228 $current_url = remove_query_arg( 'customize_changeset_uuid', $current_url );
229 }
230
231 $customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
232 if ( is_customize_preview() ) {
233 $customize_url = add_query_arg( [ 'changeset_uuid' => $wp_customize->changeset_uuid() ], $customize_url );
234 }
235
236 return $customize_url;
237 }
238 }
239