function ojsimport_tools_page()
{
    //must check that the user has the required capability
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    echo "<h2>" . __('ojs Import', 'menu-ojsimport') . "</h2>";
    $hidden_field_name = 'ojsimport_submit_hidden';
    // See if the user has posted us some information
    // If they did, this hidden field will be set to 'Y'
    if (isset($_POST[$hidden_field_name]) && $_POST[$hidden_field_name] == 'Y') {
        $class = "updated";
        if ($_POST['Import']) {
            $ret = ojsimport::import_all();
            if (is_wp_error($ret)) {
                $message = $ret->get_error_message();
                $class = "error";
            } else {
                $message = 'Successfully imported.' . "</br>" . $ret;
            }
        } else {
            if ($_POST['Delete']) {
                $ret = ojsimport::delete_all_imported();
                if (is_wp_error($ret)) {
                    $message = $ret->get_error_message();
                    $class = "error";
                } else {
                    $message = 'successfully deleted all imported posts.';
                }
            }
        }
        // Put the result  message on the screen
        ?>
<div id="message" class="<?php 
        echo $class;
        ?>
"><p><strong><?php 
        _e($message, 'menu-ojsimport');
        ?>
</strong></p></div>
<?php 
    }
    // tools form
    ?>

<form name="form1" method="post" action="">
<input type="hidden" name="<?php 
    echo $hidden_field_name;
    ?>
" value="Y">

<p class="submit">
<input type="submit" name="Import" class="button-primary" value="<?php 
    esc_attr_e('Import from ojs');
    ?>
" />
<input type="submit" name="Delete" class="button-secondary" value="<?php 
    esc_attr_e('Delete all imported');
    ?>
" />
</p>

</form>
</div>

<?php 
}
Exemple #2
0
 public static function import_all()
 {
     global $wp_rewrite;
     global $ojs_mapping;
     self::$start_time = time();
     self::$fetch_time = 0;
     self::$posttime = time();
     self::$global_imported_links = self::get_ojsimport_links();
     self::$global_imported_posts = self::get_ojsimport_posts();
     $sources = array();
     $import_tests = (bool) get_option('ojsimport_import_tests');
     if ($import_tests) {
         require_once dirname(__FILE__) . '/ojsimport-test.php';
         $sources = ojsimport_test::get_sources();
     }
     $sources = array_merge($sources, self::get_data_sources());
     // login to data source server if auth details are given
     $login_url = get_option('ojsimport_login_url');
     $username = get_option('ojsimport_username');
     $password = get_option('ojsimport_password');
     if ($username != "") {
         if ($login_url == "") {
             $login_url = null;
         }
         $ret = ojsaccess::login($login_url, $username, $password);
         if ($ret === false) {
             return new WP_Error('curl_not_installed', __("You have to install php5_curl to use authentication!"));
         }
     }
     self::load_ec3();
     $g_ret = true;
     foreach ($sources as $key => $source) {
         if (is_wp_error($source)) {
             $g_ret = $source;
             continue;
         }
         $items = call_user_func($source, $key);
         if (is_wp_error($items)) {
             if (is_array($source)) {
                 // source is a class function
                 $source = $source[0] . '::' . $source[1];
             }
             $source .= '(' . $key . ')';
             error_log("ojsimport: could not import from:" . $source);
             error_log($items->get_error_message());
             $g_ret = $items;
             continue;
         }
         foreach ($items as $item) {
             if (is_wp_error($item)) {
                 error_log($item->get_error_message());
                 $g_ret = $item;
                 continue;
             }
             $ret = self::import_data($item);
             if (is_wp_error($ret)) {
                 error_log($ret->get_error_message());
                 $g_ret = $ret;
             }
         }
     }
     // XXX: this is needed due to a bug in wordpress category cache
     wp_cache_flush();
     delete_option('category_children');
     // XXX: needed to make permalinks work (not documented)
     $wp_rewrite->flush_rules();
     // delete leftover posts that are not available in the sources anymore
     // only if import was successful
     $ret = true;
     if (is_wp_error($g_ret)) {
         error_log('Import was not successful. Not deleting leftover posts!');
     } else {
         if (!empty(self::$global_imported_posts)) {
             $ret = self::delete_posts(self::$global_imported_posts);
         }
         if (is_wp_error($ret)) {
             $g_ret = $ret;
         }
         if (!empty(self::$global_imported_links)) {
             $ret = self::delete_links(self::$global_imported_links);
         }
     }
     if (is_wp_error($ret)) {
         $g_ret = $ret;
     }
     if (!is_wp_error($g_ret)) {
         $g_ret = 'The import took ' . (time() - self::$start_time) . ' seconds.' . "\n";
         $g_ret .= 'Fetching data took ' . self::$fetch_time . ' seconds.';
     }
     return $g_ret;
 }