🚀 Finish the script
This commit is contained in:
		
							
								
								
									
										88
									
								
								export.php
									
									
									
									
									
								
							
							
						
						
									
										88
									
								
								export.php
									
									
									
									
									
								
							@@ -7,46 +7,27 @@ error_reporting(E_ALL);
 | 
				
			|||||||
require_once(dirname(__FILE__) . '/Idno/start.php');
 | 
					require_once(dirname(__FILE__) . '/Idno/start.php');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
include 'HTML-To-Markdown.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                         = [];
 | 
				
			||||||
	$garray['meta'] = array(
 | 
					$garray['data']                 = [];
 | 
				
			||||||
 | 
					$garray['data']['posts']        = [];
 | 
				
			||||||
 | 
					$garray['data']['tags']         = [];
 | 
				
			||||||
 | 
					$garray['data']['posts_tags']   = [];
 | 
				
			||||||
 | 
					$garray['data']['users']        = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function metas(&$garray) {
 | 
				
			||||||
 | 
					    $garray['meta'] = [
 | 
				
			||||||
        'exported_on'   => date( 'r' ),
 | 
					        'exported_on'   => date( 'r' ),
 | 
				
			||||||
        'version'       => '000',
 | 
					        'version'       => '000',
 | 
				
			||||||
	);
 | 
					    ];
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function tags() {
 | 
					function posts(&$garray) {
 | 
				
			||||||
	$all_tags = get_tags();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if ( ! empty( $all_tags ) ) {
 | 
					 | 
				
			||||||
		foreach ( $all_tags as $tag ) {
 | 
					 | 
				
			||||||
			$garray['data']['tags'][] = array(
 | 
					 | 
				
			||||||
				'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) {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $posts = Idno\Common\Entity::getFromAll([], [], 99999);
 | 
					    $posts = Idno\Common\Entity::getFromAll([], [], 99999);
 | 
				
			||||||
    $slug_number = 0;
 | 
					    $slug_number = 0;
 | 
				
			||||||
    $_post_tags = [];
 | 
					    $_post_tags = [];
 | 
				
			||||||
 | 
					    $i = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    foreach($posts as $post) {
 | 
					    foreach($posts as $post) {
 | 
				
			||||||
        if ($post instanceof Idno\Entities\Notification) {
 | 
					        if ($post instanceof Idno\Entities\Notification) {
 | 
				
			||||||
@@ -57,7 +38,7 @@ function posts($garray) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $_post_tags[] = [];
 | 
					        $_post_tags[] = [];
 | 
				
			||||||
        $garray['data']['posts'][] = array(
 | 
					        $garray['data']['posts'][] = array(
 | 
				
			||||||
			//'id'			=> intval( $post->ID ),
 | 
					            'id'      =>  $i,
 | 
				
			||||||
            'title'     => substr( ( empty( $post->attributes['title'] ) ) ? $post->attributes['body'] : $post->attributes['title'], 0, 150 ),
 | 
					            'title'     => substr( ( empty( $post->attributes['title'] ) ) ? $post->attributes['body'] : $post->attributes['title'], 0, 150 ),
 | 
				
			||||||
            'slug'      => $post->attributes['slug'],
 | 
					            'slug'      => $post->attributes['slug'],
 | 
				
			||||||
            'markdown'    => $post_markdown->output(),
 | 
					            'markdown'    => $post_markdown->output(),
 | 
				
			||||||
@@ -65,20 +46,21 @@ function posts($garray) {
 | 
				
			|||||||
            'image'     => null,
 | 
					            'image'     => null,
 | 
				
			||||||
            'featured'    => 0,
 | 
					            'featured'    => 0,
 | 
				
			||||||
            'page'      => 1,
 | 
					            'page'      => 1,
 | 
				
			||||||
			'status'		=> substr( $post->attributes['publish_status'], 0, 150 ),
 | 
					            'status'    => isset($post->attributes['publish_status']) ? substr( $post->attributes['publish_status'], 0, 150 ) : 'published',
 | 
				
			||||||
            'language'    => 'fr_FR',
 | 
					            'language'    => 'fr_FR',
 | 
				
			||||||
            'meta_title'  => null,
 | 
					            'meta_title'  => null,
 | 
				
			||||||
            'meta_description'  => null,
 | 
					            'meta_description'  => null,
 | 
				
			||||||
            'author_id'   => 1,
 | 
					            'author_id'   => 1,
 | 
				
			||||||
			'created_at'	=> _get_json_date( $post->post_date ),
 | 
					            'created_at'  => _get_json_date( $post->created ),
 | 
				
			||||||
            'created_by'  => 1,
 | 
					            'created_by'  => 1,
 | 
				
			||||||
			'updated_at'	=> _get_json_date( $post->post_modified ),
 | 
					            'updated_at'  => _get_json_date( $post->updated ),
 | 
				
			||||||
            'updated_by'  => 1,
 | 
					            'updated_by'  => 1,
 | 
				
			||||||
			'published_at'	=> _get_json_date( $post->post_date ),
 | 
					            'published_at'  => _get_json_date( $post->created ),
 | 
				
			||||||
            'published_by'  => 1,
 | 
					            'published_by'  => 1,
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $slug_number += 1;
 | 
					        $slug_number += 1;
 | 
				
			||||||
 | 
					        $i++;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $garray['data']['posts_tags'] = $_post_tags;
 | 
					    $garray['data']['posts_tags'] = $_post_tags;
 | 
				
			||||||
@@ -91,55 +73,37 @@ function posts($garray) {
 | 
				
			|||||||
    unset( $tags );
 | 
					    unset( $tags );
 | 
				
			||||||
    unset( $tag );
 | 
					    unset( $tag );
 | 
				
			||||||
    unset( $s );
 | 
					    unset( $s );
 | 
				
			||||||
 | 
					 | 
				
			||||||
	var_dump($garray);
 | 
					 | 
				
			||||||
	exit;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function _get_json_date( $date ) {
 | 
					function _get_json_date( $date ) {
 | 
				
			||||||
    return date( 'r', strtotime( $date ) );
 | 
					    return date( 'r', $date );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function populate_data($garray) {
 | 
					function populate_data(&$garray) {
 | 
				
			||||||
//	if ( $garray !== null ) {
 | 
					 | 
				
			||||||
//		return;
 | 
					 | 
				
			||||||
//	}
 | 
					 | 
				
			||||||
    // attaches metadata
 | 
					    // attaches metadata
 | 
				
			||||||
    metas($garray);
 | 
					    metas($garray);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// attaches tags
 | 
					 | 
				
			||||||
//	tags();
 | 
					 | 
				
			||||||
    // populates posts
 | 
					    // populates posts
 | 
				
			||||||
    posts($garray);
 | 
					    posts($garray);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Sends necessary headers and whatnot to allow to download file
 | 
					 * Sends necessary headers and whatnot to allow to download file
 | 
				
			||||||
 * @return bin file
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function download_file($garray) {
 | 
					function download_file($garray) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 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);
 | 
					    populate_data($garray);
 | 
				
			||||||
 | 
					    $filedir = dirname(__FILE__).'/Uploads';
 | 
				
			||||||
	$upload_dir = wp_upload_dir();
 | 
					    $filename = 'known2ghost_export_' . time() . '.json';
 | 
				
			||||||
	$filedir = $upload_dir['path'];
 | 
					 | 
				
			||||||
	$filename = 'wp2ghost_export_' . time() . '.json';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ( ! is_writable( $filedir ) ) {
 | 
					    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' );
 | 
					        echo ( "<p>Uploads directory is not writable, can't save the Ghost json file :/</p><p>Generated by the Ghost plugin version {$this->version}.</p>");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $handle = fopen( $filedir . '/' . $filename, 'w' );
 | 
					    $handle = fopen( $filedir . '/' . $filename, 'w' );
 | 
				
			||||||
	$content = $this->get_json( $garray );
 | 
					    $content = json_encode( $garray );
 | 
				
			||||||
    fwrite( $handle, $content );
 | 
					    fwrite( $handle, $content );
 | 
				
			||||||
    fclose( $handle );
 | 
					    fclose( $handle );
 | 
				
			||||||
 | 
					 | 
				
			||||||
    header( 'Content-Description: File Transfer' );
 | 
					    header( 'Content-Description: File Transfer' );
 | 
				
			||||||
    header( 'Content-Type: application/octet-stream' );
 | 
					    header( 'Content-Type: application/octet-stream' );
 | 
				
			||||||
    header( 'Content-Disposition: attachment; filename='.$filename );
 | 
					    header( 'Content-Disposition: attachment; filename='.$filename );
 | 
				
			||||||
@@ -154,6 +118,4 @@ function download_file($garray) {
 | 
				
			|||||||
    exit;
 | 
					    exit;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
populate_data($garray);
 | 
					download_file($garray);
 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
//highlight_string("<?php\n\$data =\n" . var_export(Idno\Common\Entity::getFromAll([], [], 99999), true) . ";\n?>");
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user