class-link.php
6.39 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/**
* Link object that contains all link editing (replace/unlink/nofollow) information and methods.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.1.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\App\Broken_Links_Actions
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\App\Broken_Links_Actions;
// Abort if called directly.
defined( 'WPINC' ) || die;
use WP_Error;
use WP_Post;
use WP_User;
use WPMUDEV_BLC\App\Options\Links_Queue\Model as Queue;
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
use WPMUDEV_BLC\Core\Traits\Execution_Time;
/**
* Class Scan_Data
*
* @package WPMUDEV_BLC\App\Broken_Links_Actions
*/
class Link extends Base {
/**
* Use the Dashboard_API Trait.
*
* @since 2.1.0
*/
use Execution_Time;
/**
* The Link type. Valid values `replace`, `unlink` or `nofollow`.
*
* @since 2.1.0
* @var string $type
*
*/
protected $type = null;
/**
* The link to be actioned.
*
* @since 2.1.0
* @var string $link
*/
protected $link = null;
/**
* The new link that will replace old one. When $this->type is `replace`.
*
* @since 2.1.0
* @var string $new_link_url
*/
protected $new_link = null;
/**
* Action should be executed on whole site if true.
*
* @since 2.1.0
* @var boolean $full_site
*/
protected $full_site = false;
/**
* The site urs where link needs to be actioned.
* Can contain string (for url) or int (for post id).
*
* @since 2.1.0
* @var array $origins
*/
protected $origins = array();
/**
* True if link should be unlinked.
*
* @since 2.1.0
* @var array $unlink
*/
protected $unlink = true;
/**
* True if link should be set to nofollow.
*
* @since 2.1.0
* @var array $nofollow
*/
protected $nofollow = true;
/**
* The offset to start from when link is full site.
*
* @var int
*/
protected $offset = 0;
/**
* The limit that sets how many post to search in for full site links on each batch.
*
* @var int
*/
protected $limit = 10;
/**
* @return void
*/
public function __construct( array $props = array() ) {
// From Execution_Time trait.
// Actually we are not using this. Instead, we use https://developer.wordpress.org/reference/functions/timer_stop/.
//$this->start_timer();
if ( ! empty( $props ) ) {
$default_props = array(
'link' => '',
'new_link' => '',
'full_site' => false,
'origins' => array(),
'unlink' => false,
'nofollow' => false,
'offset' => 0,
);
$props = apply_filters(
'wpmudev_blc_link_actions_link_props',
wp_parse_args( $props, $default_props ),
$props
);
if ( ! empty( $props ) ) {
foreach ( $props as $property_name => $property_value ) {
if ( property_exists( $this, $property_name ) ) {
$this->__set( $property_name, $property_value );
}
}
}
$this->link = stripslashes( untrailingslashit( $this->link ) );
}
}
/**
* Runs the actions that are requested by link data.
*
* @return true[]
*/
public function execute_action() {
$report = array();
if ( ! empty( $this->is_full_site() ) ) {
// If all tables have been completed for Link, no need to run `execute_full_site()`.
// The offset will be reset in caller method.
if ( Queue::instance()->check_target_tables_complete() ) {
return array(
'completed' => true,
'link' => $this->link,
);
}
$report = $this->execute_full_site();
} else {
$report = $this->execute_origins();
}
if ( ! is_wp_error( $report ) ) {
$report['link'] = $this->link;
$report['link_mode'] = $this->is_full_site() ? 'full_site_links' : 'page_links';
}
return $report;
}
public function get_type() {
if ( empty( $this->type ) || ! in_array( $this->type, array( 'replace', 'unlink', 'nofollow' ) ) ) {
if ( ! empty( $this->new_link ) ) {
$this->type = 'replace';
} else if ( ! empty( $this->unlink ) ) {
$this->type = 'unlink';
} else if ( ! empty( $this->nofollow ) ) {
$this->type = 'nofollow';
}
}
return $this->type;
}
public function is_full_site() {
return ! empty( $this->full_site );
}
/**
* Executes Link action in full site.
*
* @return void
*/
public function execute_full_site() {
$handler = new Handlers\Full_Site_Handler( $this );
$response = $handler->execute();
// Check if current table has been precessed.
if ( ! empty( $response[ 'table_completed' ] ) ) {
// Move to process next target table.
Queue::instance()->move_to_next_target_table();
$this->offset = 0;
// check if all target tables have been processed.
if ( Queue::instance()->check_target_tables_complete() ) {
return array(
'completed' => true,
'link' => $this->link,
);
}
if ( ! $this->runtime_passed_limit() ) {
$this->execute_full_site();
}
}
// At this point current table process is not complete yet.
// If response holds the row number we can store that in db.
if ( ! empty( $response['row_count'] ) ) {
$current_task = Queue::instance()->get_current_task();
$current_task['rows'] = intval( $response['row_count'] );
Queue::instance()->set( array( 'current_task' => $current_task ) );
Queue::instance()->save();
}
return $response;
}
public function get_offset() {
return intval( $this->offset );
}
public function get_limit() {
return intval( $this->limit );
}
public function get_link() {
return $this->link;
}
public function get_new_link() {
return $this->new_link ?? null;
}
/**
* Executes action when Link uses origins.
*
* @return array|WP_Error
*/
public function execute_origins() {
/*
* For origins links, we return :
* status : bool, true if action executed successfully in all origins, else false.
* notfound_in : array, list of origins the link was not found in.
*/
if ( empty( $this->origins() ) ) {
return new WP_Error(
'blc-link-execution-error',
esc_html__(
'Missing origins',
'broken-link-checker'
)
);
}
$handler = new Handlers\Origins_Handler( $this );
return $handler->execute();
}
/**
* Returns the Link origins.
*
* @return array
*/
public function origins() {
return $this->origins;
}
public function types_map() {
return apply_filters(
'wpmudev_blc_link_types_map',
array(
'replace' => 'edited',
'unlink' => 'unlinked',
'nofollow' => 'nofollowed',
),
$this
);
}
}