PluginProbe
Assistant – Every Day Productivity Apps / trunk
Assistant – Every Day Productivity Apps vtrunk
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 trunk, at backend/src/Data/Site.php

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