Import_categories

GQ class.

Defined (2)

The class is defined in the following location(s).

/includes/class-import-categories.php  
  1. class Import_categories{ 
  2. /** 
  3. * Plugin version, used for cache-busting of style and script file references. 
  4. * @since 1.0.0 
  5. * @var string 
  6. */ 
  7. protected $version = "1.0.0"; 
  8.  
  9. /** 
  10. * Unique identifier for your plugin. 
  11. * Use this value (not the variable name) as the text domain when internationalizing strings of text. It should 
  12. * match the Text Domain file header in the main plugin file. 
  13. * @since 1.0.0 
  14. * @var string 
  15. */ 
  16. protected $plugin_slug = "gq"; 
  17.  
  18. /** 
  19. * Instance of this class. 
  20. * @since 1.0.0 
  21. * @var object 
  22. */ 
  23. protected static $instance = null; 
  24.  
  25. /** 
  26. * Slug of the plugin screen. 
  27. * @since 1.0.0 
  28. * @var string 
  29. */ 
  30. protected $plugin_screen_hook_suffix = null; 
  31.  
  32. /** 
  33. * Initialize the plugin by setting localization, filters, and administration functions. 
  34. * @since 1.0.0 
  35. */ 
  36. private function __construct() { 
  37. // Add the options page and menu item. 
  38. add_action("admin_menu", array($this, "add_plugin_admin_menu")); 
  39. //call register settings function 
  40. add_action( 'admin_init', array($this, 'register_mysettings') ); 
  41. /** 
  42. * Return an instance of this class. 
  43. * @since 1.0.0 
  44. * @return object A single instance of this class. 
  45. */ 
  46. public static function get_instance() { 
  47.  
  48. // If the single instance hasn"t been set, set it now. 
  49. if (null == self::$instance) { 
  50. self::$instance = new self; 
  51.  
  52. return self::$instance; 
  53. /** 
  54. * Register the administration menu for this plugin into the WordPress Dashboard menu. 
  55. * @since 1.0.0 
  56. */ 
  57. public function add_plugin_admin_menu() { 
  58. $this->plugin_screen_hook_suffix = add_management_page(__("GQ - Administration", $this->plugin_slug), __("GQ Importer", $this->plugin_slug), "read", $this->plugin_slug, array($this, "display_plugin_admin_page")); 
  59.  
  60.  
  61. /** 
  62. * Render the settings page for this plugin. 
  63. * @since 1.0.0 
  64. */ 
  65. public function display_plugin_admin_page() { 
  66. include_once("views/admin.php"); 
  67. /** 
  68. *  
  69. */ 
  70. public function parse_csv_file($csvfile) { 
  71. $csv = Array(); 
  72. $rowcount = 0; 
  73. if (($handle = fopen($csvfile, "r")) !== FALSE) { 
  74. $max_line_length = defined('MAX_LINE_LENGTH') ? MAX_LINE_LENGTH : 10000; 
  75. $header = fgetcsv($handle, $max_line_length); 
  76. $header_colcount = count($header); 
  77. while (($row = fgetcsv($handle, $max_line_length)) !== FALSE) { 
  78. $row_colcount = count($row); 
  79. if ($row_colcount == $header_colcount) { 
  80. $entry = array_combine($header, $row); 
  81. $csv[] = $entry; 
  82. else { 
  83. error_log("csvreader: Invalid number of columns at line " . ($rowcount + 2) . " (row " . ($rowcount + 1) . "). Expected=$header_colcount Got=$row_colcount"); 
  84. return null; 
  85. $rowcount++; 
  86. //echo "Totally $rowcount rows found\n"; 
  87. fclose($handle); 
  88. else { 
  89. error_log("csvreader: Could not read CSV \"$csvfile\""); 
  90. return null; 
  91. return $csv; 
  92. /** 
  93. *  
  94. */ 
  95. public function register_mysettings() { 
  96. //register our settings 
  97. register_setting( 'baw-settings-group', 'new_option_name' ); 
  98. register_setting( 'baw-settings-group', 'some_other_option' ); 
  99. register_setting( 'baw-settings-group', 'option_etc' ); 
  100. /** 
  101. * Public import function 
  102. */ 
  103. public function import_function() { 
  104. global $wpdb; 
  105. set_time_limit(800); 
  106. $path = plugin_dir_path( __FILE__ ) . "import/cats.csv"; 
  107.  
  108. $lines = file($path); 
  109.  
  110. $csvarray = $this->parse_csv_file( $path ); 
  111. foreach ($csvarray as $key => $value) { 
  112. $name = $value['name']; 
  113. $slug = $value['slug']; 
  114. $description = $value["description"]; 
  115. $parent_slug = $value['parent_slug']; 
  116. $display_type = $value['display_type']; 
  117. $term_id = $value['id']; 
  118. if($parent_slug == "0") { 
  119.  
  120. $catarr = array( 
  121. // 'cat_ID' => intval( $term_id ),  
  122. 'cat_name' => $name,  
  123. 'category_description' => $description,  
  124. 'category_nicename' => $slug,  
  125. 'category_parent' => intval( $parent_slug ),  
  126. 'taxonomy' => 'product_cat' ); 
  127.  
  128. $new_tax = wp_insert_category( $catarr, true ); 
  129. }else{ 
  130.  
  131. $parent_term = term_exists( $parent_slug, 'product_cat' ); 
  132. $parent_term_id = $parent_term['term_id']; 
  133. $catarr = array( 
  134. // 'cat_ID' => intval( $term_id ),  
  135. 'cat_name' => $name,  
  136. 'category_description' => $description,  
  137. 'category_nicename' => $slug,  
  138. 'category_parent' => $parent_term_id,  
  139. 'taxonomy' => 'product_cat' ); 
  140.  
  141. $new_tax = wp_insert_category( $catarr, true ); 
  142.  
  143. if ( ! is_wp_error( $new_tax ) ) { 
  144.  
  145. $taxid = $new_tax["term_id"]; 
  146.  
  147. if($description != '') { 
  148. $src_preparsed = $this->get_img_url($description); 
  149. $src_clean = str_replace("%%GLOBAL_ShopPath%%", "YOUR URL HERE!", $src_preparsed); 
  150.  
  151. $thumbnail = media_sideload_image( $src_clean, $new_tax ); 
  152.  
  153. if( ! is_wp_error( $thumbnail ) ) { 
  154.  
  155. $thumbnail_link = $this->get_img_url($thumbnail); 
  156. $thumbnail_id = intval( $this->pn_get_attachment_id_from_url($thumbnail_link) ); 
  157.  
  158.  
  159. $wpdb->insert("wp_woocommerce_termmeta", array( 
  160. "woocommerce_term_id" => $new_tax,  
  161. "meta_key" => "thumbnail_id",  
  162. "meta_value" => $thumbnail_id 
  163. );  
  164. }else{ 
  165. echo "<p>Image error</p>"; 
  166. echo $thumbnail->get_error_message(); 
  167. $wpdb->insert("wp_woocommerce_termmeta", array( 
  168. "woocommerce_term_id" => $new_tax,  
  169. "meta_key" => "display_type",  
  170. "meta_value" => $display_type 
  171. ); 
  172. echo "<h4>Imported: ". $value['name'] . "</h4>"; 
  173. }else{ 
  174. // Trouble in Paradise: 
  175. echo $new_tax->get_error_message(); 
  176.  
  177.  
  178.  
  179. public function get_img_url($html) { 
  180. $doc = new DOMDocument(); 
  181. $doc->loadHTML($html); 
  182. $xpath = new DOMXPath($doc); 
  183. $src_preparsed = $xpath->evaluate("string(//img/@src)"); # "/images/image.jpg" 
  184. return $src_preparsed; 
  185. /** 
  186. *  
  187. */ 
  188. public function pn_get_attachment_id_from_url( $attachment_url = '' ) { 
  189.  
  190. global $wpdb; 
  191. $attachment_id = false; 
  192.  
  193. // If there is no url, return. 
  194. if ( '' == $attachment_url ) 
  195. return; 
  196.  
  197. // Get the upload directory paths 
  198. $upload_dir_paths = wp_upload_dir(); 
  199.  
  200. // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image 
  201. if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) { 
  202.  
  203. // If this is the URL of an auto-generated thumbnail, get the URL of the original image 
  204. $attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url ); 
  205.  
  206. // Remove the upload path base directory from the attachment URL 
  207. $attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url ); 
  208.  
  209. // Finally, run a custom database query to get the attachment ID from the modified attachment URL 
  210. $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) ); 
  211.  
  212.  
  213. return $attachment_id; 
  214.  
  215.  
/trunk/includes/class-import-categories.php  
  1. class Import_categories{ 
  2. /** 
  3. * Plugin version, used for cache-busting of style and script file references. 
  4. * @since 1.0.0 
  5. * @var string 
  6. */ 
  7. protected $version = "1.0.0"; 
  8.  
  9. /** 
  10. * Unique identifier for your plugin. 
  11. * Use this value (not the variable name) as the text domain when internationalizing strings of text. It should 
  12. * match the Text Domain file header in the main plugin file. 
  13. * @since 1.0.0 
  14. * @var string 
  15. */ 
  16. protected $plugin_slug = "gq"; 
  17.  
  18. /** 
  19. * Instance of this class. 
  20. * @since 1.0.0 
  21. * @var object 
  22. */ 
  23. protected static $instance = null; 
  24.  
  25. /** 
  26. * Slug of the plugin screen. 
  27. * @since 1.0.0 
  28. * @var string 
  29. */ 
  30. protected $plugin_screen_hook_suffix = null; 
  31.  
  32. /** 
  33. * Initialize the plugin by setting localization, filters, and administration functions. 
  34. * @since 1.0.0 
  35. */ 
  36. private function __construct() { 
  37. // Add the options page and menu item. 
  38. add_action("admin_menu", array($this, "add_plugin_admin_menu")); 
  39. //call register settings function 
  40. add_action( 'admin_init', array($this, 'register_mysettings') ); 
  41. /** 
  42. * Return an instance of this class. 
  43. * @since 1.0.0 
  44. * @return object A single instance of this class. 
  45. */ 
  46. public static function get_instance() { 
  47.  
  48. // If the single instance hasn"t been set, set it now. 
  49. if (null == self::$instance) { 
  50. self::$instance = new self; 
  51.  
  52. return self::$instance; 
  53. /** 
  54. * Register the administration menu for this plugin into the WordPress Dashboard menu. 
  55. * @since 1.0.0 
  56. */ 
  57. public function add_plugin_admin_menu() { 
  58. $this->plugin_screen_hook_suffix = add_management_page(__("GQ - Administration", $this->plugin_slug), __("GQ Importer", $this->plugin_slug), "read", $this->plugin_slug, array($this, "display_plugin_admin_page")); 
  59.  
  60.  
  61. /** 
  62. * Render the settings page for this plugin. 
  63. * @since 1.0.0 
  64. */ 
  65. public function display_plugin_admin_page() { 
  66. include_once("views/admin.php"); 
  67. /** 
  68. *  
  69. */ 
  70. public function parse_csv_file($csvfile) { 
  71. $csv = Array(); 
  72. $rowcount = 0; 
  73. if (($handle = fopen($csvfile, "r")) !== FALSE) { 
  74. $max_line_length = defined('MAX_LINE_LENGTH') ? MAX_LINE_LENGTH : 10000; 
  75. $header = fgetcsv($handle, $max_line_length); 
  76. $header_colcount = count($header); 
  77. while (($row = fgetcsv($handle, $max_line_length)) !== FALSE) { 
  78. $row_colcount = count($row); 
  79. if ($row_colcount == $header_colcount) { 
  80. $entry = array_combine($header, $row); 
  81. $csv[] = $entry; 
  82. else { 
  83. error_log("csvreader: Invalid number of columns at line " . ($rowcount + 2) . " (row " . ($rowcount + 1) . "). Expected=$header_colcount Got=$row_colcount"); 
  84. return null; 
  85. $rowcount++; 
  86. //echo "Totally $rowcount rows found\n"; 
  87. fclose($handle); 
  88. else { 
  89. error_log("csvreader: Could not read CSV \"$csvfile\""); 
  90. return null; 
  91. return $csv; 
  92. /** 
  93. *  
  94. */ 
  95. public function register_mysettings() { 
  96. //register our settings 
  97. register_setting( 'baw-settings-group', 'new_option_name' ); 
  98. register_setting( 'baw-settings-group', 'some_other_option' ); 
  99. register_setting( 'baw-settings-group', 'option_etc' ); 
  100. /** 
  101. * Public import function 
  102. */ 
  103. public function import_function() { 
  104. global $wpdb; 
  105. set_time_limit(800); 
  106. $path = plugin_dir_path( __FILE__ ) . "import/cats.csv"; 
  107.  
  108. $lines = file($path); 
  109.  
  110. $csvarray = $this->parse_csv_file( $path ); 
  111. foreach ($csvarray as $key => $value) { 
  112. $name = $value['name']; 
  113. $slug = $value['slug']; 
  114. $description = $value["description"]; 
  115. $parent_slug = $value['parent_slug']; 
  116. $display_type = $value['display_type']; 
  117. $term_id = $value['id']; 
  118. if($parent_slug == "0") { 
  119.  
  120. $catarr = array( 
  121. // 'cat_ID' => intval( $term_id ),  
  122. 'cat_name' => $name,  
  123. 'category_description' => $description,  
  124. 'category_nicename' => $slug,  
  125. 'category_parent' => intval( $parent_slug ),  
  126. 'taxonomy' => 'product_cat' ); 
  127.  
  128. $new_tax = wp_insert_category( $catarr, true ); 
  129. }else{ 
  130.  
  131. $parent_term = term_exists( $parent_slug, 'product_cat' ); 
  132. $parent_term_id = $parent_term['term_id']; 
  133. $catarr = array( 
  134. // 'cat_ID' => intval( $term_id ),  
  135. 'cat_name' => $name,  
  136. 'category_description' => $description,  
  137. 'category_nicename' => $slug,  
  138. 'category_parent' => $parent_term_id,  
  139. 'taxonomy' => 'product_cat' ); 
  140.  
  141. $new_tax = wp_insert_category( $catarr, true ); 
  142.  
  143. if ( ! is_wp_error( $new_tax ) ) { 
  144.  
  145. $taxid = $new_tax["term_id"]; 
  146.  
  147. if($description != '') { 
  148. $src_preparsed = $this->get_img_url($description); 
  149. $src_clean = str_replace("%%GLOBAL_ShopPath%%", "YOUR URL HERE!", $src_preparsed); 
  150.  
  151. $thumbnail = media_sideload_image( $src_clean, $new_tax ); 
  152.  
  153. if( ! is_wp_error( $thumbnail ) ) { 
  154.  
  155. $thumbnail_link = $this->get_img_url($thumbnail); 
  156. $thumbnail_id = intval( $this->pn_get_attachment_id_from_url($thumbnail_link) ); 
  157.  
  158.  
  159. $wpdb->insert("wp_woocommerce_termmeta", array( 
  160. "woocommerce_term_id" => $new_tax,  
  161. "meta_key" => "thumbnail_id",  
  162. "meta_value" => $thumbnail_id 
  163. );  
  164. }else{ 
  165. echo "<p>Image error</p>"; 
  166. echo $thumbnail->get_error_message(); 
  167. $wpdb->insert("wp_woocommerce_termmeta", array( 
  168. "woocommerce_term_id" => $new_tax,  
  169. "meta_key" => "display_type",  
  170. "meta_value" => $display_type 
  171. ); 
  172. echo "<h4>Imported: ". $value['name'] . "</h4>"; 
  173. }else{ 
  174. // Trouble in Paradise: 
  175. echo $new_tax->get_error_message(); 
  176.  
  177.  
  178.  
  179. public function get_img_url($html) { 
  180. $doc = new DOMDocument(); 
  181. $doc->loadHTML($html); 
  182. $xpath = new DOMXPath($doc); 
  183. $src_preparsed = $xpath->evaluate("string(//img/@src)"); # "/images/image.jpg" 
  184. return $src_preparsed; 
  185. /** 
  186. *  
  187. */ 
  188. public function pn_get_attachment_id_from_url( $attachment_url = '' ) { 
  189.  
  190. global $wpdb; 
  191. $attachment_id = false; 
  192.  
  193. // If there is no url, return. 
  194. if ( '' == $attachment_url ) 
  195. return; 
  196.  
  197. // Get the upload directory paths 
  198. $upload_dir_paths = wp_upload_dir(); 
  199.  
  200. // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image 
  201. if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) { 
  202.  
  203. // If this is the URL of an auto-generated thumbnail, get the URL of the original image 
  204. $attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url ); 
  205.  
  206. // Remove the upload path base directory from the attachment URL 
  207. $attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url ); 
  208.  
  209. // Finally, run a custom database query to get the attachment ID from the modified attachment URL 
  210. $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) ); 
  211.  
  212.  
  213. return $attachment_id; 
  214.  
  215.