Completed
Pull Request — develop (#1645)
by
unknown
01:16
created
src/wordlift/jsonld/class-jsonld-article-wrapper.php 2 patches
Indentation   +128 added lines, -128 removed lines patch added patch discarded remove patch
@@ -6,142 +6,142 @@
 block discarded – undo
6 6
 
7 7
 class Jsonld_Article_Wrapper {
8 8
 
9
-	public static $article_types = array(
10
-		'Article',
11
-		'AdvertiserContentArticle',
12
-		'NewsArticle',
13
-		'AnalysisNewsArticle',
14
-		'AskPublicNewsArticle',
15
-		'BackgroundNewsArticle',
16
-		'OpinionNewsArticle',
17
-		'ReportageNewsArticle',
18
-		'ReviewNewsArticle',
19
-		'Report',
20
-		'SatiricalArticle',
21
-		'ScholarlyArticle',
22
-		'MedicalScholarlyArticle',
23
-		'SocialMediaPosting',
24
-		'BlogPosting',
25
-		'LiveBlogPosting',
26
-		'DiscussionForumPosting',
27
-		'TechArticle',
28
-		'APIReference',
29
-	);
30
-
31
-	/**
32
-	 * @var Wordlift_Post_To_Jsonld_Converter
33
-	 */
34
-	private $post_to_jsonld_converter;
35
-	/**
36
-	 * @var \Wordlift_Cached_Post_Converter
37
-	 */
38
-	private $cached_postid_to_jsonld_converter;
39
-	/**
40
-	 * @var \Wordlift_Entity_Uri_Service
41
-	 */
42
-	private $entity_uri_service;
43
-
44
-	/**
45
-	 * Jsonld_Article_Wrapper constructor.
46
-	 *
47
-	 * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter
48
-	 * @param $cached_postid_to_jsonld_converter
49
-	 */
50
-	public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) {
51
-
52
-		$this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled();
53
-
54
-		add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), PHP_INT_MAX - 100, 3 );
55
-
56
-		$this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter;
57
-
58
-		$this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance();
59
-	}
60
-
61
-	public function after_get_jsonld( $jsonld, $post_id, $context ) {
62
-
63
-		if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] )
64
-			 || ! is_array( $jsonld[0] ) ) {
65
-			return $jsonld;
66
-		}
67
-
68
-		// Copy the 1st array element
69
-		$post_jsonld = $jsonld[0];
70
-
71
-		// Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be
72
-		// in the schema.org context, i.e. `Article`, not `http://schema.org/Article`.
73
-		if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) {
74
-			return $jsonld;
75
-		}
76
-
77
-		// Convert the post as Article.
78
-		$article_jsonld = $this->post_to_jsonld_converter->convert( $post_id );
79
-
80
-		$article_jsonld['@id'] = $post_jsonld['@id'] . '#article';
81
-		// Reset the type, since by default the type assigned via the Entity Type taxonomy is used.
82
-		$article_jsonld['@type'] = 'Article';
83
-		$article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] );
84
-
85
-		// Copy over the URLs.
86
-		if ( isset( $post_jsonld['url'] ) ) {
87
-			$article_jsonld['url'] = $post_jsonld['url'];
88
-		}
89
-
90
-		array_unshift( $jsonld, $article_jsonld );
91
-
92
-		$author_jsonld = $this->get_author_linked_entity( $article_jsonld );
93
-
94
-		/**
95
-		 * The author entities can be present in graph for some entity types
96
-		 * for Person and Organization, so check before we add it to graph.
97
-		 * reference : https://schema.org/author
98
-		 */
99
-		if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) {
100
-			$jsonld[] = $author_jsonld;
101
-		}
102
-
103
-		return $jsonld;
104
-	}
105
-
106
-	private function is_article( $schema_types ) {
107
-
108
-		$array_intersect = array_intersect( self::$article_types, (array) $schema_types );
109
-
110
-		return ! empty( $array_intersect );
111
-	}
112
-
113
-	private function get_author_linked_entity( $article_jsonld ) {
114
-		if ( ! array_key_exists( 'author', $article_jsonld ) ) {
115
-			return false;
116
-		}
117
-
118
-		$author = $article_jsonld['author'];
9
+    public static $article_types = array(
10
+        'Article',
11
+        'AdvertiserContentArticle',
12
+        'NewsArticle',
13
+        'AnalysisNewsArticle',
14
+        'AskPublicNewsArticle',
15
+        'BackgroundNewsArticle',
16
+        'OpinionNewsArticle',
17
+        'ReportageNewsArticle',
18
+        'ReviewNewsArticle',
19
+        'Report',
20
+        'SatiricalArticle',
21
+        'ScholarlyArticle',
22
+        'MedicalScholarlyArticle',
23
+        'SocialMediaPosting',
24
+        'BlogPosting',
25
+        'LiveBlogPosting',
26
+        'DiscussionForumPosting',
27
+        'TechArticle',
28
+        'APIReference',
29
+    );
30
+
31
+    /**
32
+     * @var Wordlift_Post_To_Jsonld_Converter
33
+     */
34
+    private $post_to_jsonld_converter;
35
+    /**
36
+     * @var \Wordlift_Cached_Post_Converter
37
+     */
38
+    private $cached_postid_to_jsonld_converter;
39
+    /**
40
+     * @var \Wordlift_Entity_Uri_Service
41
+     */
42
+    private $entity_uri_service;
43
+
44
+    /**
45
+     * Jsonld_Article_Wrapper constructor.
46
+     *
47
+     * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter
48
+     * @param $cached_postid_to_jsonld_converter
49
+     */
50
+    public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) {
51
+
52
+        $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled();
53
+
54
+        add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), PHP_INT_MAX - 100, 3 );
55
+
56
+        $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter;
57
+
58
+        $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance();
59
+    }
60
+
61
+    public function after_get_jsonld( $jsonld, $post_id, $context ) {
62
+
63
+        if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] )
64
+             || ! is_array( $jsonld[0] ) ) {
65
+            return $jsonld;
66
+        }
67
+
68
+        // Copy the 1st array element
69
+        $post_jsonld = $jsonld[0];
70
+
71
+        // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be
72
+        // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`.
73
+        if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) {
74
+            return $jsonld;
75
+        }
76
+
77
+        // Convert the post as Article.
78
+        $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id );
79
+
80
+        $article_jsonld['@id'] = $post_jsonld['@id'] . '#article';
81
+        // Reset the type, since by default the type assigned via the Entity Type taxonomy is used.
82
+        $article_jsonld['@type'] = 'Article';
83
+        $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] );
84
+
85
+        // Copy over the URLs.
86
+        if ( isset( $post_jsonld['url'] ) ) {
87
+            $article_jsonld['url'] = $post_jsonld['url'];
88
+        }
89
+
90
+        array_unshift( $jsonld, $article_jsonld );
91
+
92
+        $author_jsonld = $this->get_author_linked_entity( $article_jsonld );
93
+
94
+        /**
95
+         * The author entities can be present in graph for some entity types
96
+         * for Person and Organization, so check before we add it to graph.
97
+         * reference : https://schema.org/author
98
+         */
99
+        if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) {
100
+            $jsonld[] = $author_jsonld;
101
+        }
102
+
103
+        return $jsonld;
104
+    }
105
+
106
+    private function is_article( $schema_types ) {
107
+
108
+        $array_intersect = array_intersect( self::$article_types, (array) $schema_types );
109
+
110
+        return ! empty( $array_intersect );
111
+    }
112
+
113
+    private function get_author_linked_entity( $article_jsonld ) {
114
+        if ( ! array_key_exists( 'author', $article_jsonld ) ) {
115
+            return false;
116
+        }
117
+
118
+        $author = $article_jsonld['author'];
119 119
 
120
-		if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) {
121
-			return false;
122
-		}
120
+        if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) {
121
+            return false;
122
+        }
123 123
 
124
-		$author_linked_entity_id = $author['@id'];
124
+        $author_linked_entity_id = $author['@id'];
125 125
 
126
-		$author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id );
126
+        $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id );
127 127
 
128
-		if ( ! $author_entity_post instanceof \WP_Post ) {
129
-			return false;
130
-		}
128
+        if ( ! $author_entity_post instanceof \WP_Post ) {
129
+            return false;
130
+        }
131 131
 
132
-		return $this->cached_postid_to_jsonld_converter->convert( $author_entity_post->ID );
132
+        return $this->cached_postid_to_jsonld_converter->convert( $author_entity_post->ID );
133 133
 
134
-	}
134
+    }
135 135
 
136
-	private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) {
136
+    private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) {
137 137
 
138
-		foreach ( $jsonld as $item ) {
139
-			if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) {
140
-				return true;
141
-			}
142
-		}
138
+        foreach ( $jsonld as $item ) {
139
+            if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) {
140
+                return true;
141
+            }
142
+        }
143 143
 
144
-		return false;
145
-	}
144
+        return false;
145
+    }
146 146
 
147 147
 }
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -47,21 +47,21 @@  discard block
 block discarded – undo
47 47
 	 * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter
48 48
 	 * @param $cached_postid_to_jsonld_converter
49 49
 	 */
50
-	public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) {
50
+	public function __construct($post_to_jsonld_converter, $cached_postid_to_jsonld_converter) {
51 51
 
52 52
 		$this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled();
53 53
 
54
-		add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), PHP_INT_MAX - 100, 3 );
54
+		add_filter('wl_after_get_jsonld', array($this, 'after_get_jsonld'), PHP_INT_MAX - 100, 3);
55 55
 
56 56
 		$this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter;
57 57
 
58 58
 		$this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance();
59 59
 	}
60 60
 
61
-	public function after_get_jsonld( $jsonld, $post_id, $context ) {
61
+	public function after_get_jsonld($jsonld, $post_id, $context) {
62 62
 
63
-		if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] )
64
-			 || ! is_array( $jsonld[0] ) ) {
63
+		if (Jsonld_Context_Enum::PAGE !== $context || ! is_array($jsonld) || ! isset($jsonld[0])
64
+			 || ! is_array($jsonld[0])) {
65 65
 			return $jsonld;
66 66
 		}
67 67
 
@@ -70,73 +70,73 @@  discard block
 block discarded – undo
70 70
 
71 71
 		// Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be
72 72
 		// in the schema.org context, i.e. `Article`, not `http://schema.org/Article`.
73
-		if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) {
73
+		if ( ! isset($post_jsonld['@id']) || ! isset($post_jsonld['@type']) || $this->is_article($post_jsonld['@type'])) {
74 74
 			return $jsonld;
75 75
 		}
76 76
 
77 77
 		// Convert the post as Article.
78
-		$article_jsonld = $this->post_to_jsonld_converter->convert( $post_id );
78
+		$article_jsonld = $this->post_to_jsonld_converter->convert($post_id);
79 79
 
80
-		$article_jsonld['@id'] = $post_jsonld['@id'] . '#article';
80
+		$article_jsonld['@id'] = $post_jsonld['@id'].'#article';
81 81
 		// Reset the type, since by default the type assigned via the Entity Type taxonomy is used.
82 82
 		$article_jsonld['@type'] = 'Article';
83
-		$article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] );
83
+		$article_jsonld['about'] = array('@id' => $post_jsonld['@id']);
84 84
 
85 85
 		// Copy over the URLs.
86
-		if ( isset( $post_jsonld['url'] ) ) {
86
+		if (isset($post_jsonld['url'])) {
87 87
 			$article_jsonld['url'] = $post_jsonld['url'];
88 88
 		}
89 89
 
90
-		array_unshift( $jsonld, $article_jsonld );
90
+		array_unshift($jsonld, $article_jsonld);
91 91
 
92
-		$author_jsonld = $this->get_author_linked_entity( $article_jsonld );
92
+		$author_jsonld = $this->get_author_linked_entity($article_jsonld);
93 93
 
94 94
 		/**
95 95
 		 * The author entities can be present in graph for some entity types
96 96
 		 * for Person and Organization, so check before we add it to graph.
97 97
 		 * reference : https://schema.org/author
98 98
 		 */
99
-		if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) {
99
+		if ($author_jsonld && ! $this->is_author_entity_present_in_graph($jsonld, $article_jsonld['author']['@id'])) {
100 100
 			$jsonld[] = $author_jsonld;
101 101
 		}
102 102
 
103 103
 		return $jsonld;
104 104
 	}
105 105
 
106
-	private function is_article( $schema_types ) {
106
+	private function is_article($schema_types) {
107 107
 
108
-		$array_intersect = array_intersect( self::$article_types, (array) $schema_types );
108
+		$array_intersect = array_intersect(self::$article_types, (array) $schema_types);
109 109
 
110
-		return ! empty( $array_intersect );
110
+		return ! empty($array_intersect);
111 111
 	}
112 112
 
113
-	private function get_author_linked_entity( $article_jsonld ) {
114
-		if ( ! array_key_exists( 'author', $article_jsonld ) ) {
113
+	private function get_author_linked_entity($article_jsonld) {
114
+		if ( ! array_key_exists('author', $article_jsonld)) {
115 115
 			return false;
116 116
 		}
117 117
 
118 118
 		$author = $article_jsonld['author'];
119 119
 
120
-		if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) {
120
+		if (count(array_keys($author)) !== 1 || ! array_key_exists('@id', $author)) {
121 121
 			return false;
122 122
 		}
123 123
 
124 124
 		$author_linked_entity_id = $author['@id'];
125 125
 
126
-		$author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id );
126
+		$author_entity_post = $this->entity_uri_service->get_entity($author_linked_entity_id);
127 127
 
128
-		if ( ! $author_entity_post instanceof \WP_Post ) {
128
+		if ( ! $author_entity_post instanceof \WP_Post) {
129 129
 			return false;
130 130
 		}
131 131
 
132
-		return $this->cached_postid_to_jsonld_converter->convert( $author_entity_post->ID );
132
+		return $this->cached_postid_to_jsonld_converter->convert($author_entity_post->ID);
133 133
 
134 134
 	}
135 135
 
136
-	private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) {
136
+	private function is_author_entity_present_in_graph($jsonld, $author_entity_id) {
137 137
 
138
-		foreach ( $jsonld as $item ) {
139
-			if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) {
138
+		foreach ($jsonld as $item) {
139
+			if ($item && array_key_exists('@id', $item) && $item['@id'] === $author_entity_id) {
140 140
 				return true;
141 141
 			}
142 142
 		}
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-jsonld-by-id-endpoint.php 2 patches
Indentation   +118 added lines, -118 removed lines patch added patch discarded remove patch
@@ -23,123 +23,123 @@
 block discarded – undo
23 23
  */
24 24
 class Jsonld_By_Id_Endpoint {
25 25
 
26
-	/**
27
-	 * The {@link Wordlift_Jsonld_Service} instance.
28
-	 *
29
-	 * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance.
30
-	 */
31
-	private $jsonld_service;
32
-
33
-	/**
34
-	 * @var \Wordlift_Entity_Uri_Service
35
-	 */
36
-	private $entity_uri_service;
37
-
38
-	/**
39
-	 * Jsonld_Endpoint constructor.
40
-	 *
41
-	 * @param \Wordlift_Jsonld_Service     $jsonld_service
42
-	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service
43
-	 */
44
-	public function __construct( $jsonld_service, $entity_uri_service ) {
45
-
46
-		$this->jsonld_service     = $jsonld_service;
47
-		$this->entity_uri_service = $entity_uri_service;
48
-
49
-		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
50
-
51
-	}
52
-
53
-	/**
54
-	 * Get the JSON-LD.
55
-	 *
56
-	 * @param \WP_REST_Request $request The incoming {@link \WP_REST_Request}.
57
-	 *
58
-	 * @return WP_REST_Response The outgoing {@link WP_REST_Response}.
59
-	 * @throws \Exception when an error occurs.
60
-	 */
61
-	public function jsonld_by_id( $request ) {
62
-
63
-		// Get the ids.
64
-		$ids = (array) $request->get_param( 'id' );
65
-
66
-		// Preload the URIs to reduce the number of DB roundtrips.
67
-		$this->entity_uri_service->preload_uris( array_map( 'urldecode', $ids ) );
68
-
69
-		$that = $this;
70
-
71
-		// Get the posts, filtering out those not found.
72
-		$posts = array_filter(
73
-			array_map(
74
-				function ( $item ) use ( $that ) {
75
-					return $that->entity_uri_service->get_entity( urldecode( $item ) );
76
-				},
77
-				$ids
78
-			)
79
-		);
80
-
81
-		// Get the posts' IDs and make the unique.
82
-		$post_ids = array_unique(
83
-			array_map(
84
-				function ( $item ) {
85
-					return $item->ID;
86
-				},
87
-				$posts
88
-			)
89
-		);
90
-
91
-		// Get the JSON-LD.
92
-		$data = array();
93
-		foreach ( $post_ids as $post_id ) {
94
-			$data = array_merge( $data, $that->jsonld_service->get_jsonld( false, $post_id ) );
95
-		}
96
-
97
-		// Add the WebSite fragment if requested.
98
-		if ( $request->get_param( 'website' ) ) {
99
-			$data[] = $this->jsonld_service->get_jsonld( true );
100
-		}
101
-
102
-		return Jsonld_Response_Helper::to_response( $data );
103
-	}
104
-
105
-	public function register_routes() {
106
-
107
-		register_rest_route(
108
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
109
-			'/jsonld',
110
-			array(
111
-				'methods'             => WP_REST_Server::READABLE,
112
-				'callback'            => array( $this, 'jsonld_by_id' ),
113
-				'permission_callback' => '__return_true',
114
-				'args'                => array(
115
-					'id'      => array(
116
-						'description'       => __( 'One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift' ),
117
-						// We expect an array of strings.
118
-						'type'              => 'array',
119
-						'items'             => array(
120
-							'type' => 'string',
121
-						),
122
-						// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
123
-						'validate_callback' => function ( $values, $request, $param ) {
124
-
125
-							if ( ! is_array( $values ) ) {
126
-								return new WP_Error( 'rest_invalid_param', esc_html__( 'The id argument must be an array (try passing `id[]=...`.', 'wordlift' ), array( 'status' => 400 ) );
127
-							}
128
-
129
-							foreach ( $values as $value ) {
130
-								if ( 0 !== strpos( $value, 'http' ) ) {
131
-									return new WP_Error( 'rest_invalid_param', esc_html__( 'Ids must start with http.', 'wordlift' ), array( 'status' => 400 ) );
132
-								}
133
-							}
134
-						},
135
-					),
136
-					'website' => array(
137
-						'description' => __( 'Whether to include the WebSite markup.', 'wordlift' ),
138
-					),
139
-				),
140
-			)
141
-		);
142
-
143
-	}
26
+    /**
27
+     * The {@link Wordlift_Jsonld_Service} instance.
28
+     *
29
+     * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance.
30
+     */
31
+    private $jsonld_service;
32
+
33
+    /**
34
+     * @var \Wordlift_Entity_Uri_Service
35
+     */
36
+    private $entity_uri_service;
37
+
38
+    /**
39
+     * Jsonld_Endpoint constructor.
40
+     *
41
+     * @param \Wordlift_Jsonld_Service     $jsonld_service
42
+     * @param \Wordlift_Entity_Uri_Service $entity_uri_service
43
+     */
44
+    public function __construct( $jsonld_service, $entity_uri_service ) {
45
+
46
+        $this->jsonld_service     = $jsonld_service;
47
+        $this->entity_uri_service = $entity_uri_service;
48
+
49
+        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
50
+
51
+    }
52
+
53
+    /**
54
+     * Get the JSON-LD.
55
+     *
56
+     * @param \WP_REST_Request $request The incoming {@link \WP_REST_Request}.
57
+     *
58
+     * @return WP_REST_Response The outgoing {@link WP_REST_Response}.
59
+     * @throws \Exception when an error occurs.
60
+     */
61
+    public function jsonld_by_id( $request ) {
62
+
63
+        // Get the ids.
64
+        $ids = (array) $request->get_param( 'id' );
65
+
66
+        // Preload the URIs to reduce the number of DB roundtrips.
67
+        $this->entity_uri_service->preload_uris( array_map( 'urldecode', $ids ) );
68
+
69
+        $that = $this;
70
+
71
+        // Get the posts, filtering out those not found.
72
+        $posts = array_filter(
73
+            array_map(
74
+                function ( $item ) use ( $that ) {
75
+                    return $that->entity_uri_service->get_entity( urldecode( $item ) );
76
+                },
77
+                $ids
78
+            )
79
+        );
80
+
81
+        // Get the posts' IDs and make the unique.
82
+        $post_ids = array_unique(
83
+            array_map(
84
+                function ( $item ) {
85
+                    return $item->ID;
86
+                },
87
+                $posts
88
+            )
89
+        );
90
+
91
+        // Get the JSON-LD.
92
+        $data = array();
93
+        foreach ( $post_ids as $post_id ) {
94
+            $data = array_merge( $data, $that->jsonld_service->get_jsonld( false, $post_id ) );
95
+        }
96
+
97
+        // Add the WebSite fragment if requested.
98
+        if ( $request->get_param( 'website' ) ) {
99
+            $data[] = $this->jsonld_service->get_jsonld( true );
100
+        }
101
+
102
+        return Jsonld_Response_Helper::to_response( $data );
103
+    }
104
+
105
+    public function register_routes() {
106
+
107
+        register_rest_route(
108
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
109
+            '/jsonld',
110
+            array(
111
+                'methods'             => WP_REST_Server::READABLE,
112
+                'callback'            => array( $this, 'jsonld_by_id' ),
113
+                'permission_callback' => '__return_true',
114
+                'args'                => array(
115
+                    'id'      => array(
116
+                        'description'       => __( 'One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift' ),
117
+                        // We expect an array of strings.
118
+                        'type'              => 'array',
119
+                        'items'             => array(
120
+                            'type' => 'string',
121
+                        ),
122
+                        // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
123
+                        'validate_callback' => function ( $values, $request, $param ) {
124
+
125
+                            if ( ! is_array( $values ) ) {
126
+                                return new WP_Error( 'rest_invalid_param', esc_html__( 'The id argument must be an array (try passing `id[]=...`.', 'wordlift' ), array( 'status' => 400 ) );
127
+                            }
128
+
129
+                            foreach ( $values as $value ) {
130
+                                if ( 0 !== strpos( $value, 'http' ) ) {
131
+                                    return new WP_Error( 'rest_invalid_param', esc_html__( 'Ids must start with http.', 'wordlift' ), array( 'status' => 400 ) );
132
+                                }
133
+                            }
134
+                        },
135
+                    ),
136
+                    'website' => array(
137
+                        'description' => __( 'Whether to include the WebSite markup.', 'wordlift' ),
138
+                    ),
139
+                ),
140
+            )
141
+        );
142
+
143
+    }
144 144
 
145 145
 }
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -41,12 +41,12 @@  discard block
 block discarded – undo
41 41
 	 * @param \Wordlift_Jsonld_Service     $jsonld_service
42 42
 	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service
43 43
 	 */
44
-	public function __construct( $jsonld_service, $entity_uri_service ) {
44
+	public function __construct($jsonld_service, $entity_uri_service) {
45 45
 
46 46
 		$this->jsonld_service     = $jsonld_service;
47 47
 		$this->entity_uri_service = $entity_uri_service;
48 48
 
49
-		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
49
+		add_action('rest_api_init', array($this, 'register_routes'));
50 50
 
51 51
 	}
52 52
 
@@ -58,21 +58,21 @@  discard block
 block discarded – undo
58 58
 	 * @return WP_REST_Response The outgoing {@link WP_REST_Response}.
59 59
 	 * @throws \Exception when an error occurs.
60 60
 	 */
61
-	public function jsonld_by_id( $request ) {
61
+	public function jsonld_by_id($request) {
62 62
 
63 63
 		// Get the ids.
64
-		$ids = (array) $request->get_param( 'id' );
64
+		$ids = (array) $request->get_param('id');
65 65
 
66 66
 		// Preload the URIs to reduce the number of DB roundtrips.
67
-		$this->entity_uri_service->preload_uris( array_map( 'urldecode', $ids ) );
67
+		$this->entity_uri_service->preload_uris(array_map('urldecode', $ids));
68 68
 
69 69
 		$that = $this;
70 70
 
71 71
 		// Get the posts, filtering out those not found.
72 72
 		$posts = array_filter(
73 73
 			array_map(
74
-				function ( $item ) use ( $that ) {
75
-					return $that->entity_uri_service->get_entity( urldecode( $item ) );
74
+				function($item) use ($that) {
75
+					return $that->entity_uri_service->get_entity(urldecode($item));
76 76
 				},
77 77
 				$ids
78 78
 			)
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
 		// Get the posts' IDs and make the unique.
82 82
 		$post_ids = array_unique(
83 83
 			array_map(
84
-				function ( $item ) {
84
+				function($item) {
85 85
 					return $item->ID;
86 86
 				},
87 87
 				$posts
@@ -90,16 +90,16 @@  discard block
 block discarded – undo
90 90
 
91 91
 		// Get the JSON-LD.
92 92
 		$data = array();
93
-		foreach ( $post_ids as $post_id ) {
94
-			$data = array_merge( $data, $that->jsonld_service->get_jsonld( false, $post_id ) );
93
+		foreach ($post_ids as $post_id) {
94
+			$data = array_merge($data, $that->jsonld_service->get_jsonld(false, $post_id));
95 95
 		}
96 96
 
97 97
 		// Add the WebSite fragment if requested.
98
-		if ( $request->get_param( 'website' ) ) {
99
-			$data[] = $this->jsonld_service->get_jsonld( true );
98
+		if ($request->get_param('website')) {
99
+			$data[] = $this->jsonld_service->get_jsonld(true);
100 100
 		}
101 101
 
102
-		return Jsonld_Response_Helper::to_response( $data );
102
+		return Jsonld_Response_Helper::to_response($data);
103 103
 	}
104 104
 
105 105
 	public function register_routes() {
@@ -109,32 +109,32 @@  discard block
 block discarded – undo
109 109
 			'/jsonld',
110 110
 			array(
111 111
 				'methods'             => WP_REST_Server::READABLE,
112
-				'callback'            => array( $this, 'jsonld_by_id' ),
112
+				'callback'            => array($this, 'jsonld_by_id'),
113 113
 				'permission_callback' => '__return_true',
114 114
 				'args'                => array(
115 115
 					'id'      => array(
116
-						'description'       => __( 'One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift' ),
116
+						'description'       => __('One ore more itemids (e.g. http://data.wordlift.io/wordlift).', 'wordlift'),
117 117
 						// We expect an array of strings.
118 118
 						'type'              => 'array',
119 119
 						'items'             => array(
120 120
 							'type' => 'string',
121 121
 						),
122 122
 						// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
123
-						'validate_callback' => function ( $values, $request, $param ) {
123
+						'validate_callback' => function($values, $request, $param) {
124 124
 
125
-							if ( ! is_array( $values ) ) {
126
-								return new WP_Error( 'rest_invalid_param', esc_html__( 'The id argument must be an array (try passing `id[]=...`.', 'wordlift' ), array( 'status' => 400 ) );
125
+							if ( ! is_array($values)) {
126
+								return new WP_Error('rest_invalid_param', esc_html__('The id argument must be an array (try passing `id[]=...`.', 'wordlift'), array('status' => 400));
127 127
 							}
128 128
 
129
-							foreach ( $values as $value ) {
130
-								if ( 0 !== strpos( $value, 'http' ) ) {
131
-									return new WP_Error( 'rest_invalid_param', esc_html__( 'Ids must start with http.', 'wordlift' ), array( 'status' => 400 ) );
129
+							foreach ($values as $value) {
130
+								if (0 !== strpos($value, 'http')) {
131
+									return new WP_Error('rest_invalid_param', esc_html__('Ids must start with http.', 'wordlift'), array('status' => 400));
132 132
 								}
133 133
 							}
134 134
 						},
135 135
 					),
136 136
 					'website' => array(
137
-						'description' => __( 'Whether to include the WebSite markup.', 'wordlift' ),
137
+						'description' => __('Whether to include the WebSite markup.', 'wordlift'),
138 138
 					),
139 139
 				),
140 140
 			)
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/jsonld/class-reference-processor.php 2 patches
Indentation   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -12,40 +12,40 @@
 block discarded – undo
12 12
 
13 13
 class Reference_Processor extends Singleton {
14 14
 
15
-	public function serialize_references( $references ) {
16
-
17
-		return array_map(
18
-			function ( $reference ) {
19
-				if ( $reference instanceof Post_Reference ) {
20
-					  return 'post_' . $reference->get_id();
21
-				} elseif ( $reference instanceof Term_Reference ) {
22
-					return 'term_' . $reference->get_id();
23
-				}
24
-				// Backward compatibility with other hooks pushing
25
-				// references in to the cache.
26
-				return $reference;
27
-			},
28
-			$references
29
-		);
30
-	}
31
-
32
-	public function deserialize_references( $references ) {
33
-
34
-		return array_map(
35
-			function ( $reference ) {
36
-
37
-				if ( strpos( $reference, 'post_' ) !== false ) {
38
-					  return new Post_Reference( (int) str_replace( 'post_', '', $reference ) );
39
-				} elseif ( strpos( $reference, 'term_' ) !== false ) {
40
-					return new Term_Reference( (int) str_replace( 'term_', '', $reference ) );
41
-				}
42
-				// Backward compatibility with other hooks pushing
43
-				// references in to the cache.
44
-				return new Post_Reference( (int) $reference );
45
-			},
46
-			$references
47
-		);
48
-
49
-	}
15
+    public function serialize_references( $references ) {
16
+
17
+        return array_map(
18
+            function ( $reference ) {
19
+                if ( $reference instanceof Post_Reference ) {
20
+                        return 'post_' . $reference->get_id();
21
+                } elseif ( $reference instanceof Term_Reference ) {
22
+                    return 'term_' . $reference->get_id();
23
+                }
24
+                // Backward compatibility with other hooks pushing
25
+                // references in to the cache.
26
+                return $reference;
27
+            },
28
+            $references
29
+        );
30
+    }
31
+
32
+    public function deserialize_references( $references ) {
33
+
34
+        return array_map(
35
+            function ( $reference ) {
36
+
37
+                if ( strpos( $reference, 'post_' ) !== false ) {
38
+                        return new Post_Reference( (int) str_replace( 'post_', '', $reference ) );
39
+                } elseif ( strpos( $reference, 'term_' ) !== false ) {
40
+                    return new Term_Reference( (int) str_replace( 'term_', '', $reference ) );
41
+                }
42
+                // Backward compatibility with other hooks pushing
43
+                // references in to the cache.
44
+                return new Post_Reference( (int) $reference );
45
+            },
46
+            $references
47
+        );
48
+
49
+    }
50 50
 
51 51
 }
Please login to merge, or discard this patch.
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -12,14 +12,14 @@  discard block
 block discarded – undo
12 12
 
13 13
 class Reference_Processor extends Singleton {
14 14
 
15
-	public function serialize_references( $references ) {
15
+	public function serialize_references($references) {
16 16
 
17 17
 		return array_map(
18
-			function ( $reference ) {
19
-				if ( $reference instanceof Post_Reference ) {
20
-					  return 'post_' . $reference->get_id();
21
-				} elseif ( $reference instanceof Term_Reference ) {
22
-					return 'term_' . $reference->get_id();
18
+			function($reference) {
19
+				if ($reference instanceof Post_Reference) {
20
+					  return 'post_'.$reference->get_id();
21
+				} elseif ($reference instanceof Term_Reference) {
22
+					return 'term_'.$reference->get_id();
23 23
 				}
24 24
 				// Backward compatibility with other hooks pushing
25 25
 				// references in to the cache.
@@ -29,19 +29,19 @@  discard block
 block discarded – undo
29 29
 		);
30 30
 	}
31 31
 
32
-	public function deserialize_references( $references ) {
32
+	public function deserialize_references($references) {
33 33
 
34 34
 		return array_map(
35
-			function ( $reference ) {
35
+			function($reference) {
36 36
 
37
-				if ( strpos( $reference, 'post_' ) !== false ) {
38
-					  return new Post_Reference( (int) str_replace( 'post_', '', $reference ) );
39
-				} elseif ( strpos( $reference, 'term_' ) !== false ) {
40
-					return new Term_Reference( (int) str_replace( 'term_', '', $reference ) );
37
+				if (strpos($reference, 'post_') !== false) {
38
+					  return new Post_Reference((int) str_replace('post_', '', $reference));
39
+				} elseif (strpos($reference, 'term_') !== false) {
40
+					return new Term_Reference((int) str_replace('term_', '', $reference));
41 41
 				}
42 42
 				// Backward compatibility with other hooks pushing
43 43
 				// references in to the cache.
44
-				return new Post_Reference( (int) $reference );
44
+				return new Post_Reference((int) $reference);
45 45
 			},
46 46
 			$references
47 47
 		);
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-abstract-reference.php 2 patches
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -10,35 +10,35 @@
 block discarded – undo
10 10
  */
11 11
 abstract class Abstract_Reference implements Reference {
12 12
 
13
-	/**
14
-	 * @var int
15
-	 */
16
-	private $id;
13
+    /**
14
+     * @var int
15
+     */
16
+    private $id;
17 17
 
18
-	/**
19
-	 * Abstract_Reference constructor.
20
-	 *
21
-	 * @param $id int Identifier for the reference.
22
-	 */
23
-	public function __construct( $id ) {
24
-		$this->id = $id;
25
-	}
18
+    /**
19
+     * Abstract_Reference constructor.
20
+     *
21
+     * @param $id int Identifier for the reference.
22
+     */
23
+    public function __construct( $id ) {
24
+        $this->id = $id;
25
+    }
26 26
 
27
-	abstract public function get_type();
27
+    abstract public function get_type();
28 28
 
29
-	public function get_id() {
30
-		return $this->id;
31
-	}
29
+    public function get_id() {
30
+        return $this->id;
31
+    }
32 32
 
33
-	/**
34
-	 * This function is necessary because array_unique can be applied
35
-	 * on the references, we prepend the id with the type, for example
36
-	 * post reference with post id 1 would be equal to {@link Object_Type_Enum::POST."_1"}
37
-	 *
38
-	 * @return string
39
-	 */
40
-	public function __toString() {
41
-		return $this->get_type() . '_' . $this->get_id();
42
-	}
33
+    /**
34
+     * This function is necessary because array_unique can be applied
35
+     * on the references, we prepend the id with the type, for example
36
+     * post reference with post id 1 would be equal to {@link Object_Type_Enum::POST."_1"}
37
+     *
38
+     * @return string
39
+     */
40
+    public function __toString() {
41
+        return $this->get_type() . '_' . $this->get_id();
42
+    }
43 43
 
44 44
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -20,7 +20,7 @@  discard block
 block discarded – undo
20 20
 	 *
21 21
 	 * @param $id int Identifier for the reference.
22 22
 	 */
23
-	public function __construct( $id ) {
23
+	public function __construct($id) {
24 24
 		$this->id = $id;
25 25
 	}
26 26
 
@@ -38,7 +38,7 @@  discard block
 block discarded – undo
38 38
 	 * @return string
39 39
 	 */
40 40
 	public function __toString() {
41
-		return $this->get_type() . '_' . $this->get_id();
41
+		return $this->get_type().'_'.$this->get_id();
42 42
 	}
43 43
 
44 44
 }
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-jsonld-response-helper.php 2 patches
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -17,33 +17,33 @@
 block discarded – undo
17 17
 
18 18
 class Jsonld_Response_Helper {
19 19
 
20
-	/**
21
-	 * Converts the provided data into a {@link WP_REST_Response} by adding the required caching and content-type
22
-	 * headers.
23
-	 *
24
-	 * @param mixed $data The output data.
25
-	 *
26
-	 * @return WP_REST_Response The {@link WP_REST_Response}.
27
-	 */
28
-	public static function to_response( $data ) {
29
-
30
-		$response = new WP_REST_Response( $data );
31
-
32
-		$cache_in_seconds = 86400;
33
-		$date_timezone    = new DateTimeZone( 'GMT' );
34
-		$date_now         = new DateTime( 'now', $date_timezone );
35
-		$date_interval    = new DateInterval( "PT{$cache_in_seconds}S" );
36
-		$expires          = $date_now->add( $date_interval )->format( 'D, j M Y H:i:s T' );
37
-
38
-		$response->set_headers(
39
-			array(
40
-				'Content-Type'  => 'application/ld+json; charset=' . get_option( 'blog_charset' ),
41
-				'Cache-Control' => "max-age=$cache_in_seconds",
42
-				'Expires'       => $expires,
43
-			)
44
-		);
45
-
46
-		return $response;
47
-	}
20
+    /**
21
+     * Converts the provided data into a {@link WP_REST_Response} by adding the required caching and content-type
22
+     * headers.
23
+     *
24
+     * @param mixed $data The output data.
25
+     *
26
+     * @return WP_REST_Response The {@link WP_REST_Response}.
27
+     */
28
+    public static function to_response( $data ) {
29
+
30
+        $response = new WP_REST_Response( $data );
31
+
32
+        $cache_in_seconds = 86400;
33
+        $date_timezone    = new DateTimeZone( 'GMT' );
34
+        $date_now         = new DateTime( 'now', $date_timezone );
35
+        $date_interval    = new DateInterval( "PT{$cache_in_seconds}S" );
36
+        $expires          = $date_now->add( $date_interval )->format( 'D, j M Y H:i:s T' );
37
+
38
+        $response->set_headers(
39
+            array(
40
+                'Content-Type'  => 'application/ld+json; charset=' . get_option( 'blog_charset' ),
41
+                'Cache-Control' => "max-age=$cache_in_seconds",
42
+                'Expires'       => $expires,
43
+            )
44
+        );
45
+
46
+        return $response;
47
+    }
48 48
 
49 49
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -25,19 +25,19 @@
 block discarded – undo
25 25
 	 *
26 26
 	 * @return WP_REST_Response The {@link WP_REST_Response}.
27 27
 	 */
28
-	public static function to_response( $data ) {
28
+	public static function to_response($data) {
29 29
 
30
-		$response = new WP_REST_Response( $data );
30
+		$response = new WP_REST_Response($data);
31 31
 
32 32
 		$cache_in_seconds = 86400;
33
-		$date_timezone    = new DateTimeZone( 'GMT' );
34
-		$date_now         = new DateTime( 'now', $date_timezone );
35
-		$date_interval    = new DateInterval( "PT{$cache_in_seconds}S" );
36
-		$expires          = $date_now->add( $date_interval )->format( 'D, j M Y H:i:s T' );
33
+		$date_timezone    = new DateTimeZone('GMT');
34
+		$date_now         = new DateTime('now', $date_timezone);
35
+		$date_interval    = new DateInterval("PT{$cache_in_seconds}S");
36
+		$expires          = $date_now->add($date_interval)->format('D, j M Y H:i:s T');
37 37
 
38 38
 		$response->set_headers(
39 39
 			array(
40
-				'Content-Type'  => 'application/ld+json; charset=' . get_option( 'blog_charset' ),
40
+				'Content-Type'  => 'application/ld+json; charset='.get_option('blog_charset'),
41 41
 				'Cache-Control' => "max-age=$cache_in_seconds",
42 42
 				'Expires'       => $expires,
43 43
 			)
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-term-reference.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@
 block discarded – undo
13 13
 
14 14
 class Term_Reference extends Abstract_Reference {
15 15
 
16
-	public function get_type() {
17
-		return Object_Type_Enum::TERM;
18
-	}
16
+    public function get_type() {
17
+        return Object_Type_Enum::TERM;
18
+    }
19 19
 }
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-jsonld-endpoint.php 2 patches
Indentation   +215 added lines, -215 removed lines patch added patch discarded remove patch
@@ -21,215 +21,215 @@  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(
49
-			'rest_api_init',
50
-			function () use ( $that ) {
51
-				register_rest_route(
52
-					WL_REST_ROUTE_DEFAULT_NAMESPACE,
53
-					'/jsonld/(?P<id>\d+)',
54
-					array(
55
-						'methods'             => WP_REST_Server::READABLE,
56
-						'callback'            => array( $that, 'jsonld_using_post_id' ),
57
-						'permission_callback' => '__return_true',
58
-						'args'                => array(
59
-							'id' => array(
60
-								// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
61
-								'validate_callback' => function ( $param, $request, $key ) {
62
-									return is_numeric( $param );
63
-								},
64
-								'sanitize_callback' => 'absint',
65
-							),
66
-						),
67
-					)
68
-				);
69
-
70
-				register_rest_route(
71
-					WL_REST_ROUTE_DEFAULT_NAMESPACE,
72
-					'/jsonld/http/(?P<item_id>.*)',
73
-					array(
74
-						'methods'             => 'GET',
75
-						'callback'            => array( $that, 'jsonld_using_item_id' ),
76
-						'permission_callback' => '__return_true',
77
-					)
78
-				);
79
-
80
-				register_rest_route(
81
-					WL_REST_ROUTE_DEFAULT_NAMESPACE,
82
-					'/jsonld/post-meta/(?P<meta_key>[^/]+)',
83
-					array(
84
-						'methods'             => 'GET',
85
-						'callback'            => array( $that, 'jsonld_using_post_meta' ),
86
-						'permission_callback' => '__return_true',
87
-					)
88
-				);
89
-
90
-				register_rest_route(
91
-					WL_REST_ROUTE_DEFAULT_NAMESPACE,
92
-					'/jsonld/meta/(?P<meta_key>[^/]+)',
93
-					array(
94
-						'methods'             => 'GET',
95
-						'callback'            => array( $that, 'jsonld_using_meta' ),
96
-						'permission_callback' => '__return_true',
97
-					)
98
-				);
99
-
100
-				register_rest_route(
101
-					WL_REST_ROUTE_DEFAULT_NAMESPACE,
102
-					'/jsonld/(?P<post_type>.*)/(?P<post_name>.*)',
103
-					array(
104
-						'methods'             => 'GET',
105
-						'callback'            => array( $that, 'jsonld_using_get_page_by_path' ),
106
-						'permission_callback' => '__return_true',
107
-					)
108
-				);
109
-
110
-			}
111
-		);
112
-
113
-	}
114
-
115
-	/**
116
-	 * Callback for the JSON-LD request.
117
-	 *
118
-	 * @param array $request {
119
-	 *  The request array.
120
-	 *
121
-	 * @type int $id The post id.
122
-	 * }
123
-	 *
124
-	 * @return WP_REST_Response
125
-	 * @throws \Exception when an error occurs.
126
-	 */
127
-	public function jsonld_using_post_id( $request ) {
128
-
129
-		$post_id = $request['id'];
130
-		$type    = ( 0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST );
131
-
132
-		// Send the generated JSON-LD.
133
-		$data = $this->jsonld_service->get( $type, $post_id, Jsonld_Context_Enum::REST );
134
-
135
-		return Jsonld_Response_Helper::to_response( $data );
136
-	}
137
-
138
-	/**
139
-	 * Provide a JSON-LD given the itemId.
140
-	 *
141
-	 * @param array $request {
142
-	 *  The request array.
143
-	 *
144
-	 * @type string $item_id The entity item id.
145
-	 * }
146
-	 *
147
-	 * @return WP_REST_Response
148
-	 * @throws \Exception when an error occurs.
149
-	 */
150
-	public function jsonld_using_item_id( $request ) {
151
-
152
-		$item_id = 'http://' . $request['item_id'];
153
-		$post    = $this->entity_uri_service->get_entity( $item_id );
154
-
155
-		if ( ! is_a( $post, 'WP_Post' ) ) {
156
-			return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
157
-		}
158
-
159
-		return $this->jsonld_using_post_id( array( 'id' => $post->ID ) );
160
-	}
161
-
162
-	public function jsonld_using_get_page_by_path( $request ) {
163
-
164
-		$post_name = $request['post_name'];
165
-		$post_type = $request['post_type'];
166
-
167
-		global $wpdb;
168
-
169
-		$post_id = $wpdb->get_var(
170
-			$wpdb->prepare(
171
-				"
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(
49
+            'rest_api_init',
50
+            function () use ( $that ) {
51
+                register_rest_route(
52
+                    WL_REST_ROUTE_DEFAULT_NAMESPACE,
53
+                    '/jsonld/(?P<id>\d+)',
54
+                    array(
55
+                        'methods'             => WP_REST_Server::READABLE,
56
+                        'callback'            => array( $that, 'jsonld_using_post_id' ),
57
+                        'permission_callback' => '__return_true',
58
+                        'args'                => array(
59
+                            'id' => array(
60
+                                // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
61
+                                'validate_callback' => function ( $param, $request, $key ) {
62
+                                    return is_numeric( $param );
63
+                                },
64
+                                'sanitize_callback' => 'absint',
65
+                            ),
66
+                        ),
67
+                    )
68
+                );
69
+
70
+                register_rest_route(
71
+                    WL_REST_ROUTE_DEFAULT_NAMESPACE,
72
+                    '/jsonld/http/(?P<item_id>.*)',
73
+                    array(
74
+                        'methods'             => 'GET',
75
+                        'callback'            => array( $that, 'jsonld_using_item_id' ),
76
+                        'permission_callback' => '__return_true',
77
+                    )
78
+                );
79
+
80
+                register_rest_route(
81
+                    WL_REST_ROUTE_DEFAULT_NAMESPACE,
82
+                    '/jsonld/post-meta/(?P<meta_key>[^/]+)',
83
+                    array(
84
+                        'methods'             => 'GET',
85
+                        'callback'            => array( $that, 'jsonld_using_post_meta' ),
86
+                        'permission_callback' => '__return_true',
87
+                    )
88
+                );
89
+
90
+                register_rest_route(
91
+                    WL_REST_ROUTE_DEFAULT_NAMESPACE,
92
+                    '/jsonld/meta/(?P<meta_key>[^/]+)',
93
+                    array(
94
+                        'methods'             => 'GET',
95
+                        'callback'            => array( $that, 'jsonld_using_meta' ),
96
+                        'permission_callback' => '__return_true',
97
+                    )
98
+                );
99
+
100
+                register_rest_route(
101
+                    WL_REST_ROUTE_DEFAULT_NAMESPACE,
102
+                    '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)',
103
+                    array(
104
+                        'methods'             => 'GET',
105
+                        'callback'            => array( $that, 'jsonld_using_get_page_by_path' ),
106
+                        'permission_callback' => '__return_true',
107
+                    )
108
+                );
109
+
110
+            }
111
+        );
112
+
113
+    }
114
+
115
+    /**
116
+     * Callback for the JSON-LD request.
117
+     *
118
+     * @param array $request {
119
+     *  The request array.
120
+     *
121
+     * @type int $id The post id.
122
+     * }
123
+     *
124
+     * @return WP_REST_Response
125
+     * @throws \Exception when an error occurs.
126
+     */
127
+    public function jsonld_using_post_id( $request ) {
128
+
129
+        $post_id = $request['id'];
130
+        $type    = ( 0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST );
131
+
132
+        // Send the generated JSON-LD.
133
+        $data = $this->jsonld_service->get( $type, $post_id, Jsonld_Context_Enum::REST );
134
+
135
+        return Jsonld_Response_Helper::to_response( $data );
136
+    }
137
+
138
+    /**
139
+     * Provide a JSON-LD given the itemId.
140
+     *
141
+     * @param array $request {
142
+     *  The request array.
143
+     *
144
+     * @type string $item_id The entity item id.
145
+     * }
146
+     *
147
+     * @return WP_REST_Response
148
+     * @throws \Exception when an error occurs.
149
+     */
150
+    public function jsonld_using_item_id( $request ) {
151
+
152
+        $item_id = 'http://' . $request['item_id'];
153
+        $post    = $this->entity_uri_service->get_entity( $item_id );
154
+
155
+        if ( ! is_a( $post, 'WP_Post' ) ) {
156
+            return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
157
+        }
158
+
159
+        return $this->jsonld_using_post_id( array( 'id' => $post->ID ) );
160
+    }
161
+
162
+    public function jsonld_using_get_page_by_path( $request ) {
163
+
164
+        $post_name = $request['post_name'];
165
+        $post_type = $request['post_type'];
166
+
167
+        global $wpdb;
168
+
169
+        $post_id = $wpdb->get_var(
170
+            $wpdb->prepare(
171
+                "
172 172
 			SELECT ID
173 173
 			FROM $wpdb->posts
174 174
 			WHERE post_name = %s
175 175
 			 AND post_type = %s
176 176
 		",
177
-				$post_name,
178
-				$post_type
179
-			)
180
-		);
181
-
182
-		if ( $post_id === null ) {
183
-			return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
184
-		}
185
-
186
-		return $this->jsonld_using_post_id( array( 'id' => $post_id ) );
187
-	}
188
-
189
-	/**
190
-	 * @param WP_REST_Request $request
191
-	 *
192
-	 * @return WP_REST_Response
193
-	 * @throws \Exception when an error occurs.
194
-	 */
195
-	public function jsonld_using_post_meta( $request ) {
196
-
197
-		$meta_key   = $request['meta_key'];
198
-		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
199
-
200
-		global $wpdb;
201
-
202
-		$post_id = $wpdb->get_var(
203
-			$wpdb->prepare(
204
-				"
177
+                $post_name,
178
+                $post_type
179
+            )
180
+        );
181
+
182
+        if ( $post_id === null ) {
183
+            return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
184
+        }
185
+
186
+        return $this->jsonld_using_post_id( array( 'id' => $post_id ) );
187
+    }
188
+
189
+    /**
190
+     * @param WP_REST_Request $request
191
+     *
192
+     * @return WP_REST_Response
193
+     * @throws \Exception when an error occurs.
194
+     */
195
+    public function jsonld_using_post_meta( $request ) {
196
+
197
+        $meta_key   = $request['meta_key'];
198
+        $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
199
+
200
+        global $wpdb;
201
+
202
+        $post_id = $wpdb->get_var(
203
+            $wpdb->prepare(
204
+                "
205 205
 			SELECT post_id AS ID
206 206
 			FROM $wpdb->postmeta
207 207
 			WHERE meta_key = %s
208 208
 			 AND meta_value = %s
209 209
 			LIMIT 1
210 210
 		",
211
-				$meta_key,
212
-				$meta_value
213
-			)
214
-		);
211
+                $meta_key,
212
+                $meta_value
213
+            )
214
+        );
215 215
 
216
-		if ( $post_id === null ) {
217
-			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' ) );
218
-		}
216
+        if ( $post_id === null ) {
217
+            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' ) );
218
+        }
219 219
 
220
-		return $this->jsonld_using_post_id( array( 'id' => $post_id ) );
221
-	}
220
+        return $this->jsonld_using_post_id( array( 'id' => $post_id ) );
221
+    }
222 222
 
223
-	public function jsonld_using_meta( $request ) {
223
+    public function jsonld_using_meta( $request ) {
224 224
 
225
-		global $wpdb;
225
+        global $wpdb;
226 226
 
227
-		$meta_key   = $request['meta_key'];
228
-		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
227
+        $meta_key   = $request['meta_key'];
228
+        $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
229 229
 
230
-		$results = $wpdb->get_results(
231
-			$wpdb->prepare(
232
-				"
230
+        $results = $wpdb->get_results(
231
+            $wpdb->prepare(
232
+                "
233 233
 			SELECT pm.post_id AS id, %s AS type
234 234
 			 FROM {$wpdb->postmeta} pm
235 235
 			 	INNER JOIN {$wpdb->posts} p
@@ -240,28 +240,28 @@  discard block
 block discarded – undo
240 240
 			 FROM {$wpdb->termmeta}
241 241
 			 WHERE meta_key = %s AND meta_value = %s
242 242
 			",
243
-				Object_Type_Enum::POST,
244
-				$meta_key,
245
-				$meta_value,
246
-				Object_Type_Enum::TERM,
247
-				$meta_key,
248
-				$meta_value
249
-			)
250
-		);
251
-
252
-		$jsonld_service = $this->jsonld_service;
253
-
254
-		$data = array_reduce(
255
-			$results,
256
-			function ( $carry, $result ) use ( $jsonld_service ) {
257
-				$jsonld = $jsonld_service->get( $result->type, $result->id, Jsonld_Context_Enum::REST );
258
-
259
-				return array_merge( $carry, $jsonld );
260
-			},
261
-			array()
262
-		);
263
-
264
-		return Jsonld_Response_Helper::to_response( $data );
265
-	}
243
+                Object_Type_Enum::POST,
244
+                $meta_key,
245
+                $meta_value,
246
+                Object_Type_Enum::TERM,
247
+                $meta_key,
248
+                $meta_value
249
+            )
250
+        );
251
+
252
+        $jsonld_service = $this->jsonld_service;
253
+
254
+        $data = array_reduce(
255
+            $results,
256
+            function ( $carry, $result ) use ( $jsonld_service ) {
257
+                $jsonld = $jsonld_service->get( $result->type, $result->id, Jsonld_Context_Enum::REST );
258
+
259
+                return array_merge( $carry, $jsonld );
260
+            },
261
+            array()
262
+        );
263
+
264
+        return Jsonld_Response_Helper::to_response( $data );
265
+    }
266 266
 
267 267
 }
Please login to merge, or discard this patch.
Spacing   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -38,7 +38,7 @@  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;
@@ -47,19 +47,19 @@  discard block
 block discarded – undo
47 47
 		$that = $this;
48 48
 		add_action(
49 49
 			'rest_api_init',
50
-			function () use ( $that ) {
50
+			function() use ($that) {
51 51
 				register_rest_route(
52 52
 					WL_REST_ROUTE_DEFAULT_NAMESPACE,
53 53
 					'/jsonld/(?P<id>\d+)',
54 54
 					array(
55 55
 						'methods'             => WP_REST_Server::READABLE,
56
-						'callback'            => array( $that, 'jsonld_using_post_id' ),
56
+						'callback'            => array($that, 'jsonld_using_post_id'),
57 57
 						'permission_callback' => '__return_true',
58 58
 						'args'                => array(
59 59
 							'id' => array(
60 60
 								// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
61
-								'validate_callback' => function ( $param, $request, $key ) {
62
-									return is_numeric( $param );
61
+								'validate_callback' => function($param, $request, $key) {
62
+									return is_numeric($param);
63 63
 								},
64 64
 								'sanitize_callback' => 'absint',
65 65
 							),
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 					'/jsonld/http/(?P<item_id>.*)',
73 73
 					array(
74 74
 						'methods'             => 'GET',
75
-						'callback'            => array( $that, 'jsonld_using_item_id' ),
75
+						'callback'            => array($that, 'jsonld_using_item_id'),
76 76
 						'permission_callback' => '__return_true',
77 77
 					)
78 78
 				);
@@ -82,7 +82,7 @@  discard block
 block discarded – undo
82 82
 					'/jsonld/post-meta/(?P<meta_key>[^/]+)',
83 83
 					array(
84 84
 						'methods'             => 'GET',
85
-						'callback'            => array( $that, 'jsonld_using_post_meta' ),
85
+						'callback'            => array($that, 'jsonld_using_post_meta'),
86 86
 						'permission_callback' => '__return_true',
87 87
 					)
88 88
 				);
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
 					'/jsonld/meta/(?P<meta_key>[^/]+)',
93 93
 					array(
94 94
 						'methods'             => 'GET',
95
-						'callback'            => array( $that, 'jsonld_using_meta' ),
95
+						'callback'            => array($that, 'jsonld_using_meta'),
96 96
 						'permission_callback' => '__return_true',
97 97
 					)
98 98
 				);
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
 					'/jsonld/(?P<post_type>.*)/(?P<post_name>.*)',
103 103
 					array(
104 104
 						'methods'             => 'GET',
105
-						'callback'            => array( $that, 'jsonld_using_get_page_by_path' ),
105
+						'callback'            => array($that, 'jsonld_using_get_page_by_path'),
106 106
 						'permission_callback' => '__return_true',
107 107
 					)
108 108
 				);
@@ -124,15 +124,15 @@  discard block
 block discarded – undo
124 124
 	 * @return WP_REST_Response
125 125
 	 * @throws \Exception when an error occurs.
126 126
 	 */
127
-	public function jsonld_using_post_id( $request ) {
127
+	public function jsonld_using_post_id($request) {
128 128
 
129 129
 		$post_id = $request['id'];
130
-		$type    = ( 0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST );
130
+		$type    = (0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST);
131 131
 
132 132
 		// Send the generated JSON-LD.
133
-		$data = $this->jsonld_service->get( $type, $post_id, Jsonld_Context_Enum::REST );
133
+		$data = $this->jsonld_service->get($type, $post_id, Jsonld_Context_Enum::REST);
134 134
 
135
-		return Jsonld_Response_Helper::to_response( $data );
135
+		return Jsonld_Response_Helper::to_response($data);
136 136
 	}
137 137
 
138 138
 	/**
@@ -147,19 +147,19 @@  discard block
 block discarded – undo
147 147
 	 * @return WP_REST_Response
148 148
 	 * @throws \Exception when an error occurs.
149 149
 	 */
150
-	public function jsonld_using_item_id( $request ) {
150
+	public function jsonld_using_item_id($request) {
151 151
 
152
-		$item_id = 'http://' . $request['item_id'];
153
-		$post    = $this->entity_uri_service->get_entity( $item_id );
152
+		$item_id = 'http://'.$request['item_id'];
153
+		$post    = $this->entity_uri_service->get_entity($item_id);
154 154
 
155
-		if ( ! is_a( $post, 'WP_Post' ) ) {
156
-			return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) );
155
+		if ( ! is_a($post, 'WP_Post')) {
156
+			return new WP_REST_Response(esc_html("$item_id not found."), 404, array('Content-Type' => 'text/html'));
157 157
 		}
158 158
 
159
-		return $this->jsonld_using_post_id( array( 'id' => $post->ID ) );
159
+		return $this->jsonld_using_post_id(array('id' => $post->ID));
160 160
 	}
161 161
 
162
-	public function jsonld_using_get_page_by_path( $request ) {
162
+	public function jsonld_using_get_page_by_path($request) {
163 163
 
164 164
 		$post_name = $request['post_name'];
165 165
 		$post_type = $request['post_type'];
@@ -179,11 +179,11 @@  discard block
 block discarded – undo
179 179
 			)
180 180
 		);
181 181
 
182
-		if ( $post_id === null ) {
183
-			return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) );
182
+		if ($post_id === null) {
183
+			return new WP_REST_Response(esc_html("$post_name of type $post_type not found."), 404, array('Content-Type' => 'text/html'));
184 184
 		}
185 185
 
186
-		return $this->jsonld_using_post_id( array( 'id' => $post_id ) );
186
+		return $this->jsonld_using_post_id(array('id' => $post_id));
187 187
 	}
188 188
 
189 189
 	/**
@@ -192,10 +192,10 @@  discard block
 block discarded – undo
192 192
 	 * @return WP_REST_Response
193 193
 	 * @throws \Exception when an error occurs.
194 194
 	 */
195
-	public function jsonld_using_post_meta( $request ) {
195
+	public function jsonld_using_post_meta($request) {
196 196
 
197 197
 		$meta_key   = $request['meta_key'];
198
-		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
198
+		$meta_value = urldecode(current($request->get_query_params('meta_value')));
199 199
 
200 200
 		global $wpdb;
201 201
 
@@ -213,19 +213,19 @@  discard block
 block discarded – undo
213 213
 			)
214 214
 		);
215 215
 
216
-		if ( $post_id === null ) {
217
-			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' ) );
216
+		if ($post_id === null) {
217
+			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'));
218 218
 		}
219 219
 
220
-		return $this->jsonld_using_post_id( array( 'id' => $post_id ) );
220
+		return $this->jsonld_using_post_id(array('id' => $post_id));
221 221
 	}
222 222
 
223
-	public function jsonld_using_meta( $request ) {
223
+	public function jsonld_using_meta($request) {
224 224
 
225 225
 		global $wpdb;
226 226
 
227 227
 		$meta_key   = $request['meta_key'];
228
-		$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) );
228
+		$meta_value = urldecode(current($request->get_query_params('meta_value')));
229 229
 
230 230
 		$results = $wpdb->get_results(
231 231
 			$wpdb->prepare(
@@ -253,15 +253,15 @@  discard block
 block discarded – undo
253 253
 
254 254
 		$data = array_reduce(
255 255
 			$results,
256
-			function ( $carry, $result ) use ( $jsonld_service ) {
257
-				$jsonld = $jsonld_service->get( $result->type, $result->id, Jsonld_Context_Enum::REST );
256
+			function($carry, $result) use ($jsonld_service) {
257
+				$jsonld = $jsonld_service->get($result->type, $result->id, Jsonld_Context_Enum::REST);
258 258
 
259
-				return array_merge( $carry, $jsonld );
259
+				return array_merge($carry, $jsonld);
260 260
 			},
261 261
 			array()
262 262
 		);
263 263
 
264
-		return Jsonld_Response_Helper::to_response( $data );
264
+		return Jsonld_Response_Helper::to_response($data);
265 265
 	}
266 266
 
267 267
 }
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-post-reference.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@
 block discarded – undo
13 13
 
14 14
 class Post_Reference extends Abstract_Reference {
15 15
 
16
-	public function get_type() {
17
-		return Object_Type_Enum::POST;
18
-	}
16
+    public function get_type() {
17
+        return Object_Type_Enum::POST;
18
+    }
19 19
 }
Please login to merge, or discard this patch.