• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/integrations/admin/helpscout-beacon.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Admin;
4

5
use WPSEO_Addon_Manager;
6
use WPSEO_Admin_Asset_Manager;
7
use WPSEO_Tracking_Server_Data;
8
use WPSEO_Utils;
9
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
10
use Yoast\WP\SEO\Config\Migration_Status;
11
use Yoast\WP\SEO\Helpers\Options_Helper;
12
use Yoast\WP\SEO\Integrations\Academy_Integration;
13
use Yoast\WP\SEO\Integrations\Integration_Interface;
14
use Yoast\WP\SEO\Integrations\Settings_Integration;
15
use Yoast\WP\SEO\Integrations\Support_Integration;
16

17
/**
18
 * Class WPSEO_HelpScout
19
 */
20
class HelpScout_Beacon implements Integration_Interface {
21

22
        /**
23
         * The id for the beacon.
24
         *
25
         * @var string
26
         */
27
        protected $beacon_id = '2496aba6-0292-489c-8f5d-1c0fba417c2f';
28

29
        /**
30
         * The id for the beacon for users that have tracking on.
31
         *
32
         * @var string
33
         */
34
        protected $beacon_id_tracking_users = '6b8e74c5-aa81-4295-b97b-c2a62a13ea7f';
35

36
        /**
37
         * The products the beacon is loaded for.
38
         *
39
         * @var array
40
         */
41
        protected $products = [];
42

43
        /**
44
         * Whether to ask the user's consent before loading in HelpScout.
45
         *
46
         * @var bool
47
         */
48
        protected $ask_consent = true;
49

50
        /**
51
         * The options helper.
52
         *
53
         * @var Options_Helper
54
         */
55
        protected $options;
56

57
        /**
58
         * The array of pages we need to show the beacon on with their respective beacon IDs.
59
         *
60
         * @var array
61
         */
62
        protected $pages_ids;
63

64
        /**
65
         * The array of pages we need to show the beacon on.
66
         *
67
         * @var array
68
         */
69
        protected $base_pages = [
70
                'wpseo_dashboard',
71
                Settings_Integration::PAGE,
72
                Academy_Integration::PAGE,
73
                Support_Integration::PAGE,
74
                'wpseo_search_console',
75
                'wpseo_tools',
76
                'wpseo_licenses',
77
                'wpseo_workouts',
78
                'wpseo_integrations',
79
        ];
80

81
        /**
82
         * The current admin page
83
         *
84
         * @var string|null
85
         */
86
        protected $page;
87

88
        /**
89
         * The asset manager.
90
         *
91
         * @var WPSEO_Admin_Asset_Manager
92
         */
93
        protected $asset_manager;
94

95
        /**
96
         * The migration status object.
97
         *
98
         * @var Migration_Status
99
         */
100
        protected $migration_status;
101

102
        /**
103
         * Headless_Rest_Endpoints_Enabled_Conditional constructor.
104
         *
105
         * @param Options_Helper            $options          The options helper.
106
         * @param WPSEO_Admin_Asset_Manager $asset_manager    The asset manager.
107
         * @param Migration_Status          $migration_status The migrations status.
108
         */
109
        public function __construct( Options_Helper $options, WPSEO_Admin_Asset_Manager $asset_manager, Migration_Status $migration_status ) {
×
110
                $this->options       = $options;
×
111
                $this->asset_manager = $asset_manager;
×
112
                $this->ask_consent   = ! $this->options->get( 'tracking' );
×
113
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
114
                if ( isset( $_GET['page'] ) && \is_string( $_GET['page'] ) ) {
×
115
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
116
                        $this->page = \sanitize_text_field( \wp_unslash( $_GET['page'] ) );
×
117
                }
118
                else {
119
                        $this->page = null;
×
120
                }
121
                $this->migration_status = $migration_status;
×
122

123
                foreach ( $this->base_pages as $page ) {
×
124
                        if ( $this->ask_consent ) {
×
125
                                // We want to be able to show surveys to people who have tracking on, so we give them a different beacon.
126
                                $this->pages_ids[ $page ] = $this->beacon_id_tracking_users;
×
127
                        }
128
                        else {
129
                                $this->pages_ids[ $page ] = $this->beacon_id;
×
130
                        }
131
                }
132
        }
133

134
        /**
135
         * {@inheritDoc}
136
         */
137
        public function register_hooks() {
×
138
                \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_help_scout_script' ] );
×
139
                \add_action( 'admin_footer', [ $this, 'output_beacon_js' ] );
×
140
        }
141

142
        /**
143
         * Enqueues the HelpScout script.
144
         */
145
        public function enqueue_help_scout_script() {
×
146
                // Make sure plugins can filter in their "stuff", before we check whether we're outputting a beacon.
147
                $this->filter_settings();
×
148
                if ( ! $this->is_beacon_page() ) {
×
149
                        return;
×
150
                }
151

152
                $this->asset_manager->enqueue_script( 'help-scout-beacon' );
×
153
        }
154

155
        /**
156
         * Outputs a small piece of javascript for the beacon.
157
         */
158
        public function output_beacon_js() {
×
159
                if ( ! $this->is_beacon_page() ) {
×
160
                        return;
×
161
                }
162

163
                \printf(
×
164
                        '<script type="text/javascript">window.%1$s(\'%2$s\', %3$s)</script>',
×
165
                        ( $this->ask_consent ) ? 'wpseoHelpScoutBeaconConsent' : 'wpseoHelpScoutBeacon',
×
166
                        \esc_html( $this->pages_ids[ $this->page ] ),
×
167
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaping done in format_json_encode.
168
                        WPSEO_Utils::format_json_encode( (array) $this->get_session_data() )
×
169
                );
170
        }
171

172
        /**
173
         * Checks if the current page is a page containing the beacon.
174
         *
175
         * @return bool
176
         */
177
        private function is_beacon_page() {
×
178
                $return = false;
×
179
                if ( ! empty( $this->page ) && $GLOBALS['pagenow'] === 'admin.php' && isset( $this->pages_ids[ $this->page ] ) ) {
×
180
                        $return = true;
×
181
                }
182

183
                /**
184
                 * Filter: 'wpseo_helpscout_show_beacon' - Allows overriding whether we show the HelpScout beacon.
185
                 *
186
                 * @api bool - Whether we show the beacon or not.
187
                 */
188
                return \apply_filters( 'wpseo_helpscout_show_beacon', $return );
×
189
        }
190

191
        /**
192
         * Retrieves the identifying data.
193
         *
194
         * @return string The data to pass as identifying data.
195
         */
196
        protected function get_session_data() {
×
197
                // Short-circuit if we can get the needed data from a transient.
198
                $transient_data = \get_transient( 'yoast_beacon_session_data' );
×
199

200
                if ( \is_array( $transient_data ) ) {
×
201
                        return WPSEO_Utils::format_json_encode( $transient_data );
×
202
                }
203

204
                $current_user = \wp_get_current_user();
×
205

206
                // Do not make these strings translatable! They are for our support agents, the user won't see them!
207
                $data = \array_merge(
×
208
                        [
209
                                'name'               => \trim( $current_user->user_firstname . ' ' . $current_user->user_lastname ),
×
210
                                'email'              => $current_user->user_email,
×
211
                                'Languages'          => $this->get_language_settings(),
×
212
                        ],
213
                        $this->get_server_info(),
×
214
                        [
215
                                'WordPress Version'    => $this->get_wordpress_version(),
×
216
                                'Active theme'         => $this->get_theme_info(),
×
217
                                'Active plugins'       => $this->get_active_plugins(),
×
218
                                'Must-use and dropins' => $this->get_mustuse_and_dropins(),
×
219
                                'Indexables status'    => $this->get_indexables_status(),
×
220
                        ]
221
                );
222

223
                if ( ! empty( $this->products ) ) {
×
224
                        $addon_manager = new WPSEO_Addon_Manager();
×
225
                        foreach ( $this->products as $product ) {
×
226
                                $subscription = $addon_manager->get_subscription( $product );
×
227

228
                                if ( ! $subscription ) {
×
229
                                        continue;
×
230
                                }
231

232
                                $data[ $subscription->product->name ] = $this->get_product_info( $subscription );
×
233
                        }
234
                }
235

236
                // Store the data in a transient for 5 minutes to prevent overhead on every backend pageload.
237
                \set_transient( 'yoast_beacon_session_data', $data, ( 5 * \MINUTE_IN_SECONDS ) );
×
238

239
                return WPSEO_Utils::format_json_encode( $data );
×
240
        }
241

242
        /**
243
         * Returns basic info about the server software.
244
         *
245
         * @return array
246
         */
247
        private function get_server_info() {
×
248
                $server_tracking_data = new WPSEO_Tracking_Server_Data();
×
249
                $server_data          = $server_tracking_data->get();
×
250
                $server_data          = $server_data['server'];
×
251

252
                $fields_to_use = [
253
                        'Server IP'        => 'ip',
×
254
                        'PHP Version'      => 'PhpVersion',
255
                        'cURL Version'     => 'CurlVersion',
256
                ];
257

258
                $server_data['CurlVersion'] = $server_data['CurlVersion']['version'] . ' (SSL Support ' . $server_data['CurlVersion']['sslSupport'] . ')';
×
259

260
                $server_info = [];
×
261

262
                foreach ( $fields_to_use as $label => $field_to_use ) {
×
263
                        if ( isset( $server_data[ $field_to_use ] ) ) {
×
264
                                $server_info[ $label ] = \esc_html( $server_data[ $field_to_use ] );
×
265
                        }
266
                }
267

268
                // Get the memory limits for the server and, if different, from WordPress as well.
269
                $memory_limit                 = \ini_get( 'memory_limit' );
×
270
                $server_info['Memory limits'] = 'Server memory limit: ' . $memory_limit;
×
271

272
                if ( $memory_limit !== \WP_MEMORY_LIMIT ) {
×
273
                        $server_info['Memory limits'] .= ', WP_MEMORY_LIMIT: ' . \WP_MEMORY_LIMIT;
×
274
                }
275

276
                if ( $memory_limit !== \WP_MAX_MEMORY_LIMIT ) {
×
277
                        $server_info['Memory limits'] .= ', WP_MAX_MEMORY_LIMIT: ' . \WP_MAX_MEMORY_LIMIT;
×
278
                }
279

280
                return $server_info;
×
281
        }
282

283
        /**
284
         * Returns info about the Yoast SEO plugin version and license.
285
         *
286
         * @param object $plugin The plugin.
287
         *
288
         * @return string The product info.
289
         */
290
        private function get_product_info( $plugin ) {
×
291
                if ( empty( $plugin ) ) {
×
292
                        return '';
×
293
                }
294

295
                $product_info = \sprintf(
×
296
                        'Expiration date %1$s',
×
297
                        $plugin->expiry_date
×
298
                );
299

300
                return $product_info;
×
301
        }
302

303
        /**
304
         * Returns the WordPress version + a suffix about the multisite status.
305
         *
306
         * @return string The WordPress version string.
307
         */
308
        private function get_wordpress_version() {
×
309
                global $wp_version;
×
310

311
                $wordpress_version = $wp_version;
×
312
                if ( \is_multisite() ) {
×
313
                        $wordpress_version .= ' (multisite: yes)';
×
314
                }
315
                else {
316
                        $wordpress_version .= ' (multisite: no)';
×
317
                }
318

319
                return $wordpress_version;
×
320
        }
321

322
        /**
323
         * Returns information about the current theme.
324
         *
325
         * @return string The theme info as string.
326
         */
327
        private function get_theme_info() {
×
328
                $theme = \wp_get_theme();
×
329

330
                $theme_info = \sprintf(
×
331
                        '%1$s (Version %2$s, %3$s)',
×
332
                        \esc_html( $theme->display( 'Name' ) ),
×
333
                        \esc_html( $theme->display( 'Version' ) ),
×
334
                        \esc_attr( $theme->display( 'ThemeURI' ) )
×
335
                );
336

337
                if ( \is_child_theme() ) {
×
338
                        $theme_info .= \sprintf( ', this is a child theme of: %1$s', \esc_html( $theme->display( 'Template' ) ) );
×
339
                }
340

341
                return $theme_info;
×
342
        }
343

344
        /**
345
         * Returns a stringified list of all active plugins, separated by a pipe.
346
         *
347
         * @return string The active plugins.
348
         */
349
        private function get_active_plugins() {
×
350
                $updates_available = \get_site_transient( 'update_plugins' );
×
351

352
                $active_plugins = '';
×
353
                foreach ( \wp_get_active_and_valid_plugins() as $plugin ) {
×
354
                        $plugin_data             = \get_plugin_data( $plugin );
×
355
                        $plugin_file             = \str_replace( \trailingslashit( \WP_PLUGIN_DIR ), '', $plugin );
×
356
                        $plugin_update_available = '';
×
357

358
                        if ( isset( $updates_available->response[ $plugin_file ] ) ) {
×
359
                                $plugin_update_available = ' [update available]';
×
360
                        }
361

362
                        $active_plugins .= \sprintf(
×
363
                                '%1$s (Version %2$s%3$s, %4$s) | ',
×
364
                                \esc_html( $plugin_data['Name'] ),
×
365
                                \esc_html( $plugin_data['Version'] ),
×
366
                                $plugin_update_available,
×
367
                                \esc_attr( $plugin_data['PluginURI'] )
×
368
                        );
369
                }
370

371
                return $active_plugins;
×
372
        }
373

374
        /**
375
         * Returns a CSV list of all must-use and drop-in plugins.
376
         *
377
         * @return string The active plugins.
378
         */
379
        private function get_mustuse_and_dropins() {
×
380
                $dropins         = \get_dropins();
×
381
                $mustuse_plugins = \get_mu_plugins();
×
382

383
                if ( ! \is_array( $dropins ) ) {
×
384
                        $dropins = [];
×
385
                }
386

387
                if ( ! \is_array( $mustuse_plugins ) ) {
×
388
                        $mustuse_plugins = [];
×
389
                }
390

391
                return \sprintf( 'Must-Use plugins: %1$d, Drop-ins: %2$d', \count( $mustuse_plugins ), \count( $dropins ) );
×
392
        }
393

394
        /**
395
         * Return the indexables status details.
396
         *
397
         * @return string The indexables status in a string.
398
         */
399
        private function get_indexables_status() {
×
400
                $indexables_status  = 'Indexing completed: ';
×
401
                $indexing_completed = $this->options->get( 'indexables_indexing_completed' );
×
402
                $indexing_reason    = $this->options->get( 'indexing_reason' );
×
403

404
                $indexables_status .= ( $indexing_completed ) ? 'yes' : 'no';
×
405
                $indexables_status .= ( $indexing_reason ) ? ', latest indexing reason: ' . \esc_html( $indexing_reason ) : '';
×
406

407
                foreach ( [ 'free', 'premium' ] as $migration_name ) {
×
408
                        $current_status = $this->migration_status->get_error( $migration_name );
×
409

410
                        if ( \is_array( $current_status ) && isset( $current_status['message'] ) ) {
×
411
                                $indexables_status .= ', migration error: ' . \esc_html( $current_status['message'] );
×
412
                        }
413
                }
414

415
                return $indexables_status;
×
416
        }
417

418
        /**
419
         * Returns language settings for the website and the current user.
420
         *
421
         * @return string The locale settings of the site and user.
422
         */
423
        private function get_language_settings() {
×
424
                $site_locale = \get_locale();
×
425
                $user_locale = \get_user_locale();
×
426

427
                $language_settings = \sprintf(
×
428
                        'Site locale: %1$s, user locale: %2$s',
×
429
                        ( \is_string( $site_locale ) ) ? \esc_html( $site_locale ) : 'unknown',
×
430
                        ( \is_string( $user_locale ) ) ? \esc_html( $user_locale ) : 'unknown'
×
431
                );
432

433
                return $language_settings;
×
434
        }
435

436
        /**
437
         * Returns the conditionals based on which this integration should be active.
438
         *
439
         * @return array The array of conditionals.
440
         */
441
        public static function get_conditionals() {
×
442
                return [ Admin_Conditional::class ];
×
443
        }
444

445
        /**
446
         * Allows filtering of the HelpScout settings. Hooked to admin_head to prevent timing issues, not too early, not too late.
447
         */
448
        protected function filter_settings() {
×
449
                /**
450
                 * Filter: 'wpseo_helpscout_beacon_settings' - Allows overriding the HelpScout beacon settings.
451
                 *
452
                 * @api string - The HelpScout beacon settings.
453
                 */
454
                $filterable_helpscout_setting = [
455
                        'products'  => $this->products,
×
456
                        'pages_ids' => $this->pages_ids,
×
457
                ];
458

459
                $helpscout_settings = \apply_filters( 'wpseo_helpscout_beacon_settings', $filterable_helpscout_setting );
×
460

461
                $this->products  = $helpscout_settings['products'];
×
462
                $this->pages_ids = $helpscout_settings['pages_ids'];
×
463
        }
464
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc