Completed
Pull Request — develop (#900)
by William
04:03
created
src/admin/wordlift_admin_edit_post.php 2 patches
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -16,29 +16,29 @@
 block discarded – undo
16 16
  */
17 17
 function wl_admin_permalink_html( $html, $post_id, $new_title, $new_slug ) {
18 18
 
19
-	// Get the entity service instance.
20
-	$entity_service = Wordlift_Entity_Service::get_instance();
19
+    // Get the entity service instance.
20
+    $entity_service = Wordlift_Entity_Service::get_instance();
21 21
 
22
-	// Show the View Linked Data button only for entities.
23
-	//
24
-	// See https://github.com/insideout10/wordlift-plugin/issues/668.
25
-	if ( 'publish' === get_post_status( $post_id )
26
-		 && $uri = $entity_service->get_uri( $post_id ) ) {
22
+    // Show the View Linked Data button only for entities.
23
+    //
24
+    // See https://github.com/insideout10/wordlift-plugin/issues/668.
25
+    if ( 'publish' === get_post_status( $post_id )
26
+         && $uri = $entity_service->get_uri( $post_id ) ) {
27 27
 
28
-		$lod_view_href = 'http://lodview.it/lodview/?IRI=' . urlencode( $uri );
29
-		$html          .= "<span id='view-post-btn'><a href='$lod_view_href' class='button button-small wl-button' target='_blank'>" .
30
-						  esc_html__( 'View Linked Data', 'wordlift' ) .
31
-						  "</a></span>\n";
28
+        $lod_view_href = 'http://lodview.it/lodview/?IRI=' . urlencode( $uri );
29
+        $html          .= "<span id='view-post-btn'><a href='$lod_view_href' class='button button-small wl-button' target='_blank'>" .
30
+                            esc_html__( 'View Linked Data', 'wordlift' ) .
31
+                            "</a></span>\n";
32 32
 
33
-	}
33
+    }
34 34
 
35
-	$html .= "<span id='view-post-btn'><a href='" . WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL .
36
-			 urlencode( get_permalink( $post_id ) ) .
37
-			 "' class='button button-small wl-button' target='_blank'>" .
38
-			 esc_html__( 'Test Google Rich Snippets', 'wordlift' ) .
39
-			 "</a></span>\n";
35
+    $html .= "<span id='view-post-btn'><a href='" . WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL .
36
+                urlencode( get_permalink( $post_id ) ) .
37
+                "' class='button button-small wl-button' target='_blank'>" .
38
+                esc_html__( 'Test Google Rich Snippets', 'wordlift' ) .
39
+                "</a></span>\n";
40 40
 
41
-	return $html;
41
+    return $html;
42 42
 }
43 43
 
44 44
 add_filter( 'get_sample_permalink_html', 'wl_admin_permalink_html', PHP_INT_MAX, 4 );
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
14 14
  *
15 15
  * @return string The enhanced html.
16 16
  */
17
-function wl_admin_permalink_html( $html, $post_id, $new_title, $new_slug ) {
17
+function wl_admin_permalink_html($html, $post_id, $new_title, $new_slug) {
18 18
 
19 19
 	// Get the entity service instance.
20 20
 	$entity_service = Wordlift_Entity_Service::get_instance();
@@ -22,23 +22,23 @@  discard block
 block discarded – undo
22 22
 	// Show the View Linked Data button only for entities.
23 23
 	//
24 24
 	// See https://github.com/insideout10/wordlift-plugin/issues/668.
25
-	if ( 'publish' === get_post_status( $post_id )
26
-		 && $uri = $entity_service->get_uri( $post_id ) ) {
25
+	if ('publish' === get_post_status($post_id)
26
+		 && $uri = $entity_service->get_uri($post_id)) {
27 27
 
28
-		$lod_view_href = 'http://lodview.it/lodview/?IRI=' . urlencode( $uri );
29
-		$html          .= "<span id='view-post-btn'><a href='$lod_view_href' class='button button-small wl-button' target='_blank'>" .
30
-						  esc_html__( 'View Linked Data', 'wordlift' ) .
28
+		$lod_view_href = 'http://lodview.it/lodview/?IRI='.urlencode($uri);
29
+		$html .= "<span id='view-post-btn'><a href='$lod_view_href' class='button button-small wl-button' target='_blank'>".
30
+						  esc_html__('View Linked Data', 'wordlift').
31 31
 						  "</a></span>\n";
32 32
 
33 33
 	}
34 34
 
35
-	$html .= "<span id='view-post-btn'><a href='" . WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL .
36
-			 urlencode( get_permalink( $post_id ) ) .
37
-			 "' class='button button-small wl-button' target='_blank'>" .
38
-			 esc_html__( 'Test Google Rich Snippets', 'wordlift' ) .
35
+	$html .= "<span id='view-post-btn'><a href='".WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL.
36
+			 urlencode(get_permalink($post_id)).
37
+			 "' class='button button-small wl-button' target='_blank'>".
38
+			 esc_html__('Test Google Rich Snippets', 'wordlift').
39 39
 			 "</a></span>\n";
40 40
 
41 41
 	return $html;
42 42
 }
43 43
 
44
-add_filter( 'get_sample_permalink_html', 'wl_admin_permalink_html', PHP_INT_MAX, 4 );
44
+add_filter('get_sample_permalink_html', 'wl_admin_permalink_html', PHP_INT_MAX, 4);
Please login to merge, or discard this patch.
src/admin/wordlift_admin.php 2 patches
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -17,28 +17,28 @@  discard block
 block discarded – undo
17 17
  */
18 18
 function wl_serialize_entity( $entity ) {
19 19
 
20
-	$entity = ( is_numeric( $entity ) ) ? get_post( $entity ) : $entity;
20
+    $entity = ( is_numeric( $entity ) ) ? get_post( $entity ) : $entity;
21 21
 
22
-	// Bail if the entity doesn't exists.
23
-	// In some cases we have `wl_topic` meta
24
-	// pointing to an entity that has been deleted.
25
-	if ( empty( $entity ) ) {
26
-		return;
27
-	}
22
+    // Bail if the entity doesn't exists.
23
+    // In some cases we have `wl_topic` meta
24
+    // pointing to an entity that has been deleted.
25
+    if ( empty( $entity ) ) {
26
+        return;
27
+    }
28 28
 
29
-	$type   = Wordlift_Entity_Type_Service::get_instance()->get( $entity->ID );
30
-	$images = wl_get_image_urls( $entity->ID );
29
+    $type   = Wordlift_Entity_Type_Service::get_instance()->get( $entity->ID );
30
+    $images = wl_get_image_urls( $entity->ID );
31 31
 
32
-	return array(
33
-		'id'          => wl_get_entity_uri( $entity->ID ),
34
-		'label'       => $entity->post_title,
35
-		'description' => $entity->post_content,
36
-		'sameAs'      => wl_schema_get_value( $entity->ID, 'sameAs' ),
37
-		'mainType'    => str_replace( 'wl-', '', $type['css_class'] ),
38
-		'types'       => wl_get_entity_rdf_types( $entity->ID ),
39
-		'images'      => $images,
32
+    return array(
33
+        'id'          => wl_get_entity_uri( $entity->ID ),
34
+        'label'       => $entity->post_title,
35
+        'description' => $entity->post_content,
36
+        'sameAs'      => wl_schema_get_value( $entity->ID, 'sameAs' ),
37
+        'mainType'    => str_replace( 'wl-', '', $type['css_class'] ),
38
+        'types'       => wl_get_entity_rdf_types( $entity->ID ),
39
+        'images'      => $images,
40 40
 
41
-	);
41
+    );
42 42
 }
43 43
 
44 44
 /**
@@ -52,27 +52,27 @@  discard block
 block discarded – undo
52 52
  */
53 53
 function wl_remove_text_annotations( $data ) {
54 54
 
55
-	// Remove blank elements that can interfere with annotations removing
56
-	// See https://github.com/insideout10/wordlift-plugin/issues/234
57
-	// Just blank attributes without any attributes are cleaned up.
58
-	$pattern = '/<(\w+)><\/\1>/im';
59
-	// Remove the pattern while it is found (match nested annotations).
60
-	while ( 1 === preg_match( $pattern, $data['post_content'] ) ) {
61
-		$data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count );
62
-	}
63
-	// Remove text annotations
64
-	// <span class="textannotation" id="urn:enhancement-777cbed4-b131-00fb-54a4-ed9b26ae57ea">.
55
+    // Remove blank elements that can interfere with annotations removing
56
+    // See https://github.com/insideout10/wordlift-plugin/issues/234
57
+    // Just blank attributes without any attributes are cleaned up.
58
+    $pattern = '/<(\w+)><\/\1>/im';
59
+    // Remove the pattern while it is found (match nested annotations).
60
+    while ( 1 === preg_match( $pattern, $data['post_content'] ) ) {
61
+        $data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count );
62
+    }
63
+    // Remove text annotations
64
+    // <span class="textannotation" id="urn:enhancement-777cbed4-b131-00fb-54a4-ed9b26ae57ea">.
65 65
 
66
-	// @see https://github.com/insideout10/wordlift-plugin/issues/771
67
-	// Changing this:
68
-	//	$pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?![^\\"]*\sdisambiguated)[^\\"]*\\\"[^>]*>([^<]+)<\/\1>/im';
69
-	// to:
70
-	$pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?![^\\"]*\sdisambiguated)[^\\"]*\\\"[^>]*>(.*?)<\/\1>/im';
71
-	// Remove the pattern while it is found (match nested annotations).
72
-	while ( 1 === preg_match( $pattern, $data['post_content'] ) ) {
73
-		$data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count );
74
-	}
75
-	return $data;
66
+    // @see https://github.com/insideout10/wordlift-plugin/issues/771
67
+    // Changing this:
68
+    //	$pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?![^\\"]*\sdisambiguated)[^\\"]*\\\"[^>]*>([^<]+)<\/\1>/im';
69
+    // to:
70
+    $pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?![^\\"]*\sdisambiguated)[^\\"]*\\\"[^>]*>(.*?)<\/\1>/im';
71
+    // Remove the pattern while it is found (match nested annotations).
72
+    while ( 1 === preg_match( $pattern, $data['post_content'] ) ) {
73
+        $data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count );
74
+    }
75
+    return $data;
76 76
 }
77 77
 
78 78
 add_filter( 'wp_insert_post_data', 'wl_remove_text_annotations', '98', 1 );
@@ -88,5 +88,5 @@  discard block
 block discarded – undo
88 88
  */
89 89
 function wl_admin_metaboxes_add_css_class( $classes = array() ) {
90 90
 
91
-	return array_merge( $classes, array( 'wl-metabox' ) );
91
+    return array_merge( $classes, array( 'wl-metabox' ) );
92 92
 }
Please login to merge, or discard this patch.
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
6 6
  */
7 7
 
8 8
 // Add the Admin menu.
9
-require_once( 'wordlift_admin_menu.php' );
9
+require_once('wordlift_admin_menu.php');
10 10
 
11 11
 /**
12 12
  * Serialize an entity post.
@@ -15,27 +15,27 @@  discard block
 block discarded – undo
15 15
  *
16 16
  * @return array mixed The entity data array.
17 17
  */
18
-function wl_serialize_entity( $entity ) {
18
+function wl_serialize_entity($entity) {
19 19
 
20
-	$entity = ( is_numeric( $entity ) ) ? get_post( $entity ) : $entity;
20
+	$entity = (is_numeric($entity)) ? get_post($entity) : $entity;
21 21
 
22 22
 	// Bail if the entity doesn't exists.
23 23
 	// In some cases we have `wl_topic` meta
24 24
 	// pointing to an entity that has been deleted.
25
-	if ( empty( $entity ) ) {
25
+	if (empty($entity)) {
26 26
 		return;
27 27
 	}
28 28
 
29
-	$type   = Wordlift_Entity_Type_Service::get_instance()->get( $entity->ID );
30
-	$images = wl_get_image_urls( $entity->ID );
29
+	$type   = Wordlift_Entity_Type_Service::get_instance()->get($entity->ID);
30
+	$images = wl_get_image_urls($entity->ID);
31 31
 
32 32
 	return array(
33
-		'id'          => wl_get_entity_uri( $entity->ID ),
33
+		'id'          => wl_get_entity_uri($entity->ID),
34 34
 		'label'       => $entity->post_title,
35 35
 		'description' => $entity->post_content,
36
-		'sameAs'      => wl_schema_get_value( $entity->ID, 'sameAs' ),
37
-		'mainType'    => str_replace( 'wl-', '', $type['css_class'] ),
38
-		'types'       => wl_get_entity_rdf_types( $entity->ID ),
36
+		'sameAs'      => wl_schema_get_value($entity->ID, 'sameAs'),
37
+		'mainType'    => str_replace('wl-', '', $type['css_class']),
38
+		'types'       => wl_get_entity_rdf_types($entity->ID),
39 39
 		'images'      => $images,
40 40
 
41 41
 	);
@@ -50,15 +50,15 @@  discard block
 block discarded – undo
50 50
  *
51 51
  * @return array mixed The post data array.
52 52
  */
53
-function wl_remove_text_annotations( $data ) {
53
+function wl_remove_text_annotations($data) {
54 54
 
55 55
 	// Remove blank elements that can interfere with annotations removing
56 56
 	// See https://github.com/insideout10/wordlift-plugin/issues/234
57 57
 	// Just blank attributes without any attributes are cleaned up.
58 58
 	$pattern = '/<(\w+)><\/\1>/im';
59 59
 	// Remove the pattern while it is found (match nested annotations).
60
-	while ( 1 === preg_match( $pattern, $data['post_content'] ) ) {
61
-		$data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count );
60
+	while (1 === preg_match($pattern, $data['post_content'])) {
61
+		$data['post_content'] = preg_replace($pattern, '$2', $data['post_content'], -1, $count);
62 62
 	}
63 63
 	// Remove text annotations
64 64
 	// <span class="textannotation" id="urn:enhancement-777cbed4-b131-00fb-54a4-ed9b26ae57ea">.
@@ -69,13 +69,13 @@  discard block
 block discarded – undo
69 69
 	// to:
70 70
 	$pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?![^\\"]*\sdisambiguated)[^\\"]*\\\"[^>]*>(.*?)<\/\1>/im';
71 71
 	// Remove the pattern while it is found (match nested annotations).
72
-	while ( 1 === preg_match( $pattern, $data['post_content'] ) ) {
73
-		$data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count );
72
+	while (1 === preg_match($pattern, $data['post_content'])) {
73
+		$data['post_content'] = preg_replace($pattern, '$2', $data['post_content'], -1, $count);
74 74
 	}
75 75
 	return $data;
76 76
 }
77 77
 
78
-add_filter( 'wp_insert_post_data', 'wl_remove_text_annotations', '98', 1 );
78
+add_filter('wp_insert_post_data', 'wl_remove_text_annotations', '98', 1);
79 79
 
80 80
 /**
81 81
  * Adds wl-metabox CSS class to a metabox.
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
  *
87 87
  * @return array The updated list of CSS classes.
88 88
  */
89
-function wl_admin_metaboxes_add_css_class( $classes = array() ) {
89
+function wl_admin_metaboxes_add_css_class($classes = array()) {
90 90
 
91
-	return array_merge( $classes, array( 'wl-metabox' ) );
91
+	return array_merge($classes, array('wl-metabox'));
92 92
 }
Please login to merge, or discard this patch.
src/includes/cache/class-wordlift-cached-post-converter.php 2 patches
Indentation   +315 added lines, -315 removed lines patch added patch discarded remove patch
@@ -18,320 +18,320 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Cached_Post_Converter implements Wordlift_Post_Converter {
20 20
 
21
-	/**
22
-	 * A {@link Wordlift_Post_Converter} instance.
23
-	 *
24
-	 * @since 3.16.0
25
-	 *
26
-	 * @var \Wordlift_Post_Converter $converter A {@link Wordlift_Post_Converter} instance.
27
-	 */
28
-	private $converter;
29
-
30
-	/**
31
-	 * A {@link Wordlift_Log_Service} instance.
32
-	 *
33
-	 * @since 3.16.0
34
-	 *
35
-	 * @var Wordlift_Log_Service \$log A {@link Wordlift_Log_Service} instance.
36
-	 */
37
-	private $log;
38
-
39
-	/**
40
-	 * @var Wordlift_Cache_Service
41
-	 */
42
-	private $cache_service;
43
-
44
-	/**
45
-	 * The {@link Wordlift_Configuration_Service} instance.
46
-	 *
47
-	 * @since  3.16.0
48
-	 * @access private
49
-	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
50
-	 */
51
-	private $configuration_service;
52
-
53
-	/**
54
-	 * A list of meta keys that do not cause the cache to update.
55
-	 *
56
-	 * @since 3.17.3
57
-	 * @var array An array of ignored meta keys.
58
-	 */
59
-	private static $ignored_meta_keys = array(
60
-		'_edit_lock',
61
-		'_edit_last',
62
-		'_wp_page_template',
63
-		'_wp_attachment_is_custom_background',
64
-		'_wp_attachment_backup_sizes',
65
-		'_wp_attachment_is_custom_header',
66
-	);
67
-
68
-	/**
69
-	 * Wordlift_Cached_Post_Converter constructor.
70
-	 *
71
-	 * @param \Wordlift_Post_Converter        $converter             The {@link Wordlift_Post_Converter} implementation.
72
-	 * @param \Wordlift_Cache_Service         $cache_service         The {@link Wordlift_Cache_Service} implementation.
73
-	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
74
-	 */
75
-	public function __construct( $converter, $cache_service, $configuration_service ) {
76
-
77
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
78
-
79
-		$this->converter             = $converter;
80
-		$this->cache_service         = $cache_service;
81
-		$this->configuration_service = $configuration_service;
82
-
83
-		$this->init_hooks();
84
-
85
-	}
86
-
87
-	/**
88
-	 * Hooks to catch post/post meta changes in order to invalidate the cache.
89
-	 *
90
-	 * @since 3.16.0
91
-	 */
92
-	private function init_hooks() {
93
-
94
-		// Hook on post save to flush relevant cache.
95
-		add_action( 'save_post', array( $this, 'save_post' ) );
96
-
97
-		add_action( 'added_post_meta', array(
98
-			$this,
99
-			'changed_post_meta',
100
-		), 10, 3 );
101
-		add_action( 'updated_post_meta', array(
102
-			$this,
103
-			'changed_post_meta',
104
-		), 10, 3 );
105
-		add_action( 'deleted_post_meta', array(
106
-			$this,
107
-			'changed_post_meta',
108
-		), 10, 3 );
109
-
110
-		// Flush cache when wordlift settings were updated.
111
-		add_action( 'update_option_wl_general_settings', array(
112
-			$this,
113
-			'update_option_wl_general_settings',
114
-		) );
115
-
116
-		// Flushes the cache when permalink structure is changed.
117
-		add_action( 'update_option_permalink_structure', array(
118
-			$this,
119
-			'permalinks_structure_changed',
120
-		) );
121
-
122
-		// Invalid cache on relationship change.
123
-		add_action( 'wl_relation_added', array( $this, 'relation_changed' ) );
124
-		add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) );
125
-
126
-	}
127
-
128
-	/**
129
-	 * @inheritdoc
130
-	 */
131
-	public function convert( $post_id, &$references = array(), &$cache = false ) {
132
-
133
-		$this->log->trace( "Converting post $post_id..." );
134
-
135
-		// Try to get a cached result.
136
-		$contents = $this->get_cache( $post_id, $references );
137
-
138
-		// Return the cached contents if any.
139
-		if ( false !== $contents ) {
140
-			$this->log->debug( "Cached contents found for post $post_id." );
141
-
142
-			// Inform the caller that this is cached result.
143
-			$cache = true;
144
-			$this->add_http_header( $post_id, true );
145
-
146
-			// Return the contents.
147
-			return $contents;
148
-		}
149
-
150
-		// Set cached to false.
151
-		$cache = false;
152
-		$this->add_http_header( $post_id, false );
153
-
154
-		// Convert the the post.
155
-		$jsonld = $this->converter->convert( $post_id, $references );
156
-
157
-		// Cache the results.
158
-		$this->set_cache( $post_id, $references, $jsonld );
159
-
160
-		// Finally return the JSON-LD.
161
-		return $jsonld;
162
-	}
163
-
164
-	/**
165
-	 * Try to get the cached contents.
166
-	 *
167
-	 * @since 3.16.0
168
-	 *
169
-	 * @param int   $post_id    The {@link WP_Post} id.
170
-	 * @param array $references The referenced posts.
171
-	 *
172
-	 * @return mixed|bool The cached contents or false if the cached isn't found.
173
-	 */
174
-	private function get_cache( $post_id, &$references = array() ) {
175
-
176
-		$this->log->trace( "Getting cached contents for post $post_id..." );
177
-
178
-		// Get the cache.
179
-		$contents = $this->cache_service->get_cache( $post_id );
180
-
181
-		// Bail out if we don't have cached contents or the cached contents are
182
-		// invalid.
183
-		if ( false === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) {
184
-			$this->log->debug( "Cached contents for post $post_id not found." );
185
-
186
-			return false;
187
-		}
188
-
189
-		// Remap the cache.
190
-		$references = $contents['references'];
191
-
192
-		return $contents['jsonld'];
193
-	}
194
-
195
-	/**
196
-	 * Set the cache with the provided results.
197
-	 *
198
-	 * The function will prepare the provided results and will ask the {@link Wordlift_Cache_Service}
199
-	 * to cache them.
200
-	 *
201
-	 * @since 3.16.0
202
-	 *
203
-	 * @param int   $post_id    The {@link WP_Post} id.
204
-	 * @param array $references An array of references.
205
-	 * @param array $jsonld     A JSON-LD structure.
206
-	 */
207
-	private function set_cache( $post_id, $references, $jsonld ) {
208
-
209
-		$this->log->trace( "Caching result for post $post_id..." );
210
-
211
-		$this->cache_service->set_cache( $post_id, array(
212
-			'references' => $references,
213
-			'jsonld'     => $jsonld,
214
-		) );
215
-
216
-	}
217
-
218
-	/**
219
-	 * Hook to 'save_post', will invalidate the cache for that post.
220
-	 *
221
-	 * @since 3.16.0
222
-	 *
223
-	 * @param int $post_id The {@link WP_Post} id.
224
-	 */
225
-	public function save_post( $post_id ) {
226
-
227
-		$this->log->trace( "Post $post_id saved, invalidating cache..." );
228
-
229
-		$this->cache_service->delete_cache( $post_id );
230
-
231
-		$this->flush_cache_if_publisher( $post_id );
232
-
233
-	}
234
-
235
-	/**
236
-	 * Hook to meta changed for a {@link WP_Post}, will cause the cause to
237
-	 * invalidate.
238
-	 *
239
-	 * @since 3.16.0
240
-	 *
241
-	 * @param int    $id       The {@link WP_Post} meta id.
242
-	 * @param int    $post_id  The {@link WP_Post} id.
243
-	 * @param string $meta_key The meta key.
244
-	 */
245
-	public function changed_post_meta( $id, $post_id, $meta_key ) {
246
-
247
-		if ( in_array( $meta_key, self::$ignored_meta_keys ) ) {
248
-			$this->log->trace( "Post $post_id meta $meta_key ignored." );
249
-
250
-			return;
251
-		}
252
-
253
-		$this->log->trace( "Post $post_id meta $meta_key changed, invalidating cache..." );
254
-
255
-		// Delete the single cache file.
256
-		$this->cache_service->delete_cache( $post_id );
257
-
258
-		// Flush the cache if it's the publisher.
259
-		$this->flush_cache_if_publisher( $post_id );
260
-
261
-	}
262
-
263
-	/**
264
-	 * Hook to WordLift's options changes, will flush the cache.
265
-	 *
266
-	 * @since 3.16.0
267
-	 */
268
-	public function update_option_wl_general_settings() {
269
-		$this->log->trace( "WordLift options changed, flushing cache..." );
270
-
271
-		$this->cache_service->flush();
272
-	}
273
-
274
-	/**
275
-	 * Hook when permalinks are changed, will flush the cache.
276
-	 *
277
-	 * @since 3.17.0
278
-	 */
279
-	public function permalinks_structure_changed() {
280
-		$this->log->trace( "Permalinks structure changed, flushing cache..." );
281
-
282
-		$this->cache_service->flush();
283
-	}
284
-
285
-	/**
286
-	 * Hook to WordLift's post/entity relation changes, will invalidate the cache.
287
-	 *
288
-	 * @since 3.16.0
289
-	 *
290
-	 * @param int $post_id The {@link WP_Post} id.
291
-	 */
292
-	public function relation_changed( $post_id ) {
293
-		$this->log->trace( "Post $post_id relations changed, invalidating cache..." );
294
-
295
-		$this->cache_service->delete_cache( $post_id );
296
-	}
297
-
298
-	/**
299
-	 * When in Ajax, prints an http header with the information whether the
300
-	 * response is cached or not.
301
-	 *
302
-	 * @since 3.16.0
303
-	 *
304
-	 * @param int  $post_id The {@link WP_Post} id.
305
-	 * @param bool $cache   Whether the response fragment is cached.
306
-	 */
307
-	private function add_http_header( $post_id, $cache ) {
308
-
309
-		if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || headers_sent() ) {
310
-			return;
311
-		}
312
-
313
-		header( "X-WordLift-JsonLd-Cache-$post_id: " . ( $cache ? 'HIT' : 'MISS' ) );
314
-
315
-	}
316
-
317
-	/**
318
-	 * Call the `flush` operation on the {@link Wordlift_Cache_Service} if
319
-	 * the publisher has changed.
320
-	 *
321
-	 * @since 3.16.0
322
-	 *
323
-	 * @param int $post_id The changed {@link WP_Post}'s id.
324
-	 */
325
-	private function flush_cache_if_publisher( $post_id ) {
326
-
327
-		// Bail out if it's not the publisher.
328
-		if ( $post_id !== $this->configuration_service->get_publisher_id() ) {
329
-			return;
330
-		}
331
-
332
-		// Flush the cache, since the publisher has changed.
333
-		$this->cache_service->flush();
334
-
335
-	}
21
+    /**
22
+     * A {@link Wordlift_Post_Converter} instance.
23
+     *
24
+     * @since 3.16.0
25
+     *
26
+     * @var \Wordlift_Post_Converter $converter A {@link Wordlift_Post_Converter} instance.
27
+     */
28
+    private $converter;
29
+
30
+    /**
31
+     * A {@link Wordlift_Log_Service} instance.
32
+     *
33
+     * @since 3.16.0
34
+     *
35
+     * @var Wordlift_Log_Service \$log A {@link Wordlift_Log_Service} instance.
36
+     */
37
+    private $log;
38
+
39
+    /**
40
+     * @var Wordlift_Cache_Service
41
+     */
42
+    private $cache_service;
43
+
44
+    /**
45
+     * The {@link Wordlift_Configuration_Service} instance.
46
+     *
47
+     * @since  3.16.0
48
+     * @access private
49
+     * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
50
+     */
51
+    private $configuration_service;
52
+
53
+    /**
54
+     * A list of meta keys that do not cause the cache to update.
55
+     *
56
+     * @since 3.17.3
57
+     * @var array An array of ignored meta keys.
58
+     */
59
+    private static $ignored_meta_keys = array(
60
+        '_edit_lock',
61
+        '_edit_last',
62
+        '_wp_page_template',
63
+        '_wp_attachment_is_custom_background',
64
+        '_wp_attachment_backup_sizes',
65
+        '_wp_attachment_is_custom_header',
66
+    );
67
+
68
+    /**
69
+     * Wordlift_Cached_Post_Converter constructor.
70
+     *
71
+     * @param \Wordlift_Post_Converter        $converter             The {@link Wordlift_Post_Converter} implementation.
72
+     * @param \Wordlift_Cache_Service         $cache_service         The {@link Wordlift_Cache_Service} implementation.
73
+     * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
74
+     */
75
+    public function __construct( $converter, $cache_service, $configuration_service ) {
76
+
77
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
78
+
79
+        $this->converter             = $converter;
80
+        $this->cache_service         = $cache_service;
81
+        $this->configuration_service = $configuration_service;
82
+
83
+        $this->init_hooks();
84
+
85
+    }
86
+
87
+    /**
88
+     * Hooks to catch post/post meta changes in order to invalidate the cache.
89
+     *
90
+     * @since 3.16.0
91
+     */
92
+    private function init_hooks() {
93
+
94
+        // Hook on post save to flush relevant cache.
95
+        add_action( 'save_post', array( $this, 'save_post' ) );
96
+
97
+        add_action( 'added_post_meta', array(
98
+            $this,
99
+            'changed_post_meta',
100
+        ), 10, 3 );
101
+        add_action( 'updated_post_meta', array(
102
+            $this,
103
+            'changed_post_meta',
104
+        ), 10, 3 );
105
+        add_action( 'deleted_post_meta', array(
106
+            $this,
107
+            'changed_post_meta',
108
+        ), 10, 3 );
109
+
110
+        // Flush cache when wordlift settings were updated.
111
+        add_action( 'update_option_wl_general_settings', array(
112
+            $this,
113
+            'update_option_wl_general_settings',
114
+        ) );
115
+
116
+        // Flushes the cache when permalink structure is changed.
117
+        add_action( 'update_option_permalink_structure', array(
118
+            $this,
119
+            'permalinks_structure_changed',
120
+        ) );
121
+
122
+        // Invalid cache on relationship change.
123
+        add_action( 'wl_relation_added', array( $this, 'relation_changed' ) );
124
+        add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) );
125
+
126
+    }
127
+
128
+    /**
129
+     * @inheritdoc
130
+     */
131
+    public function convert( $post_id, &$references = array(), &$cache = false ) {
132
+
133
+        $this->log->trace( "Converting post $post_id..." );
134
+
135
+        // Try to get a cached result.
136
+        $contents = $this->get_cache( $post_id, $references );
137
+
138
+        // Return the cached contents if any.
139
+        if ( false !== $contents ) {
140
+            $this->log->debug( "Cached contents found for post $post_id." );
141
+
142
+            // Inform the caller that this is cached result.
143
+            $cache = true;
144
+            $this->add_http_header( $post_id, true );
145
+
146
+            // Return the contents.
147
+            return $contents;
148
+        }
149
+
150
+        // Set cached to false.
151
+        $cache = false;
152
+        $this->add_http_header( $post_id, false );
153
+
154
+        // Convert the the post.
155
+        $jsonld = $this->converter->convert( $post_id, $references );
156
+
157
+        // Cache the results.
158
+        $this->set_cache( $post_id, $references, $jsonld );
159
+
160
+        // Finally return the JSON-LD.
161
+        return $jsonld;
162
+    }
163
+
164
+    /**
165
+     * Try to get the cached contents.
166
+     *
167
+     * @since 3.16.0
168
+     *
169
+     * @param int   $post_id    The {@link WP_Post} id.
170
+     * @param array $references The referenced posts.
171
+     *
172
+     * @return mixed|bool The cached contents or false if the cached isn't found.
173
+     */
174
+    private function get_cache( $post_id, &$references = array() ) {
175
+
176
+        $this->log->trace( "Getting cached contents for post $post_id..." );
177
+
178
+        // Get the cache.
179
+        $contents = $this->cache_service->get_cache( $post_id );
180
+
181
+        // Bail out if we don't have cached contents or the cached contents are
182
+        // invalid.
183
+        if ( false === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) {
184
+            $this->log->debug( "Cached contents for post $post_id not found." );
185
+
186
+            return false;
187
+        }
188
+
189
+        // Remap the cache.
190
+        $references = $contents['references'];
191
+
192
+        return $contents['jsonld'];
193
+    }
194
+
195
+    /**
196
+     * Set the cache with the provided results.
197
+     *
198
+     * The function will prepare the provided results and will ask the {@link Wordlift_Cache_Service}
199
+     * to cache them.
200
+     *
201
+     * @since 3.16.0
202
+     *
203
+     * @param int   $post_id    The {@link WP_Post} id.
204
+     * @param array $references An array of references.
205
+     * @param array $jsonld     A JSON-LD structure.
206
+     */
207
+    private function set_cache( $post_id, $references, $jsonld ) {
208
+
209
+        $this->log->trace( "Caching result for post $post_id..." );
210
+
211
+        $this->cache_service->set_cache( $post_id, array(
212
+            'references' => $references,
213
+            'jsonld'     => $jsonld,
214
+        ) );
215
+
216
+    }
217
+
218
+    /**
219
+     * Hook to 'save_post', will invalidate the cache for that post.
220
+     *
221
+     * @since 3.16.0
222
+     *
223
+     * @param int $post_id The {@link WP_Post} id.
224
+     */
225
+    public function save_post( $post_id ) {
226
+
227
+        $this->log->trace( "Post $post_id saved, invalidating cache..." );
228
+
229
+        $this->cache_service->delete_cache( $post_id );
230
+
231
+        $this->flush_cache_if_publisher( $post_id );
232
+
233
+    }
234
+
235
+    /**
236
+     * Hook to meta changed for a {@link WP_Post}, will cause the cause to
237
+     * invalidate.
238
+     *
239
+     * @since 3.16.0
240
+     *
241
+     * @param int    $id       The {@link WP_Post} meta id.
242
+     * @param int    $post_id  The {@link WP_Post} id.
243
+     * @param string $meta_key The meta key.
244
+     */
245
+    public function changed_post_meta( $id, $post_id, $meta_key ) {
246
+
247
+        if ( in_array( $meta_key, self::$ignored_meta_keys ) ) {
248
+            $this->log->trace( "Post $post_id meta $meta_key ignored." );
249
+
250
+            return;
251
+        }
252
+
253
+        $this->log->trace( "Post $post_id meta $meta_key changed, invalidating cache..." );
254
+
255
+        // Delete the single cache file.
256
+        $this->cache_service->delete_cache( $post_id );
257
+
258
+        // Flush the cache if it's the publisher.
259
+        $this->flush_cache_if_publisher( $post_id );
260
+
261
+    }
262
+
263
+    /**
264
+     * Hook to WordLift's options changes, will flush the cache.
265
+     *
266
+     * @since 3.16.0
267
+     */
268
+    public function update_option_wl_general_settings() {
269
+        $this->log->trace( "WordLift options changed, flushing cache..." );
270
+
271
+        $this->cache_service->flush();
272
+    }
273
+
274
+    /**
275
+     * Hook when permalinks are changed, will flush the cache.
276
+     *
277
+     * @since 3.17.0
278
+     */
279
+    public function permalinks_structure_changed() {
280
+        $this->log->trace( "Permalinks structure changed, flushing cache..." );
281
+
282
+        $this->cache_service->flush();
283
+    }
284
+
285
+    /**
286
+     * Hook to WordLift's post/entity relation changes, will invalidate the cache.
287
+     *
288
+     * @since 3.16.0
289
+     *
290
+     * @param int $post_id The {@link WP_Post} id.
291
+     */
292
+    public function relation_changed( $post_id ) {
293
+        $this->log->trace( "Post $post_id relations changed, invalidating cache..." );
294
+
295
+        $this->cache_service->delete_cache( $post_id );
296
+    }
297
+
298
+    /**
299
+     * When in Ajax, prints an http header with the information whether the
300
+     * response is cached or not.
301
+     *
302
+     * @since 3.16.0
303
+     *
304
+     * @param int  $post_id The {@link WP_Post} id.
305
+     * @param bool $cache   Whether the response fragment is cached.
306
+     */
307
+    private function add_http_header( $post_id, $cache ) {
308
+
309
+        if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || headers_sent() ) {
310
+            return;
311
+        }
312
+
313
+        header( "X-WordLift-JsonLd-Cache-$post_id: " . ( $cache ? 'HIT' : 'MISS' ) );
314
+
315
+    }
316
+
317
+    /**
318
+     * Call the `flush` operation on the {@link Wordlift_Cache_Service} if
319
+     * the publisher has changed.
320
+     *
321
+     * @since 3.16.0
322
+     *
323
+     * @param int $post_id The changed {@link WP_Post}'s id.
324
+     */
325
+    private function flush_cache_if_publisher( $post_id ) {
326
+
327
+        // Bail out if it's not the publisher.
328
+        if ( $post_id !== $this->configuration_service->get_publisher_id() ) {
329
+            return;
330
+        }
331
+
332
+        // Flush the cache, since the publisher has changed.
333
+        $this->cache_service->flush();
334
+
335
+    }
336 336
 
337 337
 }
Please login to merge, or discard this patch.
Spacing   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -72,9 +72,9 @@  discard block
 block discarded – undo
72 72
 	 * @param \Wordlift_Cache_Service         $cache_service         The {@link Wordlift_Cache_Service} implementation.
73 73
 	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
74 74
 	 */
75
-	public function __construct( $converter, $cache_service, $configuration_service ) {
75
+	public function __construct($converter, $cache_service, $configuration_service) {
76 76
 
77
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
77
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
78 78
 
79 79
 		$this->converter             = $converter;
80 80
 		$this->cache_service         = $cache_service;
@@ -92,56 +92,56 @@  discard block
 block discarded – undo
92 92
 	private function init_hooks() {
93 93
 
94 94
 		// Hook on post save to flush relevant cache.
95
-		add_action( 'save_post', array( $this, 'save_post' ) );
95
+		add_action('save_post', array($this, 'save_post'));
96 96
 
97
-		add_action( 'added_post_meta', array(
97
+		add_action('added_post_meta', array(
98 98
 			$this,
99 99
 			'changed_post_meta',
100
-		), 10, 3 );
101
-		add_action( 'updated_post_meta', array(
100
+		), 10, 3);
101
+		add_action('updated_post_meta', array(
102 102
 			$this,
103 103
 			'changed_post_meta',
104
-		), 10, 3 );
105
-		add_action( 'deleted_post_meta', array(
104
+		), 10, 3);
105
+		add_action('deleted_post_meta', array(
106 106
 			$this,
107 107
 			'changed_post_meta',
108
-		), 10, 3 );
108
+		), 10, 3);
109 109
 
110 110
 		// Flush cache when wordlift settings were updated.
111
-		add_action( 'update_option_wl_general_settings', array(
111
+		add_action('update_option_wl_general_settings', array(
112 112
 			$this,
113 113
 			'update_option_wl_general_settings',
114
-		) );
114
+		));
115 115
 
116 116
 		// Flushes the cache when permalink structure is changed.
117
-		add_action( 'update_option_permalink_structure', array(
117
+		add_action('update_option_permalink_structure', array(
118 118
 			$this,
119 119
 			'permalinks_structure_changed',
120
-		) );
120
+		));
121 121
 
122 122
 		// Invalid cache on relationship change.
123
-		add_action( 'wl_relation_added', array( $this, 'relation_changed' ) );
124
-		add_action( 'wl_relation_deleted', array( $this, 'relation_changed' ) );
123
+		add_action('wl_relation_added', array($this, 'relation_changed'));
124
+		add_action('wl_relation_deleted', array($this, 'relation_changed'));
125 125
 
126 126
 	}
127 127
 
128 128
 	/**
129 129
 	 * @inheritdoc
130 130
 	 */
131
-	public function convert( $post_id, &$references = array(), &$cache = false ) {
131
+	public function convert($post_id, &$references = array(), &$cache = false) {
132 132
 
133
-		$this->log->trace( "Converting post $post_id..." );
133
+		$this->log->trace("Converting post $post_id...");
134 134
 
135 135
 		// Try to get a cached result.
136
-		$contents = $this->get_cache( $post_id, $references );
136
+		$contents = $this->get_cache($post_id, $references);
137 137
 
138 138
 		// Return the cached contents if any.
139
-		if ( false !== $contents ) {
140
-			$this->log->debug( "Cached contents found for post $post_id." );
139
+		if (false !== $contents) {
140
+			$this->log->debug("Cached contents found for post $post_id.");
141 141
 
142 142
 			// Inform the caller that this is cached result.
143 143
 			$cache = true;
144
-			$this->add_http_header( $post_id, true );
144
+			$this->add_http_header($post_id, true);
145 145
 
146 146
 			// Return the contents.
147 147
 			return $contents;
@@ -149,13 +149,13 @@  discard block
 block discarded – undo
149 149
 
150 150
 		// Set cached to false.
151 151
 		$cache = false;
152
-		$this->add_http_header( $post_id, false );
152
+		$this->add_http_header($post_id, false);
153 153
 
154 154
 		// Convert the the post.
155
-		$jsonld = $this->converter->convert( $post_id, $references );
155
+		$jsonld = $this->converter->convert($post_id, $references);
156 156
 
157 157
 		// Cache the results.
158
-		$this->set_cache( $post_id, $references, $jsonld );
158
+		$this->set_cache($post_id, $references, $jsonld);
159 159
 
160 160
 		// Finally return the JSON-LD.
161 161
 		return $jsonld;
@@ -171,17 +171,17 @@  discard block
 block discarded – undo
171 171
 	 *
172 172
 	 * @return mixed|bool The cached contents or false if the cached isn't found.
173 173
 	 */
174
-	private function get_cache( $post_id, &$references = array() ) {
174
+	private function get_cache($post_id, &$references = array()) {
175 175
 
176
-		$this->log->trace( "Getting cached contents for post $post_id..." );
176
+		$this->log->trace("Getting cached contents for post $post_id...");
177 177
 
178 178
 		// Get the cache.
179
-		$contents = $this->cache_service->get_cache( $post_id );
179
+		$contents = $this->cache_service->get_cache($post_id);
180 180
 
181 181
 		// Bail out if we don't have cached contents or the cached contents are
182 182
 		// invalid.
183
-		if ( false === $contents || ! isset( $contents['jsonld'] ) || ! isset( $contents['references'] ) ) {
184
-			$this->log->debug( "Cached contents for post $post_id not found." );
183
+		if (false === $contents || ! isset($contents['jsonld']) || ! isset($contents['references'])) {
184
+			$this->log->debug("Cached contents for post $post_id not found.");
185 185
 
186 186
 			return false;
187 187
 		}
@@ -204,14 +204,14 @@  discard block
 block discarded – undo
204 204
 	 * @param array $references An array of references.
205 205
 	 * @param array $jsonld     A JSON-LD structure.
206 206
 	 */
207
-	private function set_cache( $post_id, $references, $jsonld ) {
207
+	private function set_cache($post_id, $references, $jsonld) {
208 208
 
209
-		$this->log->trace( "Caching result for post $post_id..." );
209
+		$this->log->trace("Caching result for post $post_id...");
210 210
 
211
-		$this->cache_service->set_cache( $post_id, array(
211
+		$this->cache_service->set_cache($post_id, array(
212 212
 			'references' => $references,
213 213
 			'jsonld'     => $jsonld,
214
-		) );
214
+		));
215 215
 
216 216
 	}
217 217
 
@@ -222,13 +222,13 @@  discard block
 block discarded – undo
222 222
 	 *
223 223
 	 * @param int $post_id The {@link WP_Post} id.
224 224
 	 */
225
-	public function save_post( $post_id ) {
225
+	public function save_post($post_id) {
226 226
 
227
-		$this->log->trace( "Post $post_id saved, invalidating cache..." );
227
+		$this->log->trace("Post $post_id saved, invalidating cache...");
228 228
 
229
-		$this->cache_service->delete_cache( $post_id );
229
+		$this->cache_service->delete_cache($post_id);
230 230
 
231
-		$this->flush_cache_if_publisher( $post_id );
231
+		$this->flush_cache_if_publisher($post_id);
232 232
 
233 233
 	}
234 234
 
@@ -242,21 +242,21 @@  discard block
 block discarded – undo
242 242
 	 * @param int    $post_id  The {@link WP_Post} id.
243 243
 	 * @param string $meta_key The meta key.
244 244
 	 */
245
-	public function changed_post_meta( $id, $post_id, $meta_key ) {
245
+	public function changed_post_meta($id, $post_id, $meta_key) {
246 246
 
247
-		if ( in_array( $meta_key, self::$ignored_meta_keys ) ) {
248
-			$this->log->trace( "Post $post_id meta $meta_key ignored." );
247
+		if (in_array($meta_key, self::$ignored_meta_keys)) {
248
+			$this->log->trace("Post $post_id meta $meta_key ignored.");
249 249
 
250 250
 			return;
251 251
 		}
252 252
 
253
-		$this->log->trace( "Post $post_id meta $meta_key changed, invalidating cache..." );
253
+		$this->log->trace("Post $post_id meta $meta_key changed, invalidating cache...");
254 254
 
255 255
 		// Delete the single cache file.
256
-		$this->cache_service->delete_cache( $post_id );
256
+		$this->cache_service->delete_cache($post_id);
257 257
 
258 258
 		// Flush the cache if it's the publisher.
259
-		$this->flush_cache_if_publisher( $post_id );
259
+		$this->flush_cache_if_publisher($post_id);
260 260
 
261 261
 	}
262 262
 
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
 	 * @since 3.16.0
267 267
 	 */
268 268
 	public function update_option_wl_general_settings() {
269
-		$this->log->trace( "WordLift options changed, flushing cache..." );
269
+		$this->log->trace("WordLift options changed, flushing cache...");
270 270
 
271 271
 		$this->cache_service->flush();
272 272
 	}
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
 	 * @since 3.17.0
278 278
 	 */
279 279
 	public function permalinks_structure_changed() {
280
-		$this->log->trace( "Permalinks structure changed, flushing cache..." );
280
+		$this->log->trace("Permalinks structure changed, flushing cache...");
281 281
 
282 282
 		$this->cache_service->flush();
283 283
 	}
@@ -289,10 +289,10 @@  discard block
 block discarded – undo
289 289
 	 *
290 290
 	 * @param int $post_id The {@link WP_Post} id.
291 291
 	 */
292
-	public function relation_changed( $post_id ) {
293
-		$this->log->trace( "Post $post_id relations changed, invalidating cache..." );
292
+	public function relation_changed($post_id) {
293
+		$this->log->trace("Post $post_id relations changed, invalidating cache...");
294 294
 
295
-		$this->cache_service->delete_cache( $post_id );
295
+		$this->cache_service->delete_cache($post_id);
296 296
 	}
297 297
 
298 298
 	/**
@@ -304,13 +304,13 @@  discard block
 block discarded – undo
304 304
 	 * @param int  $post_id The {@link WP_Post} id.
305 305
 	 * @param bool $cache   Whether the response fragment is cached.
306 306
 	 */
307
-	private function add_http_header( $post_id, $cache ) {
307
+	private function add_http_header($post_id, $cache) {
308 308
 
309
-		if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || headers_sent() ) {
309
+		if ( ! defined('DOING_AJAX') || ! DOING_AJAX || headers_sent()) {
310 310
 			return;
311 311
 		}
312 312
 
313
-		header( "X-WordLift-JsonLd-Cache-$post_id: " . ( $cache ? 'HIT' : 'MISS' ) );
313
+		header("X-WordLift-JsonLd-Cache-$post_id: ".($cache ? 'HIT' : 'MISS'));
314 314
 
315 315
 	}
316 316
 
@@ -322,10 +322,10 @@  discard block
 block discarded – undo
322 322
 	 *
323 323
 	 * @param int $post_id The changed {@link WP_Post}'s id.
324 324
 	 */
325
-	private function flush_cache_if_publisher( $post_id ) {
325
+	private function flush_cache_if_publisher($post_id) {
326 326
 
327 327
 		// Bail out if it's not the publisher.
328
-		if ( $post_id !== $this->configuration_service->get_publisher_id() ) {
328
+		if ($post_id !== $this->configuration_service->get_publisher_id()) {
329 329
 			return;
330 330
 		}
331 331
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-linked-data-service.php 2 patches
Indentation   +316 added lines, -316 removed lines patch added patch discarded remove patch
@@ -18,321 +18,321 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Linked_Data_Service {
20 20
 
21
-	//<editor-fold desc="## FIELDS">
22
-	/**
23
-	 * A {@link Wordlift_Log_Service} instance.
24
-	 *
25
-	 * @since  3.15.0
26
-	 * @access private
27
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
28
-	 */
29
-	private $log;
30
-
31
-	/**
32
-	 * The {@link Wordlift_Entity_Service} instance.
33
-	 *
34
-	 * @since  3.15.0
35
-	 * @access private
36
-	 * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
37
-	 */
38
-	private $entity_service;
39
-
40
-	/**
41
-	 * The {@link Wordlift_Entity_Type_Service} instance.
42
-	 *
43
-	 * @since  3.15.0
44
-	 * @access private
45
-	 * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
46
-	 */
47
-	private $entity_type_service;
48
-
49
-	/**
50
-	 * The {@link Wordlift_Schema_Service} instance.
51
-	 *
52
-	 * @since  3.15.0
53
-	 * @access private
54
-	 * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
55
-	 */
56
-	private $schema_service;
57
-
58
-	/**
59
-	 * The {@link Wordlift_Sparql_Service} instance.
60
-	 *
61
-	 * @since  3.15.0
62
-	 * @access private
63
-	 * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance.
64
-	 */
65
-	private $sparql_service;
66
-
67
-	/**
68
-	 * The {@link Wordlift_Linked_Data_Service} singleton instance.
69
-	 *
70
-	 * @since  3.15.0
71
-	 * @access private
72
-	 * @var \Wordlift_Linked_Data_Service $instance The {@link Wordlift_Linked_Data_Service} singleton instance.
73
-	 */
74
-	private static $instance;
75
-	//</editor-fold>
76
-
77
-	/**
78
-	 * Create a {@link Wordlift_Linked_Data_Service} instance.
79
-	 *
80
-	 * @since 3.15.0
81
-	 *
82
-	 * @param \Wordlift_Entity_Service      $entity_service      The {@link Wordlift_Entity_Service} instance.
83
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
84
-	 * @param \Wordlift_Schema_Service      $schema_service      The {@link Wordlift_Schema_Service} instance.
85
-	 * @param \Wordlift_Sparql_Service      $sparql_service      The {@link Wordlift_Sparql_Service} instance.
86
-	 */
87
-	public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) {
88
-
89
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' );
90
-
91
-		$this->entity_service      = $entity_service;
92
-		$this->entity_type_service = $entity_type_service;
93
-		$this->schema_service      = $schema_service;
94
-		$this->sparql_service      = $sparql_service;
95
-
96
-		self::$instance = $this;
97
-
98
-	}
99
-
100
-	/**
101
-	 * Get the singleton instance of {@link Wordlift_Linked_Data_Service}.
102
-	 *
103
-	 * @since 3.15.0
104
-	 *
105
-	 * @return Wordlift_Linked_Data_Service The singleton instance of <a href='psi_element://Wordlift_Linked_Data_Service'>Wordlift_Linked_Data_Service</a>.
106
-	 */
107
-	public static function get_instance() {
108
-
109
-		return self::$instance;
110
-	}
111
-
112
-	//<editor-fold desc="## FUNCTIONS">
113
-
114
-	/**
115
-	 * Push a {@link WP_Post} to the Linked Data store.
116
-	 *
117
-	 * If the {@link WP_Post} is an entity and it's not of the `Article` type,
118
-	 * then it is pushed to the remote Linked Data store.
119
-	 *
120
-	 * @since 3.15.0
121
-	 *
122
-	 * @param int $post_id The {@link WP_Post}'s id.
123
-	 */
124
-	public function push( $post_id ) {
125
-
126
-		$this->log->debug( "Pushing post $post_id..." );
127
-
128
-		// @since 3.18.0 we don't check anymore if the post is an entity, i.e.
129
-		// we removed the following:
130
-		//		if ( ! $this->entity_service->is_entity( $post_id ) ) {
131
-		//			$this->log->debug( "Post $post_id is not an entity." );
132
-		//
133
-		//			return;
134
-		//		}
135
-
136
-		// Get the post and push it to the Linked Data store.
137
-		$this->do_push( $post_id );
138
-
139
-		// Reindex the triple store if buffering is turned off.
140
-		if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) {
141
-			wordlift_reindex_triple_store();
142
-		}
143
-
144
-	}
145
-
146
-	/**
147
-	 * Push an entity to the Linked Data store.
148
-	 *
149
-	 * @since 3.15.0
150
-	 *
151
-	 * @param int $post_id The {@link WP_Post}'s id.
152
-	 */
153
-	private function do_push( $post_id ) {
154
-		$this->log->debug( "Doing post $post_id push..." );
155
-
156
-		// Get the post.
157
-		$post = get_post( $post_id );
158
-
159
-		// Bail out if the post isn't found.
160
-		if ( null === $post ) {
161
-			$this->log->warn( "Post $post_id not found." );
162
-
163
-			return;
164
-		}
165
-
166
-		// Bail out if the post isn't published.
167
-		if ( 'publish' !== $post->post_status ) {
168
-			$this->log->info( "Post $post_id not published." );
169
-
170
-			return;
171
-		}
172
-
173
-		// Bail out if the URI isn't valid.
174
-		if ( ! $this->has_valid_uri( $post_id ) ) {
175
-			$this->log->warn( "Post $post_id URI invalid." );
176
-
177
-			return;
178
-		}
179
-
180
-		// First remove the post data.
181
-		$this->remove( $post_id );
182
-
183
-		// Then execute the insert query.
184
-		$this->insert( $post_id );
185
-	}
186
-
187
-	/**
188
-	 * Check if an entity's {@link WP_Post} has a valid URI.
189
-	 *
190
-	 * @since 3.15.0
191
-	 *
192
-	 * @param int $post_id The entity's {@link WP_Post}'s id.
193
-	 *
194
-	 * @return bool True if the URI is valid otherwise false.
195
-	 */
196
-	private function has_valid_uri( $post_id ) {
197
-
198
-		// Get the entity's URI.
199
-		$uri = $this->entity_service->get_uri( $post_id );
200
-
201
-		// If the URI isn't found, return false.
202
-		if ( null === $uri ) {
203
-			return false;
204
-		}
205
-
206
-		// If the URI ends with a trailing slash, return false.
207
-		if ( '/' === substr( $uri, - 1 ) ) {
208
-			return false;
209
-		}
210
-
211
-		// URI is valid.
212
-		return true;
213
-	}
214
-
215
-	/**
216
-	 * Remove the specified {@link WP_Post} from the Linked Data.
217
-	 *
218
-	 * @since 3.18.0
219
-	 *
220
-	 * @param int $post_id The {@link WP_Post}'s id.
221
-	 */
222
-	public function remove( $post_id ) {
223
-		$delete_query = '';
224
-
225
-		// Get the delete statements.
226
-		$triples = $this->get_delete_triples( $post_id );
227
-
228
-		// Loop through all triples and add the statement to delete query.
229
-		foreach ( $triples as $item ) {
230
-			$delete_query .= "DELETE { $item } WHERE { $item }; \n";
231
-		}
232
-
233
-		$this->log->trace( "Delete Query generated [ $delete_query ]." );
234
-
235
-		$this->sparql_service->execute( $delete_query );
236
-	}
237
-
238
-	/**
239
-	 * Insert the specific {@link WP_Post} to Linked Data store.
240
-	 *
241
-	 * @since 3.18.0
242
-	 *
243
-	 * @param int $post_id The {@link WP_Post}'s id.
244
-	 */
245
-	private function insert( $post_id ) {
246
-		// Get the insert statements.
247
-		$insert_triples = $this->get_insert_triples( $post_id );
248
-
249
-		// Convert all statements to single string.
250
-		$insert_query_body = implode( "\n", $insert_triples );
251
-
252
-		// Build the insert query.
253
-		$insert_query = "INSERT DATA { $insert_query_body };";
254
-
255
-		$this->log->trace( "Insert Query generated [ $insert_query ]." );
256
-
257
-		$this->sparql_service->execute( $insert_query );
258
-	}
259
-
260
-	/**
261
-	 * Get the delete statements.
262
-	 *
263
-	 * @since 3.18.0
264
-	 *
265
-	 * @param int $post_id The {@link WP_Post}'s id.
266
-	 *
267
-	 * @return array An array of delete statements.
268
-	 */
269
-	private function get_delete_triples( $post_id ) {
270
-		$delete_triples = array();
271
-
272
-		// Loop through all renditions and get the triples.
273
-		foreach ( $this->schema_service->get_renditions() as $rendition ) {
274
-			// Push the rendition delete triple to $delete_triples.
275
-			$delete_triples = array_merge(
276
-				$delete_triples,
277
-				(array) $rendition->get_delete_triples( $post_id )
278
-			);
279
-		}
280
-
281
-		/**
282
-		 * Filter: 'wl_delete_triples' - Allow third parties to hook and add additional delete triples.
283
-		 *
284
-		 * @since 3.18.0
285
-		 *
286
-		 * @param array $delete_triples Delete triples.
287
-		 * @param int   $post_id        The current post ID.
288
-		 */
289
-		return apply_filters( 'wl_delete_triples', array_unique( $delete_triples ), $post_id );
290
-	}
291
-
292
-	/**
293
-	 * Get the SPARQL insert triples ( ?s ?p ?o ) for the specified {@link WP_Post}.
294
-	 *
295
-	 * @since 3.15.0
296
-	 *
297
-	 * @param int $post_id The {@link WP_Post}'s id.
298
-	 *
299
-	 * @return array An array of insert triples.
300
-	 */
301
-	private function get_insert_triples( $post_id ) {
302
-
303
-		// Get the entity type.
304
-		$type = $this->entity_type_service->get( $post_id );
305
-
306
-		// Get the `linked_data` parameter.
307
-		$properties = $type['linked_data'];
308
-
309
-		// Accumulate the triples.
310
-		$triples = array();
311
-
312
-		/** @var Wordlift_Default_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */
313
-		foreach ( $properties as $property ) {
314
-			foreach ( $property->get_insert_triples( $post_id ) as $triple ) {
315
-				$triples[] = $triple;
316
-			}
317
-		}
318
-
319
-		$this->log->trace( count( $properties ) . ' properties and ' . count( $triples ) . " triples found for post $post_id." );
320
-
321
-		/**
322
-		 * Get the INSERT triples properties.
323
-		 *
324
-		 * The `wl_insert_triples` filter allows 3rd parties to extend
325
-		 * the list of triples for SPARQL INSERT statements.
326
-		 *
327
-		 * @since 3.17.0
328
-		 * @since 3.18.0 The hook has been renamed from `wl_insert_tuples_properties` to `wl_insert_triples`.
329
-		 *
330
-		 * @param array                         $linked_data         A {@link Wordlift_Sparql_Tuple_Rendition} instances.
331
-		 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
332
-		 * @param int                           $post_id             The {@link WP_Post}'s id.
333
-		 */
334
-		return apply_filters( 'wl_insert_triples', $triples, $this->entity_service, $post_id );
335
-	}
336
-	//</editor-fold>
21
+    //<editor-fold desc="## FIELDS">
22
+    /**
23
+     * A {@link Wordlift_Log_Service} instance.
24
+     *
25
+     * @since  3.15.0
26
+     * @access private
27
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
28
+     */
29
+    private $log;
30
+
31
+    /**
32
+     * The {@link Wordlift_Entity_Service} instance.
33
+     *
34
+     * @since  3.15.0
35
+     * @access private
36
+     * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
37
+     */
38
+    private $entity_service;
39
+
40
+    /**
41
+     * The {@link Wordlift_Entity_Type_Service} instance.
42
+     *
43
+     * @since  3.15.0
44
+     * @access private
45
+     * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
46
+     */
47
+    private $entity_type_service;
48
+
49
+    /**
50
+     * The {@link Wordlift_Schema_Service} instance.
51
+     *
52
+     * @since  3.15.0
53
+     * @access private
54
+     * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
55
+     */
56
+    private $schema_service;
57
+
58
+    /**
59
+     * The {@link Wordlift_Sparql_Service} instance.
60
+     *
61
+     * @since  3.15.0
62
+     * @access private
63
+     * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance.
64
+     */
65
+    private $sparql_service;
66
+
67
+    /**
68
+     * The {@link Wordlift_Linked_Data_Service} singleton instance.
69
+     *
70
+     * @since  3.15.0
71
+     * @access private
72
+     * @var \Wordlift_Linked_Data_Service $instance The {@link Wordlift_Linked_Data_Service} singleton instance.
73
+     */
74
+    private static $instance;
75
+    //</editor-fold>
76
+
77
+    /**
78
+     * Create a {@link Wordlift_Linked_Data_Service} instance.
79
+     *
80
+     * @since 3.15.0
81
+     *
82
+     * @param \Wordlift_Entity_Service      $entity_service      The {@link Wordlift_Entity_Service} instance.
83
+     * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
84
+     * @param \Wordlift_Schema_Service      $schema_service      The {@link Wordlift_Schema_Service} instance.
85
+     * @param \Wordlift_Sparql_Service      $sparql_service      The {@link Wordlift_Sparql_Service} instance.
86
+     */
87
+    public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) {
88
+
89
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' );
90
+
91
+        $this->entity_service      = $entity_service;
92
+        $this->entity_type_service = $entity_type_service;
93
+        $this->schema_service      = $schema_service;
94
+        $this->sparql_service      = $sparql_service;
95
+
96
+        self::$instance = $this;
97
+
98
+    }
99
+
100
+    /**
101
+     * Get the singleton instance of {@link Wordlift_Linked_Data_Service}.
102
+     *
103
+     * @since 3.15.0
104
+     *
105
+     * @return Wordlift_Linked_Data_Service The singleton instance of <a href='psi_element://Wordlift_Linked_Data_Service'>Wordlift_Linked_Data_Service</a>.
106
+     */
107
+    public static function get_instance() {
108
+
109
+        return self::$instance;
110
+    }
111
+
112
+    //<editor-fold desc="## FUNCTIONS">
113
+
114
+    /**
115
+     * Push a {@link WP_Post} to the Linked Data store.
116
+     *
117
+     * If the {@link WP_Post} is an entity and it's not of the `Article` type,
118
+     * then it is pushed to the remote Linked Data store.
119
+     *
120
+     * @since 3.15.0
121
+     *
122
+     * @param int $post_id The {@link WP_Post}'s id.
123
+     */
124
+    public function push( $post_id ) {
125
+
126
+        $this->log->debug( "Pushing post $post_id..." );
127
+
128
+        // @since 3.18.0 we don't check anymore if the post is an entity, i.e.
129
+        // we removed the following:
130
+        //		if ( ! $this->entity_service->is_entity( $post_id ) ) {
131
+        //			$this->log->debug( "Post $post_id is not an entity." );
132
+        //
133
+        //			return;
134
+        //		}
135
+
136
+        // Get the post and push it to the Linked Data store.
137
+        $this->do_push( $post_id );
138
+
139
+        // Reindex the triple store if buffering is turned off.
140
+        if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) {
141
+            wordlift_reindex_triple_store();
142
+        }
143
+
144
+    }
145
+
146
+    /**
147
+     * Push an entity to the Linked Data store.
148
+     *
149
+     * @since 3.15.0
150
+     *
151
+     * @param int $post_id The {@link WP_Post}'s id.
152
+     */
153
+    private function do_push( $post_id ) {
154
+        $this->log->debug( "Doing post $post_id push..." );
155
+
156
+        // Get the post.
157
+        $post = get_post( $post_id );
158
+
159
+        // Bail out if the post isn't found.
160
+        if ( null === $post ) {
161
+            $this->log->warn( "Post $post_id not found." );
162
+
163
+            return;
164
+        }
165
+
166
+        // Bail out if the post isn't published.
167
+        if ( 'publish' !== $post->post_status ) {
168
+            $this->log->info( "Post $post_id not published." );
169
+
170
+            return;
171
+        }
172
+
173
+        // Bail out if the URI isn't valid.
174
+        if ( ! $this->has_valid_uri( $post_id ) ) {
175
+            $this->log->warn( "Post $post_id URI invalid." );
176
+
177
+            return;
178
+        }
179
+
180
+        // First remove the post data.
181
+        $this->remove( $post_id );
182
+
183
+        // Then execute the insert query.
184
+        $this->insert( $post_id );
185
+    }
186
+
187
+    /**
188
+     * Check if an entity's {@link WP_Post} has a valid URI.
189
+     *
190
+     * @since 3.15.0
191
+     *
192
+     * @param int $post_id The entity's {@link WP_Post}'s id.
193
+     *
194
+     * @return bool True if the URI is valid otherwise false.
195
+     */
196
+    private function has_valid_uri( $post_id ) {
197
+
198
+        // Get the entity's URI.
199
+        $uri = $this->entity_service->get_uri( $post_id );
200
+
201
+        // If the URI isn't found, return false.
202
+        if ( null === $uri ) {
203
+            return false;
204
+        }
205
+
206
+        // If the URI ends with a trailing slash, return false.
207
+        if ( '/' === substr( $uri, - 1 ) ) {
208
+            return false;
209
+        }
210
+
211
+        // URI is valid.
212
+        return true;
213
+    }
214
+
215
+    /**
216
+     * Remove the specified {@link WP_Post} from the Linked Data.
217
+     *
218
+     * @since 3.18.0
219
+     *
220
+     * @param int $post_id The {@link WP_Post}'s id.
221
+     */
222
+    public function remove( $post_id ) {
223
+        $delete_query = '';
224
+
225
+        // Get the delete statements.
226
+        $triples = $this->get_delete_triples( $post_id );
227
+
228
+        // Loop through all triples and add the statement to delete query.
229
+        foreach ( $triples as $item ) {
230
+            $delete_query .= "DELETE { $item } WHERE { $item }; \n";
231
+        }
232
+
233
+        $this->log->trace( "Delete Query generated [ $delete_query ]." );
234
+
235
+        $this->sparql_service->execute( $delete_query );
236
+    }
237
+
238
+    /**
239
+     * Insert the specific {@link WP_Post} to Linked Data store.
240
+     *
241
+     * @since 3.18.0
242
+     *
243
+     * @param int $post_id The {@link WP_Post}'s id.
244
+     */
245
+    private function insert( $post_id ) {
246
+        // Get the insert statements.
247
+        $insert_triples = $this->get_insert_triples( $post_id );
248
+
249
+        // Convert all statements to single string.
250
+        $insert_query_body = implode( "\n", $insert_triples );
251
+
252
+        // Build the insert query.
253
+        $insert_query = "INSERT DATA { $insert_query_body };";
254
+
255
+        $this->log->trace( "Insert Query generated [ $insert_query ]." );
256
+
257
+        $this->sparql_service->execute( $insert_query );
258
+    }
259
+
260
+    /**
261
+     * Get the delete statements.
262
+     *
263
+     * @since 3.18.0
264
+     *
265
+     * @param int $post_id The {@link WP_Post}'s id.
266
+     *
267
+     * @return array An array of delete statements.
268
+     */
269
+    private function get_delete_triples( $post_id ) {
270
+        $delete_triples = array();
271
+
272
+        // Loop through all renditions and get the triples.
273
+        foreach ( $this->schema_service->get_renditions() as $rendition ) {
274
+            // Push the rendition delete triple to $delete_triples.
275
+            $delete_triples = array_merge(
276
+                $delete_triples,
277
+                (array) $rendition->get_delete_triples( $post_id )
278
+            );
279
+        }
280
+
281
+        /**
282
+         * Filter: 'wl_delete_triples' - Allow third parties to hook and add additional delete triples.
283
+         *
284
+         * @since 3.18.0
285
+         *
286
+         * @param array $delete_triples Delete triples.
287
+         * @param int   $post_id        The current post ID.
288
+         */
289
+        return apply_filters( 'wl_delete_triples', array_unique( $delete_triples ), $post_id );
290
+    }
291
+
292
+    /**
293
+     * Get the SPARQL insert triples ( ?s ?p ?o ) for the specified {@link WP_Post}.
294
+     *
295
+     * @since 3.15.0
296
+     *
297
+     * @param int $post_id The {@link WP_Post}'s id.
298
+     *
299
+     * @return array An array of insert triples.
300
+     */
301
+    private function get_insert_triples( $post_id ) {
302
+
303
+        // Get the entity type.
304
+        $type = $this->entity_type_service->get( $post_id );
305
+
306
+        // Get the `linked_data` parameter.
307
+        $properties = $type['linked_data'];
308
+
309
+        // Accumulate the triples.
310
+        $triples = array();
311
+
312
+        /** @var Wordlift_Default_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */
313
+        foreach ( $properties as $property ) {
314
+            foreach ( $property->get_insert_triples( $post_id ) as $triple ) {
315
+                $triples[] = $triple;
316
+            }
317
+        }
318
+
319
+        $this->log->trace( count( $properties ) . ' properties and ' . count( $triples ) . " triples found for post $post_id." );
320
+
321
+        /**
322
+         * Get the INSERT triples properties.
323
+         *
324
+         * The `wl_insert_triples` filter allows 3rd parties to extend
325
+         * the list of triples for SPARQL INSERT statements.
326
+         *
327
+         * @since 3.17.0
328
+         * @since 3.18.0 The hook has been renamed from `wl_insert_tuples_properties` to `wl_insert_triples`.
329
+         *
330
+         * @param array                         $linked_data         A {@link Wordlift_Sparql_Tuple_Rendition} instances.
331
+         * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
332
+         * @param int                           $post_id             The {@link WP_Post}'s id.
333
+         */
334
+        return apply_filters( 'wl_insert_triples', $triples, $this->entity_service, $post_id );
335
+    }
336
+    //</editor-fold>
337 337
 
338 338
 }
Please login to merge, or discard this patch.
Spacing   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -84,9 +84,9 @@  discard block
 block discarded – undo
84 84
 	 * @param \Wordlift_Schema_Service      $schema_service      The {@link Wordlift_Schema_Service} instance.
85 85
 	 * @param \Wordlift_Sparql_Service      $sparql_service      The {@link Wordlift_Sparql_Service} instance.
86 86
 	 */
87
-	public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) {
87
+	public function __construct($entity_service, $entity_type_service, $schema_service, $sparql_service) {
88 88
 
89
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' );
89
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Linked_Data_Service');
90 90
 
91 91
 		$this->entity_service      = $entity_service;
92 92
 		$this->entity_type_service = $entity_type_service;
@@ -121,9 +121,9 @@  discard block
 block discarded – undo
121 121
 	 *
122 122
 	 * @param int $post_id The {@link WP_Post}'s id.
123 123
 	 */
124
-	public function push( $post_id ) {
124
+	public function push($post_id) {
125 125
 
126
-		$this->log->debug( "Pushing post $post_id..." );
126
+		$this->log->debug("Pushing post $post_id...");
127 127
 
128 128
 		// @since 3.18.0 we don't check anymore if the post is an entity, i.e.
129 129
 		// we removed the following:
@@ -134,10 +134,10 @@  discard block
 block discarded – undo
134 134
 		//		}
135 135
 
136 136
 		// Get the post and push it to the Linked Data store.
137
-		$this->do_push( $post_id );
137
+		$this->do_push($post_id);
138 138
 
139 139
 		// Reindex the triple store if buffering is turned off.
140
-		if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) {
140
+		if (false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING) {
141 141
 			wordlift_reindex_triple_store();
142 142
 		}
143 143
 
@@ -150,38 +150,38 @@  discard block
 block discarded – undo
150 150
 	 *
151 151
 	 * @param int $post_id The {@link WP_Post}'s id.
152 152
 	 */
153
-	private function do_push( $post_id ) {
154
-		$this->log->debug( "Doing post $post_id push..." );
153
+	private function do_push($post_id) {
154
+		$this->log->debug("Doing post $post_id push...");
155 155
 
156 156
 		// Get the post.
157
-		$post = get_post( $post_id );
157
+		$post = get_post($post_id);
158 158
 
159 159
 		// Bail out if the post isn't found.
160
-		if ( null === $post ) {
161
-			$this->log->warn( "Post $post_id not found." );
160
+		if (null === $post) {
161
+			$this->log->warn("Post $post_id not found.");
162 162
 
163 163
 			return;
164 164
 		}
165 165
 
166 166
 		// Bail out if the post isn't published.
167
-		if ( 'publish' !== $post->post_status ) {
168
-			$this->log->info( "Post $post_id not published." );
167
+		if ('publish' !== $post->post_status) {
168
+			$this->log->info("Post $post_id not published.");
169 169
 
170 170
 			return;
171 171
 		}
172 172
 
173 173
 		// Bail out if the URI isn't valid.
174
-		if ( ! $this->has_valid_uri( $post_id ) ) {
175
-			$this->log->warn( "Post $post_id URI invalid." );
174
+		if ( ! $this->has_valid_uri($post_id)) {
175
+			$this->log->warn("Post $post_id URI invalid.");
176 176
 
177 177
 			return;
178 178
 		}
179 179
 
180 180
 		// First remove the post data.
181
-		$this->remove( $post_id );
181
+		$this->remove($post_id);
182 182
 
183 183
 		// Then execute the insert query.
184
-		$this->insert( $post_id );
184
+		$this->insert($post_id);
185 185
 	}
186 186
 
187 187
 	/**
@@ -193,18 +193,18 @@  discard block
 block discarded – undo
193 193
 	 *
194 194
 	 * @return bool True if the URI is valid otherwise false.
195 195
 	 */
196
-	private function has_valid_uri( $post_id ) {
196
+	private function has_valid_uri($post_id) {
197 197
 
198 198
 		// Get the entity's URI.
199
-		$uri = $this->entity_service->get_uri( $post_id );
199
+		$uri = $this->entity_service->get_uri($post_id);
200 200
 
201 201
 		// If the URI isn't found, return false.
202
-		if ( null === $uri ) {
202
+		if (null === $uri) {
203 203
 			return false;
204 204
 		}
205 205
 
206 206
 		// If the URI ends with a trailing slash, return false.
207
-		if ( '/' === substr( $uri, - 1 ) ) {
207
+		if ('/' === substr($uri, - 1)) {
208 208
 			return false;
209 209
 		}
210 210
 
@@ -219,20 +219,20 @@  discard block
 block discarded – undo
219 219
 	 *
220 220
 	 * @param int $post_id The {@link WP_Post}'s id.
221 221
 	 */
222
-	public function remove( $post_id ) {
222
+	public function remove($post_id) {
223 223
 		$delete_query = '';
224 224
 
225 225
 		// Get the delete statements.
226
-		$triples = $this->get_delete_triples( $post_id );
226
+		$triples = $this->get_delete_triples($post_id);
227 227
 
228 228
 		// Loop through all triples and add the statement to delete query.
229
-		foreach ( $triples as $item ) {
229
+		foreach ($triples as $item) {
230 230
 			$delete_query .= "DELETE { $item } WHERE { $item }; \n";
231 231
 		}
232 232
 
233
-		$this->log->trace( "Delete Query generated [ $delete_query ]." );
233
+		$this->log->trace("Delete Query generated [ $delete_query ].");
234 234
 
235
-		$this->sparql_service->execute( $delete_query );
235
+		$this->sparql_service->execute($delete_query);
236 236
 	}
237 237
 
238 238
 	/**
@@ -242,19 +242,19 @@  discard block
 block discarded – undo
242 242
 	 *
243 243
 	 * @param int $post_id The {@link WP_Post}'s id.
244 244
 	 */
245
-	private function insert( $post_id ) {
245
+	private function insert($post_id) {
246 246
 		// Get the insert statements.
247
-		$insert_triples = $this->get_insert_triples( $post_id );
247
+		$insert_triples = $this->get_insert_triples($post_id);
248 248
 
249 249
 		// Convert all statements to single string.
250
-		$insert_query_body = implode( "\n", $insert_triples );
250
+		$insert_query_body = implode("\n", $insert_triples);
251 251
 
252 252
 		// Build the insert query.
253 253
 		$insert_query = "INSERT DATA { $insert_query_body };";
254 254
 
255
-		$this->log->trace( "Insert Query generated [ $insert_query ]." );
255
+		$this->log->trace("Insert Query generated [ $insert_query ].");
256 256
 
257
-		$this->sparql_service->execute( $insert_query );
257
+		$this->sparql_service->execute($insert_query);
258 258
 	}
259 259
 
260 260
 	/**
@@ -266,15 +266,15 @@  discard block
 block discarded – undo
266 266
 	 *
267 267
 	 * @return array An array of delete statements.
268 268
 	 */
269
-	private function get_delete_triples( $post_id ) {
269
+	private function get_delete_triples($post_id) {
270 270
 		$delete_triples = array();
271 271
 
272 272
 		// Loop through all renditions and get the triples.
273
-		foreach ( $this->schema_service->get_renditions() as $rendition ) {
273
+		foreach ($this->schema_service->get_renditions() as $rendition) {
274 274
 			// Push the rendition delete triple to $delete_triples.
275 275
 			$delete_triples = array_merge(
276 276
 				$delete_triples,
277
-				(array) $rendition->get_delete_triples( $post_id )
277
+				(array) $rendition->get_delete_triples($post_id)
278 278
 			);
279 279
 		}
280 280
 
@@ -286,7 +286,7 @@  discard block
 block discarded – undo
286 286
 		 * @param array $delete_triples Delete triples.
287 287
 		 * @param int   $post_id        The current post ID.
288 288
 		 */
289
-		return apply_filters( 'wl_delete_triples', array_unique( $delete_triples ), $post_id );
289
+		return apply_filters('wl_delete_triples', array_unique($delete_triples), $post_id);
290 290
 	}
291 291
 
292 292
 	/**
@@ -298,10 +298,10 @@  discard block
 block discarded – undo
298 298
 	 *
299 299
 	 * @return array An array of insert triples.
300 300
 	 */
301
-	private function get_insert_triples( $post_id ) {
301
+	private function get_insert_triples($post_id) {
302 302
 
303 303
 		// Get the entity type.
304
-		$type = $this->entity_type_service->get( $post_id );
304
+		$type = $this->entity_type_service->get($post_id);
305 305
 
306 306
 		// Get the `linked_data` parameter.
307 307
 		$properties = $type['linked_data'];
@@ -310,13 +310,13 @@  discard block
 block discarded – undo
310 310
 		$triples = array();
311 311
 
312 312
 		/** @var Wordlift_Default_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */
313
-		foreach ( $properties as $property ) {
314
-			foreach ( $property->get_insert_triples( $post_id ) as $triple ) {
313
+		foreach ($properties as $property) {
314
+			foreach ($property->get_insert_triples($post_id) as $triple) {
315 315
 				$triples[] = $triple;
316 316
 			}
317 317
 		}
318 318
 
319
-		$this->log->trace( count( $properties ) . ' properties and ' . count( $triples ) . " triples found for post $post_id." );
319
+		$this->log->trace(count($properties).' properties and '.count($triples)." triples found for post $post_id.");
320 320
 
321 321
 		/**
322 322
 		 * Get the INSERT triples properties.
@@ -331,7 +331,7 @@  discard block
 block discarded – undo
331 331
 		 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
332 332
 		 * @param int                           $post_id             The {@link WP_Post}'s id.
333 333
 		 */
334
-		return apply_filters( 'wl_insert_triples', $triples, $this->entity_service, $post_id );
334
+		return apply_filters('wl_insert_triples', $triples, $this->entity_service, $post_id);
335 335
 	}
336 336
 	//</editor-fold>
337 337
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-publisher-service.php 2 patches
Indentation   +214 added lines, -214 removed lines patch added patch discarded remove patch
@@ -18,222 +18,222 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Publisher_Service {
20 20
 
21
-	/**
22
-	 * The {@link Wordlift_Configuration_Service} instance.
23
-	 *
24
-	 * @since  3.19.0
25
-	 * @access private
26
-	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
27
-	 */
28
-	private $configuration_service;
29
-
30
-	/**
31
-	 * The {@link Wordlift_Publisher_Service} instance.
32
-	 *
33
-	 * @since 3.19.0
34
-	 *
35
-	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
36
-	 */
37
-	public function __construct( $configuration_service ) {
38
-		$this->configuration_service = $configuration_service;
39
-	}
40
-
41
-	/**
42
-	 * Counts the number of potential publishers.
43
-	 *
44
-	 * @since 3.11.0
45
-	 *
46
-	 * @return int The number of potential publishers.
47
-	 */
48
-	public function count() {
49
-
50
-		// Search for entities which are either a Person
51
-		// or Organization.
52
-
53
-		// Get only the ids as all we need is the count.
54
-		$entities = get_posts( array(
55
-			'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
56
-			'post_status'    => 'publish',
57
-			'posts_per_page' => - 1,
58
-			'tax_query'      => array(
59
-				array(
60
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
61
-					'field'    => 'slug',
62
-					'terms'    => array( 'organization', 'person' ),
63
-				),
64
-			),
65
-			'fields'         => 'ids',
66
-		) );
67
-
68
-		// Finally return the count.
69
-		return count( $entities );
70
-	}
71
-
72
-	/**
73
-	 * Search SQL filter for matching against post title only.
74
-	 *
75
-	 * @link    http://wordpress.stackexchange.com/a/11826/1685
76
-	 *
77
-	 * @since   3.15.0
78
-	 *
79
-	 * @param   string   $search   The search string.
80
-	 * @param   WP_Query $wp_query The {@link WP_Query} instance.
81
-	 *
82
-	 * @return array|string An array of results.
83
-	 */
84
-	public function limit_search_to_title( $search, $wp_query ) {
85
-
86
-		// Bail out if the search or the `search_terms` haven't been set.
87
-		if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) {
88
-			return $search;
89
-		}
90
-
91
-		global $wpdb;
92
-
93
-		$query_vars = $wp_query->query_vars;
94
-		$percent    = ! empty( $query_vars['exact'] ) ? '' : '%';
95
-		$search     = array();
96
-
97
-		foreach ( (array) $query_vars['search_terms'] as $term ) {
98
-			$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent );
99
-		}
100
-
101
-		if ( ! is_user_logged_in() ) {
102
-			$search[] = "$wpdb->posts.post_password = ''";
103
-		}
104
-
105
-		$search = ' AND ' . implode( ' AND ', $search );
106
-
107
-		return $search;
108
-	}
109
-
110
-	/**
111
-	 * Query WP for potential publishers, i.e. {@link WP_Post}s which are associated`
112
-	 * with `wl_entity_type` (taxonomy) terms of `Organization` or `Person`.
113
-	 *
114
-	 * @since 3.11.0
115
-	 *
116
-	 * @param string $filter The title filter.
117
-	 *
118
-	 * @return array An array of results in a select2 friendly format.
119
-	 */
120
-	public function query( $filter = '' ) {
121
-
122
-		// Search for the filter in the titles only.
123
-		add_filter( 'posts_search', array(
124
-			$this,
125
-			'limit_search_to_title',
126
-		), 10, 2 );
127
-
128
-		/*
21
+    /**
22
+     * The {@link Wordlift_Configuration_Service} instance.
23
+     *
24
+     * @since  3.19.0
25
+     * @access private
26
+     * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
27
+     */
28
+    private $configuration_service;
29
+
30
+    /**
31
+     * The {@link Wordlift_Publisher_Service} instance.
32
+     *
33
+     * @since 3.19.0
34
+     *
35
+     * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
36
+     */
37
+    public function __construct( $configuration_service ) {
38
+        $this->configuration_service = $configuration_service;
39
+    }
40
+
41
+    /**
42
+     * Counts the number of potential publishers.
43
+     *
44
+     * @since 3.11.0
45
+     *
46
+     * @return int The number of potential publishers.
47
+     */
48
+    public function count() {
49
+
50
+        // Search for entities which are either a Person
51
+        // or Organization.
52
+
53
+        // Get only the ids as all we need is the count.
54
+        $entities = get_posts( array(
55
+            'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
56
+            'post_status'    => 'publish',
57
+            'posts_per_page' => - 1,
58
+            'tax_query'      => array(
59
+                array(
60
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
61
+                    'field'    => 'slug',
62
+                    'terms'    => array( 'organization', 'person' ),
63
+                ),
64
+            ),
65
+            'fields'         => 'ids',
66
+        ) );
67
+
68
+        // Finally return the count.
69
+        return count( $entities );
70
+    }
71
+
72
+    /**
73
+     * Search SQL filter for matching against post title only.
74
+     *
75
+     * @link    http://wordpress.stackexchange.com/a/11826/1685
76
+     *
77
+     * @since   3.15.0
78
+     *
79
+     * @param   string   $search   The search string.
80
+     * @param   WP_Query $wp_query The {@link WP_Query} instance.
81
+     *
82
+     * @return array|string An array of results.
83
+     */
84
+    public function limit_search_to_title( $search, $wp_query ) {
85
+
86
+        // Bail out if the search or the `search_terms` haven't been set.
87
+        if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) {
88
+            return $search;
89
+        }
90
+
91
+        global $wpdb;
92
+
93
+        $query_vars = $wp_query->query_vars;
94
+        $percent    = ! empty( $query_vars['exact'] ) ? '' : '%';
95
+        $search     = array();
96
+
97
+        foreach ( (array) $query_vars['search_terms'] as $term ) {
98
+            $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent );
99
+        }
100
+
101
+        if ( ! is_user_logged_in() ) {
102
+            $search[] = "$wpdb->posts.post_password = ''";
103
+        }
104
+
105
+        $search = ' AND ' . implode( ' AND ', $search );
106
+
107
+        return $search;
108
+    }
109
+
110
+    /**
111
+     * Query WP for potential publishers, i.e. {@link WP_Post}s which are associated`
112
+     * with `wl_entity_type` (taxonomy) terms of `Organization` or `Person`.
113
+     *
114
+     * @since 3.11.0
115
+     *
116
+     * @param string $filter The title filter.
117
+     *
118
+     * @return array An array of results in a select2 friendly format.
119
+     */
120
+    public function query( $filter = '' ) {
121
+
122
+        // Search for the filter in the titles only.
123
+        add_filter( 'posts_search', array(
124
+            $this,
125
+            'limit_search_to_title',
126
+        ), 10, 2 );
127
+
128
+        /*
129 129
 		 * Search for entities which are either a Person
130 130
 		 * or Organization. Sort the results by title in ascending order.
131 131
 		 */
132
-		$entities = get_posts( array(
133
-			'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
134
-			'post_status'    => 'publish',
135
-			'posts_per_page' => - 1,
136
-			'tax_query'      => array(
137
-				array(
138
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
139
-					'field'    => 'slug',
140
-					'terms'    => array( 'organization', 'person' ),
141
-				),
142
-			),
143
-			's'              => $filter,
144
-			'orderby'        => 'title',
145
-			'order'          => 'ASC',
146
-		) );
147
-
148
-		// Remove the search filter added before the query.
149
-		remove_filter( 'posts_search', array(
150
-			$this,
151
-			'limit_search_to_title',
152
-		), 10, 2 );
153
-
154
-		// Set a reference to ourselves to pass to the closure.
155
-		$publisher_service = $this;
156
-
157
-		// Map the results in a `Select2` compatible array.
158
-		return array_map( function ( $entity ) use ( $publisher_service ) {
159
-			$type     = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
160
-			$thumb_id = get_post_thumbnail_id( $entity->ID );
161
-
162
-			return array(
163
-				'id'            => $entity->ID,
164
-				'text'          => $entity->post_title,
165
-				'type'          => $type[0]->name,
166
-				'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ),
167
-			);
168
-		}, $entities );
169
-	}
170
-
171
-	/**
172
-	 * Get the thumbnail's URL.
173
-	 *
174
-	 * @since 3.11.0
175
-	 *
176
-	 * @param int    $attachment_id The attachment id.
177
-	 * @param string $size          The attachment size (default = 'thumbnail').
178
-	 *
179
-	 * @return string|bool The image URL or false if not found.
180
-	 */
181
-	public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) {
182
-
183
-		$image = wp_get_attachment_image_src( $attachment_id, $size );
184
-
185
-		return isset( $image['0'] ) ? $image['0'] : false;
186
-	}
187
-
188
-	/**
189
-	 * Add additional instructions to featured image metabox
190
-	 * when the entity type is the publisher.
191
-	 *
192
-	 * @since  3.19.0
193
-	 *
194
-	 * @param  string $content Current metabox content.
195
-	 *
196
-	 * @return string $content metabox content with additional instructions.
197
-	 */
198
-	public function add_featured_image_instruction( $content ) {
199
-		// Get the current post ID.
200
-		$post_id = get_the_ID();
201
-
202
-		// Get the publisher id.
203
-		$publisher_id = $this->configuration_service->get_publisher_id();
204
-
205
-
206
-		// Bail if for some reason the post id is not set.
207
-		if (
208
-			empty( $post_id ) ||
209
-			$post_id !== (int) $publisher_id
210
-		) {
211
-			return $content;
212
-		}
213
-
214
-		$terms = wp_get_post_terms(
215
-			$post_id, // The post id.
216
-			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, // The taxonomy slug.
217
-			array(
218
-				'fields' => 'slugs',
219
-				// We don't need all fields, but only slugs.
220
-			)
221
-		);
222
-
223
-		// Check that the entity type is "Organization".
224
-		if ( in_array( 'organization', $terms, true ) ) {
225
-			// Add the featured image description when the type is "Organization".
226
-
227
-			$link = sprintf( '<a target="_blank" href="%s">%s</a>',
228
-				esc_attr__( 'https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift' ),
229
-				esc_html__( 'AMP logo guidelines', 'wordlift' ) );
230
-			$content .= sprintf( '<p>'
231
-								 . esc_html_x( 'According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift' )
232
-								 . '</p>', $link );
233
-		}
234
-
235
-		// Finally return the content.
236
-		return $content;
237
-	}
132
+        $entities = get_posts( array(
133
+            'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
134
+            'post_status'    => 'publish',
135
+            'posts_per_page' => - 1,
136
+            'tax_query'      => array(
137
+                array(
138
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
139
+                    'field'    => 'slug',
140
+                    'terms'    => array( 'organization', 'person' ),
141
+                ),
142
+            ),
143
+            's'              => $filter,
144
+            'orderby'        => 'title',
145
+            'order'          => 'ASC',
146
+        ) );
147
+
148
+        // Remove the search filter added before the query.
149
+        remove_filter( 'posts_search', array(
150
+            $this,
151
+            'limit_search_to_title',
152
+        ), 10, 2 );
153
+
154
+        // Set a reference to ourselves to pass to the closure.
155
+        $publisher_service = $this;
156
+
157
+        // Map the results in a `Select2` compatible array.
158
+        return array_map( function ( $entity ) use ( $publisher_service ) {
159
+            $type     = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
160
+            $thumb_id = get_post_thumbnail_id( $entity->ID );
161
+
162
+            return array(
163
+                'id'            => $entity->ID,
164
+                'text'          => $entity->post_title,
165
+                'type'          => $type[0]->name,
166
+                'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ),
167
+            );
168
+        }, $entities );
169
+    }
170
+
171
+    /**
172
+     * Get the thumbnail's URL.
173
+     *
174
+     * @since 3.11.0
175
+     *
176
+     * @param int    $attachment_id The attachment id.
177
+     * @param string $size          The attachment size (default = 'thumbnail').
178
+     *
179
+     * @return string|bool The image URL or false if not found.
180
+     */
181
+    public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) {
182
+
183
+        $image = wp_get_attachment_image_src( $attachment_id, $size );
184
+
185
+        return isset( $image['0'] ) ? $image['0'] : false;
186
+    }
187
+
188
+    /**
189
+     * Add additional instructions to featured image metabox
190
+     * when the entity type is the publisher.
191
+     *
192
+     * @since  3.19.0
193
+     *
194
+     * @param  string $content Current metabox content.
195
+     *
196
+     * @return string $content metabox content with additional instructions.
197
+     */
198
+    public function add_featured_image_instruction( $content ) {
199
+        // Get the current post ID.
200
+        $post_id = get_the_ID();
201
+
202
+        // Get the publisher id.
203
+        $publisher_id = $this->configuration_service->get_publisher_id();
204
+
205
+
206
+        // Bail if for some reason the post id is not set.
207
+        if (
208
+            empty( $post_id ) ||
209
+            $post_id !== (int) $publisher_id
210
+        ) {
211
+            return $content;
212
+        }
213
+
214
+        $terms = wp_get_post_terms(
215
+            $post_id, // The post id.
216
+            Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, // The taxonomy slug.
217
+            array(
218
+                'fields' => 'slugs',
219
+                // We don't need all fields, but only slugs.
220
+            )
221
+        );
222
+
223
+        // Check that the entity type is "Organization".
224
+        if ( in_array( 'organization', $terms, true ) ) {
225
+            // Add the featured image description when the type is "Organization".
226
+
227
+            $link = sprintf( '<a target="_blank" href="%s">%s</a>',
228
+                esc_attr__( 'https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift' ),
229
+                esc_html__( 'AMP logo guidelines', 'wordlift' ) );
230
+            $content .= sprintf( '<p>'
231
+                                    . esc_html_x( 'According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift' )
232
+                                    . '</p>', $link );
233
+        }
234
+
235
+        // Finally return the content.
236
+        return $content;
237
+    }
238 238
 
239 239
 }
Please login to merge, or discard this patch.
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
 	 *
35 35
 	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
36 36
 	 */
37
-	public function __construct( $configuration_service ) {
37
+	public function __construct($configuration_service) {
38 38
 		$this->configuration_service = $configuration_service;
39 39
 	}
40 40
 
@@ -51,22 +51,22 @@  discard block
 block discarded – undo
51 51
 		// or Organization.
52 52
 
53 53
 		// Get only the ids as all we need is the count.
54
-		$entities = get_posts( array(
54
+		$entities = get_posts(array(
55 55
 			'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
56 56
 			'post_status'    => 'publish',
57
-			'posts_per_page' => - 1,
57
+			'posts_per_page' => -1,
58 58
 			'tax_query'      => array(
59 59
 				array(
60 60
 					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
61 61
 					'field'    => 'slug',
62
-					'terms'    => array( 'organization', 'person' ),
62
+					'terms'    => array('organization', 'person'),
63 63
 				),
64 64
 			),
65 65
 			'fields'         => 'ids',
66
-		) );
66
+		));
67 67
 
68 68
 		// Finally return the count.
69
-		return count( $entities );
69
+		return count($entities);
70 70
 	}
71 71
 
72 72
 	/**
@@ -81,28 +81,28 @@  discard block
 block discarded – undo
81 81
 	 *
82 82
 	 * @return array|string An array of results.
83 83
 	 */
84
-	public function limit_search_to_title( $search, $wp_query ) {
84
+	public function limit_search_to_title($search, $wp_query) {
85 85
 
86 86
 		// Bail out if the search or the `search_terms` haven't been set.
87
-		if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) {
87
+		if (empty($search) || empty($wp_query->query_vars['search_terms'])) {
88 88
 			return $search;
89 89
 		}
90 90
 
91 91
 		global $wpdb;
92 92
 
93 93
 		$query_vars = $wp_query->query_vars;
94
-		$percent    = ! empty( $query_vars['exact'] ) ? '' : '%';
94
+		$percent    = ! empty($query_vars['exact']) ? '' : '%';
95 95
 		$search     = array();
96 96
 
97
-		foreach ( (array) $query_vars['search_terms'] as $term ) {
98
-			$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent );
97
+		foreach ((array) $query_vars['search_terms'] as $term) {
98
+			$search[] = $wpdb->prepare("$wpdb->posts.post_title LIKE %s", $percent.$wpdb->esc_like($term).$percent);
99 99
 		}
100 100
 
101
-		if ( ! is_user_logged_in() ) {
101
+		if ( ! is_user_logged_in()) {
102 102
 			$search[] = "$wpdb->posts.post_password = ''";
103 103
 		}
104 104
 
105
-		$search = ' AND ' . implode( ' AND ', $search );
105
+		$search = ' AND '.implode(' AND ', $search);
106 106
 
107 107
 		return $search;
108 108
 	}
@@ -117,55 +117,55 @@  discard block
 block discarded – undo
117 117
 	 *
118 118
 	 * @return array An array of results in a select2 friendly format.
119 119
 	 */
120
-	public function query( $filter = '' ) {
120
+	public function query($filter = '') {
121 121
 
122 122
 		// Search for the filter in the titles only.
123
-		add_filter( 'posts_search', array(
123
+		add_filter('posts_search', array(
124 124
 			$this,
125 125
 			'limit_search_to_title',
126
-		), 10, 2 );
126
+		), 10, 2);
127 127
 
128 128
 		/*
129 129
 		 * Search for entities which are either a Person
130 130
 		 * or Organization. Sort the results by title in ascending order.
131 131
 		 */
132
-		$entities = get_posts( array(
132
+		$entities = get_posts(array(
133 133
 			'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
134 134
 			'post_status'    => 'publish',
135
-			'posts_per_page' => - 1,
135
+			'posts_per_page' => -1,
136 136
 			'tax_query'      => array(
137 137
 				array(
138 138
 					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
139 139
 					'field'    => 'slug',
140
-					'terms'    => array( 'organization', 'person' ),
140
+					'terms'    => array('organization', 'person'),
141 141
 				),
142 142
 			),
143 143
 			's'              => $filter,
144 144
 			'orderby'        => 'title',
145 145
 			'order'          => 'ASC',
146
-		) );
146
+		));
147 147
 
148 148
 		// Remove the search filter added before the query.
149
-		remove_filter( 'posts_search', array(
149
+		remove_filter('posts_search', array(
150 150
 			$this,
151 151
 			'limit_search_to_title',
152
-		), 10, 2 );
152
+		), 10, 2);
153 153
 
154 154
 		// Set a reference to ourselves to pass to the closure.
155 155
 		$publisher_service = $this;
156 156
 
157 157
 		// Map the results in a `Select2` compatible array.
158
-		return array_map( function ( $entity ) use ( $publisher_service ) {
159
-			$type     = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
160
-			$thumb_id = get_post_thumbnail_id( $entity->ID );
158
+		return array_map(function($entity) use ($publisher_service) {
159
+			$type     = wp_get_post_terms($entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
160
+			$thumb_id = get_post_thumbnail_id($entity->ID);
161 161
 
162 162
 			return array(
163 163
 				'id'            => $entity->ID,
164 164
 				'text'          => $entity->post_title,
165 165
 				'type'          => $type[0]->name,
166
-				'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ),
166
+				'thumbnail_url' => $publisher_service->get_attachment_image_url($thumb_id),
167 167
 			);
168
-		}, $entities );
168
+		}, $entities);
169 169
 	}
170 170
 
171 171
 	/**
@@ -178,11 +178,11 @@  discard block
 block discarded – undo
178 178
 	 *
179 179
 	 * @return string|bool The image URL or false if not found.
180 180
 	 */
181
-	public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) {
181
+	public function get_attachment_image_url($attachment_id, $size = 'thumbnail') {
182 182
 
183
-		$image = wp_get_attachment_image_src( $attachment_id, $size );
183
+		$image = wp_get_attachment_image_src($attachment_id, $size);
184 184
 
185
-		return isset( $image['0'] ) ? $image['0'] : false;
185
+		return isset($image['0']) ? $image['0'] : false;
186 186
 	}
187 187
 
188 188
 	/**
@@ -195,7 +195,7 @@  discard block
 block discarded – undo
195 195
 	 *
196 196
 	 * @return string $content metabox content with additional instructions.
197 197
 	 */
198
-	public function add_featured_image_instruction( $content ) {
198
+	public function add_featured_image_instruction($content) {
199 199
 		// Get the current post ID.
200 200
 		$post_id = get_the_ID();
201 201
 
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
 
206 206
 		// Bail if for some reason the post id is not set.
207 207
 		if (
208
-			empty( $post_id ) ||
208
+			empty($post_id) ||
209 209
 			$post_id !== (int) $publisher_id
210 210
 		) {
211 211
 			return $content;
@@ -221,15 +221,15 @@  discard block
 block discarded – undo
221 221
 		);
222 222
 
223 223
 		// Check that the entity type is "Organization".
224
-		if ( in_array( 'organization', $terms, true ) ) {
224
+		if (in_array('organization', $terms, true)) {
225 225
 			// Add the featured image description when the type is "Organization".
226 226
 
227
-			$link = sprintf( '<a target="_blank" href="%s">%s</a>',
228
-				esc_attr__( 'https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift' ),
229
-				esc_html__( 'AMP logo guidelines', 'wordlift' ) );
230
-			$content .= sprintf( '<p>'
231
-								 . esc_html_x( 'According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift' )
232
-								 . '</p>', $link );
227
+			$link = sprintf('<a target="_blank" href="%s">%s</a>',
228
+				esc_attr__('https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift'),
229
+				esc_html__('AMP logo guidelines', 'wordlift'));
230
+			$content .= sprintf('<p>'
231
+								 . esc_html_x('According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift')
232
+								 . '</p>', $link);
233 233
 		}
234 234
 
235 235
 		// Finally return the content.
Please login to merge, or discard this patch.
src/admin/wordlift_admin_shortcodes.php 2 patches
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -9,11 +9,11 @@  discard block
 block discarded – undo
9 9
  * Loads the buttons in TinyMCE.
10 10
  */
11 11
 function wl_admin_shortcode_buttons() {
12
-	// Only add hooks when the current user has permissions AND is in Rich Text editor mode
13
-	if ( ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) && get_user_option( 'rich_editing' ) ) {
14
-		// add_filter( 'mce_external_plugins', 'wl_admin_shortcode_buttons_register_tinymce_javascript' );
15
-		add_filter( 'mce_buttons', 'wl_admin_shortcode_register_buttons' );
16
-	}
12
+    // Only add hooks when the current user has permissions AND is in Rich Text editor mode
13
+    if ( ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) && get_user_option( 'rich_editing' ) ) {
14
+        // add_filter( 'mce_external_plugins', 'wl_admin_shortcode_buttons_register_tinymce_javascript' );
15
+        add_filter( 'mce_buttons', 'wl_admin_shortcode_register_buttons' );
16
+    }
17 17
 }
18 18
 
19 19
 /**
@@ -24,9 +24,9 @@  discard block
 block discarded – undo
24 24
  * @return array The buttons array including the *wl_shortcodes_menu*.
25 25
  */
26 26
 function wl_admin_shortcode_register_buttons( $buttons ) {
27
-	array_push( $buttons, 'wl_shortcodes_menu' );
27
+    array_push( $buttons, 'wl_shortcodes_menu' );
28 28
 
29
-	return $buttons;
29
+    return $buttons;
30 30
 }
31 31
 
32 32
 // init process for button control
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -10,9 +10,9 @@  discard block
 block discarded – undo
10 10
  */
11 11
 function wl_admin_shortcode_buttons() {
12 12
 	// Only add hooks when the current user has permissions AND is in Rich Text editor mode
13
-	if ( ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) && get_user_option( 'rich_editing' ) ) {
13
+	if ((current_user_can('edit_posts') || current_user_can('edit_pages')) && get_user_option('rich_editing')) {
14 14
 		// add_filter( 'mce_external_plugins', 'wl_admin_shortcode_buttons_register_tinymce_javascript' );
15
-		add_filter( 'mce_buttons', 'wl_admin_shortcode_register_buttons' );
15
+		add_filter('mce_buttons', 'wl_admin_shortcode_register_buttons');
16 16
 	}
17 17
 }
18 18
 
@@ -23,11 +23,11 @@  discard block
 block discarded – undo
23 23
  *
24 24
  * @return array The buttons array including the *wl_shortcodes_menu*.
25 25
  */
26
-function wl_admin_shortcode_register_buttons( $buttons ) {
27
-	array_push( $buttons, 'wl_shortcodes_menu' );
26
+function wl_admin_shortcode_register_buttons($buttons) {
27
+	array_push($buttons, 'wl_shortcodes_menu');
28 28
 
29 29
 	return $buttons;
30 30
 }
31 31
 
32 32
 // init process for button control
33
-add_action( 'admin_init', 'wl_admin_shortcode_buttons' );
33
+add_action('admin_init', 'wl_admin_shortcode_buttons');
Please login to merge, or discard this patch.
src/install/class-wordlift-install-3-18-3.php 2 patches
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -15,47 +15,47 @@  discard block
 block discarded – undo
15 15
  * @subpackage Wordlift/install
16 16
  */
17 17
 class Wordlift_Install_3_18_3 extends Wordlift_Install {
18
-	/**
19
-	 * @inheritdoc
20
-	 */
21
-	protected static $version = '3.18.3';
18
+    /**
19
+     * @inheritdoc
20
+     */
21
+    protected static $version = '3.18.3';
22 22
 
23
-	/**
24
-	 * @inheritdoc
25
-	 */
26
-	public function install() {
27
-		$this->set_article_term_to_posts();
28
-	}
23
+    /**
24
+     * @inheritdoc
25
+     */
26
+    public function install() {
27
+        $this->set_article_term_to_posts();
28
+    }
29 29
 
30
-	/**
31
-	 * Set default article term to posts
32
-	 * that exists in `wl_relation_instances` table.
33
-	 *
34
-	 * @since 3.18.3
35
-	 *
36
-	 * @return mixed False if the `article` doesn't exists.
37
-	 */
38
-	public function set_article_term_to_posts() {
39
-		// Load the global $wpdb;
40
-		global $wpdb;
30
+    /**
31
+     * Set default article term to posts
32
+     * that exists in `wl_relation_instances` table.
33
+     *
34
+     * @since 3.18.3
35
+     *
36
+     * @return mixed False if the `article` doesn't exists.
37
+     */
38
+    public function set_article_term_to_posts() {
39
+        // Load the global $wpdb;
40
+        global $wpdb;
41 41
 
42
-		// Get the article term.
43
-		$term = get_term_by(
44
-			'slug',
45
-			'article',
46
-			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME
47
-		);
42
+        // Get the article term.
43
+        $term = get_term_by(
44
+            'slug',
45
+            'article',
46
+            Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME
47
+        );
48 48
 
49
-		// Bail if term doesn't exist.
50
-		if ( empty( $term ) ) {
51
-			return false;
52
-		}
49
+        // Bail if term doesn't exist.
50
+        if ( empty( $term ) ) {
51
+            return false;
52
+        }
53 53
 
54
-		// Set `article` term to all posts that exists in
55
-		// `wl_relation_instances` table and don't have `article` term set.
56
-		$post_ids = $wpdb->get_results(
57
-			$wpdb->prepare(
58
-				"
54
+        // Set `article` term to all posts that exists in
55
+        // `wl_relation_instances` table and don't have `article` term set.
56
+        $post_ids = $wpdb->get_results(
57
+            $wpdb->prepare(
58
+                "
59 59
 				SELECT DISTINCT p.ID
60 60
 				FROM $wpdb->posts AS p
61 61
 				INNER JOIN {$wpdb->prefix}wl_relation_instances AS ri
@@ -70,19 +70,19 @@  discard block
 block discarded – undo
70 70
 					)
71 71
 				)
72 72
 				",
73
-				'post',
74
-				$term->term_id
75
-			)
76
-		);
73
+                'post',
74
+                $term->term_id
75
+            )
76
+        );
77 77
 
78
-		// Loop through all posts and set `article` term for each one.
79
-		foreach ($post_ids as $p) {
80
-			wp_set_object_terms(
81
-				(int) $p->ID,
82
-				$term->term_id,
83
-				Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME
84
-			);
85
-		}
86
-	}
78
+        // Loop through all posts and set `article` term for each one.
79
+        foreach ($post_ids as $p) {
80
+            wp_set_object_terms(
81
+                (int) $p->ID,
82
+                $term->term_id,
83
+                Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME
84
+            );
85
+        }
86
+    }
87 87
 
88 88
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -47,7 +47,7 @@
 block discarded – undo
47 47
 		);
48 48
 
49 49
 		// Bail if term doesn't exist.
50
-		if ( empty( $term ) ) {
50
+		if (empty($term)) {
51 51
 			return false;
52 52
 		}
53 53
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-jsonld-website-converter.php 2 patches
Indentation   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -15,68 +15,68 @@
 block discarded – undo
15 15
  */
16 16
 class Wordlift_Website_Jsonld_Converter extends Wordlift_Post_To_Jsonld_Converter {
17 17
 
18
-	/**
19
-	 * Convert the home/blog page to a JSON-LD array.
20
-	 *
21
-	 * @since 3.14.0
22
-	 *
23
-	 * @return array A JSON-LD array.
24
-	 */
25
-	public function create_schema() {
18
+    /**
19
+     * Convert the home/blog page to a JSON-LD array.
20
+     *
21
+     * @since 3.14.0
22
+     *
23
+     * @return array A JSON-LD array.
24
+     */
25
+    public function create_schema() {
26 26
 
27
-		// Create new jsonld.
28
-		$home_url = home_url( '/' );
27
+        // Create new jsonld.
28
+        $home_url = home_url( '/' );
29 29
 
30
-		$jsonld = array(
31
-			'@context'      => 'http://schema.org',
32
-			'@type'         => 'WebSite',
33
-			'@id'           => "$home_url#website",
34
-			'name'          => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
35
-			'alternateName' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ),
36
-			'url'           => $home_url,
37
-		);
30
+        $jsonld = array(
31
+            '@context'      => 'http://schema.org',
32
+            '@type'         => 'WebSite',
33
+            '@id'           => "$home_url#website",
34
+            'name'          => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
35
+            'alternateName' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ),
36
+            'url'           => $home_url,
37
+        );
38 38
 
39
-		// Add publisher information.
40
-		$this->set_publisher( $jsonld );
39
+        // Add publisher information.
40
+        $this->set_publisher( $jsonld );
41 41
 
42
-		// Add search action.
43
-		$this->set_search_action( $jsonld );
42
+        // Add search action.
43
+        $this->set_search_action( $jsonld );
44 44
 
45
-		/**
46
-		 * Call the `wl_website_jsonld` filter.
47
-		 *
48
-		 * @api
49
-		 *
50
-		 * @since 3.14.0
51
-		 *
52
-		 * @param array $jsonld The JSON-LD structure.
53
-		 */
54
-		return apply_filters( 'wl_website_jsonld', $jsonld );
55
-	}
45
+        /**
46
+         * Call the `wl_website_jsonld` filter.
47
+         *
48
+         * @api
49
+         *
50
+         * @since 3.14.0
51
+         *
52
+         * @param array $jsonld The JSON-LD structure.
53
+         */
54
+        return apply_filters( 'wl_website_jsonld', $jsonld );
55
+    }
56 56
 
57
-	/**
58
-	 * Add SearchAction part to the schema
59
-	 *
60
-	 * @since 3.14.0
61
-	 *
62
-	 * @param array $params The parameters array.
63
-	 */
64
-	private function set_search_action( &$params ) {
65
-		/**
66
-		 * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL.
67
-		 *
68
-		 * @since  3.14.0
69
-		 * @api    string $search_url The search URL for this site with a `{search_term_string}` variable.
70
-		 */
71
-		$search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' );
57
+    /**
58
+     * Add SearchAction part to the schema
59
+     *
60
+     * @since 3.14.0
61
+     *
62
+     * @param array $params The parameters array.
63
+     */
64
+    private function set_search_action( &$params ) {
65
+        /**
66
+         * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL.
67
+         *
68
+         * @since  3.14.0
69
+         * @api    string $search_url The search URL for this site with a `{search_term_string}` variable.
70
+         */
71
+        $search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' );
72 72
 
73
-		// Add search action
74
-		$params['potentialAction'] = array(
75
-			'@type'       => 'SearchAction',
76
-			'target'      => $search_url,
77
-			'query-input' => 'required name=search_term_string',
78
-		);
73
+        // Add search action
74
+        $params['potentialAction'] = array(
75
+            '@type'       => 'SearchAction',
76
+            'target'      => $search_url,
77
+            'query-input' => 'required name=search_term_string',
78
+        );
79 79
 
80
-	}
80
+    }
81 81
 
82 82
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -25,22 +25,22 @@  discard block
 block discarded – undo
25 25
 	public function create_schema() {
26 26
 
27 27
 		// Create new jsonld.
28
-		$home_url = home_url( '/' );
28
+		$home_url = home_url('/');
29 29
 
30 30
 		$jsonld = array(
31 31
 			'@context'      => 'http://schema.org',
32 32
 			'@type'         => 'WebSite',
33 33
 			'@id'           => "$home_url#website",
34
-			'name'          => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
35
-			'alternateName' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ),
34
+			'name'          => html_entity_decode(get_bloginfo('name'), ENT_QUOTES),
35
+			'alternateName' => html_entity_decode(get_bloginfo('description'), ENT_QUOTES),
36 36
 			'url'           => $home_url,
37 37
 		);
38 38
 
39 39
 		// Add publisher information.
40
-		$this->set_publisher( $jsonld );
40
+		$this->set_publisher($jsonld);
41 41
 
42 42
 		// Add search action.
43
-		$this->set_search_action( $jsonld );
43
+		$this->set_search_action($jsonld);
44 44
 
45 45
 		/**
46 46
 		 * Call the `wl_website_jsonld` filter.
@@ -51,7 +51,7 @@  discard block
 block discarded – undo
51 51
 		 *
52 52
 		 * @param array $jsonld The JSON-LD structure.
53 53
 		 */
54
-		return apply_filters( 'wl_website_jsonld', $jsonld );
54
+		return apply_filters('wl_website_jsonld', $jsonld);
55 55
 	}
56 56
 
57 57
 	/**
@@ -61,14 +61,14 @@  discard block
 block discarded – undo
61 61
 	 *
62 62
 	 * @param array $params The parameters array.
63 63
 	 */
64
-	private function set_search_action( &$params ) {
64
+	private function set_search_action(&$params) {
65 65
 		/**
66 66
 		 * Filter: 'wl_jsonld_search_url' - Allows filtering of the search URL.
67 67
 		 *
68 68
 		 * @since  3.14.0
69 69
 		 * @api    string $search_url The search URL for this site with a `{search_term_string}` variable.
70 70
 		 */
71
-		$search_url = apply_filters( 'wl_jsonld_search_url', home_url( '/' ) . '?s={search_term_string}' );
71
+		$search_url = apply_filters('wl_jsonld_search_url', home_url('/').'?s={search_term_string}');
72 72
 
73 73
 		// Add search action
74 74
 		$params['potentialAction'] = array(
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-post-type-service.php 2 patches
Indentation   +163 added lines, -163 removed lines patch added patch discarded remove patch
@@ -16,168 +16,168 @@
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Entity_Post_Type_Service {
18 18
 
19
-	/**
20
-	 * The entity post type.
21
-	 *
22
-	 * @since  3.6.0
23
-	 * @access private
24
-	 * @var string $post_type The entity post type.
25
-	 */
26
-	private $post_type;
27
-
28
-	/**
29
-	 * The entity type slug.
30
-	 *
31
-	 * @since  3.6.0
32
-	 * @access private
33
-	 * @var string $slug The entity type slug.
34
-	 */
35
-	private $slug;
36
-
37
-	/**
38
-	 * A {@link Wordlift_Log_Service} instance.
39
-	 *
40
-	 * @since  3.16.3
41
-	 * @access private
42
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
43
-	 */
44
-	private $log;
45
-
46
-	/**
47
-	 * A singleton instance of the entity type service.
48
-	 *
49
-	 * @since  3.6.0
50
-	 * @access private
51
-	 * @var Wordlift_Entity_Post_Type_Service
52
-	 */
53
-	private static $instance;
54
-
55
-	/**
56
-	 * Create an entity type service instance.
57
-	 *
58
-	 * @since 3.6.0
59
-	 *
60
-	 * @param string $post_type The post type, e.g. entity.
61
-	 * @param string $slug      The entity type slug, if the slug is empty, the default slug will be used.
62
-	 */
63
-	public function __construct( $post_type, $slug ) {
64
-
65
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
66
-
67
-		$this->post_type = $post_type;
68
-
69
-		// We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default
70
-		// to the type name.
71
-		$this->slug = $slug ?: $post_type;
72
-
73
-		self::$instance = $this;
74
-
75
-	}
76
-
77
-	/**
78
-	 * Get the entity type service singleton instance.
79
-	 *
80
-	 * @since 3.6.0
81
-	 *
82
-	 * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance.
83
-	 */
84
-	public static function get_instance() {
85
-
86
-		return self::$instance;
87
-	}
88
-
89
-	/**
90
-	 * Get the entity type slug.
91
-	 *
92
-	 * @since 3.6.0
93
-	 *
94
-	 * @return string The entity type slug.
95
-	 */
96
-	public function get_slug() {
97
-
98
-		return $this->slug;
99
-	}
100
-
101
-	/**
102
-	 * Get the entity post type name.
103
-	 *
104
-	 * @since 3.6.0
105
-	 *
106
-	 * @return string The entity post type.
107
-	 */
108
-	public function get_post_type() {
109
-
110
-		return $this->post_type;
111
-	}
112
-
113
-
114
-	/**
115
-	 * Register the WordLift entity post type. This method is hooked to WordPress' init action.
116
-	 *
117
-	 * @since 3.6.0
118
-	 */
119
-	public function register() {
120
-
121
-		$this->log->debug( "Registering post type $this->post_type..." );
122
-
123
-		$labels = array(
124
-			'name'               => _x( 'Vocabulary', 'post type general name', 'wordlift' ),
125
-			'singular_name'      => _x( 'Entity', 'post type singular name', 'wordlift' ),
126
-			'add_new'            => _x( 'Add New Entity', 'entity', 'wordlift' ),
127
-			'add_new_item'       => __( 'Add New Entity', 'wordlift' ),
128
-			'edit_item'          => __( 'Edit Entity', 'wordlift' ),
129
-			'new_item'           => __( 'New Entity', 'wordlift' ),
130
-			'all_items'          => __( 'All Entities', 'wordlift' ),
131
-			'view_item'          => __( 'View Entity', 'wordlift' ),
132
-			'search_items'       => __( 'Search in Vocabulary', 'wordlift' ),
133
-			'not_found'          => __( 'No entities found', 'wordlift' ),
134
-			'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ),
135
-			'parent_item_colon'  => '',
136
-			'menu_name'          => __( 'Vocabulary', 'wordlift' ),
137
-		);
138
-
139
-		$args = array(
140
-			'labels'          => $labels,
141
-			'description'     => 'Holds our vocabulary (set of entities) and entity specific data',
142
-			'public'          => true,
143
-			'menu_position'   => 20,
144
-			// after the pages menu.
145
-			// Add support for 'authors' and 'revisions':
146
-			// * see https://github.com/insideout10/wordlift-plugin/issues/395
147
-			// * see https://github.com/insideout10/wordlift-plugin/issues/376
148
-			'supports'        => array(
149
-				'title',
150
-				'editor',
151
-				'thumbnail',
152
-				'excerpt',
153
-				'comments',
154
-				'author',
155
-				'revisions',
156
-			),
157
-			'has_archive'     => true,
158
-			'menu_icon'       => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg',
159
-			// Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service.
160
-			'rewrite'         => array( 'slug' => $this->slug ),
161
-			'capability_type' => array(
162
-				'wordlift_entity',
163
-				'wordlift_entities',
164
-			),
165
-			'capabilities' => array(
166
-				'delete_posts' => 'delete_wordlift_entities',
167
-			)
168
-		);
169
-
170
-		register_post_type( $this->post_type, $args );
171
-
172
-		// Enable WP's standard `category` taxonomy for entities.
173
-		//
174
-		// While this enables editors to bind entities to the WP posts' category
175
-		// taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter
176
-		// WP's main category query to include the `entity` post type.
177
-		//
178
-		// See https://github.com/insideout10/wordlift-plugin/issues/442
179
-		register_taxonomy_for_object_type( 'category', $this->post_type );
180
-
181
-	}
19
+    /**
20
+     * The entity post type.
21
+     *
22
+     * @since  3.6.0
23
+     * @access private
24
+     * @var string $post_type The entity post type.
25
+     */
26
+    private $post_type;
27
+
28
+    /**
29
+     * The entity type slug.
30
+     *
31
+     * @since  3.6.0
32
+     * @access private
33
+     * @var string $slug The entity type slug.
34
+     */
35
+    private $slug;
36
+
37
+    /**
38
+     * A {@link Wordlift_Log_Service} instance.
39
+     *
40
+     * @since  3.16.3
41
+     * @access private
42
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
43
+     */
44
+    private $log;
45
+
46
+    /**
47
+     * A singleton instance of the entity type service.
48
+     *
49
+     * @since  3.6.0
50
+     * @access private
51
+     * @var Wordlift_Entity_Post_Type_Service
52
+     */
53
+    private static $instance;
54
+
55
+    /**
56
+     * Create an entity type service instance.
57
+     *
58
+     * @since 3.6.0
59
+     *
60
+     * @param string $post_type The post type, e.g. entity.
61
+     * @param string $slug      The entity type slug, if the slug is empty, the default slug will be used.
62
+     */
63
+    public function __construct( $post_type, $slug ) {
64
+
65
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
66
+
67
+        $this->post_type = $post_type;
68
+
69
+        // We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default
70
+        // to the type name.
71
+        $this->slug = $slug ?: $post_type;
72
+
73
+        self::$instance = $this;
74
+
75
+    }
76
+
77
+    /**
78
+     * Get the entity type service singleton instance.
79
+     *
80
+     * @since 3.6.0
81
+     *
82
+     * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance.
83
+     */
84
+    public static function get_instance() {
85
+
86
+        return self::$instance;
87
+    }
88
+
89
+    /**
90
+     * Get the entity type slug.
91
+     *
92
+     * @since 3.6.0
93
+     *
94
+     * @return string The entity type slug.
95
+     */
96
+    public function get_slug() {
97
+
98
+        return $this->slug;
99
+    }
100
+
101
+    /**
102
+     * Get the entity post type name.
103
+     *
104
+     * @since 3.6.0
105
+     *
106
+     * @return string The entity post type.
107
+     */
108
+    public function get_post_type() {
109
+
110
+        return $this->post_type;
111
+    }
112
+
113
+
114
+    /**
115
+     * Register the WordLift entity post type. This method is hooked to WordPress' init action.
116
+     *
117
+     * @since 3.6.0
118
+     */
119
+    public function register() {
120
+
121
+        $this->log->debug( "Registering post type $this->post_type..." );
122
+
123
+        $labels = array(
124
+            'name'               => _x( 'Vocabulary', 'post type general name', 'wordlift' ),
125
+            'singular_name'      => _x( 'Entity', 'post type singular name', 'wordlift' ),
126
+            'add_new'            => _x( 'Add New Entity', 'entity', 'wordlift' ),
127
+            'add_new_item'       => __( 'Add New Entity', 'wordlift' ),
128
+            'edit_item'          => __( 'Edit Entity', 'wordlift' ),
129
+            'new_item'           => __( 'New Entity', 'wordlift' ),
130
+            'all_items'          => __( 'All Entities', 'wordlift' ),
131
+            'view_item'          => __( 'View Entity', 'wordlift' ),
132
+            'search_items'       => __( 'Search in Vocabulary', 'wordlift' ),
133
+            'not_found'          => __( 'No entities found', 'wordlift' ),
134
+            'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ),
135
+            'parent_item_colon'  => '',
136
+            'menu_name'          => __( 'Vocabulary', 'wordlift' ),
137
+        );
138
+
139
+        $args = array(
140
+            'labels'          => $labels,
141
+            'description'     => 'Holds our vocabulary (set of entities) and entity specific data',
142
+            'public'          => true,
143
+            'menu_position'   => 20,
144
+            // after the pages menu.
145
+            // Add support for 'authors' and 'revisions':
146
+            // * see https://github.com/insideout10/wordlift-plugin/issues/395
147
+            // * see https://github.com/insideout10/wordlift-plugin/issues/376
148
+            'supports'        => array(
149
+                'title',
150
+                'editor',
151
+                'thumbnail',
152
+                'excerpt',
153
+                'comments',
154
+                'author',
155
+                'revisions',
156
+            ),
157
+            'has_archive'     => true,
158
+            'menu_icon'       => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg',
159
+            // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service.
160
+            'rewrite'         => array( 'slug' => $this->slug ),
161
+            'capability_type' => array(
162
+                'wordlift_entity',
163
+                'wordlift_entities',
164
+            ),
165
+            'capabilities' => array(
166
+                'delete_posts' => 'delete_wordlift_entities',
167
+            )
168
+        );
169
+
170
+        register_post_type( $this->post_type, $args );
171
+
172
+        // Enable WP's standard `category` taxonomy for entities.
173
+        //
174
+        // While this enables editors to bind entities to the WP posts' category
175
+        // taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter
176
+        // WP's main category query to include the `entity` post type.
177
+        //
178
+        // See https://github.com/insideout10/wordlift-plugin/issues/442
179
+        register_taxonomy_for_object_type( 'category', $this->post_type );
180
+
181
+    }
182 182
 
183 183
 }
Please login to merge, or discard this patch.
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -60,9 +60,9 @@  discard block
 block discarded – undo
60 60
 	 * @param string $post_type The post type, e.g. entity.
61 61
 	 * @param string $slug      The entity type slug, if the slug is empty, the default slug will be used.
62 62
 	 */
63
-	public function __construct( $post_type, $slug ) {
63
+	public function __construct($post_type, $slug) {
64 64
 
65
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
65
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
66 66
 
67 67
 		$this->post_type = $post_type;
68 68
 
@@ -118,22 +118,22 @@  discard block
 block discarded – undo
118 118
 	 */
119 119
 	public function register() {
120 120
 
121
-		$this->log->debug( "Registering post type $this->post_type..." );
121
+		$this->log->debug("Registering post type $this->post_type...");
122 122
 
123 123
 		$labels = array(
124
-			'name'               => _x( 'Vocabulary', 'post type general name', 'wordlift' ),
125
-			'singular_name'      => _x( 'Entity', 'post type singular name', 'wordlift' ),
126
-			'add_new'            => _x( 'Add New Entity', 'entity', 'wordlift' ),
127
-			'add_new_item'       => __( 'Add New Entity', 'wordlift' ),
128
-			'edit_item'          => __( 'Edit Entity', 'wordlift' ),
129
-			'new_item'           => __( 'New Entity', 'wordlift' ),
130
-			'all_items'          => __( 'All Entities', 'wordlift' ),
131
-			'view_item'          => __( 'View Entity', 'wordlift' ),
132
-			'search_items'       => __( 'Search in Vocabulary', 'wordlift' ),
133
-			'not_found'          => __( 'No entities found', 'wordlift' ),
134
-			'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ),
124
+			'name'               => _x('Vocabulary', 'post type general name', 'wordlift'),
125
+			'singular_name'      => _x('Entity', 'post type singular name', 'wordlift'),
126
+			'add_new'            => _x('Add New Entity', 'entity', 'wordlift'),
127
+			'add_new_item'       => __('Add New Entity', 'wordlift'),
128
+			'edit_item'          => __('Edit Entity', 'wordlift'),
129
+			'new_item'           => __('New Entity', 'wordlift'),
130
+			'all_items'          => __('All Entities', 'wordlift'),
131
+			'view_item'          => __('View Entity', 'wordlift'),
132
+			'search_items'       => __('Search in Vocabulary', 'wordlift'),
133
+			'not_found'          => __('No entities found', 'wordlift'),
134
+			'not_found_in_trash' => __('No entities found in the Trash', 'wordlift'),
135 135
 			'parent_item_colon'  => '',
136
-			'menu_name'          => __( 'Vocabulary', 'wordlift' ),
136
+			'menu_name'          => __('Vocabulary', 'wordlift'),
137 137
 		);
138 138
 
139 139
 		$args = array(
@@ -155,9 +155,9 @@  discard block
 block discarded – undo
155 155
 				'revisions',
156 156
 			),
157 157
 			'has_archive'     => true,
158
-			'menu_icon'       => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg',
158
+			'menu_icon'       => WP_CONTENT_URL.'/plugins/wordlift/images/svg/wl-vocabulary-icon.svg',
159 159
 			// Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service.
160
-			'rewrite'         => array( 'slug' => $this->slug ),
160
+			'rewrite'         => array('slug' => $this->slug),
161 161
 			'capability_type' => array(
162 162
 				'wordlift_entity',
163 163
 				'wordlift_entities',
@@ -167,7 +167,7 @@  discard block
 block discarded – undo
167 167
 			)
168 168
 		);
169 169
 
170
-		register_post_type( $this->post_type, $args );
170
+		register_post_type($this->post_type, $args);
171 171
 
172 172
 		// Enable WP's standard `category` taxonomy for entities.
173 173
 		//
@@ -176,7 +176,7 @@  discard block
 block discarded – undo
176 176
 		// WP's main category query to include the `entity` post type.
177 177
 		//
178 178
 		// See https://github.com/insideout10/wordlift-plugin/issues/442
179
-		register_taxonomy_for_object_type( 'category', $this->post_type );
179
+		register_taxonomy_for_object_type('category', $this->post_type);
180 180
 
181 181
 	}
182 182
 
Please login to merge, or discard this patch.