PluginProbe
Assistant – Every Day Productivity Apps / 0.6.0
Assistant – Every Day Productivity Apps v0.6.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 / backend / src / Data / Site.php

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

269 lines 7.4 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
22 if ( is_admin() ) {
23
24 $intro = __( 'Currently Viewing Admin Page', 'fl-assistant' );
25 $type = __( 'Admin Page', 'fl-assistant' );
26 $screen = get_current_screen();
27 $name = $screen->id;
28
29 } else {
30
31 if ( is_404() ) {
32 $name = __( 'Page Not Found', 'fl-assistant' );
33 $type = __( '404', 'fl-assistant' );
34
35 } elseif ( is_search() ) {
36
37 $intro = __( 'Currently Viewing Search Results For', 'fl-assistant' );
38 $type = __( 'Search Results For:', 'fl-assistant' );
39 $name = get_search_query();
40
41 } elseif ( is_date() ) {
42
43 $intro = __( 'Currently Viewing Date Archive', 'fl-assistant' );
44 $type = __( 'Date Archive', 'fl-assistant' );
45 $name = get_the_date();
46
47 } elseif ( is_post_type_archive() ) {
48
49 $post_type = get_post_type_object( 'post' );
50 $intro = __( 'Currently Viewing Post Type Archive', 'fl-assistant' );
51 $type = __( 'Post Type Archive', 'fl-assistant' );
52 $name = $post_type->labels->singular_name;
53
54 } elseif ( is_tax() || is_category() || is_tag() ) {
55
56 $tax = get_taxonomy( $obj->taxonomy );
57 $labels = $tax->labels;
58
59 $intro = sprintf( esc_html__( 'Currently Viewing %s', 'fl-assistant' ), $labels->singular_name );
60 $type = $labels->singular_name;
61 $name = $obj->name;
62
63 $actions[] = [
64 'label' => $labels->edit_item,
65 'href' => get_edit_term_link( $obj->term_id, $obj->taxonomy, null ),
66 'capability' => 'manage_categories',
67 ];
68
69 } elseif ( is_singular() || is_attachment() ) {
70
71 $post_type = get_post_type_object( get_post_type() );
72 $labels = $post_type->labels;
73 $post_type = $labels->singular_name;
74 $intro = sprintf( esc_html__( 'Currently Viewing %s', 'fl-assistant' ), $post_type );
75 $type = $post_type;
76 $name = $obj->post_title;
77
78 if ( is_attachment() ) {
79 $meta = wp_get_attachment_metadata( $obj->ID );
80 $name = basename( $meta['file'] );
81 }
82
83 $actions[] = [
84 'label' => $labels->edit_item,
85 'href' => get_edit_post_link( $obj->ID, '' ),
86 'capability' => 'edit_pages',
87 ];
88
89 // Add Beaver Builder edit link
90 global $wp_the_query;
91 if ( class_exists( '\FLBuilderModel' ) ) {
92 if ( \FLBuilderModel::is_post_editable() && is_object( $wp_the_query->post ) ) {
93
94 $enabled = get_post_meta( $wp_the_query->post->ID, '_fl_builder_enabled', true );
95
96 $actions[] = [
97 'label' => \FLBuilderModel::get_branding(),
98 'href' => \FLBuilderModel::get_edit_url( $wp_the_query->post->ID ),
99 'capability' => 'edit_pages',
100 'isEnabled' => $enabled,
101 ];
102 }
103 }
104 } elseif ( is_author() ) {
105
106 $intro = __( 'Currently Viewing Author', 'fl-assistant' );
107 $type = __( 'Author Archive', 'fl-assistant' );
108 $name = wp_get_current_user()->display_name;
109
110 } elseif ( is_front_page() ) {
111 $intro = __( 'Currently Viewing Post Archive', 'fl-assistant' );
112 $type = __( 'Post Archive', 'fl-assistant' );
113 $name = __( 'Latest Posts', 'fl-assistant' );
114 }
115 }
116
117 $data['intro'] = $intro;
118 $data['name'] = $name;
119 $data['type'] = $type;
120
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 'defaultIcon' => $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 $data['isAdmin'] = is_admin();
141 $data['is404'] = is_404();
142 $data['isSearch'] = is_search();
143 $data['isDate'] = is_date();
144 $data['isPostTypeArchive'] = is_post_type_archive();
145 $data['isSingular'] = is_singular();
146 $data['isAttachment'] = is_attachment();
147 $data['isCategory'] = is_tax();
148 $data['isCategory'] = is_category();
149 $data['isTag'] = is_tag();
150 $data['isAuthor'] = is_author();
151 $data['isFrontPage'] = is_front_page();
152
153 if ( is_singular() ) {
154 $data['id'] = $obj->ID;
155 }
156
157 return $data;
158 }
159
160 /**
161 * Filter an array of actions by their capability
162 */
163 public function filter_actions_by_capability( $actions = [], $exclude_unset = true ) {
164
165 foreach ( $actions as $i => $action ) {
166 $defaults = [
167 'label' => '',
168 'capability' => '',
169 ];
170 $action = wp_parse_args( $action, $defaults );
171 $cap = $action['capability'];
172
173 // Remove actions without a capability set
174 if ( $exclude_unset && ( '' === $cap || empty( $cap ) ) ) {
175 unset( $actions[ $i ] );
176 }
177 // Test capability
178 if ( is_string( $cap ) && ! current_user_can( $cap ) ) {
179 unset( $actions[ $i ] );
180 }
181
182 // Test array of capabilities
183 if ( is_array( $cap ) ) {
184 foreach ( $cap as $single_cap ) {
185 if ( ! current_user_can( $single_cap ) ) {
186 unset( $actions[ $i ] );
187 }
188 }
189 }
190 }
191
192 return $actions;
193 }
194
195 /**
196 * Get an action set for allowed admin links.
197 */
198 public function get_admin_urls() {
199 $urls = [];
200
201 $urls['dashboard'] = admin_url();
202
203 $urls['createPost'] = admin_url( 'post-new.php' );
204 $urls['editPost'] = admin_url( 'post.php?action=edit&post=' );
205
206 $url = static::get_customize_url();
207 if ( $url ) {
208 $urls['customize'] = $url;
209 }
210
211 if ( current_user_can( 'switch_themes' ) ) {
212 $urls['switchThemes'] = '';
213 }
214
215 // Your User Profile
216 $user_id = get_current_user_id();
217 if ( current_user_can( 'read' ) ) {
218 $urls['userProfile'] = get_edit_profile_url( $user_id );
219 }
220
221 if ( current_user_can( 'create_users' ) ) {
222 $urls['createUser'] = admin_url( 'user-new.php' );
223 }
224
225 return $urls;
226 }
227
228 /**
229 * Get customize url
230 */
231 public function get_customize_url() {
232 global $wp_customize;
233
234 // Don't show for users who can't access the customizer or when in the admin.
235 if ( ! current_user_can( 'customize' ) || is_admin() ) {
236 return;
237 }
238
239 // Don't show if the user cannot edit a given customize_changeset post currently being previewed.
240 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() ) ) {
241 return;
242 }
243
244 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
245 if ( is_customize_preview() && $wp_customize->changeset_uuid() ) {
246 $current_url = remove_query_arg( 'customize_changeset_uuid', $current_url );
247 }
248
249 $customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
250 if ( is_customize_preview() ) {
251 $customize_url = add_query_arg( [ 'changeset_uuid' => $wp_customize->changeset_uuid() ], $customize_url );
252 }
253
254 return $customize_url;
255 }
256
257 /**
258 * Check if the site is on localhost or not.
259 */
260 public function is_local() {
261 $ips = [ '127.0.0.1', '::1' ];
262 $domain = '.local';
263 if ( ! in_array( $_SERVER['REMOTE_ADDR'], $ips, true ) ) {
264 return substr_compare( $_SERVER['SERVER_NAME'], $domain, -strlen( $domain ) ) === 0;
265 }
266 return true;
267 }
268 }
269