Completed
Push — develop ( 155215...157c8e )
by Naveen
01:39 queued 14s
created
src/modules/gardening-kg/includes/Jsonld.php 1 patch
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -7,74 +7,74 @@
 block discarded – undo
7 7
 
8 8
 class Jsonld {
9 9
 
10
-	/**
11
-	 * @var Content_Service $content_service
12
-	 */
13
-	private $content_service;
10
+    /**
11
+     * @var Content_Service $content_service
12
+     */
13
+    private $content_service;
14 14
 
15
-	public function __construct( Content_Service $content_service ) {
16
-		$this->content_service = $content_service;
17
-	}
15
+    public function __construct( Content_Service $content_service ) {
16
+        $this->content_service = $content_service;
17
+    }
18 18
 
19
-	public function register_hooks() {
20
-		add_action( 'wl_post_jsonld', array( $this, 'post_jsonld' ), 10, 2 );
21
-		add_action( 'wl_entity_jsonld', array( $this, 'post_jsonld' ), 10, 2 );
19
+    public function register_hooks() {
20
+        add_action( 'wl_post_jsonld', array( $this, 'post_jsonld' ), 10, 2 );
21
+        add_action( 'wl_entity_jsonld', array( $this, 'post_jsonld' ), 10, 2 );
22 22
 
23
-	}
23
+    }
24 24
 
25
-	public function post_jsonld( $jsonld, $post_id ) {
25
+    public function post_jsonld( $jsonld, $post_id ) {
26 26
 
27
-		$mentions = array();
28
-		$term_ids = $this->get_term_ids( $post_id );
27
+        $mentions = array();
28
+        $term_ids = $this->get_term_ids( $post_id );
29 29
 
30
-		foreach ( $term_ids as $term_id ) {
31
-			$this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_term( $term_id ) );
32
-		}
30
+        foreach ( $term_ids as $term_id ) {
31
+            $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_term( $term_id ) );
32
+        }
33 33
 
34
-		// Add also the post jsonld.
35
-		$this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_post( $post_id ) );
34
+        // Add also the post jsonld.
35
+        $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_post( $post_id ) );
36 36
 
37
-		$existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array();
37
+        $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array();
38 38
 
39
-		if ( count( $mentions ) > 0 ) {
40
-			$jsonld['mentions'] = array_merge( $existing_mentions, $mentions );
41
-		}
42
-		return $jsonld;
43
-	}
39
+        if ( count( $mentions ) > 0 ) {
40
+            $jsonld['mentions'] = array_merge( $existing_mentions, $mentions );
41
+        }
42
+        return $jsonld;
43
+    }
44 44
 
45
-	private function get_term_ids( $post_id ) {
46
-		global $wpdb;
47
-		$result = $wpdb->get_col(
48
-			$wpdb->prepare(
49
-				"SELECT tt.term_id from {$wpdb->prefix}term_relationships r 
45
+    private function get_term_ids( $post_id ) {
46
+        global $wpdb;
47
+        $result = $wpdb->get_col(
48
+            $wpdb->prepare(
49
+                "SELECT tt.term_id from {$wpdb->prefix}term_relationships r 
50 50
     INNER JOIN {$wpdb->prefix}term_taxonomy tt ON r.term_taxonomy_id=tt.term_taxonomy_id WHERE r.object_id= %d",
51
-				$post_id
52
-			)
53
-		);
54
-
55
-		return is_array( $result ) ? $result : array();
56
-	}
57
-
58
-	/**
59
-	 * @param $list array Adds the jsonld to the list if valid.
60
-	 * @param $content_id \Wordlift\Content\Wordpress\Wordpress_Content_Id
61
-	 *
62
-	 * @return void
63
-	 */
64
-	private function add_about_jsonld( &$list, $content_id ) {
65
-		$about_jsonld   = $this->content_service->get_about_jsonld(
66
-			$content_id
67
-		);
68
-		$decoded_jsonld = json_decode( $about_jsonld, true );
69
-
70
-		if ( null === $decoded_jsonld ) {
71
-			return;
72
-		}
73
-
74
-		$value = wp_is_numeric_array( $decoded_jsonld ) ? $decoded_jsonld : array( $decoded_jsonld );
75
-
76
-		$list = array_merge( $list, $value );
77
-
78
-	}
51
+                $post_id
52
+            )
53
+        );
54
+
55
+        return is_array( $result ) ? $result : array();
56
+    }
57
+
58
+    /**
59
+     * @param $list array Adds the jsonld to the list if valid.
60
+     * @param $content_id \Wordlift\Content\Wordpress\Wordpress_Content_Id
61
+     *
62
+     * @return void
63
+     */
64
+    private function add_about_jsonld( &$list, $content_id ) {
65
+        $about_jsonld   = $this->content_service->get_about_jsonld(
66
+            $content_id
67
+        );
68
+        $decoded_jsonld = json_decode( $about_jsonld, true );
69
+
70
+        if ( null === $decoded_jsonld ) {
71
+            return;
72
+        }
73
+
74
+        $value = wp_is_numeric_array( $decoded_jsonld ) ? $decoded_jsonld : array( $decoded_jsonld );
75
+
76
+        $list = array_merge( $list, $value );
77
+
78
+    }
79 79
 
80 80
 }
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-graph.php 2 patches
Indentation   +180 added lines, -180 removed lines patch added patch discarded remove patch
@@ -14,185 +14,185 @@
 block discarded – undo
14 14
  */
15 15
 class Graph {
16 16
 
17
-	/**
18
-	 * A single item in jsonld ( associative array )
19
-	 *
20
-	 * @var array
21
-	 */
22
-	private $main_jsonld;
23
-
24
-	/**
25
-	 * @var array<Content_Id>
26
-	 */
27
-	private $referenced_content_ids = array();
28
-	/**
29
-	 * @var \Wordlift_Post_Converter
30
-	 */
31
-	private $post_converter;
32
-	/**
33
-	 * @var \Wordlift_Term_JsonLd_Adapter
34
-	 */
35
-	private $term_converter;
36
-
37
-	public function __construct( $main_jsonld, $post_converter, $term_converter ) {
38
-		$this->main_jsonld    = $main_jsonld;
39
-		$this->post_converter = $post_converter;
40
-		$this->term_converter = $term_converter;
41
-	}
42
-
43
-	public function set_main_jsonld( $main_jsonld ) {
44
-		$this->main_jsonld = $main_jsonld;
45
-	}
46
-
47
-	public function get_main_jsonld() {
48
-		return $this->main_jsonld;
49
-	}
50
-
51
-	/**
52
-	 * @param $references array<int>
53
-	 *
54
-	 * @return Graph
55
-	 */
56
-	public function add_references( $refs ) {
57
-		Assertions::is_array( $refs );
58
-
59
-		foreach ( $refs as $ref ) {
60
-			$this->referenced_content_ids[] = Wordpress_Content_Id::create_post( $ref );
61
-		}
62
-		return $this;
63
-	}
64
-
65
-	/**
66
-	 * @param $reference_infos array
67
-	 *     Structure: [
68
-	 *         [
69
-	 *             'reference' => \Wordlift_Property_Entity_Reference,
70
-	 *         ],
71
-	 *         // more array items ...
72
-	 *     ]
73
-	 *
74
-	 * @return Graph
75
-	 */
76
-	public function add_required_reference_infos( $references_infos ) {
77
-
78
-		/**
79
-		 * @var $required_references array<\Wordlift_Property_Entity_Reference>
80
-		 */
81
-		$required_references = array_filter(
82
-			$references_infos,
83
-			function ( $item ) {
84
-				return isset( $item['reference'] ) &&
85
-					   $item['reference'] instanceof \Wordlift_Property_Entity_Reference &&
86
-					   // Check that the reference is required
87
-					   $item['reference']->get_required();
88
-			}
89
-		);
90
-
91
-		foreach ( $required_references as $data ) {
92
-			$required_reference             = $data['reference'];
93
-			$this->referenced_content_ids[] = new Wordpress_Content_Id(
94
-				$required_reference->get_id(),
95
-				$required_reference->get_type()
96
-			);
97
-		}
98
-		return $this;
99
-
100
-	}
101
-
102
-	/**
103
-	 * @param $relations Relations
104
-	 *
105
-	 * @return Graph
106
-	 */
107
-	public function add_relations( $relations ) {
108
-
109
-		foreach ( $relations->toArray() as $relation ) {
110
-
111
-			$this->referenced_content_ids[] = $relation->get_object();
112
-
113
-		}
114
-
115
-		return $this;
116
-	}
117
-
118
-	/**
119
-	 * This method expands the content id and return only the jsonld.
120
-	 *
121
-	 * @param $content_id Wordpress_Content_Id
122
-	 * @param $context int
123
-	 * @return array|bool
124
-	 */
125
-	private function expand( $content_id, $context ) {
126
-		$object_id   = $content_id->get_id();
127
-		$object_type = $content_id->get_type();
128
-
129
-		if ( $object_type === Object_Type_Enum::POST ) {
130
-			return $this->expand_post( $object_id );
131
-		} elseif ( $object_type === Object_Type_Enum::TERM ) {
132
-			return $this->expand_term( $object_id, $context );
133
-		}
134
-
135
-		return false;
136
-
137
-	}
138
-
139
-	/**
140
-	 * @param $context int Instance of Jsonld_Context_Enum
141
-	 *
142
-	 * @return array
143
-	 */
144
-	public function render( $context ) {
145
-
146
-		/**
147
-		 * This is possible because the toString() method of
148
-		 * Wordpress_Content_Id is used to get the unique value.
149
-		 */
150
-		$unique_content_ids = array_unique( $this->referenced_content_ids, SORT_STRING );
151
-
152
-		$result = array( $this->main_jsonld );
153
-
154
-		foreach ( $unique_content_ids as $unique_content_id ) {
155
-			$result[] = $this->expand( $unique_content_id, $context );
156
-		}
157
-
158
-		// Filter out the false and empty results.
159
-		return array_filter( $result );
160
-
161
-	}
162
-
163
-	/**
164
-	 * @param $object_id
165
-	 *
166
-	 * @return false|mixed
167
-	 */
168
-	public function expand_post( $object_id ) {
169
-		if ( get_post_status( $object_id ) !== 'publish' || \Wordlift_Entity_Type_Service::get_instance()->has_entity_type(
170
-			$object_id,
171
-			'http://schema.org/Article'
172
-		) ) {
173
-			return false;
174
-		}
175
-
176
-		$references     = array();
177
-		$reference_info = array();
178
-		$relations      = new Relations();
179
-
180
-		return $this->post_converter->convert( $object_id, $references, $reference_info, $relations );
181
-	}
182
-
183
-	/**
184
-	 * @param $object_id
185
-	 * @param $context
186
-	 *
187
-	 * @return false|mixed
188
-	 */
189
-	public function expand_term( $object_id, $context ) {
190
-		// Skip the Uncategorized term.
191
-		if ( 1 === $object_id ) {
192
-			return false;
193
-		}
194
-
195
-		return current( $this->term_converter->get( $object_id, $context ) );
196
-	}
17
+    /**
18
+     * A single item in jsonld ( associative array )
19
+     *
20
+     * @var array
21
+     */
22
+    private $main_jsonld;
23
+
24
+    /**
25
+     * @var array<Content_Id>
26
+     */
27
+    private $referenced_content_ids = array();
28
+    /**
29
+     * @var \Wordlift_Post_Converter
30
+     */
31
+    private $post_converter;
32
+    /**
33
+     * @var \Wordlift_Term_JsonLd_Adapter
34
+     */
35
+    private $term_converter;
36
+
37
+    public function __construct( $main_jsonld, $post_converter, $term_converter ) {
38
+        $this->main_jsonld    = $main_jsonld;
39
+        $this->post_converter = $post_converter;
40
+        $this->term_converter = $term_converter;
41
+    }
42
+
43
+    public function set_main_jsonld( $main_jsonld ) {
44
+        $this->main_jsonld = $main_jsonld;
45
+    }
46
+
47
+    public function get_main_jsonld() {
48
+        return $this->main_jsonld;
49
+    }
50
+
51
+    /**
52
+     * @param $references array<int>
53
+     *
54
+     * @return Graph
55
+     */
56
+    public function add_references( $refs ) {
57
+        Assertions::is_array( $refs );
58
+
59
+        foreach ( $refs as $ref ) {
60
+            $this->referenced_content_ids[] = Wordpress_Content_Id::create_post( $ref );
61
+        }
62
+        return $this;
63
+    }
64
+
65
+    /**
66
+     * @param $reference_infos array
67
+     *     Structure: [
68
+     *         [
69
+     *             'reference' => \Wordlift_Property_Entity_Reference,
70
+     *         ],
71
+     *         // more array items ...
72
+     *     ]
73
+     *
74
+     * @return Graph
75
+     */
76
+    public function add_required_reference_infos( $references_infos ) {
77
+
78
+        /**
79
+         * @var $required_references array<\Wordlift_Property_Entity_Reference>
80
+         */
81
+        $required_references = array_filter(
82
+            $references_infos,
83
+            function ( $item ) {
84
+                return isset( $item['reference'] ) &&
85
+                       $item['reference'] instanceof \Wordlift_Property_Entity_Reference &&
86
+                       // Check that the reference is required
87
+                       $item['reference']->get_required();
88
+            }
89
+        );
90
+
91
+        foreach ( $required_references as $data ) {
92
+            $required_reference             = $data['reference'];
93
+            $this->referenced_content_ids[] = new Wordpress_Content_Id(
94
+                $required_reference->get_id(),
95
+                $required_reference->get_type()
96
+            );
97
+        }
98
+        return $this;
99
+
100
+    }
101
+
102
+    /**
103
+     * @param $relations Relations
104
+     *
105
+     * @return Graph
106
+     */
107
+    public function add_relations( $relations ) {
108
+
109
+        foreach ( $relations->toArray() as $relation ) {
110
+
111
+            $this->referenced_content_ids[] = $relation->get_object();
112
+
113
+        }
114
+
115
+        return $this;
116
+    }
117
+
118
+    /**
119
+     * This method expands the content id and return only the jsonld.
120
+     *
121
+     * @param $content_id Wordpress_Content_Id
122
+     * @param $context int
123
+     * @return array|bool
124
+     */
125
+    private function expand( $content_id, $context ) {
126
+        $object_id   = $content_id->get_id();
127
+        $object_type = $content_id->get_type();
128
+
129
+        if ( $object_type === Object_Type_Enum::POST ) {
130
+            return $this->expand_post( $object_id );
131
+        } elseif ( $object_type === Object_Type_Enum::TERM ) {
132
+            return $this->expand_term( $object_id, $context );
133
+        }
134
+
135
+        return false;
136
+
137
+    }
138
+
139
+    /**
140
+     * @param $context int Instance of Jsonld_Context_Enum
141
+     *
142
+     * @return array
143
+     */
144
+    public function render( $context ) {
145
+
146
+        /**
147
+         * This is possible because the toString() method of
148
+         * Wordpress_Content_Id is used to get the unique value.
149
+         */
150
+        $unique_content_ids = array_unique( $this->referenced_content_ids, SORT_STRING );
151
+
152
+        $result = array( $this->main_jsonld );
153
+
154
+        foreach ( $unique_content_ids as $unique_content_id ) {
155
+            $result[] = $this->expand( $unique_content_id, $context );
156
+        }
157
+
158
+        // Filter out the false and empty results.
159
+        return array_filter( $result );
160
+
161
+    }
162
+
163
+    /**
164
+     * @param $object_id
165
+     *
166
+     * @return false|mixed
167
+     */
168
+    public function expand_post( $object_id ) {
169
+        if ( get_post_status( $object_id ) !== 'publish' || \Wordlift_Entity_Type_Service::get_instance()->has_entity_type(
170
+            $object_id,
171
+            'http://schema.org/Article'
172
+        ) ) {
173
+            return false;
174
+        }
175
+
176
+        $references     = array();
177
+        $reference_info = array();
178
+        $relations      = new Relations();
179
+
180
+        return $this->post_converter->convert( $object_id, $references, $reference_info, $relations );
181
+    }
182
+
183
+    /**
184
+     * @param $object_id
185
+     * @param $context
186
+     *
187
+     * @return false|mixed
188
+     */
189
+    public function expand_term( $object_id, $context ) {
190
+        // Skip the Uncategorized term.
191
+        if ( 1 === $object_id ) {
192
+            return false;
193
+        }
194
+
195
+        return current( $this->term_converter->get( $object_id, $context ) );
196
+    }
197 197
 
198 198
 }
Please login to merge, or discard this patch.
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -34,13 +34,13 @@  discard block
 block discarded – undo
34 34
 	 */
35 35
 	private $term_converter;
36 36
 
37
-	public function __construct( $main_jsonld, $post_converter, $term_converter ) {
37
+	public function __construct($main_jsonld, $post_converter, $term_converter) {
38 38
 		$this->main_jsonld    = $main_jsonld;
39 39
 		$this->post_converter = $post_converter;
40 40
 		$this->term_converter = $term_converter;
41 41
 	}
42 42
 
43
-	public function set_main_jsonld( $main_jsonld ) {
43
+	public function set_main_jsonld($main_jsonld) {
44 44
 		$this->main_jsonld = $main_jsonld;
45 45
 	}
46 46
 
@@ -53,11 +53,11 @@  discard block
 block discarded – undo
53 53
 	 *
54 54
 	 * @return Graph
55 55
 	 */
56
-	public function add_references( $refs ) {
57
-		Assertions::is_array( $refs );
56
+	public function add_references($refs) {
57
+		Assertions::is_array($refs);
58 58
 
59
-		foreach ( $refs as $ref ) {
60
-			$this->referenced_content_ids[] = Wordpress_Content_Id::create_post( $ref );
59
+		foreach ($refs as $ref) {
60
+			$this->referenced_content_ids[] = Wordpress_Content_Id::create_post($ref);
61 61
 		}
62 62
 		return $this;
63 63
 	}
@@ -73,22 +73,22 @@  discard block
 block discarded – undo
73 73
 	 *
74 74
 	 * @return Graph
75 75
 	 */
76
-	public function add_required_reference_infos( $references_infos ) {
76
+	public function add_required_reference_infos($references_infos) {
77 77
 
78 78
 		/**
79 79
 		 * @var $required_references array<\Wordlift_Property_Entity_Reference>
80 80
 		 */
81 81
 		$required_references = array_filter(
82 82
 			$references_infos,
83
-			function ( $item ) {
84
-				return isset( $item['reference'] ) &&
83
+			function($item) {
84
+				return isset($item['reference']) &&
85 85
 					   $item['reference'] instanceof \Wordlift_Property_Entity_Reference &&
86 86
 					   // Check that the reference is required
87 87
 					   $item['reference']->get_required();
88 88
 			}
89 89
 		);
90 90
 
91
-		foreach ( $required_references as $data ) {
91
+		foreach ($required_references as $data) {
92 92
 			$required_reference             = $data['reference'];
93 93
 			$this->referenced_content_ids[] = new Wordpress_Content_Id(
94 94
 				$required_reference->get_id(),
@@ -104,9 +104,9 @@  discard block
 block discarded – undo
104 104
 	 *
105 105
 	 * @return Graph
106 106
 	 */
107
-	public function add_relations( $relations ) {
107
+	public function add_relations($relations) {
108 108
 
109
-		foreach ( $relations->toArray() as $relation ) {
109
+		foreach ($relations->toArray() as $relation) {
110 110
 
111 111
 			$this->referenced_content_ids[] = $relation->get_object();
112 112
 
@@ -122,14 +122,14 @@  discard block
 block discarded – undo
122 122
 	 * @param $context int
123 123
 	 * @return array|bool
124 124
 	 */
125
-	private function expand( $content_id, $context ) {
125
+	private function expand($content_id, $context) {
126 126
 		$object_id   = $content_id->get_id();
127 127
 		$object_type = $content_id->get_type();
128 128
 
129
-		if ( $object_type === Object_Type_Enum::POST ) {
130
-			return $this->expand_post( $object_id );
131
-		} elseif ( $object_type === Object_Type_Enum::TERM ) {
132
-			return $this->expand_term( $object_id, $context );
129
+		if ($object_type === Object_Type_Enum::POST) {
130
+			return $this->expand_post($object_id);
131
+		} elseif ($object_type === Object_Type_Enum::TERM) {
132
+			return $this->expand_term($object_id, $context);
133 133
 		}
134 134
 
135 135
 		return false;
@@ -141,22 +141,22 @@  discard block
 block discarded – undo
141 141
 	 *
142 142
 	 * @return array
143 143
 	 */
144
-	public function render( $context ) {
144
+	public function render($context) {
145 145
 
146 146
 		/**
147 147
 		 * This is possible because the toString() method of
148 148
 		 * Wordpress_Content_Id is used to get the unique value.
149 149
 		 */
150
-		$unique_content_ids = array_unique( $this->referenced_content_ids, SORT_STRING );
150
+		$unique_content_ids = array_unique($this->referenced_content_ids, SORT_STRING);
151 151
 
152
-		$result = array( $this->main_jsonld );
152
+		$result = array($this->main_jsonld);
153 153
 
154
-		foreach ( $unique_content_ids as $unique_content_id ) {
155
-			$result[] = $this->expand( $unique_content_id, $context );
154
+		foreach ($unique_content_ids as $unique_content_id) {
155
+			$result[] = $this->expand($unique_content_id, $context);
156 156
 		}
157 157
 
158 158
 		// Filter out the false and empty results.
159
-		return array_filter( $result );
159
+		return array_filter($result);
160 160
 
161 161
 	}
162 162
 
@@ -165,11 +165,11 @@  discard block
 block discarded – undo
165 165
 	 *
166 166
 	 * @return false|mixed
167 167
 	 */
168
-	public function expand_post( $object_id ) {
169
-		if ( get_post_status( $object_id ) !== 'publish' || \Wordlift_Entity_Type_Service::get_instance()->has_entity_type(
168
+	public function expand_post($object_id) {
169
+		if (get_post_status($object_id) !== 'publish' || \Wordlift_Entity_Type_Service::get_instance()->has_entity_type(
170 170
 			$object_id,
171 171
 			'http://schema.org/Article'
172
-		) ) {
172
+		)) {
173 173
 			return false;
174 174
 		}
175 175
 
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 		$reference_info = array();
178 178
 		$relations      = new Relations();
179 179
 
180
-		return $this->post_converter->convert( $object_id, $references, $reference_info, $relations );
180
+		return $this->post_converter->convert($object_id, $references, $reference_info, $relations);
181 181
 	}
182 182
 
183 183
 	/**
@@ -186,13 +186,13 @@  discard block
 block discarded – undo
186 186
 	 *
187 187
 	 * @return false|mixed
188 188
 	 */
189
-	public function expand_term( $object_id, $context ) {
189
+	public function expand_term($object_id, $context) {
190 190
 		// Skip the Uncategorized term.
191
-		if ( 1 === $object_id ) {
191
+		if (1 === $object_id) {
192 192
 			return false;
193 193
 		}
194 194
 
195
-		return current( $this->term_converter->get( $object_id, $context ) );
195
+		return current($this->term_converter->get($object_id, $context));
196 196
 	}
197 197
 
198 198
 }
Please login to merge, or discard this patch.