Skip to content

Commit

Permalink
PLANET-7308 Automatically create a News & Stories page (#2146)
Browse files Browse the repository at this point in the history
* PLANET-7308 Automatically create a News & Stories page

- Added migration to automatically create a posts page if none is assigned
- New posts page title is News & Stories and is set as default after creation

* Add multilingual support

1. Create identical pages for each language
2. Connect translation meta to original page

---------

Co-authored-by: Nikos Roussos <nikos.roussos@greenpeace.org>
  • Loading branch information
Osong-Michael and comzeradd authored Nov 14, 2023
1 parent a4c48be commit 1200cf9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/Migrations/M025CreateDefaultPostsPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace P4\MasterTheme\Migrations;

use P4\MasterTheme\MigrationRecord;
use P4\MasterTheme\MigrationScript;

/**
* Add default for Posts page.
*/
class M025CreateDefaultPostsPage extends MigrationScript
{
/**
* Perform the actual migration.
*
* @param MigrationRecord $record Information on the execution, can be used to add logs.
* phpcs:disable SlevomatCodingStandard.Functions.UnusedParameter -- interface implementation
*/
protected static function execute(MigrationRecord $record): void
{
$existing_posts_page = get_option('page_for_posts');
if ($existing_posts_page) {
return;
}

$new_posts_page = array(
'post_type' => 'page',
'post_title' => 'News & Stories',
'post_excerpt' => 'Read the latest updates.',
'post_status' => 'publish',
);

$new_posts_page_id = wp_insert_post($new_posts_page);
update_option('page_for_posts', $new_posts_page_id);

$multilingual = is_plugin_active('sitepress-multilingual-cms/sitepress.php');
if (!$multilingual) {
exit();
}

// Get all active languages.
$languages = apply_filters('wpml_active_languages', null, 'orderby=id&order=desc');

foreach ($languages as $lang) {
global $sitepress;
$default_locale = $sitepress->get_default_language();

$locale = $lang['language_code'];
if ($default_locale === $locale) {
continue;
}

do_action('wpml_switch_language', $locale);

// Create one more page with same content.
$new_posts_page_id_tr = wp_insert_post($new_posts_page);
update_option('page_for_posts', $new_posts_page_id_tr);

// https://wpml.org/wpml-hook/wpml_element_type/
$wpml_element_type = apply_filters('wpml_element_type', 'page');

// Get the language info of the original post.
// https://wpml.org/wpml-hook/wpml_element_language_details/
$get_language_args = array('element_id' => $new_posts_page_id, 'element_type' => 'page');
$original_post_language_info = apply_filters('wpml_element_language_details', null, $get_language_args);

$set_language_args = array(
'element_id' => $new_posts_page_id_tr,
'element_type' => $wpml_element_type, // post_page
'trid' => $original_post_language_info->trid,
'language_code' => $locale,
'source_language_code' => $default_locale
);

do_action('wpml_set_element_language_details', $set_language_args);
}
}
}
2 changes: 2 additions & 0 deletions src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use P4\MasterTheme\Migrations\M022UpdatePostRevisions;
use P4\MasterTheme\Migrations\M023EnablePlanet4Blocks;
use P4\MasterTheme\Migrations\M024RemoveNewIdentitySylesOption;
use P4\MasterTheme\Migrations\M025CreateDefaultPostsPage;

/**
* Run any new migration scripts and record results in the log.
Expand Down Expand Up @@ -69,6 +70,7 @@ public static function migrate(): void
M022UpdatePostRevisions::class,
M023EnablePlanet4Blocks::class,
M024RemoveNewIdentitySylesOption::class,
M025CreateDefaultPostsPage::class,
];

// Loop migrations and run those that haven't run yet.
Expand Down

0 comments on commit 1200cf9

Please sign in to comment.