function yiw_message() { $message = array('element_exists' => yiw_error_message(__('The element you have written is already exists. Please, add another name.', 'yiw'), false), 'saved' => yiw_updated_message('<strong>' . __('Settings saved', 'yiw') . '.</strong>', false), 'reset' => yiw_updated_message('<strong>' . __('Settings reset', 'yiw') . '.</strong>', false), 'delete' => yiw_updated_message('<strong>' . __('Element deleted correctly.', 'yiw') . '</strong>', false), 'updated' => yiw_updated_message('<strong>' . __('Element updated correctly.', 'yiw') . '</strong>', false), 'imported' => yiw_updated_message('<strong>' . __('Database imported correctly.', 'yiw') . '</strong>', false), 'no-imported' => yiw_error_message('<strong>' . __('An error encoured during during import. Please try again.', 'yiw') . '</strong>', false), 'file-not-valid' => yiw_error_message('<strong>' . __("The file you have insert doesn't valid.", 'yiw') . '</strong>', false), 'cant-import' => yiw_error_message('<strong>' . __("I'm sorry, the import featured is disabled.", 'yiw') . '</strong>', false), 'ord' => yiw_updated_message('<strong>' . __("Sorting done correctly.", 'yiw') . '</strong>', false)); if (isset($_GET['message'])) { echo $message[$_GET['message']]; } }
function yiw_import_theme() { global $wpdb, $yiw_wptables; $wpdb->show_errors(); if (!isset($_FILES['import-file'])) { wp_die(__("The file you have insert doesn't valid.", 'yiw')); } set_time_limit(0); switch (substr($_FILES['import-file']['name'], -3)) { case 'xml': $error = __(sprintf("The file you have insert is a WordPress eXtended RSS (WXR) file. You need to use this into the %s admin page to import this file. Here only <b>.gz</b> file are allowed.", '<a href="' . admin_url('import.php', false) . '">' . __('Tools -> Import', 'yiw') . '</a>'), 'yiw'); yiw_error_message($error); return; case 'zip': case 'rar': $error = __(sprintf("The file you have insert is a ZIP or RAR file, that it doesn't allowed in this case. Here only <b>.gz</b> file are allowed.", '<a href="' . admin_url('import.php', false) . '">' . __('Tools -> Import', 'yiw') . '</a>'), 'yiw'); yiw_error_message($error); return; } if (substr($_FILES['import-file']['name'], -2) != 'gz') { $error = __(sprintf("The file you have insert is not a valid file. Here only <b>.gz</b> file are allowed.", '<a href="' . admin_url('import.php', false) . '">' . __('Tools -> Import', 'yiw') . '</a>'), 'yiw'); yiw_error_message($error); return; } // get db encoded $content_file = file_get_contents($_FILES['import-file']['tmp_name']); $db = unserialize(base64_decode(gzuncompress($content_file))); // $url_from = array( 'siteurl' => $db['siteurl'], 'uploadsurl' => $db['uploadsurl'] ); // // $param = array( // 'type' => 'in_import', // 'url_from' => $url_from // ); array_walk_recursive($db, 'yiw_convert_url', 'in_import'); //yiw_debug($db); if (!is_array($db)) { wp_die(__('An error encoured during during import. Please try again.', 'yiw')); } // limit to avoid the MySQL gone away error $insert_limit = defined('YIW_INSERT_LIMIT') ? YIT_INSERT_LIMIT : 200; // tables foreach ($yiw_wptables as $table) { yiw_string_('<p></p><p><strong>', '// ' . $table, '</strong><br />'); // delete all row of each table $wpdb->query("TRUNCATE TABLE {$wpdb->{$table}}"); yiw_string_('', sprintf(__('Truncated %s table', 'yiw'), $wpdb->{$table}), '... Done!<br />'); // insert new data $error_data = array(); $insert = array(); foreach ($db[$table] as $id => $data) { $insert[] = yiw_makeInsertSQL($data); } // split isnert values in more ranges $insert = array_chunk($insert, $insert_limit); if (!empty($db[$table])) { foreach ($insert as $values) { $values = implode(', ', $values); $fields = implode('`, `', array_keys($db[$table][0])); //wp_die("INSERT INTO `{$wpdb->$table}` ( `$fields` ) VALUES " . $values); $wpdb->query("INSERT INTO `{$wpdb->{$table}}` ( `{$fields}` ) VALUES " . $values); } } yiw_string_('', sprintf(__('Updating %s table', 'yiw'), $wpdb->{$table}), '... Done!<br />'); } $tables = apply_filters('yit_sample_data_tables', array()); $tables = apply_filters('yit_sample_data_tables_import', $tables); if (!empty($tables)) { foreach ($tables as $table) { yiw_string_('<p></p><p><strong>', '// ' . $table, '</strong><br />'); // delete all row of each table $wpdb->query("TRUNCATE TABLE {$wpdb->prefix}{$table}"); yiw_string_('', sprintf(__('Truncated %s table', 'yiw'), $wpdb->prefix . $table), '... Done!<br />'); // insert new data $insert = array(); foreach ($db[$table] as $id => $data) { $insert[] = yiw_makeInsertSQL($data); } // split isnert values in more ranges $insert = array_chunk($insert, $insert_limit); if (!empty($db[$table])) { foreach ($insert as $values) { $values = implode(', ', $values); $fields = implode('`, `', array_keys($db[$table][0])); //wp_die("INSERT INTO `{$wpdb->$table}` ( `$fields` ) VALUES " . $values); $wpdb->query("INSERT INTO `{$wpdb->prefix}{$table}` ( `{$fields}` ) VALUES " . $values); } } yiw_string_('', sprintf(__('Updating %s table', 'yiw'), $wpdb->prefix . $table), '... Done!<br />'); } } yiw_string_('<p></p><p><strong>', '// options', '</strong><br />'); // delete options $theme = get_option('stylesheet'); $options = array(YIW_OPTIONS_DB . "_" . get_template(), 'sidebars_widgets', 'show_on_front', 'page_on_front', 'page_for_posts', 'widget%', 'theme\\_mods\\_%'); $options = apply_filters('yiw_sample_data_options', $options); $options = apply_filters('yiw_sample_data_options_import', $options); $sql_options = array(); foreach ($options as $option) { if (strpos($option, '%') !== FALSE) { $operator = 'LIKE'; } else { $operator = '='; } $sql_options[] = "option_name {$operator} '{$option}'"; } $sql_options = implode(' OR ', $sql_options); $sql = "DELETE FROM {$wpdb->options} WHERE {$sql_options};"; if ($wpdb->query($sql)) { yiw_string_('', sprintf(__('Deleted value from %s table', 'yiw'), $wpdb->options), '... Done!<br />'); } else { yiw_string_('', sprintf(__('Error during deleting from %s table (SQL: %s)', 'yiw'), $wpdb->options, $sql), '...<br />'); } // update options $error_data = array(); $check = $wpdb->get_results("SELECT * FROM {$wpdb->options} WHERE option_id = 1", ARRAY_A); foreach ($db['options'] as $id => $option) { if (!isset($check['blog_id'])) { unset($option['blog_id']); } if ($wpdb->insert($wpdb->options, $option)) { $insert = true; } else { $insert = false; } // save the ID that has error, to show. if (!$insert) { $wpdb->print_error(); } //$error_data[] = $option['option_name']; } if ($insert) { yiw_string_('', sprintf(__('Insert new values, into %s table', 'yiw'), $wpdb->options), '... Done!</p>'); } else { yiw_string_('', sprintf(__('Error during insert new values (IDs: %s), into %s table', 'yiw'), implode($error_data, ' '), $wpdb->options), '...</p>'); } echo '</p>'; return true; }