Completed
Push — develop ( a884d9...830457 )
by David
09:30
created
src/includes/class-wordlift-jsonld-service.php 2 patches
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -13,158 +13,158 @@
 block discarded – undo
13 13
  */
14 14
 class Wordlift_Jsonld_Service {
15 15
 
16
-	/**
17
-	 * A {@link Wordlift_Entity_Service} instance.
18
-	 *
19
-	 * @since  3.8.0
20
-	 * @access private
21
-	 * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
22
-	 */
23
-	private $entity_service;
24
-
25
-	/**
26
-	 * A {@link Wordlift_Post_Converter} instance.
27
-	 *
28
-	 * @since  3.8.0
29
-	 * @access private
30
-	 * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance.
31
-	 */
32
-	private $converter;
33
-
34
-
35
-	/**
36
-	 * A {@link Wordlift_Website_Jsonld_Converter} instance.
37
-	 *
38
-	 * @since  3.14.0
39
-	 * @access private
40
-	 * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance.
41
-	 */
42
-	private $website_converter;
43
-
44
-	/**
45
-	 * The singleton instance for the JSON-LD service.
46
-	 *
47
-	 * @since 3.15.1
48
-	 *
49
-	 * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service.
50
-	 */
51
-	private static $instance;
52
-
53
-	/**
54
-	 * Create a JSON-LD service.
55
-	 *
56
-	 * @since 3.8.0
57
-	 *
58
-	 * @param \Wordlift_Entity_Service           $entity_service    A {@link Wordlift_Entity_Service} instance.
59
-	 * @param \Wordlift_Post_Converter           $converter         A {@link Wordlift_Uri_To_Jsonld_Converter} instance.
60
-	 * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance.
61
-	 */
62
-	public function __construct( $entity_service, $converter, $website_converter ) {
63
-
64
-		$this->entity_service    = $entity_service;
65
-		$this->converter         = $converter;
66
-		$this->website_converter = $website_converter;
67
-
68
-		self::$instance = $this;
69
-
70
-	}
71
-
72
-	/**
73
-	 * Get the singleton instance for the JSON-LD service.
74
-	 *
75
-	 * @since 3.15.1
76
-	 *
77
-	 * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service.
78
-	 */
79
-	public static function get_instance() {
80
-
81
-		return self::$instance;
82
-	}
83
-
84
-	/**
85
-	 * Process calls to the AJAX 'wl_jsonld' endpoint.
86
-	 *
87
-	 * @since 3.8.0
88
-	 */
89
-	public function get() {
90
-		// Clear the buffer to be sure someone doesn't mess with our response.
91
-		//
92
-		// See https://github.com/insideout10/wordlift-plugin/issues/406.
93
-		// See https://codex.wordpress.org/AJAX_in_Plugins.
94
-		@ob_clean();
95
-
96
-		// Get the parameter from the request.
97
-		$is_homepage = isset( $_REQUEST['homepage'] );
98
-		$post_id     = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null;
99
-
100
-		// Send the generated JSON-LD.
101
-		wp_send_json( $this->get_jsonld( $is_homepage, $post_id ) );
102
-
103
-	}
104
-
105
-	/**
106
-	 * Get the JSON-LD.
107
-	 *
108
-	 * @since 3.15.1
109
-	 *
110
-	 * @param bool     $is_homepage Whether the JSON-LD for the homepage is being requested.
111
-	 * @param int|null $post_id     The JSON-LD for the specified {@link WP_Post} id.
112
-	 *
113
-	 * @return array A JSON-LD structure.
114
-	 */
115
-	public function get_jsonld( $is_homepage = false, $post_id = null ) {
116
-
117
-		// Tell NewRelic to ignore us, otherwise NewRelic customers might receive
118
-		// e-mails with a low apdex score.
119
-		//
120
-		// See https://github.com/insideout10/wordlift-plugin/issues/521
121
-		Wordlift_NewRelic_Adapter::ignore_apdex();
122
-
123
-		// Switch to Website converter if is home page.
124
-		if ( $is_homepage ) {
125
-			/**
126
-			 * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output.
127
-			 *
128
-			 * @since  3.14.0
129
-			 * @api    bool $display_search Whether or not to display json+ld search on the frontend.
130
-			 */
131
-			if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) {
132
-				// Set a reference to the website_converter.
133
-				$website_converter = $this->website_converter;
134
-
135
-				// Send JSON-LD.
136
-				return $website_converter->create_schema();
137
-			}
138
-		}
139
-
140
-		// If no id has been provided return an empty array.
141
-		if ( ! isset( $post_id ) ) {
142
-			return array();
143
-		}
144
-
145
-		// An array of references which is captured when converting an URI to a
146
-		// json which we gather to further expand our json-ld.
147
-		$references = array();
148
-
149
-		// Set a reference to the entity_to_jsonld_converter to use in the closures.
150
-		$entity_to_jsonld_converter = $this->converter;
151
-
152
-		// Convert each URI to a JSON-LD array, while gathering referenced entities.
153
-		// in the references array.
154
-		$jsonld = array_merge(
155
-			array( $entity_to_jsonld_converter->convert( $post_id, $references ) ),
156
-			// Convert each URI in the references array to JSON-LD. We don't output
157
-			// entities already output above (hence the array_diff).
158
-			array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) {
159
-
160
-				// "2nd level properties" may not output here, e.g. a post
161
-				// mentioning an event, located in a place: the place is referenced
162
-				// via the `@id` but no other properties are loaded.
163
-				return $entity_to_jsonld_converter->convert( $item, $references );
164
-			}, $references ) ) );
165
-
166
-		// Finally send the JSON-LD.
167
-		return $jsonld;
168
-	}
16
+    /**
17
+     * A {@link Wordlift_Entity_Service} instance.
18
+     *
19
+     * @since  3.8.0
20
+     * @access private
21
+     * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
22
+     */
23
+    private $entity_service;
24
+
25
+    /**
26
+     * A {@link Wordlift_Post_Converter} instance.
27
+     *
28
+     * @since  3.8.0
29
+     * @access private
30
+     * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance.
31
+     */
32
+    private $converter;
33
+
34
+
35
+    /**
36
+     * A {@link Wordlift_Website_Jsonld_Converter} instance.
37
+     *
38
+     * @since  3.14.0
39
+     * @access private
40
+     * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance.
41
+     */
42
+    private $website_converter;
43
+
44
+    /**
45
+     * The singleton instance for the JSON-LD service.
46
+     *
47
+     * @since 3.15.1
48
+     *
49
+     * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service.
50
+     */
51
+    private static $instance;
52
+
53
+    /**
54
+     * Create a JSON-LD service.
55
+     *
56
+     * @since 3.8.0
57
+     *
58
+     * @param \Wordlift_Entity_Service           $entity_service    A {@link Wordlift_Entity_Service} instance.
59
+     * @param \Wordlift_Post_Converter           $converter         A {@link Wordlift_Uri_To_Jsonld_Converter} instance.
60
+     * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance.
61
+     */
62
+    public function __construct( $entity_service, $converter, $website_converter ) {
63
+
64
+        $this->entity_service    = $entity_service;
65
+        $this->converter         = $converter;
66
+        $this->website_converter = $website_converter;
67
+
68
+        self::$instance = $this;
69
+
70
+    }
71
+
72
+    /**
73
+     * Get the singleton instance for the JSON-LD service.
74
+     *
75
+     * @since 3.15.1
76
+     *
77
+     * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service.
78
+     */
79
+    public static function get_instance() {
80
+
81
+        return self::$instance;
82
+    }
83
+
84
+    /**
85
+     * Process calls to the AJAX 'wl_jsonld' endpoint.
86
+     *
87
+     * @since 3.8.0
88
+     */
89
+    public function get() {
90
+        // Clear the buffer to be sure someone doesn't mess with our response.
91
+        //
92
+        // See https://github.com/insideout10/wordlift-plugin/issues/406.
93
+        // See https://codex.wordpress.org/AJAX_in_Plugins.
94
+        @ob_clean();
95
+
96
+        // Get the parameter from the request.
97
+        $is_homepage = isset( $_REQUEST['homepage'] );
98
+        $post_id     = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null;
99
+
100
+        // Send the generated JSON-LD.
101
+        wp_send_json( $this->get_jsonld( $is_homepage, $post_id ) );
102
+
103
+    }
104
+
105
+    /**
106
+     * Get the JSON-LD.
107
+     *
108
+     * @since 3.15.1
109
+     *
110
+     * @param bool     $is_homepage Whether the JSON-LD for the homepage is being requested.
111
+     * @param int|null $post_id     The JSON-LD for the specified {@link WP_Post} id.
112
+     *
113
+     * @return array A JSON-LD structure.
114
+     */
115
+    public function get_jsonld( $is_homepage = false, $post_id = null ) {
116
+
117
+        // Tell NewRelic to ignore us, otherwise NewRelic customers might receive
118
+        // e-mails with a low apdex score.
119
+        //
120
+        // See https://github.com/insideout10/wordlift-plugin/issues/521
121
+        Wordlift_NewRelic_Adapter::ignore_apdex();
122
+
123
+        // Switch to Website converter if is home page.
124
+        if ( $is_homepage ) {
125
+            /**
126
+             * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output.
127
+             *
128
+             * @since  3.14.0
129
+             * @api    bool $display_search Whether or not to display json+ld search on the frontend.
130
+             */
131
+            if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) {
132
+                // Set a reference to the website_converter.
133
+                $website_converter = $this->website_converter;
134
+
135
+                // Send JSON-LD.
136
+                return $website_converter->create_schema();
137
+            }
138
+        }
139
+
140
+        // If no id has been provided return an empty array.
141
+        if ( ! isset( $post_id ) ) {
142
+            return array();
143
+        }
144
+
145
+        // An array of references which is captured when converting an URI to a
146
+        // json which we gather to further expand our json-ld.
147
+        $references = array();
148
+
149
+        // Set a reference to the entity_to_jsonld_converter to use in the closures.
150
+        $entity_to_jsonld_converter = $this->converter;
151
+
152
+        // Convert each URI to a JSON-LD array, while gathering referenced entities.
153
+        // in the references array.
154
+        $jsonld = array_merge(
155
+            array( $entity_to_jsonld_converter->convert( $post_id, $references ) ),
156
+            // Convert each URI in the references array to JSON-LD. We don't output
157
+            // entities already output above (hence the array_diff).
158
+            array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) {
159
+
160
+                // "2nd level properties" may not output here, e.g. a post
161
+                // mentioning an event, located in a place: the place is referenced
162
+                // via the `@id` but no other properties are loaded.
163
+                return $entity_to_jsonld_converter->convert( $item, $references );
164
+            }, $references ) ) );
165
+
166
+        // Finally send the JSON-LD.
167
+        return $jsonld;
168
+    }
169 169
 
170 170
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
 	 * @param \Wordlift_Post_Converter           $converter         A {@link Wordlift_Uri_To_Jsonld_Converter} instance.
60 60
 	 * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance.
61 61
 	 */
62
-	public function __construct( $entity_service, $converter, $website_converter ) {
62
+	public function __construct($entity_service, $converter, $website_converter) {
63 63
 
64 64
 		$this->entity_service    = $entity_service;
65 65
 		$this->converter         = $converter;
@@ -94,11 +94,11 @@  discard block
 block discarded – undo
94 94
 		@ob_clean();
95 95
 
96 96
 		// Get the parameter from the request.
97
-		$is_homepage = isset( $_REQUEST['homepage'] );
98
-		$post_id     = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null;
97
+		$is_homepage = isset($_REQUEST['homepage']);
98
+		$post_id     = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
99 99
 
100 100
 		// Send the generated JSON-LD.
101
-		wp_send_json( $this->get_jsonld( $is_homepage, $post_id ) );
101
+		wp_send_json($this->get_jsonld($is_homepage, $post_id));
102 102
 
103 103
 	}
104 104
 
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
 	 *
113 113
 	 * @return array A JSON-LD structure.
114 114
 	 */
115
-	public function get_jsonld( $is_homepage = false, $post_id = null ) {
115
+	public function get_jsonld($is_homepage = false, $post_id = null) {
116 116
 
117 117
 		// Tell NewRelic to ignore us, otherwise NewRelic customers might receive
118 118
 		// e-mails with a low apdex score.
@@ -121,14 +121,14 @@  discard block
 block discarded – undo
121 121
 		Wordlift_NewRelic_Adapter::ignore_apdex();
122 122
 
123 123
 		// Switch to Website converter if is home page.
124
-		if ( $is_homepage ) {
124
+		if ($is_homepage) {
125 125
 			/**
126 126
 			 * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output.
127 127
 			 *
128 128
 			 * @since  3.14.0
129 129
 			 * @api    bool $display_search Whether or not to display json+ld search on the frontend.
130 130
 			 */
131
-			if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) {
131
+			if ( ! apply_filters('wordlift_disable_website_json_ld', false)) {
132 132
 				// Set a reference to the website_converter.
133 133
 				$website_converter = $this->website_converter;
134 134
 
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
 		}
139 139
 
140 140
 		// If no id has been provided return an empty array.
141
-		if ( ! isset( $post_id ) ) {
141
+		if ( ! isset($post_id)) {
142 142
 			return array();
143 143
 		}
144 144
 
@@ -152,16 +152,16 @@  discard block
 block discarded – undo
152 152
 		// Convert each URI to a JSON-LD array, while gathering referenced entities.
153 153
 		// in the references array.
154 154
 		$jsonld = array_merge(
155
-			array( $entity_to_jsonld_converter->convert( $post_id, $references ) ),
155
+			array($entity_to_jsonld_converter->convert($post_id, $references)),
156 156
 			// Convert each URI in the references array to JSON-LD. We don't output
157 157
 			// entities already output above (hence the array_diff).
158
-			array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) {
158
+			array_filter(array_map(function($item) use ($entity_to_jsonld_converter, $references) {
159 159
 
160 160
 				// "2nd level properties" may not output here, e.g. a post
161 161
 				// mentioning an event, located in a place: the place is referenced
162 162
 				// via the `@id` but no other properties are loaded.
163
-				return $entity_to_jsonld_converter->convert( $item, $references );
164
-			}, $references ) ) );
163
+				return $entity_to_jsonld_converter->convert($item, $references);
164
+			}, $references)) );
165 165
 
166 166
 		// Finally send the JSON-LD.
167 167
 		return $jsonld;
Please login to merge, or discard this patch.
src/includes/class-wordlift-post-to-jsonld-converter.php 2 patches
Indentation   +251 added lines, -251 removed lines patch added patch discarded remove patch
@@ -15,256 +15,256 @@
 block discarded – undo
15 15
  */
16 16
 class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter {
17 17
 
18
-	/**
19
-	 * A {@link Wordlift_Configuration_Service} instance.
20
-	 *
21
-	 * @since  3.10.0
22
-	 * @access private
23
-	 * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
24
-	 */
25
-	private $configuration_service;
26
-
27
-	/**
28
-	 * A {@link Wordlift_Log_Service} instance.
29
-	 *
30
-	 * @since  3.10.0
31
-	 * @access private
32
-	 * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
33
-	 */
34
-	private $log;
35
-
36
-	/**
37
-	 * Wordlift_Post_To_Jsonld_Converter constructor.
38
-	 *
39
-	 * @since 3.10.0
40
-	 *
41
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
42
-	 * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
43
-	 * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
44
-	 * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
45
-	 * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
46
-	 */
47
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) {
48
-		parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
49
-
50
-		$this->configuration_service = $configuration_service;
51
-
52
-		// Set a reference to the logger.
53
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
54
-	}
55
-
56
-	/**
57
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
58
-	 * found while processing the post is set in the $references array.
59
-	 *
60
-	 * @since 3.10.0
61
-	 *
62
-	 * @param int $post_id The post id.
63
-	 * @param array $references An array of entity references.
64
-	 *
65
-	 * @return array A JSON-LD array.
66
-	 */
67
-	public function convert( $post_id, &$references = array() ) {
68
-
69
-		// Get the post instance.
70
-		if ( null === $post = get_post( $post_id ) ) {
71
-			// Post not found.
72
-			return null;
73
-		}
74
-
75
-		// Get the base JSON-LD and the list of entities referenced by this entity.
76
-		$jsonld = parent::convert( $post_id, $references );
77
-
78
-		// Get the entity name.
79
-		$jsonld['headline'] = $post->post_title;
80
-
81
-		// Set the published and modified dates.
82
-		$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
83
-		$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
84
-
85
-		// Get the word count for the post.
86
-		$post_adapter        = new Wordlift_Post_Adapter( $post_id );
87
-		$jsonld['wordCount'] = $post_adapter->word_count();
88
-
89
-		// Set the publisher.
90
-		$this->set_publisher( $jsonld );
91
-
92
-		// Process the references if any.
93
-		if ( 0 < count( $references ) ) {
94
-
95
-			// Prepare the `about` and `mentions` array.
96
-			$about = $mentions = array();
97
-
98
-			// If the entity is in the title, then it should be an `about`.
99
-			foreach ( $references as $reference ) {
100
-
101
-				// Get the entity labels.
102
-				$labels = $this->entity_service->get_labels( $reference );
103
-
104
-				// Get the entity URI.
105
-				$item = array(
106
-					'@id' => $this->entity_service->get_uri( $reference ),
107
-				);
108
-
109
-				$escaped_lables = array_map(
110
-					function ( $value ) {
111
-						return preg_quote( $value, '/' );
112
-					}, $labels
113
-				);
114
-
115
-				// Check if the labels match any part of the title.
116
-				$matches = 1 === preg_match( '/' . implode( '|', $escaped_lables ) . '/', $post->post_title );
117
-
118
-				// If the title matches, assign the entity to the about, otherwise to the mentions.
119
-				if ( $matches ) {
120
-					$about[] = $item;
121
-				} else {
122
-					$mentions[] = $item;
123
-				}
124
-			}
125
-
126
-			// If we have abouts, assign them to the JSON-LD.
127
-			if ( 0 < count( $about ) ) {
128
-				$jsonld['about'] = $about;
129
-			}
130
-
131
-			// If we have mentions, assign them to the JSON-LD.
132
-			if ( 0 < count( $mentions ) ) {
133
-				$jsonld['mentions'] = $mentions;
134
-			}
135
-		}
136
-
137
-		// Finally set the author.
138
-		$jsonld['author'] = $this->get_author( $post->post_author, $references );
139
-
140
-		/**
141
-		 * Call the `wl_post_jsonld` filter.
142
-		 *
143
-		 * @api
144
-		 *
145
-		 * @since 3.14.0
146
-		 *
147
-		 * @param array $jsonld The JSON-LD structure.
148
-		 * @param int $post_id The {@link WP_Post} `id`.
149
-		 * @param array $references The array of referenced entities.
150
-		 */
151
-		return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
152
-	}
153
-
154
-	/**
155
-	 * Get the author's JSON-LD fragment.
156
-	 *
157
-	 * The JSON-LD fragment is generated using the {@link WP_User}'s data or
158
-	 * the referenced entity if configured for the {@link WP_User}.
159
-	 *
160
-	 * @since 3.14.0
161
-	 *
162
-	 * @param int $author_id The author {@link WP_User}'s `id`.
163
-	 * @param array $references An array of referenced entities.
164
-	 *
165
-	 * @return string|array A JSON-LD structure.
166
-	 */
167
-	private function get_author( $author_id, &$references ) {
168
-
169
-		// Get the entity bound to this user.
170
-		$entity_id = $this->user_service->get_entity( $author_id );
171
-
172
-		// If there's no entity bound return a simple author structure.
173
-		if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
174
-
175
-			$author     = get_the_author_meta( 'display_name', $author_id );
176
-			$author_uri = $this->user_service->get_uri( $author_id );
177
-
178
-			return array(
179
-				'@type' => 'Person',
180
-				'@id'   => $author_uri,
181
-				'name'  => $author,
182
-			);
183
-		}
184
-
185
-		// Add the author to the references.
186
-		$author_uri   = $this->entity_service->get_uri( $entity_id );
187
-		$references[] = $entity_id;
188
-
189
-		// Return the JSON-LD for the referenced entity.
190
-		return array(
191
-			'@id' => $author_uri,
192
-		);
193
-	}
194
-
195
-	/**
196
-	 * Enrich the provided params array with publisher data, if available.
197
-	 *
198
-	 * @since 3.10.0
199
-	 *
200
-	 * @param array $params The parameters array.
201
-	 */
202
-	protected function set_publisher( &$params ) {
203
-
204
-		// If the publisher id isn't set don't do anything.
205
-		if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) {
206
-			return;
207
-		}
208
-
209
-		// Get the post instance.
210
-		if ( null === $post = get_post( $publisher_id ) ) {
211
-			// Publisher not found.
212
-			return;
213
-		}
214
-
215
-		// Get the item id.
216
-		$id = $this->entity_service->get_uri( $publisher_id );
217
-
218
-		// Get the type.
219
-		$type = $this->entity_type_service->get( $publisher_id );
220
-
221
-		// Get the name.
222
-		$name = $post->post_title;
223
-
224
-		// Set the publisher data.
225
-		$params['publisher'] = array(
226
-			'@type' => $this->relative_to_context( $type['uri'] ),
227
-			'@id'   => $id,
228
-			'name'  => $name,
229
-		);
230
-
231
-		// Add the sameAs values associated with the publisher.
232
-		$storage_factory = Wordlift_Storage_Factory::get_instance();
233
-		$sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
234
-		if ( ! empty( $sameas ) ) {
235
-			$params['publisher']['sameAs'] = $sameas;
236
-		}
237
-
238
-		// Set the logo, only for http://schema.org/Organization as Person doesn't
239
-		// support the logo property.
240
-		//
241
-		// See http://schema.org/logo.
242
-		if ( 'http://schema.org/Organization' !== $type['uri'] ) {
243
-			return;
244
-		}
245
-
246
-		// Get the logo, WP < 4.4 way: only post ID accepted here.
247
-		if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) {
248
-			return;
249
-		}
250
-
251
-		// Get the image URL.
252
-		if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, array( 600, 60 ) ) ) {
253
-			return;
254
-		}
255
-
256
-		// Copy over some useful properties.
257
-		//
258
-		// See https://developers.google.com/search/docs/data-types/articles.
259
-		$params['publisher']['logo']['@type'] = 'ImageObject';
260
-		$params['publisher']['logo']['url']   = $attachment[0];
261
-		// If you specify a "width" or "height" value you should leave out
262
-		// 'px'. For example: "width":"4608px" should be "width":"4608".
263
-		//
264
-		// See https://github.com/insideout10/wordlift-plugin/issues/451.
265
-		$params['publisher']['logo']['width']  = $attachment[1];
266
-		$params['publisher']['logo']['height'] = $attachment[2];
267
-
268
-	}
18
+    /**
19
+     * A {@link Wordlift_Configuration_Service} instance.
20
+     *
21
+     * @since  3.10.0
22
+     * @access private
23
+     * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
24
+     */
25
+    private $configuration_service;
26
+
27
+    /**
28
+     * A {@link Wordlift_Log_Service} instance.
29
+     *
30
+     * @since  3.10.0
31
+     * @access private
32
+     * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
33
+     */
34
+    private $log;
35
+
36
+    /**
37
+     * Wordlift_Post_To_Jsonld_Converter constructor.
38
+     *
39
+     * @since 3.10.0
40
+     *
41
+     * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
42
+     * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
43
+     * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
44
+     * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
45
+     * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
46
+     */
47
+    public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) {
48
+        parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
49
+
50
+        $this->configuration_service = $configuration_service;
51
+
52
+        // Set a reference to the logger.
53
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
54
+    }
55
+
56
+    /**
57
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
58
+     * found while processing the post is set in the $references array.
59
+     *
60
+     * @since 3.10.0
61
+     *
62
+     * @param int $post_id The post id.
63
+     * @param array $references An array of entity references.
64
+     *
65
+     * @return array A JSON-LD array.
66
+     */
67
+    public function convert( $post_id, &$references = array() ) {
68
+
69
+        // Get the post instance.
70
+        if ( null === $post = get_post( $post_id ) ) {
71
+            // Post not found.
72
+            return null;
73
+        }
74
+
75
+        // Get the base JSON-LD and the list of entities referenced by this entity.
76
+        $jsonld = parent::convert( $post_id, $references );
77
+
78
+        // Get the entity name.
79
+        $jsonld['headline'] = $post->post_title;
80
+
81
+        // Set the published and modified dates.
82
+        $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
83
+        $jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
84
+
85
+        // Get the word count for the post.
86
+        $post_adapter        = new Wordlift_Post_Adapter( $post_id );
87
+        $jsonld['wordCount'] = $post_adapter->word_count();
88
+
89
+        // Set the publisher.
90
+        $this->set_publisher( $jsonld );
91
+
92
+        // Process the references if any.
93
+        if ( 0 < count( $references ) ) {
94
+
95
+            // Prepare the `about` and `mentions` array.
96
+            $about = $mentions = array();
97
+
98
+            // If the entity is in the title, then it should be an `about`.
99
+            foreach ( $references as $reference ) {
100
+
101
+                // Get the entity labels.
102
+                $labels = $this->entity_service->get_labels( $reference );
103
+
104
+                // Get the entity URI.
105
+                $item = array(
106
+                    '@id' => $this->entity_service->get_uri( $reference ),
107
+                );
108
+
109
+                $escaped_lables = array_map(
110
+                    function ( $value ) {
111
+                        return preg_quote( $value, '/' );
112
+                    }, $labels
113
+                );
114
+
115
+                // Check if the labels match any part of the title.
116
+                $matches = 1 === preg_match( '/' . implode( '|', $escaped_lables ) . '/', $post->post_title );
117
+
118
+                // If the title matches, assign the entity to the about, otherwise to the mentions.
119
+                if ( $matches ) {
120
+                    $about[] = $item;
121
+                } else {
122
+                    $mentions[] = $item;
123
+                }
124
+            }
125
+
126
+            // If we have abouts, assign them to the JSON-LD.
127
+            if ( 0 < count( $about ) ) {
128
+                $jsonld['about'] = $about;
129
+            }
130
+
131
+            // If we have mentions, assign them to the JSON-LD.
132
+            if ( 0 < count( $mentions ) ) {
133
+                $jsonld['mentions'] = $mentions;
134
+            }
135
+        }
136
+
137
+        // Finally set the author.
138
+        $jsonld['author'] = $this->get_author( $post->post_author, $references );
139
+
140
+        /**
141
+         * Call the `wl_post_jsonld` filter.
142
+         *
143
+         * @api
144
+         *
145
+         * @since 3.14.0
146
+         *
147
+         * @param array $jsonld The JSON-LD structure.
148
+         * @param int $post_id The {@link WP_Post} `id`.
149
+         * @param array $references The array of referenced entities.
150
+         */
151
+        return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
152
+    }
153
+
154
+    /**
155
+     * Get the author's JSON-LD fragment.
156
+     *
157
+     * The JSON-LD fragment is generated using the {@link WP_User}'s data or
158
+     * the referenced entity if configured for the {@link WP_User}.
159
+     *
160
+     * @since 3.14.0
161
+     *
162
+     * @param int $author_id The author {@link WP_User}'s `id`.
163
+     * @param array $references An array of referenced entities.
164
+     *
165
+     * @return string|array A JSON-LD structure.
166
+     */
167
+    private function get_author( $author_id, &$references ) {
168
+
169
+        // Get the entity bound to this user.
170
+        $entity_id = $this->user_service->get_entity( $author_id );
171
+
172
+        // If there's no entity bound return a simple author structure.
173
+        if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
174
+
175
+            $author     = get_the_author_meta( 'display_name', $author_id );
176
+            $author_uri = $this->user_service->get_uri( $author_id );
177
+
178
+            return array(
179
+                '@type' => 'Person',
180
+                '@id'   => $author_uri,
181
+                'name'  => $author,
182
+            );
183
+        }
184
+
185
+        // Add the author to the references.
186
+        $author_uri   = $this->entity_service->get_uri( $entity_id );
187
+        $references[] = $entity_id;
188
+
189
+        // Return the JSON-LD for the referenced entity.
190
+        return array(
191
+            '@id' => $author_uri,
192
+        );
193
+    }
194
+
195
+    /**
196
+     * Enrich the provided params array with publisher data, if available.
197
+     *
198
+     * @since 3.10.0
199
+     *
200
+     * @param array $params The parameters array.
201
+     */
202
+    protected function set_publisher( &$params ) {
203
+
204
+        // If the publisher id isn't set don't do anything.
205
+        if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) {
206
+            return;
207
+        }
208
+
209
+        // Get the post instance.
210
+        if ( null === $post = get_post( $publisher_id ) ) {
211
+            // Publisher not found.
212
+            return;
213
+        }
214
+
215
+        // Get the item id.
216
+        $id = $this->entity_service->get_uri( $publisher_id );
217
+
218
+        // Get the type.
219
+        $type = $this->entity_type_service->get( $publisher_id );
220
+
221
+        // Get the name.
222
+        $name = $post->post_title;
223
+
224
+        // Set the publisher data.
225
+        $params['publisher'] = array(
226
+            '@type' => $this->relative_to_context( $type['uri'] ),
227
+            '@id'   => $id,
228
+            'name'  => $name,
229
+        );
230
+
231
+        // Add the sameAs values associated with the publisher.
232
+        $storage_factory = Wordlift_Storage_Factory::get_instance();
233
+        $sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
234
+        if ( ! empty( $sameas ) ) {
235
+            $params['publisher']['sameAs'] = $sameas;
236
+        }
237
+
238
+        // Set the logo, only for http://schema.org/Organization as Person doesn't
239
+        // support the logo property.
240
+        //
241
+        // See http://schema.org/logo.
242
+        if ( 'http://schema.org/Organization' !== $type['uri'] ) {
243
+            return;
244
+        }
245
+
246
+        // Get the logo, WP < 4.4 way: only post ID accepted here.
247
+        if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) {
248
+            return;
249
+        }
250
+
251
+        // Get the image URL.
252
+        if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, array( 600, 60 ) ) ) {
253
+            return;
254
+        }
255
+
256
+        // Copy over some useful properties.
257
+        //
258
+        // See https://developers.google.com/search/docs/data-types/articles.
259
+        $params['publisher']['logo']['@type'] = 'ImageObject';
260
+        $params['publisher']['logo']['url']   = $attachment[0];
261
+        // If you specify a "width" or "height" value you should leave out
262
+        // 'px'. For example: "width":"4608px" should be "width":"4608".
263
+        //
264
+        // See https://github.com/insideout10/wordlift-plugin/issues/451.
265
+        $params['publisher']['logo']['width']  = $attachment[1];
266
+        $params['publisher']['logo']['height'] = $attachment[2];
267
+
268
+    }
269 269
 
270 270
 }
Please login to merge, or discard this patch.
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -44,13 +44,13 @@  discard block
 block discarded – undo
44 44
 	 * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
45 45
 	 * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
46 46
 	 */
47
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) {
48
-		parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
47
+	public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service) {
48
+		parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service);
49 49
 
50 50
 		$this->configuration_service = $configuration_service;
51 51
 
52 52
 		// Set a reference to the logger.
53
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' );
53
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter');
54 54
 	}
55 55
 
56 56
 	/**
@@ -64,59 +64,59 @@  discard block
 block discarded – undo
64 64
 	 *
65 65
 	 * @return array A JSON-LD array.
66 66
 	 */
67
-	public function convert( $post_id, &$references = array() ) {
67
+	public function convert($post_id, &$references = array()) {
68 68
 
69 69
 		// Get the post instance.
70
-		if ( null === $post = get_post( $post_id ) ) {
70
+		if (null === $post = get_post($post_id)) {
71 71
 			// Post not found.
72 72
 			return null;
73 73
 		}
74 74
 
75 75
 		// Get the base JSON-LD and the list of entities referenced by this entity.
76
-		$jsonld = parent::convert( $post_id, $references );
76
+		$jsonld = parent::convert($post_id, $references);
77 77
 
78 78
 		// Get the entity name.
79 79
 		$jsonld['headline'] = $post->post_title;
80 80
 
81 81
 		// Set the published and modified dates.
82
-		$jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false );
83
-		$jsonld['dateModified']  = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false );
82
+		$jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false);
83
+		$jsonld['dateModified']  = get_post_modified_time('Y-m-d\TH:i', true, $post, false);
84 84
 
85 85
 		// Get the word count for the post.
86
-		$post_adapter        = new Wordlift_Post_Adapter( $post_id );
86
+		$post_adapter        = new Wordlift_Post_Adapter($post_id);
87 87
 		$jsonld['wordCount'] = $post_adapter->word_count();
88 88
 
89 89
 		// Set the publisher.
90
-		$this->set_publisher( $jsonld );
90
+		$this->set_publisher($jsonld);
91 91
 
92 92
 		// Process the references if any.
93
-		if ( 0 < count( $references ) ) {
93
+		if (0 < count($references)) {
94 94
 
95 95
 			// Prepare the `about` and `mentions` array.
96 96
 			$about = $mentions = array();
97 97
 
98 98
 			// If the entity is in the title, then it should be an `about`.
99
-			foreach ( $references as $reference ) {
99
+			foreach ($references as $reference) {
100 100
 
101 101
 				// Get the entity labels.
102
-				$labels = $this->entity_service->get_labels( $reference );
102
+				$labels = $this->entity_service->get_labels($reference);
103 103
 
104 104
 				// Get the entity URI.
105 105
 				$item = array(
106
-					'@id' => $this->entity_service->get_uri( $reference ),
106
+					'@id' => $this->entity_service->get_uri($reference),
107 107
 				);
108 108
 
109 109
 				$escaped_lables = array_map(
110
-					function ( $value ) {
111
-						return preg_quote( $value, '/' );
110
+					function($value) {
111
+						return preg_quote($value, '/');
112 112
 					}, $labels
113 113
 				);
114 114
 
115 115
 				// Check if the labels match any part of the title.
116
-				$matches = 1 === preg_match( '/' . implode( '|', $escaped_lables ) . '/', $post->post_title );
116
+				$matches = 1 === preg_match('/'.implode('|', $escaped_lables).'/', $post->post_title);
117 117
 
118 118
 				// If the title matches, assign the entity to the about, otherwise to the mentions.
119
-				if ( $matches ) {
119
+				if ($matches) {
120 120
 					$about[] = $item;
121 121
 				} else {
122 122
 					$mentions[] = $item;
@@ -124,18 +124,18 @@  discard block
 block discarded – undo
124 124
 			}
125 125
 
126 126
 			// If we have abouts, assign them to the JSON-LD.
127
-			if ( 0 < count( $about ) ) {
127
+			if (0 < count($about)) {
128 128
 				$jsonld['about'] = $about;
129 129
 			}
130 130
 
131 131
 			// If we have mentions, assign them to the JSON-LD.
132
-			if ( 0 < count( $mentions ) ) {
132
+			if (0 < count($mentions)) {
133 133
 				$jsonld['mentions'] = $mentions;
134 134
 			}
135 135
 		}
136 136
 
137 137
 		// Finally set the author.
138
-		$jsonld['author'] = $this->get_author( $post->post_author, $references );
138
+		$jsonld['author'] = $this->get_author($post->post_author, $references);
139 139
 
140 140
 		/**
141 141
 		 * Call the `wl_post_jsonld` filter.
@@ -148,7 +148,7 @@  discard block
 block discarded – undo
148 148
 		 * @param int $post_id The {@link WP_Post} `id`.
149 149
 		 * @param array $references The array of referenced entities.
150 150
 		 */
151
-		return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references );
151
+		return apply_filters('wl_post_jsonld', $jsonld, $post_id, $references);
152 152
 	}
153 153
 
154 154
 	/**
@@ -164,16 +164,16 @@  discard block
 block discarded – undo
164 164
 	 *
165 165
 	 * @return string|array A JSON-LD structure.
166 166
 	 */
167
-	private function get_author( $author_id, &$references ) {
167
+	private function get_author($author_id, &$references) {
168 168
 
169 169
 		// Get the entity bound to this user.
170
-		$entity_id = $this->user_service->get_entity( $author_id );
170
+		$entity_id = $this->user_service->get_entity($author_id);
171 171
 
172 172
 		// If there's no entity bound return a simple author structure.
173
-		if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
173
+		if (empty($entity_id) || 'publish' !== get_post_status($entity_id)) {
174 174
 
175
-			$author     = get_the_author_meta( 'display_name', $author_id );
176
-			$author_uri = $this->user_service->get_uri( $author_id );
175
+			$author     = get_the_author_meta('display_name', $author_id);
176
+			$author_uri = $this->user_service->get_uri($author_id);
177 177
 
178 178
 			return array(
179 179
 				'@type' => 'Person',
@@ -183,7 +183,7 @@  discard block
 block discarded – undo
183 183
 		}
184 184
 
185 185
 		// Add the author to the references.
186
-		$author_uri   = $this->entity_service->get_uri( $entity_id );
186
+		$author_uri   = $this->entity_service->get_uri($entity_id);
187 187
 		$references[] = $entity_id;
188 188
 
189 189
 		// Return the JSON-LD for the referenced entity.
@@ -199,39 +199,39 @@  discard block
 block discarded – undo
199 199
 	 *
200 200
 	 * @param array $params The parameters array.
201 201
 	 */
202
-	protected function set_publisher( &$params ) {
202
+	protected function set_publisher(&$params) {
203 203
 
204 204
 		// If the publisher id isn't set don't do anything.
205
-		if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) {
205
+		if (null === $publisher_id = $this->configuration_service->get_publisher_id()) {
206 206
 			return;
207 207
 		}
208 208
 
209 209
 		// Get the post instance.
210
-		if ( null === $post = get_post( $publisher_id ) ) {
210
+		if (null === $post = get_post($publisher_id)) {
211 211
 			// Publisher not found.
212 212
 			return;
213 213
 		}
214 214
 
215 215
 		// Get the item id.
216
-		$id = $this->entity_service->get_uri( $publisher_id );
216
+		$id = $this->entity_service->get_uri($publisher_id);
217 217
 
218 218
 		// Get the type.
219
-		$type = $this->entity_type_service->get( $publisher_id );
219
+		$type = $this->entity_type_service->get($publisher_id);
220 220
 
221 221
 		// Get the name.
222 222
 		$name = $post->post_title;
223 223
 
224 224
 		// Set the publisher data.
225 225
 		$params['publisher'] = array(
226
-			'@type' => $this->relative_to_context( $type['uri'] ),
226
+			'@type' => $this->relative_to_context($type['uri']),
227 227
 			'@id'   => $id,
228 228
 			'name'  => $name,
229 229
 		);
230 230
 
231 231
 		// Add the sameAs values associated with the publisher.
232 232
 		$storage_factory = Wordlift_Storage_Factory::get_instance();
233
-		$sameas          = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id );
234
-		if ( ! empty( $sameas ) ) {
233
+		$sameas          = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id);
234
+		if ( ! empty($sameas)) {
235 235
 			$params['publisher']['sameAs'] = $sameas;
236 236
 		}
237 237
 
@@ -239,17 +239,17 @@  discard block
 block discarded – undo
239 239
 		// support the logo property.
240 240
 		//
241 241
 		// See http://schema.org/logo.
242
-		if ( 'http://schema.org/Organization' !== $type['uri'] ) {
242
+		if ('http://schema.org/Organization' !== $type['uri']) {
243 243
 			return;
244 244
 		}
245 245
 
246 246
 		// Get the logo, WP < 4.4 way: only post ID accepted here.
247
-		if ( '' === $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) {
247
+		if ('' === $thumbnail_id = get_post_thumbnail_id($post->ID)) {
248 248
 			return;
249 249
 		}
250 250
 
251 251
 		// Get the image URL.
252
-		if ( false === $attachment = wp_get_attachment_image_src( $thumbnail_id, array( 600, 60 ) ) ) {
252
+		if (false === $attachment = wp_get_attachment_image_src($thumbnail_id, array(600, 60))) {
253 253
 			return;
254 254
 		}
255 255
 
Please login to merge, or discard this patch.