Known2Ghost/export.php

159 lines
3.9 KiB
PHP
Raw Normal View History

2017-02-27 14:07:17 +01:00
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
2017-02-27 14:07:17 +01:00
require_once(dirname(__FILE__) . '/Idno/start.php');
include 'HTML-To-Markdown.php';
$garray = [];
$garray['data'] = array();
$garray['data']['posts'] = array();
$garray['data']['tags'] = array();
$garray['data']['posts_tags'] = array();
$garray['data']['users'] = array();
function metas($garray) {
$garray['meta'] = array(
2017-02-27 14:07:17 +01:00
'exported_on' => date( 'r' ),
'version' => '000',
);
}
function tags() {
$all_tags = get_tags();
if ( ! empty( $all_tags ) ) {
foreach ( $all_tags as $tag ) {
$garray['data']['tags'][] = array(
2017-02-27 14:07:17 +01:00
'id' => intval( $tag->term_id ),
'name' => $tag->name,
'slug' => $tag->slug,
'description' => $tag->description,
);
}
}
// cleanup
unset( $all_tags );
unset( $tag );
}
// TODO modifier ça pour Known !
// https://github.com/idno/Known/blob/master/Idno/Common/Entity.php
function posts($garray) {
2017-02-27 14:07:17 +01:00
$posts = Idno\Common\Entity::getFromAll([], [], 99999);
$slug_number = 0;
$_post_tags = [];
foreach($posts as $post) {
if ($post instanceof Idno\Entities\Notification) {
continue;
}
2017-02-27 14:07:17 +01:00
$post_markdown = new HTML_To_Markdown( $post->attributes['body'] );
2017-02-27 14:07:17 +01:00
$_post_tags[] = [];
$garray['data']['posts'][] = array(
2017-02-27 14:07:17 +01:00
//'id' => intval( $post->ID ),
'title' => substr( ( empty( $post->attributes['title'] ) ) ? $post->attributes['body'] : $post->attributes['title'], 0, 150 ),
'slug' => $post->attributes['slug'],
'markdown' => $post_markdown->output(),
'html' => $post->attributes['body'],
'image' => null,
2017-02-27 14:07:17 +01:00
'featured' => 0,
'page' => 1,
'status' => substr( $post->attributes['publish_status'], 0, 150 ),
'language' => 'fr_FR',
2017-02-27 14:07:17 +01:00
'meta_title' => null,
'meta_description' => null,
'author_id' => 1,
'created_at' => _get_json_date( $post->post_date ),
2017-02-27 14:07:17 +01:00
'created_by' => 1,
'updated_at' => _get_json_date( $post->post_modified ),
2017-02-27 14:07:17 +01:00
'updated_by' => 1,
'published_at' => _get_json_date( $post->post_date ),
2017-02-27 14:07:17 +01:00
'published_by' => 1,
);
$slug_number += 1;
}
$garray['data']['posts_tags'] = $_post_tags;
2017-02-27 14:07:17 +01:00
// cleanup
unset( $posts_args );
unset( $posts );
unset( $slug_number );
unset( $_post_tags );
unset( $tags );
unset( $tag );
unset( $s );
var_dump($garray);
exit;
2017-02-27 14:07:17 +01:00
}
2017-02-27 14:07:17 +01:00
function _get_json_date( $date ) {
return date( 'r', strtotime( $date ) );
}
2017-02-27 14:07:17 +01:00
function populate_data($garray) {
// if ( $garray !== null ) {
// return;
// }
2017-02-27 14:07:17 +01:00
// attaches metadata
metas($garray);
2017-02-27 14:07:17 +01:00
// attaches tags
// tags();
2017-02-27 14:07:17 +01:00
// populates posts
posts($garray);
2017-02-27 14:07:17 +01:00
}
/**
* Sends necessary headers and whatnot to allow to download file
* @return bin file
*/
function download_file($garray) {
2017-02-27 14:07:17 +01:00
// Ensure the user accessing the function actually has permission to do this
if ( ! current_user_can('export') ) {
wp_die( "<p>You are not allowed to do that.</p>", 'Permission error' );
}
populate_data($garray);
2017-02-27 14:07:17 +01:00
$upload_dir = wp_upload_dir();
$filedir = $upload_dir['path'];
$filename = 'wp2ghost_export_' . time() . '.json';
if ( ! is_writable( $filedir ) ) {
wp_die( "<p>Uploads directory is not writable, can't save the Ghost json file :/</p><p>Generated by the Ghost plugin version {$this->version}.</p>", 'Directory not writable' );
}
$handle = fopen( $filedir . '/' . $filename, 'w' );
$content = $this->get_json( $garray );
2017-02-27 14:07:17 +01:00
fwrite( $handle, $content );
fclose( $handle );
header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename='.$filename );
header( 'Content-Transfer-Encoding: binary' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate' );
header( 'Pragma: public' );
header( 'Content-Length: ' . filesize( $filedir . '/' . $filename ) );
flush();
readfile( $filedir . '/' . $filename );
exit;
}
populate_data($garray);
/*
//highlight_string("<?php\n\$data =\n" . var_export(Idno\Common\Entity::getFromAll([], [], 99999), true) . ";\n?>");