Plugin Directory

Changeset 3131936

Timestamp:
08/07/2024 05:58:25 AM (3 months ago)
Author:
leemon
Message:

1.0.6 release

Location:
indexhibit2-importer/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • indexhibit2-importer/trunk/indexhibit2-importer.php

    r2085196 r3131936  
    11<?php
    2 /*
    3   Plugin Name: Indexhibit 2 Importer
    4   Plugin URI: http://wordpress.org/plugins/indexhibit2-importer/
    5   Description: Import exhibits and media files from an Indexhibit 2 site.
    6   Version: 1.0.5
    7   Author: leemon
    8   Text Domain: indexhibit2-importer
    9   License: GPLv2 or later
    10 */
    11 
    12 if ( !defined( 'WP_LOAD_IMPORTERS' ) ) {
    13     return;
     2/**
     3 * Plugin Name: Indexhibit 2 Importer
     4 * Plugin URI: http://wordpress.org/plugins/indexhibit2-importer/
     5 * Description: Import exhibits and media files from an Indexhibit 2 site.
     6 * Version: 1.0.6
     7 * Author: leemon
     8 * Text Domain: indexhibit2-importer
     9 * License: GPLv2 or later
     10 *
     11 * @package indexhibit2-importer
     12 */
     13
     14if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) {
     15    return;
    1416}
    1517
    16 // Load Importer API
     18// Load Importer API
    1719require_once ABSPATH . 'wp-admin/includes/import.php';
    1820
    19 if ( !class_exists( 'WP_Importer' ) ) {
    20     $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
    21     if ( file_exists( $class_wp_importer ) ) {
    22         require_once $class_wp_importer;
    23     }
     21if ( !class_exists( 'WP_Importer' ) ) {
     22$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
     23if ( file_exists( $class_wp_importer ) ) {
     24require_once $class_wp_importer;
     25}
    2426}
    2527
     
    3133 */
    3234if ( class_exists( 'WP_Importer' ) ) {
    33    
    34 class Ix2_Import extends WP_Importer {
    35 
    36     // Database connection
    37     public $ix2db;
    38 
    39     /**
    40      * Constructor. Intentionally left empty and public.
    41      */
    42     public function __construct() {}
    43 
    44     /**
    45      * Display header
    46      *
    47      */
    48     public function header() {
    49     ?>
    50         <div class="wrap">
    51         <h2><?php _e( 'Import Indexhibit 2', 'indexhibit2-importer' ); ?></h2>
    52     <?php
    53     }
    54 
    55     /**
    56      * Display footer
    57      *
    58      */
    59     public function footer() {
    60     ?>
    61         </div>
    62     <?php
    63     }
    64 
    65     /**
    66      * Display introductory text and Indexhibit 2 settings form
    67      *
    68      */
    69     public function greet() {
    70     ?>
    71         <div class="narrow">
    72             <p><?php _e( 'This importer allows you to import most of the contents from an Indexhibit 2 site into your WordPress site. It imports exhibits and media files but ignores links, sections, subsections and exhibit formats.', 'indexhibit2-importer' ); ?></p>
    73             <p><?php _e( 'The process may take a few minutes depending on the size of your database. Please be patient.', 'indexhibit2-importer' ); ?></p>
    74             <p><?php _e( 'Fill the following form with your Indexhibit 2 configuration settings. They can be found in the <code>/ndxzsite/config/config.php</code> file.', 'indexhibit2-importer' ); ?></p>
    75             <form action="admin.php?import=indexhibit2&amp;step=1" method="post">
    76     <?php
    77         wp_nonce_field( 'import-indexhibit2' );
    78         $this->db_form();
    79     ?>
    80             <p class="submit"><input type="submit" name="submit" class="button" value="<?php echo esc_attr__( 'Import exhibits', 'indexhibit2-importer' ); ?>" /></p>
    81             </form>
    82         </div>
    83     <?php
    84     }
    85 
    86     /**
    87      * Display Indexhibit 2 settings form
    88      *
    89      */
    90     public function db_form() {
    91     ?>
    92         <table class="form-table">
    93         <tr>
    94             <th scope="row"><label for="ixurl"><?php _e( 'Indexhibit 2 Site Address', 'indexhibit2-importer' ); ?></label></th>
    95             <td>
    96             <input type="url" name="ixurl" id="ixurl" class="regular-text" required placeholder="http://" />
    97             <p class="description" id="ixurl-description"><?php _e( "The URL where your Indexhibit 2 site is installed", 'indexhibit2-importer' ); ?></p>
    98             </td>
    99         </tr>
    100         <tr>
    101             <th scope="row"><label for="dbname"><?php _e( 'Indexhibit 2 Database Name', 'indexhibit2-importer' ); ?></label></th>
    102             <td>
    103             <input type="text" name="dbname" id="dbname" class="regular-text" required />
    104             <p class="description" id="dbname-description"><?php _e( "The <code>&dollar;indx['db']</code> value", 'indexhibit2-importer' ); ?></p>
    105             </td>
    106         </tr>
    107         <tr>
    108             <th scope="row"><label for="dbuser"><?php _e( 'Indexhibit 2 Database User', 'indexhibit2-importer' ); ?></label></th>
    109             <td>
    110             <input type="text" name="dbuser" id="dbuser" class="regular-text" required />
    111             <p class="description" id="dbuser-description"><?php _e( "The <code>&dollar;indx['user']</code> value", 'indexhibit2-importer' ); ?></p>
    112             </td>
    113         </tr>
    114         <tr>
    115             <th scope="row"><label for="dbpass"><?php _e( 'Indexhibit 2 Database Password', 'indexhibit2-importer' ); ?></label></th>
    116             <td>
    117             <input type="password" name="dbpass" id="dbpass" class="regular-text" required />
    118             <p class="description" id="dbpass-description"><?php _e( "The <code>&dollar;indx['pass']</code> value", 'indexhibit2-importer' ); ?></p>
    119             </td>
    120         </tr>
    121         <tr>
    122             <th scope="row"><label for="dbhost"><?php _e( 'Indexhibit 2 Database Host', 'indexhibit2-importer' ); ?></label></th>
    123             <td>
    124             <input type="text" name="dbhost" id="dbhost" class="regular-text" required placeholder="localhost" />
    125             <p class="description" id="dbhost-description"><?php _e( "The <code>&dollar;indx['host']</code> value", 'indexhibit2-importer' ); ?></p>
    126             </td>
    127         </tr>
    128         <tr>
    129             <th scope="row"><label for="dbprefix"><?php _e( 'Indexhibit 2 Table Prefix', 'indexhibit2-importer' ); ?></label></th>
    130             <td>
    131             <input type="text" name="dbprefix" id="dbprefix" class="regular-text" required placeholder="ndxz_" />
    132             <p class="description" id="dbprefix-description"><?php _e( "The <code>xxxx</code> value in <code>define('PX', 'xxxx')</code> value", 'indexhibit2-importer' ); ?></p>
    133             </td>
    134         </tr>
    135         </table>
    136     <?php
    137     }
    138 
    139     /**
    140      * Setup connection to Indexhibit 2 database
    141      *
    142      */
    143     public function init_ix2db() {
    144         $options = get_option( 'ix2_options' );
    145         $this->ix2db = new wpdb( $options['ix2_user'], $options['ix2_pass'], $options['ix2_name'], $options['ix2_host'] );
    146         $result = $this->ix2db->check_connection( false );
    147         if ( !$result ) {
    148             return false;
    149         }
    150         return true;
    151     }
    152 
    153     /**
    154      * Get exhibits from Indexhibit 2 database
    155      *
    156      */
    157     public function get_ix2_exhibits() {
    158         $options = get_option( 'ix2_options' );
    159         $dbprefix = $options['ix2_dbprefix'];
    160         return $this->ix2db->get_results( "SELECT * FROM " . $dbprefix . "objects WHERE link = ''", ARRAY_A );
    161     }
    162 
    163     /**
    164      * Get media files from Indexhibit 2 database
    165      *
    166      */
    167     public function get_ix2_media( $post_id ) {
    168         $options = get_option( 'ix2_options' );
    169         $dbprefix = $options['ix2_dbprefix'];
    170         return $this->ix2db->get_results(
    171             $this->ix2db->prepare( "SELECT * FROM " . $dbprefix . "media WHERE media_ref_id = %s AND media_mime NOT IN ( 'youtube', 'vimeo' ) ORDER BY media_order ASC", $post_id ),
    172             ARRAY_A );
    173     }
    174 
    175     /**
    176      * Import exhibits
    177      *
    178      */
    179     public function import_exhibits() {
    180         // Import exhibits
    181         $exhibits = $this->get_ix2_exhibits();
    182         $result = $this->exhibits2wp( $exhibits );
    183         if ( is_wp_error( $result ) ) {
    184             return $result;
    185         }
    186 
    187         ?>
    188         <form action="admin.php?import=indexhibit2&amp;step=2" method="post">
    189         <?php wp_nonce_field( 'import-indexhibit2' ); ?>
    190         <table class="form-table">
    191             <tr>
    192             <th scope="row"><label for="mdinsert"><?php _e( 'Insert imported media files into pages content?', 'indexhibit2-importer' ); ?></label></th>
    193             <td>
    194             <input type="checkbox" name="mdinsert" id="mdinsert" value="1" checked />
    195             </td>
    196             </tr>
    197         </table>
    198         <p class="submit"><input type="submit" name="submit" class="button" value="<?php echo esc_attr__( 'Import media files', 'indexhibit2-importer' ); ?>" /></p>
    199         </form>
    200         <?php
    201     }
    202 
    203     /**
    204      * Import media files
    205      *
    206      */
    207     public function import_media() {
    208         $options = get_option( 'ix2_options' );
    209         $mdinsert = $options['ix2_mdinsert'];
    210         $ixexhibits2wpposts = $options['ix2_exhibits2wpposts'];
    211 
    212         // Import media
    213         foreach ( $ixexhibits2wpposts as $key => $value ) {
    214             $media = $this->get_ix2_media( $key );
    215             $result = $this->media2wp( $media, $value );
    216             if ( is_wp_error( $result ) ) {
    217                 return $result;
    218             }
    219             if ( !empty( $mdinsert ) ) {
    220                 $this->insert_attachments( $value );
    221             }
    222         }
    223 
    224         ?>
    225         <form action="admin.php?import=indexhibit2&amp;step=3" method="post">
    226         <?php wp_nonce_field( 'import-indexhibit2' ); ?>
    227         <p class="submit"><input type="submit" name="submit" class="button" value="<?php echo esc_attr__( 'Finish', 'indexhibit2-importer' ); ?>" /></p>
    228         </form>
    229         <?php
    230     }
    231 
    232     /**
    233      * Perform import of exhibits
    234      *
    235      */
    236     public function exhibits2wp( $exhibits = '' ) {
    237         $options = get_option( 'ix2_options' );
    238         $count = 0;
    239         $ixexhibits2wpposts = array();
    240 
    241         if ( is_array( $exhibits ) ) {
    242             echo '<p>' . __( 'Importing exhibits...', 'indexhibit2-importer' ) . '</p>';
    243             foreach ( $exhibits as $exhibit ) {
    244                 $post_author = get_current_user_id();
    245                 $post_title = $exhibit['title'];
    246                 $post_content = $exhibit['content'];
    247                 $post_home = $exhibit['home'];
    248                 $post_date = $exhibit['pdate'];
    249                 $post_modified = $exhibit['udate'];
    250                 $post_format = $exhibit['format'];
    251 
    252                 // Set Indexhibit-to-WordPress status translation
    253                 $ixstatus = array(
    254                     0 => 'draft',
    255                     1 => 'publish'
    256                 );
    257                 $post_status = $ixstatus[$exhibit['status']];
    258 
    259                 $ret_id = 0;
    260 
    261                 // Check if the exhibit is already imported
    262                 if ( $pinfo = post_exists( $post_title, '', $post_date ) ) {
    263                     $ret_id = wp_insert_post( array(
    264                             'ID'                => $pinfo,
    265                             'post_author'       => $post_author,
    266                             'post_date'         => $post_date,
    267                             'post_date_gmt'     => $post_date,
    268                             'post_modified'     => $post_modified,
    269                             'post_modified_gmt' => $post_modified,
    270                             'post_title'        => $post_title,
    271                             'post_content'      => $post_content,
    272                             'post_status'       => $post_status,
    273                             'post_type'         => 'page',
    274                             'comment_status'    => 'closed',
    275                             'ping_status'       => 'closed' )
    276                             );
    277                     if ( is_wp_error( $ret_id ) ) {
    278                         return $ret_id;
    279                     }
    280                     add_post_meta( $ret_id, 'ix2_exhibit_format', $post_format );
    281                 } else {
    282                     $ret_id = wp_insert_post( array(
    283                             'post_author'       => $post_author,
    284                             'post_date'         => $post_date,
    285                             'post_date_gmt'     => $post_date,
    286                             'post_modified'     => $post_modified,
    287                             'post_modified_gmt' => $post_modified,
    288                             'post_title'        => $post_title,
    289                             'post_content'      => $post_content,
    290                             'post_status'       => $post_status,
    291                             'post_type'         => 'page',
    292                             'comment_status'    => 'closed',
    293                             'ping_status'       => 'closed' )
    294                             );
    295                     if ( is_wp_error( $ret_id ) ) {
    296                         return $ret_id;
    297                     }
    298                     add_post_meta( $ret_id, 'ix2_exhibit_format', $post_format );
    299                 }
    300 
    301                 // Set front page
    302                 if ( !empty( $post_home ) ) {
    303                     update_option( 'page_on_front', $ret_id );
    304                     update_option( 'show_on_front', 'page' );
    305                 }
    306 
    307                 $ixexhibits2wpposts[$exhibit['id']] = $ret_id;
    308                 $count++;
    309             }
    310         }
    311 
    312         // Store exhibit2post translation for later use
    313         $options['ix2_exhibits2wpposts'] = $ixexhibits2wpposts;
    314         update_option( 'ix2_options', $options );
    315 
    316         echo '<p>' . sprintf( __( 'Done! <strong>%1$s</strong> exhibits imported.', 'indexhibit2-importer' ), $count ) . '<br /><br /></p>';
    317         return true;
    318     }
    319 
    320     /**
    321      * Perform import of media files
    322      *
    323      */
    324     public function media2wp( $files = '', $post_id ) {
    325         $count = 0;
    326 
    327         if ( is_array( $files ) ) {
    328             echo '<p>' . sprintf( __( 'Importing media files from exhibit "%1$s"...', 'indexhibit2-importer' ), get_the_title( $post_id ) ) . '</p>';
    329             foreach ( $files as $file ) {
    330                 $count++;
    331                 $process = $this->process_attachment( $file, $post_id );
    332             }
    333         }
    334 
    335         echo '<p>' . sprintf( __( 'Done! <strong>%1$s</strong> media files imported.', 'indexhibit2-importer' ), $count ) . '<br /><br /></p>';
    336         return true;
    337 
    338     }
    339    
    340     /**
    341      * Create new attachments based on import information
    342      *
    343      */
    344     public function process_attachment( $file, $parent ) {
    345         $options = get_option( 'ix2_options' );
    346         $ixurl = $options['ix2_url'];
    347 
    348         $media_file = $file['media_file'];
    349         $media_file_noext = preg_replace( '/\\.[^.\\s]{3,4}$/', '', $media_file );
    350 
    351         $post = array(
    352             'post_title'    => ( !empty( $file['media_title'] ) ? $file['media_title'] : $media_file_noext ),
    353             'post_content'  => $file['media_caption'],
    354             'post_date'     => $file['media_udate'],
    355             'post_date_gmt' => $file['media_udate'],
    356             'menu_order'    => $file['media_order'],
    357             'post_parent'   => $parent,
    358         );
    359 
    360         $media_url = trailingslashit( $ixurl ) . 'files/gimgs/' . $media_file;
    361 
    362         $pre_process = $this->pre_process_attachment( $post, $media_url );
    363         if ( is_wp_error( $pre_process ) ) {
    364             return array(
    365                 'fatal' => false,
    366                 'type' => 'error',
    367                 'code' => $pre_process->get_error_code(),
    368                 'message' => $pre_process->get_error_message(),
    369                 'text' => sprintf( __( '%1$s was not uploaded. (<strong>%2$s</strong>: %3$s)', 'indexhibit2-importer' ), $post['post_title'], $pre_process->get_error_code(), $pre_process->get_error_message() )
    370             );
    371         }
    372         // If the URL is absolute, but does not contain address, then upload it assuming base_site_url
    373         if ( preg_match( '|^/[\w\W]+$|', $media_url ) ) {
    374             $base_url = get_home_url();
    375             $media_url = rtrim( $base_url, '/' ) . $media_url;
    376         }
    377         $upload = $this->fetch_remote_file( $media_url, $post );
    378         if ( is_wp_error( $upload ) ) {
    379             return array(
    380                 'fatal' => ( $upload->get_error_code() == 'upload_dir_error' && $upload->get_error_message() != 'Invalid file type' ? true : false ),
    381                 'type' => 'error',
    382                 'code' => $upload->get_error_code(),
    383                 'message' => $upload->get_error_message(),
    384                 'text' => sprintf( __( '%1$s could not be uploaded because of an error. (<strong>%2$s</strong>: %3$s)', 'indexhibit2-importer' ), $post['post_title'], $upload->get_error_code(), $upload->get_error_message() )
    385             );
    386         }
    387         if ( $info = wp_check_filetype( $upload['file'] ) ) {
    388             $post['post_mime_type'] = $info['type'];
    389         } else {
    390             $upload = new WP_Error( 'attachment_processing_error', __( 'Invalid file type', 'indexhibit2-importer' ) );
    391             return array(
    392                 'fatal' => false,
    393                 'type' => 'error',
    394                 'code' => $upload->get_error_code(),
    395                 'message' => $upload->get_error_message(),
    396                 'text' => sprintf( __( '%1$s could not be uploaded because of an error. (<strong>%2$s</strong>: %3$s)', 'indexhibit2-importer' ), $post['post_title'], $upload->get_error_code(), $upload->get_error_message() )
    397             );
    398         }
    399         $post['guid'] = $upload['url'];
    400         $post['post_author'] = get_current_user_id();
    401         // as per wp-admin/includes/upload.php
    402         $post_id = wp_insert_attachment( $post, $upload['file'] );
    403         wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
    404         return array(
    405             'fatal' => false,
    406             'type' => 'updated',
    407             'text' => sprintf( __( '%s was uploaded successfully', 'indexhibit2-importer' ), $post['post_title'] )
    408         );
    409     }
    410 
    411     /**
    412      * Check if attachment already exist
    413      *
    414      */
    415     public function pre_process_attachment( $post, $url ) {
    416         global $wpdb;
    417         $imported = $wpdb->get_results(
    418             $wpdb->prepare( "SELECT ID, post_date_gmt, guid
     35
     36    /**
     37     * Core class used to implement the plugin.
     38     */
     39    class Ix2_Import extends WP_Importer {
     40
     41        /**
     42         * Database connection.
     43         *
     44         * @var object $ix2db
     45         */
     46        public $ix2db;
     47
     48        /**
     49         * Constructor. Intentionally left empty and public.
     50         */
     51        public function __construct() {}
     52
     53        /**
     54         * Display header
     55         */
     56        public function header() {
     57            ?>
     58        <div class="wrap">
     59        <h2><?php _e( 'Import Indexhibit 2', 'indexhibit2-importer' ); ?></h2>
     60            <?php
     61        }
     62
     63        /**
     64         * Display footer
     65         */
     66        public function footer() {
     67            ?>
     68        </div>
     69            <?php
     70        }
     71
     72        /**
     73         * Display introductory text and Indexhibit 2 settings form
     74         */
     75        public function greet() {
     76            ?>
     77        <div class="narrow">
     78            <p><?php _e( 'This importer allows you to import most of the contents from an Indexhibit 2 site into your WordPress site. It imports exhibits and media files but ignores links, sections, subsections and exhibit formats.', 'indexhibit2-importer' ); ?></p>
     79            <p><?php _e( 'The process may take a few minutes depending on the size of your database. Please be patient.', 'indexhibit2-importer' ); ?></p>
     80            <p><?php _e( 'Fill the following form with your Indexhibit 2 configuration settings. They can be found in the <code>/ndxzsite/config/config.php</code> file.', 'indexhibit2-importer' ); ?></p>
     81            <form action="admin.php?import=indexhibit2&amp;step=1" method="post">
     82            <?php
     83            wp_nonce_field( 'import-indexhibit2' );
     84            $this->db_form();
     85            ?>
     86            <p class="submit"><input type="submit" name="submit" class="button" value="<?php echo esc_attr__( 'Import exhibits', 'indexhibit2-importer' ); ?>" /></p>
     87            </form>
     88        </div>
     89            <?php
     90        }
     91
     92        /**
     93         * Display Indexhibit 2 settings form
     94         */
     95        public function db_form() {
     96            ?>
     97        <table class="form-table">
     98        <tr>
     99            <th scope="row"><label for="ixurl"><?php _e( 'Indexhibit 2 Site Address', 'indexhibit2-importer' ); ?></label></th>
     100            <td>
     101            <input type="url" name="ixurl" id="ixurl" class="regular-text" required placeholder="http://" />
     102            <p class="description" id="ixurl-description"><?php _e( 'The URL where your Indexhibit 2 site is installed', 'indexhibit2-importer' ); ?></p>
     103            </td>
     104        </tr>
     105        <tr>
     106            <th scope="row"><label for="dbname"><?php _e( 'Indexhibit 2 Database Name', 'indexhibit2-importer' ); ?></label></th>
     107            <td>
     108            <input type="text" name="dbname" id="dbname" class="regular-text" required />
     109            <p class="description" id="dbname-description"><?php _e( "The <code>&dollar;indx['db']</code> value", 'indexhibit2-importer' ); ?></p>
     110            </td>
     111        </tr>
     112        <tr>
     113            <th scope="row"><label for="dbuser"><?php _e( 'Indexhibit 2 Database User', 'indexhibit2-importer' ); ?></label></th>
     114            <td>
     115            <input type="text" name="dbuser" id="dbuser" class="regular-text" required />
     116            <p class="description" id="dbuser-description"><?php _e( "The <code>&dollar;indx['user']</code> value", 'indexhibit2-importer' ); ?></p>
     117            </td>
     118        </tr>
     119        <tr>
     120            <th scope="row"><label for="dbpass"><?php _e( 'Indexhibit 2 Database Password', 'indexhibit2-importer' ); ?></label></th>
     121            <td>
     122            <input type="password" name="dbpass" id="dbpass" class="regular-text" required />
     123            <p class="description" id="dbpass-description"><?php _e( "The <code>&dollar;indx['pass']</code> value", 'indexhibit2-importer' ); ?></p>
     124            </td>
     125        </tr>
     126        <tr>
     127            <th scope="row"><label for="dbhost"><?php _e( 'Indexhibit 2 Database Host', 'indexhibit2-importer' ); ?></label></th>
     128            <td>
     129            <input type="text" name="dbhost" id="dbhost" class="regular-text" required placeholder="localhost" />
     130            <p class="description" id="dbhost-description"><?php _e( "The <code>&dollar;indx['host']</code> value", 'indexhibit2-importer' ); ?></p>
     131            </td>
     132        </tr>
     133        <tr>
     134            <th scope="row"><label for="dbprefix"><?php _e( 'Indexhibit 2 Table Prefix', 'indexhibit2-importer' ); ?></label></th>
     135            <td>
     136            <input type="text" name="dbprefix" id="dbprefix" class="regular-text" required placeholder="ndxz_" />
     137            <p class="description" id="dbprefix-description"><?php _e( "The <code>xxxx</code> value in <code>define('PX', 'xxxx')</code> value", 'indexhibit2-importer' ); ?></p>
     138            </td>
     139        </tr>
     140        </table>
     141            <?php
     142        }
     143
     144        /**
     145         * Setup connection to Indexhibit 2 database
     146         */
     147        public function init_ix2db() {
     148            $options     = get_option( 'ix2_options' );
     149            $this->ix2db = new wpdb( $options['ix2_user'], $options['ix2_pass'], $options['ix2_name'], $options['ix2_host'] );
     150            $result      = $this->ix2db->check_connection( false );
     151            if ( ! $result ) {
     152                return false;
     153            }
     154            return true;
     155        }
     156
     157        /**
     158         * Get exhibits from Indexhibit 2 database
     159         */
     160        public function get_ix2_exhibits() {
     161            $options  = get_option( 'ix2_options' );
     162            $dbprefix = $options['ix2_dbprefix'];
     163            return $this->ix2db->get_results( 'SELECT * FROM ' . $dbprefix . "objects WHERE link = ''", ARRAY_A );
     164        }
     165
     166        /**
     167         * Get media files from Indexhibit 2 database
     168         */
     169        public function get_ix2_media( $post_id ) {
     170            $options  = get_option( 'ix2_options' );
     171            $dbprefix = $options['ix2_dbprefix'];
     172            return $this->ix2db->get_results(
     173                $this->ix2db->prepare( 'SELECT * FROM ' . $dbprefix . "media WHERE media_ref_id = %s AND media_mime NOT IN ( 'youtube', 'vimeo' ) ORDER BY media_order ASC", $post_id ),
     174                ARRAY_A
     175            );
     176        }
     177
     178        /**
     179         * Import exhibits
     180         */
     181        public function import_exhibits() {
     182            // Import exhibits.
     183            $exhibits = $this->get_ix2_exhibits();
     184            $result   = $this->exhibits2wp( $exhibits );
     185            if ( is_wp_error( $result ) ) {
     186                return $result;
     187            }
     188
     189            ?>
     190        <form action="admin.php?import=indexhibit2&amp;step=2" method="post">
     191            <?php wp_nonce_field( 'import-indexhibit2' ); ?>
     192        <table class="form-table">
     193            <tr>
     194            <th scope="row"><label for="mdinsert"><?php _e( 'Insert imported media files into pages content?', 'indexhibit2-importer' ); ?></label></th>
     195            <td>
     196            <input type="checkbox" name="mdinsert" id="mdinsert" value="1" checked />
     197            </td>
     198            </tr>
     199        </table>
     200        <p class="submit"><input type="submit" name="submit" class="button" value="<?php echo esc_attr__( 'Import media files', 'indexhibit2-importer' ); ?>" /></p>
     201        </form>
     202            <?php
     203        }
     204
     205        /**
     206         * Import media files
     207         */
     208        public function import_media() {
     209            $options            = get_option( 'ix2_options' );
     210            $mdinsert           = $options['ix2_mdinsert'];
     211            $ixexhibits2wpposts = $options['ix2_exhibits2wpposts'];
     212
     213            // Import media.
     214            foreach ( $ixexhibits2wpposts as $key => $value ) {
     215                $media  = $this->get_ix2_media( $key );
     216                $result = $this->media2wp( $media, $value );
     217                if ( is_wp_error( $result ) ) {
     218                    return $result;
     219                }
     220                if ( ! empty( $mdinsert ) ) {
     221                    $this->insert_attachments( $value );
     222                }
     223            }
     224
     225            ?>
     226        <form action="admin.php?import=indexhibit2&amp;step=3" method="post">
     227            <?php wp_nonce_field( 'import-indexhibit2' ); ?>
     228        <p class="submit"><input type="submit" name="submit" class="button" value="<?php echo esc_attr__( 'Finish', 'indexhibit2-importer' ); ?>" /></p>
     229        </form>
     230            <?php
     231        }
     232
     233        /**
     234         * Perform import of exhibits
     235         */
     236        public function exhibits2wp( $exhibits = '' ) {
     237            $options            = get_option( 'ix2_options' );
     238            $count              = 0;
     239            $ixexhibits2wpposts = array();
     240
     241            if ( is_array( $exhibits ) ) {
     242                echo '<p>' . __( 'Importing exhibits...', 'indexhibit2-importer' ) . '</p>';
     243                foreach ( $exhibits as $exhibit ) {
     244                    $post_author   = get_current_user_id();
     245                    $post_title    = $exhibit['title'];
     246                    $post_content  = $exhibit['content'];
     247                    $post_home     = $exhibit['home'];
     248                    $post_date     = $exhibit['pdate'];
     249                    $post_modified = $exhibit['udate'];
     250                    $post_format   = $exhibit['format'];
     251
     252                    // Set Indexhibit-to-WordPress status translation.
     253                    $ixstatus    = array(
     254                        0 => 'draft',
     255                        1 => 'publish',
     256                    );
     257                    $post_status = $ixstatus[ $exhibit['status'] ];
     258
     259                    $ret_id = 0;
     260
     261                    // Check if the exhibit is already imported.
     262                    if ( $pinfo = post_exists( $post_title, '', $post_date ) ) {
     263                        $ret_id = wp_insert_post(
     264                            array(
     265                                'ID'                => $pinfo,
     266                                'post_author'       => $post_author,
     267                                'post_date'         => $post_date,
     268                                'post_date_gmt'     => $post_date,
     269                                'post_modified'     => $post_modified,
     270                                'post_modified_gmt' => $post_modified,
     271                                'post_title'        => $post_title,
     272                                'post_content'      => $post_content,
     273                                'post_status'       => $post_status,
     274                                'post_type'         => 'page',
     275                                'comment_status'    => 'closed',
     276                                'ping_status'       => 'closed',
     277                            )
     278                        );
     279                        if ( is_wp_error( $ret_id ) ) {
     280                            return $ret_id;
     281                        }
     282                        add_post_meta( $ret_id, 'ix2_exhibit_format', $post_format );
     283                    } else {
     284                        $ret_id = wp_insert_post(
     285                            array(
     286                                'post_author'       => $post_author,
     287                                'post_date'         => $post_date,
     288                                'post_date_gmt'     => $post_date,
     289                                'post_modified'     => $post_modified,
     290                                'post_modified_gmt' => $post_modified,
     291                                'post_title'        => $post_title,
     292                                'post_content'      => $post_content,
     293                                'post_status'       => $post_status,
     294                                'post_type'         => 'page',
     295                                'comment_status'    => 'closed',
     296                                'ping_status'       => 'closed',
     297                            )
     298                        );
     299                        if ( is_wp_error( $ret_id ) ) {
     300                            return $ret_id;
     301                        }
     302                        add_post_meta( $ret_id, 'ix2_exhibit_format', $post_format );
     303                    }
     304
     305                    // Set front page.
     306                    if ( ! empty( $post_home ) ) {
     307                        update_option( 'page_on_front', $ret_id );
     308                        update_option( 'show_on_front', 'page' );
     309                    }
     310
     311                    $ixexhibits2wpposts[ $exhibit['id'] ] = $ret_id;
     312                    ++$count;
     313                }
     314            }
     315
     316            // Store exhibit2post translation for later use.
     317            $options['ix2_exhibits2wpposts'] = $ixexhibits2wpposts;
     318            update_option( 'ix2_options', $options );
     319
     320            echo '<p>' . sprintf( __( 'Done! <strong>%1$s</strong> exhibits imported.', 'indexhibit2-importer' ), $count ) . '<br /><br /></p>';
     321            return true;
     322        }
     323
     324        /**
     325         * Perform import of media files
     326         */
     327        public function media2wp( $files = '', $post_id ) {
     328            $count = 0;
     329
     330            if ( is_array( $files ) ) {
     331                echo '<p>' . sprintf( __( 'Importing media files from exhibit "%1$s"...', 'indexhibit2-importer' ), get_the_title( $post_id ) ) . '</p>';
     332                foreach ( $files as $file ) {
     333                    ++$count;
     334                    $process = $this->process_attachment( $file, $post_id );
     335                }
     336            }
     337
     338            echo '<p>' . sprintf( __( 'Done! <strong>%1$s</strong> media files imported.', 'indexhibit2-importer' ), $count ) . '<br /><br /></p>';
     339            return true;
     340        }
     341
     342        /**
     343         * Create new attachments based on import information
     344         */
     345        public function process_attachment( $file, $parent_post ) {
     346            $options = get_option( 'ix2_options' );
     347            $ixurl   = $options['ix2_url'];
     348
     349            $media_file       = $file['media_file'];
     350            $media_file_noext = preg_replace( '/\\.[^.\\s]{3,4}$/', '', $media_file );
     351
     352            $post = array(
     353                'post_title'    => ( ! empty( $file['media_title'] ) ? $file['media_title'] : $media_file_noext ),
     354                'post_content'  => $file['media_caption'],
     355                'post_date'     => $file['media_udate'],
     356                'post_date_gmt' => $file['media_udate'],
     357                'menu_order'    => $file['media_order'],
     358                'post_parent'   => $parent_post,
     359            );
     360
     361            $media_url = trailingslashit( $ixurl ) . 'files/gimgs/' . $media_file;
     362
     363            $pre_process = $this->pre_process_attachment( $post, $media_url );
     364            if ( is_wp_error( $pre_process ) ) {
     365                return array(
     366                    'fatal'   => false,
     367                    'type'    => 'error',
     368                    'code'    => $pre_process->get_error_code(),
     369                    'message' => $pre_process->get_error_message(),
     370                    'text'    => sprintf( __( '%1$s was not uploaded. (<strong>%2$s</strong>: %3$s)', 'indexhibit2-importer' ), $post['post_title'], $pre_process->get_error_code(), $pre_process->get_error_message() ),
     371                );
     372            }
     373            // If the URL is absolute, but does not contain address, then upload it assuming base_site_url.
     374            if ( preg_match( '|^/[\w\W]+$|', $media_url ) ) {
     375                $base_url  = get_home_url();
     376                $media_url = rtrim( $base_url, '/' ) . $media_url;
     377            }
     378            $upload = $this->fetch_remote_file( $media_url, $post );
     379            if ( is_wp_error( $upload ) ) {
     380                return array(
     381                    'fatal'   => ( $upload->get_error_code() === 'upload_dir_error' && $upload->get_error_message() !== 'Invalid file type' ? true : false ),
     382                    'type'    => 'error',
     383                    'code'    => $upload->get_error_code(),
     384                    'message' => $upload->get_error_message(),
     385                    'text'    => sprintf( __( '%1$s could not be uploaded because of an error. (<strong>%2$s</strong>: %3$s)', 'indexhibit2-importer' ), $post['post_title'], $upload->get_error_code(), $upload->get_error_message() ),
     386                );
     387            }
     388            if ( $info = wp_check_filetype( $upload['file'] ) ) {
     389                $post['post_mime_type'] = $info['type'];
     390            } else {
     391                $upload = new WP_Error( 'attachment_processing_error', __( 'Invalid file type', 'indexhibit2-importer' ) );
     392                return array(
     393                    'fatal'   => false,
     394                    'type'    => 'error',
     395                    'code'    => $upload->get_error_code(),
     396                    'message' => $upload->get_error_message(),
     397                    'text'    => sprintf( __( '%1$s could not be uploaded because of an error. (<strong>%2$s</strong>: %3$s)', 'indexhibit2-importer' ), $post['post_title'], $upload->get_error_code(), $upload->get_error_message() ),
     398                );
     399            }
     400            $post['guid']        = $upload['url'];
     401            $post['post_author'] = get_current_user_id();
     402            // As per wp-admin/includes/upload.php.
     403            $post_id = wp_insert_attachment( $post, $upload['file'] );
     404            wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
     405            return array(
     406                'fatal' => false,
     407                'type'  => 'updated',
     408                'text'  => sprintf( __( '%s was uploaded successfully', 'indexhibit2-importer' ), $post['post_title'] ),
     409            );
     410        }
     411
     412        /**
     413         * Check if attachment already exist
     414         */
     415        public function pre_process_attachment( $post, $url ) {
     416            global $wpdb;
     417            $imported = $wpdb->get_results(
     418                $wpdb->prepare(
     419                    "SELECT ID, post_date_gmt, guid
    419420                FROM $wpdb->posts WHERE post_type = 'attachment' AND post_title = %s
    420421                ",
    421                 $post['post_title']
    422             )
    423         );
    424         if ( $imported ) {
    425             foreach ( $imported as $attachment ) {
    426                 if ( sanitize_file_name( basename( $url ) ) == basename( $attachment->guid ) ) {
    427                     if ( $post['post_date_gmt'] == $attachment->post_date_gmt ) {
    428                         $remote_response = wp_safe_remote_get( $url );
    429                         $headers = wp_remote_retrieve_headers( $remote_response );
    430                         if ( filesize( get_attached_file( $attachment->ID ) ) == $headers['content-length'] ) {
    431                             return new WP_Error( 'duplicate_file_notice', __( 'File already exists', 'indexhibit2-importer' ) );
    432                         }
    433                     }
    434                 }
    435             }
    436         }
    437         return false;
    438     }
    439    
    440     /**
    441      * Attempt to download a remote file attachment
    442      *
    443      */
    444     public function fetch_remote_file( $url, $post ) {
    445         // Extract the file name and extension from the url
    446         $file_name = basename( $url );
    447         // Get placeholder file in the upload dir with a unique, sanitized filename
    448         $upload = wp_upload_bits( $file_name, 0, '', $post['post_date'] );
    449         if ( $upload['error'] ) {
    450             return new WP_Error( 'upload_dir_error', $upload['error'] );
    451         }
    452         // Fetch the remote url and write it to the placeholder file
    453         $remote_response = wp_safe_remote_get( $url, array(
    454             'timeout'  => 300,
    455             'stream'   => true,
    456             'filename' => $upload['file'],
    457             )
    458         );
    459         $headers = wp_remote_retrieve_headers( $remote_response );
    460 
    461         // Request failed
    462         if ( ! $headers ) {
    463             @unlink( $upload['file'] );
    464             return new WP_Error( 'import_file_error', __( 'Remote server did not respond', 'indexhibit2-importer' ) );
    465         }
    466         // Make sure the fetch was successful
    467         $remote_response_code = wp_remote_retrieve_response_code( $remote_response );
    468         if ( $remote_response_code != '200' ) {
    469             @unlink( $upload['file'] );
    470             return new WP_Error( 'import_file_error', sprintf( __( 'Remote server returned error response %1$d %2$s', 'indexhibit2-importer' ), esc_html( $remote_response_code ), get_status_header_desc( $remote_response_code ) ) );
    471         }
    472         $filesize = filesize( $upload['file'] );
    473         if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
    474             @unlink( $upload['file'] );
    475             return new WP_Error( 'import_file_error', __( 'Remote file is incorrect size', 'indexhibit2-importer' ) );
    476         }
    477         if ( 0 == $filesize ) {
    478             @unlink( $upload['file'] );
    479             return new WP_Error( 'import_file_error', __( 'Zero size file downloaded', 'indexhibit2-importer' ) );
    480         }
    481         return $upload;
    482     }
    483 
    484     /**
    485      * Insert attachments into posts content
    486      *
    487      */
    488     public function insert_attachments( $post_id ) {
    489         $attachments = get_attached_media( 'image', $post_id );
    490         if ( $attachments ) {
    491             $post = get_post( $post_id );
    492             $content = $post->post_content;
    493             foreach ( $attachments as $attachment ) {
    494                 $content .= wp_get_attachment_image( $attachment->ID, 'full' );
    495             }
    496             $updated_post = array(
    497                 'ID'           => $post_id,
    498                 'post_content' => $content,
    499             );
    500             // Update post
    501             wp_update_post( $updated_post );
    502         }
    503     }
    504 
    505     /**
    506      * Perform cleanup of plugin options
    507      *
    508      */
    509     public function clean_options() {
    510         delete_option( 'ix2_options' );
    511     }
    512 
    513     /**
    514      * Display post-import information
    515      *
    516      */
    517     public function tips() {
    518     ?>
    519         <p><?php _e( 'Welcome to WordPress. We hope that you will find this platform incredibly rewarding! As a new WordPress user coming from Indexhibit 2, there are some things that we would like to point out. Hopefully, they will help your transition go as smoothly as possible.', 'indexhibit2-importer' ); ?></p>
    520         <p><?php _e( 'This plugin imports exhibits and media files from Indexhibit 2 sites, but ignores links, sections, subsections and exhibit formats.', 'indexhibit2-importer' ); ?></p>
    521         <p><?php _e( 'Exhibits are imported as pages and media files are imported as attachments which are attached to their corresponding pages. If needed, you can convert the imported pages to posts or other post types with a plugin such as <a href="https://wordpress.org/plugins/post-type-switcher/">Post Type Switcher</a>.', 'indexhibit2-importer' ); ?></p>
    522         <h3><?php _e( 'WordPress Resources', 'indexhibit2-importer' ); ?></h3>
    523         <p><?php _e( 'Finally, there are numerous WordPress resources around the internet. Some of them are:', 'indexhibit2-importer' ); ?></p>
    524         <ul>
    525         <li><?php _e( '<a href="http://wordpress.org/">The official WordPress site</a>', 'indexhibit2-importer' ); ?></li>
    526         <li><?php _e( '<a href="http://wordpress.org/support/">The WordPress support forums</a>', 'indexhibit2-importer' ); ?></li>
    527         <li><?php _e( '<a href="http://developer.wordpress.org/">The WordPress developer docs (In other words, the WordPress Bible)</a>', 'indexhibit2-importer' ); ?></li>
    528         </ul>
    529         <p><?php _e( 'That&#8217;s it! What are you waiting for? Go <a href="../wp-login.php">log in</a>!', 'indexhibit2-importer' ); ?></p>
    530     <?php
    531     }
    532 
    533     /**
    534      * Registered callback function for the importer
    535      *
    536      */
    537     public function dispatch() {
    538         $this->header();
    539 
    540         $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
    541         if ( $step > 0 ) {
    542             check_admin_referer( 'import-indexhibit2' );
    543 
    544             $options = get_option( 'ix2_options' );
    545 
    546             if ( isset( $_POST['ixurl'] ) ) {
    547                 $options['ix2_url'] = sanitize_text_field( esc_url( $_POST['ixurl'] ) );
    548             }
    549             if ( isset( $_POST['dbname'] ) ) {
    550                 $options['ix2_name'] = sanitize_text_field( $_POST['dbname'] );
    551             }
    552             if ( isset( $_POST['dbuser'] ) ) {
    553                 $options['ix2_user'] = sanitize_text_field( $_POST['dbuser'] );
    554             }
    555             if ( isset( $_POST['dbpass'] ) ) {
    556                 $options['ix2_pass'] = sanitize_text_field( $_POST['dbpass'] );
    557             }
    558             if ( isset( $_POST['dbhost'] ) ) {
    559                 $options['ix2_host'] = sanitize_text_field( $_POST['dbhost'] );
    560             }
    561             if ( isset( $_POST['dbprefix'] ) ) {
    562                 $options['ix2_dbprefix'] = sanitize_text_field( $_POST['dbprefix'] );
    563             }
    564             if ( isset( $_POST['mdinsert'] ) ) {
    565                 $options['ix2_mdinsert'] = '1';
    566             } else {
    567                 $options['ix2_mdinsert'] = '0';
    568             }
    569 
    570             update_option( 'ix2_options', $options );
    571         }
    572 
    573         switch ( $step ) {
    574             default:
    575             case 0 :
    576                 // Clean options
    577                 $this->clean_options();
    578                 $this->greet();
    579                 break;
    580             case 1 :
    581                 // Try to remove execution time limit to avoid timeouts
    582                 set_time_limit( 0 );
    583                 // Initialize database connection
    584                 $conn = $this->init_ix2db();
    585                 if ( $conn ) {
    586                     // Import content
    587                     $result = $this->import_exhibits();
    588                     if ( is_wp_error( $result ) ) {
    589                         echo $result->get_error_message();
    590                     }
    591                 } else {
    592                     echo '<p>' . __( 'Cannot connect to Indexhibit 2 database', 'indexhibit2-importer' ) . '</p>';
    593                     echo '<a class="button" href="admin.php?import=indexhibit2">' . __( 'Try Again', 'indexhibit2-importer' ) . '</a>';
    594                 }
    595                 break;
    596             case 2 :
    597                 // Try to remove execution time limit to avoid timeouts
    598                 set_time_limit( 0 );
    599                 // Initialize database connection
    600                 $conn = $this->init_ix2db();
    601                 if ( $conn ) {
    602                     // Import content
    603                     $result = $this->import_media();
    604                     if ( is_wp_error( $result ) ) {
    605                         echo $result->get_error_message();
    606                     }
    607                 } else {
    608                     echo '<p>' . __( 'Cannot connect to Indexhibit 2 database', 'indexhibit2-importer' ) . '</p>';
    609                     echo '<a class="button" href="admin.php?import=indexhibit2">' . __( 'Try Again', 'indexhibit2-importer' ) . '</a>';
    610                 }
    611                 break;
    612             case 3 :
    613                 do_action( 'import_done', 'indexhibit2' );
    614                 $this->tips();
    615                 break;
    616         }
    617 
    618         $this->footer();
    619     }
    620 }
     422                    $post['post_title']
     423                )
     424            );
     425            if ( $imported ) {
     426                foreach ( $imported as $attachment ) {
     427                    if ( sanitize_file_name( basename( $url ) ) === basename( $attachment->guid ) ) {
     428                        if ( $post['post_date_gmt'] === $attachment->post_date_gmt ) {
     429                            $remote_response = wp_safe_remote_get( $url );
     430                            $headers         = wp_remote_retrieve_headers( $remote_response );
     431                            if ( filesize( get_attached_file( $attachment->ID ) ) === $headers['content-length'] ) {
     432                                return new WP_Error( 'duplicate_file_notice', __( 'File already exists', 'indexhibit2-importer' ) );
     433                            }
     434                        }
     435                    }
     436                }
     437            }
     438            return false;
     439        }
     440
     441        /**
     442         * Attempt to download a remote file attachment
     443         */
     444        public function fetch_remote_file( $url, $post ) {
     445            // Extract the file name and extension from the url.
     446            $file_name = basename( $url );
     447            // Get placeholder file in the upload dir with a unique, sanitized filename.
     448            $upload = wp_upload_bits( $file_name, null, '', $post['post_date'] );
     449            if ( $upload['error'] ) {
     450                return new WP_Error( 'upload_dir_error', $upload['error'] );
     451            }
     452            // Fetch the remote url and write it to the placeholder file.
     453            $remote_response = wp_safe_remote_get(
     454                $url,
     455                array(
     456                    'timeout'  => 300,
     457                    'stream'   => true,
     458                    'filename' => $upload['file'],
     459                )
     460            );
     461            $headers         = wp_remote_retrieve_headers( $remote_response );
     462
     463            // Request failed.
     464            if ( ! $headers ) {
     465                @unlink( $upload['file'] );
     466                return new WP_Error( 'import_file_error', __( 'Remote server did not respond', 'indexhibit2-importer' ) );
     467            }
     468            // Make sure the fetch was successful.
     469            $remote_response_code = wp_remote_retrieve_response_code( $remote_response );
     470            if ( '200' !== $remote_response_code ) {
     471                @unlink( $upload['file'] );
     472                return new WP_Error( 'import_file_error', sprintf( __( 'Remote server returned error response %1$d %2$s', 'indexhibit2-importer' ), esc_html( $remote_response_code ), get_status_header_desc( $remote_response_code ) ) );
     473            }
     474            $filesize = filesize( $upload['file'] );
     475            if ( isset( $headers['content-length'] ) && $filesize !== $headers['content-length'] ) {
     476                @unlink( $upload['file'] );
     477                return new WP_Error( 'import_file_error', __( 'Remote file is incorrect size', 'indexhibit2-importer' ) );
     478            }
     479            if ( 0 === $filesize ) {
     480                @unlink( $upload['file'] );
     481                return new WP_Error( 'import_file_error', __( 'Zero size file downloaded', 'indexhibit2-importer' ) );
     482            }
     483            return $upload;
     484        }
     485
     486        /**
     487         * Insert attachments into posts content
     488         */
     489        public function insert_attachments( $post_id ) {
     490            $attachments = get_attached_media( 'image', $post_id );
     491            if ( $attachments ) {
     492                $post    = get_post( $post_id );
     493                $content = $post->post_content;
     494                foreach ( $attachments as $attachment ) {
     495                    $content .= wp_get_attachment_image(
     496                        $attachment->ID,
     497                        'full',
     498                        false,
     499                        array( 'class' => 'attachment-full size-full ' . sprintf( 'wp-image-%d', $image['id'] ) )
     500                    );
     501                }
     502                $updated_post = array(
     503                    'ID'           => $post_id,
     504                    'post_content' => $content,
     505                );
     506                // Update post.
     507                wp_update_post( $updated_post );
     508            }
     509        }
     510
     511        /**
     512         * Perform cleanup of plugin options
     513         */
     514        public function clean_options() {
     515            delete_option( 'ix2_options' );
     516        }
     517
     518        /**
     519         * Display post-import information
     520         */
     521        public function tips() {
     522            ?>
     523        <p><?php _e( 'Welcome to WordPress. We hope that you will find this platform incredibly rewarding! As a new WordPress user coming from Indexhibit 2, there are some things that we would like to point out. Hopefully, they will help your transition go as smoothly as possible.', 'indexhibit2-importer' ); ?></p>
     524        <p><?php _e( 'This plugin imports exhibits and media files from Indexhibit 2 sites, but ignores links, sections, subsections and exhibit formats.', 'indexhibit2-importer' ); ?></p>
     525        <p><?php _e( 'Exhibits are imported as pages and media files are imported as attachments which are attached to their corresponding pages. If needed, you can convert the imported pages to posts or other post types with a plugin such as <a href="https://wordpress.org/plugins/post-type-switcher/">Post Type Switcher</a>.', 'indexhibit2-importer' ); ?></p>
     526        <h3><?php _e( 'WordPress Resources', 'indexhibit2-importer' ); ?></h3>
     527        <p><?php _e( 'Finally, there are numerous WordPress resources around the internet. Some of them are:', 'indexhibit2-importer' ); ?></p>
     528        <ul>
     529        <li><?php _e( '<a href="http://wordpress.org/">The official WordPress site</a>', 'indexhibit2-importer' ); ?></li>
     530        <li><?php _e( '<a href="http://wordpress.org/support/">The WordPress support forums</a>', 'indexhibit2-importer' ); ?></li>
     531        <li><?php _e( '<a href="http://developer.wordpress.org/">The WordPress developer docs (In other words, the WordPress Bible)</a>', 'indexhibit2-importer' ); ?></li>
     532        </ul>
     533        <p><?php _e( 'That&#8217;s it! What are you waiting for? Go <a href="../wp-login.php">log in</a>!', 'indexhibit2-importer' ); ?></p>
     534            <?php
     535        }
     536
     537        /**
     538         * Registered callback function for the importer
     539         */
     540        public function dispatch() {
     541            $this->header();
     542
     543            $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
     544            if ( $step > 0 ) {
     545                check_admin_referer( 'import-indexhibit2' );
     546
     547                $options = get_option( 'ix2_options' );
     548
     549                if ( isset( $_POST['ixurl'] ) ) {
     550                    $options['ix2_url'] = sanitize_text_field( esc_url( $_POST['ixurl'] ) );
     551                }
     552                if ( isset( $_POST['dbname'] ) ) {
     553                    $options['ix2_name'] = sanitize_text_field( $_POST['dbname'] );
     554                }
     555                if ( isset( $_POST['dbuser'] ) ) {
     556                    $options['ix2_user'] = sanitize_text_field( $_POST['dbuser'] );
     557                }
     558                if ( isset( $_POST['dbpass'] ) ) {
     559                    $options['ix2_pass'] = sanitize_text_field( $_POST['dbpass'] );
     560                }
     561                if ( isset( $_POST['dbhost'] ) ) {
     562                    $options['ix2_host'] = sanitize_text_field( $_POST['dbhost'] );
     563                }
     564                if ( isset( $_POST['dbprefix'] ) ) {
     565                    $options['ix2_dbprefix'] = sanitize_text_field( $_POST['dbprefix'] );
     566                }
     567                if ( isset( $_POST['mdinsert'] ) ) {
     568                    $options['ix2_mdinsert'] = '1';
     569                } else {
     570                    $options['ix2_mdinsert'] = '0';
     571                }
     572
     573                update_option( 'ix2_options', $options );
     574            }
     575
     576            switch ( $step ) {
     577                default:
     578                case 0:
     579                    // Clean options.
     580                    $this->clean_options();
     581                    $this->greet();
     582                    break;
     583                case 1:
     584                    // Try to remove execution time limit to avoid timeouts.
     585                    set_time_limit( 0 );
     586                    // Initialize database connection.
     587                    $conn = $this->init_ix2db();
     588                    if ( $conn ) {
     589                        // Import content.
     590                        $result = $this->import_exhibits();
     591                        if ( is_wp_error( $result ) ) {
     592                            echo $result->get_error_message();
     593                        }
     594                    } else {
     595                        echo '<p>' . __( 'Cannot connect to Indexhibit 2 database', 'indexhibit2-importer' ) . '</p>';
     596                        echo '<a class="button" href="admin.php?import=indexhibit2">' . __( 'Try Again', 'indexhibit2-importer' ) . '</a>';
     597                    }
     598                    break;
     599                case 2:
     600                    // Try to remove execution time limit to avoid timeouts.
     601                    set_time_limit( 0 );
     602                    // Initialize database connection.
     603                    $conn = $this->init_ix2db();
     604                    if ( $conn ) {
     605                        // Import content.
     606                        $result = $this->import_media();
     607                        if ( is_wp_error( $result ) ) {
     608                            echo $result->get_error_message();
     609                        }
     610                    } else {
     611                        echo '<p>' . __( 'Cannot connect to Indexhibit 2 database', 'indexhibit2-importer' ) . '</p>';
     612                        echo '<a class="button" href="admin.php?import=indexhibit2">' . __( 'Try Again', 'indexhibit2-importer' ) . '</a>';
     613                    }
     614                    break;
     615                case 3:
     616                    do_action( 'import_done', 'indexhibit2' );
     617                    $this->tips();
     618                    break;
     619            }
     620
     621            $this->footer();
     622        }
     623    }
    621624
    622625}
     
    624627/**
    625628 * Register importer
    626  *
    627629 */
    628630function ix2_importer_init() {
    629     load_plugin_textdomain( 'indexhibit2-importer' );
    630 
    631     $ix2_import = new Ix2_Import();
    632     register_importer( 'indexhibit2', __( 'Indexhibit 2', 'indexhibit2-importer' ), __( 'Import exhibits and media files from an Indexhibit 2 site.', 'indexhibit2-importer' ), array( $ix2_import, 'dispatch' ) );
     631load_plugin_textdomain( 'indexhibit2-importer' );
     632
     633$ix2_import = new Ix2_Import();
     634register_importer( 'indexhibit2', __( 'Indexhibit 2', 'indexhibit2-importer' ), __( 'Import exhibits and media files from an Indexhibit 2 site.', 'indexhibit2-importer' ), array( $ix2_import, 'dispatch' ) );
    633635}
    634636add_action( 'init', 'ix2_importer_init' );
  • indexhibit2-importer/trunk/readme.txt

    r2085196 r3131936  
    44Tags: importer, indexhibit
    55Requires at least: 4.0
    6 Tested up to: 5.2
     6Tested up to:
    77Requires PHP: 5.6
    8 Stable tag: 1.0.5
     8Stable tag: 1.0.
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4747
    4848== Changelog ==
     49
     50
     51
     52
    4953= 1.0.5 =
    5054* Ignore content in page check function
Note: See TracChangeset for help on using the changeset viewer.