|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* API Data Hooks |
|
6
|
|
|
* @author Navdeep Singh <[email protected]> |
|
7
|
|
|
* @package Wordlift\Api_Data |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Wordlift\Api_Data; |
|
11
|
|
|
|
|
12
|
|
|
class Api_Data_Hooks { |
|
13
|
|
|
|
|
14
|
|
|
const META_KEY = 'wl_schema_url'; |
|
15
|
|
|
|
|
16
|
|
|
private $configuration_service; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( $configuration_service ) { |
|
19
|
|
|
|
|
20
|
|
|
$this->configuration_service = $configuration_service; |
|
21
|
|
|
/** |
|
22
|
|
|
* Hook for Post Save |
|
23
|
|
|
*/ |
|
24
|
|
|
add_action( 'save_post', array( $this, 'post_save_request_delete_cache' ) ); |
|
25
|
|
|
/** |
|
26
|
|
|
* Check for Meta Key change on Post Save |
|
27
|
|
|
*/ |
|
28
|
|
|
add_action( 'updated_post_meta', array( $this, 'post_meta_request_delete_cache' ), 10, 4 ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function post_save_request_delete_cache( $post_id ) { |
|
32
|
|
|
$this->delete_cache_for_meta_values( $post_id ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function post_meta_request_delete_cache( $meta_id, $post_id, $meta_key, $_meta_value ) { |
|
|
|
|
|
|
36
|
|
|
if ( self::META_KEY === $meta_key ) { |
|
37
|
|
|
$this->delete_cache_for_meta_values( $post_id ); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param integer $post_id |
|
43
|
|
|
* |
|
44
|
|
|
* @return |
|
45
|
|
|
* |
|
46
|
|
|
*/ |
|
47
|
|
|
private function delete_cache_for_meta_values( $post_id ) { |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get Post Meta Values |
|
51
|
|
|
*/ |
|
52
|
|
|
$values = get_post_meta( $post_id, self::META_KEY, false ); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Iterate over $values array |
|
56
|
|
|
*/ |
|
57
|
|
|
if ( ! empty( $values ) && count( $values ) > 1 ) { |
|
58
|
|
|
foreach ( $values as $link ) { |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Skip the <permalink> |
|
62
|
|
|
*/ |
|
63
|
|
|
if ( $link === '<permalink>' ) { |
|
64
|
|
|
$link = get_permalink(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Sanitize the link |
|
69
|
|
|
*/ |
|
70
|
|
|
$link = $this->sanitize( $link ); |
|
71
|
|
|
/** |
|
72
|
|
|
* Make actual API DELETE cache request |
|
73
|
|
|
*/ |
|
74
|
|
|
$this->api_call_delete_cache( $link ); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @desc Sanitize the $link |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $link |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
private function sanitize( $link ) { |
|
87
|
|
|
return preg_replace( '/:\//i', '', $link ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @desc Do a DELETE request with WP request function |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $path path that goes after the URL eg. "/user/login" |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool True if successful otherwise false |
|
96
|
|
|
*/ |
|
97
|
|
|
private function api_call_delete_cache( $path ) { |
|
98
|
|
|
|
|
99
|
|
|
// Bailout if path is empty |
|
100
|
|
|
if ( empty( $path ) ) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$log = \Wordlift_Log_Service::get_logger( 'api_call_delete_cache' ); |
|
105
|
|
|
|
|
106
|
|
|
$log->debug( "Delete cache request started:: '$path'" ); |
|
107
|
|
|
|
|
108
|
|
|
$url = $this->configuration_service->get_api_url() . 'data/' . $path; |
|
109
|
|
|
$args = array( |
|
110
|
|
|
'method' => 'DELETE' |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
// Execute the request |
|
114
|
|
|
$api_response = wp_remote_request( $url, $args ); |
|
115
|
|
|
|
|
116
|
|
|
// If an error occured, return false |
|
117
|
|
|
if ( is_wp_error( $api_response ) || 200 !== (int) $api_response['response']['code'] ) { |
|
118
|
|
|
|
|
119
|
|
|
$log->warn( var_export( $api_response, true ) ); |
|
120
|
|
|
|
|
121
|
|
|
return false; |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$log->debug( "Request executed successfully" ); |
|
126
|
|
|
|
|
127
|
|
|
return true; |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.