Completed
Push — develop ( a437a9...1753bd )
by David
03:04
created
src/includes/class-wordlift-abstract-post-to-jsonld-converter.php 2 patches
Indentation   +211 added lines, -211 removed lines patch added patch discarded remove patch
@@ -16,216 +16,216 @@
 block discarded – undo
16 16
  */
17 17
 abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter {
18 18
 
19
-	/**
20
-	 * The JSON-LD context.
21
-	 *
22
-	 * @since 3.10.0
23
-	 */
24
-	const CONTEXT = 'http://schema.org';
25
-
26
-	/**
27
-	 * A {@link Wordlift_Entity_Type_Service} instance.
28
-	 *
29
-	 * @since  3.10.0
30
-	 * @access protected
31
-	 * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
32
-	 */
33
-	protected $entity_type_service;
34
-
35
-	/**
36
-	 * A {@link Wordlift_Entity_Service} instance.
37
-	 *
38
-	 * @since  3.10.0
39
-	 * @access protected
40
-	 * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance.
41
-	 */
42
-	protected $entity_service;
43
-
44
-	/**
45
-	 * A {@link Wordlift_User_Service} instance.
46
-	 *
47
-	 * @since  3.10.0
48
-	 * @access private
49
-	 * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
50
-	 */
51
-	protected $user_service;
52
-
53
-	/**
54
-	 * A {@link Wordlift_Attachment_Service} instance.
55
-	 *
56
-	 * @since  3.10.0
57
-	 * @access private
58
-	 * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
59
-	 */
60
-	protected $attachment_service;
61
-
62
-	/**
63
-	 * Wordlift_Post_To_Jsonld_Converter constructor.
64
-	 *
65
-	 * @since 3.10.0
66
-	 *
67
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
68
-	 * @param \Wordlift_Entity_Service      $entity_service      A {@link Wordlift_Entity_Service} instance.
69
-	 * @param \Wordlift_User_Service        $user_service        A {@link Wordlift_User_Service} instance.
70
-	 * @param \Wordlift_Attachment_Service  $attachment_service  A {@link Wordlift_Attachment_Service} instance.
71
-	 */
72
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) {
73
-
74
-		$this->entity_type_service = $entity_type_service;
75
-		$this->entity_service      = $entity_service;
76
-		$this->user_service        = $user_service;
77
-		$this->attachment_service  = $attachment_service;
78
-
79
-	}
80
-
81
-	/**
82
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
83
-	 * found while processing the post is set in the $references array.
84
-	 *
85
-	 * @since 3.10.0
86
-	 *
87
-	 *
88
-	 * @param int   $post_id    The post id.
89
-	 * @param array $references An array of entity references.
90
-	 *
91
-	 * @return array A JSON-LD array.
92
-	 */
93
-	public function convert( $post_id, &$references = array() ) {
94
-
95
-		// Get the post instance.
96
-		if ( null === $post = get_post( $post_id ) ) {
97
-			// Post not found.
98
-			return null;
99
-		}
100
-
101
-		// Get the post URI @id.
102
-		$id = $this->entity_service->get_uri( $post->ID );
103
-
104
-		// Get the entity @type. We consider `post` BlogPostings.
105
-		$type = $this->entity_type_service->get( $post_id );
106
-
107
-		// Prepare the response.
108
-		$jsonld = array(
109
-			'@context'    => self::CONTEXT,
110
-			'@id'         => $id,
111
-			'@type'       => $this->relative_to_context( $type['uri'] ),
112
-			'description' => $this->get_excerpt( $post ),
113
-		);
114
-
115
-		// Set the `mainEntityOfPage` property if the post has some contents.
116
-		if ( ! empty( $post->post_content ) ) {
117
-			// We're setting the `mainEntityOfPage` to signal which one is the
118
-			// main entity for the specified URL. It might be however that the
119
-			// post/page is actually about another specific entity. How WL deals
120
-			// with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
121
-			//
122
-			// See http://schema.org/mainEntityOfPage
123
-			$jsonld['mainEntityOfPage'] = array(
124
-				'@type' => 'WebPage',
125
-				'@id'   => get_the_permalink( $post->ID ),
126
-			);
127
-		};
128
-
129
-		$this->set_images( $post, $jsonld );
130
-
131
-		// Get the entities referenced by this post and set it to the `references`
132
-		// array so that the caller can do further processing, such as printing out
133
-		// more of those references.
134
-		$references = $this->entity_service->get_related_entities( $post->ID );
135
-
136
-		return $jsonld;
137
-	}
138
-
139
-	/**
140
-	 * If the provided value starts with the schema.org context, we remove the schema.org
141
-	 * part since it is set with the '@context'.
142
-	 *
143
-	 * @since 3.10.0
144
-	 *
145
-	 * @param string $value The property value.
146
-	 *
147
-	 * @return string The property value without the context.
148
-	 */
149
-	public function relative_to_context( $value ) {
150
-
151
-		return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
152
-	}
153
-
154
-	/**
155
-	 * Get the excerpt for the provided {@link WP_Post}.
156
-	 *
157
-	 * @since 3.10.0
158
-	 *
159
-	 * @param WP_Post $post The {@link WP_Post}.
160
-	 *
161
-	 * @return string The excerpt.
162
-	 */
163
-	protected function get_excerpt( $post ) {
164
-
165
-		// Temporary pop the previous post.
166
-		$original = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
167
-
168
-		// Setup our own post.
169
-		setup_postdata( $GLOBALS['post'] = $post );
170
-
171
-		$excerpt = get_the_excerpt();
172
-
173
-		// Restore the previous post.
174
-		if ( null !== $original ) {
175
-			setup_postdata( $GLOBALS['post'] = $original );
176
-		}
177
-
178
-		// Finally return the excerpt.
179
-		return html_entity_decode( $excerpt );
180
-	}
181
-
182
-	/**
183
-	 * Set the images, by looking for embedded images, for images loaded via the
184
-	 * gallery and for the featured image.
185
-	 *
186
-	 * @since 3.10.0
187
-	 *
188
-	 * @param WP_Post $post   The target {@link WP_Post}.
189
-	 * @param array   $jsonld The JSON-LD array.
190
-	 */
191
-	protected function set_images( $post, &$jsonld ) {
192
-
193
-		// Prepare the attachment ids array.
194
-		$ids = array();
195
-
196
-		// Set the thumbnail id as first attachment id, if any.
197
-		if ( '' !== $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) {
198
-			$ids[] = $thumbnail_id;
199
-		}
200
-
201
-		// Get the embeds, removing existing ids.
202
-		$embeds = array_diff( $this->attachment_service->get_image_embeds( $post->post_content ), $ids );
203
-
204
-		// Get the gallery, removing existing ids.
205
-		$gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds );
206
-
207
-		// Map the attachment ids to images' data structured for schema.org use.
208
-		$images = array_map( function ( $item ) {
209
-
210
-			// @todo: we're not sure that we're getting attachment data here, we
211
-			// should filter `false`s.
212
-
213
-			// Get the attachment data.
214
-			$attachment = wp_get_attachment_image_src( $item, 'full' );
215
-
216
-			// Refactor data as per schema.org specifications.
217
-			return array(
218
-				'@type'  => 'ImageObject',
219
-				'url'    => $attachment[0],
220
-				'width'  => $attachment[1] . 'px',
221
-				'height' => $attachment[2] . 'px',
222
-			);
223
-		}, array_merge( $ids, $embeds, $gallery ) );
224
-
225
-		if ( 0 < sizeof( $images ) ) {
226
-			$jsonld['image'] = $images;
227
-		};
228
-
229
-	}
19
+    /**
20
+     * The JSON-LD context.
21
+     *
22
+     * @since 3.10.0
23
+     */
24
+    const CONTEXT = 'http://schema.org';
25
+
26
+    /**
27
+     * A {@link Wordlift_Entity_Type_Service} instance.
28
+     *
29
+     * @since  3.10.0
30
+     * @access protected
31
+     * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
32
+     */
33
+    protected $entity_type_service;
34
+
35
+    /**
36
+     * A {@link Wordlift_Entity_Service} instance.
37
+     *
38
+     * @since  3.10.0
39
+     * @access protected
40
+     * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance.
41
+     */
42
+    protected $entity_service;
43
+
44
+    /**
45
+     * A {@link Wordlift_User_Service} instance.
46
+     *
47
+     * @since  3.10.0
48
+     * @access private
49
+     * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
50
+     */
51
+    protected $user_service;
52
+
53
+    /**
54
+     * A {@link Wordlift_Attachment_Service} instance.
55
+     *
56
+     * @since  3.10.0
57
+     * @access private
58
+     * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
59
+     */
60
+    protected $attachment_service;
61
+
62
+    /**
63
+     * Wordlift_Post_To_Jsonld_Converter constructor.
64
+     *
65
+     * @since 3.10.0
66
+     *
67
+     * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
68
+     * @param \Wordlift_Entity_Service      $entity_service      A {@link Wordlift_Entity_Service} instance.
69
+     * @param \Wordlift_User_Service        $user_service        A {@link Wordlift_User_Service} instance.
70
+     * @param \Wordlift_Attachment_Service  $attachment_service  A {@link Wordlift_Attachment_Service} instance.
71
+     */
72
+    public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) {
73
+
74
+        $this->entity_type_service = $entity_type_service;
75
+        $this->entity_service      = $entity_service;
76
+        $this->user_service        = $user_service;
77
+        $this->attachment_service  = $attachment_service;
78
+
79
+    }
80
+
81
+    /**
82
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
83
+     * found while processing the post is set in the $references array.
84
+     *
85
+     * @since 3.10.0
86
+     *
87
+     *
88
+     * @param int   $post_id    The post id.
89
+     * @param array $references An array of entity references.
90
+     *
91
+     * @return array A JSON-LD array.
92
+     */
93
+    public function convert( $post_id, &$references = array() ) {
94
+
95
+        // Get the post instance.
96
+        if ( null === $post = get_post( $post_id ) ) {
97
+            // Post not found.
98
+            return null;
99
+        }
100
+
101
+        // Get the post URI @id.
102
+        $id = $this->entity_service->get_uri( $post->ID );
103
+
104
+        // Get the entity @type. We consider `post` BlogPostings.
105
+        $type = $this->entity_type_service->get( $post_id );
106
+
107
+        // Prepare the response.
108
+        $jsonld = array(
109
+            '@context'    => self::CONTEXT,
110
+            '@id'         => $id,
111
+            '@type'       => $this->relative_to_context( $type['uri'] ),
112
+            'description' => $this->get_excerpt( $post ),
113
+        );
114
+
115
+        // Set the `mainEntityOfPage` property if the post has some contents.
116
+        if ( ! empty( $post->post_content ) ) {
117
+            // We're setting the `mainEntityOfPage` to signal which one is the
118
+            // main entity for the specified URL. It might be however that the
119
+            // post/page is actually about another specific entity. How WL deals
120
+            // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451).
121
+            //
122
+            // See http://schema.org/mainEntityOfPage
123
+            $jsonld['mainEntityOfPage'] = array(
124
+                '@type' => 'WebPage',
125
+                '@id'   => get_the_permalink( $post->ID ),
126
+            );
127
+        };
128
+
129
+        $this->set_images( $post, $jsonld );
130
+
131
+        // Get the entities referenced by this post and set it to the `references`
132
+        // array so that the caller can do further processing, such as printing out
133
+        // more of those references.
134
+        $references = $this->entity_service->get_related_entities( $post->ID );
135
+
136
+        return $jsonld;
137
+    }
138
+
139
+    /**
140
+     * If the provided value starts with the schema.org context, we remove the schema.org
141
+     * part since it is set with the '@context'.
142
+     *
143
+     * @since 3.10.0
144
+     *
145
+     * @param string $value The property value.
146
+     *
147
+     * @return string The property value without the context.
148
+     */
149
+    public function relative_to_context( $value ) {
150
+
151
+        return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
152
+    }
153
+
154
+    /**
155
+     * Get the excerpt for the provided {@link WP_Post}.
156
+     *
157
+     * @since 3.10.0
158
+     *
159
+     * @param WP_Post $post The {@link WP_Post}.
160
+     *
161
+     * @return string The excerpt.
162
+     */
163
+    protected function get_excerpt( $post ) {
164
+
165
+        // Temporary pop the previous post.
166
+        $original = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
167
+
168
+        // Setup our own post.
169
+        setup_postdata( $GLOBALS['post'] = $post );
170
+
171
+        $excerpt = get_the_excerpt();
172
+
173
+        // Restore the previous post.
174
+        if ( null !== $original ) {
175
+            setup_postdata( $GLOBALS['post'] = $original );
176
+        }
177
+
178
+        // Finally return the excerpt.
179
+        return html_entity_decode( $excerpt );
180
+    }
181
+
182
+    /**
183
+     * Set the images, by looking for embedded images, for images loaded via the
184
+     * gallery and for the featured image.
185
+     *
186
+     * @since 3.10.0
187
+     *
188
+     * @param WP_Post $post   The target {@link WP_Post}.
189
+     * @param array   $jsonld The JSON-LD array.
190
+     */
191
+    protected function set_images( $post, &$jsonld ) {
192
+
193
+        // Prepare the attachment ids array.
194
+        $ids = array();
195
+
196
+        // Set the thumbnail id as first attachment id, if any.
197
+        if ( '' !== $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) {
198
+            $ids[] = $thumbnail_id;
199
+        }
200
+
201
+        // Get the embeds, removing existing ids.
202
+        $embeds = array_diff( $this->attachment_service->get_image_embeds( $post->post_content ), $ids );
203
+
204
+        // Get the gallery, removing existing ids.
205
+        $gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds );
206
+
207
+        // Map the attachment ids to images' data structured for schema.org use.
208
+        $images = array_map( function ( $item ) {
209
+
210
+            // @todo: we're not sure that we're getting attachment data here, we
211
+            // should filter `false`s.
212
+
213
+            // Get the attachment data.
214
+            $attachment = wp_get_attachment_image_src( $item, 'full' );
215
+
216
+            // Refactor data as per schema.org specifications.
217
+            return array(
218
+                '@type'  => 'ImageObject',
219
+                'url'    => $attachment[0],
220
+                'width'  => $attachment[1] . 'px',
221
+                'height' => $attachment[2] . 'px',
222
+            );
223
+        }, array_merge( $ids, $embeds, $gallery ) );
224
+
225
+        if ( 0 < sizeof( $images ) ) {
226
+            $jsonld['image'] = $images;
227
+        };
228
+
229
+    }
230 230
 
231 231
 }
Please login to merge, or discard this patch.
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 	 * @param \Wordlift_User_Service        $user_service        A {@link Wordlift_User_Service} instance.
70 70
 	 * @param \Wordlift_Attachment_Service  $attachment_service  A {@link Wordlift_Attachment_Service} instance.
71 71
 	 */
72
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) {
72
+	public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service) {
73 73
 
74 74
 		$this->entity_type_service = $entity_type_service;
75 75
 		$this->entity_service      = $entity_service;
@@ -90,30 +90,30 @@  discard block
 block discarded – undo
90 90
 	 *
91 91
 	 * @return array A JSON-LD array.
92 92
 	 */
93
-	public function convert( $post_id, &$references = array() ) {
93
+	public function convert($post_id, &$references = array()) {
94 94
 
95 95
 		// Get the post instance.
96
-		if ( null === $post = get_post( $post_id ) ) {
96
+		if (null === $post = get_post($post_id)) {
97 97
 			// Post not found.
98 98
 			return null;
99 99
 		}
100 100
 
101 101
 		// Get the post URI @id.
102
-		$id = $this->entity_service->get_uri( $post->ID );
102
+		$id = $this->entity_service->get_uri($post->ID);
103 103
 
104 104
 		// Get the entity @type. We consider `post` BlogPostings.
105
-		$type = $this->entity_type_service->get( $post_id );
105
+		$type = $this->entity_type_service->get($post_id);
106 106
 
107 107
 		// Prepare the response.
108 108
 		$jsonld = array(
109 109
 			'@context'    => self::CONTEXT,
110 110
 			'@id'         => $id,
111
-			'@type'       => $this->relative_to_context( $type['uri'] ),
112
-			'description' => $this->get_excerpt( $post ),
111
+			'@type'       => $this->relative_to_context($type['uri']),
112
+			'description' => $this->get_excerpt($post),
113 113
 		);
114 114
 
115 115
 		// Set the `mainEntityOfPage` property if the post has some contents.
116
-		if ( ! empty( $post->post_content ) ) {
116
+		if ( ! empty($post->post_content)) {
117 117
 			// We're setting the `mainEntityOfPage` to signal which one is the
118 118
 			// main entity for the specified URL. It might be however that the
119 119
 			// post/page is actually about another specific entity. How WL deals
@@ -122,16 +122,16 @@  discard block
 block discarded – undo
122 122
 			// See http://schema.org/mainEntityOfPage
123 123
 			$jsonld['mainEntityOfPage'] = array(
124 124
 				'@type' => 'WebPage',
125
-				'@id'   => get_the_permalink( $post->ID ),
125
+				'@id'   => get_the_permalink($post->ID),
126 126
 			);
127 127
 		};
128 128
 
129
-		$this->set_images( $post, $jsonld );
129
+		$this->set_images($post, $jsonld);
130 130
 
131 131
 		// Get the entities referenced by this post and set it to the `references`
132 132
 		// array so that the caller can do further processing, such as printing out
133 133
 		// more of those references.
134
-		$references = $this->entity_service->get_related_entities( $post->ID );
134
+		$references = $this->entity_service->get_related_entities($post->ID);
135 135
 
136 136
 		return $jsonld;
137 137
 	}
@@ -146,9 +146,9 @@  discard block
 block discarded – undo
146 146
 	 *
147 147
 	 * @return string The property value without the context.
148 148
 	 */
149
-	public function relative_to_context( $value ) {
149
+	public function relative_to_context($value) {
150 150
 
151
-		return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value;
151
+		return 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value;
152 152
 	}
153 153
 
154 154
 	/**
@@ -160,23 +160,23 @@  discard block
 block discarded – undo
160 160
 	 *
161 161
 	 * @return string The excerpt.
162 162
 	 */
163
-	protected function get_excerpt( $post ) {
163
+	protected function get_excerpt($post) {
164 164
 
165 165
 		// Temporary pop the previous post.
166
-		$original = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
166
+		$original = isset($GLOBALS['post']) ? $GLOBALS['post'] : null;
167 167
 
168 168
 		// Setup our own post.
169
-		setup_postdata( $GLOBALS['post'] = $post );
169
+		setup_postdata($GLOBALS['post'] = $post);
170 170
 
171 171
 		$excerpt = get_the_excerpt();
172 172
 
173 173
 		// Restore the previous post.
174
-		if ( null !== $original ) {
175
-			setup_postdata( $GLOBALS['post'] = $original );
174
+		if (null !== $original) {
175
+			setup_postdata($GLOBALS['post'] = $original);
176 176
 		}
177 177
 
178 178
 		// Finally return the excerpt.
179
-		return html_entity_decode( $excerpt );
179
+		return html_entity_decode($excerpt);
180 180
 	}
181 181
 
182 182
 	/**
@@ -188,41 +188,41 @@  discard block
 block discarded – undo
188 188
 	 * @param WP_Post $post   The target {@link WP_Post}.
189 189
 	 * @param array   $jsonld The JSON-LD array.
190 190
 	 */
191
-	protected function set_images( $post, &$jsonld ) {
191
+	protected function set_images($post, &$jsonld) {
192 192
 
193 193
 		// Prepare the attachment ids array.
194 194
 		$ids = array();
195 195
 
196 196
 		// Set the thumbnail id as first attachment id, if any.
197
-		if ( '' !== $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) {
197
+		if ('' !== $thumbnail_id = get_post_thumbnail_id($post->ID)) {
198 198
 			$ids[] = $thumbnail_id;
199 199
 		}
200 200
 
201 201
 		// Get the embeds, removing existing ids.
202
-		$embeds = array_diff( $this->attachment_service->get_image_embeds( $post->post_content ), $ids );
202
+		$embeds = array_diff($this->attachment_service->get_image_embeds($post->post_content), $ids);
203 203
 
204 204
 		// Get the gallery, removing existing ids.
205
-		$gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds );
205
+		$gallery = array_diff($this->attachment_service->get_gallery($post), $ids, $embeds);
206 206
 
207 207
 		// Map the attachment ids to images' data structured for schema.org use.
208
-		$images = array_map( function ( $item ) {
208
+		$images = array_map(function($item) {
209 209
 
210 210
 			// @todo: we're not sure that we're getting attachment data here, we
211 211
 			// should filter `false`s.
212 212
 
213 213
 			// Get the attachment data.
214
-			$attachment = wp_get_attachment_image_src( $item, 'full' );
214
+			$attachment = wp_get_attachment_image_src($item, 'full');
215 215
 
216 216
 			// Refactor data as per schema.org specifications.
217 217
 			return array(
218 218
 				'@type'  => 'ImageObject',
219 219
 				'url'    => $attachment[0],
220
-				'width'  => $attachment[1] . 'px',
221
-				'height' => $attachment[2] . 'px',
220
+				'width'  => $attachment[1].'px',
221
+				'height' => $attachment[2].'px',
222 222
 			);
223
-		}, array_merge( $ids, $embeds, $gallery ) );
223
+		}, array_merge($ids, $embeds, $gallery));
224 224
 
225
-		if ( 0 < sizeof( $images ) ) {
225
+		if (0 < sizeof($images)) {
226 226
 			$jsonld['image'] = $images;
227 227
 		};
228 228
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-timeline-service.php 2 patches
Indentation   +330 added lines, -330 removed lines patch added patch discarded remove patch
@@ -7,335 +7,335 @@
 block discarded – undo
7 7
  */
8 8
 class Wordlift_Timeline_Service {
9 9
 
10
-	/**
11
-	 * The Log service.
12
-	 *
13
-	 * @since  3.1.0
14
-	 * @access private
15
-	 * @var \Wordlift_Log_Service $log_service The Log service.
16
-	 */
17
-	private $log_service;
18
-
19
-	/**
20
-	 * The Entity service.
21
-	 *
22
-	 * @since  3.1.0
23
-	 * @access private
24
-	 * @var \Wordlift_Entity_Service $entity_service The Entity service.
25
-	 */
26
-	private $entity_service;
27
-
28
-	/**
29
-	 * The number of words to use for the excerpt, set in the `to_json` function
30
-	 * and used by a filter.
31
-	 *
32
-	 * @since  3.7.0
33
-	 * @access private
34
-	 * @var int $excerpt_length The number of words to use for the excerpt.
35
-	 */
36
-	private $excerpt_length;
37
-
38
-	/**
39
-	 * A singleton instance of the Timeline service (useful for unit tests).
40
-	 *
41
-	 * @since  3.1.0
42
-	 * @access private
43
-	 * @var \Wordlift_Timeline_Service $instance The singleton instance.
44
-	 */
45
-	private static $instance;
46
-
47
-	/**
48
-	 * Create a Wordlift_Timeline_Service instance.
49
-	 *
50
-	 * @since 3.1.0
51
-	 *
52
-	 * @param \Wordlift_Entity_Service $entity_service The Entity service.
53
-	 */
54
-	public function __construct( $entity_service ) {
55
-
56
-		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' );
57
-
58
-		$this->entity_service = $entity_service;
59
-
60
-		self::$instance = $this;
61
-
62
-	}
63
-
64
-	/**
65
-	 * Get the singleton instance of the Wordlift_Timeline_Service
66
-	 *
67
-	 * @since 3.1.0
68
-	 *
69
-	 * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service.
70
-	 */
71
-	public static function get_instance() {
72
-
73
-		return self::$instance;
74
-	}
75
-
76
-	/**
77
-	 * Retrieve timeline events and output them in JSON.
78
-	 *
79
-	 * @since 3.1.0
80
-	 */
81
-	public function ajax_timeline() {
82
-
83
-		// Get the ID of the post who requested the timeline.
84
-		$post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
85
-
86
-		// Get the events and transform them for the JSON response, then send them to the client.
87
-		wp_send_json( $this->to_json( $this->get_events( $post_id ) ) );
88
-
89
-	}
90
-
91
-	/**
92
-	 * Retrieve timeline events.
93
-	 *
94
-	 * @since 3.1.0
95
-	 *
96
-	 * @uses  wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post.
97
-	 *
98
-	 * @param int $post_id The post ID.
99
-	 *
100
-	 * @return array An array of event posts.
101
-	 */
102
-	public function get_events( $post_id = null ) {
103
-
104
-		// Get the entity IDs either from the entities related to the specified post or from the last 50 published
105
-		// posts if no post has been specified.
106
-		$ids = ( is_numeric( $post_id )
107
-			? wl_core_get_related_entity_ids( $post_id )
108
-			: $this->get_all_related_to_last_50_published_posts() );
109
-
110
-		// Add the post itself if it's an entity.
111
-		if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) {
112
-			$ids[] = $post_id;
113
-		}
114
-
115
-		// If there's no entities, return an empty array right away.
116
-		if ( 0 === sizeof( $ids ) ) {
117
-			$this->log_service->trace( "No events found [ post id :: $post_id ]" );
118
-
119
-			return array();
120
-		}
121
-
122
-		$this->log_service->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' );
123
-
124
-		return get_posts( array(
125
-			'post__in'       => $ids,
126
-			'post_type'      => Wordlift_Entity_Service::TYPE_NAME,
127
-			'post_status'    => 'publish',
128
-			'posts_per_page' => - 1,
129
-			'meta_query'     => array(
130
-				'relation' => 'AND',
131
-				array(
132
-					'key'     => Wordlift_Schema_Service::FIELD_DATE_START,
133
-					'value'   => null,
134
-					'compare' => '!=',
135
-				),
136
-				array(
137
-					'key'     => Wordlift_Schema_Service::FIELD_DATE_END,
138
-					'value'   => null,
139
-					'compare' => '!=',
140
-				),
141
-			),
142
-			'tax_query'      => array(
143
-				'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
144
-				'field'    => 'slug',
145
-				'terms'    => 'event',
146
-			),
147
-		) );
148
-	}
149
-
150
-	/**
151
-	 * Convert timeline events to JSON. This function sets the global post in order
152
-	 * to get an automatic excerpt. Since we're being called inside an AJAX request,
153
-	 * we're not taking care of restoring any previous post: there isn't any.
154
-	 *
155
-	 * @since 3.1.0
156
-	 *
157
-	 * @param array $posts An array of posts.
158
-	 *
159
-	 * @return array|string An array of timeline events or an empty string if no posts are provided.
160
-	 */
161
-	public function to_json( $posts ) {
162
-
163
-		// If there are no events, return empty JSON
164
-		if ( empty( $posts ) || is_null( $posts ) ) {
165
-			return '';
166
-		}
167
-
168
-		// {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background.
169
-		$display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media';
170
-
171
-		// The number of words for the excerpt (by default 55, as WordPress).
172
-		$this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55;
173
-		add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
174
-
175
-		// Add a filter to remove the [...] after excerpts, since we're adding
176
-		// a link to the post itself.
177
-		add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) );
178
-
179
-		// Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates.
180
-		$start_at_slide = 0;
181
-		$event_index    = - 1;
182
-		$now            = time();
183
-
184
-		// Prepare the timeline variable.
185
-		$timeline = array();
186
-
187
-		// Populate the arrays.
188
-		$timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) {
189
-
190
-			// Get the start and end dates.
191
-			$start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) );
192
-			$end_date   = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) );
193
-
194
-			// Set the starting slide.
195
-			$event_index ++;
196
-			if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) {
197
-				$start_at_slide = $event_index;
198
-			}
199
-
200
-			// Load thumbnail
201
-			if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $item->ID ) )
202
-			     && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) )
203
-			) {
204
-
205
-				// Set the thumbnail URL.
206
-				if ( 'background' === $display_images_as ) {
207
-					$date['background'] = array( 'url' => $attachment[0] );
208
-					$date['media']      = array( 'thumbnail' => $attachment[0] );
209
-				} else {
210
-					$date['media'] = array(
211
-						'url'       => $attachment[0],
212
-						'thumbnail' => $attachment[0],
213
-					);
214
-				}
215
-			}
216
-
217
-			// Set the start/end dates by converting them to TimelineJS required format.
218
-			$date['start_date'] = Wordlift_Timeline_Service::date( $start_date );
219
-			$date['end_date']   = Wordlift_Timeline_Service::date( $end_date );
220
-
221
-			setup_postdata( $GLOBALS['post'] = $item );
222
-
223
-			$more_link_text = sprintf(
224
-				'<span aria-label="%1$s">%2$s</span>',
225
-				sprintf(
226
-				/* translators: %s: Name of current post */
227
-					__( 'Continue reading %s' ),
228
-					the_title_attribute( array( 'echo' => false ) )
229
-				),
230
-				__( '(more&hellip;)' )
231
-			);
232
-
233
-			// Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352).
234
-			$date['text'] = array(
235
-				'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>',
236
-			);
237
-
238
-			// If we have an excerpt, set it.
239
-			if ( 0 < $excerpt_length ) {
240
-				$date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text );
241
-			}
242
-
243
-			return $date;
244
-
245
-		}, $posts );
246
-
247
-		// Finally remove the excerpt filter.
248
-		remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
249
-
250
-		// The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html
251
-		return array(
252
-			'timeline'       => $timeline,
253
-			'start_at_slide' => $start_at_slide,
254
-		);
255
-	}
256
-
257
-	/**
258
-	 * This function filters {@link excerpt_more} by removing it, since we're
259
-	 * adding the 'read more' link. This filter is set by {@see to_json}.
260
-	 *
261
-	 * @since 3.7.0
262
-	 *
263
-	 * @param string $excerpt_more The excerpt more preset.
264
-	 *
265
-	 * @return string An empty string.
266
-	 */
267
-	public function excerpt_more( $excerpt_more ) {
268
-
269
-		return '';
270
-	}
271
-
272
-	/**
273
-	 * A filter for the excerpt length, set by the `to_json` function, to tailor
274
-	 * how many words to return according to the client setting.
275
-	 *
276
-	 * @since 3.7.0
277
-	 *
278
-	 * @param int $length The preset number of words.
279
-	 *
280
-	 * @return int The number of words for the preset.
281
-	 */
282
-	public function excerpt_length( $length ) {
283
-
284
-		return $this->excerpt_length;
285
-	}
286
-
287
-
288
-	/**
289
-	 * Convert the date to a date array.
290
-	 *
291
-	 * @since 3.7.0
292
-	 *
293
-	 * @param $value int A date value.
294
-	 *
295
-	 * @return array An array containing year, month and day values.
296
-	 */
297
-	public static function date( $value ) {
298
-
299
-		return array(
300
-			'year'  => (int) date( 'Y', $value ),
301
-			'month' => (int) date( 'm', $value ),
302
-			'day'   => (int) date( 'd', $value ),
303
-
304
-		);
305
-	}
306
-
307
-	/**
308
-	 * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to
309
-	 * its specific function).
310
-	 *
311
-	 * @since 3.1.0
312
-	 *
313
-	 * @return array An array of post IDs.
314
-	 */
315
-	public function get_all_related_to_last_50_published_posts() {
316
-
317
-		// Global timeline. Get entities from the latest posts.
318
-		$latest_posts_ids = get_posts( array(
319
-			'numberposts' => 50,
320
-			'fields'      => 'ids', //only get post IDs
321
-			'post_type'   => array( 'post', 'page' ),
322
-			'post_status' => 'publish',
323
-		) );
324
-
325
-		if ( empty( $latest_posts_ids ) ) {
326
-			// There are no posts.
327
-			return array();
328
-		}
329
-
330
-		// Collect entities related to latest posts
331
-		$entity_ids = array();
332
-		foreach ( $latest_posts_ids as $id ) {
333
-			$entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array(
334
-				'status' => 'publish',
335
-			) ) );
336
-		}
337
-
338
-		return $entity_ids;
339
-	}
10
+    /**
11
+     * The Log service.
12
+     *
13
+     * @since  3.1.0
14
+     * @access private
15
+     * @var \Wordlift_Log_Service $log_service The Log service.
16
+     */
17
+    private $log_service;
18
+
19
+    /**
20
+     * The Entity service.
21
+     *
22
+     * @since  3.1.0
23
+     * @access private
24
+     * @var \Wordlift_Entity_Service $entity_service The Entity service.
25
+     */
26
+    private $entity_service;
27
+
28
+    /**
29
+     * The number of words to use for the excerpt, set in the `to_json` function
30
+     * and used by a filter.
31
+     *
32
+     * @since  3.7.0
33
+     * @access private
34
+     * @var int $excerpt_length The number of words to use for the excerpt.
35
+     */
36
+    private $excerpt_length;
37
+
38
+    /**
39
+     * A singleton instance of the Timeline service (useful for unit tests).
40
+     *
41
+     * @since  3.1.0
42
+     * @access private
43
+     * @var \Wordlift_Timeline_Service $instance The singleton instance.
44
+     */
45
+    private static $instance;
46
+
47
+    /**
48
+     * Create a Wordlift_Timeline_Service instance.
49
+     *
50
+     * @since 3.1.0
51
+     *
52
+     * @param \Wordlift_Entity_Service $entity_service The Entity service.
53
+     */
54
+    public function __construct( $entity_service ) {
55
+
56
+        $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' );
57
+
58
+        $this->entity_service = $entity_service;
59
+
60
+        self::$instance = $this;
61
+
62
+    }
63
+
64
+    /**
65
+     * Get the singleton instance of the Wordlift_Timeline_Service
66
+     *
67
+     * @since 3.1.0
68
+     *
69
+     * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service.
70
+     */
71
+    public static function get_instance() {
72
+
73
+        return self::$instance;
74
+    }
75
+
76
+    /**
77
+     * Retrieve timeline events and output them in JSON.
78
+     *
79
+     * @since 3.1.0
80
+     */
81
+    public function ajax_timeline() {
82
+
83
+        // Get the ID of the post who requested the timeline.
84
+        $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
85
+
86
+        // Get the events and transform them for the JSON response, then send them to the client.
87
+        wp_send_json( $this->to_json( $this->get_events( $post_id ) ) );
88
+
89
+    }
90
+
91
+    /**
92
+     * Retrieve timeline events.
93
+     *
94
+     * @since 3.1.0
95
+     *
96
+     * @uses  wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post.
97
+     *
98
+     * @param int $post_id The post ID.
99
+     *
100
+     * @return array An array of event posts.
101
+     */
102
+    public function get_events( $post_id = null ) {
103
+
104
+        // Get the entity IDs either from the entities related to the specified post or from the last 50 published
105
+        // posts if no post has been specified.
106
+        $ids = ( is_numeric( $post_id )
107
+            ? wl_core_get_related_entity_ids( $post_id )
108
+            : $this->get_all_related_to_last_50_published_posts() );
109
+
110
+        // Add the post itself if it's an entity.
111
+        if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) {
112
+            $ids[] = $post_id;
113
+        }
114
+
115
+        // If there's no entities, return an empty array right away.
116
+        if ( 0 === sizeof( $ids ) ) {
117
+            $this->log_service->trace( "No events found [ post id :: $post_id ]" );
118
+
119
+            return array();
120
+        }
121
+
122
+        $this->log_service->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' );
123
+
124
+        return get_posts( array(
125
+            'post__in'       => $ids,
126
+            'post_type'      => Wordlift_Entity_Service::TYPE_NAME,
127
+            'post_status'    => 'publish',
128
+            'posts_per_page' => - 1,
129
+            'meta_query'     => array(
130
+                'relation' => 'AND',
131
+                array(
132
+                    'key'     => Wordlift_Schema_Service::FIELD_DATE_START,
133
+                    'value'   => null,
134
+                    'compare' => '!=',
135
+                ),
136
+                array(
137
+                    'key'     => Wordlift_Schema_Service::FIELD_DATE_END,
138
+                    'value'   => null,
139
+                    'compare' => '!=',
140
+                ),
141
+            ),
142
+            'tax_query'      => array(
143
+                'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME,
144
+                'field'    => 'slug',
145
+                'terms'    => 'event',
146
+            ),
147
+        ) );
148
+    }
149
+
150
+    /**
151
+     * Convert timeline events to JSON. This function sets the global post in order
152
+     * to get an automatic excerpt. Since we're being called inside an AJAX request,
153
+     * we're not taking care of restoring any previous post: there isn't any.
154
+     *
155
+     * @since 3.1.0
156
+     *
157
+     * @param array $posts An array of posts.
158
+     *
159
+     * @return array|string An array of timeline events or an empty string if no posts are provided.
160
+     */
161
+    public function to_json( $posts ) {
162
+
163
+        // If there are no events, return empty JSON
164
+        if ( empty( $posts ) || is_null( $posts ) ) {
165
+            return '';
166
+        }
167
+
168
+        // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background.
169
+        $display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media';
170
+
171
+        // The number of words for the excerpt (by default 55, as WordPress).
172
+        $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55;
173
+        add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
174
+
175
+        // Add a filter to remove the [...] after excerpts, since we're adding
176
+        // a link to the post itself.
177
+        add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) );
178
+
179
+        // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates.
180
+        $start_at_slide = 0;
181
+        $event_index    = - 1;
182
+        $now            = time();
183
+
184
+        // Prepare the timeline variable.
185
+        $timeline = array();
186
+
187
+        // Populate the arrays.
188
+        $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) {
189
+
190
+            // Get the start and end dates.
191
+            $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) );
192
+            $end_date   = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) );
193
+
194
+            // Set the starting slide.
195
+            $event_index ++;
196
+            if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) {
197
+                $start_at_slide = $event_index;
198
+            }
199
+
200
+            // Load thumbnail
201
+            if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $item->ID ) )
202
+                 && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) )
203
+            ) {
204
+
205
+                // Set the thumbnail URL.
206
+                if ( 'background' === $display_images_as ) {
207
+                    $date['background'] = array( 'url' => $attachment[0] );
208
+                    $date['media']      = array( 'thumbnail' => $attachment[0] );
209
+                } else {
210
+                    $date['media'] = array(
211
+                        'url'       => $attachment[0],
212
+                        'thumbnail' => $attachment[0],
213
+                    );
214
+                }
215
+            }
216
+
217
+            // Set the start/end dates by converting them to TimelineJS required format.
218
+            $date['start_date'] = Wordlift_Timeline_Service::date( $start_date );
219
+            $date['end_date']   = Wordlift_Timeline_Service::date( $end_date );
220
+
221
+            setup_postdata( $GLOBALS['post'] = $item );
222
+
223
+            $more_link_text = sprintf(
224
+                '<span aria-label="%1$s">%2$s</span>',
225
+                sprintf(
226
+                /* translators: %s: Name of current post */
227
+                    __( 'Continue reading %s' ),
228
+                    the_title_attribute( array( 'echo' => false ) )
229
+                ),
230
+                __( '(more&hellip;)' )
231
+            );
232
+
233
+            // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352).
234
+            $date['text'] = array(
235
+                'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>',
236
+            );
237
+
238
+            // If we have an excerpt, set it.
239
+            if ( 0 < $excerpt_length ) {
240
+                $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text );
241
+            }
242
+
243
+            return $date;
244
+
245
+        }, $posts );
246
+
247
+        // Finally remove the excerpt filter.
248
+        remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
249
+
250
+        // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html
251
+        return array(
252
+            'timeline'       => $timeline,
253
+            'start_at_slide' => $start_at_slide,
254
+        );
255
+    }
256
+
257
+    /**
258
+     * This function filters {@link excerpt_more} by removing it, since we're
259
+     * adding the 'read more' link. This filter is set by {@see to_json}.
260
+     *
261
+     * @since 3.7.0
262
+     *
263
+     * @param string $excerpt_more The excerpt more preset.
264
+     *
265
+     * @return string An empty string.
266
+     */
267
+    public function excerpt_more( $excerpt_more ) {
268
+
269
+        return '';
270
+    }
271
+
272
+    /**
273
+     * A filter for the excerpt length, set by the `to_json` function, to tailor
274
+     * how many words to return according to the client setting.
275
+     *
276
+     * @since 3.7.0
277
+     *
278
+     * @param int $length The preset number of words.
279
+     *
280
+     * @return int The number of words for the preset.
281
+     */
282
+    public function excerpt_length( $length ) {
283
+
284
+        return $this->excerpt_length;
285
+    }
286
+
287
+
288
+    /**
289
+     * Convert the date to a date array.
290
+     *
291
+     * @since 3.7.0
292
+     *
293
+     * @param $value int A date value.
294
+     *
295
+     * @return array An array containing year, month and day values.
296
+     */
297
+    public static function date( $value ) {
298
+
299
+        return array(
300
+            'year'  => (int) date( 'Y', $value ),
301
+            'month' => (int) date( 'm', $value ),
302
+            'day'   => (int) date( 'd', $value ),
303
+
304
+        );
305
+    }
306
+
307
+    /**
308
+     * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to
309
+     * its specific function).
310
+     *
311
+     * @since 3.1.0
312
+     *
313
+     * @return array An array of post IDs.
314
+     */
315
+    public function get_all_related_to_last_50_published_posts() {
316
+
317
+        // Global timeline. Get entities from the latest posts.
318
+        $latest_posts_ids = get_posts( array(
319
+            'numberposts' => 50,
320
+            'fields'      => 'ids', //only get post IDs
321
+            'post_type'   => array( 'post', 'page' ),
322
+            'post_status' => 'publish',
323
+        ) );
324
+
325
+        if ( empty( $latest_posts_ids ) ) {
326
+            // There are no posts.
327
+            return array();
328
+        }
329
+
330
+        // Collect entities related to latest posts
331
+        $entity_ids = array();
332
+        foreach ( $latest_posts_ids as $id ) {
333
+            $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array(
334
+                'status' => 'publish',
335
+            ) ) );
336
+        }
337
+
338
+        return $entity_ids;
339
+    }
340 340
 
341 341
 }
Please login to merge, or discard this patch.
Spacing   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -51,9 +51,9 @@  discard block
 block discarded – undo
51 51
 	 *
52 52
 	 * @param \Wordlift_Entity_Service $entity_service The Entity service.
53 53
 	 */
54
-	public function __construct( $entity_service ) {
54
+	public function __construct($entity_service) {
55 55
 
56
-		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' );
56
+		$this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Timeline_Service');
57 57
 
58 58
 		$this->entity_service = $entity_service;
59 59
 
@@ -81,10 +81,10 @@  discard block
 block discarded – undo
81 81
 	public function ajax_timeline() {
82 82
 
83 83
 		// Get the ID of the post who requested the timeline.
84
-		$post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
84
+		$post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : null);
85 85
 
86 86
 		// Get the events and transform them for the JSON response, then send them to the client.
87
-		wp_send_json( $this->to_json( $this->get_events( $post_id ) ) );
87
+		wp_send_json($this->to_json($this->get_events($post_id)));
88 88
 
89 89
 	}
90 90
 
@@ -99,33 +99,33 @@  discard block
 block discarded – undo
99 99
 	 *
100 100
 	 * @return array An array of event posts.
101 101
 	 */
102
-	public function get_events( $post_id = null ) {
102
+	public function get_events($post_id = null) {
103 103
 
104 104
 		// Get the entity IDs either from the entities related to the specified post or from the last 50 published
105 105
 		// posts if no post has been specified.
106
-		$ids = ( is_numeric( $post_id )
107
-			? wl_core_get_related_entity_ids( $post_id )
108
-			: $this->get_all_related_to_last_50_published_posts() );
106
+		$ids = (is_numeric($post_id)
107
+			? wl_core_get_related_entity_ids($post_id)
108
+			: $this->get_all_related_to_last_50_published_posts());
109 109
 
110 110
 		// Add the post itself if it's an entity.
111
-		if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) {
111
+		if (is_numeric($post_id) && $this->entity_service->is_entity($post_id)) {
112 112
 			$ids[] = $post_id;
113 113
 		}
114 114
 
115 115
 		// If there's no entities, return an empty array right away.
116
-		if ( 0 === sizeof( $ids ) ) {
117
-			$this->log_service->trace( "No events found [ post id :: $post_id ]" );
116
+		if (0 === sizeof($ids)) {
117
+			$this->log_service->trace("No events found [ post id :: $post_id ]");
118 118
 
119 119
 			return array();
120 120
 		}
121 121
 
122
-		$this->log_service->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' );
122
+		$this->log_service->trace('Getting events [ entity ids :: '.join(', ', $ids).' ]');
123 123
 
124
-		return get_posts( array(
124
+		return get_posts(array(
125 125
 			'post__in'       => $ids,
126 126
 			'post_type'      => Wordlift_Entity_Service::TYPE_NAME,
127 127
 			'post_status'    => 'publish',
128
-			'posts_per_page' => - 1,
128
+			'posts_per_page' => -1,
129 129
 			'meta_query'     => array(
130 130
 				'relation' => 'AND',
131 131
 				array(
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
 				'field'    => 'slug',
145 145
 				'terms'    => 'event',
146 146
 			),
147
-		) );
147
+		));
148 148
 	}
149 149
 
150 150
 	/**
@@ -158,23 +158,23 @@  discard block
 block discarded – undo
158 158
 	 *
159 159
 	 * @return array|string An array of timeline events or an empty string if no posts are provided.
160 160
 	 */
161
-	public function to_json( $posts ) {
161
+	public function to_json($posts) {
162 162
 
163 163
 		// If there are no events, return empty JSON
164
-		if ( empty( $posts ) || is_null( $posts ) ) {
164
+		if (empty($posts) || is_null($posts)) {
165 165
 			return '';
166 166
 		}
167 167
 
168 168
 		// {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background.
169
-		$display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media';
169
+		$display_images_as = isset($_REQUEST['display_images_as']) ? $_REQUEST['display_images_as'] : 'media';
170 170
 
171 171
 		// The number of words for the excerpt (by default 55, as WordPress).
172
-		$this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55;
173
-		add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
172
+		$this->excerpt_length = $excerpt_length = isset($_REQUEST['excerpt_length']) && is_numeric($_REQUEST['excerpt_length']) ? $_REQUEST['excerpt_length'] : 55;
173
+		add_filter('excerpt_length', array($this, 'excerpt_length'));
174 174
 
175 175
 		// Add a filter to remove the [...] after excerpts, since we're adding
176 176
 		// a link to the post itself.
177
-		add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) );
177
+		add_filter('excerpt_more', array($this, 'excerpt_more'));
178 178
 
179 179
 		// Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates.
180 180
 		$start_at_slide = 0;
@@ -185,27 +185,27 @@  discard block
 block discarded – undo
185 185
 		$timeline = array();
186 186
 
187 187
 		// Populate the arrays.
188
-		$timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) {
188
+		$timeline['events'] = array_map(function($item) use (&$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length) {
189 189
 
190 190
 			// Get the start and end dates.
191
-			$start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) );
192
-			$end_date   = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) );
191
+			$start_date = strtotime(get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true));
192
+			$end_date   = strtotime(get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true));
193 193
 
194 194
 			// Set the starting slide.
195
-			$event_index ++;
196
-			if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) {
195
+			$event_index++;
196
+			if (0 === $start_at_slide && $now >= $start_date && $now <= $end_date) {
197 197
 				$start_at_slide = $event_index;
198 198
 			}
199 199
 
200 200
 			// Load thumbnail
201
-			if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $item->ID ) )
202
-			     && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) )
201
+			if ('' !== ($thumbnail_id = get_post_thumbnail_id($item->ID))
202
+			     && false !== ($attachment = wp_get_attachment_image_src($thumbnail_id))
203 203
 			) {
204 204
 
205 205
 				// Set the thumbnail URL.
206
-				if ( 'background' === $display_images_as ) {
207
-					$date['background'] = array( 'url' => $attachment[0] );
208
-					$date['media']      = array( 'thumbnail' => $attachment[0] );
206
+				if ('background' === $display_images_as) {
207
+					$date['background'] = array('url' => $attachment[0]);
208
+					$date['media']      = array('thumbnail' => $attachment[0]);
209 209
 				} else {
210 210
 					$date['media'] = array(
211 211
 						'url'       => $attachment[0],
@@ -215,37 +215,37 @@  discard block
 block discarded – undo
215 215
 			}
216 216
 
217 217
 			// Set the start/end dates by converting them to TimelineJS required format.
218
-			$date['start_date'] = Wordlift_Timeline_Service::date( $start_date );
219
-			$date['end_date']   = Wordlift_Timeline_Service::date( $end_date );
218
+			$date['start_date'] = Wordlift_Timeline_Service::date($start_date);
219
+			$date['end_date']   = Wordlift_Timeline_Service::date($end_date);
220 220
 
221
-			setup_postdata( $GLOBALS['post'] = $item );
221
+			setup_postdata($GLOBALS['post'] = $item);
222 222
 
223 223
 			$more_link_text = sprintf(
224 224
 				'<span aria-label="%1$s">%2$s</span>',
225 225
 				sprintf(
226 226
 				/* translators: %s: Name of current post */
227
-					__( 'Continue reading %s' ),
228
-					the_title_attribute( array( 'echo' => false ) )
227
+					__('Continue reading %s'),
228
+					the_title_attribute(array('echo' => false))
229 229
 				),
230
-				__( '(more&hellip;)' )
230
+				__('(more&hellip;)')
231 231
 			);
232 232
 
233 233
 			// Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352).
234 234
 			$date['text'] = array(
235
-				'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>',
235
+				'headline' => '<a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>',
236 236
 			);
237 237
 
238 238
 			// If we have an excerpt, set it.
239
-			if ( 0 < $excerpt_length ) {
240
-				$date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text );
239
+			if (0 < $excerpt_length) {
240
+				$date['text']['text'] = sprintf('%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text);
241 241
 			}
242 242
 
243 243
 			return $date;
244 244
 
245
-		}, $posts );
245
+		}, $posts);
246 246
 
247 247
 		// Finally remove the excerpt filter.
248
-		remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
248
+		remove_filter('excerpt_length', array($this, 'excerpt_length'));
249 249
 
250 250
 		// The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html
251 251
 		return array(
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
 	 *
265 265
 	 * @return string An empty string.
266 266
 	 */
267
-	public function excerpt_more( $excerpt_more ) {
267
+	public function excerpt_more($excerpt_more) {
268 268
 
269 269
 		return '';
270 270
 	}
@@ -279,7 +279,7 @@  discard block
 block discarded – undo
279 279
 	 *
280 280
 	 * @return int The number of words for the preset.
281 281
 	 */
282
-	public function excerpt_length( $length ) {
282
+	public function excerpt_length($length) {
283 283
 
284 284
 		return $this->excerpt_length;
285 285
 	}
@@ -294,12 +294,12 @@  discard block
 block discarded – undo
294 294
 	 *
295 295
 	 * @return array An array containing year, month and day values.
296 296
 	 */
297
-	public static function date( $value ) {
297
+	public static function date($value) {
298 298
 
299 299
 		return array(
300
-			'year'  => (int) date( 'Y', $value ),
301
-			'month' => (int) date( 'm', $value ),
302
-			'day'   => (int) date( 'd', $value ),
300
+			'year'  => (int) date('Y', $value),
301
+			'month' => (int) date('m', $value),
302
+			'day'   => (int) date('d', $value),
303 303
 
304 304
 		);
305 305
 	}
@@ -315,24 +315,24 @@  discard block
 block discarded – undo
315 315
 	public function get_all_related_to_last_50_published_posts() {
316 316
 
317 317
 		// Global timeline. Get entities from the latest posts.
318
-		$latest_posts_ids = get_posts( array(
318
+		$latest_posts_ids = get_posts(array(
319 319
 			'numberposts' => 50,
320 320
 			'fields'      => 'ids', //only get post IDs
321
-			'post_type'   => array( 'post', 'page' ),
321
+			'post_type'   => array('post', 'page'),
322 322
 			'post_status' => 'publish',
323
-		) );
323
+		));
324 324
 
325
-		if ( empty( $latest_posts_ids ) ) {
325
+		if (empty($latest_posts_ids)) {
326 326
 			// There are no posts.
327 327
 			return array();
328 328
 		}
329 329
 
330 330
 		// Collect entities related to latest posts
331 331
 		$entity_ids = array();
332
-		foreach ( $latest_posts_ids as $id ) {
333
-			$entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array(
332
+		foreach ($latest_posts_ids as $id) {
333
+			$entity_ids = array_merge($entity_ids, wl_core_get_related_entity_ids($id, array(
334 334
 				'status' => 'publish',
335
-			) ) );
335
+			)));
336 336
 		}
337 337
 
338 338
 		return $entity_ids;
Please login to merge, or discard this patch.