Skip to content

Instantly share code, notes, and snippets.

@zorgsoft
Last active May 27, 2022 08:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save zorgsoft/365d3a8d543d817b4bc7 to your computer and use it in GitHub Desktop.
Save zorgsoft/365d3a8d543d817b4bc7 to your computer and use it in GitHub Desktop.
MODx Evo module for import posts from Wordpress
<?php
/*
* WP Import, Ver 2015.01.04
* (C) 2015 by ZoRg Soft
* MODx Notes - WP Import модуь для импорта записей из Wordpress
* http://agarkov.org/modx-x/evo-wpimport
*/
$theme = $modx->config['manager_theme'];
$basePath = $modx->config['base_path'];
$action = isset($_POST['action']) ? $_POST['action'] : '';
global $modx;
/* Content templates */
/* ====================================================================================================================== */
$contentTitle = 'WP Import, <small>Ver 2015.01.04</small>';
$contentText = '';
$contentHeader = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="media/style/' . $theme . '/style.css" />
<script type="text/javascript" src="media/script/tabpane.js"></script>
<script type="text/javascript" src="media/script/datefunctions.js"></script>
<script type="text/javascript" src="media/script/mootools/mootools.js"></script>
<script type="text/javascript" src="media/calendar/datepicker.js"></script>
<script type="text/javascript" src="media/script/mootools/moodx.js"></script>
</head>
<body>';
$contentFooter = '</div>
</body>
</html>';
/* ====================================================================================================================== */
/* Start actions */
/* ====================================================================================================================== */
switch ($action) {
case 'import':
$contentText = actionFuncImport($contentText, $basePath, $modx);
break;
default:
$contentText = actionFuncStart($contentText);
break;
}
/* ====================================================================================================================== */
/* Action functions */
/* ====================================================================================================================== */
function actionFuncStart($contentText) {
$contentText.='
<form method="post">
<h2>Основные настройки:</h2>
<strong>
Заполняйте внимательно перед импортом!<br/>
И желательно прочтите сначала краткую <a href="http://agarkov.org/modx-x/evo-wpimport">документацию</a> к этому модулю.
</strong><br/><br/>
<table width="100%">
<tr>
<td><label for="mainCatID">ID каталога для записей:</label></td>
<td><input name="mainCatID" id="mainCatID" /></td>
</tr>
<tr>
<td><label for="mainNoCatID">ID каталога для записей у которых нет раздела:</label></td>
<td><input name="mainNoCatID" id="mainNoCatID" /></td>
</tr>
<tr>
<td><label for="tplCatID">ID шаблона для раздела:</label></td>
<td><input name="tplCatID" id="tplCatID" /></td>
</tr>
<tr>
<td><label for="tplPostID">ID шаблона записи блога:</label></td>
<td><input name="tplPostID" id="tplPostID" /></td>
</tr>
<tr>
<td><label for="tvTagsID">ID TV для тегов:</label></td>
<td><input name="tvTagsID" id="tvTagsID" /></td>
</tr>
<tr>
<td><label for="XMLData">Путьи имя XML файла относительно каталога сайта:</label></td>
<td><input name="XMLData" id="XMLData" placeholder="Пример: /import.xml" /></td>
</tr>
</table>
<input type="hidden" name="action" id="action" value="import" />
<input type="submit" value="Начать импорт" />
</form>';
return $contentText;
}
function actionFuncImport($contentText, $basePath, $modx) {
/* Check params */
$paramsArray = array(
'mainCatID' => $_POST['mainCatID'],
'mainNoCatID' => $_POST['mainNoCatID'],
'tplCatID' => $_POST['tplCatID'],
'tplPostID' => $_POST['tplPostID'],
'tvTagsID' => $_POST['tvTagsID'],
'XMLData' => $_POST['XMLData']
);
$paramsOk = true;
foreach ($paramsArray as $k => $val) {
if (empty($val))
$paramsOk = false;
}
if ($XMLData = simplexml_load_file($basePath . $paramsArray['XMLData']) AND $paramsOk) {
$totalPosts = 0;
$totalCats = 0;
foreach ($XMLData->channel->item as $postItem) {
$tagsList = "";
$newCatData = array();
foreach ($postItem->category as $tagItem) {
if ($tagItem['domain'] == 'post_tag')
$tagsList[] = $tagItem;
if ($tagItem['domain'] == 'category' AND empty($catUrl)) {
$newCatData['pagetitle'] = $tagItem;
$newCatData['alias'] = $tagItem['nicename'];
}
}
$tagsList = implode(", ", $tagsList);
$newPostData = array(
'pagetitle' => $postItem->title,
'alias' => $postItem->children('wp', true)->post_name,
'introtext' => $postItem->description,
'content' => $postItem->children('content', true)->encoded,
'tags' => $tagsList,
'pubDate' => strtotime($postItem->children('wp', true)->post_date_gmt)
);
/* Start import */
// Find Cat
$catID = $paramsArray['mainNoCatID'];
if (isset($newCatData['alias']) AND ! empty($newCatData['alias'])) {
$res = $modx->db->select("*", $modx->getFullTableName('site_content'), "alias='" . $newCatData['alias'] . "' AND parent=" . intval($paramsArray['mainCatID']));
$catData = $modx->db->getRow($res);
if ($catData) {
$catID = $catData['id'];
} else {
$newCatSQLData = array(
'pagetitle' => $modx->db->escape($newCatData['pagetitle']),
'longtitle' => $modx->db->escape($newCatData['pagetitle']),
'description' => '',
'alias' => $modx->db->escape($newCatData['alias']),
'published' => 1,
'parent' => intval($paramsArray['mainCatID']),
'introtext' => '',
'content' => '',
'template' => intval($paramsArray['tplCatID']),
'isfolder' => 1,
'searchable' => 1,
'cacheable' => 0,
'createdby' => 1,
'createdon' => time(),
'editedby' => 1,
'editedon' => time()
);
$catID = $modx->db->insert($newCatSQLData, $modx->getFullTableName('site_content'));
$totalCats++;
}
}
//echo "!!!!!";die();
// Create post
$newPostSQLData = array(
'pagetitle' => $modx->db->escape($newPostData['pagetitle']),
'longtitle' => $modx->db->escape($newPostData['pagetitle']),
'description' => '',
'alias' => $modx->db->escape($newPostData['alias']),
'published' => 1,
'parent' => intval($catID),
'introtext' => $modx->db->escape($newPostData['introtext']),
'content' => $modx->db->escape($newPostData['content']),
'template' => intval($paramsArray['tplPostID']),
'isfolder' => 0,
'searchable' => 1,
'cacheable' => 1,
'createdby' => 1,
'createdon' => $newPostData['pubDate'],
'editedby' => 1,
'editedon' => $newPostData['pubDate']
);
$newPostID = $modx->db->insert($newPostSQLData, $modx->getFullTableName('site_content'));
// Create Tags TV
if ($newPostID) {
$newTVData = array(
'tmplvarid' => intval($paramsArray['tvTagsID']),
'contentid' => $newPostID,
'value' => $modx->db->escape($newPostData['tags'])
);
$modx->db->insert($newTVData, $modx->getFullTableName('site_tmplvar_contentvalues'));
}
$totalPosts++;
/* End import */
}
$contentText.='<h1>Итого:</h1><br><h2>Новых разделов: ' . $totalCats . '<br>Новых записей: ' . $totalPosts . '</h2>';
include_once "manager/processors/cache_sync.class.processor.php";
$sync = new synccache();
$sync->setCachepath("assets/cache/");
$sync->setReport(false);
$sync->emptyCache();
} else {
$contentText.='<strong>Ошибка при импорте. Возможно данные не являются XML или не заданны все параметры.</strong>';
}
return $contentText;
}
/* ====================================================================================================================== */
/* Content render */
/* ====================================================================================================================== */
echo $contentHeader;
echo '<h1>' . $contentTitle . '</h1>';
echo '<div class="sectionBody">
<div class="tab-pane" id="WPImportPane">
<script type="text/javascript">
tpResources = new WebFXTabPane(document.getElementById(\'WPImportPane\'));
</script>
<div class="tab-page" id="tabImport">
<h2 class="tab">Импорт:</h2>
<script type="text/javascript">tpResources.addTabPage(document.getElementById(\'tabImport\'));</script>
' . $contentText . '
</div>
</div>
</div>';
echo $contentFooter;
/* ====================================================================================================================== */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment