PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.6
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 / user-meta / user-interface / additional-contactmethods-integration.php

additional-contactmethods-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.6, at src/user-meta/user-interface/additional-contactmethods-integration.php

78 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 namespace Yoast\WP\SEO\User_Meta\User_Interface;
4
5 use Yoast\WP\SEO\Conditionals\No_Conditionals;
6 use Yoast\WP\SEO\Integrations\Integration_Interface;
7 use Yoast\WP\SEO\User_Meta\Application\Additional_Contactmethods_Collector;
8
9 /**
10 * Handles registering and saving additional contactmethods for users.
11 */
12 class Additional_Contactmethods_Integration implements Integration_Interface {
13
14 use No_Conditionals;
15
16 /**
17 * The additional contactmethods collector.
18 *
19 * @var Additional_Contactmethods_Collector
20 */
21 private $additional_contactmethods_collector;
22
23 /**
24 * The constructor.
25 *
26 * @param Additional_Contactmethods_Collector $additional_contactmethods_collector The additional contactmethods collector.
27 */
28 public function __construct( Additional_Contactmethods_Collector $additional_contactmethods_collector ) {
29 $this->additional_contactmethods_collector = $additional_contactmethods_collector;
30 }
31
32 /**
33 * Registers action hook.
34 *
35 * @return void
36 */
37 public function register_hooks(): void {
38 \add_filter( 'user_contactmethods', [ $this, 'update_contactmethods' ] );
39 \add_filter( 'update_user_metadata', [ $this, 'stop_storing_empty_metadata' ], 10, 4 );
40 }
41
42 /**
43 * Updates the contactmethods with an additional set of social profiles.
44 *
45 * These are used with the Facebook author, rel="author", Twitter cards implementation, but also in the `sameAs` schema attributes.
46 *
47 * @param array<string, string> $contactmethods Currently set contactmethods.
48 *
49 * @return array<string, string> Contactmethods with added contactmethods.
50 */
51 public function update_contactmethods( $contactmethods ) {
52 $additional_contactmethods = $this->additional_contactmethods_collector->get_additional_contactmethods_objects();
53
54 return \array_merge( ( $contactmethods ?? [] ), $additional_contactmethods );
55 }
56
57 /**
58 * Returns a check value, which will stop empty contactmethods from going into the database.
59 *
60 * @param bool|null $check Whether to allow updating metadata for the given type.
61 * @param int $object_id ID of the object metadata is for.
62 * @param string $meta_key Metadata key.
63 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
64 *
65 * @return false|null False for when we are to filter out empty metadata, null for no filtering.
66 */
67 public function stop_storing_empty_metadata( $check, $object_id, $meta_key, $meta_value ) {
68 $additional_contactmethods = $this->additional_contactmethods_collector->get_additional_contactmethods_keys();
69
70 if ( \in_array( $meta_key, $additional_contactmethods, true ) && $meta_value === '' ) {
71 \delete_user_meta( $object_id, $meta_key );
72 return false;
73 }
74
75 return $check;
76 }
77 }
78