Completed
Pull Request — develop (#1147)
by Naveen
02:32
created

Wordlift_Post_Adapter::locale()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * An adapter to access and manipulate single post data for WordLift needs
4
 *
5
 * @since      3.14.0
6
 * @package    Wordlift
7
 * @subpackage Wordlift/admin
8
 */
9
10
/**
11
 * Define the {@link Wordlift_Post_Adatpter}.
12
 *
13
 * @since      3.14.0
14
 * @package    Wordlift
15
 * @subpackage Wordlift/admin
16
 */
17
class Wordlift_Post_Adapter {
18
19
	/**
20
	 * The post id to which the adopter relates.
21
	 *
22
	 * @since 3.14.0
23
	 *
24
	 * @var integer $post_id .
25
	 */
26
	private $post_id;
27
28
	const TYPE_ENTITY_LINK = 0;
29
	const TYPE_TERM_LINK = 1;
30
31
	/**
32
	 * Create the {@link Wordlift_Post_Adatpter} instance.
33
	 *
34
	 * @param integer $post_id the post ID of the post the adopter relates to.
35
	 *
36
	 * @since 3.14.0
37
	 *
38
	 */
39
	public function __construct( $post_id ) {
40
41
		$this->post_id = $post_id;
42
43
	}
44
45
	/**
46
	 * Get the word count of the post content.
47
	 *
48
	 * The count is calculated over the post content after stripping shortcodes and html tags.
49
	 *
50
	 * @return integer the number of words in the content after stripping shortcodes and html tags..
51
	 * @since 3.14.0
52
	 *
53
	 */
54
	public function word_count() {
55
56
		$post = get_post( $this->post_id );
57
58
		/*
59
		 * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g.
60
		 * because the content is written elsewhere.
61
		 *
62
		 * @since 3.20.0
63
		 */
64
		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
65
66
		return self::str_word_count_utf8( strip_tags( strip_shortcodes( $post_content ) ) );
67
	}
68
69
	/**
70
	 * Count words in the string, taking into account UTF-8 characters.
71
	 *
72
	 * @see https://github.com/insideout10/wordlift-plugin/issues/884
73
	 *
74
	 * @since 3.20.0
75
	 *
76
	 * @param string $str The target string.
77
	 *
78
	 * @return int The number of words.
79
	 */
80
	private static function str_word_count_utf8( $str ) {
81
82
		return count( preg_split( '~[^\p{L}\p{N}\']+~u', $str ) );
83
	}
84
85
	/**
86
	 * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL.
87
	 *
88
	 * @param int $post_id Post ID.
89
	 *
90
	 * @return string The post permalink.
91
	 * @since 3.20.0
92
	 *
93
	 */
94
	public static function get_production_permalink( $post_id ) {
95
96
		/**
97
		 * WordPress 4.4 doesn't support meta queries for terms, therefore we only support post permalinks here.
98
		 *
99
		 * Later on for WordPress 4.5+ we look for terms bound to the entity and we use the term link instead of the
100
		 * post permalink if we find them.
101
		 */
102
		global $wp_version;
103
		if ( version_compare( $wp_version, '4.5', '<' ) ) {
104
			$permalink = get_permalink( $post_id );
105
106
			return apply_filters( 'wl_production_permalink', $permalink, $post_id, self::TYPE_ENTITY_LINK, null );
107
		}
108
109
		/**
110
		 * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts
111
		 * when the production environment is copied over from a staging environment with staging
112
		 * URLs.
113
		 *
114
		 * @param string $permalink_url The default permalink.
115
		 * @param int $post_id The post id.
116
		 *
117
		 * @since 3.23.0 we check whether the entity is bound to a term and, in that case, we link to the term.
118
		 * @since 3.20.0
119
		 *
120
		 * @see https://github.com/insideout10/wordlift-plugin/issues/850
121
		 *
122
		 */
123
124
		// Get all the URIs for the entity, i.e. itemid and sameAs.
125
		$uris = array_merge(
126
			(array) Wordlift_Entity_Service::get_instance()->get_uri( $post_id ),
127
			get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS )
128
		);
129
130
		// Only try to link a term if `WL_ENABLE_TERM_LINKING` is enabled.
131
		$terms = array();
132
		if ( defined( 'WL_ENABLE_TERM_LINKING' ) && WL_ENABLE_TERM_LINKING ) {
133
			// Try to find one term matching the entity.
134
			$terms = get_terms( array(
135
				'number'                 => 1,
136
				'hide_empty'             => false,
137
				'update_term_meta_cache' => false,
138
				'meta_key'               => '_wl_entity_id',
139
				'meta_value'             => $uris,
140
				'meta_compare'           => 'IN',
141
			) );
142
		}
143
144
		// If found use the term link, otherwise the permalink.
145
		if ( 1 === count( $terms ) ) {
146
			$term      = current( $terms );
147
			$permalink = get_term_link( $term );
148
			$type      = self::TYPE_TERM_LINK;
149
		} else {
150
			$term      = null;
151
			$permalink = get_permalink( $post_id );
152
			$type      = self::TYPE_ENTITY_LINK;
153
		}
154
155
		/**
156
		 * Apply the `wl_production_permalink` filter.
157
		 *
158
		 * @param string $permalink The permalink.
159
		 * @param int $post_id The post id.
160
		 * @param int $type The permalink type: 0 = entity permalink, 1 = term link.
161
		 * @param WP_Term $term The term if type is term link, otherwise null.
162
		 *
163
		 * @since 3.23.0 add the permalink type and term parameters.
164
		 *
165
		 */
166
		return apply_filters( 'wl_production_permalink', $permalink, $post_id, $type, $term );
167
	}
168
169
	/**
170
	 * Get comma separated tags to be used as keywords
171
	 *
172
	 * @return string|void Comma separated tags
173
	 *
174
	 * @since 3.27.2
175
	 */
176 View Code Duplication
	public function keywords() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
		$tags = get_the_tags( $this->post_id );
178
179
		if ( empty( $tags ) ) {
180
			return;
181
		}
182
183
		return implode( ',', array_map( function ( $tag ) {
184
			return $tag->name;
185
		}, $tags ) );
186
	}
187
188
	/**
189
	 * Get comma separated categories to be used as article section
190
	 *
191
	 * @return string|void Comma separated categories
192
	 *
193
	 * @since 3.27.2
194
	 */
195 View Code Duplication
	public function article_section() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
		$categories = get_the_category( $this->post_id );
197
198
		if ( empty( $categories ) ) {
199
			return;
200
		}
201
202
		return implode( ',', array_map( function ( $tag ) {
203
			return $tag->cat_name;
204
		}, $categories ) );
205
	}
206
207
	/**
208
	 * Get comment count
209
	 *
210
	 * @return string|void Comment count
211
	 *
212
	 * @since 3.27.2
213
	 */
214
	public function comment_count() {
215
		return get_comments_number( $this->post_id );
216
	}
217
218
	/**
219
	 * Get Language
220
	 * Try WPML, Polylang for post specific languages, else fallback on get_locale()
221
	 *
222
	 * @return string|void Language code (locale)
223
	 *
224
	 * @since 3.27.2
225
	 */
226
	public function locale() {
227
		// WPML handling
228
		if ( function_exists( 'wpml_get_language_information' ) ) {
229
			$post_language = wpml_get_language_information( $this->post_id );
230
231
			return $post_language['locale'];
232
		}
233
234
		// Polylang handling
235
		if ( function_exists( 'pll_get_post_language' ) ) {
236
			return pll_get_post_language( $this->post_id, 'locale' );
237
		}
238
239
		return get_locale();
240
241
	}
242
243
	/**
244
	 * Add mentions to an article.
245
	 * To add mentions we just push the referenced entities to mentions.
246
	 *
247
	 * @param $post_id int
248
	 * @param $references int[]
249
	 */
250
	public function add_mentions( $post_id, &$references ) {
251
		$tags = get_the_tags( $post_id );
252
		// Loop through the tags and push it to references.
253
		foreach ( $tags as $tag ) {
254
			/**
255
			 * @var $tag WP_Term
256
			 */
257
			$entity_uris = get_term_meta( $tag->term_id, '_wl_entity_id' );
258
			foreach ( $entity_uris as $uri ) {
259
				$referenced_entity = Wordlift_Entity_Uri_Service::get_instance()->get_entity( $uri );
260
				if ( $referenced_entity instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
261
					// push the referenced entities to references.
262
					$references[] = $referenced_entity->ID;
263
				}
264
			}
265
		}
266
	}
267
268
}
269