PluginProbe ʕ •ᴥ•ʔ
XML Sitemap & Google News / trunk
XML Sitemap & Google News vtrunk
5.7.7 5.7.5 5.7.6 5.7.4 trunk 1.0 2.0 2.1 3.0 3.2 3.3 3.4 3.5 3.6.1 3.7.4 3.8.8 3.9.3 4.0.1 4.1.4 4.2.3 4.3.2 4.4.1 4.5.1 4.6.3 4.7.5 4.8.3 4.9.4 5.0.7 5.1.2 5.2.7 5.3.6 5.4.6 5.4.9 5.5.1 5.5.2 5.5.3 5.5.4 5.5.5 5.5.6 5.5.7 5.5.8 5.5.9 5.6 5.6.1 5.6.2 5.6.3 5.7 5.7.1 5.7.2 5.7.3
xml-sitemap-feed / inc / admin / class-gsc-connect-settings.php
xml-sitemap-feed / inc / admin Last commit date
class-bwt-connect-settings.php 7 months ago class-bwt-connect.php 4 months ago class-gsc-connect-settings.php 7 months ago class-gsc-connect.php 4 months ago class-main.php 7 months ago class-sanitize.php 7 months ago class-sitemap-fields.php 4 months ago class-sitemap-news-fields.php 4 months ago class-sitemap-news-settings.php 2 months ago class-sitemap-news.php 7 months ago class-sitemap-settings.php 4 months ago class-sitemap.php 4 months ago
class-gsc-connect-settings.php
167 lines
1 <?php
2 /**
3 * Status301 Premium Google Search Console Connection Onboarding
4 *
5 * @package XML Sitemap & Google News
6 */
7
8 namespace XMLSF\Admin;
9
10 use XMLSF\Secret;
11
12 /**
13 * Helper class with public methods to set up a Google Search Console connection.
14 *
15 * @author RavanH
16 * @version 5.6
17 */
18 class GSC_Connect_Settings extends GSC_Connect {
19 /**
20 * Placeholder for the saved password.
21 * This is used to avoid showing the actual password in the settings page.
22 * If this value is present, it means the password is saved and should not be changed.
23 *
24 * @var string
25 */
26 public static $pw_placeholder = 'DONT_BOTHER_CLIENT_SECRET_ENCRYPTED';
27
28 /**
29 * Display the plugin options page HTML.
30 */
31 public static function options_page_render() {
32 // Check user capabilities.
33 if ( ! \current_user_can( 'manage_options' ) ) {
34 return;
35 }
36 ?>
37 <div class="wrap">
38 <h1><?php echo \esc_html( get_admin_page_title() ); ?></h1>
39
40 <form action="options.php" method="post">
41 <?php
42 // Output security fields for the registered setting section.
43 \settings_fields( self::$option_group );
44
45 // Output setting sections and fields.
46 \do_settings_sections( self::$page_slug );
47
48 // Output save settings button.
49 \submit_button();
50 ?>
51 </form>
52 </div>
53 <?php
54 }
55
56 /**
57 * Callback function for the Google Search Console OAuth Settings section header.
58 */
59 public static function oauth_section_callback() {
60 // Set referrer transient to redirect to after connection.
61 isset( $_GET['ref'] ) && in_array( $_GET['ref'], array( 'xmlsf', 'xmlsf_news' ), true ) && \set_transient( 'gsc_connect_origin', \sanitize_key( $_GET['ref'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
62
63 // Prepare the option if it does not already exist. Sets it as non-autoloaded option.
64 \add_option( self::$option_group, '', '', false );
65
66 // Get existing data from DB.
67 $options = (array) \get_option( self::$option_group, array() );
68
69 \settings_errors();
70
71 // Intro.
72 include XMLSF_DIR . '/views/admin/section-gsc-oauth-intro.php';
73
74 // Check if the Google Client ID and Secret are set.
75 if ( empty( $options['google_client_id'] ) || empty( $options['google_client_secret'] ) ) {
76 // Stage 1.
77 include XMLSF_DIR . '/views/admin/section-gsc-oauth-stage-1-2.php';
78 } else {
79 $redirect_uri = \site_url( 'index.php?' . self::$query_var );
80 $oauth_url = \add_query_arg(
81 array(
82 'client_id' => $options['google_client_id'],
83 'redirect_uri' => \rawurlencode( $redirect_uri ),
84 'scope' => \rawurlencode( 'https://www.googleapis.com/auth/webmasters' ),
85 'response_type' => 'code',
86 'access_type' => 'offline', // Request a refresh token.
87 'prompt' => 'consent', // Ensure consent screen is shown.
88 ),
89 'https://accounts.google.com/o/oauth2/auth'
90 );
91
92 // Stage 2.
93 include XMLSF_DIR . '/views/admin/section-gsc-oauth-stage-3.php';
94 }
95 }
96
97 /**
98 * Render the text field for the Google Client ID.
99 */
100 public static function google_client_id_render() {
101 $options = (array) \get_option( self::$option_group, array() );
102 $client_id = isset( $options['google_client_id'] ) ? \sanitize_text_field( $options['google_client_id'] ) : '';
103 ?>
104 <input type="text" autocomplete="off" name="<?php echo \esc_attr( self::$option_group ); ?>[google_client_id]" id="xmlsf_notifier_google_client_id" value="<?php echo \esc_attr( $client_id ); ?>" class="regular-text">
105 <p class="description">
106 <?php \esc_html_e( 'Enter your Google Cloud Project Client ID. You can find this in the Google Cloud Console under APIs & Services > Credentials.', 'xml-sitemap-feed' ); ?>
107 </p>
108 <?php
109 }
110
111 /**
112 * Render the text field for the Google Client Secret.
113 */
114 public static function google_client_secret_render() {
115 $options = (array) \get_option( self::$option_group, array() );
116 $client_secret = ! empty( $options['google_client_secret'] ) ? self::$pw_placeholder : '';
117 ?>
118 <input type="password" autocomplete="new-password" name="<?php echo \esc_attr( self::$option_group ); ?>[google_client_secret]" id="xmlsf_notifier_google_client_secret" value="<?php echo \esc_attr( $client_secret ); ?>" class="regular-text">
119 <p class="description">
120 <?php \esc_html_e( 'Enter your Google Cloud Project Client Secret. Keep this secret confidential. If you loose it, you will need to create a new one in the Google Cloud Console under APIs & Services > Credentials.', 'xml-sitemap-feed' ); ?>
121 </p>
122 <?php
123 }
124
125 /**
126 * Sanitize the plugin options before saving.
127 *
128 * @param array $input The options array submitted by the form.
129 *
130 * @return array The sanitized options array.
131 */
132 public static function sanitize_settings( $input ) {
133 $sanitized = array();
134 $input = (array) $input;
135 $options = (array) \get_option( self::$option_group, array() ); // Not strictly needed if only sanitizing submitted input.
136
137 // Sanitize Google Client ID.
138 if ( isset( $input['google_client_id'] ) ) {
139 $sanitized['google_client_id'] = \sanitize_text_field( $input['google_client_id'] );
140 } else {
141 $sanitized['google_client_id'] = isset( $options['google_client_id'] ) ? $options['google_client_id'] : '';
142 }
143
144 // Sanitize Google Client Secret.
145 if ( isset( $input['google_client_secret'] ) && self::$pw_placeholder !== $input['google_client_secret'] && ( empty( $options['google_client_secret'] ) || $options['google_client_secret'] !== $input['google_client_secret'] ) ) {
146 $sanitized['google_client_secret'] = Secret::encrypt( \sanitize_text_field( $input['google_client_secret'] ) );
147 } else {
148 $sanitized['google_client_secret'] = isset( $options['google_client_secret'] ) ? $options['google_client_secret'] : '';
149 }
150
151 // Make sure to not loose existing refresh token, but only if client id was not changed. Allows clearing the token with an empty string.
152 if ( isset( $input['google_refresh_token'] ) ) {
153 $sanitized['google_refresh_token'] = \sanitize_text_field( $input['google_refresh_token'] );
154 } else {
155 $sanitized['google_refresh_token'] = isset( $options['google_refresh_token'] ) && ! empty( $options['google_client_id'] ) && $sanitized['google_client_id'] === $options['google_client_id'] ? $options['google_refresh_token'] : '';
156 }
157
158 if ( isset( $input['property_url'] ) ) {
159 $sanitized['property_url'] = \sanitize_text_field( $input['property_url'] );
160 } else {
161 $sanitized['property_url'] = isset( $options['property_url'] ) ? $options['property_url'] : '';
162 }
163
164 return $sanitized;
165 }
166 }
167