Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sessions #96

Merged
merged 11 commits into from
Mar 23, 2018
2 changes: 0 additions & 2 deletions anthologize.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
modifications of TDPDF must comply with its license.
*/

@session_start();

if ( ! defined( 'ANTHOLOGIZE_VERSION' ) )
define( 'ANTHOLOGIZE_VERSION', '0.7.7' );

Expand Down
13 changes: 7 additions & 6 deletions includes/class-admin-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,17 @@ function load_template() {
require_once( anthologize()->includes_dir . 'class-export-panel.php' );
Anthologize_Export_Panel::save_session();

$type = $_SESSION['filetype'];
$session = anthologize_get_session();
$format = $session['filetype'];

if ( !is_array( $anthologize_formats[$type] ) )
if ( ! is_array( $anthologize_formats[ $format ] ) ) {
return;
}

$project_id = $_SESSION['project_id'];

load_template( $anthologize_formats[$type]['loader-path'] );
$project_id = $session['project_id'];

return false;
load_template( $anthologize_formats[ $format ]['loader-path'] );
die;
}

/**
Expand Down
42 changes: 16 additions & 26 deletions includes/class-export-panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function __construct() {

$export_step = ( isset( $_POST['export-step'] ) ) ? $_POST['export-step'] : '1';

if ( '1' === $export_step ) {
anthologize_delete_session();
}

if ( $export_step != '3' )
$this->display();
}
Expand Down Expand Up @@ -197,7 +201,7 @@ function display() {

<?php elseif ( $_POST['export-step'] == 2 ) : ?>

<form action="admin.php?page=anthologize_export_panel&project_id=<?php echo $project_id ?>&noheader=true" method="post">
<form action="admin.php?page=anthologize_export_project&project_id=<?php echo $project_id ?>&noheader=true" method="post">

<h3><?php $this->export_format_options_title() ?></h3>
<div id="publishing-options">
Expand Down Expand Up @@ -243,42 +247,27 @@ function display() {
function export_format_options_title() {
global $anthologize_formats;

$format = $_SESSION['filetype'];
$session = anthologize_get_session();
$format = $session['filetype'];

$title = sprintf( __( '%s Publishing Options', 'anthologize' ), $anthologize_formats[$format]['label'] );

echo $title;
}

public static function save_session() {
$keys = anthologize_get_session_data_keys();
$data = array();

if ( $_POST['export-step'] == '2' )
$_SESSION['outputParams'] = array( 'format' => $_POST['filetype'] );

// outputParams need to be reset at step 3 so that
// on a refresh null values will overwrite
if ( $_POST['export-step'] == '3' ) {
// filetype has been set different ways in different versions
// This is to be safe
$filetype = isset( $_SESSION['outputParams']['filetype'] ) ? $_SESSION['outputParams']['filetype'] : $_SESSION['filetype'];
$_SESSION['outputParams'] = array( 'format' => $filetype );
}


foreach ( $_POST as $key => $value ) {
if ( $key == 'submit' || $key == 'export-step' )
foreach ( $keys as $key ) {
if ( ! isset( $_POST[ $key ] ) ) {
continue;
}

if ( $key == '' )
echo "OK";

if ( $_POST['export-step'] == '3' )
$_SESSION['outputParams'][$key] = stripslashes( $value );
else
$_SESSION[$key] = stripslashes( $value );

$data[ $key ] = wp_unslash( $_POST[ $key ] );
}

anthologize_save_session( $data );
}

function export_format_list() {
Expand All @@ -301,7 +290,8 @@ function export_format_list() {
function render_format_options() {
global $anthologize_formats;

$format = $_SESSION['filetype'];
$session = anthologize_get_session();
$format = $session['filetype'];

if ( $fdata = $anthologize_formats[$format] ) {
$return = '';
Expand Down
7 changes: 0 additions & 7 deletions includes/class-import-feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ function display() {

<?php else : ?>

<?php

$the_items = serialize( $items );
$_SESSION['items'] = $the_items;

?>

<div id="export-form">

<p><?php _e( 'Select the items you\'d like to import to your Imported Items library and click Import.', 'anthologize' ) ?></p>
Expand Down
30 changes: 17 additions & 13 deletions includes/class-tei-dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function __construct($sessionArray, $ops = array()) {
$this->$op = $value;
}
$this->projectData = $sessionArray;
$this->projectData['outputParams'] = anthologize_get_session_output_params();

if(isset($this->outputParams['gravatar-default'])) {
$this->avatarDefault = $this->outputParams['gravatar-default'];
Expand Down Expand Up @@ -376,7 +377,6 @@ public function newStructuredCommenter($commenterObj) {
}

public function newStructuredPerson($wpUserObj) {

$id = $wpUserObj->user_login;

if( array_key_exists($id, $this->knownPersons)) {
Expand Down Expand Up @@ -671,24 +671,28 @@ public function newHead($postObject) {

if($this->includeOriginalPostData) {
//TODO: include date created and modified data
$origPostData = get_post($postObject->original_post_id);
$permalinkURL = get_permalink($postObject->original_post_id);

$permalink = $this->dom->createElementNS(TEI, 'ident');
$permalink->setAttribute('type', 'permalink');
$permalink->appendChild($this->dom->createCDataSection($permalinkURL));
$newHead->appendChild($permalink);

$origGuid = $this->dom->createElementNS(TEI, 'ident');
$origGuid->appendChild($this->dom->createCDataSection($origPostData->guid));
$origGuid->setAttribute('type', 'origGuid');
$newHead->appendChild($origGuid);

$origCreator = get_userdata($origPostData->post_author);
$bibl->appendChild($this->newAuthor($origCreator, 'originalAuthor') );
if($this->includeStructuredCreatorData) {
$this->addStructuredPerson($origCreator);
}
$origPostData = get_post($postObject->original_post_id);
if ( $origPostData ) {
$origGuid = $this->dom->createElementNS(TEI, 'ident');
$origGuid->appendChild($this->dom->createCDataSection($origPostData->guid));
$origGuid->setAttribute('type', 'origGuid');
$newHead->appendChild($origGuid);

$origCreator = get_userdata($origPostData->post_author);
if ( $origCreator ) {
$bibl->appendChild($this->newAuthor($origCreator, 'originalAuthor') );
if($this->includeStructuredCreatorData) {
$this->addStructuredPerson($origCreator);
}
}
}
}

if($this->includeItemSubjects) {
Expand Down Expand Up @@ -1060,7 +1064,7 @@ public function getParentItem($node) {

public function getNodeLabelForIndex($node) {

if ($node->firstChild->nodeName == 'span') {
if ( isset( $node->firstChild->nodeName ) && $node->firstChild->nodeName == 'span' ) {
return $node->firstChild->cloneNode(true);
}

Expand Down
120 changes: 120 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ function anthologize_save_project_meta() {
return;

$project_meta = get_post_meta( $project_id, 'anthologize_meta', true );
if ( ! is_array( $project_meta ) ) {
$project_meta = array();
}

foreach( $_POST as $key => $value ) {

Expand Down Expand Up @@ -66,3 +69,120 @@ function anthologize_filter_post_content($content) {
}

//add_filter('the_content', 'anthologize_filter_post_content');

/**
* Get data about an export "session".
*
* @since 0.7.8
*/
function anthologize_get_session() {
$session = get_user_meta( get_current_user_id(), 'anthologize_export_session', true );
if ( ! $session ) {
$session = array();
}

return $session;
}

/**
* Delete current user's active export session.
*
* @since 0.7.8
*/
function anthologize_delete_session() {
delete_user_meta( get_current_user_id(), 'anthologize_export_session' );
}

/**
* Save data to the current export "session".
*
* @since 0.7.8
*
* @param array $data
*/
function anthologize_save_session( $data ) {
$keys = anthologize_get_session_data_keys();

$session = anthologize_get_session();

foreach ( $keys as $key ) {
if ( isset( $data[ $key ] ) ) {
$session[ $key ] = $data[ $key ];
}
}

update_user_meta( get_current_user_id(), 'anthologize_export_session', $session );
}

/**
* Get a list of keys that are whitelisted for sessions.
*
* @return array
*/
function anthologize_get_session_data_keys() {
$keys = array(
// Step 1
'project_id',
'cyear',
'cname',
'ctype',
'cctype',
'edition',
'authors',

// Step 2
'post-title',
'dedication',
'acknowledgements',
'filetype',

// Step 3
'page-size',
'font-size',
'font-face',
'break-parts',
'break-items',
'colophon',
'do-shortcodes',

'creatorOutputSettings',
'outputParams',
);

/**
* Filters the keys that can be saved as part of an export session.
*
* @since 0.7.8
*/
return apply_filters( 'anthologize_get_session_data_keys', $keys );
}

/**
* Get session "outputParams" needed by export formats.
*
* @return array
*/
function anthologize_get_session_output_params() {
$session = anthologize_get_session();

$keys = array(
'page-size',
'font-size',
'font-face',
'break-parts',
'break-items',
'colophon',
'do-shortcodes',
'creatorOutputSettings',
'download',
'gravatar-default',
);

$params = array();
foreach ( $keys as $key ) {
$value = isset( $session[ $key ] ) ? $session[ $key ] : '';
$params[ $key ] = $value;
}

return $params;
}
4 changes: 4 additions & 0 deletions includes/theming-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ function anth_person_details($role = 'author') {

}

if ( ! $person ) {
return;
}

$person_details = $api->getDetailsByRef($person['atts']['ref']);
}

Expand Down
10 changes: 8 additions & 2 deletions templates/epub/includes/class-epub-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ public function __construct($tei, $data) {

$this->tei = $tei;
$this->createDirs();
$this->outFileName = $tei->getFileName($_SESSION) . '.epub';
$this->outFileName = $tei->getFileName( anthologize_get_session() ) . '.epub';
$this->proc = new XSLTProcessor();
$anthEpubDir = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'anthologize' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'epub' . DIRECTORY_SEPARATOR;
$this->ncxXSL = $anthEpubDir . 'tei2ncx.xsl';
$this->opfXSL = $anthEpubDir . 'tei2opf.xsl';

//dig up the selected cover image
$this->coverImage = $this->tei->xpath->query("//anth:param[@name = 'cover']")->item(0)->nodeValue;
$cover_item = $this->tei->xpath->query("//anth:param[@name = 'cover']")->item(0);
if ( $cover_item && isset( $cover_item->nodeValue ) ) {
$this->coverImage = $cover_item->nodeValue;
} else {
$this->coverImage = 'none';
}

$this->localizeLinks();

if (is_string($data)) {
Expand Down
4 changes: 2 additions & 2 deletions templates/epub/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'indexImages' => false,
);

$ops['outputParams'] = $_SESSION['outputParams'];
$ops['outputParams'] = anthologize_get_session_output_params();

if ( ! class_exists( 'XSLTProcessor', false ) ) {
die( 'ePub export requires XSL support' );
Expand All @@ -41,7 +41,7 @@
// Load intermediate TEI file
// generated by anthologize/includes/class-tei-dom.php

$tei_data = new TeiDom( $_SESSION, $ops );
$tei_data = new TeiDom( anthologize_get_session(), $ops );
$epub = new EpubBuilder( $tei_data, $anthEpubDir . 'tei2html.xsl' );
//echo $epub->tei->getTeiString();
//echo $epub->html->saveHTML();
Expand Down
6 changes: 3 additions & 3 deletions templates/html/output.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
);


$ops['outputParams'] = $_SESSION['outputParams'];
$ops['outputParams'] = anthologize_get_session_output_params();
$session = anthologize_get_session();


$tei = new TeiDom($_SESSION, $ops);
$tei = new TeiDom($session, $ops);

header("Content-type: text/html");

Expand Down
Loading