UserProfileTab.php
4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace AIOSEO\Plugin\Common\Standalone;
use AIOSEO\Plugin\Pro\Standalone as ProStandalone;
/**
* Registers the standalone components.
*
* @since 4.2.2
*/
class UserProfileTab {
/**
* Class constructor.
*
* @since 4.2.2
*/
public function __construct() {
if ( ! is_admin() ) {
return;
}
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScript' ] );
add_action( 'profile_update', [ $this, 'updateUserSocialProfiles' ], 10, 1 );
}
/**
* Enqueues the script.
*
* @since 4.2.2
*
* @return void
*/
public function enqueueScript() {
if ( apply_filters( 'aioseo_user_profile_tab_disable', false ) ) {
return;
}
$screen = get_current_screen();
if ( ! in_array( $screen->id, [ 'user-edit', 'profile' ], true ) ) {
if ( 'follow-up_page_followup-emails-reports' === $screen->id ) {
aioseo()->core->assets->load( 'src/vue/standalone/user-profile-tab/follow-up-emails-nav-bar.js' );
}
return;
}
global $user_id;
if ( ! intval( $user_id ) ) {
return;
}
aioseo()->core->assets->load( 'src/vue/standalone/user-profile-tab/main.js', [], $this->getVueData() );
}
/**
* Returns the data Vue requires.
*
* @since 4.2.2
*
* @return array
*/
private function getVueData() {
global $user_id;
$socialProfiles = $this->getSocialProfiles();
foreach ( $socialProfiles as $platformKey => $v ) {
$metaName = 'aioseo_' . aioseo()->helpers->toSnakeCase( $platformKey );
$socialProfiles[ $platformKey ] = get_user_meta( $user_id, $metaName, true );
}
$sameUsername = get_user_meta( $user_id, 'aioseo_profiles_same_username', true );
if ( empty( $sameUsername ) ) {
$sameUsername = [
'enable' => false,
'username' => '',
'included' => [ 'facebookPageUrl', 'twitterUrl', 'pinterestUrl', 'instagramUrl', 'youtubeUrl', 'linkedinUrl' ] // Same as in Options.php.
];
}
$additionalurls = get_user_meta( $user_id, 'aioseo_profiles_additional_urls', true );
$extraVueData = [
'userProfile' => [
'userData' => get_userdata( $user_id )->data,
'profiles' => [
'sameUsername' => $sameUsername,
'urls' => $socialProfiles,
'additionalUrls' => $additionalurls
],
'isWooCommerceFollowupEmailsActive' => aioseo()->helpers->isWooCommerceFollowupEmailsActive()
]
];
$vueData = aioseo()->helpers->getVueData();
$vueData = array_merge( $vueData, $extraVueData );
return $vueData;
}
/**
* Updates the user social profile URLs when a user's profile is updated.
*
* @since 4.2.2
*
* @param int $userId The user ID.
* @return void
*/
public function updateUserSocialProfiles( $userId ) {
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $userId ) ) {
return;
}
if ( empty( $_POST['aioseo-user-social-profiles'] ) ) {
return;
}
$data = json_decode( sanitize_text_field( wp_unslash( $_POST['aioseo-user-social-profiles'] ) ), true );
if ( empty( $data ) ) {
return;
}
$sanitizedIncluded = [];
foreach ( $data['sameUsername']['included'] as $platformKey ) {
$sanitizedIncluded[] = sanitize_text_field( $platformKey );
}
$sanitizedSameUsernameData = [
'enable' => (bool) $data['sameUsername']['enable'],
'username' => sanitize_text_field( $data['sameUsername']['username'] ),
'included' => array_filter( $sanitizedIncluded )
];
update_user_meta( $userId, 'aioseo_profiles_same_username', $sanitizedSameUsernameData );
foreach ( $data['urls'] as $platformKey => $value ) {
$value = sanitize_text_field( $value );
$metaName = 'aioseo_' . aioseo()->helpers->toSnakeCase( $platformKey );
update_user_meta( $userId, $metaName, $value );
}
$additionalUrls = sanitize_text_field( $data['additionalUrls'] );
$sanitizedAdditionalUrls = preg_replace( '/\h/', "\n", $additionalUrls );
update_user_meta( $userId, 'aioseo_profiles_additional_urls', $sanitizedAdditionalUrls );
}
/**
* Returns a list of supported social profiles.
*
* @since 4.2.2
*
* @return array
*/
public function getSocialProfiles() {
return [
'facebookPageUrl' => '',
'twitterUrl' => '',
'instagramUrl' => '',
'pinterestUrl' => '',
'youtubeUrl' => '',
'linkedinUrl' => '',
'tumblrUrl' => '',
'yelpPageUrl' => '',
'soundCloudUrl' => '',
'wikipediaUrl' => '',
'myspaceUrl' => ''
];
}
}