class-controller.php
4.36 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
169
170
171
172
173
174
175
<?php
/**
* Controller for Recipient activation webhook.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\App\Webhooks\Recipient_Activation
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\App\Webhooks\Recipient_Activation;
// Abort if called directly.
defined( 'WPINC' ) || die;
use WPMUDEV_BLC\App\Options\Settings\Model as Settings;
use WPMUDEV_BLC\App\Users\Recipients\Model as Recipients;
use WPMUDEV_BLC\Core\Controllers\Webhook;
/**
* Class Controller
*
* @package WPMUDEV_BLC\App\Emails\Recipient_Activation
*/
class Controller extends Webhook {
/**
* The webhook tag.
*
* @var string $webhook The webhook tag
*/
public $webhook_tag = 'blc-activate-recipient';
/**
* The activated recipient data in an array. Retrieved by activation code.
*
* @var array $activated_recipient
*/
public $activated_recipient = array();
/**
* The plugin settings.
*
* @var array $settings
*/
public $settings = array();
/**
* Prepares the class properties.
*
* @return void
*/
public function prepare_vars() {
$this->webhook_title = __( 'Activate broken links recipient', 'broken-link-checker' );
}
/**
* Executes the webhook action(s).
*
* @param $wp
*
* @return void
*/
public function webhook_action( &$wp ) {
$this->settings = Settings::instance()->get();
$activation_key = $_GET['activation_code'] ?? null;
$action = isset( $_GET['action'] ) && in_array( $_GET['action'], array(
'activate',
'cancel'
) ) ?
$_GET['action'] : null;
if ( is_null( $action ) ) {
return;
}
$this->activated_recipient = Recipients::get_recipient_by_key( $activation_key );
if ( empty( $this->activated_recipient ) ) {
return;
}
switch ( $action ) {
case 'activate' :
if ( $this->set_recipient_status( $this->activated_recipient ) ) {
$this->activated_recipient['cancellation_link'] = add_query_arg( array(
'action' => 'cancel',
'activation_code' => esc_html( $activation_key ),
), $this->webhook_url() );
}
break;
case 'cancel' :
if ( isset( $this->activated_recipient['user_id'] ) ) {
$this->unset_registered_recipient( intval( $this->activated_recipient['user_id'] ) );
} else {
$this->set_recipient_status( $this->activated_recipient, false );
}
break;
}
}
/**
* Changes the recipient status for recipients added by email.
*
* @param array $recipient_data
* @param bool $new_status
*
* @return bool
*/
protected function set_recipient_status( array $recipient_data = array(), bool $new_status = true ) {
if (
empty( $recipient_data ) ||
! isset( $recipient_data['email'] ) ||
! isset( $this->settings['schedule'] ) ||
( empty( $this->settings['schedule']['emailrecipients'] ) && empty( $this->settings['schedule']['registered_recipients_data'] ) )
) {
return false;
}
Settings::instance()->init();
$this->settings['schedule']['emailrecipients'] = array_map(
function ( $recipient ) use ( $recipient_data, $new_status ) {
if ( isset( $recipient['email'] ) && $recipient['email'] === $recipient_data['email'] ) {
$recipient['confirmed'] = boolval( $new_status );
}
return $recipient;
},
$this->settings['schedule']['emailrecipients']
);
Settings::instance()->set( array( 'schedule' => $this->settings['schedule'] ) );
Settings::instance()->save();
return true;
}
/**
* Removes a recipient by user id.
*
* @param int|null $user_id
*
* @return false|void
*/
protected function unset_registered_recipient( int $user_id = null ) {
if ( empty( $user_id ) ) {
return false;
}
$user_key = array_search(
$user_id,
$this->settings['schedule']['recipients']
);
if (
! is_numeric( $user_key ) ||
empty( $this->settings['schedule']['recipients'][ $user_key ] ) ||
intval( $this->settings['schedule']['recipients'][ $user_key ] ) !== $user_id
) {
return false;
}
unset( $this->settings['schedule']['recipients'][ $user_key ] );
$this->settings['schedule']['recipients'] = array_values( $this->settings['schedule']['recipients'] );
unset( $this->settings['schedule']['registered_recipients_data'][ $user_id ] );
Settings::instance()->set( array( 'schedule' => $this->settings['schedule'] ) );
Settings::instance()->save();
}
}