Skip to content

Commit d0c33c7

Browse files
authored
Merge pull request #24 from TeamEver/codex/ajouter-enregistrement-redirections-wordpress
Generate WordPress redirect log
2 parents 408a8db + d4165d7 commit d0c33c7

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

controllers/front/blog.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,15 @@ public function initContent()
152152
// Default blog text
153153
$everblog_top_text = $this->module::getConfigInMultipleLangs('EVERBLOG_TOP_TEXT');
154154
$default_blog_top_text = $everblog_top_text[(int) Context::getContext()->language->id];
155-
$default_blog_top_text =
156-
$default_blog_top_text->customer->id
155+
$default_blog_top_text = EverPsBlogPost::changeShortcodes(
156+
$default_blog_top_text,
157+
(int) Context::getContext()->customer->id
157158
);
158159
$everblog_bottom_text = $this->module::getConfigInMultipleLangs('EVERBLOG_BOTTOM_TEXT');
159160
$default_blog_bottom_text = $everblog_bottom_text[(int) Context::getContext()->language->id];
160-
$default_blog_bottom_text =
161-
$default_blog_bottom_text->customer->id
161+
$default_blog_bottom_text = EverPsBlogPost::changeShortcodes(
162+
$default_blog_bottom_text,
163+
(int) Context::getContext()->customer->id
162164
);
163165
Hook::exec('actionBeforeEverBlogInitContent', [
164166
'blog_post_number' => &$this->post_number,

everpsblog.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,6 +3076,14 @@ private function importWordPressFile($file)
30763076
$xml_str
30773077
);
30783078
$obj = new SimpleXMLElement($xml_str, LIBXML_NOCDATA);
3079+
$redirects = [
3080+
'posts' => [],
3081+
'categories' => [],
3082+
'tags' => [],
3083+
'authors' => [],
3084+
];
3085+
$link = new Link();
3086+
$default_lang = (int) Context::getContext()->language->id;
30793087
foreach ($obj->channel->item as $el) {
30803088
// Post categories and post tags
30813089
$post_categories = [];
@@ -3107,6 +3115,17 @@ private function importWordPressFile($file)
31073115
} else {
31083116
$post_categories[] = $category->id;
31093117
}
3118+
$old_path = '/category/' . (string) $wp_taxonomy['nicename'];
3119+
if (!isset($redirects['categories'][$old_path])) {
3120+
$redirects['categories'][$old_path] = $link->getModuleLink(
3121+
'everpsblog',
3122+
'category',
3123+
[
3124+
'id_ever_category' => $category->id,
3125+
'link_rewrite' => $category->link_rewrite[$default_lang],
3126+
]
3127+
);
3128+
}
31103129
} elseif ($wp_taxonomy->attributes()['domain'] == 'post_tag'
31113130
&& (bool)Configuration::get('EVERBLOG_IMPORT_TAGS') === true
31123131
) {
@@ -3131,6 +3150,17 @@ private function importWordPressFile($file)
31313150
} else {
31323151
$post_tags[] = $tag->id;
31333152
}
3153+
$old_path = '/tag/' . (string) $wp_taxonomy['nicename'];
3154+
if (!isset($redirects['tags'][$old_path])) {
3155+
$redirects['tags'][$old_path] = $link->getModuleLink(
3156+
'everpsblog',
3157+
'tag',
3158+
[
3159+
'id_ever_tag' => $tag->id,
3160+
'link_rewrite' => $tag->link_rewrite[$default_lang],
3161+
]
3162+
);
3163+
}
31343164
}
31353165
}
31363166
// Post author
@@ -3156,6 +3186,18 @@ private function importWordPressFile($file)
31563186
$author->active = (bool) Configuration::get('EVERBLOG_ENABLE_AUTHORS');
31573187
$result &= $author->save();
31583188
}
3189+
$author_slug = Tools::str2url((string) $el->creator);
3190+
$old_path = '/author/' . $author_slug;
3191+
if (!isset($redirects['authors'][$old_path])) {
3192+
$redirects['authors'][$old_path] = $link->getModuleLink(
3193+
'everpsblog',
3194+
'author',
3195+
[
3196+
'id_ever_author' => $author->id,
3197+
'link_rewrite' => $author->link_rewrite[$default_lang],
3198+
]
3199+
);
3200+
}
31593201
// Post
31603202
$parsed_url = parse_url((string) $el->link);
31613203
$host = $parsed_url['host'];
@@ -3225,6 +3267,7 @@ private function importWordPressFile($file)
32253267
$post_content = preg_replace('/<!--(.|\s)*?-->/', '', $post_content);
32263268
$post_content = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $post_content);
32273269
$post_content = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $post_content);
3270+
$post_content = $this->cleanWpShortcodes($post_content);
32283271
$post = new EverPsBlogPost();
32293272
// Multilingual fields
32303273
foreach (Language::getLanguages(false) as $lang) {
@@ -3283,8 +3326,31 @@ private function importWordPressFile($file)
32833326
$result &= $image->save();
32843327
}
32853328
}
3329+
$old_path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
3330+
if (!isset($redirects['posts'][$old_path])) {
3331+
$redirects['posts'][$old_path] = $link->getModuleLink(
3332+
'everpsblog',
3333+
'post',
3334+
[
3335+
'id_ever_post' => $post->id,
3336+
'link_rewrite' => $post_link_rewrite,
3337+
]
3338+
);
3339+
}
3340+
}
3341+
}
3342+
$redirect_lines = [];
3343+
foreach ($redirects as $datas) {
3344+
foreach ($datas as $from => $to) {
3345+
$redirect_lines[] = 'Redirect 301 ' . rtrim($from, '/') . ' ' . $to;
32863346
}
32873347
}
3348+
if (!empty($redirect_lines)) {
3349+
file_put_contents(
3350+
dirname(__FILE__) . '/wordpress_redirects.txt',
3351+
implode(PHP_EOL, $redirect_lines)
3352+
);
3353+
}
32883354
// Reset iframes
32893355
if ((bool) $allow_iframes === false) {
32903356
Configuration::updateValue('PS_ALLOW_HTML_IFRAME', false);
@@ -3664,6 +3730,11 @@ public static function getConfigInMultipleLangs($key, $idShopGroup = null, $idSh
36643730
return $resultsArray;
36653731
}
36663732

3733+
private function cleanWpShortcodes($html)
3734+
{
3735+
return preg_replace('/\[(?!everpsblog)(?:\/)?[\w\-]+(?:\s[^\]]*)?\]/i', '', $html);
3736+
}
3737+
36673738
private function parseShortcodes($html)
36683739
{
36693740
return preg_replace_callback('/\[everpsblog([^\]]*)\]/i', function ($m) {

0 commit comments

Comments
 (0)