PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / tracking / infrastructure / tracking-on-page-load-integration.php

tracking-on-page-load-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.5, at src/tracking/infrastructure/tracking-on-page-load-integration.php

105 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
4 namespace Yoast\WP\SEO\Tracking\Infrastructure;
5
6 use Yoast\WP\SEO\Conditionals\Admin_Conditional;
7 use Yoast\WP\SEO\Helpers\Capability_Helper;
8 use Yoast\WP\SEO\Helpers\Options_Helper;
9 use Yoast\WP\SEO\Integrations\Integration_Interface;
10 use Yoast\WP\SEO\Tracking\Application\Action_Tracker;
11
12 /**
13 * Handles tracking on page load.
14 *
15 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
16 */
17 class Tracking_On_Page_Load_Integration implements Integration_Interface {
18
19 /**
20 * Holds the action tracker instance.
21 *
22 * @var Action_Tracker
23 */
24 private $action_tracker;
25
26 /**
27 * Holds the capability helper instance.
28 *
29 * @var Capability_Helper
30 */
31 private $capability_helper;
32
33 /**
34 * Holds the options helper instance.
35 *
36 * @var Options_Helper
37 */
38 private $options_helper;
39
40 /**
41 * The constructor.
42 *
43 * @param Action_Tracker $action_tracker The action tracker.
44 * @param Capability_Helper $capability_helper The capability helper.
45 * @param Options_Helper $options_helper The options helper.
46 */
47 public function __construct(
48 Action_Tracker $action_tracker,
49 Capability_Helper $capability_helper,
50 Options_Helper $options_helper
51 ) {
52 $this->action_tracker = $action_tracker;
53 $this->capability_helper = $capability_helper;
54 $this->options_helper = $options_helper;
55 }
56
57 /**
58 * Returns the needed conditionals.
59 *
60 * @return array<string> The conditionals that must be met to load this.
61 */
62 public static function get_conditionals(): array {
63 return [
64 Admin_Conditional::class,
65 ];
66 }
67
68 /**
69 * Registers action hook.
70 *
71 * @return void
72 */
73 public function register_hooks(): void {
74 \add_action( 'admin_init', [ $this, 'store_version_on_page_load' ] );
75 }
76
77 /**
78 * Stores the current version for the tracking option taken from the URL.
79 *
80 * @return void
81 */
82 public function store_version_on_page_load() {
83 if ( ! isset( $_GET['wpseo_tracked_action'] ) || ! \is_string( $_GET['wpseo_tracked_action'] ) ) {
84 return;
85 }
86
87 if ( $this->capability_helper->current_user_can( 'wpseo_manage_options' ) !== true ) {
88 return;
89 }
90
91 if ( ! isset( $_GET['wpseo_tracking_nonce'] ) || ! \wp_verify_nonce( \sanitize_text_field( \wp_unslash( $_GET['wpseo_tracking_nonce'] ) ), 'wpseo_tracking_nonce' ) ) {
92 return;
93 }
94
95 $action_to_track = \sanitize_text_field( \wp_unslash( $_GET['wpseo_tracked_action'] ) );
96
97 // Verify that the option to store is one of our tracking options.
98 if ( ! \in_array( $action_to_track, $this->options_helper->get_tracking_only_options(), true ) ) {
99 return;
100 }
101
102 $this->action_tracker->track_version_for_performed_action( $action_to_track );
103 }
104 }
105