-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathAuthorize_Application.php
More file actions
126 lines (110 loc) · 3.18 KB
/
Copy pathAuthorize_Application.php
File metadata and controls
126 lines (110 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Class Google\Site_Kit\Core\Admin\Authorize_Application
*
* @package Google\Site_Kit
* @copyright 2024 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\Site_Kit\Core\Admin;
use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Assets\Assets;
use Google\Site_Kit\Core\Util\Current_Screen;
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
/**
* Class to handle all wp-admin Authorize Application related functionality.
*
* @since 1.126.0
* @access private
* @ignore
*/
final class Authorize_Application {
use Method_Proxy_Trait;
/**
* Plugin context.
*
* @since 1.126.0
* @var Context
*/
private $context;
/**
* Assets instance.
*
* @since 1.126.0
* @var Assets
*/
private $assets;
/**
* Constructor.
*
* @since 1.126.0
*
* @param Context $context Plugin context.
* @param Assets $assets Optional. Assets API instance. Default is a new instance.
*/
public function __construct(
Context $context,
?Assets $assets = null
) {
$this->context = $context;
$this->assets = $assets ?: new Assets( $this->context );
}
/**
* Registers functionality through WordPress hooks.
*
* @since 1.126.0
*/
public function register() {
add_action( 'admin_enqueue_scripts', $this->get_method_proxy( 'enqueue_assets' ) );
add_action( 'admin_footer', $this->get_method_proxy( 'render_custom_footer' ) );
}
/**
* Checks if the current screen is the Authorize Application screen.
*
* @since 1.126.0
*
* @return bool True if the current screen is the Authorize Application screen, false otherwise.
*/
protected function is_authorize_application_screen() {
$current_screen = Current_Screen::get();
return null !== $current_screen && 'authorize-application' === $current_screen->id;
}
/**
* Checks if the current service is a Google service.
*
* @since 1.126.0
*
* @return bool True if the current service is a Google service, false otherwise.
*/
protected function is_google_service() {
$success_url = isset( $_GET['success_url'] ) ? esc_url_raw( wp_unslash( $_GET['success_url'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
$success_url = sanitize_text_field( $success_url );
$parsed_url = wp_parse_url( $success_url );
if ( empty( $parsed_url['host'] ) ) {
return false;
}
// Check if the domain is a '*.google.com' domain.
return preg_match( '/\.google\.com$/', $parsed_url['host'] ) === 1;
}
/**
* Enqueues assets for the Authorize Application screen.
*
* @since 1.126.0
*/
private function enqueue_assets() {
if ( $this->is_authorize_application_screen() && $this->is_google_service() ) {
$this->assets->enqueue_asset( 'googlesitekit-authorize-application-css' );
}
}
/**
* Renders custom footer for the Authorize Application screen if the service is a Google service.
*
* @since 1.126.0
*/
private function render_custom_footer() {
if ( $this->is_authorize_application_screen() && $this->is_google_service() ) {
echo '<div class="googlesitekit-authorize-application__footer"><p>' . esc_html__( 'Powered by Site Kit', 'google-site-kit' ) . '</p></div>';
}
}
}