Completed
Pull Request — develop (#1554)
by Naveen
48s
created
src/wordlift/jsonld/class-reference-processor.php 2 patches
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -12,43 +12,43 @@
 block discarded – undo
12 12
 
13 13
 class Reference_Processor  extends  Singleton {
14 14
 
15
-	/**
16
-	 * @return Reference_Processor
17
-	 */
18
-	public static function get_instance() {
19
-		return parent::get_instance();
20
-	}
21
-
22
-
23
-	public function serialize_references( $references ) {
24
-
25
-		return array_map( function ( $reference ) {
26
-			if ( $reference instanceof Post_Reference ) {
27
-				return 'post_' . $reference->get_id();
28
-			} else if ( $reference instanceof Term_Reference ) {
29
-				return 'term_' . $reference->get_id();
30
-			}
31
-			// Backward compatibility with other hooks pushing
32
-			// references in to the cache.
33
-			return $reference;
34
-		}, $references );
35
-	}
36
-
37
-	public function deserialize_references( $references ) {
38
-
39
-		return array_map( function ( $reference ) {
40
-
41
-			if ( strpos( $reference, 'post_' ) !== false ) {
42
-				return new Post_Reference( (int) str_replace('post_', '', $reference ) );
43
-			} else if ( strpos( $reference, 'term_' ) !== false  ) {
44
-				return new Term_Reference( (int) str_replace('term_', '', $reference ) );
45
-			}
46
-			// Backward compatibility with other hooks pushing
47
-			// references in to the cache.
48
-			return new Post_Reference( (int) $reference );
49
-		}, $references );
50
-
51
-
52
-	}
15
+    /**
16
+     * @return Reference_Processor
17
+     */
18
+    public static function get_instance() {
19
+        return parent::get_instance();
20
+    }
21
+
22
+
23
+    public function serialize_references( $references ) {
24
+
25
+        return array_map( function ( $reference ) {
26
+            if ( $reference instanceof Post_Reference ) {
27
+                return 'post_' . $reference->get_id();
28
+            } else if ( $reference instanceof Term_Reference ) {
29
+                return 'term_' . $reference->get_id();
30
+            }
31
+            // Backward compatibility with other hooks pushing
32
+            // references in to the cache.
33
+            return $reference;
34
+        }, $references );
35
+    }
36
+
37
+    public function deserialize_references( $references ) {
38
+
39
+        return array_map( function ( $reference ) {
40
+
41
+            if ( strpos( $reference, 'post_' ) !== false ) {
42
+                return new Post_Reference( (int) str_replace('post_', '', $reference ) );
43
+            } else if ( strpos( $reference, 'term_' ) !== false  ) {
44
+                return new Term_Reference( (int) str_replace('term_', '', $reference ) );
45
+            }
46
+            // Backward compatibility with other hooks pushing
47
+            // references in to the cache.
48
+            return new Post_Reference( (int) $reference );
49
+        }, $references );
50
+
51
+
52
+    }
53 53
 
54 54
 }
55 55
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -20,33 +20,33 @@
 block discarded – undo
20 20
 	}
21 21
 
22 22
 
23
-	public function serialize_references( $references ) {
23
+	public function serialize_references($references) {
24 24
 
25
-		return array_map( function ( $reference ) {
26
-			if ( $reference instanceof Post_Reference ) {
27
-				return 'post_' . $reference->get_id();
28
-			} else if ( $reference instanceof Term_Reference ) {
29
-				return 'term_' . $reference->get_id();
25
+		return array_map(function($reference) {
26
+			if ($reference instanceof Post_Reference) {
27
+				return 'post_'.$reference->get_id();
28
+			} else if ($reference instanceof Term_Reference) {
29
+				return 'term_'.$reference->get_id();
30 30
 			}
31 31
 			// Backward compatibility with other hooks pushing
32 32
 			// references in to the cache.
33 33
 			return $reference;
34
-		}, $references );
34
+		}, $references);
35 35
 	}
36 36
 
37
-	public function deserialize_references( $references ) {
37
+	public function deserialize_references($references) {
38 38
 
39
-		return array_map( function ( $reference ) {
39
+		return array_map(function($reference) {
40 40
 
41
-			if ( strpos( $reference, 'post_' ) !== false ) {
42
-				return new Post_Reference( (int) str_replace('post_', '', $reference ) );
43
-			} else if ( strpos( $reference, 'term_' ) !== false  ) {
44
-				return new Term_Reference( (int) str_replace('term_', '', $reference ) );
41
+			if (strpos($reference, 'post_') !== false) {
42
+				return new Post_Reference((int) str_replace('post_', '', $reference));
43
+			} else if (strpos($reference, 'term_') !== false) {
44
+				return new Term_Reference((int) str_replace('term_', '', $reference));
45 45
 			}
46 46
 			// Backward compatibility with other hooks pushing
47 47
 			// references in to the cache.
48
-			return new Post_Reference( (int) $reference );
49
-		}, $references );
48
+			return new Post_Reference((int) $reference);
49
+		}, $references);
50 50
 
51 51
 
52 52
 	}
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
@@ -12,7 +12,7 @@
 block discarded – undo
12 12
 
13 13
 class Term_Reference extends Abstract_Reference {
14 14
 
15
-	function get_type() {
16
-		return Object_Type_Enum::TERM;
17
-	}
15
+    function get_type() {
16
+        return Object_Type_Enum::TERM;
17
+    }
18 18
 }
19 19
\ No newline at end of file
Please login to merge, or discard this patch.
src/wordlift/jsonld/class-reference.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -10,15 +10,15 @@
 block discarded – undo
10 10
 
11 11
 interface Reference {
12 12
 
13
-	/**
14
-	 * @return int
15
-	 */
16
-	public function get_type();
13
+    /**
14
+     * @return int
15
+     */
16
+    public function get_type();
17 17
 
18
-	/**
19
-	 * @return int Identifier
20
-	 */
21
-	public function get_id();
18
+    /**
19
+     * @return int Identifier
20
+     */
21
+    public function get_id();
22 22
 
23 23
 }
24 24
 
Please login to merge, or discard this patch.
src/wordlift/link/class-link-builder.php 2 patches
Indentation   +89 added lines, -89 removed lines patch added patch discarded remove patch
@@ -10,97 +10,97 @@
 block discarded – undo
10 10
 
11 11
 class Link_Builder {
12 12
 
13
-	private $id;
14
-	private $type;
15
-	private $label;
16
-	private $href;
17
-	private $entity_url;
18
-	/**
19
-	 * @var Object_Link_Provider
20
-	 */
21
-	private $object_link_provider;
22
-
23
-	public function __construct( $entity_url, $id ) {
24
-		$this->entity_url           = $entity_url;
25
-		$this->id                   = $id;
26
-		$this->object_link_provider = Object_Link_Provider::get_instance();
27
-		$this->type                 = $this->object_link_provider->get_object_type( $entity_url );
28
-	}
29
-
30
-	public static function create( $entity_url, $id ) {
31
-		return new Link_Builder( $entity_url, $id );
32
-	}
33
-
34
-	public function label( $label ) {
35
-		$this->label = $label;
36
-
37
-		return $this;
38
-	}
39
-
40
-	public function href( $href ) {
41
-		$this->href = $href;
42
-
43
-		return $this;
44
-	}
45
-
46
-	private function get_attributes_for_link() {
47
-		/**
48
-		 * Allow 3rd parties to add additional attributes to the anchor link.
49
-		 *
50
-		 * @since 3.26.0
51
-		 */
52
-		$default_attributes = array(
53
-			'id' => implode( ';', $this->object_link_provider->get_same_as_uris( $this->id, $this->type ) )
54
-		);
55
-
56
-		/**
57
-		 * @since 3.32.0
58
-		 * Additional parameter {@link $this->type} is added to the filter denoting the type of
59
-		 * the entity url by the enum values {@link Object_Type_Enum}
60
-		 */
61
-		$attributes      = apply_filters( 'wl_anchor_data_attributes', $default_attributes, $this->id, $this->type );
62
-		$attributes_html = '';
63
-		foreach ( $attributes as $key => $value ) {
64
-			$attributes_html .= ' data-' . esc_html( $key ) . '="' . esc_attr( $value ) . '" ';
65
-		}
66
-
67
-		return $attributes_html;
68
-	}
69
-
70
-	/**
71
-	 * Get a `title` attribute with an alternative label for the link.
72
-	 *
73
-	 * If an alternative title isn't available an empty string is returned.
74
-	 *
75
-	 * @return string A `title` attribute with an alternative label or an empty
76
-	 *                string if none available.
77
-	 * @since 3.32.0
78
-	 *
79
-	 */
80
-	private function get_title_attribute() {
81
-
82
-		// Get an alternative title.
83
-		$title = $this->object_link_provider->get_link_title( $this->id, $this->label, $this->type );
84
-		if ( ! empty( $title ) ) {
85
-			return 'title="' . esc_attr( $title ) . '"';
86
-		}
87
-
88
-		return '';
89
-	}
90
-
91
-	/**
92
-	 * @return string
93
-	 */
94
-	public function generate_link() {
95
-		// Get an alternative title attribute.
96
-		$title_attribute = $this->get_title_attribute();
97
-		$attributes_html = $this->get_attributes_for_link();
98
-
99
-		// Return the link.
100
-		return <<<EOF
13
+    private $id;
14
+    private $type;
15
+    private $label;
16
+    private $href;
17
+    private $entity_url;
18
+    /**
19
+     * @var Object_Link_Provider
20
+     */
21
+    private $object_link_provider;
22
+
23
+    public function __construct( $entity_url, $id ) {
24
+        $this->entity_url           = $entity_url;
25
+        $this->id                   = $id;
26
+        $this->object_link_provider = Object_Link_Provider::get_instance();
27
+        $this->type                 = $this->object_link_provider->get_object_type( $entity_url );
28
+    }
29
+
30
+    public static function create( $entity_url, $id ) {
31
+        return new Link_Builder( $entity_url, $id );
32
+    }
33
+
34
+    public function label( $label ) {
35
+        $this->label = $label;
36
+
37
+        return $this;
38
+    }
39
+
40
+    public function href( $href ) {
41
+        $this->href = $href;
42
+
43
+        return $this;
44
+    }
45
+
46
+    private function get_attributes_for_link() {
47
+        /**
48
+         * Allow 3rd parties to add additional attributes to the anchor link.
49
+         *
50
+         * @since 3.26.0
51
+         */
52
+        $default_attributes = array(
53
+            'id' => implode( ';', $this->object_link_provider->get_same_as_uris( $this->id, $this->type ) )
54
+        );
55
+
56
+        /**
57
+         * @since 3.32.0
58
+         * Additional parameter {@link $this->type} is added to the filter denoting the type of
59
+         * the entity url by the enum values {@link Object_Type_Enum}
60
+         */
61
+        $attributes      = apply_filters( 'wl_anchor_data_attributes', $default_attributes, $this->id, $this->type );
62
+        $attributes_html = '';
63
+        foreach ( $attributes as $key => $value ) {
64
+            $attributes_html .= ' data-' . esc_html( $key ) . '="' . esc_attr( $value ) . '" ';
65
+        }
66
+
67
+        return $attributes_html;
68
+    }
69
+
70
+    /**
71
+     * Get a `title` attribute with an alternative label for the link.
72
+     *
73
+     * If an alternative title isn't available an empty string is returned.
74
+     *
75
+     * @return string A `title` attribute with an alternative label or an empty
76
+     *                string if none available.
77
+     * @since 3.32.0
78
+     *
79
+     */
80
+    private function get_title_attribute() {
81
+
82
+        // Get an alternative title.
83
+        $title = $this->object_link_provider->get_link_title( $this->id, $this->label, $this->type );
84
+        if ( ! empty( $title ) ) {
85
+            return 'title="' . esc_attr( $title ) . '"';
86
+        }
87
+
88
+        return '';
89
+    }
90
+
91
+    /**
92
+     * @return string
93
+     */
94
+    public function generate_link() {
95
+        // Get an alternative title attribute.
96
+        $title_attribute = $this->get_title_attribute();
97
+        $attributes_html = $this->get_attributes_for_link();
98
+
99
+        // Return the link.
100
+        return <<<EOF
101 101
 <a class="wl-entity-page-link" $title_attribute href="{$this->href}"$attributes_html>{$this->label}</a>
102 102
 EOF;
103
-	}
103
+    }
104 104
 
105 105
 
106 106
 }
107 107
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -20,24 +20,24 @@  discard block
 block discarded – undo
20 20
 	 */
21 21
 	private $object_link_provider;
22 22
 
23
-	public function __construct( $entity_url, $id ) {
23
+	public function __construct($entity_url, $id) {
24 24
 		$this->entity_url           = $entity_url;
25 25
 		$this->id                   = $id;
26 26
 		$this->object_link_provider = Object_Link_Provider::get_instance();
27
-		$this->type                 = $this->object_link_provider->get_object_type( $entity_url );
27
+		$this->type                 = $this->object_link_provider->get_object_type($entity_url);
28 28
 	}
29 29
 
30
-	public static function create( $entity_url, $id ) {
31
-		return new Link_Builder( $entity_url, $id );
30
+	public static function create($entity_url, $id) {
31
+		return new Link_Builder($entity_url, $id);
32 32
 	}
33 33
 
34
-	public function label( $label ) {
34
+	public function label($label) {
35 35
 		$this->label = $label;
36 36
 
37 37
 		return $this;
38 38
 	}
39 39
 
40
-	public function href( $href ) {
40
+	public function href($href) {
41 41
 		$this->href = $href;
42 42
 
43 43
 		return $this;
@@ -50,7 +50,7 @@  discard block
 block discarded – undo
50 50
 		 * @since 3.26.0
51 51
 		 */
52 52
 		$default_attributes = array(
53
-			'id' => implode( ';', $this->object_link_provider->get_same_as_uris( $this->id, $this->type ) )
53
+			'id' => implode(';', $this->object_link_provider->get_same_as_uris($this->id, $this->type))
54 54
 		);
55 55
 
56 56
 		/**
@@ -58,10 +58,10 @@  discard block
 block discarded – undo
58 58
 		 * Additional parameter {@link $this->type} is added to the filter denoting the type of
59 59
 		 * the entity url by the enum values {@link Object_Type_Enum}
60 60
 		 */
61
-		$attributes      = apply_filters( 'wl_anchor_data_attributes', $default_attributes, $this->id, $this->type );
61
+		$attributes      = apply_filters('wl_anchor_data_attributes', $default_attributes, $this->id, $this->type);
62 62
 		$attributes_html = '';
63
-		foreach ( $attributes as $key => $value ) {
64
-			$attributes_html .= ' data-' . esc_html( $key ) . '="' . esc_attr( $value ) . '" ';
63
+		foreach ($attributes as $key => $value) {
64
+			$attributes_html .= ' data-'.esc_html($key).'="'.esc_attr($value).'" ';
65 65
 		}
66 66
 
67 67
 		return $attributes_html;
@@ -80,9 +80,9 @@  discard block
 block discarded – undo
80 80
 	private function get_title_attribute() {
81 81
 
82 82
 		// Get an alternative title.
83
-		$title = $this->object_link_provider->get_link_title( $this->id, $this->label, $this->type );
84
-		if ( ! empty( $title ) ) {
85
-			return 'title="' . esc_attr( $title ) . '"';
83
+		$title = $this->object_link_provider->get_link_title($this->id, $this->label, $this->type);
84
+		if ( ! empty($title)) {
85
+			return 'title="'.esc_attr($title).'"';
86 86
 		}
87 87
 
88 88
 		return '';
Please login to merge, or discard this patch.
src/includes/class-wordlift-jsonld-service.php 3 patches
Indentation   +238 added lines, -238 removed lines patch added patch discarded remove patch
@@ -17,261 +17,261 @@
 block discarded – undo
17 17
  */
18 18
 class Wordlift_Jsonld_Service {
19 19
 
20
-	/**
21
-	 * A {@link Wordlift_Entity_Service} instance.
22
-	 *
23
-	 * @since  3.8.0
24
-	 * @access private
25
-	 * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
26
-	 */
27
-	private $entity_service;
28
-
29
-	/**
30
-	 * A {@link Wordlift_Term_JsonLd_Adapter} instance.
31
-	 *
32
-	 * @since  3.32.0
33
-	 * @access private
34
-	 * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance.
35
-	 */
36
-	private $term_jsonld_adapter;
37
-
38
-	/**
39
-	 * A {@link Wordlift_Post_Converter} instance.
40
-	 *
41
-	 * @since  3.8.0
42
-	 * @access private
43
-	 * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance.
44
-	 */
45
-	private $converter;
46
-
47
-
48
-	/**
49
-	 * A {@link Wordlift_Website_Jsonld_Converter} instance.
50
-	 *
51
-	 * @since  3.14.0
52
-	 * @access private
53
-	 * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance.
54
-	 */
55
-	private $website_converter;
56
-
57
-	/**
58
-	 * The singleton instance for the JSON-LD service.
59
-	 *
60
-	 * @since 3.15.1
61
-	 *
62
-	 * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service.
63
-	 */
64
-	private static $instance;
65
-
66
-	/**
67
-	 * Create a JSON-LD service.
68
-	 *
69
-	 * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
70
-	 * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance.
71
-	 * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance.
72
-	 * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter
73
-	 *
74
-	 * @since 3.8.0
75
-	 */
76
-	public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) {
77
-
78
-		$this->entity_service      = $entity_service;
79
-		$this->converter           = $converter;
80
-		$this->website_converter   = $website_converter;
81
-		$this->term_jsonld_adapter = $term_jsonld_adapter;
82
-		self::$instance            = $this;
83
-
84
-	}
85
-
86
-	/**
87
-	 * Get the singleton instance for the JSON-LD service.
88
-	 *
89
-	 * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service.
90
-	 * @since 3.15.1
91
-	 *
92
-	 */
93
-	public static function get_instance() {
94
-
95
-		return self::$instance;
96
-	}
97
-
98
-	/**
99
-	 * Process calls to the AJAX 'wl_jsonld' endpoint.
100
-	 *
101
-	 * @since 3.8.0
102
-	 */
103
-	public function get() {
104
-		// Clear the buffer to be sure someone doesn't mess with our response.
105
-		//
106
-		// See https://github.com/insideout10/wordlift-plugin/issues/406.
107
-		// See https://codex.wordpress.org/AJAX_in_Plugins.
108
-		@ob_clean();
109
-
110
-		// Get the parameter from the request.
111
-		$is_homepage = isset( $_REQUEST['homepage'] );
112
-		$post_id     = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null;
113
-
114
-		// Send the generated JSON-LD.
115
-		$this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) );
116
-
117
-	}
118
-
119
-	/**
120
-	 * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type.
121
-	 *
122
-	 * @param mixed $response Variable (usually an array or object) to encode as JSON,
123
-	 *                           then print and die.
124
-	 * @param int $status_code The HTTP status code to output.
125
-	 *
126
-	 * @since 3.18.5
127
-	 *
128
-	 */
129
-	private function send_jsonld( $response, $status_code = null ) {
130
-		@header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) );
131
-		echo wp_json_encode( $response );
132
-		if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
133
-			wp_die();
134
-		} else {
135
-			die;
136
-		}
137
-	}
138
-
139
-	/**
140
-	 * Get the JSON-LD.
141
-	 *
142
-	 * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested.
143
-	 * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id.
144
-	 * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
145
-	 *
146
-	 * @return array A JSON-LD structure.
147
-	 * @since 3.15.1
148
-	 *
149
-	 */
150
-	public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) {
151
-
152
-		// Tell NewRelic to ignore us, otherwise NewRelic customers might receive
153
-		// e-mails with a low apdex score.
154
-		//
155
-		// See https://github.com/insideout10/wordlift-plugin/issues/521
156
-		Wordlift_NewRelic_Adapter::ignore_apdex();
157
-
158
-		// Switch to Website converter if is home page.
159
-		if ( $is_homepage ) {
160
-			/**
161
-			 * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output.
162
-			 *
163
-			 * @since  3.14.0
164
-			 * @api    bool $display_search Whether or not to display json+ld search on the frontend.
165
-			 */
166
-			if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) {
167
-				return array();
168
-			}
169
-
170
-			// Set a reference to the website_converter.
171
-			$website_converter = $this->website_converter;
172
-
173
-			// Send JSON-LD.
174
-			return $website_converter->create_schema();
175
-		}
176
-
177
-		// If no id has been provided return an empty array.
178
-		if ( ! isset( $post_id ) ) {
179
-			return array();
180
-		}
181
-
182
-		// An array of references which is captured when converting an URI to a
183
-		// json which we gather to further expand our json-ld.
184
-		$references       = array();
185
-		$references_infos = array();
186
-
187
-		// Set a reference to the entity_to_jsonld_converter to use in the closures.
188
-		$entity_to_jsonld_converter = $this->converter;
189
-
190
-		$jsonld = array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) );
191
-
192
-
193
-		$that = $this;
194
-		$expanded_references_jsonld = array_map( function ( $item ) use ( $context, $entity_to_jsonld_converter, &$references_infos, $that ) {
195
-			// "2nd level properties" may not output here, e.g. a post
196
-			// mentioning an event, located in a place: the place is referenced
197
-			// via the `@id` but no other properties are loaded.
198
-			$ignored = array();
199
-			if ( $item instanceof Term_Reference ) {
200
-			    $term_jsonld =  $that->term_jsonld_adapter->get( $item->get_id(), $context );
201
-			    // For term references, we publish a jsonld array on the page, use only the first item.
202
-			    return count( $term_jsonld ) > 0 ? $term_jsonld[0] : false;
203
-			}
204
-			else if ( $item instanceof  Post_Reference) {
205
-			    $item = $item->get_id();
20
+    /**
21
+     * A {@link Wordlift_Entity_Service} instance.
22
+     *
23
+     * @since  3.8.0
24
+     * @access private
25
+     * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
26
+     */
27
+    private $entity_service;
28
+
29
+    /**
30
+     * A {@link Wordlift_Term_JsonLd_Adapter} instance.
31
+     *
32
+     * @since  3.32.0
33
+     * @access private
34
+     * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance.
35
+     */
36
+    private $term_jsonld_adapter;
37
+
38
+    /**
39
+     * A {@link Wordlift_Post_Converter} instance.
40
+     *
41
+     * @since  3.8.0
42
+     * @access private
43
+     * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance.
44
+     */
45
+    private $converter;
46
+
47
+
48
+    /**
49
+     * A {@link Wordlift_Website_Jsonld_Converter} instance.
50
+     *
51
+     * @since  3.14.0
52
+     * @access private
53
+     * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance.
54
+     */
55
+    private $website_converter;
56
+
57
+    /**
58
+     * The singleton instance for the JSON-LD service.
59
+     *
60
+     * @since 3.15.1
61
+     *
62
+     * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service.
63
+     */
64
+    private static $instance;
65
+
66
+    /**
67
+     * Create a JSON-LD service.
68
+     *
69
+     * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
70
+     * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance.
71
+     * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance.
72
+     * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter
73
+     *
74
+     * @since 3.8.0
75
+     */
76
+    public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) {
77
+
78
+        $this->entity_service      = $entity_service;
79
+        $this->converter           = $converter;
80
+        $this->website_converter   = $website_converter;
81
+        $this->term_jsonld_adapter = $term_jsonld_adapter;
82
+        self::$instance            = $this;
83
+
84
+    }
85
+
86
+    /**
87
+     * Get the singleton instance for the JSON-LD service.
88
+     *
89
+     * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service.
90
+     * @since 3.15.1
91
+     *
92
+     */
93
+    public static function get_instance() {
94
+
95
+        return self::$instance;
96
+    }
97
+
98
+    /**
99
+     * Process calls to the AJAX 'wl_jsonld' endpoint.
100
+     *
101
+     * @since 3.8.0
102
+     */
103
+    public function get() {
104
+        // Clear the buffer to be sure someone doesn't mess with our response.
105
+        //
106
+        // See https://github.com/insideout10/wordlift-plugin/issues/406.
107
+        // See https://codex.wordpress.org/AJAX_in_Plugins.
108
+        @ob_clean();
109
+
110
+        // Get the parameter from the request.
111
+        $is_homepage = isset( $_REQUEST['homepage'] );
112
+        $post_id     = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null;
113
+
114
+        // Send the generated JSON-LD.
115
+        $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) );
116
+
117
+    }
118
+
119
+    /**
120
+     * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type.
121
+     *
122
+     * @param mixed $response Variable (usually an array or object) to encode as JSON,
123
+     *                           then print and die.
124
+     * @param int $status_code The HTTP status code to output.
125
+     *
126
+     * @since 3.18.5
127
+     *
128
+     */
129
+    private function send_jsonld( $response, $status_code = null ) {
130
+        @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) );
131
+        echo wp_json_encode( $response );
132
+        if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
133
+            wp_die();
134
+        } else {
135
+            die;
136
+        }
137
+    }
138
+
139
+    /**
140
+     * Get the JSON-LD.
141
+     *
142
+     * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested.
143
+     * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id.
144
+     * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum.
145
+     *
146
+     * @return array A JSON-LD structure.
147
+     * @since 3.15.1
148
+     *
149
+     */
150
+    public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) {
151
+
152
+        // Tell NewRelic to ignore us, otherwise NewRelic customers might receive
153
+        // e-mails with a low apdex score.
154
+        //
155
+        // See https://github.com/insideout10/wordlift-plugin/issues/521
156
+        Wordlift_NewRelic_Adapter::ignore_apdex();
157
+
158
+        // Switch to Website converter if is home page.
159
+        if ( $is_homepage ) {
160
+            /**
161
+             * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output.
162
+             *
163
+             * @since  3.14.0
164
+             * @api    bool $display_search Whether or not to display json+ld search on the frontend.
165
+             */
166
+            if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) {
167
+                return array();
206 168
             }
207 169
 
208
-			return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos );
209
-		}, array_unique( $references ) );
170
+            // Set a reference to the website_converter.
171
+            $website_converter = $this->website_converter;
210 172
 
173
+            // Send JSON-LD.
174
+            return $website_converter->create_schema();
175
+        }
211 176
 
212
-		// Convert each URI to a JSON-LD array, while gathering referenced entities.
213
-		// in the references array.
214
-		$jsonld = array_merge( $jsonld,
215
-			// Convert each URI in the references array to JSON-LD. We don't output
216
-			// entities already output above (hence the array_diff).
217
-			array_filter( $expanded_references_jsonld ) );
177
+        // If no id has been provided return an empty array.
178
+        if ( ! isset( $post_id ) ) {
179
+            return array();
180
+        }
218 181
 
219
-		$required_references = array_filter( $references_infos, function ( $item ) use ( $references ) {
182
+        // An array of references which is captured when converting an URI to a
183
+        // json which we gather to further expand our json-ld.
184
+        $references       = array();
185
+        $references_infos = array();
220 186
 
221
-			return isset( $item['reference'] ) &&
222
-			       // Check that the reference is required
223
-			       $item['reference']->get_required() &&
224
-			       // Check that the reference isn't being output already.
225
-			       ! in_array( $item['reference']->get_id(), $references );
226
-		} );
187
+        // Set a reference to the entity_to_jsonld_converter to use in the closures.
188
+        $entity_to_jsonld_converter = $this->converter;
227 189
 
190
+        $jsonld = array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) );
228 191
 
229
-		$jsonld = array_merge( $jsonld, array_filter( array_map( function ( $item ) use ( $references, $entity_to_jsonld_converter ) {
230 192
 
231
-			if ( ! isset( $item['reference'] ) ) {
232
-				return null;
233
-			}
193
+        $that = $this;
194
+        $expanded_references_jsonld = array_map( function ( $item ) use ( $context, $entity_to_jsonld_converter, &$references_infos, $that ) {
195
+            // "2nd level properties" may not output here, e.g. a post
196
+            // mentioning an event, located in a place: the place is referenced
197
+            // via the `@id` but no other properties are loaded.
198
+            $ignored = array();
199
+            if ( $item instanceof Term_Reference ) {
200
+                $term_jsonld =  $that->term_jsonld_adapter->get( $item->get_id(), $context );
201
+                // For term references, we publish a jsonld array on the page, use only the first item.
202
+                return count( $term_jsonld ) > 0 ? $term_jsonld[0] : false;
203
+            }
204
+            else if ( $item instanceof  Post_Reference) {
205
+                $item = $item->get_id();
206
+            }
207
+
208
+            return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos );
209
+        }, array_unique( $references ) );
234 210
 
235
-			$post_id = $item['reference']->get_id();
236
-			if ( in_array( $post_id, $references ) ) {
237
-				return null;
238
-			}
239 211
 
240
-			$references[] = $post_id;
212
+        // Convert each URI to a JSON-LD array, while gathering referenced entities.
213
+        // in the references array.
214
+        $jsonld = array_merge( $jsonld,
215
+            // Convert each URI in the references array to JSON-LD. We don't output
216
+            // entities already output above (hence the array_diff).
217
+            array_filter( $expanded_references_jsonld ) );
241 218
 
242
-			return $entity_to_jsonld_converter->convert( $post_id, $references );
243
-		}, $required_references ) ) );
219
+        $required_references = array_filter( $references_infos, function ( $item ) use ( $references ) {
244 220
 
245
-		/**
246
-		 * Filter name: wl_after_get_jsonld
247
-		 * @return array
248
-		 * @since 3.27.2
249
-		 * @var $jsonld array The final jsonld before outputting to page.
250
-		 * @var $post_id int The post id for which the jsonld is generated.
251
-		 *
252
-		 */
253
-		$jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id, $context );
221
+            return isset( $item['reference'] ) &&
222
+                   // Check that the reference is required
223
+                   $item['reference']->get_required() &&
224
+                   // Check that the reference isn't being output already.
225
+                   ! in_array( $item['reference']->get_id(), $references );
226
+        } );
254 227
 
255
-		return $jsonld;
256
-	}
257 228
 
258
-	/**
259
-	 * Write the JSON-LD in the head.
260
-	 *
261
-	 * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the
262
-	 * document head, using the `wp_head` hook.
263
-	 *
264
-	 * @since 3.18.5
265
-	 */
266
-	public function wp_head() {
229
+        $jsonld = array_merge( $jsonld, array_filter( array_map( function ( $item ) use ( $references, $entity_to_jsonld_converter ) {
267 230
 
268
-		// Determine whether this is the home page or whether we're displaying a single post.
269
-		$is_homepage = is_home() || is_front_page();
270
-		$post_id     = is_singular() ? get_the_ID() : null;
231
+            if ( ! isset( $item['reference'] ) ) {
232
+                return null;
233
+            }
234
+
235
+            $post_id = $item['reference']->get_id();
236
+            if ( in_array( $post_id, $references ) ) {
237
+                return null;
238
+            }
271 239
 
272
-		$jsonld = json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) );
273
-		?>
240
+            $references[] = $post_id;
241
+
242
+            return $entity_to_jsonld_converter->convert( $post_id, $references );
243
+        }, $required_references ) ) );
244
+
245
+        /**
246
+         * Filter name: wl_after_get_jsonld
247
+         * @return array
248
+         * @since 3.27.2
249
+         * @var $jsonld array The final jsonld before outputting to page.
250
+         * @var $post_id int The post id for which the jsonld is generated.
251
+         *
252
+         */
253
+        $jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id, $context );
254
+
255
+        return $jsonld;
256
+    }
257
+
258
+    /**
259
+     * Write the JSON-LD in the head.
260
+     *
261
+     * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the
262
+     * document head, using the `wp_head` hook.
263
+     *
264
+     * @since 3.18.5
265
+     */
266
+    public function wp_head() {
267
+
268
+        // Determine whether this is the home page or whether we're displaying a single post.
269
+        $is_homepage = is_home() || is_front_page();
270
+        $post_id     = is_singular() ? get_the_ID() : null;
271
+
272
+        $jsonld = json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) );
273
+        ?>
274 274
         <script type="application/ld+json"><?php echo $jsonld; ?></script><?php
275
-	}
275
+    }
276 276
 
277 277
 }
Please login to merge, or discard this patch.
Spacing   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -73,7 +73,7 @@  discard block
 block discarded – undo
73 73
 	 *
74 74
 	 * @since 3.8.0
75 75
 	 */
76
-	public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) {
76
+	public function __construct($entity_service, $converter, $website_converter, $term_jsonld_adapter) {
77 77
 
78 78
 		$this->entity_service      = $entity_service;
79 79
 		$this->converter           = $converter;
@@ -108,11 +108,11 @@  discard block
 block discarded – undo
108 108
 		@ob_clean();
109 109
 
110 110
 		// Get the parameter from the request.
111
-		$is_homepage = isset( $_REQUEST['homepage'] );
112
-		$post_id     = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null;
111
+		$is_homepage = isset($_REQUEST['homepage']);
112
+		$post_id     = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
113 113
 
114 114
 		// Send the generated JSON-LD.
115
-		$this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) );
115
+		$this->send_jsonld($this->get_jsonld($is_homepage, $post_id));
116 116
 
117 117
 	}
118 118
 
@@ -126,10 +126,10 @@  discard block
 block discarded – undo
126 126
 	 * @since 3.18.5
127 127
 	 *
128 128
 	 */
129
-	private function send_jsonld( $response, $status_code = null ) {
130
-		@header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) );
131
-		echo wp_json_encode( $response );
132
-		if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
129
+	private function send_jsonld($response, $status_code = null) {
130
+		@header('Content-Type: application/ld+json; charset='.get_option('blog_charset'));
131
+		echo wp_json_encode($response);
132
+		if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) {
133 133
 			wp_die();
134 134
 		} else {
135 135
 			die;
@@ -147,7 +147,7 @@  discard block
 block discarded – undo
147 147
 	 * @since 3.15.1
148 148
 	 *
149 149
 	 */
150
-	public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) {
150
+	public function get_jsonld($is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN) {
151 151
 
152 152
 		// Tell NewRelic to ignore us, otherwise NewRelic customers might receive
153 153
 		// e-mails with a low apdex score.
@@ -156,14 +156,14 @@  discard block
 block discarded – undo
156 156
 		Wordlift_NewRelic_Adapter::ignore_apdex();
157 157
 
158 158
 		// Switch to Website converter if is home page.
159
-		if ( $is_homepage ) {
159
+		if ($is_homepage) {
160 160
 			/**
161 161
 			 * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output.
162 162
 			 *
163 163
 			 * @since  3.14.0
164 164
 			 * @api    bool $display_search Whether or not to display json+ld search on the frontend.
165 165
 			 */
166
-			if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) {
166
+			if (apply_filters('wordlift_disable_website_json_ld', false)) {
167 167
 				return array();
168 168
 			}
169 169
 
@@ -175,7 +175,7 @@  discard block
 block discarded – undo
175 175
 		}
176 176
 
177 177
 		// If no id has been provided return an empty array.
178
-		if ( ! isset( $post_id ) ) {
178
+		if ( ! isset($post_id)) {
179 179
 			return array();
180 180
 		}
181 181
 
@@ -187,60 +187,60 @@  discard block
 block discarded – undo
187 187
 		// Set a reference to the entity_to_jsonld_converter to use in the closures.
188 188
 		$entity_to_jsonld_converter = $this->converter;
189 189
 
190
-		$jsonld = array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) );
190
+		$jsonld = array($entity_to_jsonld_converter->convert($post_id, $references, $references_infos));
191 191
 
192 192
 
193 193
 		$that = $this;
194
-		$expanded_references_jsonld = array_map( function ( $item ) use ( $context, $entity_to_jsonld_converter, &$references_infos, $that ) {
194
+		$expanded_references_jsonld = array_map(function($item) use ($context, $entity_to_jsonld_converter, &$references_infos, $that) {
195 195
 			// "2nd level properties" may not output here, e.g. a post
196 196
 			// mentioning an event, located in a place: the place is referenced
197 197
 			// via the `@id` but no other properties are loaded.
198 198
 			$ignored = array();
199
-			if ( $item instanceof Term_Reference ) {
200
-			    $term_jsonld =  $that->term_jsonld_adapter->get( $item->get_id(), $context );
199
+			if ($item instanceof Term_Reference) {
200
+			    $term_jsonld = $that->term_jsonld_adapter->get($item->get_id(), $context);
201 201
 			    // For term references, we publish a jsonld array on the page, use only the first item.
202
-			    return count( $term_jsonld ) > 0 ? $term_jsonld[0] : false;
202
+			    return count($term_jsonld) > 0 ? $term_jsonld[0] : false;
203 203
 			}
204
-			else if ( $item instanceof  Post_Reference) {
204
+			else if ($item instanceof  Post_Reference) {
205 205
 			    $item = $item->get_id();
206 206
             }
207 207
 
208
-			return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos );
209
-		}, array_unique( $references ) );
208
+			return $entity_to_jsonld_converter->convert($item, $ignored, $references_infos);
209
+		}, array_unique($references));
210 210
 
211 211
 
212 212
 		// Convert each URI to a JSON-LD array, while gathering referenced entities.
213 213
 		// in the references array.
214
-		$jsonld = array_merge( $jsonld,
214
+		$jsonld = array_merge($jsonld,
215 215
 			// Convert each URI in the references array to JSON-LD. We don't output
216 216
 			// entities already output above (hence the array_diff).
217
-			array_filter( $expanded_references_jsonld ) );
217
+			array_filter($expanded_references_jsonld));
218 218
 
219
-		$required_references = array_filter( $references_infos, function ( $item ) use ( $references ) {
219
+		$required_references = array_filter($references_infos, function($item) use ($references) {
220 220
 
221
-			return isset( $item['reference'] ) &&
221
+			return isset($item['reference']) &&
222 222
 			       // Check that the reference is required
223 223
 			       $item['reference']->get_required() &&
224 224
 			       // Check that the reference isn't being output already.
225
-			       ! in_array( $item['reference']->get_id(), $references );
225
+			       ! in_array($item['reference']->get_id(), $references);
226 226
 		} );
227 227
 
228 228
 
229
-		$jsonld = array_merge( $jsonld, array_filter( array_map( function ( $item ) use ( $references, $entity_to_jsonld_converter ) {
229
+		$jsonld = array_merge($jsonld, array_filter(array_map(function($item) use ($references, $entity_to_jsonld_converter) {
230 230
 
231
-			if ( ! isset( $item['reference'] ) ) {
231
+			if ( ! isset($item['reference'])) {
232 232
 				return null;
233 233
 			}
234 234
 
235 235
 			$post_id = $item['reference']->get_id();
236
-			if ( in_array( $post_id, $references ) ) {
236
+			if (in_array($post_id, $references)) {
237 237
 				return null;
238 238
 			}
239 239
 
240 240
 			$references[] = $post_id;
241 241
 
242
-			return $entity_to_jsonld_converter->convert( $post_id, $references );
243
-		}, $required_references ) ) );
242
+			return $entity_to_jsonld_converter->convert($post_id, $references);
243
+		}, $required_references)));
244 244
 
245 245
 		/**
246 246
 		 * Filter name: wl_after_get_jsonld
@@ -250,7 +250,7 @@  discard block
 block discarded – undo
250 250
 		 * @var $post_id int The post id for which the jsonld is generated.
251 251
 		 *
252 252
 		 */
253
-		$jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id, $context );
253
+		$jsonld = apply_filters('wl_after_get_jsonld', $jsonld, $post_id, $context);
254 254
 
255 255
 		return $jsonld;
256 256
 	}
@@ -269,7 +269,7 @@  discard block
 block discarded – undo
269 269
 		$is_homepage = is_home() || is_front_page();
270 270
 		$post_id     = is_singular() ? get_the_ID() : null;
271 271
 
272
-		$jsonld = json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) );
272
+		$jsonld = json_encode($this->get_jsonld($is_homepage, $post_id, Jsonld_Context_Enum::PAGE));
273 273
 		?>
274 274
         <script type="application/ld+json"><?php echo $jsonld; ?></script><?php
275 275
 	}
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -200,8 +200,7 @@
 block discarded – undo
200 200
 			    $term_jsonld =  $that->term_jsonld_adapter->get( $item->get_id(), $context );
201 201
 			    // For term references, we publish a jsonld array on the page, use only the first item.
202 202
 			    return count( $term_jsonld ) > 0 ? $term_jsonld[0] : false;
203
-			}
204
-			else if ( $item instanceof  Post_Reference) {
203
+			} else if ( $item instanceof  Post_Reference) {
205 204
 			    $item = $item->get_id();
206 205
             }
207 206
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-property-service.php 2 patches
Indentation   +114 added lines, -114 removed lines patch added patch discarded remove patch
@@ -14,119 +14,119 @@
 block discarded – undo
14 14
  */
15 15
 abstract class Wordlift_Property_Service {
16 16
 
17
-	// TODO: check that this is relative to the extending class.
18
-	protected static $instance;
19
-
20
-	public function __construct() {
21
-
22
-		static::$instance = $this;
23
-	}
24
-
25
-	/**
26
-	 * Get the field singleton.
27
-	 *
28
-	 * @since 3.6.0
29
-	 * @return \Wordlift_Schema_Url_Property_Service The singleton instance.
30
-	 */
31
-	public static function get_instance() {
32
-
33
-		return static::$instance;
34
-	}
35
-
36
-	/**
37
-	 * Get the value for the specified post/entity.
38
-	 *
39
-	 * @since 3.6.0
40
-	 *
41
-	 * @param int $post_id The post id.
42
-	 *
43
-	 * @return mixed
44
-	 */
45
-	public abstract function get( $post_id );
46
-
47
-	/**
48
-	 * Sanitize the provided value.
49
-	 *
50
-	 * @since 3.6.0
51
-	 *
52
-	 * @param mixed $value The value to sanitize.
53
-	 *
54
-	 * @return mixed|NULL The sanitized value or NULL avoid saving this value (see {@link Wl_Metabox_Field}).
55
-	 */
56
-	public abstract function sanitize( $value );
57
-
58
-	/**
59
-	 * The RDF predicate for the property.
60
-	 *
61
-	 * @since 3.6.0
62
-	 * @return string The RDF predicate.
63
-	 */
64
-	public abstract function get_rdf_predicate();
65
-
66
-	/**
67
-	 * The RDF data type.
68
-	 *
69
-	 * @since 3.6.0
70
-	 * @return string The RDF data type.
71
-	 */
72
-	public abstract function get_rdf_data_type();
73
-
74
-	/**
75
-	 * The internal data type.
76
-	 *
77
-	 * @since 3.6.0
78
-	 * @return string The internal data type.
79
-	 */
80
-	public abstract function get_data_type();
81
-
82
-	/**
83
-	 * The cardinality.
84
-	 *
85
-	 * @since 3.6.0
86
-	 * @return mixed The cardinality.
87
-	 */
88
-	public abstract function get_cardinality();
89
-
90
-	/**
91
-	 * The metabox field class name.
92
-	 *
93
-	 * @since 3.6.0
94
-	 * @return string The metabox field class name.
95
-	 */
96
-	public abstract function get_metabox_class();
97
-
98
-	/**
99
-	 * The untranslated metabox field label.
100
-	 *
101
-	 * @since 3.6.0
102
-	 * @return string The untranslated metabox field label.
103
-	 */
104
-	public abstract function get_metabox_label();
105
-
106
-	/**
107
-	 * The definition of the property returned as a compatible array.
108
-	 *
109
-	 * @deprecated
110
-	 *
111
-	 * @since 3.6.0
112
-	 * @return array An array of property definitions.
113
-	 */
114
-	public function get_compat_definition() {
115
-
116
-		return array(
117
-			'type'        => $this->get_data_type(),
118
-			'predicate'   => $this->get_rdf_predicate(),
119
-			'export_type' => $this->get_rdf_data_type(),
120
-			'constraints' => array(
121
-				'cardinality' => $this->get_cardinality(),
122
-			),
123
-			// Use the standard metabox for these URI (the URI metabox creates local entities).
124
-			'metabox'     => array(
125
-				'class' => $this->get_metabox_class(),
126
-				'label' => $this->get_metabox_label(),
127
-			),
128
-			'sanitize'    => array( $this, 'sanitize' ),
129
-		);
130
-	}
17
+    // TODO: check that this is relative to the extending class.
18
+    protected static $instance;
19
+
20
+    public function __construct() {
21
+
22
+        static::$instance = $this;
23
+    }
24
+
25
+    /**
26
+     * Get the field singleton.
27
+     *
28
+     * @since 3.6.0
29
+     * @return \Wordlift_Schema_Url_Property_Service The singleton instance.
30
+     */
31
+    public static function get_instance() {
32
+
33
+        return static::$instance;
34
+    }
35
+
36
+    /**
37
+     * Get the value for the specified post/entity.
38
+     *
39
+     * @since 3.6.0
40
+     *
41
+     * @param int $post_id The post id.
42
+     *
43
+     * @return mixed
44
+     */
45
+    public abstract function get( $post_id );
46
+
47
+    /**
48
+     * Sanitize the provided value.
49
+     *
50
+     * @since 3.6.0
51
+     *
52
+     * @param mixed $value The value to sanitize.
53
+     *
54
+     * @return mixed|NULL The sanitized value or NULL avoid saving this value (see {@link Wl_Metabox_Field}).
55
+     */
56
+    public abstract function sanitize( $value );
57
+
58
+    /**
59
+     * The RDF predicate for the property.
60
+     *
61
+     * @since 3.6.0
62
+     * @return string The RDF predicate.
63
+     */
64
+    public abstract function get_rdf_predicate();
65
+
66
+    /**
67
+     * The RDF data type.
68
+     *
69
+     * @since 3.6.0
70
+     * @return string The RDF data type.
71
+     */
72
+    public abstract function get_rdf_data_type();
73
+
74
+    /**
75
+     * The internal data type.
76
+     *
77
+     * @since 3.6.0
78
+     * @return string The internal data type.
79
+     */
80
+    public abstract function get_data_type();
81
+
82
+    /**
83
+     * The cardinality.
84
+     *
85
+     * @since 3.6.0
86
+     * @return mixed The cardinality.
87
+     */
88
+    public abstract function get_cardinality();
89
+
90
+    /**
91
+     * The metabox field class name.
92
+     *
93
+     * @since 3.6.0
94
+     * @return string The metabox field class name.
95
+     */
96
+    public abstract function get_metabox_class();
97
+
98
+    /**
99
+     * The untranslated metabox field label.
100
+     *
101
+     * @since 3.6.0
102
+     * @return string The untranslated metabox field label.
103
+     */
104
+    public abstract function get_metabox_label();
105
+
106
+    /**
107
+     * The definition of the property returned as a compatible array.
108
+     *
109
+     * @deprecated
110
+     *
111
+     * @since 3.6.0
112
+     * @return array An array of property definitions.
113
+     */
114
+    public function get_compat_definition() {
115
+
116
+        return array(
117
+            'type'        => $this->get_data_type(),
118
+            'predicate'   => $this->get_rdf_predicate(),
119
+            'export_type' => $this->get_rdf_data_type(),
120
+            'constraints' => array(
121
+                'cardinality' => $this->get_cardinality(),
122
+            ),
123
+            // Use the standard metabox for these URI (the URI metabox creates local entities).
124
+            'metabox'     => array(
125
+                'class' => $this->get_metabox_class(),
126
+                'label' => $this->get_metabox_label(),
127
+            ),
128
+            'sanitize'    => array( $this, 'sanitize' ),
129
+        );
130
+    }
131 131
 
132 132
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
 	 *
43 43
 	 * @return mixed
44 44
 	 */
45
-	public abstract function get( $post_id );
45
+	public abstract function get($post_id);
46 46
 
47 47
 	/**
48 48
 	 * Sanitize the provided value.
@@ -53,7 +53,7 @@  discard block
 block discarded – undo
53 53
 	 *
54 54
 	 * @return mixed|NULL The sanitized value or NULL avoid saving this value (see {@link Wl_Metabox_Field}).
55 55
 	 */
56
-	public abstract function sanitize( $value );
56
+	public abstract function sanitize($value);
57 57
 
58 58
 	/**
59 59
 	 * The RDF predicate for the property.
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
 				'class' => $this->get_metabox_class(),
126 126
 				'label' => $this->get_metabox_label(),
127 127
 			),
128
-			'sanitize'    => array( $this, 'sanitize' ),
128
+			'sanitize'    => array($this, 'sanitize'),
129 129
 		);
130 130
 	}
131 131
 
Please login to merge, or discard this patch.
src/includes/linked-data/storage/class-wordlift-url-property-storage.php 2 patches
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -18,42 +18,42 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Url_Property_Storage extends Wordlift_Storage {
20 20
 
21
-	/**
22
-	 * The {@link Wordlift_Property_Getter} instance.
23
-	 *
24
-	 * @since  3.15.0
25
-	 * @access private
26
-	 * @var \Wordlift_Property_Getter The {@link Wordlift_Property_Getter}
27
-	 *                                     instance.
28
-	 */
29
-	private $property_getter;
30
-
31
-	/**
32
-	 * Create a {@link Wordlift_Property_Storage} instance.
33
-	 *
34
-	 * @since 3.15.0
35
-	 *
36
-	 * @param \Wordlift_Property_Getter $property_getter The {@link Wordlift_Property_Getter}
37
-	 *                                                   instance.
38
-	 */
39
-	public function __construct( $property_getter ) {
40
-
41
-		$this->property_getter = $property_getter;
42
-
43
-	}
44
-
45
-	/**
46
-	 * Get the values for the property of the {@link WP_Post}.
47
-	 *
48
-	 * @since 3.15.0
49
-	 *
50
-	 * @param int $post_id The {@link WP_Post}'s id.
51
-	 *
52
-	 * @return array
53
-	 */
54
-	public function get( $post_id ) {
55
-
56
-		return $this->property_getter->get( $post_id, Wordlift_Schema_Url_Property_Service::META_KEY, Object_Type_Enum::POST );
57
-	}
21
+    /**
22
+     * The {@link Wordlift_Property_Getter} instance.
23
+     *
24
+     * @since  3.15.0
25
+     * @access private
26
+     * @var \Wordlift_Property_Getter The {@link Wordlift_Property_Getter}
27
+     *                                     instance.
28
+     */
29
+    private $property_getter;
30
+
31
+    /**
32
+     * Create a {@link Wordlift_Property_Storage} instance.
33
+     *
34
+     * @since 3.15.0
35
+     *
36
+     * @param \Wordlift_Property_Getter $property_getter The {@link Wordlift_Property_Getter}
37
+     *                                                   instance.
38
+     */
39
+    public function __construct( $property_getter ) {
40
+
41
+        $this->property_getter = $property_getter;
42
+
43
+    }
44
+
45
+    /**
46
+     * Get the values for the property of the {@link WP_Post}.
47
+     *
48
+     * @since 3.15.0
49
+     *
50
+     * @param int $post_id The {@link WP_Post}'s id.
51
+     *
52
+     * @return array
53
+     */
54
+    public function get( $post_id ) {
55
+
56
+        return $this->property_getter->get( $post_id, Wordlift_Schema_Url_Property_Service::META_KEY, Object_Type_Enum::POST );
57
+    }
58 58
 
59 59
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
 	 * @param \Wordlift_Property_Getter $property_getter The {@link Wordlift_Property_Getter}
37 37
 	 *                                                   instance.
38 38
 	 */
39
-	public function __construct( $property_getter ) {
39
+	public function __construct($property_getter) {
40 40
 
41 41
 		$this->property_getter = $property_getter;
42 42
 
@@ -51,9 +51,9 @@  discard block
 block discarded – undo
51 51
 	 *
52 52
 	 * @return array
53 53
 	 */
54
-	public function get( $post_id ) {
54
+	public function get($post_id) {
55 55
 
56
-		return $this->property_getter->get( $post_id, Wordlift_Schema_Url_Property_Service::META_KEY, Object_Type_Enum::POST );
56
+		return $this->property_getter->get($post_id, Wordlift_Schema_Url_Property_Service::META_KEY, Object_Type_Enum::POST);
57 57
 	}
58 58
 
59 59
 }
Please login to merge, or discard this patch.
src/includes/properties/class-wordlift-simple-property-service.php 3 patches
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -13,35 +13,35 @@
 block discarded – undo
13 13
  */
14 14
 class Wordlift_Simple_Property_Service {
15 15
 
16
-	/**
17
-	 * The meta key for this property service.
18
-	 *
19
-	 * @since 3.8.0
20
-	 */
21
-	const META_KEY = '*';
16
+    /**
17
+     * The meta key for this property service.
18
+     *
19
+     * @since 3.8.0
20
+     */
21
+    const META_KEY = '*';
22 22
 
23
-	/**
24
-	 * Get the property value for the specified post id and meta with the specified key.
25
-	 *
26
-	 * @param int $id The post id.
27
-	 * @param string $meta_key The meta key.
28
-	 *
29
-	 * @param $type int Post or Term
30
-	 *
31
-	 * @return mixed|null The property value.
32
-	 * @since 3.8.0
33
-	 *
34
-	 */
35
-	public function get( $id, $meta_key, $type ) {
23
+    /**
24
+     * Get the property value for the specified post id and meta with the specified key.
25
+     *
26
+     * @param int $id The post id.
27
+     * @param string $meta_key The meta key.
28
+     *
29
+     * @param $type int Post or Term
30
+     *
31
+     * @return mixed|null The property value.
32
+     * @since 3.8.0
33
+     *
34
+     */
35
+    public function get( $id, $meta_key, $type ) {
36 36
 
37
-		if ( Object_Type_Enum::POST === $type ) {
38
-			// Get the value stored in WP.
39
-			return get_post_meta( $id, $meta_key );
40
-		}
41
-		else if ( Object_Type_Enum::TERM === $type ) {
42
-			return get_term_meta( $id, $meta_key );
43
-		}
44
-		return null;
45
-	}
37
+        if ( Object_Type_Enum::POST === $type ) {
38
+            // Get the value stored in WP.
39
+            return get_post_meta( $id, $meta_key );
40
+        }
41
+        else if ( Object_Type_Enum::TERM === $type ) {
42
+            return get_term_meta( $id, $meta_key );
43
+        }
44
+        return null;
45
+    }
46 46
 
47 47
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -3,8 +3,8 @@  discard block
 block discarded – undo
3 3
 // Require the Wordlift_Property_Not_Found class.
4 4
 use Wordlift\Object_Type_Enum;
5 5
 
6
-require_once( "class-wordlift-property-not-found.php" );
7
-require_once( "class-wordlift-property-entity-reference.php" );
6
+require_once("class-wordlift-property-not-found.php");
7
+require_once("class-wordlift-property-entity-reference.php");
8 8
 
9 9
 /**
10 10
  * A property service that just returns the value stored in WP's meta.
@@ -32,14 +32,14 @@  discard block
 block discarded – undo
32 32
 	 * @since 3.8.0
33 33
 	 *
34 34
 	 */
35
-	public function get( $id, $meta_key, $type ) {
35
+	public function get($id, $meta_key, $type) {
36 36
 
37
-		if ( Object_Type_Enum::POST === $type ) {
37
+		if (Object_Type_Enum::POST === $type) {
38 38
 			// Get the value stored in WP.
39
-			return get_post_meta( $id, $meta_key );
39
+			return get_post_meta($id, $meta_key);
40 40
 		}
41
-		else if ( Object_Type_Enum::TERM === $type ) {
42
-			return get_term_meta( $id, $meta_key );
41
+		else if (Object_Type_Enum::TERM === $type) {
42
+			return get_term_meta($id, $meta_key);
43 43
 		}
44 44
 		return null;
45 45
 	}
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -37,8 +37,7 @@
 block discarded – undo
37 37
 		if ( Object_Type_Enum::POST === $type ) {
38 38
 			// Get the value stored in WP.
39 39
 			return get_post_meta( $id, $meta_key );
40
-		}
41
-		else if ( Object_Type_Enum::TERM === $type ) {
40
+		} else if ( Object_Type_Enum::TERM === $type ) {
42 41
 			return get_term_meta( $id, $meta_key );
43 42
 		}
44 43
 		return null;
Please login to merge, or discard this patch.
src/includes/properties/class-wordlift-required-property-service.php 2 patches
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -6,20 +6,20 @@
 block discarded – undo
6 6
  */
7 7
 class Wordlift_Required_Property_Service extends Wordlift_Entity_Property_Service {
8 8
 
9
-	/**
10
-	 * {@inheritdoc}
11
-	 */
12
-	public function get( $id, $meta_key, $type ) {
9
+    /**
10
+     * {@inheritdoc}
11
+     */
12
+    public function get( $id, $meta_key, $type ) {
13 13
 
14
-		return array_map( function ( $item ) {
14
+        return array_map( function ( $item ) {
15 15
 
16
-			// If this is an entity reference, set that this entity is always required in SD output.
17
-			if ( $item instanceof Wordlift_Property_Entity_Reference ) {
18
-				$item->set_required( true );
19
-			}
16
+            // If this is an entity reference, set that this entity is always required in SD output.
17
+            if ( $item instanceof Wordlift_Property_Entity_Reference ) {
18
+                $item->set_required( true );
19
+            }
20 20
 
21
-			return $item;
22
-		}, parent::get( $id, $meta_key, $type ) );
23
-	}
21
+            return $item;
22
+        }, parent::get( $id, $meta_key, $type ) );
23
+    }
24 24
 
25 25
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -9,17 +9,17 @@
 block discarded – undo
9 9
 	/**
10 10
 	 * {@inheritdoc}
11 11
 	 */
12
-	public function get( $id, $meta_key, $type ) {
12
+	public function get($id, $meta_key, $type) {
13 13
 
14
-		return array_map( function ( $item ) {
14
+		return array_map(function($item) {
15 15
 
16 16
 			// If this is an entity reference, set that this entity is always required in SD output.
17
-			if ( $item instanceof Wordlift_Property_Entity_Reference ) {
18
-				$item->set_required( true );
17
+			if ($item instanceof Wordlift_Property_Entity_Reference) {
18
+				$item->set_required(true);
19 19
 			}
20 20
 
21 21
 			return $item;
22
-		}, parent::get( $id, $meta_key, $type ) );
22
+		}, parent::get($id, $meta_key, $type));
23 23
 	}
24 24
 
25 25
 }
Please login to merge, or discard this patch.