| Server IP : 198.71.58.22 / Your IP : 216.73.216.72 Web Server : Apache/2.4.62 (AlmaLinux) OpenSSL/3.2.2 System : Linux localhost 5.14.0-570.33.2.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Aug 14 07:37:35 EDT 2025 x86_64 User : root ( 0) PHP Version : 8.3.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /opt/automate/ |
Upload File : |
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Psr7\UriResolver;
use Symfony\Component\DomCrawler\Crawler;
use fivefilters\Readability\Readability;
use fivefilters\Readability\Configuration;
use Masterminds\HTML5;
// -------------------- CLI → WP bootstrap shims --------------------
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_HOST'] ?? 'theloopsll.virtuopsdev.com';
$_SERVER['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$_SERVER['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
$_SERVER['SERVER_PROTOCOL'] = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1';
// -------------------- CONFIG --------------------
$WP_LOAD = '/var/www/html/drjessicawacker.virtuopsdev.com/wp-load.php'; // <— set correctly
$TEMPLATE_POST_ID = 650; // template post (SiteOrigin)
$CATEGORY_SLUG = 'blogpage'; // must already exist
$PUBLISH_STATUS = 'publish'; // 'draft' or 'publish'
$url = $argv[1] ?? 'https://example.com/some-article';
// -------------------- WORDPRESS BOOTSTRAP --------------------
if (!file_exists($WP_LOAD)) {
fwrite(STDERR, "ERROR: Update \$WP_LOAD to your wp-load.php path.\n");
exit(1);
}
require $WP_LOAD;
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
// -------------------- FETCH PAGE --------------------
$client = new Client([
'timeout' => 25,
'headers' => ['User-Agent' => 'AI-Importer/1.0 (+admin@your-site.com)'],
]);
$res = $client->get($url);
$html = (string) $res->getBody();
$base = $url;
// -------------------- EXTRACT CONTENT --------------------
$config = new Configuration();
$config->setFixRelativeURLs(true);
$config->setOriginalURL($base);
$readability = new Readability($config);
$readability->parse($html);
$article = $readability->getContent() ?: '';
$title = $readability->getTitle() ?: '';
$articleText = trim(strip_tags($article));
// -------------------- META (excerpt) + fallback title --------------------
$crawler = new Crawler($html, $base);
$metaDesc = '';
if ($crawler->filter('meta[name="description"]')->count()) {
$metaDesc = trim($crawler->filter('meta[name="description"]')->attr('content'));
} elseif ($crawler->filter('meta[property="og:description"]')->count()) {
$metaDesc = trim($crawler->filter('meta[property="og:description"]')->attr('content'));
}
if (!$title && $crawler->filter('title')->count()) {
$title = trim($crawler->filter('title')->text());
}
// -------------------- WIX SCRUB (remove any element referencing wix) --------------------
if ($article) {
$html5 = new HTML5();
$doc = $html5->loadHTML('<!doctype html><meta charset="utf-8"><body>'.$article.'</body>');
$xpath = new \DOMXPath($doc);
$nodesToRemove = [];
foreach ($xpath->query('//*') as $node) {
if (!$node->hasAttributes()) continue;
foreach (iterator_to_array($node->attributes) as $attr) {
$val = strtolower($attr->value ?? '');
if (strpos($val, 'wixstatic.com') !== false || strpos($val, 'wix.com') !== false) {
$nodesToRemove[] = $node;
break;
}
}
}
foreach ($nodesToRemove as $n) {
if ($n->parentNode) $n->parentNode->removeChild($n);
}
// prune empty wrappers
$changed = true;
while ($changed) {
$changed = false;
foreach (['figure','wow-image','div'] as $tag) {
$list = $doc->getElementsByTagName($tag);
$copy = [];
foreach ($list as $n) $copy[] = $n;
foreach ($copy as $wrap) {
$hasContent = false;
foreach (iterator_to_array($wrap->childNodes) as $ch) {
if ($ch->nodeType === XML_ELEMENT_NODE) { $hasContent = true; break; }
if ($ch->nodeType === XML_TEXT_NODE && trim($ch->nodeValue) !== '') { $hasContent = true; break; }
}
if (!$hasContent && $wrap->parentNode) {
$wrap->parentNode->removeChild($wrap);
$changed = true;
}
}
}
}
$body = $doc->getElementsByTagName('body')->item(0);
$clean = '';
foreach ($body->childNodes as $child) {
$clean .= $doc->saveHTML($child);
}
$article = $clean;
}
// -------------------- IMAGE CANDIDATES (allow Wix as fallback) --------------------
$allImgUrls = [];
$leadImage = $readability->getImage() ?: '';
if (!empty($leadImage)) $allImgUrls[] = $leadImage;
$crawler->filter('img')->each(function (Crawler $node) use (&$allImgUrls, $base) {
$src = $node->attr('src');
if ($src && stripos($src, 'data:') !== 0) {
try {
$resolved = UriResolver::resolve(new Uri($base), new Uri($src));
$allImgUrls[] = (string) $resolved;
} catch (\Throwable $e) {}
}
});
$allImgUrls = array_values(array_unique($allImgUrls));
$nonWix = array_values(array_filter($allImgUrls, function($u){
$u = strtolower((string)$u);
return strpos($u, 'wixstatic.com') === false && strpos($u, 'wix.com') === false;
}));
fwrite(STDERR, "Image candidates (non-Wix): ".count($nonWix)." / total: ".count($allImgUrls).PHP_EOL);
// prefer non-Wix; fall back to Wix if needed
$firstImgUrl = $nonWix[0] ?? ($allImgUrls[0] ?? '');
// -------------------- BUILD HTML with {HERO} placeholder --------------------
$style = <<<CSS
h1,p,span,div { color:#00487A; }
p { font-family:'Mooli'; }
img.hero { width:100%; height:auto; display:block; margin:0 0 1.25rem 0; }
CSS;
$htmlOut = <<<HTML
<!doctype html>
<meta charset="utf-8">
<style>{$style}</style>
<title>{$title}</title>
{HERO}
{$article}
HTML;
// -------------------- CREATE THE POST (initially draft) --------------------
$cat = get_category_by_slug($CATEGORY_SLUG);
$cat_id = $cat ? intval($cat->term_id) : 0;
$post_id = wp_insert_post([
'post_title' => $title ?: '(No title)',
'post_content' => '', // SiteOrigin panels will carry content
'post_excerpt' => $metaDesc, // excerpt = meta description
'post_status' => 'draft',
'post_type' => 'post',
'post_category' => $cat_id ? [$cat_id] : [],
]);
if (is_wp_error($post_id) || !$post_id) {
fwrite(STDERR, "ERROR: Could not create WP post.\n");
exit(1);
}
// -------------------- SIDELOAD FIRST IMAGE (featured + hero) --------------------
$featured_attachment_id = 0;
$hero_img_url_for_html = '';
if ($firstImgUrl) {
try {
$tmp = download_url($firstImgUrl, 25);
if (is_wp_error($tmp)) {
fwrite(STDERR, "download_url error: ".$tmp->get_error_message().PHP_EOL);
} else {
$file_array = [
'name' => basename(parse_url($firstImgUrl, PHP_URL_PATH) ?: ('image-'.time().'.jpg')),
'tmp_name' => $tmp
];
$attachment_id = media_handle_sideload($file_array, $post_id);
if (is_wp_error($attachment_id)) {
fwrite(STDERR, "media_handle_sideload error: ".$attachment_id->get_error_message().PHP_EOL);
@unlink($tmp);
} else {
$featured_attachment_id = $attachment_id;
set_post_thumbnail($post_id, $attachment_id);
$src = wp_get_attachment_image_url($attachment_id, 'full');
if ($src) $hero_img_url_for_html = $src;
}
}
} catch (\Throwable $e) {
fwrite(STDERR, "Exception during sideload: ".$e->getMessage().PHP_EOL);
}
}
// Fallbacks: use the featured URL if set, else original URL
if (!$hero_img_url_for_html && has_post_thumbnail($post_id)) {
$maybe = get_the_post_thumbnail_url($post_id, 'full');
if ($maybe) $hero_img_url_for_html = $maybe;
}
if (!$hero_img_url_for_html && $firstImgUrl) {
$hero_img_url_for_html = $firstImgUrl;
}
$heroBlock = $hero_img_url_for_html ? '<img class="hero" src="'.esc_url($hero_img_url_for_html).'" alt="">' : '';
$htmlOut = str_replace('{HERO}', $heroBlock, $htmlOut);
fwrite(STDERR, "Hero chosen: ".($hero_img_url_for_html ?: '(none)').PHP_EOL);
// -------------------- APPLY TEMPLATE (SiteOrigin panels_data) --------------------
$panels = get_post_meta($TEMPLATE_POST_ID, 'panels_data', true);
if (!is_array($panels)) {
$maybe = @maybe_unserialize($panels);
$panels = is_array($maybe) ? $maybe : null;
}
if ($panels && is_array($panels)) {
$widgetReplaced = false;
if (!empty($panels['widgets']) && is_array($panels['widgets'])) {
foreach ($panels['widgets'] as $idx => $w) {
if (!empty($w['panels_info']['class']) && $w['panels_info']['class'] === 'WP_Widget_Custom_HTML') {
$panels['widgets'][$idx]['content'] = $htmlOut;
$widgetReplaced = true;
break;
}
}
if (!$widgetReplaced) {
array_unshift($panels['widgets'], [
'content' => $htmlOut,
'panels_info' => [
'class' => 'WP_Widget_Custom_HTML',
'raw' => false,
'grid' => 0,
'cell' => 0,
'id' => uniqid('custom_html_', true),
'style' => [],
],
]);
}
} else {
$panels = [
'grids' => [['cells' => 1, 'style' => []]],
'grid_cells' => [['grid' => 0, 'weight' => 1, 'style' => []]],
'widgets' => [[
'content' => $htmlOut,
'panels_info' => [
'class' => 'WP_Widget_Custom_HTML',
'raw' => false,
'grid' => 0,
'cell' => 0,
'id' => uniqid('custom_html_', true),
'style' => [],
],
]],
];
}
update_post_meta($post_id, 'panels_data', $panels);
update_post_meta($post_id, 'panels_used', true);
} else {
wp_update_post([
'ID' => $post_id,
'post_content' => $htmlOut,
]);
}
// -------------------- Finalize: status & category --------------------
if ($cat_id) {
wp_set_post_terms($post_id, [$cat_id], 'category', false);
}
if ($PUBLISH_STATUS !== 'draft') {
wp_update_post([
'ID' => $post_id,
'post_status' => $PUBLISH_STATUS,
]);
}
// -------------------- Done --------------------
$view = get_permalink($post_id);
echo "Created post #{$post_id}\n";
if ($featured_attachment_id) {
echo "Set featured image attachment #{$featured_attachment_id}\n";
}
echo "Permalink: {$view}\n";