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 / custom-meta-integration.php

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

120 lines 3.2 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\Admin_Conditional;
6 use Yoast\WP\SEO\Conditionals\User_Can_Edit_Users_Conditional;
7 use Yoast\WP\SEO\Conditionals\User_Edit_Conditional;
8 use Yoast\WP\SEO\Integrations\Integration_Interface;
9 use Yoast\WP\SEO\User_Meta\Application\Custom_Meta_Collector;
10
11 /**
12 * Handles custom meta for users.
13 */
14 class Custom_Meta_Integration implements Integration_Interface {
15
16 /**
17 * The custom meta collector.
18 *
19 * @var Custom_Meta_Collector
20 */
21 private $custom_meta_collector;
22
23 /**
24 * The constructor.
25 *
26 * @param Custom_Meta_Collector $custom_meta_collector The custom meta collector.
27 */
28 public function __construct( Custom_Meta_Collector $custom_meta_collector ) {
29 $this->custom_meta_collector = $custom_meta_collector;
30 }
31
32 /**
33 * Retrieves the conditionals for the integration.
34 *
35 * @return array<Yoast\WP\SEO\Conditionals> The conditionals.
36 */
37 public static function get_conditionals() {
38 return [
39 Admin_Conditional::class,
40 User_Can_Edit_Users_Conditional::class,
41 User_Edit_Conditional::class,
42 ];
43 }
44
45 /**
46 * Registers action hook.
47 *
48 * @return void
49 */
50 public function register_hooks(): void {
51 \add_action( 'show_user_profile', [ $this, 'user_profile' ] );
52 \add_action( 'edit_user_profile', [ $this, 'user_profile' ] );
53
54 \add_action( 'personal_options_update', [ $this, 'process_user_option_update' ] );
55 \add_action( 'edit_user_profile_update', [ $this, 'process_user_option_update' ] );
56 }
57
58 /**
59 * Updates the user metas that (might) have been set on the user profile page.
60 *
61 * @param int $user_id User ID of the updated user.
62 *
63 * @return void
64 */
65 public function process_user_option_update( $user_id ) {
66 \update_user_meta( $user_id, '_yoast_wpseo_profile_updated', \time() );
67
68 if ( ! \check_admin_referer( 'wpseo_user_profile_update', 'wpseo_nonce' ) ) {
69 return;
70 }
71
72 foreach ( $this->custom_meta_collector->get_custom_meta() as $meta ) {
73 if ( ! $meta->is_setting_enabled() ) {
74 continue;
75 }
76
77 $meta_field_id = $meta->get_field_id();
78 $user_input_to_store = isset( $_POST[ $meta_field_id ] ) ? \sanitize_text_field( \wp_unslash( $_POST[ $meta_field_id ] ) ) : '';
79
80 if ( $meta->is_empty_allowed() || $user_input_to_store !== '' ) {
81 \update_user_meta( $user_id, $meta->get_key(), $user_input_to_store );
82 continue;
83 }
84
85 \delete_user_meta( $user_id, $meta->get_key() );
86 }
87 }
88
89 /**
90 * Adds the inputs needed for SEO values to the User Profile page.
91 *
92 * @param WP_User $user User instance to output for.
93 *
94 * @return void
95 */
96 public function user_profile( $user ) {
97 \wp_nonce_field( 'wpseo_user_profile_update', 'wpseo_nonce' );
98
99 /* translators: %1$s expands to Yoast SEO */
100 $yoast_user_settings_header = \sprintf( \__( '%1$s settings', 'wordpress-seo' ), 'Yoast SEO' );
101
102 echo '
103
104 <div class="yoast yoast-settings">
105
106 <h2 id="wordpress-seo">' . \esc_html( $yoast_user_settings_header ) . '</h2>';
107
108 foreach ( $this->custom_meta_collector->get_sorted_custom_meta() as $meta ) {
109 if ( ! $meta->is_setting_enabled() ) {
110 continue;
111 }
112
113 $meta->render_field( $user->ID );
114 }
115
116 \do_action( 'wpseo_render_user_profile', $user );
117 echo '</div>';
118 }
119 }
120