Completed
Pull Request — develop (#1554)
by Naveen
48s
created
src/wordlift/jsonld/class-jsonld-endpoint.php 2 patches
Indentation   +165 added lines, -165 removed lines patch added patch discarded remove patch
@@ -21,157 +21,157 @@  discard block
 block discarded – undo
21 21
  */
22 22
 class Jsonld_Endpoint {
23 23
 
24
-	/**
25
-	 * The {@link Wordlift_Jsonld_Service} instance.
26
-	 *
27
-	 * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance.
28
-	 */
29
-	private $jsonld_service;
30
-	/**
31
-	 * @var \Wordlift_Entity_Uri_Service
32
-	 */
33
-	private $entity_uri_service;
34
-
35
-	/**
36
-	 * Jsonld_Endpoint constructor.
37
-	 *
38
-	 * @param Jsonld_Service $jsonld_service
39
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service
40
-	 */
41
-	public function __construct( $jsonld_service, $entity_uri_service ) {
42
-
43
-		$this->jsonld_service     = $jsonld_service;
44
-		$this->entity_uri_service = $entity_uri_service;
45
-
46
-		// PHP 5.3 compatibility.
47
-		$that = $this;
48
-		add_action( 'rest_api_init', function () use ( $that ) {
49
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array(
50
-				'methods'             => WP_REST_Server::READABLE,
51
-				'callback'            => array( $that, 'jsonld_using_post_id' ),
52
-				'permission_callback' => '__return_true',
53
-				'args'                => array(
54
-					'id' => array(
55
-						'validate_callback' => function ( $param, $request, $key ) {
56
-							return is_numeric( $param );
57
-						},
58
-						'sanitize_callback' => 'absint',
59
-					),
60
-				)
61
-			) );
62
-
63
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array(
64
-				'methods'             => 'GET',
65
-				'callback'            => array( $that, 'jsonld_using_item_id' ),
66
-				'permission_callback' => '__return_true',
67
-			) );
68
-
69
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array(
70
-				'methods'             => 'GET',
71
-				'callback'            => array( $that, 'jsonld_using_post_meta' ),
72
-				'permission_callback' => '__return_true',
73
-			) );
74
-
75
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array(
76
-				'methods'             => 'GET',
77
-				'callback'            => array( $that, 'jsonld_using_meta' ),
78
-				'permission_callback' => '__return_true',
79
-			) );
80
-
81
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array(
82
-				'methods'             => 'GET',
83
-				'callback'            => array( $that, 'jsonld_using_get_page_by_path' ),
84
-				'permission_callback' => '__return_true',
85
-			) );
86
-
87
-		} );
88
-
89
-	}
90
-
91
-	/**
92
-	 * Callback for the JSON-LD request.
93
-	 *
94
-	 * @param array $request {
95
-	 *  The request array.
96
-	 *
97
-	 * @type int $id The post id.
98
-	 * }
99
-	 *
100
-	 * @return WP_REST_Response
101
-	 * @throws \Exception
102
-	 */
103
-	public function jsonld_using_post_id( $request ) {
104
-
105
-		$post_id = $request['id'];
106
-		$type    = ( 0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST );
107
-
108
-		// Send the generated JSON-LD.
109
-		$data = $this->jsonld_service->get( $type, $post_id, Jsonld_Context_Enum::REST );
110
-
111
-		return Jsonld_Response_Helper::to_response( $data );
112
-	}
113
-
114
-	/**
115
-	 * Provide a JSON-LD given the itemId.
116
-	 *
117
-	 * @param array $request {
118
-	 *  The request array.
119
-	 *
120
-	 * @type string $item_id The entity item id.
121
-	 * }
122
-	 *
123
-	 * @return WP_REST_Response
124
-	 * @throws \Exception
125
-	 */
126
-	public function jsonld_using_item_id( $request ) {
127
-
128
-		$item_id = 'http://' . $request['item_id'];
129
-		$post    = $this->entity_uri_service->get_entity( $item_id );
130
-
131
-		if ( ! is_a( $post, 'WP_Post' ) ) {
132
-			return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
133
-		}
134
-
135
-		return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) );
136
-	}
137
-
138
-	public function jsonld_using_get_page_by_path( $request ) {
139
-
140
-		$post_name = $request['post_name'];
141
-		$post_type = $request['post_type'];
142
-
143
-		global $wpdb;
144
-
145
-		$sql = "
24
+    /**
25
+     * The {@link Wordlift_Jsonld_Service} instance.
26
+     *
27
+     * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance.
28
+     */
29
+    private $jsonld_service;
30
+    /**
31
+     * @var \Wordlift_Entity_Uri_Service
32
+     */
33
+    private $entity_uri_service;
34
+
35
+    /**
36
+     * Jsonld_Endpoint constructor.
37
+     *
38
+     * @param Jsonld_Service $jsonld_service
39
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service
40
+     */
41
+    public function __construct( $jsonld_service, $entity_uri_service ) {
42
+
43
+        $this->jsonld_service     = $jsonld_service;
44
+        $this->entity_uri_service = $entity_uri_service;
45
+
46
+        // PHP 5.3 compatibility.
47
+        $that = $this;
48
+        add_action( 'rest_api_init', function () use ( $that ) {
49
+            register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array(
50
+                'methods'             => WP_REST_Server::READABLE,
51
+                'callback'            => array( $that, 'jsonld_using_post_id' ),
52
+                'permission_callback' => '__return_true',
53
+                'args'                => array(
54
+                    'id' => array(
55
+                        'validate_callback' => function ( $param, $request, $key ) {
56
+                            return is_numeric( $param );
57
+                        },
58
+                        'sanitize_callback' => 'absint',
59
+                    ),
60
+                )
61
+            ) );
62
+
63
+            register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array(
64
+                'methods'             => 'GET',
65
+                'callback'            => array( $that, 'jsonld_using_item_id' ),
66
+                'permission_callback' => '__return_true',
67
+            ) );
68
+
69
+            register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array(
70
+                'methods'             => 'GET',
71
+                'callback'            => array( $that, 'jsonld_using_post_meta' ),
72
+                'permission_callback' => '__return_true',
73
+            ) );
74
+
75
+            register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array(
76
+                'methods'             => 'GET',
77
+                'callback'            => array( $that, 'jsonld_using_meta' ),
78
+                'permission_callback' => '__return_true',
79
+            ) );
80
+
81
+            register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array(
82
+                'methods'             => 'GET',
83
+                'callback'            => array( $that, 'jsonld_using_get_page_by_path' ),
84
+                'permission_callback' => '__return_true',
85
+            ) );
86
+
87
+        } );
88
+
89
+    }
90
+
91
+    /**
92
+     * Callback for the JSON-LD request.
93
+     *
94
+     * @param array $request {
95
+     *  The request array.
96
+     *
97
+     * @type int $id The post id.
98
+     * }
99
+     *
100
+     * @return WP_REST_Response
101
+     * @throws \Exception
102
+     */
103
+    public function jsonld_using_post_id( $request ) {
104
+
105
+        $post_id = $request['id'];
106
+        $type    = ( 0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST );
107
+
108
+        // Send the generated JSON-LD.
109
+        $data = $this->jsonld_service->get( $type, $post_id, Jsonld_Context_Enum::REST );
110
+
111
+        return Jsonld_Response_Helper::to_response( $data );
112
+    }
113
+
114
+    /**
115
+     * Provide a JSON-LD given the itemId.
116
+     *
117
+     * @param array $request {
118
+     *  The request array.
119
+     *
120
+     * @type string $item_id The entity item id.
121
+     * }
122
+     *
123
+     * @return WP_REST_Response
124
+     * @throws \Exception
125
+     */
126
+    public function jsonld_using_item_id( $request ) {
127
+
128
+        $item_id = 'http://' . $request['item_id'];
129
+        $post    = $this->entity_uri_service->get_entity( $item_id );
130
+
131
+        if ( ! is_a( $post, 'WP_Post' ) ) {
132
+            return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
133
+        }
134
+
135
+        return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) );
136
+    }
137
+
138
+    public function jsonld_using_get_page_by_path( $request ) {
139
+
140
+        $post_name = $request['post_name'];
141
+        $post_type = $request['post_type'];
142
+
143
+        global $wpdb;
144
+
145
+        $sql = "
146 146
 			SELECT ID
147 147
 			FROM $wpdb->posts
148 148
 			WHERE post_name = %s
149 149
 			 AND post_type = %s
150 150
 		";
151 151
 
152
-		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) );
152
+        $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) );
153 153
 
154
-		if ( is_null( $post_id ) ) {
155
-			return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
156
-		}
154
+        if ( is_null( $post_id ) ) {
155
+            return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
156
+        }
157 157
 
158
-		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
159
-	}
158
+        return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
159
+    }
160 160
 
161
-	/**
162
-	 * @param WP_REST_Request $request
163
-	 *
164
-	 * @return WP_REST_Response
165
-	 * @throws \Exception
166
-	 */
167
-	public function jsonld_using_post_meta( $request ) {
161
+    /**
162
+     * @param WP_REST_Request $request
163
+     *
164
+     * @return WP_REST_Response
165
+     * @throws \Exception
166
+     */
167
+    public function jsonld_using_post_meta( $request ) {
168 168
 
169
-		$meta_key   = $request['meta_key'];
170
-		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
169
+        $meta_key   = $request['meta_key'];
170
+        $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
171 171
 
172
-		global $wpdb;
172
+        global $wpdb;
173 173
 
174
-		$sql = "
174
+        $sql = "
175 175
 			SELECT post_id AS ID
176 176
 			FROM $wpdb->postmeta
177 177
 			WHERE meta_key = %s
@@ -179,24 +179,24 @@  discard block
 block discarded – undo
179 179
 			LIMIT 1
180 180
 		";
181 181
 
182
-		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) );
182
+        $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) );
183 183
 
184
-		if ( is_null( $post_id ) ) {
185
-			return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) );
186
-		}
184
+        if ( is_null( $post_id ) ) {
185
+            return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) );
186
+        }
187 187
 
188
-		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
189
-	}
188
+        return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
189
+    }
190 190
 
191
-	public function jsonld_using_meta( $request ) {
191
+    public function jsonld_using_meta( $request ) {
192 192
 
193
-		global $wpdb;
193
+        global $wpdb;
194 194
 
195
-		$meta_key   = $request['meta_key'];
196
-		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
195
+        $meta_key   = $request['meta_key'];
196
+        $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
197 197
 
198
-		$results = $wpdb->get_results( $wpdb->prepare(
199
-			"
198
+        $results = $wpdb->get_results( $wpdb->prepare(
199
+            "
200 200
 			SELECT pm.post_id AS id, %s AS type
201 201
 			 FROM {$wpdb->postmeta} pm
202 202
 			 	INNER JOIN {$wpdb->posts} p
@@ -207,23 +207,23 @@  discard block
 block discarded – undo
207 207
 			 FROM {$wpdb->termmeta}
208 208
 			 WHERE meta_key = %s AND meta_value = %s
209 209
 			",
210
-			Object_Type_Enum::POST,
211
-			$meta_key,
212
-			$meta_value,
213
-			Object_Type_Enum::TERM,
214
-			$meta_key,
215
-			$meta_value
216
-		) );
210
+            Object_Type_Enum::POST,
211
+            $meta_key,
212
+            $meta_value,
213
+            Object_Type_Enum::TERM,
214
+            $meta_key,
215
+            $meta_value
216
+        ) );
217 217
 
218
-		$jsonld_service = $this->jsonld_service;
218
+        $jsonld_service = $this->jsonld_service;
219 219
 
220
-		$data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) {
221
-			$jsonld = $jsonld_service->get( $result->type, $result->id, Jsonld_Context_Enum::REST );
220
+        $data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) {
221
+            $jsonld = $jsonld_service->get( $result->type, $result->id, Jsonld_Context_Enum::REST );
222 222
 
223
-			return array_merge( $carry, $jsonld );
224
-		}, array() );
223
+            return array_merge( $carry, $jsonld );
224
+        }, array() );
225 225
 
226
-		return Jsonld_Response_Helper::to_response( $data );
227
-	}
226
+        return Jsonld_Response_Helper::to_response( $data );
227
+    }
228 228
 
229 229
 }
Please login to merge, or discard this patch.
Spacing   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -38,51 +38,51 @@  discard block
 block discarded – undo
38 38
 	 * @param Jsonld_Service $jsonld_service
39 39
 	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service
40 40
 	 */
41
-	public function __construct( $jsonld_service, $entity_uri_service ) {
41
+	public function __construct($jsonld_service, $entity_uri_service) {
42 42
 
43 43
 		$this->jsonld_service     = $jsonld_service;
44 44
 		$this->entity_uri_service = $entity_uri_service;
45 45
 
46 46
 		// PHP 5.3 compatibility.
47 47
 		$that = $this;
48
-		add_action( 'rest_api_init', function () use ( $that ) {
49
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array(
48
+		add_action('rest_api_init', function() use ($that) {
49
+			register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array(
50 50
 				'methods'             => WP_REST_Server::READABLE,
51
-				'callback'            => array( $that, 'jsonld_using_post_id' ),
51
+				'callback'            => array($that, 'jsonld_using_post_id'),
52 52
 				'permission_callback' => '__return_true',
53 53
 				'args'                => array(
54 54
 					'id' => array(
55
-						'validate_callback' => function ( $param, $request, $key ) {
56
-							return is_numeric( $param );
55
+						'validate_callback' => function($param, $request, $key) {
56
+							return is_numeric($param);
57 57
 						},
58 58
 						'sanitize_callback' => 'absint',
59 59
 					),
60 60
 				)
61
-			) );
61
+			));
62 62
 
63
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array(
63
+			register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array(
64 64
 				'methods'             => 'GET',
65
-				'callback'            => array( $that, 'jsonld_using_item_id' ),
65
+				'callback'            => array($that, 'jsonld_using_item_id'),
66 66
 				'permission_callback' => '__return_true',
67
-			) );
67
+			));
68 68
 
69
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array(
69
+			register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array(
70 70
 				'methods'             => 'GET',
71
-				'callback'            => array( $that, 'jsonld_using_post_meta' ),
71
+				'callback'            => array($that, 'jsonld_using_post_meta'),
72 72
 				'permission_callback' => '__return_true',
73
-			) );
73
+			));
74 74
 
75
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array(
75
+			register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array(
76 76
 				'methods'             => 'GET',
77
-				'callback'            => array( $that, 'jsonld_using_meta' ),
77
+				'callback'            => array($that, 'jsonld_using_meta'),
78 78
 				'permission_callback' => '__return_true',
79
-			) );
79
+			));
80 80
 
81
-			register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array(
81
+			register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array(
82 82
 				'methods'             => 'GET',
83
-				'callback'            => array( $that, 'jsonld_using_get_page_by_path' ),
83
+				'callback'            => array($that, 'jsonld_using_get_page_by_path'),
84 84
 				'permission_callback' => '__return_true',
85
-			) );
85
+			));
86 86
 
87 87
 		} );
88 88
 
@@ -100,15 +100,15 @@  discard block
 block discarded – undo
100 100
 	 * @return WP_REST_Response
101 101
 	 * @throws \Exception
102 102
 	 */
103
-	public function jsonld_using_post_id( $request ) {
103
+	public function jsonld_using_post_id($request) {
104 104
 
105 105
 		$post_id = $request['id'];
106
-		$type    = ( 0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST );
106
+		$type    = (0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST);
107 107
 
108 108
 		// Send the generated JSON-LD.
109
-		$data = $this->jsonld_service->get( $type, $post_id, Jsonld_Context_Enum::REST );
109
+		$data = $this->jsonld_service->get($type, $post_id, Jsonld_Context_Enum::REST);
110 110
 
111
-		return Jsonld_Response_Helper::to_response( $data );
111
+		return Jsonld_Response_Helper::to_response($data);
112 112
 	}
113 113
 
114 114
 	/**
@@ -123,19 +123,19 @@  discard block
 block discarded – undo
123 123
 	 * @return WP_REST_Response
124 124
 	 * @throws \Exception
125 125
 	 */
126
-	public function jsonld_using_item_id( $request ) {
126
+	public function jsonld_using_item_id($request) {
127 127
 
128
-		$item_id = 'http://' . $request['item_id'];
129
-		$post    = $this->entity_uri_service->get_entity( $item_id );
128
+		$item_id = 'http://'.$request['item_id'];
129
+		$post    = $this->entity_uri_service->get_entity($item_id);
130 130
 
131
-		if ( ! is_a( $post, 'WP_Post' ) ) {
132
-			return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
131
+		if ( ! is_a($post, 'WP_Post')) {
132
+			return new WP_REST_Response(esc_html("$item_id not found."), 404, array('Content-Type' => 'text/html'));
133 133
 		}
134 134
 
135
-		return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) );
135
+		return $this->jsonld_using_post_id(array('id' => $post->ID,));
136 136
 	}
137 137
 
138
-	public function jsonld_using_get_page_by_path( $request ) {
138
+	public function jsonld_using_get_page_by_path($request) {
139 139
 
140 140
 		$post_name = $request['post_name'];
141 141
 		$post_type = $request['post_type'];
@@ -149,13 +149,13 @@  discard block
 block discarded – undo
149 149
 			 AND post_type = %s
150 150
 		";
151 151
 
152
-		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) );
152
+		$post_id = $wpdb->get_var($wpdb->prepare($sql, $post_name, $post_type));
153 153
 
154
-		if ( is_null( $post_id ) ) {
155
-			return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
154
+		if (is_null($post_id)) {
155
+			return new WP_REST_Response(esc_html("$post_name of type $post_type not found."), 404, array('Content-Type' => 'text/html'));
156 156
 		}
157 157
 
158
-		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
158
+		return $this->jsonld_using_post_id(array('id' => $post_id,));
159 159
 	}
160 160
 
161 161
 	/**
@@ -164,10 +164,10 @@  discard block
 block discarded – undo
164 164
 	 * @return WP_REST_Response
165 165
 	 * @throws \Exception
166 166
 	 */
167
-	public function jsonld_using_post_meta( $request ) {
167
+	public function jsonld_using_post_meta($request) {
168 168
 
169 169
 		$meta_key   = $request['meta_key'];
170
-		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
170
+		$meta_value = urldecode(current($request->get_query_params('meta_value')));
171 171
 
172 172
 		global $wpdb;
173 173
 
@@ -179,23 +179,23 @@  discard block
 block discarded – undo
179 179
 			LIMIT 1
180 180
 		";
181 181
 
182
-		$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) );
182
+		$post_id = $wpdb->get_var($wpdb->prepare($sql, $meta_key, $meta_value));
183 183
 
184
-		if ( is_null( $post_id ) ) {
185
-			return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) );
184
+		if (is_null($post_id)) {
185
+			return new WP_REST_Response(esc_html("Post with meta key $meta_key and value $meta_value not found."), 404, array('Content-Type' => 'text/html'));
186 186
 		}
187 187
 
188
-		return $this->jsonld_using_post_id( array( 'id' => $post_id, ) );
188
+		return $this->jsonld_using_post_id(array('id' => $post_id,));
189 189
 	}
190 190
 
191
-	public function jsonld_using_meta( $request ) {
191
+	public function jsonld_using_meta($request) {
192 192
 
193 193
 		global $wpdb;
194 194
 
195 195
 		$meta_key   = $request['meta_key'];
196
-		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
196
+		$meta_value = urldecode(current($request->get_query_params('meta_value')));
197 197
 
198
-		$results = $wpdb->get_results( $wpdb->prepare(
198
+		$results = $wpdb->get_results($wpdb->prepare(
199 199
 			"
200 200
 			SELECT pm.post_id AS id, %s AS type
201 201
 			 FROM {$wpdb->postmeta} pm
@@ -213,17 +213,17 @@  discard block
 block discarded – undo
213 213
 			Object_Type_Enum::TERM,
214 214
 			$meta_key,
215 215
 			$meta_value
216
-		) );
216
+		));
217 217
 
218 218
 		$jsonld_service = $this->jsonld_service;
219 219
 
220
-		$data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) {
221
-			$jsonld = $jsonld_service->get( $result->type, $result->id, Jsonld_Context_Enum::REST );
220
+		$data = array_reduce($results, function($carry, $result) use ($jsonld_service) {
221
+			$jsonld = $jsonld_service->get($result->type, $result->id, Jsonld_Context_Enum::REST);
222 222
 
223
-			return array_merge( $carry, $jsonld );
224
-		}, array() );
223
+			return array_merge($carry, $jsonld);
224
+		}, array());
225 225
 
226
-		return Jsonld_Response_Helper::to_response( $data );
226
+		return Jsonld_Response_Helper::to_response($data);
227 227
 	}
228 228
 
229 229
 }
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-jsonld-context-enum.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -4,13 +4,13 @@
 block discarded – undo
4 4
 
5 5
 class Jsonld_Context_Enum {
6 6
 
7
-	const UNKNOWN = 0;
8
-	/** Web Page output (typically in the head). */
9
-	const PAGE = 1;
10
-	/** Carousel. */
11
-	const CAROUSEL = 2;
12
-	const FAQ = 3;
13
-	/** REST services. */
14
-	const REST = 3;
7
+    const UNKNOWN = 0;
8
+    /** Web Page output (typically in the head). */
9
+    const PAGE = 1;
10
+    /** Carousel. */
11
+    const CAROUSEL = 2;
12
+    const FAQ = 3;
13
+    /** REST services. */
14
+    const REST = 3;
15 15
 
16 16
 }
Please login to merge, or discard this patch.
src/wordlift/mappings/class-mappings-rest-controller.php 2 patches
Indentation   +458 added lines, -458 removed lines patch added patch discarded remove patch
@@ -13,135 +13,135 @@  discard block
 block discarded – undo
13 13
  * @subpackage Wordlift/includes/sync-mappings
14 14
  */
15 15
 class Mappings_REST_Controller {
16
-	// Namespace for CRUD mappings.
17
-	const MAPPINGS_NAMESPACE = '/mappings';
18
-
19
-	/**
20
-	 * Registers route on rest api initialisation.
21
-	 */
22
-	public static function register_routes() {
23
-
24
-		add_action( 'rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback' );
25
-
26
-	}
27
-
28
-	/**
29
-	 * Get a single mapping item by its mapping_id
30
-	 *
31
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
32
-	 *
33
-	 * @return array
34
-	 */
35
-	public static function get_mapping_item( $request ) {
36
-		$dbo             = new Mappings_DBO();
37
-		$mapping_id      = $request['id'];
38
-		$mapping_id_data = array();
39
-		$rule_groups     = $dbo->get_rule_groups_by_mapping( $mapping_id );
40
-		$properties      = $dbo->get_properties( $mapping_id );
41
-		$mapping_row     = $dbo->get_mapping_item_data( $mapping_id );
42
-
43
-		$mapping_id_data['mapping_id']      = $mapping_id;
44
-		$mapping_id_data['property_list']   = $properties;
45
-		$mapping_id_data['rule_group_list'] = $rule_groups;
46
-		$mapping_id_data['mapping_title']   = $mapping_row['mapping_title'];
47
-
48
-		return $mapping_id_data;
49
-	}
50
-
51
-	/**
52
-	 * Register route call back function, called when rest api gets initialised
53
-	 *
54
-	 * @return void
55
-	 */
56
-	public static function register_route_callback() {
57
-		register_rest_route(
58
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
59
-			'/mappings',
60
-			array(
61
-				'methods'             => WP_REST_Server::CREATABLE,
62
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::insert_or_update_mapping_item',
63
-				'permission_callback' => function () {
64
-					return current_user_can( 'manage_options' );
65
-				},
66
-			)
67
-		);
68
-		// Get list of mapping items.
69
-		register_rest_route(
70
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
71
-			'/mappings',
72
-			array(
73
-				'methods'             => WP_REST_Server::READABLE,
74
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::list_mapping_items',
75
-				'permission_callback' => function () {
76
-					return current_user_can( 'manage_options' );
77
-				},
78
-			)
79
-		);
80
-
81
-		// Delete mapping items by id.
82
-		register_rest_route(
83
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
84
-			'mappings',
85
-			array(
86
-				'methods'             => WP_REST_Server::DELETABLE,
87
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::delete_mapping_items',
88
-				'permission_callback' => function () {
89
-					return current_user_can( 'manage_options' );
90
-				},
91
-			)
92
-		);
93
-
94
-		// Get single mapping item route.
95
-		register_rest_route(
96
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
97
-			'mappings/(?P<id>\d+)',
98
-			array(
99
-				'methods'             => WP_REST_Server::READABLE,
100
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_mapping_item',
101
-				'permission_callback' => function () {
102
-					return current_user_can( 'manage_options' );
103
-				},
104
-			)
105
-		);
106
-
107
-		// Update mapping items.
108
-		register_rest_route(
109
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
110
-			'mappings',
111
-			array(
112
-				'methods'             => WP_REST_Server::EDITABLE,
113
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::update_mapping_items',
114
-				'permission_callback' => function () {
115
-					return current_user_can( 'manage_options' );
116
-				},
117
-			)
118
-		);
119
-
120
-		// Clone mapping items.
121
-		register_rest_route(
122
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
123
-			'mappings/clone',
124
-			array(
125
-				'methods'             => WP_REST_Server::CREATABLE,
126
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::clone_mapping_items',
127
-				'permission_callback' => function () {
128
-					return current_user_can( 'manage_options' );
129
-				},
130
-			)
131
-		);
132
-
133
-		// Register rest endpoint to get the terms.
134
-		register_rest_route(
135
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
136
-			'mappings/get_terms',
137
-			array(
138
-				'methods'             => WP_REST_Server::CREATABLE,
139
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_terms_for_the_posted_taxonomy',
140
-				'permission_callback' => function () {
141
-					return current_user_can( 'manage_options' );
142
-				},
143
-			)
144
-		);
16
+    // Namespace for CRUD mappings.
17
+    const MAPPINGS_NAMESPACE = '/mappings';
18
+
19
+    /**
20
+     * Registers route on rest api initialisation.
21
+     */
22
+    public static function register_routes() {
23
+
24
+        add_action( 'rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback' );
25
+
26
+    }
27
+
28
+    /**
29
+     * Get a single mapping item by its mapping_id
30
+     *
31
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
32
+     *
33
+     * @return array
34
+     */
35
+    public static function get_mapping_item( $request ) {
36
+        $dbo             = new Mappings_DBO();
37
+        $mapping_id      = $request['id'];
38
+        $mapping_id_data = array();
39
+        $rule_groups     = $dbo->get_rule_groups_by_mapping( $mapping_id );
40
+        $properties      = $dbo->get_properties( $mapping_id );
41
+        $mapping_row     = $dbo->get_mapping_item_data( $mapping_id );
42
+
43
+        $mapping_id_data['mapping_id']      = $mapping_id;
44
+        $mapping_id_data['property_list']   = $properties;
45
+        $mapping_id_data['rule_group_list'] = $rule_groups;
46
+        $mapping_id_data['mapping_title']   = $mapping_row['mapping_title'];
47
+
48
+        return $mapping_id_data;
49
+    }
50
+
51
+    /**
52
+     * Register route call back function, called when rest api gets initialised
53
+     *
54
+     * @return void
55
+     */
56
+    public static function register_route_callback() {
57
+        register_rest_route(
58
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
59
+            '/mappings',
60
+            array(
61
+                'methods'             => WP_REST_Server::CREATABLE,
62
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::insert_or_update_mapping_item',
63
+                'permission_callback' => function () {
64
+                    return current_user_can( 'manage_options' );
65
+                },
66
+            )
67
+        );
68
+        // Get list of mapping items.
69
+        register_rest_route(
70
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
71
+            '/mappings',
72
+            array(
73
+                'methods'             => WP_REST_Server::READABLE,
74
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::list_mapping_items',
75
+                'permission_callback' => function () {
76
+                    return current_user_can( 'manage_options' );
77
+                },
78
+            )
79
+        );
80
+
81
+        // Delete mapping items by id.
82
+        register_rest_route(
83
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
84
+            'mappings',
85
+            array(
86
+                'methods'             => WP_REST_Server::DELETABLE,
87
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::delete_mapping_items',
88
+                'permission_callback' => function () {
89
+                    return current_user_can( 'manage_options' );
90
+                },
91
+            )
92
+        );
93
+
94
+        // Get single mapping item route.
95
+        register_rest_route(
96
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
97
+            'mappings/(?P<id>\d+)',
98
+            array(
99
+                'methods'             => WP_REST_Server::READABLE,
100
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_mapping_item',
101
+                'permission_callback' => function () {
102
+                    return current_user_can( 'manage_options' );
103
+                },
104
+            )
105
+        );
106
+
107
+        // Update mapping items.
108
+        register_rest_route(
109
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
110
+            'mappings',
111
+            array(
112
+                'methods'             => WP_REST_Server::EDITABLE,
113
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::update_mapping_items',
114
+                'permission_callback' => function () {
115
+                    return current_user_can( 'manage_options' );
116
+                },
117
+            )
118
+        );
119
+
120
+        // Clone mapping items.
121
+        register_rest_route(
122
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
123
+            'mappings/clone',
124
+            array(
125
+                'methods'             => WP_REST_Server::CREATABLE,
126
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::clone_mapping_items',
127
+                'permission_callback' => function () {
128
+                    return current_user_can( 'manage_options' );
129
+                },
130
+            )
131
+        );
132
+
133
+        // Register rest endpoint to get the terms.
134
+        register_rest_route(
135
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
136
+            'mappings/get_terms',
137
+            array(
138
+                'methods'             => WP_REST_Server::CREATABLE,
139
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_terms_for_the_posted_taxonomy',
140
+                'permission_callback' => function () {
141
+                    return current_user_can( 'manage_options' );
142
+                },
143
+            )
144
+        );
145 145
 
146 146
         // Register rest endpoint to get the terms.
147 147
         register_rest_route(
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
                 },
156 156
             )
157 157
         );
158
-	}
158
+    }
159 159
 
160 160
     /**
161 161
      * Get the taxonomy & terms
@@ -206,334 +206,334 @@  discard block
 block discarded – undo
206 206
         return $taxonomy_terms;
207 207
     }
208 208
 
209
-	/**
210
-	 * Get the terms for the posted taxonomy name.
211
-	 *
212
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
213
-	 *
214
-	 * @return array The array of the terms for the taxonomy.
215
-	 */
216
-	public static function get_terms_for_the_posted_taxonomy( $request ) {
217
-		$post_data = $request->get_params();
218
-		if ( ! array_key_exists( 'taxonomy', $post_data ) ) {
219
-			return array(
220
-				'status'  => 'failure',
221
-				'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' )
222
-			);
223
-		} else {
224
-			$taxonomy = $post_data['taxonomy'];
225
-			$terms    = get_terms( $taxonomy, array( 'hide_empty' => false, ) );
226
-			if ( is_wp_error( $terms ) ) {
227
-				// Return error response, if the taxonomy is not valid.
228
-				return array(
229
-					'status'  => 'failure',
230
-					'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' )
231
-				);
232
-			}
233
-
234
-			return $terms;
235
-		}
236
-	}
237
-
238
-	/**
239
-	 * Clone posted mapping items.
240
-	 *
241
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
242
-	 *
243
-	 * @return array
244
-	 */
245
-	public static function clone_mapping_items( $request ) {
246
-		$dbo           = new Mappings_DBO();
247
-		$post_data     = (array) $request->get_params();
248
-		$mapping_items = (array) $post_data['mapping_items'];
249
-		foreach ( $mapping_items as $mapping_item ) {
250
-			$mapping_id = (int) $mapping_item['mapping_id'];
251
-			// Clone the current mapping item.
252
-			$cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] );
253
-			// Clone all the rule groups.
254
-			$rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id );
255
-			// Clone all the properties.
256
-			$properties_to_be_cloned = $dbo->get_properties( $mapping_id );
257
-			foreach ( $properties_to_be_cloned as $property ) {
258
-				// Assign a new mapping id.
259
-				$property['mapping_id'] = $cloned_mapping_id;
260
-				// Removing this property id, since a new id needed to be created for
261
-				// new property.
262
-				unset( $property['property_id'] );
263
-				$dbo->insert_or_update_property( $property );
264
-			}
265
-			// Loop through the rule groups and insert them in table with the mapping id.
266
-			foreach ( $rule_groups_to_be_cloned as $rule_group ) {
267
-				$cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id );
268
-				$original_rules       = (array) $rule_group['rules'];
269
-				// Now we need to insert these rules for the cloned rule group id.
270
-				foreach ( $original_rules as $clone_rule ) {
271
-					// We should replace only rule group id in the cloned rules.
272
-					$clone_rule['rule_group_id'] = (int) $cloned_rule_group_id;
273
-					unset( $clone_rule['rule_id'] );
274
-					$dbo->insert_or_update_rule_item( $clone_rule );
275
-				}
276
-			}
277
-		}
278
-
279
-		return array(
280
-			'status'  => 'success',
281
-			'message' => __( 'Successfully cloned mapping items', 'wordlift' ),
282
-		);
283
-	}
284
-
285
-	/**
286
-	 * Update posted mapping items.
287
-	 *
288
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
289
-	 *
290
-	 * @return array
291
-	 */
292
-	public static function update_mapping_items( $request ) {
293
-		$dbo       = new Mappings_DBO();
294
-		$post_data = $request->get_params();
295
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
296
-			$mapping_items = (array) $post_data['mapping_items'];
297
-			foreach ( $mapping_items as $mapping_item ) {
298
-				$dbo->insert_or_update_mapping_item( $mapping_item );
299
-			}
300
-
301
-			return array(
302
-				'status'  => 'success',
303
-				'message' => __( 'Mapping items successfully updated', 'wordlift' ),
304
-			);
305
-		} else {
306
-			return array(
307
-				'status'  => 'failure',
308
-				'message' => __( 'Unable to update mapping item', 'wordlift' ),
309
-			);
310
-		}
311
-	}
312
-
313
-	/**
314
-	 * Delete mapping items by mapping id
315
-	 *
316
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
317
-	 *
318
-	 * @return array
319
-	 */
320
-	public static function delete_mapping_items( $request ) {
321
-		$dbo       = new Mappings_DBO();
322
-		$post_data = $request->get_params();
323
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
324
-			$mapping_items = (array) $post_data['mapping_items'];
325
-			foreach ( $mapping_items as $mapping_item ) {
326
-				$dbo->delete_mapping_item( $mapping_item['mapping_id'] );
327
-			}
328
-
329
-			return array(
330
-				'status'  => 'success',
331
-				'message' => __( 'successfully deleted mapping items', 'wordlift' )
332
-			);
333
-		} else {
334
-			return array(
335
-				'status'  => 'failure',
336
-				'message' => __( 'Unable to delete mapping items', 'wordlift' )
337
-			);
338
-		}
339
-	}
340
-
341
-	/**
342
-	 * Get all mapping items
343
-	 *
344
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
345
-	 *
346
-	 * @return array
347
-	 */
348
-	public static function list_mapping_items( $request ) {
349
-		$dbo = new Mappings_DBO();
350
-
351
-		return $dbo->get_mappings();
352
-	}
353
-
354
-	/**
355
-	 * Returns a array of rule ids for the rule group id
356
-	 *
357
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
358
-	 * @param int $rule_group_id Primary key of rule group table.
359
-	 *
360
-	 * @return array A list of rule ids.
361
-	 */
362
-	private static function get_rule_ids( $dbo, $rule_group_id ) {
363
-		$rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id );
364
-		$rule_ids        = array();
365
-		foreach ( $rule_rows_in_db as $rule_row ) {
366
-			array_push( $rule_ids, (int) $rule_row['rule_id'] );
367
-		}
368
-
369
-		return $rule_ids;
370
-	}
371
-
372
-	/**
373
-	 * Insert or update mapping item depends on data
374
-	 *
375
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
376
-	 * @param int $rule_group_id Refers to a rule group which this rule belongs to.
377
-	 * @param array $rule_list Array of rule  items.
378
-	 *
379
-	 * @return void
380
-	 */
381
-	private static function save_rules( $dbo, $rule_group_id, $rule_list ) {
382
-		$rule_ids = self::get_rule_ids( $dbo, $rule_group_id );
383
-		foreach ( $rule_list as $rule ) {
384
-			// Some rules may not have rule group id, because they are inserted
385
-			// in ui, so lets add them any way.
386
-			$rule['rule_group_id'] = $rule_group_id;
387
-			$dbo->insert_or_update_rule_item( $rule );
388
-			if ( array_key_exists( 'rule_id', $rule ) ) {
389
-				$index_to_be_removed = array_search(
390
-					(int) $rule['rule_id'],
391
-					$rule_ids,
392
-					true
393
-				);
394
-				if ( false !== $index_to_be_removed ) {
395
-					unset( $rule_ids[ $index_to_be_removed ] );
396
-				}
397
-			}
398
-		}
399
-		foreach ( $rule_ids as $rule_id ) {
400
-			// Delete all the rule ids which are not posted.
401
-			$dbo->delete_rule_item( $rule_id );
402
-		}
403
-	}
404
-
405
-	/**
406
-	 * Insert or update rule group list based on data
407
-	 *
408
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
409
-	 * @param int $mapping_id Primary key of mapping table.
410
-	 * @param array $property_list { Array of property items }.
411
-	 *
412
-	 * @return void
413
-	 */
414
-	private static function save_property_list( $dbo, $mapping_id, $property_list ) {
415
-		$properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id );
416
-		$property_ids                    = array();
417
-		foreach ( $properties_needed_to_be_deleted as $property ) {
418
-			array_push( $property_ids, (int) $property['property_id'] );
419
-		}
420
-		foreach ( $property_list as $property ) {
421
-			if ( array_key_exists( 'property_id', $property ) ) {
422
-				// Remove the id from the list of property ids needed to be deleted
423
-				// because it is posted.
424
-				$index_to_be_removed = array_search(
425
-					(int) $property['property_id'],
426
-					$property_ids,
427
-					true
428
-				);
429
-				if ( false !== $index_to_be_removed ) {
430
-					unset( $property_ids[ $index_to_be_removed ] );
431
-				}
432
-			}
433
-			// Add mapping id to property data.
434
-			$property['mapping_id'] = $mapping_id;
435
-			$dbo->insert_or_update_property( $property );
436
-
437
-		}
438
-		// At the end remove all the property ids which are not posted.
439
-		foreach ( $property_ids as $property_id ) {
440
-			$dbo->delete_property( $property_id );
441
-		}
442
-
443
-	}
444
-
445
-	/**
446
-	 * Returns a array of rule group ids for the mapping id
447
-	 *
448
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
449
-	 * @param int $mapping_id Primary key of mapping table.
450
-	 *
451
-	 * @return array $rule_group_ids A list of rule group ids.
452
-	 */
453
-	private static function get_rule_group_ids( $dbo, $mapping_id ) {
454
-		$rule_group_rows = $dbo->get_rule_group_list( $mapping_id );
455
-		$rule_group_ids  = array();
456
-		foreach ( $rule_group_rows as $rule_group_row ) {
457
-			array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] );
458
-		}
459
-
460
-		return $rule_group_ids;
461
-	}
462
-
463
-	/**
464
-	 * Insert or update rule group list
465
-	 *
466
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
467
-	 * @param int $mapping_id Primary key of mapping table.
468
-	 * @param array $rule_group_list { Array of rule group items }.
469
-	 *
470
-	 * @return void
471
-	 */
472
-	private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) {
473
-		// The rule groups not posted should be deleted.
474
-		$rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id );
475
-		// Loop through rule group list and save the rule group.
476
-		foreach ( $rule_group_list as $rule_group ) {
477
-			if ( array_key_exists( 'rule_group_id', $rule_group ) ) {
478
-				$rule_group_id = $rule_group['rule_group_id'];
479
-			} else {
480
-				// New rule group, should create new rule group id.
481
-				$rule_group_id = $dbo->insert_rule_group( $mapping_id );
482
-			}
483
-			$index_to_be_removed = array_search(
484
-				(int) $rule_group_id,
485
-				$rule_group_ids,
486
-				true
487
-			);
488
-			if ( false !== $index_to_be_removed ) {
489
-				unset( $rule_group_ids[ $index_to_be_removed ] );
490
-			}
491
-			self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] );
492
-		}
493
-
494
-		// Remove all the rule groups which are not posted.
495
-		foreach ( $rule_group_ids as $rule_group_id ) {
496
-			$dbo->delete_rule_group_item( $rule_group_id );
497
-		}
498
-	}
499
-
500
-	/**
501
-	 * Insert or update mapping item depends on data
502
-	 *
503
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
504
-	 *
505
-	 * @return array
506
-	 */
507
-	public static function insert_or_update_mapping_item( $request ) {
508
-		$post_data = $request->get_params() === null ? array() : $request->get_params();
509
-		$dbo       = new Mappings_DBO();
510
-		// check if valid object is posted.
511
-		if ( array_key_exists( 'mapping_title', $post_data ) &&
512
-		     array_key_exists( 'rule_group_list', $post_data ) &&
513
-		     array_key_exists( 'property_list', $post_data ) ) {
514
-			// Do validation, remove all incomplete data.
515
-			$mapping_item = array();
516
-			if ( array_key_exists( 'mapping_id', $post_data ) ) {
517
-				$mapping_item['mapping_id'] = $post_data['mapping_id'];
518
-			}
519
-			$mapping_item['mapping_title'] = $post_data['mapping_title'];
520
-			// lets save the mapping item.
521
-			$mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item );
522
-			self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] );
523
-			self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] );
524
-
525
-			return array(
526
-				'status'     => 'success',
527
-				'message'    => __( 'Successfully saved mapping item', 'wordlift' ),
528
-				'mapping_id' => (int) $mapping_id,
529
-			);
530
-		} else {
531
-			return array(
532
-				'status'  => 'error',
533
-				'message' => __( 'Unable to save mapping item', 'wordlift' ),
534
-			);
535
-		}
536
-	}
209
+    /**
210
+     * Get the terms for the posted taxonomy name.
211
+     *
212
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
213
+     *
214
+     * @return array The array of the terms for the taxonomy.
215
+     */
216
+    public static function get_terms_for_the_posted_taxonomy( $request ) {
217
+        $post_data = $request->get_params();
218
+        if ( ! array_key_exists( 'taxonomy', $post_data ) ) {
219
+            return array(
220
+                'status'  => 'failure',
221
+                'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' )
222
+            );
223
+        } else {
224
+            $taxonomy = $post_data['taxonomy'];
225
+            $terms    = get_terms( $taxonomy, array( 'hide_empty' => false, ) );
226
+            if ( is_wp_error( $terms ) ) {
227
+                // Return error response, if the taxonomy is not valid.
228
+                return array(
229
+                    'status'  => 'failure',
230
+                    'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' )
231
+                );
232
+            }
233
+
234
+            return $terms;
235
+        }
236
+    }
237
+
238
+    /**
239
+     * Clone posted mapping items.
240
+     *
241
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
242
+     *
243
+     * @return array
244
+     */
245
+    public static function clone_mapping_items( $request ) {
246
+        $dbo           = new Mappings_DBO();
247
+        $post_data     = (array) $request->get_params();
248
+        $mapping_items = (array) $post_data['mapping_items'];
249
+        foreach ( $mapping_items as $mapping_item ) {
250
+            $mapping_id = (int) $mapping_item['mapping_id'];
251
+            // Clone the current mapping item.
252
+            $cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] );
253
+            // Clone all the rule groups.
254
+            $rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id );
255
+            // Clone all the properties.
256
+            $properties_to_be_cloned = $dbo->get_properties( $mapping_id );
257
+            foreach ( $properties_to_be_cloned as $property ) {
258
+                // Assign a new mapping id.
259
+                $property['mapping_id'] = $cloned_mapping_id;
260
+                // Removing this property id, since a new id needed to be created for
261
+                // new property.
262
+                unset( $property['property_id'] );
263
+                $dbo->insert_or_update_property( $property );
264
+            }
265
+            // Loop through the rule groups and insert them in table with the mapping id.
266
+            foreach ( $rule_groups_to_be_cloned as $rule_group ) {
267
+                $cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id );
268
+                $original_rules       = (array) $rule_group['rules'];
269
+                // Now we need to insert these rules for the cloned rule group id.
270
+                foreach ( $original_rules as $clone_rule ) {
271
+                    // We should replace only rule group id in the cloned rules.
272
+                    $clone_rule['rule_group_id'] = (int) $cloned_rule_group_id;
273
+                    unset( $clone_rule['rule_id'] );
274
+                    $dbo->insert_or_update_rule_item( $clone_rule );
275
+                }
276
+            }
277
+        }
278
+
279
+        return array(
280
+            'status'  => 'success',
281
+            'message' => __( 'Successfully cloned mapping items', 'wordlift' ),
282
+        );
283
+    }
284
+
285
+    /**
286
+     * Update posted mapping items.
287
+     *
288
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
289
+     *
290
+     * @return array
291
+     */
292
+    public static function update_mapping_items( $request ) {
293
+        $dbo       = new Mappings_DBO();
294
+        $post_data = $request->get_params();
295
+        if ( array_key_exists( 'mapping_items', $post_data ) ) {
296
+            $mapping_items = (array) $post_data['mapping_items'];
297
+            foreach ( $mapping_items as $mapping_item ) {
298
+                $dbo->insert_or_update_mapping_item( $mapping_item );
299
+            }
300
+
301
+            return array(
302
+                'status'  => 'success',
303
+                'message' => __( 'Mapping items successfully updated', 'wordlift' ),
304
+            );
305
+        } else {
306
+            return array(
307
+                'status'  => 'failure',
308
+                'message' => __( 'Unable to update mapping item', 'wordlift' ),
309
+            );
310
+        }
311
+    }
312
+
313
+    /**
314
+     * Delete mapping items by mapping id
315
+     *
316
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
317
+     *
318
+     * @return array
319
+     */
320
+    public static function delete_mapping_items( $request ) {
321
+        $dbo       = new Mappings_DBO();
322
+        $post_data = $request->get_params();
323
+        if ( array_key_exists( 'mapping_items', $post_data ) ) {
324
+            $mapping_items = (array) $post_data['mapping_items'];
325
+            foreach ( $mapping_items as $mapping_item ) {
326
+                $dbo->delete_mapping_item( $mapping_item['mapping_id'] );
327
+            }
328
+
329
+            return array(
330
+                'status'  => 'success',
331
+                'message' => __( 'successfully deleted mapping items', 'wordlift' )
332
+            );
333
+        } else {
334
+            return array(
335
+                'status'  => 'failure',
336
+                'message' => __( 'Unable to delete mapping items', 'wordlift' )
337
+            );
338
+        }
339
+    }
340
+
341
+    /**
342
+     * Get all mapping items
343
+     *
344
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
345
+     *
346
+     * @return array
347
+     */
348
+    public static function list_mapping_items( $request ) {
349
+        $dbo = new Mappings_DBO();
350
+
351
+        return $dbo->get_mappings();
352
+    }
353
+
354
+    /**
355
+     * Returns a array of rule ids for the rule group id
356
+     *
357
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
358
+     * @param int $rule_group_id Primary key of rule group table.
359
+     *
360
+     * @return array A list of rule ids.
361
+     */
362
+    private static function get_rule_ids( $dbo, $rule_group_id ) {
363
+        $rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id );
364
+        $rule_ids        = array();
365
+        foreach ( $rule_rows_in_db as $rule_row ) {
366
+            array_push( $rule_ids, (int) $rule_row['rule_id'] );
367
+        }
368
+
369
+        return $rule_ids;
370
+    }
371
+
372
+    /**
373
+     * Insert or update mapping item depends on data
374
+     *
375
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
376
+     * @param int $rule_group_id Refers to a rule group which this rule belongs to.
377
+     * @param array $rule_list Array of rule  items.
378
+     *
379
+     * @return void
380
+     */
381
+    private static function save_rules( $dbo, $rule_group_id, $rule_list ) {
382
+        $rule_ids = self::get_rule_ids( $dbo, $rule_group_id );
383
+        foreach ( $rule_list as $rule ) {
384
+            // Some rules may not have rule group id, because they are inserted
385
+            // in ui, so lets add them any way.
386
+            $rule['rule_group_id'] = $rule_group_id;
387
+            $dbo->insert_or_update_rule_item( $rule );
388
+            if ( array_key_exists( 'rule_id', $rule ) ) {
389
+                $index_to_be_removed = array_search(
390
+                    (int) $rule['rule_id'],
391
+                    $rule_ids,
392
+                    true
393
+                );
394
+                if ( false !== $index_to_be_removed ) {
395
+                    unset( $rule_ids[ $index_to_be_removed ] );
396
+                }
397
+            }
398
+        }
399
+        foreach ( $rule_ids as $rule_id ) {
400
+            // Delete all the rule ids which are not posted.
401
+            $dbo->delete_rule_item( $rule_id );
402
+        }
403
+    }
404
+
405
+    /**
406
+     * Insert or update rule group list based on data
407
+     *
408
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
409
+     * @param int $mapping_id Primary key of mapping table.
410
+     * @param array $property_list { Array of property items }.
411
+     *
412
+     * @return void
413
+     */
414
+    private static function save_property_list( $dbo, $mapping_id, $property_list ) {
415
+        $properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id );
416
+        $property_ids                    = array();
417
+        foreach ( $properties_needed_to_be_deleted as $property ) {
418
+            array_push( $property_ids, (int) $property['property_id'] );
419
+        }
420
+        foreach ( $property_list as $property ) {
421
+            if ( array_key_exists( 'property_id', $property ) ) {
422
+                // Remove the id from the list of property ids needed to be deleted
423
+                // because it is posted.
424
+                $index_to_be_removed = array_search(
425
+                    (int) $property['property_id'],
426
+                    $property_ids,
427
+                    true
428
+                );
429
+                if ( false !== $index_to_be_removed ) {
430
+                    unset( $property_ids[ $index_to_be_removed ] );
431
+                }
432
+            }
433
+            // Add mapping id to property data.
434
+            $property['mapping_id'] = $mapping_id;
435
+            $dbo->insert_or_update_property( $property );
436
+
437
+        }
438
+        // At the end remove all the property ids which are not posted.
439
+        foreach ( $property_ids as $property_id ) {
440
+            $dbo->delete_property( $property_id );
441
+        }
442
+
443
+    }
444
+
445
+    /**
446
+     * Returns a array of rule group ids for the mapping id
447
+     *
448
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
449
+     * @param int $mapping_id Primary key of mapping table.
450
+     *
451
+     * @return array $rule_group_ids A list of rule group ids.
452
+     */
453
+    private static function get_rule_group_ids( $dbo, $mapping_id ) {
454
+        $rule_group_rows = $dbo->get_rule_group_list( $mapping_id );
455
+        $rule_group_ids  = array();
456
+        foreach ( $rule_group_rows as $rule_group_row ) {
457
+            array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] );
458
+        }
459
+
460
+        return $rule_group_ids;
461
+    }
462
+
463
+    /**
464
+     * Insert or update rule group list
465
+     *
466
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
467
+     * @param int $mapping_id Primary key of mapping table.
468
+     * @param array $rule_group_list { Array of rule group items }.
469
+     *
470
+     * @return void
471
+     */
472
+    private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) {
473
+        // The rule groups not posted should be deleted.
474
+        $rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id );
475
+        // Loop through rule group list and save the rule group.
476
+        foreach ( $rule_group_list as $rule_group ) {
477
+            if ( array_key_exists( 'rule_group_id', $rule_group ) ) {
478
+                $rule_group_id = $rule_group['rule_group_id'];
479
+            } else {
480
+                // New rule group, should create new rule group id.
481
+                $rule_group_id = $dbo->insert_rule_group( $mapping_id );
482
+            }
483
+            $index_to_be_removed = array_search(
484
+                (int) $rule_group_id,
485
+                $rule_group_ids,
486
+                true
487
+            );
488
+            if ( false !== $index_to_be_removed ) {
489
+                unset( $rule_group_ids[ $index_to_be_removed ] );
490
+            }
491
+            self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] );
492
+        }
493
+
494
+        // Remove all the rule groups which are not posted.
495
+        foreach ( $rule_group_ids as $rule_group_id ) {
496
+            $dbo->delete_rule_group_item( $rule_group_id );
497
+        }
498
+    }
499
+
500
+    /**
501
+     * Insert or update mapping item depends on data
502
+     *
503
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
504
+     *
505
+     * @return array
506
+     */
507
+    public static function insert_or_update_mapping_item( $request ) {
508
+        $post_data = $request->get_params() === null ? array() : $request->get_params();
509
+        $dbo       = new Mappings_DBO();
510
+        // check if valid object is posted.
511
+        if ( array_key_exists( 'mapping_title', $post_data ) &&
512
+             array_key_exists( 'rule_group_list', $post_data ) &&
513
+             array_key_exists( 'property_list', $post_data ) ) {
514
+            // Do validation, remove all incomplete data.
515
+            $mapping_item = array();
516
+            if ( array_key_exists( 'mapping_id', $post_data ) ) {
517
+                $mapping_item['mapping_id'] = $post_data['mapping_id'];
518
+            }
519
+            $mapping_item['mapping_title'] = $post_data['mapping_title'];
520
+            // lets save the mapping item.
521
+            $mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item );
522
+            self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] );
523
+            self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] );
524
+
525
+            return array(
526
+                'status'     => 'success',
527
+                'message'    => __( 'Successfully saved mapping item', 'wordlift' ),
528
+                'mapping_id' => (int) $mapping_id,
529
+            );
530
+        } else {
531
+            return array(
532
+                'status'  => 'error',
533
+                'message' => __( 'Unable to save mapping item', 'wordlift' ),
534
+            );
535
+        }
536
+    }
537 537
 }
538 538
 
539 539
 Mappings_REST_Controller::register_routes();
Please login to merge, or discard this patch.
Spacing   +108 added lines, -108 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@  discard block
 block discarded – undo
21 21
 	 */
22 22
 	public static function register_routes() {
23 23
 
24
-		add_action( 'rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback' );
24
+		add_action('rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback');
25 25
 
26 26
 	}
27 27
 
@@ -32,13 +32,13 @@  discard block
 block discarded – undo
32 32
 	 *
33 33
 	 * @return array
34 34
 	 */
35
-	public static function get_mapping_item( $request ) {
35
+	public static function get_mapping_item($request) {
36 36
 		$dbo             = new Mappings_DBO();
37 37
 		$mapping_id      = $request['id'];
38 38
 		$mapping_id_data = array();
39
-		$rule_groups     = $dbo->get_rule_groups_by_mapping( $mapping_id );
40
-		$properties      = $dbo->get_properties( $mapping_id );
41
-		$mapping_row     = $dbo->get_mapping_item_data( $mapping_id );
39
+		$rule_groups     = $dbo->get_rule_groups_by_mapping($mapping_id);
40
+		$properties      = $dbo->get_properties($mapping_id);
41
+		$mapping_row     = $dbo->get_mapping_item_data($mapping_id);
42 42
 
43 43
 		$mapping_id_data['mapping_id']      = $mapping_id;
44 44
 		$mapping_id_data['property_list']   = $properties;
@@ -60,8 +60,8 @@  discard block
 block discarded – undo
60 60
 			array(
61 61
 				'methods'             => WP_REST_Server::CREATABLE,
62 62
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::insert_or_update_mapping_item',
63
-				'permission_callback' => function () {
64
-					return current_user_can( 'manage_options' );
63
+				'permission_callback' => function() {
64
+					return current_user_can('manage_options');
65 65
 				},
66 66
 			)
67 67
 		);
@@ -72,8 +72,8 @@  discard block
 block discarded – undo
72 72
 			array(
73 73
 				'methods'             => WP_REST_Server::READABLE,
74 74
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::list_mapping_items',
75
-				'permission_callback' => function () {
76
-					return current_user_can( 'manage_options' );
75
+				'permission_callback' => function() {
76
+					return current_user_can('manage_options');
77 77
 				},
78 78
 			)
79 79
 		);
@@ -85,8 +85,8 @@  discard block
 block discarded – undo
85 85
 			array(
86 86
 				'methods'             => WP_REST_Server::DELETABLE,
87 87
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::delete_mapping_items',
88
-				'permission_callback' => function () {
89
-					return current_user_can( 'manage_options' );
88
+				'permission_callback' => function() {
89
+					return current_user_can('manage_options');
90 90
 				},
91 91
 			)
92 92
 		);
@@ -98,8 +98,8 @@  discard block
 block discarded – undo
98 98
 			array(
99 99
 				'methods'             => WP_REST_Server::READABLE,
100 100
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_mapping_item',
101
-				'permission_callback' => function () {
102
-					return current_user_can( 'manage_options' );
101
+				'permission_callback' => function() {
102
+					return current_user_can('manage_options');
103 103
 				},
104 104
 			)
105 105
 		);
@@ -111,8 +111,8 @@  discard block
 block discarded – undo
111 111
 			array(
112 112
 				'methods'             => WP_REST_Server::EDITABLE,
113 113
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::update_mapping_items',
114
-				'permission_callback' => function () {
115
-					return current_user_can( 'manage_options' );
114
+				'permission_callback' => function() {
115
+					return current_user_can('manage_options');
116 116
 				},
117 117
 			)
118 118
 		);
@@ -124,8 +124,8 @@  discard block
 block discarded – undo
124 124
 			array(
125 125
 				'methods'             => WP_REST_Server::CREATABLE,
126 126
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::clone_mapping_items',
127
-				'permission_callback' => function () {
128
-					return current_user_can( 'manage_options' );
127
+				'permission_callback' => function() {
128
+					return current_user_can('manage_options');
129 129
 				},
130 130
 			)
131 131
 		);
@@ -137,8 +137,8 @@  discard block
 block discarded – undo
137 137
 			array(
138 138
 				'methods'             => WP_REST_Server::CREATABLE,
139 139
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_terms_for_the_posted_taxonomy',
140
-				'permission_callback' => function () {
141
-					return current_user_can( 'manage_options' );
140
+				'permission_callback' => function() {
141
+					return current_user_can('manage_options');
142 142
 				},
143 143
 			)
144 144
 		);
@@ -150,8 +150,8 @@  discard block
 block discarded – undo
150 150
             array(
151 151
                 'methods'             => WP_REST_Server::CREATABLE,
152 152
                 'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_taxonomy_terms_for_the_posted_taxonomy',
153
-                'permission_callback' => function () {
154
-                    return current_user_can( 'manage_options' );
153
+                'permission_callback' => function() {
154
+                    return current_user_can('manage_options');
155 155
                 },
156 156
             )
157 157
         );
@@ -164,11 +164,11 @@  discard block
 block discarded – undo
164 164
      *
165 165
      * @return array The array of the taxonomies & terms.
166 166
      */
167
-    public static function get_taxonomy_terms_for_the_posted_taxonomy( $request ) {
167
+    public static function get_taxonomy_terms_for_the_posted_taxonomy($request) {
168 168
         $taxonomy_terms = array();
169
-        $post_taxonomies = get_taxonomies( array(), 'objects' );
169
+        $post_taxonomies = get_taxonomies(array(), 'objects');
170 170
 
171
-        foreach ( $post_taxonomies as $post_taxonomy ) {
171
+        foreach ($post_taxonomies as $post_taxonomy) {
172 172
             $taxonomy_config = array(
173 173
                 'taxonomy' => $post_taxonomy->name,
174 174
                 'hide_empty' => false
@@ -176,7 +176,7 @@  discard block
 block discarded – undo
176 176
 
177 177
             $total_terms = wp_count_terms($taxonomy_config);
178 178
 
179
-            $post_taxonomy_terms = get_terms( $taxonomy_config );
179
+            $post_taxonomy_terms = get_terms($taxonomy_config);
180 180
 
181 181
             if ($total_terms) {
182 182
                 $group_taxonomy = array('parentValue' => 'post_taxonomy', 'group_name' => $post_taxonomy->label, 'group_options' => array());
@@ -184,7 +184,7 @@  discard block
 block discarded – undo
184 184
                 foreach ($post_taxonomy_terms as $post_taxonomy_term) {
185 185
                     array_push($group_taxonomy['group_options'],
186 186
                         array(
187
-                            'label' => ' - ' . $post_taxonomy_term->name,
187
+                            'label' => ' - '.$post_taxonomy_term->name,
188 188
                             'value' => $post_taxonomy_term->slug,
189 189
                             'taxonomy' => 'post_taxonomy',
190 190
                         )
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 
198 198
                         array_push($group_taxonomy['group_options'],
199 199
                             array(
200
-                                'label' => ' -- ' . $child_term->name,
200
+                                'label' => ' -- '.$child_term->name,
201 201
                                 'value' => $child_term->slug,
202 202
                                 'taxonomy' => 'post_taxonomy',
203 203
                             )
@@ -218,21 +218,21 @@  discard block
 block discarded – undo
218 218
 	 *
219 219
 	 * @return array The array of the terms for the taxonomy.
220 220
 	 */
221
-	public static function get_terms_for_the_posted_taxonomy( $request ) {
221
+	public static function get_terms_for_the_posted_taxonomy($request) {
222 222
 		$post_data = $request->get_params();
223
-		if ( ! array_key_exists( 'taxonomy', $post_data ) ) {
223
+		if ( ! array_key_exists('taxonomy', $post_data)) {
224 224
 			return array(
225 225
 				'status'  => 'failure',
226
-				'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' )
226
+				'message' => __('Request not valid, must post a taxonomy to get terms', 'wordlift')
227 227
 			);
228 228
 		} else {
229 229
 			$taxonomy = $post_data['taxonomy'];
230
-			$terms    = get_terms( $taxonomy, array( 'hide_empty' => false, ) );
231
-			if ( is_wp_error( $terms ) ) {
230
+			$terms    = get_terms($taxonomy, array('hide_empty' => false,));
231
+			if (is_wp_error($terms)) {
232 232
 				// Return error response, if the taxonomy is not valid.
233 233
 				return array(
234 234
 					'status'  => 'failure',
235
-					'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' )
235
+					'message' => __('Request not valid, must post a valid taxonomy', 'wordlift')
236 236
 				);
237 237
 			}
238 238
 
@@ -247,43 +247,43 @@  discard block
 block discarded – undo
247 247
 	 *
248 248
 	 * @return array
249 249
 	 */
250
-	public static function clone_mapping_items( $request ) {
250
+	public static function clone_mapping_items($request) {
251 251
 		$dbo           = new Mappings_DBO();
252 252
 		$post_data     = (array) $request->get_params();
253 253
 		$mapping_items = (array) $post_data['mapping_items'];
254
-		foreach ( $mapping_items as $mapping_item ) {
254
+		foreach ($mapping_items as $mapping_item) {
255 255
 			$mapping_id = (int) $mapping_item['mapping_id'];
256 256
 			// Clone the current mapping item.
257
-			$cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] );
257
+			$cloned_mapping_id = $dbo->insert_mapping_item($mapping_item['mapping_title']);
258 258
 			// Clone all the rule groups.
259
-			$rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id );
259
+			$rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping($mapping_id);
260 260
 			// Clone all the properties.
261
-			$properties_to_be_cloned = $dbo->get_properties( $mapping_id );
262
-			foreach ( $properties_to_be_cloned as $property ) {
261
+			$properties_to_be_cloned = $dbo->get_properties($mapping_id);
262
+			foreach ($properties_to_be_cloned as $property) {
263 263
 				// Assign a new mapping id.
264 264
 				$property['mapping_id'] = $cloned_mapping_id;
265 265
 				// Removing this property id, since a new id needed to be created for
266 266
 				// new property.
267
-				unset( $property['property_id'] );
268
-				$dbo->insert_or_update_property( $property );
267
+				unset($property['property_id']);
268
+				$dbo->insert_or_update_property($property);
269 269
 			}
270 270
 			// Loop through the rule groups and insert them in table with the mapping id.
271
-			foreach ( $rule_groups_to_be_cloned as $rule_group ) {
272
-				$cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id );
271
+			foreach ($rule_groups_to_be_cloned as $rule_group) {
272
+				$cloned_rule_group_id = $dbo->insert_rule_group($cloned_mapping_id);
273 273
 				$original_rules       = (array) $rule_group['rules'];
274 274
 				// Now we need to insert these rules for the cloned rule group id.
275
-				foreach ( $original_rules as $clone_rule ) {
275
+				foreach ($original_rules as $clone_rule) {
276 276
 					// We should replace only rule group id in the cloned rules.
277 277
 					$clone_rule['rule_group_id'] = (int) $cloned_rule_group_id;
278
-					unset( $clone_rule['rule_id'] );
279
-					$dbo->insert_or_update_rule_item( $clone_rule );
278
+					unset($clone_rule['rule_id']);
279
+					$dbo->insert_or_update_rule_item($clone_rule);
280 280
 				}
281 281
 			}
282 282
 		}
283 283
 
284 284
 		return array(
285 285
 			'status'  => 'success',
286
-			'message' => __( 'Successfully cloned mapping items', 'wordlift' ),
286
+			'message' => __('Successfully cloned mapping items', 'wordlift'),
287 287
 		);
288 288
 	}
289 289
 
@@ -294,23 +294,23 @@  discard block
 block discarded – undo
294 294
 	 *
295 295
 	 * @return array
296 296
 	 */
297
-	public static function update_mapping_items( $request ) {
297
+	public static function update_mapping_items($request) {
298 298
 		$dbo       = new Mappings_DBO();
299 299
 		$post_data = $request->get_params();
300
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
300
+		if (array_key_exists('mapping_items', $post_data)) {
301 301
 			$mapping_items = (array) $post_data['mapping_items'];
302
-			foreach ( $mapping_items as $mapping_item ) {
303
-				$dbo->insert_or_update_mapping_item( $mapping_item );
302
+			foreach ($mapping_items as $mapping_item) {
303
+				$dbo->insert_or_update_mapping_item($mapping_item);
304 304
 			}
305 305
 
306 306
 			return array(
307 307
 				'status'  => 'success',
308
-				'message' => __( 'Mapping items successfully updated', 'wordlift' ),
308
+				'message' => __('Mapping items successfully updated', 'wordlift'),
309 309
 			);
310 310
 		} else {
311 311
 			return array(
312 312
 				'status'  => 'failure',
313
-				'message' => __( 'Unable to update mapping item', 'wordlift' ),
313
+				'message' => __('Unable to update mapping item', 'wordlift'),
314 314
 			);
315 315
 		}
316 316
 	}
@@ -322,23 +322,23 @@  discard block
 block discarded – undo
322 322
 	 *
323 323
 	 * @return array
324 324
 	 */
325
-	public static function delete_mapping_items( $request ) {
325
+	public static function delete_mapping_items($request) {
326 326
 		$dbo       = new Mappings_DBO();
327 327
 		$post_data = $request->get_params();
328
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
328
+		if (array_key_exists('mapping_items', $post_data)) {
329 329
 			$mapping_items = (array) $post_data['mapping_items'];
330
-			foreach ( $mapping_items as $mapping_item ) {
331
-				$dbo->delete_mapping_item( $mapping_item['mapping_id'] );
330
+			foreach ($mapping_items as $mapping_item) {
331
+				$dbo->delete_mapping_item($mapping_item['mapping_id']);
332 332
 			}
333 333
 
334 334
 			return array(
335 335
 				'status'  => 'success',
336
-				'message' => __( 'successfully deleted mapping items', 'wordlift' )
336
+				'message' => __('successfully deleted mapping items', 'wordlift')
337 337
 			);
338 338
 		} else {
339 339
 			return array(
340 340
 				'status'  => 'failure',
341
-				'message' => __( 'Unable to delete mapping items', 'wordlift' )
341
+				'message' => __('Unable to delete mapping items', 'wordlift')
342 342
 			);
343 343
 		}
344 344
 	}
@@ -350,7 +350,7 @@  discard block
 block discarded – undo
350 350
 	 *
351 351
 	 * @return array
352 352
 	 */
353
-	public static function list_mapping_items( $request ) {
353
+	public static function list_mapping_items($request) {
354 354
 		$dbo = new Mappings_DBO();
355 355
 
356 356
 		return $dbo->get_mappings();
@@ -364,11 +364,11 @@  discard block
 block discarded – undo
364 364
 	 *
365 365
 	 * @return array A list of rule ids.
366 366
 	 */
367
-	private static function get_rule_ids( $dbo, $rule_group_id ) {
368
-		$rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id );
367
+	private static function get_rule_ids($dbo, $rule_group_id) {
368
+		$rule_rows_in_db = $dbo->get_rules_by_rule_group($rule_group_id);
369 369
 		$rule_ids        = array();
370
-		foreach ( $rule_rows_in_db as $rule_row ) {
371
-			array_push( $rule_ids, (int) $rule_row['rule_id'] );
370
+		foreach ($rule_rows_in_db as $rule_row) {
371
+			array_push($rule_ids, (int) $rule_row['rule_id']);
372 372
 		}
373 373
 
374 374
 		return $rule_ids;
@@ -383,27 +383,27 @@  discard block
 block discarded – undo
383 383
 	 *
384 384
 	 * @return void
385 385
 	 */
386
-	private static function save_rules( $dbo, $rule_group_id, $rule_list ) {
387
-		$rule_ids = self::get_rule_ids( $dbo, $rule_group_id );
388
-		foreach ( $rule_list as $rule ) {
386
+	private static function save_rules($dbo, $rule_group_id, $rule_list) {
387
+		$rule_ids = self::get_rule_ids($dbo, $rule_group_id);
388
+		foreach ($rule_list as $rule) {
389 389
 			// Some rules may not have rule group id, because they are inserted
390 390
 			// in ui, so lets add them any way.
391 391
 			$rule['rule_group_id'] = $rule_group_id;
392
-			$dbo->insert_or_update_rule_item( $rule );
393
-			if ( array_key_exists( 'rule_id', $rule ) ) {
392
+			$dbo->insert_or_update_rule_item($rule);
393
+			if (array_key_exists('rule_id', $rule)) {
394 394
 				$index_to_be_removed = array_search(
395 395
 					(int) $rule['rule_id'],
396 396
 					$rule_ids,
397 397
 					true
398 398
 				);
399
-				if ( false !== $index_to_be_removed ) {
400
-					unset( $rule_ids[ $index_to_be_removed ] );
399
+				if (false !== $index_to_be_removed) {
400
+					unset($rule_ids[$index_to_be_removed]);
401 401
 				}
402 402
 			}
403 403
 		}
404
-		foreach ( $rule_ids as $rule_id ) {
404
+		foreach ($rule_ids as $rule_id) {
405 405
 			// Delete all the rule ids which are not posted.
406
-			$dbo->delete_rule_item( $rule_id );
406
+			$dbo->delete_rule_item($rule_id);
407 407
 		}
408 408
 	}
409 409
 
@@ -416,14 +416,14 @@  discard block
 block discarded – undo
416 416
 	 *
417 417
 	 * @return void
418 418
 	 */
419
-	private static function save_property_list( $dbo, $mapping_id, $property_list ) {
420
-		$properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id );
419
+	private static function save_property_list($dbo, $mapping_id, $property_list) {
420
+		$properties_needed_to_be_deleted = $dbo->get_properties($mapping_id);
421 421
 		$property_ids                    = array();
422
-		foreach ( $properties_needed_to_be_deleted as $property ) {
423
-			array_push( $property_ids, (int) $property['property_id'] );
422
+		foreach ($properties_needed_to_be_deleted as $property) {
423
+			array_push($property_ids, (int) $property['property_id']);
424 424
 		}
425
-		foreach ( $property_list as $property ) {
426
-			if ( array_key_exists( 'property_id', $property ) ) {
425
+		foreach ($property_list as $property) {
426
+			if (array_key_exists('property_id', $property)) {
427 427
 				// Remove the id from the list of property ids needed to be deleted
428 428
 				// because it is posted.
429 429
 				$index_to_be_removed = array_search(
@@ -431,18 +431,18 @@  discard block
 block discarded – undo
431 431
 					$property_ids,
432 432
 					true
433 433
 				);
434
-				if ( false !== $index_to_be_removed ) {
435
-					unset( $property_ids[ $index_to_be_removed ] );
434
+				if (false !== $index_to_be_removed) {
435
+					unset($property_ids[$index_to_be_removed]);
436 436
 				}
437 437
 			}
438 438
 			// Add mapping id to property data.
439 439
 			$property['mapping_id'] = $mapping_id;
440
-			$dbo->insert_or_update_property( $property );
440
+			$dbo->insert_or_update_property($property);
441 441
 
442 442
 		}
443 443
 		// At the end remove all the property ids which are not posted.
444
-		foreach ( $property_ids as $property_id ) {
445
-			$dbo->delete_property( $property_id );
444
+		foreach ($property_ids as $property_id) {
445
+			$dbo->delete_property($property_id);
446 446
 		}
447 447
 
448 448
 	}
@@ -455,11 +455,11 @@  discard block
 block discarded – undo
455 455
 	 *
456 456
 	 * @return array $rule_group_ids A list of rule group ids.
457 457
 	 */
458
-	private static function get_rule_group_ids( $dbo, $mapping_id ) {
459
-		$rule_group_rows = $dbo->get_rule_group_list( $mapping_id );
458
+	private static function get_rule_group_ids($dbo, $mapping_id) {
459
+		$rule_group_rows = $dbo->get_rule_group_list($mapping_id);
460 460
 		$rule_group_ids  = array();
461
-		foreach ( $rule_group_rows as $rule_group_row ) {
462
-			array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] );
461
+		foreach ($rule_group_rows as $rule_group_row) {
462
+			array_push($rule_group_ids, (int) $rule_group_row['rule_group_id']);
463 463
 		}
464 464
 
465 465
 		return $rule_group_ids;
@@ -474,31 +474,31 @@  discard block
 block discarded – undo
474 474
 	 *
475 475
 	 * @return void
476 476
 	 */
477
-	private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) {
477
+	private static function save_rule_group_list($dbo, $mapping_id, $rule_group_list) {
478 478
 		// The rule groups not posted should be deleted.
479
-		$rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id );
479
+		$rule_group_ids = self::get_rule_group_ids($dbo, $mapping_id);
480 480
 		// Loop through rule group list and save the rule group.
481
-		foreach ( $rule_group_list as $rule_group ) {
482
-			if ( array_key_exists( 'rule_group_id', $rule_group ) ) {
481
+		foreach ($rule_group_list as $rule_group) {
482
+			if (array_key_exists('rule_group_id', $rule_group)) {
483 483
 				$rule_group_id = $rule_group['rule_group_id'];
484 484
 			} else {
485 485
 				// New rule group, should create new rule group id.
486
-				$rule_group_id = $dbo->insert_rule_group( $mapping_id );
486
+				$rule_group_id = $dbo->insert_rule_group($mapping_id);
487 487
 			}
488 488
 			$index_to_be_removed = array_search(
489 489
 				(int) $rule_group_id,
490 490
 				$rule_group_ids,
491 491
 				true
492 492
 			);
493
-			if ( false !== $index_to_be_removed ) {
494
-				unset( $rule_group_ids[ $index_to_be_removed ] );
493
+			if (false !== $index_to_be_removed) {
494
+				unset($rule_group_ids[$index_to_be_removed]);
495 495
 			}
496
-			self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] );
496
+			self::save_rules($dbo, $rule_group_id, $rule_group['rules']);
497 497
 		}
498 498
 
499 499
 		// Remove all the rule groups which are not posted.
500
-		foreach ( $rule_group_ids as $rule_group_id ) {
501
-			$dbo->delete_rule_group_item( $rule_group_id );
500
+		foreach ($rule_group_ids as $rule_group_id) {
501
+			$dbo->delete_rule_group_item($rule_group_id);
502 502
 		}
503 503
 	}
504 504
 
@@ -509,33 +509,33 @@  discard block
 block discarded – undo
509 509
 	 *
510 510
 	 * @return array
511 511
 	 */
512
-	public static function insert_or_update_mapping_item( $request ) {
512
+	public static function insert_or_update_mapping_item($request) {
513 513
 		$post_data = $request->get_params() === null ? array() : $request->get_params();
514 514
 		$dbo       = new Mappings_DBO();
515 515
 		// check if valid object is posted.
516
-		if ( array_key_exists( 'mapping_title', $post_data ) &&
517
-		     array_key_exists( 'rule_group_list', $post_data ) &&
518
-		     array_key_exists( 'property_list', $post_data ) ) {
516
+		if (array_key_exists('mapping_title', $post_data) &&
517
+		     array_key_exists('rule_group_list', $post_data) &&
518
+		     array_key_exists('property_list', $post_data)) {
519 519
 			// Do validation, remove all incomplete data.
520 520
 			$mapping_item = array();
521
-			if ( array_key_exists( 'mapping_id', $post_data ) ) {
521
+			if (array_key_exists('mapping_id', $post_data)) {
522 522
 				$mapping_item['mapping_id'] = $post_data['mapping_id'];
523 523
 			}
524 524
 			$mapping_item['mapping_title'] = $post_data['mapping_title'];
525 525
 			// lets save the mapping item.
526
-			$mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item );
527
-			self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] );
528
-			self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] );
526
+			$mapping_id = $dbo->insert_or_update_mapping_item($mapping_item);
527
+			self::save_rule_group_list($dbo, $mapping_id, $post_data['rule_group_list']);
528
+			self::save_property_list($dbo, $mapping_id, $post_data['property_list']);
529 529
 
530 530
 			return array(
531 531
 				'status'     => 'success',
532
-				'message'    => __( 'Successfully saved mapping item', 'wordlift' ),
532
+				'message'    => __('Successfully saved mapping item', 'wordlift'),
533 533
 				'mapping_id' => (int) $mapping_id,
534 534
 			);
535 535
 		} else {
536 536
 			return array(
537 537
 				'status'  => 'error',
538
-				'message' => __( 'Unable to save mapping item', 'wordlift' ),
538
+				'message' => __('Unable to save mapping item', 'wordlift'),
539 539
 			);
540 540
 		}
541 541
 	}
Please login to merge, or discard this patch.
src/shortcodes/class-wordlift-products-navigator-shortcode-rest.php 2 patches
Indentation   +232 added lines, -232 removed lines patch added patch discarded remove patch
@@ -4,208 +4,208 @@  discard block
 block discarded – undo
4 4
 
5 5
 class Wordlift_Products_Navigator_Shortcode_REST extends Wordlift_Shortcode_REST {
6 6
 
7
-	const CACHE_TTL = 3600; // 1 hour
8
-
9
-	public function __construct() {
10
-		parent::__construct(
11
-			'/products-navigator',
12
-			array(
13
-				'post_id' => array(
14
-					'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ),
15
-					'type'        => 'integer',
16
-					'required'    => true
17
-				),
18
-				'uniqid'  => array(
19
-					'description' => __( 'Navigator uniqueid', 'wordlift' ),
20
-					'type'        => 'string',
21
-					'required'    => true
22
-				),
23
-				'limit'   => array(
24
-					'default'           => 4,
25
-					'type'              => 'integer',
26
-					'sanitize_callback' => 'absint'
27
-				),
28
-				'offset'  => array(
29
-					'default'           => 0,
30
-					'type'              => 'integer',
31
-					'sanitize_callback' => 'absint'
32
-				),
33
-				'sort'    => array(
34
-					'default'           => 'ID DESC',
35
-					'sanitize_callback' => 'sanitize_sql_orderby'
36
-				),
37
-				'amp'     => array(
38
-					'sanitize_callback' => 'rest_sanitize_boolean'
39
-				)
40
-			)
41
-		);
42
-	}
43
-
44
-	public function get_data( $request ) {
45
-
46
-		// Sanitize and set defaults
47
-		$navigator_length = $request['limit'];
48
-		$navigator_offset = $request['offset'];
49
-		$order_by         = $request['sort'];
50
-		$post_id          = $request['post_id'];
51
-		$navigator_id     = $request['uniqid'];
52
-		$amp              = $request['amp'];
53
-
54
-		$post = get_post( $post_id );
55
-
56
-		// Post ID has to match an existing item
57
-		if ( null === $post ) {
58
-			return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) );
59
-		}
60
-
61
-		// Determine navigator type and call respective get_*_results
62
-		if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
63
-			$referencing_posts = $this->get_entity_results( $post_id, array(
64
-				'ID',
65
-				'post_title',
66
-			), $order_by, $navigator_length, $navigator_offset );
67
-		} else {
68
-			$referencing_posts = $this->get_post_results( $post_id, array(
69
-				'ID',
70
-				'post_title',
71
-			), $order_by, $navigator_length, $navigator_offset );
72
-		}
73
-
74
-		// Fetch directly referencing posts excluding referencing posts via entities
75
-		$directly_referencing_posts = $this->get_directly_referencing_posts( $post_id, array_map( function ( $referencing_post ) {
76
-			return $referencing_post->ID;
77
-		}, $referencing_posts ) );
78
-
79
-		// Combine directly referencing posts and referencing posts via entities
80
-		$referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts );
81
-
82
-		// loop over them and take the first one which is not already in the $related_posts
83
-		$results = array();
84
-		foreach ( $referencing_posts as $referencing_post ) {
85
-			$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
86
-			$product           = wc_get_product( $referencing_post->ID );
87
-
88
-			$result = array(
89
-				'product' => array(
90
-					'id'              => $referencing_post->ID,
91
-					'permalink'       => get_permalink( $referencing_post->ID ),
92
-					'title'           => $referencing_post->post_title,
93
-					'thumbnail'       => get_the_post_thumbnail_url( $referencing_post, 'medium' ),
94
-					'regular_price'   => $product->get_regular_price(),
95
-					'sale_price'      => $product->get_sale_price(),
96
-					'price'           => $product->get_price(),
97
-					'currency_symbol' => get_woocommerce_currency_symbol(),
98
-					'discount_pc'     => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0,
99
-					'average_rating'  => $product->get_average_rating(),
100
-					'rating_count'    => $product->get_rating_count(),
101
-					'rating_html'     => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() )
102
-				),
103
-				'entity'  => array(
104
-					'id'        => $referencing_post->entity_id,
105
-					'label'     => $serialized_entity['label'],
106
-					'mainType'  => $serialized_entity['mainType'],
107
-					'permalink' => get_permalink( $referencing_post->entity_id ),
108
-				),
109
-			);
110
-
111
-			$results[] = $result;
112
-
113
-		}
114
-
115
-		if ( count( $results ) < $navigator_length ) {
116
-			$results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
117
-		}
118
-
119
-
120
-		// Add filler posts if needed
121
-		$filler_count = $navigator_length - count( $results );
122
-		if ( $filler_count > 0 ) {
123
-			$referencing_post_ids = array_map( function ( $p ) {
124
-				return $p->ID;
125
-			}, $referencing_posts );
126
-			/**
127
-			 * @since 3.28.0
128
-			 * Filler posts are fetched using this util.
129
-			 */
130
-			$filler_posts_util    = new Filler_Posts_Util( $post_id, 'product' );
131
-			$post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids );
132
-			$filler_posts            = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded );
133
-			$results                 = array_merge( $results, $filler_posts );
134
-		}
135
-
136
-		// Apply filters after fillers are added
137
-		foreach ( $results as $result_index => $result ) {
138
-			$results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id );
139
-			$results[ $result_index ]['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
140
-		}
141
-
142
-		$results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id );
143
-
144
-		return $amp ? array(
145
-			'items' => array(
146
-				array( 'values' => $results ),
147
-			),
148
-		) : $results;
149
-
150
-	}
151
-
152
-	private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) {
153
-
154
-		$directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id );
155
-
156
-		$post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids );
157
-
158
-		$directly_referencing_posts = get_posts( array(
159
-			'meta_query'          => array(
160
-				array(
161
-					'key' => '_thumbnail_id'
162
-				),
163
-				array(
164
-					'key'   => '_stock_status',
165
-					'value' => 'instock'
166
-				)
167
-			),
168
-			'post__in'            => $post__in,
169
-			'post_type'           => 'product',
170
-			'ignore_sticky_posts' => 1
171
-		) );
172
-
173
-		$results = array();
174
-
175
-		foreach ( $directly_referencing_posts as $post ) {
176
-			$result             = new stdClass();
177
-			$result->ID         = $post->ID;
178
-			$result->post_title = $post->post_title;
179
-			$result->entity_id  = $post->ID;
180
-			$results[]          = $result;
181
-		}
182
-
183
-		return $results;
184
-	}
185
-
186
-	private function get_entity_results(
187
-		$post_id,
188
-		$fields = array(
189
-			'ID',
190
-			'post_title',
191
-		),
192
-		$order_by = 'ID DESC',
193
-		$limit = 10,
194
-		$offset = 0
195
-	) {
196
-		global $wpdb;
197
-
198
-		$select = implode( ', ', array_map( function ( $item ) {
199
-			return "p.$item AS $item";
200
-		}, (array) $fields ) );
201
-
202
-		$order_by = implode( ', ', array_map( function ( $item ) {
203
-			return "p.$item";
204
-		}, (array) $order_by ) );
205
-
206
-		/** @noinspection SqlNoDataSourceInspection */
207
-		return $wpdb->get_results(
208
-			$wpdb->prepare( <<<EOF
7
+    const CACHE_TTL = 3600; // 1 hour
8
+
9
+    public function __construct() {
10
+        parent::__construct(
11
+            '/products-navigator',
12
+            array(
13
+                'post_id' => array(
14
+                    'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ),
15
+                    'type'        => 'integer',
16
+                    'required'    => true
17
+                ),
18
+                'uniqid'  => array(
19
+                    'description' => __( 'Navigator uniqueid', 'wordlift' ),
20
+                    'type'        => 'string',
21
+                    'required'    => true
22
+                ),
23
+                'limit'   => array(
24
+                    'default'           => 4,
25
+                    'type'              => 'integer',
26
+                    'sanitize_callback' => 'absint'
27
+                ),
28
+                'offset'  => array(
29
+                    'default'           => 0,
30
+                    'type'              => 'integer',
31
+                    'sanitize_callback' => 'absint'
32
+                ),
33
+                'sort'    => array(
34
+                    'default'           => 'ID DESC',
35
+                    'sanitize_callback' => 'sanitize_sql_orderby'
36
+                ),
37
+                'amp'     => array(
38
+                    'sanitize_callback' => 'rest_sanitize_boolean'
39
+                )
40
+            )
41
+        );
42
+    }
43
+
44
+    public function get_data( $request ) {
45
+
46
+        // Sanitize and set defaults
47
+        $navigator_length = $request['limit'];
48
+        $navigator_offset = $request['offset'];
49
+        $order_by         = $request['sort'];
50
+        $post_id          = $request['post_id'];
51
+        $navigator_id     = $request['uniqid'];
52
+        $amp              = $request['amp'];
53
+
54
+        $post = get_post( $post_id );
55
+
56
+        // Post ID has to match an existing item
57
+        if ( null === $post ) {
58
+            return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) );
59
+        }
60
+
61
+        // Determine navigator type and call respective get_*_results
62
+        if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
63
+            $referencing_posts = $this->get_entity_results( $post_id, array(
64
+                'ID',
65
+                'post_title',
66
+            ), $order_by, $navigator_length, $navigator_offset );
67
+        } else {
68
+            $referencing_posts = $this->get_post_results( $post_id, array(
69
+                'ID',
70
+                'post_title',
71
+            ), $order_by, $navigator_length, $navigator_offset );
72
+        }
73
+
74
+        // Fetch directly referencing posts excluding referencing posts via entities
75
+        $directly_referencing_posts = $this->get_directly_referencing_posts( $post_id, array_map( function ( $referencing_post ) {
76
+            return $referencing_post->ID;
77
+        }, $referencing_posts ) );
78
+
79
+        // Combine directly referencing posts and referencing posts via entities
80
+        $referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts );
81
+
82
+        // loop over them and take the first one which is not already in the $related_posts
83
+        $results = array();
84
+        foreach ( $referencing_posts as $referencing_post ) {
85
+            $serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
86
+            $product           = wc_get_product( $referencing_post->ID );
87
+
88
+            $result = array(
89
+                'product' => array(
90
+                    'id'              => $referencing_post->ID,
91
+                    'permalink'       => get_permalink( $referencing_post->ID ),
92
+                    'title'           => $referencing_post->post_title,
93
+                    'thumbnail'       => get_the_post_thumbnail_url( $referencing_post, 'medium' ),
94
+                    'regular_price'   => $product->get_regular_price(),
95
+                    'sale_price'      => $product->get_sale_price(),
96
+                    'price'           => $product->get_price(),
97
+                    'currency_symbol' => get_woocommerce_currency_symbol(),
98
+                    'discount_pc'     => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0,
99
+                    'average_rating'  => $product->get_average_rating(),
100
+                    'rating_count'    => $product->get_rating_count(),
101
+                    'rating_html'     => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() )
102
+                ),
103
+                'entity'  => array(
104
+                    'id'        => $referencing_post->entity_id,
105
+                    'label'     => $serialized_entity['label'],
106
+                    'mainType'  => $serialized_entity['mainType'],
107
+                    'permalink' => get_permalink( $referencing_post->entity_id ),
108
+                ),
109
+            );
110
+
111
+            $results[] = $result;
112
+
113
+        }
114
+
115
+        if ( count( $results ) < $navigator_length ) {
116
+            $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
117
+        }
118
+
119
+
120
+        // Add filler posts if needed
121
+        $filler_count = $navigator_length - count( $results );
122
+        if ( $filler_count > 0 ) {
123
+            $referencing_post_ids = array_map( function ( $p ) {
124
+                return $p->ID;
125
+            }, $referencing_posts );
126
+            /**
127
+             * @since 3.28.0
128
+             * Filler posts are fetched using this util.
129
+             */
130
+            $filler_posts_util    = new Filler_Posts_Util( $post_id, 'product' );
131
+            $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids );
132
+            $filler_posts            = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded );
133
+            $results                 = array_merge( $results, $filler_posts );
134
+        }
135
+
136
+        // Apply filters after fillers are added
137
+        foreach ( $results as $result_index => $result ) {
138
+            $results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id );
139
+            $results[ $result_index ]['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
140
+        }
141
+
142
+        $results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id );
143
+
144
+        return $amp ? array(
145
+            'items' => array(
146
+                array( 'values' => $results ),
147
+            ),
148
+        ) : $results;
149
+
150
+    }
151
+
152
+    private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) {
153
+
154
+        $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id );
155
+
156
+        $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids );
157
+
158
+        $directly_referencing_posts = get_posts( array(
159
+            'meta_query'          => array(
160
+                array(
161
+                    'key' => '_thumbnail_id'
162
+                ),
163
+                array(
164
+                    'key'   => '_stock_status',
165
+                    'value' => 'instock'
166
+                )
167
+            ),
168
+            'post__in'            => $post__in,
169
+            'post_type'           => 'product',
170
+            'ignore_sticky_posts' => 1
171
+        ) );
172
+
173
+        $results = array();
174
+
175
+        foreach ( $directly_referencing_posts as $post ) {
176
+            $result             = new stdClass();
177
+            $result->ID         = $post->ID;
178
+            $result->post_title = $post->post_title;
179
+            $result->entity_id  = $post->ID;
180
+            $results[]          = $result;
181
+        }
182
+
183
+        return $results;
184
+    }
185
+
186
+    private function get_entity_results(
187
+        $post_id,
188
+        $fields = array(
189
+            'ID',
190
+            'post_title',
191
+        ),
192
+        $order_by = 'ID DESC',
193
+        $limit = 10,
194
+        $offset = 0
195
+    ) {
196
+        global $wpdb;
197
+
198
+        $select = implode( ', ', array_map( function ( $item ) {
199
+            return "p.$item AS $item";
200
+        }, (array) $fields ) );
201
+
202
+        $order_by = implode( ', ', array_map( function ( $item ) {
203
+            return "p.$item";
204
+        }, (array) $order_by ) );
205
+
206
+        /** @noinspection SqlNoDataSourceInspection */
207
+        return $wpdb->get_results(
208
+            $wpdb->prepare( <<<EOF
209 209
 SELECT %4\$s, p2.ID as entity_id
210 210
  FROM {$wpdb->prefix}wl_relation_instances r1
211 211
 	-- get the ID of the post entity in common between the object and the subject 2. 
@@ -238,33 +238,33 @@  discard block
 block discarded – undo
238 238
  LIMIT %2\$d
239 239
  OFFSET %3\$d
240 240
 EOF
241
-				, $post_id, $limit, $offset, $select, $order_by )
242
-		);
243
-	}
244
-
245
-	private function get_post_results(
246
-		$post_id,
247
-		$fields = array(
248
-			'ID',
249
-			'post_title',
250
-		),
251
-		$order_by = 'ID DESC',
252
-		$limit = 10,
253
-		$offset = 0
254
-	) {
255
-		global $wpdb;
256
-
257
-		$select = implode( ', ', array_map( function ( $item ) {
258
-			return "p.$item AS $item";
259
-		}, (array) $fields ) );
260
-
261
-		$order_by = implode( ', ', array_map( function ( $item ) {
262
-			return "p.$item";
263
-		}, (array) $order_by ) );
264
-
265
-		/** @noinspection SqlNoDataSourceInspection */
266
-		return $wpdb->get_results(
267
-			$wpdb->prepare( <<<EOF
241
+                , $post_id, $limit, $offset, $select, $order_by )
242
+        );
243
+    }
244
+
245
+    private function get_post_results(
246
+        $post_id,
247
+        $fields = array(
248
+            'ID',
249
+            'post_title',
250
+        ),
251
+        $order_by = 'ID DESC',
252
+        $limit = 10,
253
+        $offset = 0
254
+    ) {
255
+        global $wpdb;
256
+
257
+        $select = implode( ', ', array_map( function ( $item ) {
258
+            return "p.$item AS $item";
259
+        }, (array) $fields ) );
260
+
261
+        $order_by = implode( ', ', array_map( function ( $item ) {
262
+            return "p.$item";
263
+        }, (array) $order_by ) );
264
+
265
+        /** @noinspection SqlNoDataSourceInspection */
266
+        return $wpdb->get_results(
267
+            $wpdb->prepare( <<<EOF
268 268
 SELECT %4\$s, p2.ID as entity_id
269 269
  FROM {$wpdb->prefix}wl_relation_instances r1
270 270
     INNER JOIN {$wpdb->prefix}wl_relation_instances r2
@@ -300,8 +300,8 @@  discard block
 block discarded – undo
300 300
  LIMIT %2\$d
301 301
  OFFSET %3\$d
302 302
 EOF
303
-				, $post_id, $limit, $offset, $select, $order_by )
304
-		);
305
-	}
303
+                , $post_id, $limit, $offset, $select, $order_by )
304
+        );
305
+    }
306 306
 
307 307
 }
Please login to merge, or discard this patch.
Spacing   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -11,12 +11,12 @@  discard block
 block discarded – undo
11 11
 			'/products-navigator',
12 12
 			array(
13 13
 				'post_id' => array(
14
-					'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ),
14
+					'description' => __('Post ID for which Navigator has to be queried', 'wordlift'),
15 15
 					'type'        => 'integer',
16 16
 					'required'    => true
17 17
 				),
18 18
 				'uniqid'  => array(
19
-					'description' => __( 'Navigator uniqueid', 'wordlift' ),
19
+					'description' => __('Navigator uniqueid', 'wordlift'),
20 20
 					'type'        => 'string',
21 21
 					'required'    => true
22 22
 				),
@@ -41,7 +41,7 @@  discard block
 block discarded – undo
41 41
 		);
42 42
 	}
43 43
 
44
-	public function get_data( $request ) {
44
+	public function get_data($request) {
45 45
 
46 46
 		// Sanitize and set defaults
47 47
 		$navigator_length = $request['limit'];
@@ -51,60 +51,60 @@  discard block
 block discarded – undo
51 51
 		$navigator_id     = $request['uniqid'];
52 52
 		$amp              = $request['amp'];
53 53
 
54
-		$post = get_post( $post_id );
54
+		$post = get_post($post_id);
55 55
 
56 56
 		// Post ID has to match an existing item
57
-		if ( null === $post ) {
58
-			return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) );
57
+		if (null === $post) {
58
+			return new WP_Error('rest_invalid_post_id', __('Invalid post_id', 'wordlift'), array('status' => 404));
59 59
 		}
60 60
 
61 61
 		// Determine navigator type and call respective get_*_results
62
-		if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
63
-			$referencing_posts = $this->get_entity_results( $post_id, array(
62
+		if (get_post_type($post_id) === Wordlift_Entity_Service::TYPE_NAME) {
63
+			$referencing_posts = $this->get_entity_results($post_id, array(
64 64
 				'ID',
65 65
 				'post_title',
66
-			), $order_by, $navigator_length, $navigator_offset );
66
+			), $order_by, $navigator_length, $navigator_offset);
67 67
 		} else {
68
-			$referencing_posts = $this->get_post_results( $post_id, array(
68
+			$referencing_posts = $this->get_post_results($post_id, array(
69 69
 				'ID',
70 70
 				'post_title',
71
-			), $order_by, $navigator_length, $navigator_offset );
71
+			), $order_by, $navigator_length, $navigator_offset);
72 72
 		}
73 73
 
74 74
 		// Fetch directly referencing posts excluding referencing posts via entities
75
-		$directly_referencing_posts = $this->get_directly_referencing_posts( $post_id, array_map( function ( $referencing_post ) {
75
+		$directly_referencing_posts = $this->get_directly_referencing_posts($post_id, array_map(function($referencing_post) {
76 76
 			return $referencing_post->ID;
77
-		}, $referencing_posts ) );
77
+		}, $referencing_posts));
78 78
 
79 79
 		// Combine directly referencing posts and referencing posts via entities
80
-		$referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts );
80
+		$referencing_posts = array_merge($directly_referencing_posts, $referencing_posts);
81 81
 
82 82
 		// loop over them and take the first one which is not already in the $related_posts
83 83
 		$results = array();
84
-		foreach ( $referencing_posts as $referencing_post ) {
85
-			$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
86
-			$product           = wc_get_product( $referencing_post->ID );
84
+		foreach ($referencing_posts as $referencing_post) {
85
+			$serialized_entity = wl_serialize_entity($referencing_post->entity_id);
86
+			$product           = wc_get_product($referencing_post->ID);
87 87
 
88 88
 			$result = array(
89 89
 				'product' => array(
90 90
 					'id'              => $referencing_post->ID,
91
-					'permalink'       => get_permalink( $referencing_post->ID ),
91
+					'permalink'       => get_permalink($referencing_post->ID),
92 92
 					'title'           => $referencing_post->post_title,
93
-					'thumbnail'       => get_the_post_thumbnail_url( $referencing_post, 'medium' ),
93
+					'thumbnail'       => get_the_post_thumbnail_url($referencing_post, 'medium'),
94 94
 					'regular_price'   => $product->get_regular_price(),
95 95
 					'sale_price'      => $product->get_sale_price(),
96 96
 					'price'           => $product->get_price(),
97 97
 					'currency_symbol' => get_woocommerce_currency_symbol(),
98
-					'discount_pc'     => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0,
98
+					'discount_pc'     => ($product->get_sale_price() && ($product->get_regular_price() > 0)) ? round(1 - ($product->get_sale_price() / $product->get_regular_price()), 2) * 100 : 0,
99 99
 					'average_rating'  => $product->get_average_rating(),
100 100
 					'rating_count'    => $product->get_rating_count(),
101
-					'rating_html'     => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() )
101
+					'rating_html'     => wc_get_rating_html($product->get_average_rating(), $product->get_rating_count())
102 102
 				),
103 103
 				'entity'  => array(
104 104
 					'id'        => $referencing_post->entity_id,
105 105
 					'label'     => $serialized_entity['label'],
106 106
 					'mainType'  => $serialized_entity['mainType'],
107
-					'permalink' => get_permalink( $referencing_post->entity_id ),
107
+					'permalink' => get_permalink($referencing_post->entity_id),
108 108
 				),
109 109
 			);
110 110
 
@@ -112,50 +112,50 @@  discard block
 block discarded – undo
112 112
 
113 113
 		}
114 114
 
115
-		if ( count( $results ) < $navigator_length ) {
116
-			$results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
115
+		if (count($results) < $navigator_length) {
116
+			$results = apply_filters('wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length);
117 117
 		}
118 118
 
119 119
 
120 120
 		// Add filler posts if needed
121
-		$filler_count = $navigator_length - count( $results );
122
-		if ( $filler_count > 0 ) {
123
-			$referencing_post_ids = array_map( function ( $p ) {
121
+		$filler_count = $navigator_length - count($results);
122
+		if ($filler_count > 0) {
123
+			$referencing_post_ids = array_map(function($p) {
124 124
 				return $p->ID;
125
-			}, $referencing_posts );
125
+			}, $referencing_posts);
126 126
 			/**
127 127
 			 * @since 3.28.0
128 128
 			 * Filler posts are fetched using this util.
129 129
 			 */
130
-			$filler_posts_util    = new Filler_Posts_Util( $post_id, 'product' );
131
-			$post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids );
132
-			$filler_posts            = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded );
133
-			$results                 = array_merge( $results, $filler_posts );
130
+			$filler_posts_util = new Filler_Posts_Util($post_id, 'product');
131
+			$post_ids_to_be_excluded = array_merge(array($post_id), $referencing_post_ids);
132
+			$filler_posts            = $filler_posts_util->get_product_navigator_response($filler_count, $post_ids_to_be_excluded);
133
+			$results                 = array_merge($results, $filler_posts);
134 134
 		}
135 135
 
136 136
 		// Apply filters after fillers are added
137
-		foreach ( $results as $result_index => $result ) {
138
-			$results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id );
139
-			$results[ $result_index ]['entity'] = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
137
+		foreach ($results as $result_index => $result) {
138
+			$results[$result_index]['product'] = apply_filters('wl_products_navigator_data_post', $result['product'], intval($result['product']['id']), $navigator_id);
139
+			$results[$result_index]['entity'] = apply_filters('wl_products_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id);
140 140
 		}
141 141
 
142
-		$results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id );
142
+		$results = apply_filters('wl_products_navigator_results', $results, $navigator_id);
143 143
 
144 144
 		return $amp ? array(
145 145
 			'items' => array(
146
-				array( 'values' => $results ),
146
+				array('values' => $results),
147 147
 			),
148 148
 		) : $results;
149 149
 
150 150
 	}
151 151
 
152
-	private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) {
152
+	private function get_directly_referencing_posts($post_id, $referencing_post_ids) {
153 153
 
154
-		$directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id );
154
+		$directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities($post_id);
155 155
 
156
-		$post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids );
156
+		$post__in = array_diff($directly_referencing_post_ids, $referencing_post_ids);
157 157
 
158
-		$directly_referencing_posts = get_posts( array(
158
+		$directly_referencing_posts = get_posts(array(
159 159
 			'meta_query'          => array(
160 160
 				array(
161 161
 					'key' => '_thumbnail_id'
@@ -168,11 +168,11 @@  discard block
 block discarded – undo
168 168
 			'post__in'            => $post__in,
169 169
 			'post_type'           => 'product',
170 170
 			'ignore_sticky_posts' => 1
171
-		) );
171
+		));
172 172
 
173 173
 		$results = array();
174 174
 
175
-		foreach ( $directly_referencing_posts as $post ) {
175
+		foreach ($directly_referencing_posts as $post) {
176 176
 			$result             = new stdClass();
177 177
 			$result->ID         = $post->ID;
178 178
 			$result->post_title = $post->post_title;
@@ -195,17 +195,17 @@  discard block
 block discarded – undo
195 195
 	) {
196 196
 		global $wpdb;
197 197
 
198
-		$select = implode( ', ', array_map( function ( $item ) {
198
+		$select = implode(', ', array_map(function($item) {
199 199
 			return "p.$item AS $item";
200
-		}, (array) $fields ) );
200
+		}, (array) $fields));
201 201
 
202
-		$order_by = implode( ', ', array_map( function ( $item ) {
202
+		$order_by = implode(', ', array_map(function($item) {
203 203
 			return "p.$item";
204
-		}, (array) $order_by ) );
204
+		}, (array) $order_by));
205 205
 
206 206
 		/** @noinspection SqlNoDataSourceInspection */
207 207
 		return $wpdb->get_results(
208
-			$wpdb->prepare( <<<EOF
208
+			$wpdb->prepare(<<<EOF
209 209
 SELECT %4\$s, p2.ID as entity_id
210 210
  FROM {$wpdb->prefix}wl_relation_instances r1
211 211
 	-- get the ID of the post entity in common between the object and the subject 2. 
@@ -238,7 +238,7 @@  discard block
 block discarded – undo
238 238
  LIMIT %2\$d
239 239
  OFFSET %3\$d
240 240
 EOF
241
-				, $post_id, $limit, $offset, $select, $order_by )
241
+				, $post_id, $limit, $offset, $select, $order_by)
242 242
 		);
243 243
 	}
244 244
 
@@ -254,17 +254,17 @@  discard block
 block discarded – undo
254 254
 	) {
255 255
 		global $wpdb;
256 256
 
257
-		$select = implode( ', ', array_map( function ( $item ) {
257
+		$select = implode(', ', array_map(function($item) {
258 258
 			return "p.$item AS $item";
259
-		}, (array) $fields ) );
259
+		}, (array) $fields));
260 260
 
261
-		$order_by = implode( ', ', array_map( function ( $item ) {
261
+		$order_by = implode(', ', array_map(function($item) {
262 262
 			return "p.$item";
263
-		}, (array) $order_by ) );
263
+		}, (array) $order_by));
264 264
 
265 265
 		/** @noinspection SqlNoDataSourceInspection */
266 266
 		return $wpdb->get_results(
267
-			$wpdb->prepare( <<<EOF
267
+			$wpdb->prepare(<<<EOF
268 268
 SELECT %4\$s, p2.ID as entity_id
269 269
  FROM {$wpdb->prefix}wl_relation_instances r1
270 270
     INNER JOIN {$wpdb->prefix}wl_relation_instances r2
@@ -300,7 +300,7 @@  discard block
 block discarded – undo
300 300
  LIMIT %2\$d
301 301
  OFFSET %3\$d
302 302
 EOF
303
-				, $post_id, $limit, $offset, $select, $order_by )
303
+				, $post_id, $limit, $offset, $select, $order_by)
304 304
 		);
305 305
 	}
306 306
 
Please login to merge, or discard this patch.
widgets/navigator/filler-posts/class-same-category-filler-posts.php 2 patches
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -14,12 +14,12 @@
 block discarded – undo
14 14
  */
15 15
 class Same_Category_Filler_Posts extends Filler_Posts {
16 16
 
17
-	function get_posts( $filler_count, $post_ids_to_be_excluded ) {
17
+    function get_posts( $filler_count, $post_ids_to_be_excluded ) {
18 18
 
19
-		$current_post_categories = wp_get_post_categories( $this->post_id );
20
-		$post_type = $this->alternate_post_type ?: get_post_types();
19
+        $current_post_categories = wp_get_post_categories( $this->post_id );
20
+        $post_type = $this->alternate_post_type ?: get_post_types();
21 21
 
22
-		return get_posts( array( 'category__in' => $current_post_categories, 'post_type' => $post_type )
23
-		                  + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) );
24
-	}
22
+        return get_posts( array( 'category__in' => $current_post_categories, 'post_type' => $post_type )
23
+                          + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) );
24
+    }
25 25
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -14,12 +14,12 @@
 block discarded – undo
14 14
  */
15 15
 class Same_Category_Filler_Posts extends Filler_Posts {
16 16
 
17
-	function get_posts( $filler_count, $post_ids_to_be_excluded ) {
17
+	function get_posts($filler_count, $post_ids_to_be_excluded) {
18 18
 
19
-		$current_post_categories = wp_get_post_categories( $this->post_id );
19
+		$current_post_categories = wp_get_post_categories($this->post_id);
20 20
 		$post_type = $this->alternate_post_type ?: get_post_types();
21 21
 
22
-		return get_posts( array( 'category__in' => $current_post_categories, 'post_type' => $post_type )
23
-		                  + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) );
22
+		return get_posts(array('category__in' => $current_post_categories, 'post_type' => $post_type)
23
+		                  + $this->get_posts_config($filler_count, $post_ids_to_be_excluded));
24 24
 	}
25 25
 }
Please login to merge, or discard this patch.
widgets/navigator/filler-posts/class-same-post-type-same-category-posts.php 2 patches
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -8,14 +8,14 @@
 block discarded – undo
8 8
 class Same_Post_Type_Same_Category_Posts extends Filler_Posts {
9 9
 
10 10
 
11
-	function get_posts( $filler_count, $post_ids_to_be_excluded ) {
12
-		$current_post_categories = wp_get_post_categories( $this->post_id );
13
-		$post_type = $this->alternate_post_type ?: get_post_type( $this->post_id );
11
+    function get_posts( $filler_count, $post_ids_to_be_excluded ) {
12
+        $current_post_categories = wp_get_post_categories( $this->post_id );
13
+        $post_type = $this->alternate_post_type ?: get_post_type( $this->post_id );
14 14
 
15
-		return get_posts( array(
16
-			                  'category__in' => $current_post_categories,
17
-			                  'post_type'    => $post_type
18
-		                  )
19
-		                  + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) );
20
-	}
15
+        return get_posts( array(
16
+                                'category__in' => $current_post_categories,
17
+                                'post_type'    => $post_type
18
+                            )
19
+                          + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) );
20
+    }
21 21
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -8,14 +8,14 @@
 block discarded – undo
8 8
 class Same_Post_Type_Same_Category_Posts extends Filler_Posts {
9 9
 
10 10
 
11
-	function get_posts( $filler_count, $post_ids_to_be_excluded ) {
12
-		$current_post_categories = wp_get_post_categories( $this->post_id );
13
-		$post_type = $this->alternate_post_type ?: get_post_type( $this->post_id );
11
+	function get_posts($filler_count, $post_ids_to_be_excluded) {
12
+		$current_post_categories = wp_get_post_categories($this->post_id);
13
+		$post_type = $this->alternate_post_type ?: get_post_type($this->post_id);
14 14
 
15
-		return get_posts( array(
15
+		return get_posts(array(
16 16
 			                  'category__in' => $current_post_categories,
17 17
 			                  'post_type'    => $post_type
18 18
 		                  )
19
-		                  + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) );
19
+		                  + $this->get_posts_config($filler_count, $post_ids_to_be_excluded));
20 20
 	}
21 21
 }
Please login to merge, or discard this patch.
widgets/navigator/filler-posts/class-same-post-type-filler-posts.php 2 patches
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -7,15 +7,15 @@
 block discarded – undo
7 7
  */
8 8
 class Same_Post_Type_Filler_Posts extends Filler_Posts {
9 9
 
10
-	function get_posts( $filler_count, $post_ids_to_be_excluded ) {
10
+    function get_posts( $filler_count, $post_ids_to_be_excluded ) {
11 11
 
12
-		$post_type = $this->alternate_post_type ?: get_post_type( $this->post_id );
12
+        $post_type = $this->alternate_post_type ?: get_post_type( $this->post_id );
13 13
 
14
-		if ( $post_type === 'entity' ) {
15
-			$post_type = 'post';
16
-		}
14
+        if ( $post_type === 'entity' ) {
15
+            $post_type = 'post';
16
+        }
17 17
 
18
-		return get_posts( array( 'post_type' => $post_type )
19
-		                  + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) );
20
-	}
18
+        return get_posts( array( 'post_type' => $post_type )
19
+                          + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) );
20
+    }
21 21
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -7,15 +7,15 @@
 block discarded – undo
7 7
  */
8 8
 class Same_Post_Type_Filler_Posts extends Filler_Posts {
9 9
 
10
-	function get_posts( $filler_count, $post_ids_to_be_excluded ) {
10
+	function get_posts($filler_count, $post_ids_to_be_excluded) {
11 11
 
12
-		$post_type = $this->alternate_post_type ?: get_post_type( $this->post_id );
12
+		$post_type = $this->alternate_post_type ?: get_post_type($this->post_id);
13 13
 
14
-		if ( $post_type === 'entity' ) {
14
+		if ($post_type === 'entity') {
15 15
 			$post_type = 'post';
16 16
 		}
17 17
 
18
-		return get_posts( array( 'post_type' => $post_type )
19
-		                  + $this->get_posts_config( $filler_count, $post_ids_to_be_excluded ) );
18
+		return get_posts(array('post_type' => $post_type)
19
+		                  + $this->get_posts_config($filler_count, $post_ids_to_be_excluded));
20 20
 	}
21 21
 }
Please login to merge, or discard this patch.
src/wordlift/widgets/navigator/filler-posts/class-filler-posts.php 2 patches
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -7,55 +7,55 @@
 block discarded – undo
7 7
  */
8 8
 abstract class Filler_Posts {
9 9
 
10
-	/**
11
-	 * @var int
12
-	 */
13
-	public $filler_count;
14
-
15
-	/**
16
-	 * @var array<int>
17
-	 */
18
-	public $post_ids_to_be_excluded;
19
-
20
-	/**
21
-	 * @var $post_id int
22
-	 */
23
-	protected $post_id;
24
-
25
-	/**
26
-	 * Filler_Posts constructor.
27
-	 *
28
-	 * @param $post_id
29
-	 */
30
-	public function __construct( $post_id, $alternate_post_type = null ) {
31
-
32
-		$this->post_id = $post_id;
33
-		$this->alternate_post_type = $alternate_post_type;
34
-
35
-	}
36
-
37
-	protected function get_posts_config($filler_count, $post_ids_to_be_excluded) {
38
-
39
-		return array(
40
-			'meta_query'          => array(
41
-				array(
42
-					'key' => '_thumbnail_id'
43
-				)
44
-			),
45
-			'numberposts'         => $filler_count,
46
-			'post__not_in'        => $post_ids_to_be_excluded,
47
-			'ignore_sticky_posts' => 1
48
-		);
49
-
50
-	}
51
-
52
-	/**
53
-	 * @param $filler_count
54
-	 * @param $post_ids_to_be_excluded
55
-	 *
56
-	 * @return array<\WP_Post>
57
-	 */
58
-	abstract function get_posts( $filler_count, $post_ids_to_be_excluded );
10
+    /**
11
+     * @var int
12
+     */
13
+    public $filler_count;
14
+
15
+    /**
16
+     * @var array<int>
17
+     */
18
+    public $post_ids_to_be_excluded;
19
+
20
+    /**
21
+     * @var $post_id int
22
+     */
23
+    protected $post_id;
24
+
25
+    /**
26
+     * Filler_Posts constructor.
27
+     *
28
+     * @param $post_id
29
+     */
30
+    public function __construct( $post_id, $alternate_post_type = null ) {
31
+
32
+        $this->post_id = $post_id;
33
+        $this->alternate_post_type = $alternate_post_type;
34
+
35
+    }
36
+
37
+    protected function get_posts_config($filler_count, $post_ids_to_be_excluded) {
38
+
39
+        return array(
40
+            'meta_query'          => array(
41
+                array(
42
+                    'key' => '_thumbnail_id'
43
+                )
44
+            ),
45
+            'numberposts'         => $filler_count,
46
+            'post__not_in'        => $post_ids_to_be_excluded,
47
+            'ignore_sticky_posts' => 1
48
+        );
49
+
50
+    }
51
+
52
+    /**
53
+     * @param $filler_count
54
+     * @param $post_ids_to_be_excluded
55
+     *
56
+     * @return array<\WP_Post>
57
+     */
58
+    abstract function get_posts( $filler_count, $post_ids_to_be_excluded );
59 59
 
60 60
 
61 61
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
27 27
 	 *
28 28
 	 * @param $post_id
29 29
 	 */
30
-	public function __construct( $post_id, $alternate_post_type = null ) {
30
+	public function __construct($post_id, $alternate_post_type = null) {
31 31
 
32 32
 		$this->post_id = $post_id;
33 33
 		$this->alternate_post_type = $alternate_post_type;
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 	 *
56 56
 	 * @return array<\WP_Post>
57 57
 	 */
58
-	abstract function get_posts( $filler_count, $post_ids_to_be_excluded );
58
+	abstract function get_posts($filler_count, $post_ids_to_be_excluded);
59 59
 
60 60
 
61 61
 }
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-object-adapter-factory.php 2 patches
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -6,34 +6,34 @@
 block discarded – undo
6 6
 
7 7
 class Sync_Object_Adapter_Factory {
8 8
 
9
-	/**
10
-	 * @param int $type One of Object_Type_Enum::POST, Object_Type_Enum::USER, ...
11
-	 * @param int $object_id The object id.
12
-	 *
13
-	 * @return Sync_Object_Adapter
14
-	 * @throws \Exception
15
-	 */
16
-	function create( $type, $object_id ) {
17
-
18
-		switch ( $type ) {
19
-			case Object_Type_Enum::POST:
20
-				return new Sync_Post_Adapter( $object_id );
21
-			case Object_Type_Enum::USER:
22
-				return new Sync_User_Adapter( $object_id );
23
-			case Object_Type_Enum::TERM:
24
-				return new Sync_Term_Adapter( $object_id );
25
-			default:
26
-				throw new \Exception( "Unsupported type $type." );
27
-		}
28
-
29
-	}
30
-
31
-	function create_many( $type, $object_ids ) {
32
-		$that = $this;
33
-
34
-		return array_map( function ( $item ) use ( $type, $that ) {
35
-			return $that->create( $type, $item );
36
-		}, (array) $object_ids );
37
-	}
9
+    /**
10
+     * @param int $type One of Object_Type_Enum::POST, Object_Type_Enum::USER, ...
11
+     * @param int $object_id The object id.
12
+     *
13
+     * @return Sync_Object_Adapter
14
+     * @throws \Exception
15
+     */
16
+    function create( $type, $object_id ) {
17
+
18
+        switch ( $type ) {
19
+            case Object_Type_Enum::POST:
20
+                return new Sync_Post_Adapter( $object_id );
21
+            case Object_Type_Enum::USER:
22
+                return new Sync_User_Adapter( $object_id );
23
+            case Object_Type_Enum::TERM:
24
+                return new Sync_Term_Adapter( $object_id );
25
+            default:
26
+                throw new \Exception( "Unsupported type $type." );
27
+        }
28
+
29
+    }
30
+
31
+    function create_many( $type, $object_ids ) {
32
+        $that = $this;
33
+
34
+        return array_map( function ( $item ) use ( $type, $that ) {
35
+            return $that->create( $type, $item );
36
+        }, (array) $object_ids );
37
+    }
38 38
 
39 39
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -13,27 +13,27 @@
 block discarded – undo
13 13
 	 * @return Sync_Object_Adapter
14 14
 	 * @throws \Exception
15 15
 	 */
16
-	function create( $type, $object_id ) {
16
+	function create($type, $object_id) {
17 17
 
18
-		switch ( $type ) {
18
+		switch ($type) {
19 19
 			case Object_Type_Enum::POST:
20
-				return new Sync_Post_Adapter( $object_id );
20
+				return new Sync_Post_Adapter($object_id);
21 21
 			case Object_Type_Enum::USER:
22
-				return new Sync_User_Adapter( $object_id );
22
+				return new Sync_User_Adapter($object_id);
23 23
 			case Object_Type_Enum::TERM:
24
-				return new Sync_Term_Adapter( $object_id );
24
+				return new Sync_Term_Adapter($object_id);
25 25
 			default:
26
-				throw new \Exception( "Unsupported type $type." );
26
+				throw new \Exception("Unsupported type $type.");
27 27
 		}
28 28
 
29 29
 	}
30 30
 
31
-	function create_many( $type, $object_ids ) {
31
+	function create_many($type, $object_ids) {
32 32
 		$that = $this;
33 33
 
34
-		return array_map( function ( $item ) use ( $type, $that ) {
35
-			return $that->create( $type, $item );
36
-		}, (array) $object_ids );
34
+		return array_map(function($item) use ($type, $that) {
35
+			return $that->create($type, $item);
36
+		}, (array) $object_ids);
37 37
 	}
38 38
 
39 39
 }
Please login to merge, or discard this patch.