Skip to content

Commit

Permalink
Merge pull request #19 from ItinerisLtd/clickup/86c11c4qp/Multilingua…
Browse files Browse the repository at this point in the history
…lPress-archives-suport

MultilingualPress Integration
  • Loading branch information
danlapteacru authored Nov 18, 2024
2 parents 0427254 + 2040a35 commit 05c6a34
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/Integrations/MultilingualPress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Itineris\PageAsPostTypeArchive\Integrations;

/**
* Inpsyde MultilingualPress integration.
*/

final class MultilingualPress {
public function __construct()
{
if (
! is_plugin_active('multilingualpress/multilingualpress.php')
|| ! class_exists('\Inpsyde\MultilingualPress\Framework\Api\Translation')
)
{
return;
}

add_filter(
\Inpsyde\MultilingualPress\Framework\Api\Translation::FILTER_URL,
[$this, 'translateMultilingualPressArchivePageUrl'],
10,
4,
);
}

public static function init(): self
{
return new self();
}

/**
* MultilingualPress Archive Page URL translation.
*
* @param string $remoteUrl
* @param int $remoteSiteId
* @param int $remoteContentId
* @param \Inpsyde\MultilingualPress\Framework\Api\Translation $translation
* @return string
*/
public static function translateMultilingualPressArchivePageUrl(
string $remoteUrl,
int $remoteSiteId,
int $remoteContentId,
\Inpsyde\MultilingualPress\Framework\Api\Translation $translation
): string {
if (
! class_exists('\Inpsyde\MultilingualPress\Framework\WordpressContext')
|| ! is_archive()
|| \Inpsyde\MultilingualPress\Framework\WordpressContext::TYPE_POST_TYPE_ARCHIVE !== $translation->type()
) {
return $remoteUrl;
}

$postType = get_queried_post_type();
switch_to_blog($remoteSiteId);
$remoteArchivePageId = get_archive_page_id($postType);
if (0 !== $remoteArchivePageId) {
$remoteUrl = get_permalink($remoteArchivePageId);
}
restore_current_blog();

return $remoteUrl;
}
}
12 changes: 12 additions & 0 deletions src/Types/CustomPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Itineris\PageAsPostTypeArchive\Types;

use Itineris\PageAsPostTypeArchive\Integrations\MultilingualPress;
use WP_Post;
use WP_Post_Type;

Expand All @@ -18,6 +19,13 @@ class CustomPostType extends AbstractType {
*/
public ?array $archivePages = null;

/**
* @var array|\class-string[] $integrations
*/
public array $integrations = [
MultilingualPress::class,
];

public function __construct()
{
add_filter('display_post_states', [$this, 'addPageStates'], 10, 2);
Expand All @@ -27,6 +35,10 @@ public function __construct()
add_action('admin_init', [$this, 'addCustomPostTypePageSelectorOptions']);
add_action('deleted_post', [$this, 'deletedPost']);
add_action('transition_post_status', [$this, 'transitionPostStatus'], 10, 3);

foreach ($this->integrations as $integration) {
$integration::init();
}
}

/**
Expand Down

0 comments on commit 05c6a34

Please sign in to comment.