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

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