action = 'anno_xml_download_action';
$this->i18n = 'anno';
}
public static function i() {
if (!isset(self::$instance)) {
self::$instance = new Anno_XML_Download;
}
return self::$instance;
}
public function setup_filterable_props() {
$this->debug = apply_filters(__CLASS__.'_debug', false);
}
/**
* Let WP know how to handle "pretty" requests for PDFs
*
* @return void
*/
public function setup_permalinks() {
// Returns an array of post types named article
$article_post_types = get_post_types(array('name' => 'article'), 'object');
// Ensure we have what we're looking for
if (!is_array($article_post_types) || empty($article_post_types)) {
return;
}
// Get the first (and hopefully only) one
$article_post_type = array_shift($article_post_types);
global $wp;
$wp->add_query_var('xml'); // Allows us to use get_query_var('xml') later on
// Tells WP that a request like /articles/my-article-name/xml/ is valid
add_rewrite_rule($article_post_type->rewrite['slug'].'/([^/]+)/xml/?$', 'index.php?articles=$matches[1]&xml=true', 'top');
// Enable preview URLs
$wp->add_query_var('preview');
add_rewrite_rule($article_post_type->rewrite['slug'].'/([^/]+)/xml/preview/?$', 'index.php?articles=$matches[1]&xml=true&preview=true', 'top');
}
public function add_actions() {
add_action('init', array($this, 'setup_filterable_props'));
// Run late to make sure all custom post types are registered
add_action('init', array($this, 'setup_permalinks'), 20);
// Run at 'wp' so we can use the cool get_query_var functions
add_action('wp', array($this, 'request_handler'));
}
/**
* Request handler for XML.
*/
public function request_handler() {
if (get_query_var('xml')) {
// Sanitize our article ID
$id = get_the_ID();
// If we don't have an Article, get out
if (empty($id)) {
wp_die(__('No article found.', $this->i18n));
}
// Default Article
$article = null;
// If we're published, grab the published article
if (!is_preview()) {
$article = get_post($id);
}
else if (is_preview() && current_user_can('edit_post', $id)) {
$article = get_post($id);
/* Drafts and sometimes pending statuses append a preview_id on the
end of the preview URL. While we're building the XML download link
we do a check for that, and set this query arg if the preview_id is
present. */
if (isset($_GET['autosave'])) {
$article = wp_get_post_autosave($id);
}
}
// Ensure we have an article
if (empty($article)) {
wp_die(__('Required article first.', $this->i18n));
}
// Get our XML ready and stored
$this->generate_xml($article);
// Send our headers
if (!$_GET['screen']) {
$this->set_headers($article);
}
// Send the file
echo $this->xml;
exit;
}
}
/**
* Sets the download headers for what will be delivered
*
* @param obj $article
* @return void
*/
private function set_headers($article) {
// Get the post title, so we can set it as the filename
$filename = sanitize_title(get_the_title($article->ID));
header('Cache-Control: private');
header('Content-Type:text/xml; charset=utf-8');
header('Content-Length:' . mb_strlen($this->xml, '8bit'));
header('Content-Disposition: attachment; filename="'.$filename.'.xml"');
}
/**
* Builds the entire XML document
*
* @param obj $article - WP Post type object
* @return void
*/
private function generate_xml($article) {
$this->xml = $this->xml_front($article)."\n".$this->xml_body($article)."\n".$this->xml_back($article);
}
/**
* Generate the Front portion of an article XML
*
* @param postObject $article Article to generate the XML for.
* @return string XML generated
*/
private function xml_front($article) {
// Journal Title
$journal_title = cfct_get_option('journal_name');
if (!empty($journal_title)) {
$journal_title_xml = ''.esc_html($journal_title).'';
}
else {
$journal_title_xml = '';
}
// Journal ID
$journal_id = cfct_get_option('journal_id');
if (!empty($journal_id)) {
$journal_id_type = cfct_get_option('journal_id_type');
if (!empty($journal_id_type)) {
$journal_id_type_xml = ' journal-id-type="'.esc_attr($journal_id_type).'"';
}
else {
$journal_id_type_xml = '';
}
$journal_id_xml = ''.esc_html($journal_id).'';
}
else {
$journal_id_xml = '';
}
// Publisher ISSN
$pub_issn = cfct_get_option('publisher_issn');
if (!empty($pub_issn)) {
$pub_issn_xml = ''.esc_html($pub_issn).'';
}
else {
$pub_issn_xml = '';
}
// Abstract
$abstract = $article->post_excerpt;
if (!empty($abstract)) {
$abstract_xml = ''._x('Abstract', 'xml abstract title', 'anno').'