Skip to content

Commit

Permalink
Fixes translation support
Browse files Browse the repository at this point in the history
Fixes #151
  • Loading branch information
ajaydsouza committed Oct 29, 2023
1 parent 02583df commit e1a1cbe
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 3 deletions.
14 changes: 11 additions & 3 deletions includes/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ final class Main {
*/
public $styles;

/**
* Language Handler.
*
* @since 3.3.0
*
* @var object Language Handler.
*/
public $language;

/**
* Gets the instance of the class.
*
Expand Down Expand Up @@ -127,13 +136,14 @@ private function __construct() {
* @since 3.3.0
*/
private function init() {
$this->language = new Frontend\Language_Handler();
$this->styles = new Frontend\Styles_Handler();
$this->counter = new Counter();
$this->tracker = new Tracker();
$this->shortcodes = new Frontend\Shortcodes();
$this->blocks = new Frontend\Blocks\Blocks();
$this->filters = new Frontend\Filters();
$this->feed = new Frontend\Feed();
$this->styles = new Frontend\Styles_Handler();

$this->hooks();

Expand Down Expand Up @@ -162,8 +172,6 @@ public function hooks() {
* @since 3.3.0
*/
public function initiate_plugin() {
load_plugin_textdomain( 'top-10', false, dirname( plugin_basename( TOP_TEN_PLUGIN_FILE ) ) . '/languages/' );

Frontend\Media_Handler::add_image_sizes();
}

Expand Down
125 changes: 125 additions & 0 deletions includes/frontend/class-language-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* Language handler
*
* @package Top_Ten
*/

namespace WebberZone\Top_Ten\Frontend;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}

/**
* Language handler class.
*
* @since 3.3.3
*/
class Language_Handler {

/**
* Constructor.
*
* @since 3.3.3
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
add_filter( 'top_ten_query_the_posts', array( $this, 'translate_ids' ), 999 );
}

/**
* Initialises text domain for l10n.
*
* @since 3.3.3
*
* @return void
*/
public static function load_plugin_textdomain() {
load_plugin_textdomain( 'top-10', false, dirname( plugin_basename( TOP_TEN_PLUGIN_FILE ) ) . '/languages/' );
}

/**
* Get the ID of a post in the current language. Works with WPML and PolyLang.
*
* @since 3.3.3
*
* @param int[] $results Arry of Posts.
* @return \WP_Post[] Updated array of WP_Post objects.
*/
public static function translate_ids( $results ) {
global $post;

$processed_ids = array();
$processed_results = array();

foreach ( $results as $result ) {

$result = self::object_id_cur_lang( $result );
if ( ! $result ) {
continue;
}

// If this is NULL or already processed ID or matches current post then skip processing this loop.
if ( ! $result->ID || in_array( $result->ID, $processed_ids ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
continue;
}

// Push the current ID into the array to ensure we're not repeating it.
array_push( $processed_ids, $result->ID );

// Let's get the Post using the ID.
$result = get_post( $result );
array_push( $processed_results, $result );
}
return $processed_results;
}

/**
* Returns the object identifier for the current language (WPML).
*
* @since 3.3.3
*
* @param int|string|\WP_Post $post Post object or Post ID.
* @return \WP_Post|array|null Post opbject, updated if needed.
*/
public static function object_id_cur_lang( $post ) {

$return_original_if_missing = false;

$post = get_post( $post );
$current_lang = apply_filters( 'wpml_current_language', null );

// Polylang implementation.
if ( function_exists( 'pll_get_post' ) ) {
$post = \pll_get_post( $post->ID );
$post = get_post( $post );
}

// WPML implementation.
if ( class_exists( 'SitePress' ) ) {
/**
* Filter to modify if the original language ID is returned.
*
* @since 2.2.3
*
* @param bool $return_original_if_missing Flag to return original post ID if translated post ID is missing.
* @param int $id Post ID
*/
$return_original_if_missing = apply_filters( 'tptn_wpml_return_original', $return_original_if_missing, $post->ID );

$post = apply_filters( 'wpml_object_id', $post->ID, $post->post_type, $return_original_if_missing, $current_lang );
$post = get_post( $post );
}

/**
* Filters Post object for current language.
*
* @since 2.1.0
*
* @param \WP_Post|array|null $id Post object.
*/
return apply_filters( 'tptn_object_id_cur_lang', $post );
}
}

0 comments on commit e1a1cbe

Please sign in to comment.