Completed
Pull Request — develop (#1333)
by
unknown
02:59
created
src/wordlift/api-data/class-api-data-hooks.php 2 patches
Indentation   +135 added lines, -135 removed lines patch added patch discarded remove patch
@@ -11,139 +11,139 @@
 block discarded – undo
11 11
 
12 12
 class Api_Data_Hooks {
13 13
 
14
-	const META_KEY = 'wl_schema_url';
15
-
16
-	protected $cache_requests = array();
17
-
18
-	private $configuration_service;
19
-
20
-	public function __construct( $configuration_service ) {
21
-
22
-		$this->configuration_service = $configuration_service;
23
-		/**
24
-		 * Hook for Post Save
25
-		 */
26
-		add_action( 'save_post', array( $this, 'post_save_request_delete_cache' ) );
27
-		/**
28
-		 * Check for Meta Key change on Post Save
29
-		 */
30
-		add_action( 'updated_post_meta', array( $this, 'post_meta_request_delete_cache' ), 10, 4 );
31
-
32
-		/**
33
-		 * Initiate the purge cache requests
34
-		 */
35
-		add_action( 'shutdown', array( $this, 'init_purge_cache_requests' ) );
36
-	}
37
-
38
-	public function post_save_request_delete_cache( $post_id ) {
39
-		$this->delete_cache_for_meta_values( $post_id );
40
-	}
41
-
42
-	public function post_meta_request_delete_cache( $meta_id, $post_id, $meta_key, $_meta_value ) {
43
-		$this->cache_requests[] = $post_id;
44
-	}
45
-
46
-	public function init_purge_cache_requests() {
47
-		// Bail early.
48
-		if( empty( $this->cache_requests) ) {
49
-			return;
50
-		}
51
-
52
-		// Avoid duplicate cache requests.
53
-		$unique_requests = array_unique( $this->cache_requests );
54
-
55
-		foreach ( $unique_requests as $key => $post_id ) {
56
-			$this->delete_cache_for_meta_values( $post_id );
57
-		}
58
-	}
59
-
60
-	/**
61
-	 * @param integer $post_id
62
-	 *
63
-	 * @return
64
-	 *
65
-	 */
66
-	private function delete_cache_for_meta_values( $post_id ) {
67
-
68
-		/**
69
-		 * Get Post Meta Values
70
-		 */
71
-		$values = get_post_meta( $post_id, self::META_KEY, false );
72
-
73
-		/**
74
-		 * Iterate over $values array
75
-		 */
76
-		if ( ! empty( $values ) && count( $values ) > 1 ) {
77
-			foreach ( $values as $link ) {
78
-
79
-				/**
80
-				 * Skip the <permalink>
81
-				 */
82
-				if ( $link === '<permalink>' ) {
83
-					$link = get_permalink();
84
-				}
85
-
86
-				/**
87
-				 * Sanitize the link
88
-				 */
89
-				$link = $this->sanitize( $link );
90
-				/**
91
-				 * Make actual API DELETE cache request
92
-				 */
93
-				$this->api_call_delete_cache( $link );
94
-			}
95
-		}
96
-	}
97
-
98
-	/**
99
-	 * @desc Sanitize the $link
100
-	 *
101
-	 * @param string $link
102
-	 *
103
-	 * @return string
104
-	 */
105
-	private function sanitize( $link ) {
106
-		return preg_replace( '/:\//i', '', $link );
107
-	}
108
-
109
-	/**
110
-	 * @desc Do a DELETE request with WP request function
111
-	 *
112
-	 * @param string $path path that goes after the URL eg. "/user/login"
113
-	 *
114
-	 * @return bool  True if successful otherwise false
115
-	 */
116
-	private function api_call_delete_cache( $path ) {
117
-
118
-		// Bailout if path is empty
119
-		if ( empty( $path ) ) {
120
-			return false;
121
-		}
122
-
123
-		$log = \Wordlift_Log_Service::get_logger( 'api_call_delete_cache' );
124
-
125
-		$log->debug( "Delete cache request started:: '$path'" );
126
-
127
-		$url  = $this->configuration_service->get_api_url() . 'data/' . $path;
128
-		$args = array(
129
-			'method' => 'DELETE'
130
-		);
131
-
132
-		// Execute the request
133
-		$api_response = wp_remote_request( $url, $args );
134
-
135
-		// If an error occured, return false
136
-		if ( is_wp_error( $api_response ) || 200 !== (int) $api_response['response']['code'] ) {
137
-
138
-			$log->warn( var_export( $api_response, true ) );
139
-
140
-			return false;
141
-
142
-		}
143
-
144
-		$log->debug( "Request executed successfully" );
145
-
146
-		return true;
147
-
148
-	}
14
+    const META_KEY = 'wl_schema_url';
15
+
16
+    protected $cache_requests = array();
17
+
18
+    private $configuration_service;
19
+
20
+    public function __construct( $configuration_service ) {
21
+
22
+        $this->configuration_service = $configuration_service;
23
+        /**
24
+         * Hook for Post Save
25
+         */
26
+        add_action( 'save_post', array( $this, 'post_save_request_delete_cache' ) );
27
+        /**
28
+         * Check for Meta Key change on Post Save
29
+         */
30
+        add_action( 'updated_post_meta', array( $this, 'post_meta_request_delete_cache' ), 10, 4 );
31
+
32
+        /**
33
+         * Initiate the purge cache requests
34
+         */
35
+        add_action( 'shutdown', array( $this, 'init_purge_cache_requests' ) );
36
+    }
37
+
38
+    public function post_save_request_delete_cache( $post_id ) {
39
+        $this->delete_cache_for_meta_values( $post_id );
40
+    }
41
+
42
+    public function post_meta_request_delete_cache( $meta_id, $post_id, $meta_key, $_meta_value ) {
43
+        $this->cache_requests[] = $post_id;
44
+    }
45
+
46
+    public function init_purge_cache_requests() {
47
+        // Bail early.
48
+        if( empty( $this->cache_requests) ) {
49
+            return;
50
+        }
51
+
52
+        // Avoid duplicate cache requests.
53
+        $unique_requests = array_unique( $this->cache_requests );
54
+
55
+        foreach ( $unique_requests as $key => $post_id ) {
56
+            $this->delete_cache_for_meta_values( $post_id );
57
+        }
58
+    }
59
+
60
+    /**
61
+     * @param integer $post_id
62
+     *
63
+     * @return
64
+     *
65
+     */
66
+    private function delete_cache_for_meta_values( $post_id ) {
67
+
68
+        /**
69
+         * Get Post Meta Values
70
+         */
71
+        $values = get_post_meta( $post_id, self::META_KEY, false );
72
+
73
+        /**
74
+         * Iterate over $values array
75
+         */
76
+        if ( ! empty( $values ) && count( $values ) > 1 ) {
77
+            foreach ( $values as $link ) {
78
+
79
+                /**
80
+                 * Skip the <permalink>
81
+                 */
82
+                if ( $link === '<permalink>' ) {
83
+                    $link = get_permalink();
84
+                }
85
+
86
+                /**
87
+                 * Sanitize the link
88
+                 */
89
+                $link = $this->sanitize( $link );
90
+                /**
91
+                 * Make actual API DELETE cache request
92
+                 */
93
+                $this->api_call_delete_cache( $link );
94
+            }
95
+        }
96
+    }
97
+
98
+    /**
99
+     * @desc Sanitize the $link
100
+     *
101
+     * @param string $link
102
+     *
103
+     * @return string
104
+     */
105
+    private function sanitize( $link ) {
106
+        return preg_replace( '/:\//i', '', $link );
107
+    }
108
+
109
+    /**
110
+     * @desc Do a DELETE request with WP request function
111
+     *
112
+     * @param string $path path that goes after the URL eg. "/user/login"
113
+     *
114
+     * @return bool  True if successful otherwise false
115
+     */
116
+    private function api_call_delete_cache( $path ) {
117
+
118
+        // Bailout if path is empty
119
+        if ( empty( $path ) ) {
120
+            return false;
121
+        }
122
+
123
+        $log = \Wordlift_Log_Service::get_logger( 'api_call_delete_cache' );
124
+
125
+        $log->debug( "Delete cache request started:: '$path'" );
126
+
127
+        $url  = $this->configuration_service->get_api_url() . 'data/' . $path;
128
+        $args = array(
129
+            'method' => 'DELETE'
130
+        );
131
+
132
+        // Execute the request
133
+        $api_response = wp_remote_request( $url, $args );
134
+
135
+        // If an error occured, return false
136
+        if ( is_wp_error( $api_response ) || 200 !== (int) $api_response['response']['code'] ) {
137
+
138
+            $log->warn( var_export( $api_response, true ) );
139
+
140
+            return false;
141
+
142
+        }
143
+
144
+        $log->debug( "Request executed successfully" );
145
+
146
+        return true;
147
+
148
+    }
149 149
 }
Please login to merge, or discard this patch.
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -17,43 +17,43 @@  discard block
 block discarded – undo
17 17
 
18 18
 	private $configuration_service;
19 19
 
20
-	public function __construct( $configuration_service ) {
20
+	public function __construct($configuration_service) {
21 21
 
22 22
 		$this->configuration_service = $configuration_service;
23 23
 		/**
24 24
 		 * Hook for Post Save
25 25
 		 */
26
-		add_action( 'save_post', array( $this, 'post_save_request_delete_cache' ) );
26
+		add_action('save_post', array($this, 'post_save_request_delete_cache'));
27 27
 		/**
28 28
 		 * Check for Meta Key change on Post Save
29 29
 		 */
30
-		add_action( 'updated_post_meta', array( $this, 'post_meta_request_delete_cache' ), 10, 4 );
30
+		add_action('updated_post_meta', array($this, 'post_meta_request_delete_cache'), 10, 4);
31 31
 
32 32
 		/**
33 33
 		 * Initiate the purge cache requests
34 34
 		 */
35
-		add_action( 'shutdown', array( $this, 'init_purge_cache_requests' ) );
35
+		add_action('shutdown', array($this, 'init_purge_cache_requests'));
36 36
 	}
37 37
 
38
-	public function post_save_request_delete_cache( $post_id ) {
39
-		$this->delete_cache_for_meta_values( $post_id );
38
+	public function post_save_request_delete_cache($post_id) {
39
+		$this->delete_cache_for_meta_values($post_id);
40 40
 	}
41 41
 
42
-	public function post_meta_request_delete_cache( $meta_id, $post_id, $meta_key, $_meta_value ) {
42
+	public function post_meta_request_delete_cache($meta_id, $post_id, $meta_key, $_meta_value) {
43 43
 		$this->cache_requests[] = $post_id;
44 44
 	}
45 45
 
46 46
 	public function init_purge_cache_requests() {
47 47
 		// Bail early.
48
-		if( empty( $this->cache_requests) ) {
48
+		if (empty($this->cache_requests)) {
49 49
 			return;
50 50
 		}
51 51
 
52 52
 		// Avoid duplicate cache requests.
53
-		$unique_requests = array_unique( $this->cache_requests );
53
+		$unique_requests = array_unique($this->cache_requests);
54 54
 
55
-		foreach ( $unique_requests as $key => $post_id ) {
56
-			$this->delete_cache_for_meta_values( $post_id );
55
+		foreach ($unique_requests as $key => $post_id) {
56
+			$this->delete_cache_for_meta_values($post_id);
57 57
 		}
58 58
 	}
59 59
 
@@ -63,34 +63,34 @@  discard block
 block discarded – undo
63 63
 	 * @return
64 64
 	 *
65 65
 	 */
66
-	private function delete_cache_for_meta_values( $post_id ) {
66
+	private function delete_cache_for_meta_values($post_id) {
67 67
 
68 68
 		/**
69 69
 		 * Get Post Meta Values
70 70
 		 */
71
-		$values = get_post_meta( $post_id, self::META_KEY, false );
71
+		$values = get_post_meta($post_id, self::META_KEY, false);
72 72
 
73 73
 		/**
74 74
 		 * Iterate over $values array
75 75
 		 */
76
-		if ( ! empty( $values ) && count( $values ) > 1 ) {
77
-			foreach ( $values as $link ) {
76
+		if ( ! empty($values) && count($values) > 1) {
77
+			foreach ($values as $link) {
78 78
 
79 79
 				/**
80 80
 				 * Skip the <permalink>
81 81
 				 */
82
-				if ( $link === '<permalink>' ) {
82
+				if ($link === '<permalink>') {
83 83
 					$link = get_permalink();
84 84
 				}
85 85
 
86 86
 				/**
87 87
 				 * Sanitize the link
88 88
 				 */
89
-				$link = $this->sanitize( $link );
89
+				$link = $this->sanitize($link);
90 90
 				/**
91 91
 				 * Make actual API DELETE cache request
92 92
 				 */
93
-				$this->api_call_delete_cache( $link );
93
+				$this->api_call_delete_cache($link);
94 94
 			}
95 95
 		}
96 96
 	}
@@ -102,8 +102,8 @@  discard block
 block discarded – undo
102 102
 	 *
103 103
 	 * @return string
104 104
 	 */
105
-	private function sanitize( $link ) {
106
-		return preg_replace( '/:\//i', '', $link );
105
+	private function sanitize($link) {
106
+		return preg_replace('/:\//i', '', $link);
107 107
 	}
108 108
 
109 109
 	/**
@@ -113,35 +113,35 @@  discard block
 block discarded – undo
113 113
 	 *
114 114
 	 * @return bool  True if successful otherwise false
115 115
 	 */
116
-	private function api_call_delete_cache( $path ) {
116
+	private function api_call_delete_cache($path) {
117 117
 
118 118
 		// Bailout if path is empty
119
-		if ( empty( $path ) ) {
119
+		if (empty($path)) {
120 120
 			return false;
121 121
 		}
122 122
 
123
-		$log = \Wordlift_Log_Service::get_logger( 'api_call_delete_cache' );
123
+		$log = \Wordlift_Log_Service::get_logger('api_call_delete_cache');
124 124
 
125
-		$log->debug( "Delete cache request started:: '$path'" );
125
+		$log->debug("Delete cache request started:: '$path'");
126 126
 
127
-		$url  = $this->configuration_service->get_api_url() . 'data/' . $path;
127
+		$url  = $this->configuration_service->get_api_url().'data/'.$path;
128 128
 		$args = array(
129 129
 			'method' => 'DELETE'
130 130
 		);
131 131
 
132 132
 		// Execute the request
133
-		$api_response = wp_remote_request( $url, $args );
133
+		$api_response = wp_remote_request($url, $args);
134 134
 
135 135
 		// If an error occured, return false
136
-		if ( is_wp_error( $api_response ) || 200 !== (int) $api_response['response']['code'] ) {
136
+		if (is_wp_error($api_response) || 200 !== (int) $api_response['response']['code']) {
137 137
 
138
-			$log->warn( var_export( $api_response, true ) );
138
+			$log->warn(var_export($api_response, true));
139 139
 
140 140
 			return false;
141 141
 
142 142
 		}
143 143
 
144
-		$log->debug( "Request executed successfully" );
144
+		$log->debug("Request executed successfully");
145 145
 
146 146
 		return true;
147 147
 
Please login to merge, or discard this patch.