Skip to content

Commit

Permalink
Add support for WPML post locale
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-pike committed Jan 15, 2025
1 parent 1981ea6 commit 51604dd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
41 changes: 41 additions & 0 deletions integration/class-wpml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* WPML integration.
*
* @package Activitypub
*/

namespace Activitypub\Integration;

/**
* Compatibility with the WPML Multilingual CMS plugin.
*
* @see https://wpml.org/
*/
class WPML {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_filter( 'activitypub_post_locale', array( self::class, 'get_wpml_post_locale' ), 10, 3 );
}

/**
* Fetch the post locale from the WPML post data.
*
* @param string $lang The language code.
* @param int $post_id The post ID.
* @param WP_Post $post The post object
*
* @return string The modified language code.
*/
public static function get_wpml_post_locale( $lang, $post_id, $post ) {
$language_details = apply_filters( 'wpml_post_language_details', NULL, $post_id );

if ( is_array( $language_details ) && isset( $language_details['language_code'] ) ) {
$lang = $language_details['language_code'];
}

return $lang;
}
}
11 changes: 11 additions & 0 deletions integration/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ function ( $transformer, $data, $object_class ) {
3
);
}

/**
* Adds WPML Multilingual CMS (plugin) support.
*
* This class handles the compatibility with the WPML plugin.
*
* @see https://wpml.org/
*/
if ( \defined( 'ICL_SITEPRESS_VERSION' ) ) {
WPML::init();
}
}
\add_action( 'plugins_loaded', __NAMESPACE__ . '\plugin_init' );

Expand Down

0 comments on commit 51604dd

Please sign in to comment.