/** * Import backup data * * @param $file array * @return bool * */ public static function import_backup($file = '') { global $wpdb, $yiw_wptables; $wpdb->show_errors(); $error = ''; if (isset($_FILES['import-file']) && empty($file)) { if (!isset($_FILES['import-file'])) { wp_die(__("The file you have insert doesn't valid.", 'yit')); } 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="%s" title="Tools -> Import">Tools -> Import</a>', 'yit'), admin_url('import.php', false)); break; 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="%s" title="Tools -> Import">Tools -> Import</a>', 'yit'), admin_url('import.php', false)); break; } 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="%s" title="Tools -> Import">Tools -> Import</a>', 'yit'), admin_url('import.php', false)); } if ($error != '') { yit_get_model('message')->addMessage($error, 'error'); return false; } } $file = empty($file) ? $_FILES['import-file']['tmp_name'] : $file; // get db encoded $content_file = file_get_contents($file); $db = unserialize(base64_decode(gzuncompress($content_file))); //yit_debug($db); die; array_walk_recursive($db, 'yit_convert_url', 'in_import'); //yit_debug($db); die; if (!is_array($db)) { wp_die(__('An error encoured during during import. Please try again.', 'yit')); } set_time_limit(0); // tables foreach (self::$wptables as $table) { // delete all row of each table $wpdb->query("TRUNCATE TABLE {$wpdb->{$table}}"); // insert new data $error_data = array(); $insert = array(); foreach ($db[$table] as $id => $data) { $insert[] = YIT_Backup::_makeInsertSQL($data); } if (!empty($db[$table])) { $insert = implode(', ', $insert); $fields = implode('`, `', array_keys($db[$table][0])); //wp_die("INSERT INTO `{$wpdb->$table}` ( `$fields` ) VALUES " . $insert); $wpdb->query("INSERT INTO `{$wpdb->{$table}}` ( `{$fields}` ) VALUES " . $insert); } } $tables = apply_filters('yit_sample_data_tables', array()); $tables = apply_filters('yit_sample_data_tables_import', $tables); if (!empty($tables)) { foreach ($tables as $table) { if (!isset($db[$table])) { continue; } if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}{$table}'") == $wpdb->prefix . $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', 'yit' ), $wpdb->prefix . $table ), '...<br />' ); // insert new data $insert = array(); foreach ($db[$table] as $id => $data) { $insert[] = YIT_Backup::_makeInsertSQL($data); } if (!empty($db[$table])) { $insert = implode(', ', $insert); $fields = implode('`, `', array_keys($db[$table][0])); $wpdb->query("INSERT INTO `{$wpdb->prefix}{$table}` ( `{$fields}` ) VALUES " . $insert); } } } } # yiw_string_( '<p></p><p><strong>', '// options', '</strong><br />' ); // delete options $theme = get_option('stylesheet'); $options = array(yit_get_model('panel')->option_name, 'sidebars_widgets', 'show_on_front', 'page_on_front', 'page_for_posts', 'widget%', 'theme\\_mods\\_%', 'rewrite_rules'); $options = apply_filters('yit_sample_data_options', $options); $options = apply_filters('yit_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', 'yit' ), $wpdb->options ), '...<br />' ); #else #yiw_string_( '', sprintf( __( 'Error during deleting from %s table (SQL: %s)', 'yit' ), $wpdb->options, $sql ), '...<br />' ); $wpdb->query($sql); // 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', 'yit' ), $wpdb->options ), '...</p>' ); #else # yiw_string_( '', sprintf( __( 'Error during insert new values (IDs: %s), into %s table', 'yit' ), implode( $error_data, ' ' ), $wpdb->options ), '...</p>' ); # echo '</p>'; yit_delete_cache_callback(); do_action('yit_after_import'); return true; }
/** * @return void * @since 1.0.0 */ function yit_install_sampledata_callback() { global $wpdb; $gz = YIT_THEME_ASSETS_DIR . '/sample-data/' . YIT_THEME_NAME . '.gz'; if (file_exists($gz)) { require_once YIT_CORE_LIB . '/yit/Backup/Backup.php'; if (YIT_Backup::import_backup($gz)) { yit_get_model('message')->addMessage(__('Sample data installed correctly!', 'yit')); } else { yit_get_model('message')->addMessage(__('Error. Unable to install sample data!', 'yit') . ' SQL ERROR: ' . $wpdb->last_error, 'error'); } } else { yit_get_model('message')->addMessage(__('Error. The file ' . $gz . ' does not exists!', 'yit'), 'error'); } yit_get_model('message')->printGlobalMessages(); die; }
/** * @return void * @since 1.0.0 */ function yit_install_sampledata_callback() { global $wpdb; $gz = YIT_THEME_ASSETS_DIR . '/sample-data/' . YIT_THEME_NAME . '.gz'; global $woocommerce; if (!empty($woocommerce) && version_compare(preg_replace('/-beta-([0-9]+)/', '', $woocommerce->version), '2.1', '<')) { $gz = YIT_THEME_ASSETS_DIR . '/sample-data/' . YIT_THEME_NAME . '_woo_2.0.x.gz'; } if (file_exists($gz)) { require_once YIT_CORE_LIB . '/yit/Backup/Backup.php'; if (YIT_Backup::import_backup($gz)) { yit_get_model('message')->addMessage(__('Sample data installed correctly!', 'yit')); } else { yit_get_model('message')->addMessage(__('Error. Unable to install sample data!', 'yit') . ' SQL ERROR: ' . $wpdb->last_error, 'error'); } } else { yit_get_model('message')->addMessage(__('Error. The file ' . $gz . ' does not exists!', 'yit'), 'error'); } yit_get_model('message')->printGlobalMessages(); die; }
/** * Save all options in the database, after panel submit * * @since 1.0.0 */ public function save_options_callback() { if (!(isset($_POST['_yit_theme_options_nonce']) && wp_verify_nonce($_POST['_yit_theme_options_nonce'], 'yit-theme-options'))) { return; } if (isset($_POST['yit-subpage']) && $_POST['yit-subpage'] == 'backup') { if ($_POST['yit-action'] == 'import-file' && isset($_FILES['import-file'])) { require_once YIT_CORE_LIB . '/yit/Backup/Backup.php'; if (YIT_Backup::import_backup()) { yit_add_message($this->_messages['imported'], 'updated', 'panel'); } else { yit_add_message($this->_messages['imported-error'], 'error', 'panel'); } } elseif ($_POST['yit-action'] == 'export-file') { require_once YIT_CORE_LIB . '/yit/Backup/Backup.php'; $backup = YIT_Backup::export_backup(); header("Content-type: application/gzip-compressed"); header("Content-Disposition: attachment; filename={$backup['filename']}"); header("Content-Length: " . strlen($backup['content'])); header("Content-Transfer-Encoding: binary"); header('Accept-Ranges: bytes'); header("Pragma: no-cache"); header("Expires: 0"); echo $backup['content']; } elseif ($_POST['yit-action'] == 'configuration-save') { if (isset($_POST['configuration-name']) && $_POST['configuration-name'] != '') { $configs = get_option($this->configs_name); if ($configs != false) { $configs = maybe_unserialize($configs); } else { $configs = array(); } $new_config = array(); $new_config_name = esc_attr($_POST['configuration-name']); $new_config_slug = yit_avoid_duplicate(sanitize_title($new_config_name), $configs, 'key'); $new_config_backup = base64_encode(serialize($this->db_options)); $new_config[$new_config_slug] = array('name' => $new_config_name, 'values' => $new_config_backup); if (update_option($this->configs_name, array_merge($configs, $new_config))) { yit_get_model('message')->addMessage(__('Configuration has been saved.', 'yit'), 'updated', 'panel'); if (defined('DOING_AJAX')) { yit_get_model('message')->printMessages(); die; } } else { yit_get_model('message')->addMessage(__('An error occurring while trying to save configuration. Please try again.', 'yit'), 'error', 'panel'); if (defined('DOING_AJAX')) { yit_get_model('message')->printMessages(); die; } } } else { yit_get_model('message')->addMessage(__('Configuration name is missing.', 'yit'), 'error', 'panel'); if (defined('DOING_AJAX')) { yit_get_model('message')->printMessages(); die; } } } elseif ($_POST['yit-action'] == 'configuration-restore') { $configs = get_option($this->configs_name); $config_name = $_POST['configuration-restore']; $config = unserialize(base64_decode($configs[$config_name]['values'])); if (is_array($config)) { if (update_option($this->option_name, $config)) { yit_get_model('message')->addMessage(__('Configuration has been restored.', 'yit'), 'updated', 'panel'); if (defined('DOING_AJAX')) { yit_get_model('message')->printMessages(); die; } } else { yit_get_model('message')->addMessage(__('An error occurring while trying to restore configuration. Please try again.', 'yit'), 'error', 'panel'); if (defined('DOING_AJAX')) { yit_get_model('message')->printMessages(); die; } } } else { yit_get_model('message')->addMessage(__('An error occurring while trying to restore configuration. The backup seems to be damaged.', 'yit'), 'error', 'panel'); if (defined('DOING_AJAX')) { yit_get_model('message')->printMessages(); die; } } } elseif ($_POST['yit-action'] == 'configuration-remove') { $configs = get_option($this->configs_name); if ($configs != false) { $configs = maybe_unserialize($configs); } else { $configs = array(); } $to_delete = esc_attr($_POST['configuration-remove']); if (isset($configs[$to_delete])) { unset($configs[$to_delete]); } if (update_option($this->configs_name, $configs)) { yit_get_model('message')->addMessage(__('Configuration has been deleted.', 'yit'), 'updated', 'panel'); if (defined('DOING_AJAX')) { yit_get_model('message')->printMessages(); die; } } else { yit_get_model('message')->addMessage(__('An error occurring while trying to delete the configuration. Please try again.', 'yit'), 'error', 'panel'); if (defined('DOING_AJAX')) { yit_get_model('message')->printMessages(); die; } } } return; } elseif (!(isset($_POST['yit-action']) && $_POST['yit-action'] == 'save-options' && isset($_POST['yit-subpage']) && isset($_POST['yit_panel_option']) && isset($this->panel[$_POST['yit-subpage']]))) { return; } $page_options = $this->panel[$_POST['yit-subpage']]; $post_data = $_POST['yit_panel_option']; foreach ($page_options as $tab_path => $options) { foreach ($options as $option) { // must be process the saving also when there are the ID and the TYPE set for this option if (!(isset($option['id']) && isset($option['type']))) { continue; } // the option types that are one checkbox $checkbox_type = array('checkbox', 'onoff', 'checklist'); $multicheck_type = array('cat', 'pag'); // if there isn't this option in the form data sent and it's not a checkbox, can't process this option if (!in_array($option['type'], $checkbox_type) && !in_array($option['type'], $multicheck_type) && !isset($post_data[$option['id']])) { continue; } // if the option is a checkbox and the data it's not sent in the form data, it means that the checkbox is not checked if (in_array($option['type'], $checkbox_type) && !isset($post_data[$option['id']])) { $post_data[$option['id']] = false; } // for the types "cat" and "pag", if there are if (in_array($option['type'], $multicheck_type) && !isset($post_data[$option['id']])) { $post_data[$option['id']] = array(); } // get the value from the POST data, after having done all controls $value = $post_data[$option['id']]; // validation if (isset($option['validate'])) { if (is_array($option['validate'])) { $validate_filters = $option['validate']; } else { $validate_filters = array($option['validate']); } foreach ($validate_filters as $filter) { switch ($filter) { case 'yit_avoid_duplicate': $value = yit_avoid_duplicate($value, $this->get_option($option['id'])); break; default: $value = call_user_func($filter, $value); break; } } } // check if is defined the "data" index, with 'array-merge', that add the value in an array if (isset($option['data']) && 'array-merge' == $option['data']) { if (empty($value)) { continue; } $existing_array = $this->get_option($option['id'], array()); if (!is_array($existing_array) || empty($existing_array)) { $value = array($value); } else { $value = array_merge(array($value), $existing_array); } } // update the option in the database $this->update_option($option['id'], $value); } } yit_add_message($this->_messages['saved'], 'updated', 'panel'); if (defined('DOING_AJAX')) { $this->update_db_options(); yit_get_model('message')->printMessages(); die; } }