/** * Delete associated files * @return PMXI_Import_Record * @chainable */ public function deleteFiles() { $fileList = new PMXI_File_List(); foreach ($fileList->getBy('import_id', $this->id)->convertRecords() as $f) { if (@file_exists($f->path)) { wp_all_import_remove_source($f->path); } $f->delete(); } return $this; }
/** * Delete associated files * @return PMXI_Import_Record * @chainable */ public function deleteFiles() { $fileList = new PMXI_File_List(); foreach ($fileList->getBy('import_id', $this->id)->convertRecords() as $f) { $f->delete(); } return $this; }
public function __fix_db_schema() { $uploads = wp_upload_dir(); if (!is_dir($uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY) or !is_writable($uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY)) { die(sprintf(__('Uploads folder %s must be writable', 'wp_all_import_plugin'), $uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY)); } if (!is_dir($uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY) or !is_writable($uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY)) { die(sprintf(__('Uploads folder %s must be writable', 'wp_all_import_plugin'), $uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY)); } global $wpdb; // do not execute ALTER TABLE queries if sql user doesn't have ALTER privileges $grands = $wpdb->get_results("SELECT * FROM information_schema.user_privileges WHERE grantee LIKE \"'" . DB_USER . "'%\" AND PRIVILEGE_TYPE = 'ALTER' AND IS_GRANTABLE = 'YES';"); $table = $table = $this->getTablePrefix() . 'files'; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); // For every field in the table foreach ($tablefields as $tablefield) { if ('contents' == $tablefield->Field) { $list = new PMXI_File_List(); for ($i = 1; $list->getBy(NULL, 'id', $i, 1)->count(); $i++) { foreach ($list->convertRecords() as $file) { $file->save(); // resave file for file to be stored in uploads folder } } if (!empty($grands)) { $wpdb->query("ALTER TABLE {$table} DROP " . $tablefield->Field); } break; } } $table = $this->getTablePrefix() . 'imports'; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); $fields_to_alter = array('parent_import_id', 'iteration', 'deleted', 'executing', 'canceled', 'canceled_on', 'failed', 'failed_on', 'settings_update_on', 'last_activity'); // Check if field exists foreach ($tablefields as $tablefield) { if (in_array($tablefield->Field, $fields_to_alter)) { $fields_to_alter = array_diff($fields_to_alter, array($tablefield->Field)); } } if (!empty($fields_to_alter)) { if (empty($grands)) { return false; } foreach ($fields_to_alter as $field) { switch ($field) { case 'parent_import_id': $wpdb->query("ALTER TABLE {$table} ADD `parent_import_id` BIGINT(20) NOT NULL DEFAULT 0;"); break; case 'iteration': $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;"); break; case 'deleted': $wpdb->query("ALTER TABLE {$table} ADD `deleted` BIGINT(20) NOT NULL DEFAULT 0;"); break; case 'executing': $wpdb->query("ALTER TABLE {$table} ADD `executing` BOOL NOT NULL DEFAULT 0;"); break; case 'canceled': $wpdb->query("ALTER TABLE {$table} ADD `canceled` BOOL NOT NULL DEFAULT 0;"); break; case 'canceled_on': $wpdb->query("ALTER TABLE {$table} ADD `canceled_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); break; case 'failed': $wpdb->query("ALTER TABLE {$table} ADD `failed` BOOL NOT NULL DEFAULT 0;"); break; case 'failed_on': $wpdb->query("ALTER TABLE {$table} ADD `failed_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); break; case 'settings_update_on': $wpdb->query("ALTER TABLE {$table} ADD `settings_update_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); break; case 'last_activity': $wpdb->query("ALTER TABLE {$table} ADD `last_activity` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); break; default: # code... break; } } } $table = $this->getTablePrefix() . 'posts'; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); $iteration = false; // Check if field exists foreach ($tablefields as $tablefield) { if ('iteration' == $tablefield->Field) { $iteration = true; } } if (!$iteration) { if (empty($grands)) { ?> <div class="error"><p> <?php printf(__('<b>%s Plugin</b>: Current sql user %s doesn\'t have ALTER privileges', 'pmwi_plugin'), self::getInstance()->getName(), DB_USER); ?> </p></div> <?php return false; } $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;"); } if (!empty($wpdb->charset)) { $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; } if (!empty($wpdb->collate)) { $charset_collate .= " COLLATE {$wpdb->collate}"; } $table_prefix = $this->getTablePrefix(); $wpdb->query("CREATE TABLE IF NOT EXISTS {$table_prefix}history (\n\t\t\tid BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n\t\t\timport_id BIGINT(20) UNSIGNED NOT NULL,\n\t\t\ttype ENUM('manual','processing','trigger','continue','') NOT NULL DEFAULT '',\t\n\t\t\ttime_run TEXT,\t\n\t\t\tdate DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\t\t\n\t\t\tsummary TEXT,\n\t\t\tPRIMARY KEY (id)\n\t\t) {$charset_collate};"); return true; }
public function __fix_db_schema() { $uploads = wp_upload_dir(); if (!is_dir($uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY) or !is_writable($uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY)) { die(sprintf(__('Uploads folder %s must be writable', 'wp_all_import_plugin'), $uploads['basedir'] . DIRECTORY_SEPARATOR . self::LOGS_DIRECTORY)); } if (!is_dir($uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY) or !is_writable($uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY)) { die(sprintf(__('Uploads folder %s must be writable', 'wp_all_import_plugin'), $uploads['basedir'] . DIRECTORY_SEPARATOR . WP_ALL_IMPORT_UPLOADS_BASE_DIRECTORY)); } // create/update required database tables require_once ABSPATH . 'wp-admin/includes/upgrade.php'; require self::ROOT_DIR . '/schema.php'; global $wpdb; if (function_exists('is_multisite') && is_multisite()) { // check if it is a network activation - if so, run the activation function for each blog id if (isset($_GET['networkwide']) && $_GET['networkwide'] == 1) { $old_blog = $wpdb->blogid; // Get all blog ids $blogids = $wpdb->get_col("SELECT blog_id FROM {$wpdb->blogs}"); foreach ($blogids as $blog_id) { switch_to_blog($blog_id); require self::ROOT_DIR . '/schema.php'; dbDelta($plugin_queries); // sync data between plugin tables and wordpress (mostly for the case when plugin is reactivated) $post = new PMXI_Post_Record(); $wpdb->query('DELETE FROM ' . $post->getTable() . ' WHERE post_id NOT IN (SELECT ID FROM ' . $wpdb->posts . ')'); } switch_to_blog($old_blog); return; } } dbDelta($plugin_queries); // do not execute ALTER TABLE queries if sql user doesn't have ALTER privileges $grands = $wpdb->get_results("SELECT * FROM information_schema.user_privileges WHERE grantee LIKE \"'" . DB_USER . "'%\" AND PRIVILEGE_TYPE = 'ALTER' AND IS_GRANTABLE = 'YES';"); $table = $table = $this->getTablePrefix() . 'files'; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); // For every field in the table foreach ($tablefields as $tablefield) { if ('contents' == $tablefield->Field) { $list = new PMXI_File_List(); for ($i = 1; $list->getBy(NULL, 'id', $i, 1)->count(); $i++) { foreach ($list->convertRecords() as $file) { $file->save(); // resave file for file to be stored in uploads folder } } if (!empty($grands)) { $wpdb->query("ALTER TABLE {$table} DROP " . $tablefield->Field); } break; } } $table = $this->getTablePrefix() . 'imports'; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); $fields_to_alter = array('parent_import_id', 'iteration', 'deleted', 'executing', 'canceled', 'canceled_on', 'failed', 'failed_on', 'settings_update_on', 'last_activity'); // Check if field exists foreach ($tablefields as $tablefield) { if (in_array($tablefield->Field, $fields_to_alter)) { $fields_to_alter = array_diff($fields_to_alter, array($tablefield->Field)); } } if (!empty($fields_to_alter)) { if (empty($grands)) { return false; } foreach ($fields_to_alter as $field) { switch ($field) { case 'parent_import_id': $wpdb->query("ALTER TABLE {$table} ADD `parent_import_id` BIGINT(20) NOT NULL DEFAULT 0;"); break; case 'iteration': $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;"); break; case 'deleted': $wpdb->query("ALTER TABLE {$table} ADD `deleted` BIGINT(20) NOT NULL DEFAULT 0;"); break; case 'executing': $wpdb->query("ALTER TABLE {$table} ADD `executing` BOOL NOT NULL DEFAULT 0;"); break; case 'canceled': $wpdb->query("ALTER TABLE {$table} ADD `canceled` BOOL NOT NULL DEFAULT 0;"); break; case 'canceled_on': $wpdb->query("ALTER TABLE {$table} ADD `canceled_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); break; case 'failed': $wpdb->query("ALTER TABLE {$table} ADD `failed` BOOL NOT NULL DEFAULT 0;"); break; case 'failed_on': $wpdb->query("ALTER TABLE {$table} ADD `failed_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); break; case 'settings_update_on': $wpdb->query("ALTER TABLE {$table} ADD `settings_update_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); break; case 'last_activity': $wpdb->query("ALTER TABLE {$table} ADD `last_activity` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); break; default: # code... break; } } } $table = $this->getTablePrefix() . 'posts'; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); $iteration = false; $specified = false; // Check if field exists foreach ($tablefields as $tablefield) { if ('iteration' == $tablefield->Field) { $iteration = true; } if ('specified' == $tablefield->Field) { $specified = true; } } if (!$iteration) { if (empty($grands)) { ?> <div class="error"><p> <?php printf(__('<b>%s Plugin</b>: Current sql user %s doesn\'t have ALTER privileges', 'pmwi_plugin'), self::getInstance()->getName(), DB_USER); ?> </p></div> <?php return false; } $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;"); } if (!$specified and !empty($grands)) { $wpdb->query("ALTER TABLE {$table} ADD `specified` BOOL NOT NULL DEFAULT 0;"); } if (!empty($wpdb->charset)) { $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; } if (!empty($wpdb->collate)) { $charset_collate .= " COLLATE {$wpdb->collate}"; } $table_prefix = $this->getTablePrefix(); $wpdb->query("CREATE TABLE IF NOT EXISTS {$table_prefix}history (\n\t\t\tid BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n\t\t\timport_id BIGINT(20) UNSIGNED NOT NULL,\n\t\t\ttype ENUM('manual','processing','trigger','continue','') NOT NULL DEFAULT '',\t\n\t\t\ttime_run TEXT,\t\n\t\t\tdate DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\t\t\n\t\t\tsummary TEXT,\n\t\t\tPRIMARY KEY (id)\n\t\t) {$charset_collate};"); return true; }
/** * Method perfoms transition from version when file uploads has been stored in dabase to the solution when it stored on disk * NOTE: the function can be removed when plugin version progress and it's sure matter nobody has ver 1.03 */ public function __ver_1_04_transition_fix() { $uploads = wp_upload_dir(); if (!is_dir($uploads['basedir'] . '/wpallimport_history') or !is_writable($uploads['basedir'] . '/wpallimport_history')) { die(sprintf(__('Uploads folder %s must be writable', 'pmxi_plugin'), $uploads['basedir'] . '/wpallimport_history')); } if (!is_dir($uploads['basedir'] . '/wpallimport_logs') or !is_writable($uploads['basedir'] . '/wpallimport_logs')) { die(sprintf(__('Uploads folder %s must be writable', 'pmxi_plugin'), $uploads['basedir'] . '/wpallimport_logs')); } $table = $table = $this->getTablePrefix() . 'files'; global $wpdb; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); // For every field in the table foreach ($tablefields as $tablefield) { if ('contents' == $tablefield->Field) { $list = new PMXI_File_List(); for ($i = 1; $list->getBy(NULL, 'id', $i, 1)->count(); $i++) { foreach ($list->convertRecords() as $file) { $file->save(); // resave file for file to be stored in uploads folder } } $wpdb->query("ALTER TABLE {$table} DROP " . $tablefield->Field); break; } } }
public function __fix_db_schema() { $uploads = wp_upload_dir(); if (!is_dir($uploads['basedir'] . '/wpallimport/logs') or !is_writable($uploads['basedir'] . '/wpallimport/logs')) { die(sprintf(__('Uploads folder %s must be writable', 'pmxi_plugin'), $uploads['basedir'] . '/wpallimport/logs')); } if (!is_dir($uploads['basedir'] . '/wpallimport') or !is_writable($uploads['basedir'] . '/wpallimport')) { die(sprintf(__('Uploads folder %s must be writable', 'pmxi_plugin'), $uploads['basedir'] . '/wpallimport')); } $table = $table = $this->getTablePrefix() . 'files'; global $wpdb; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); // For every field in the table foreach ($tablefields as $tablefield) { if ('contents' == $tablefield->Field) { $list = new PMXI_File_List(); for ($i = 1; $list->getBy(NULL, 'id', $i, 1)->count(); $i++) { foreach ($list->convertRecords() as $file) { $file->save(); // resave file for file to be stored in uploads folder } } $wpdb->query("ALTER TABLE {$table} DROP " . $tablefield->Field); break; } } $table = $this->getTablePrefix() . 'imports'; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); $parent_import_id = false; $iteration = false; $deleted = false; $executing = false; $canceled = false; $canceled_on = false; $failed = false; $failed_on = false; $settings_update_on = false; $last_activity = false; // Check if field exists foreach ($tablefields as $tablefield) { if ('parent_import_id' == $tablefield->Field) { $parent_import_id = true; } if ('iteration' == $tablefield->Field) { $iteration = true; } if ('deleted' == $tablefield->Field) { $deleted = true; } if ('executing' == $tablefield->Field) { $executing = true; } if ('canceled' == $tablefield->Field) { $canceled = true; } if ('canceled_on' == $tablefield->Field) { $canceled_on = true; } if ('failed' == $tablefield->Field) { $failed = true; } if ('failed_on' == $tablefield->Field) { $failed_on = true; } if ('settings_update_on' == $tablefield->Field) { $settings_update_on = true; } if ('last_activity' == $tablefield->Field) { $last_activity = true; } } if (!$parent_import_id) { $wpdb->query("ALTER TABLE {$table} ADD `parent_import_id` BIGINT(20) NOT NULL DEFAULT 0;"); } if (!$iteration) { $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;"); } if (!$deleted) { $wpdb->query("ALTER TABLE {$table} ADD `deleted` BIGINT(20) NOT NULL DEFAULT 0;"); } if (!$executing) { $wpdb->query("ALTER TABLE {$table} ADD `executing` BOOL NOT NULL DEFAULT 0;"); } if (!$canceled) { $wpdb->query("ALTER TABLE {$table} ADD `canceled` BOOL NOT NULL DEFAULT 0;"); } if (!$canceled_on) { $wpdb->query("ALTER TABLE {$table} ADD `canceled_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); } if (!$failed) { $wpdb->query("ALTER TABLE {$table} ADD `failed` BOOL NOT NULL DEFAULT 0;"); } if (!$failed_on) { $wpdb->query("ALTER TABLE {$table} ADD `failed_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); } if (!$settings_update_on) { $wpdb->query("ALTER TABLE {$table} ADD `settings_update_on` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); } if (!$last_activity) { $wpdb->query("ALTER TABLE {$table} ADD `last_activity` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';"); } $table = $this->getTablePrefix() . 'posts'; $tablefields = $wpdb->get_results("DESCRIBE {$table};"); $iteration = false; // Check if field exists foreach ($tablefields as $tablefield) { if ('iteration' == $tablefield->Field) { $iteration = true; } } if (!$iteration) { $wpdb->query("ALTER TABLE {$table} ADD `iteration` BIGINT(20) NOT NULL DEFAULT 0;"); } if (!empty($wpdb->charset)) { $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; } if (!empty($wpdb->collate)) { $charset_collate .= " COLLATE {$wpdb->collate}"; } $table_prefix = $this->getTablePrefix(); $wpdb->query("CREATE TABLE IF NOT EXISTS {$table_prefix}history (\n\t\t\t\tid BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n\t\t\t\timport_id BIGINT(20) UNSIGNED NOT NULL,\n\t\t\t\ttype ENUM('manual','processing','trigger','continue','') NOT NULL DEFAULT '',\t\n\t\t\t\ttime_run TEXT,\t\n\t\t\t\tdate DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',\t\t\n\t\t\t\tsummary TEXT,\n\t\t\t\tPRIMARY KEY (id)\n\t\t\t) {$charset_collate};"); }