PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.4
WebberZone Top 10 — Popular Posts v4.3.4
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / includes / frontend / class-language-handler.php

class-language-handler.php in WebberZone Top 10 — Popular Posts 4.3.4, at includes/frontend/class-language-handler.php

118 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Language Handler class.
4 *
5 * @package WebberZone\Top_Ten\Frontend
6 */
7
8 namespace WebberZone\Top_Ten\Frontend;
9
10 use WebberZone\Top_Ten\Util\Hook_Registry;
11
12 // If this file is called directly, abort.
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * Language handler class.
19 *
20 * @since 3.3.3
21 */
22 class Language_Handler {
23
24 /**
25 * Constructor.
26 *
27 * @since 3.3.3
28 */
29 public function __construct() {
30 Hook_Registry::add_filter( 'top_ten_query_the_posts', array( $this, 'translate_ids' ), 999 );
31 Hook_Registry::add_filter( 'top_ten_query_posts_pre_query', array( $this, 'translate_ids' ), 999 );
32 }
33
34 /**
35 * Get the ID of a post in the current language. Works with WPML and PolyLang.
36 *
37 * @since 3.3.3
38 *
39 * @param int[] $results Arry of Posts.
40 * @return \WP_Post[] Updated array of WP_Post objects.
41 */
42 public static function translate_ids( $results ) {
43
44 if ( empty( $results ) ) {
45 return $results;
46 }
47
48 $processed_ids = array();
49 $processed_results = array();
50
51 foreach ( $results as $result ) {
52
53 $result = self::object_id_cur_lang( $result );
54 if ( ! $result ) {
55 continue;
56 }
57
58 // If this is NULL or already processed ID or matches current post then skip processing this loop.
59 if ( ! $result->ID || in_array( $result->ID, $processed_ids ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
60 continue;
61 }
62
63 // Push the current ID into the array to ensure we're not repeating it.
64 array_push( $processed_ids, $result->ID );
65
66 // Let's get the Post using the ID.
67 $result = get_post( $result );
68 array_push( $processed_results, $result );
69 }
70 return $processed_results;
71 }
72
73 /**
74 * Returns the object identifier for the current language (WPML).
75 *
76 * @since 3.3.3
77 *
78 * @param int|string|\WP_Post $post Post object or Post ID.
79 * @return \WP_Post|array|null Post opbject, updated if needed.
80 */
81 public static function object_id_cur_lang( $post ) {
82
83 $return_original_if_missing = false;
84
85 $post = get_post( $post );
86 $current_lang = apply_filters( 'wpml_current_language', null );
87
88 // Polylang implementation.
89 if ( function_exists( 'pll_get_post' ) ) {
90 $post = \pll_get_post( $post->ID );
91 $post = get_post( $post );
92 }
93
94 // WPML implementation.
95 /**
96 * Filter to modify if the original language ID is returned.
97 *
98 * @since 2.2.3
99 *
100 * @param bool $return_original_if_missing Flag to return original post ID if translated post ID is missing.
101 * @param int $id Post ID
102 */
103 $return_original_if_missing = apply_filters( 'tptn_wpml_return_original', $return_original_if_missing, $post->ID );
104
105 $post = apply_filters( 'wpml_object_id', $post->ID, $post->post_type, $return_original_if_missing, $current_lang );
106 $post = get_post( $post );
107
108 /**
109 * Filters Post object for current language.
110 *
111 * @since 2.1.0
112 *
113 * @param \WP_Post|array|null $id Post object.
114 */
115 return apply_filters( 'tptn_object_id_cur_lang', $post );
116 }
117 }
118