/** * Return an instance of this class. * * @since 1.0.0 * * @return object A single instance of this class. */ public static function get_instance() { // If the single instance hasn't been set, set it now. if (null == self::$instance) { self::$instance = new self(); } return self::$instance; }
/** * Initialize the plugin by loading admin scripts & styles and adding a * settings page and menu. * * @since 1.0.0 */ private function __construct() { /* * Call $plugin_slug from public plugin class. * */ $plugin = Alter_DB_Tables::get_instance(); $this->plugin_slug = $plugin->get_plugin_slug(); $this->plugin_name = $plugin->get_plugin_name(); $this->version = $plugin->get_plugin_version(); $this->plugin_slug_exp = 'alter-db-tables-exp'; $this->plugin_slug_imp = 'alter-db-tables-imp'; // Load admin style sheet and JavaScript. add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_styles')); add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts')); // Add the options page and menu item. add_action('init', array($this, 'alter_tables_exp')); add_action('admin_menu', array($this, 'add_plugin_admin_menu')); // Add an action link pointing to the options page. $plugin_basename = plugin_basename(plugin_dir_path(realpath(dirname(__FILE__))) . $this->plugin_slug . '.php'); add_filter('plugin_action_links_' . $plugin_basename, array($this, 'add_action_links')); /** * CONVERT TABLE DATA INTO JSON FORMAT. * * @since 1.0.0 */ function alter_tables_exp_json($extTable) { //Hide $wpdb object errors if has //ini_set( 'display_errors', false ); //error_reporting( 0 ); //Prepare query to get selected column if ($extTable) { if (ob_get_contents()) { ob_clean(); } global $wpdb; $field = ''; $getField = ''; $query = "SELECT * FROM {$extTable}"; $results = $wpdb->get_results($query, OBJECT); //$wpdb->print_error(); //echo "<pre>"; print_r($results); echo "</pre>"; die; //just to see everything //Set json file name $output_filename = $extTable . '_' . date('Ymd_His') . '.json'; # CSV FILE NAME WILL BE table_name_yyyymmdd_hhmmss.csv header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Description: File Transfer'); header('Content-Type: application/json'); header('Content-Disposition: attachment; filename=' . $output_filename); header('Expires: 0'); header('Pragma: public'); // Insert header row $first = true; $count = 0; // this is for $preJSON[] index foreach ($results as $key => $row) { // Add table headers $first = false; // Cast the Object to an array $preJSON[$count] = (array) $row; // increment the index ++$count; } // Build JSON $jSON = json_encode($preJSON); return $jSON; } //Die process after actions die; } /** * CONVERT TABLE DATA INTO CSV FORMAT. * * @since 1.0.0 */ function alter_tables_exp_csv($getTable) { //Hide $wpdb object errors if has ini_set('display_errors', false); error_reporting(0); //Prepare query to get selected column if ($getTable) { if (ob_get_contents()) { ob_clean(); } global $wpdb; $field = ''; $getField = ''; $query = "SELECT * FROM {$getTable}"; $results = $wpdb->get_results($wpdb->prepare($query, NULL)); //$wpdb->print_error(); //echo "<pre>"; print_r($results); echo "</pre>"; die; //just to see everything //Set csv file name $output_filename = $getTable . '_' . date('Ymd_His') . '.csv'; # CSV FILE NAME WILL BE table_name_yyyymmdd_hhmmss.csv $output_handle = @fopen('php://output', 'w'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Description: File Transfer'); header('Content-type: text/csv'); header('Content-Disposition: attachment; filename=' . $output_filename); header('Expires: 0'); header('Pragma: public'); // Insert header row fputcsv($output_handle, $csv_fields); //Parse results to csv format $first = true; // Parse results to csv format foreach ($results as $row) { // Add table headers if ($first) { $titles = array(); foreach ($row as $key => $val) { $titles[] = $key; } fputcsv($output_handle, $titles); $first = false; } $leadArray = (array) $row; // Cast the Object to an array // Add row to file fputcsv($output_handle, $leadArray); } //Flush DB cache and die process after actions echo $wpdb->flush(); die; } } }