Skip to content

Instantly share code, notes, and snippets.

@SamuelHadsall
Last active August 7, 2020 03:49
Show Gist options
  • Save SamuelHadsall/89ca0b12e3888eb8137d001424ed5e5e to your computer and use it in GitHub Desktop.
Save SamuelHadsall/89ca0b12e3888eb8137d001424ed5e5e to your computer and use it in GitHub Desktop.
Generate XML with Custom Post Type
<?php
/**
* The admin-specific functionality of the plugin.
*
* @link https://www.steadyrain.com
* @since 1.0.0
*
* @package The_Factory
* @subpackage The_Factory/admin
*/
/**
* The admin-specific functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the admin-specific stylesheet and JavaScript.
*
* @package The_Factory
* @subpackage The_Factory/admin
* @author SteadyRain <info@steadyrain.com>
*/
class The_Factory_Data_Feed {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
}
public function generateDataXML($ID, $post)
{
$upload = wp_upload_dir();
$upload_dir = $upload['basedir'];
$upload_dir = $upload_dir . '/events';
if ( ! file_exists( $upload_dir ) ) {
wp_mkdir_p( $upload_dir );
}
$xml = new \SimpleXMLElement('<rss xmlns:g="http://base.google.com/ns/1.0" />', LIBXML_NOERROR, false, 'g', true);
$xml->addAttribute('version', '2.0');
$channel = $xml->addChild('channel');
$channel->addChild('title', get_bloginfo('name'));
$channel->addChild('link', get_bloginfo('url'));
$channel->addChild('description', get_bloginfo('description'));
$eventID = $post->ID;
//die(var_dump(get_post_meta($eventID)));
$eventDate = get_field( "date_of_event", $eventID );
$postName = $post->post_name;
$posttitle = $post->post_title;
$abspath = get_site_url();
$showInfo = $_POST['acf']['field_5f2241e2c0bf4'];
$campaignShowInfo = $_POST['acf']['field_5f2458fc9d313'];
$eventDate = $showInfo['field_5f224203c0bf5'];
$campaignEventDate = $campaignShowInfo['field_5f2458fc9d315'];
$doorsOpen = $showInfo['field_5f2242b5c0bf7'];
$campaignDoorsOpen = $campaignShowInfo['field_5f2458fc9d317'];
$tourName = $showInfo['field_5f22400c381dc'];
$compaignTourName = $campaignShowInfo['field_5f2458f49d30f'];
$eventImage = intval($showInfo['field_5f245777ac9b5']);
$campaignEventImage = intval($campaignShowInfo['field_5f2458f49d30f'];
$image = wp_get_attachment_image_src($eventImage, 'full');
$campaignImage = wp_get_attachment_image_src($campaignEventImage, 'full');
$eventRow = $channel->addChild('item');
$eventRow->addAttribute("id", $postName.'-'.$eventID);
$eventRow->addChild("g:g:title", $posttitle);
if (!empty($compaignTourName)) {
$eventRow->addChild('g:g:tour', $compaignTourName);
} else {
$eventRow->addChild('g:g:tour', $tourName);
}
if (!empty($campaignEventDate)) {
$eventRow->addChild('g:g:event', date('F j, Y', strtotime($campaignEventDate)));
} else {
$eventRow->addChild('g:g:event', date('F j, Y', strtotime($eventDate)));
}
if (!empty($campaignDoorsOpen)) {
$eventRow->addChild('g:g:opens', $campaignDoorsOpen);
} else {
$eventRow->addChild('g:g:opens', $doorsOpen);
}
if ($campaignEventImage) {
$eventRow->addChild('g:g:image', $campaignImage[0]);
} else {
$eventRow->addChild('g:g:image', $image[0]);
}
$file = trailingslashit( $upload_dir ) . 'events-data.xml';
$open = fopen($file, 'w') or die ("File cannot be opened.");
$dom = new \DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
fwrite($open, $dom->saveXML());
fclose($open);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment