|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @since 1.3.0 |
|
4
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Wordlift\Vocabulary\Api; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
use Wordlift\Vocabulary\Analysis_Background_Service; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This endpoint is used to obtain the reconcile progress, number of tags accepted / total number of tags. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Reconcile_Progress_Endpoint { |
|
16
|
|
|
|
|
17
|
|
|
public function register_routes() { |
|
18
|
|
|
$that = $this; |
|
19
|
|
|
add_action( 'rest_api_init', |
|
20
|
|
|
function () use ( $that ) { |
|
21
|
|
|
$that->register_progress_route(); |
|
22
|
|
|
} ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
View Code Duplication |
public function get_terms_compat( $taxonomy, $args_with_taxonomy_key ) { |
|
|
|
|
|
|
27
|
|
|
global $wp_version; |
|
28
|
|
|
|
|
29
|
|
|
if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
30
|
|
|
return get_terms( $taxonomy, $args_with_taxonomy_key ); |
|
31
|
|
|
} else { |
|
32
|
|
|
return get_terms( $args_with_taxonomy_key ); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
public function progress() { |
|
38
|
|
|
|
|
39
|
|
|
$total_tags = count( $this->get_terms_compat( 'post_tag', array( |
|
40
|
|
|
'taxonomy' => 'post_tag', |
|
41
|
|
|
'hide_empty' => false, |
|
42
|
|
|
'fields' => 'ids', |
|
43
|
|
|
'meta_query' => array( |
|
44
|
|
|
array( |
|
45
|
|
|
'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
46
|
|
|
'compare' => '=', |
|
47
|
|
|
'value' => '1' |
|
48
|
|
|
) |
|
49
|
|
|
), |
|
50
|
|
|
) ) ); |
|
51
|
|
|
|
|
52
|
|
|
$completed = count( $this->get_terms_compat( |
|
53
|
|
|
'post_tag', |
|
54
|
|
|
array( |
|
55
|
|
|
'taxonomy' => 'post_tag', |
|
56
|
|
|
'hide_empty' => false, |
|
57
|
|
|
'fields' => 'ids', |
|
58
|
|
|
'meta_query' => array( |
|
59
|
|
|
array( |
|
60
|
|
|
'key' => Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, |
|
61
|
|
|
'compare' => '=', |
|
62
|
|
|
'value' => '1' |
|
63
|
|
|
) |
|
64
|
|
|
), |
|
65
|
|
|
) |
|
66
|
|
|
) |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
return array( |
|
71
|
|
|
'completed' => $completed, |
|
72
|
|
|
'total' => $total_tags |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
private function register_progress_route() { |
|
|
|
|
|
|
78
|
|
|
register_rest_route( |
|
79
|
|
|
Api_Config::REST_NAMESPACE, |
|
80
|
|
|
'/reconcile_progress/progress', |
|
81
|
|
|
array( |
|
82
|
|
|
'methods' => \WP_REST_Server::CREATABLE, |
|
83
|
|
|
'callback' => array( $this, 'progress' ), |
|
84
|
|
|
'permission_callback' => function () { |
|
85
|
|
|
return current_user_can( 'manage_options' ); |
|
86
|
|
|
}, |
|
87
|
|
|
) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.