class-model.php
4.2 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
<?php
/**
* The Recipients model.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\App\Users\Recipients;
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\App\Users\Recipients;
// Abort if called directly.
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
use WPMUDEV_BLC\App\Options\Settings\Model as Settings;
defined( 'WPINC' ) || die;
/**
* Class Settings
*
* @package WPMUDEV_BLC\App\Admin_Pages\Dashboard
*/
class Model extends Base {
/**
* Common key to be used in user meta and options tables.
*
* @var string
*/
public static $review_flag_key = 'wpmudev_blc_reviewed';
/**
* Returns a boolean indicating if user can review on wp org or not. This is based on user role (administrators only) and specific meta that is used as a flag `wpmudev_blc_reviewed` in case already reviewed.
*
* @param int|null $user_id
*
* @return mixed
*/
/*public static function can_review( int $user_id = null ) {
if ( empty( $user_id ) ) {
return false;
}
return apply_filters(
'wpmudev_blc_users_recipient_can_review',
user_can( $user_id, 'manage_options' ) && empty( get_option( self::$review_flag_key ) ) && empty( get_user_meta( $user_id, self::$review_flag_key, true ) )
);
}
*/
/**
* Returns true if user has reviewd, else false.
*
* @param int|null $user_id
*
* @return bool
*/
public static function recipient_has_reviewed( int $user_id = null ) {
if ( empty( $user_id ) ) {
return false;
}
return apply_filters(
'wpmudev_blc_users_recipient_has_reviewed',
! empty( get_user_meta( $user_id, self::$review_flag_key, true ) )
);
}
/**
* Set the reviewed flag to true if user can review.
*
* @param int|null $user_id
*
* @return bool
*/
public static function flag_recipient_reviewed( int $user_id = null ) {
return update_user_meta( $user_id, self::$review_flag_key, true ) && update_option( self::$review_flag_key, true, false );
}
public static function has_been_reviewed() {
return ! empty( get_option( self::$review_flag_key ) );
}
public static function get_recipient_by_key( string $key = null ) {
if ( empty( $key ) ) {
return false;
}
$key_parts = explode( '_', base64_decode( $key ) );
if ( count( $key_parts ) < 2 ) {
return array();
}
$hashed_email = $key_parts[0];
$recipient_key = sanitize_text_field( $key_parts[1] );
$schedule = Settings::instance()->get( 'schedule' );
$recipient = array();
// First go through unregistered recipients (that were added by email).
if ( ! empty( $schedule['emailrecipients'] ) ) {
$recipient = array_filter(
$schedule['emailrecipients'],
function ( $recipient_data ) use ( $recipient_key, $hashed_email ) {
return isset( $recipient_data['key'] ) &&
$recipient_data['key'] === $recipient_key &&
$hashed_email === md5( $recipient_data['email'] );
}
);
}
// If token does not belong to unregistered recipient we need to check in the recipients that are actual users.
if ( empty( $recipient ) ) {
if ( ! empty( $schedule['registered_recipients_data'] ) ) {
foreach ( $schedule['registered_recipients_data'] as $user_id => $user_data ) {
$user = null;
if ( md5( $user_data['email'] ) === $hashed_email ) {
$user = get_user_by( 'email', sanitize_email( $user_data['email'] ) );
}
if ( $user instanceof \WP_User ) {
$recipient = array(
'key' => $user_data['key'],
'name' => $user->display_name,
'email' => $user->user_email,
'user_id' => $user->ID,
);
break;
}
}
}
if ( empty( $recipient ) ) {
$user_key_parts = explode( '|', $recipient_key );
$user = ! empty( $user_key_parts[1] ) ? get_userdata( intval( $user_key_parts[1] ) ) : null;
if ( $user instanceof \WP_User && md5( $user->user_email ) === $hashed_email ) {
$recipient = array(
'key' => $recipient_key,
'name' => $user->display_name,
'email' => $user->user_email,
'user_id' => $user->ID,
);
}
}
} else {
$recipient = array_values( $recipient )[0];
}
return $recipient;
}
}