| 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 |
|