WC_Compare_Install

WooCommerce Compare Install Class.

Defined (1)

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

/includes/class-wc-compare-install.php  
  1. class WC_Compare_Install 
  2. /** 
  3. * Init. 
  4. */ 
  5. public function __construct() { 
  6.  
  7. add_action( 'admin_init', array( $this, 'automatic_add_compare_categories' ) ); 
  8. add_action( 'admin_init', array( $this, 'automatic_add_features' ) ); 
  9. add_action( 'admin_init', array( $this, 'add_meta_all_products' ) ); 
  10. add_action( 'admin_init', array( $this, 'automatic_add_widget_to_sidebar' ) ); 
  11.  
  12. /** 
  13. * Automatic Set all Product Categories as Compare Categories 
  14. */ 
  15. public function automatic_add_compare_categories() { 
  16. $terms = get_terms("product_cat", array('hide_empty' => 0)); 
  17. if ( count($terms) > 0 ) { 
  18. foreach ($terms as $category_product) { 
  19. $check_existed = WC_Compare_Categories_Data::get_count("category_name='".trim(addslashes($category_product->name))."'"); 
  20. if ($check_existed < 1 ) { 
  21. WC_Compare_Categories_Data::insert_row(array('category_name' => trim(addslashes($category_product->name)))); 
  22.  
  23. /** 
  24. * Automatic Set all Product Attribute as Compare Features 
  25. */ 
  26. public function automatic_add_features() { 
  27. $current_db_version = get_option( 'woocommerce_db_version', null ); 
  28. if ( version_compare( $current_db_version, '2.1.0', '<' ) && null !== $current_db_version ) { 
  29. global $woocommerce; 
  30. $top_variations = $woocommerce->get_attribute_taxonomies(); 
  31. } else { 
  32. $top_variations = wc_get_attribute_taxonomies(); 
  33. if ( $top_variations ) { 
  34. foreach ($top_variations as $top_variation) { 
  35. $check_existed = WC_Compare_Data::get_count("field_name='".trim(addslashes($top_variation->attribute_label))."'"); 
  36. if ($check_existed < 1 ) { 
  37. $child_variations = get_terms( ( ( version_compare( $current_db_version, '2.1.0', '<' ) && null !== $current_db_version ) ? $woocommerce->attribute_taxonomy_name($top_variation->attribute_name) : wc_attribute_taxonomy_name($top_variation->attribute_name) ) , array('parent' => 0, 'hide_empty' => 0, 'hierarchical' => 0) ); 
  38. $default_value = ''; 
  39. if ( count($child_variations) > 0 ) { 
  40. $line = ''; 
  41. foreach ($child_variations as $child_variation) { 
  42. $default_value .= $line.addslashes($child_variation->name); 
  43. $line = ' 
  44. '; 
  45. if ( trim($default_value) != '') 
  46. $feature_id = WC_Compare_Data::insert_row(array('field_name' => trim(addslashes($top_variation->attribute_label)), 'field_type' => 'checkbox', 'field_unit' => '', 'default_value' => $default_value) ); 
  47. else 
  48. $feature_id = WC_Compare_Data::insert_row(array('field_name' => trim(addslashes($top_variation->attribute_label)), 'field_type' => 'input-text', 'field_unit' => '', 'default_value' => '') ); 
  49.  
  50. public function add_meta_all_products() { 
  51.  
  52. // Add deactivate compare feature meta for all products when activate this plugin 
  53. $have_deactivate_meta = get_posts(array('numberposts' => -1, 'post_type' => array('product', 'product_variation'), 'post_status' => array('publish', 'private'), 'meta_key' => '_woo_deactivate_compare_feature')); 
  54. $have_ids = array(); 
  55. if (is_array($have_deactivate_meta) && count($have_deactivate_meta) > 0) { 
  56. foreach ($have_deactivate_meta as $product) { 
  57. $have_ids[] = $product->ID; 
  58. if (is_array($have_ids) && count($have_ids) > 0) { 
  59. $no_deactivate_meta = get_posts(array('numberposts' => -1, 'post_type' => array('product', 'product_variation'), 'post_status' => array('publish', 'private'), 'post__not_in' => $have_ids)); 
  60. }else { 
  61. $no_deactivate_meta = get_posts(array('numberposts' => -1, 'post_type' => array('product', 'product_variation'), 'post_status' => array('publish', 'private'))); 
  62. if (is_array($no_deactivate_meta) && count($no_deactivate_meta) > 0) { 
  63. foreach ($no_deactivate_meta as $product) { 
  64. add_post_meta($product->ID, '_woo_deactivate_compare_feature', ''); 
  65.  
  66. // Add deactivate compare feature meta for all products when activate this plugin 
  67. $have_compare_category_meta = get_posts(array('numberposts' => -1, 'post_type' => array('product', 'product_variation'), 'post_status' => array('publish', 'private'), 'meta_key' => '_woo_compare_category_name')); 
  68. $have_ids = array(); 
  69. if (is_array($have_compare_category_meta) && count($have_compare_category_meta) > 0) { 
  70. foreach ($have_compare_category_meta as $product) { 
  71. $have_ids[] = $product->ID; 
  72. if (is_array($have_ids) && count($have_ids) > 0) { 
  73. $no_compare_category_meta = get_posts(array('numberposts' => -1, 'post_type' => array('product', 'product_variation'), 'post_status' => array('publish', 'private'), 'post__not_in' => $have_ids)); 
  74. }else { 
  75. $no_compare_category_meta = get_posts(array('numberposts' => -1, 'post_type' => array('product', 'product_variation'), 'post_status' => array('publish', 'private'))); 
  76. if (is_array($no_compare_category_meta) && count($no_compare_category_meta) > 0) { 
  77. foreach ($no_compare_category_meta as $product) { 
  78. add_post_meta($product->ID, '_woo_compare_category_name', ''); 
  79.  
  80. // Add compare category name into product have compare category id 
  81. $have_compare_category_id_meta = get_posts(array('numberposts' => -1, 'post_type' => array('product', 'product_variation'), 'post_status' => array('publish', 'private'), 'meta_key' => '_woo_compare_category')); 
  82. if (is_array($have_compare_category_id_meta) && count($have_compare_category_id_meta) > 0) { 
  83. foreach ($have_compare_category_id_meta as $product) { 
  84. $compare_category = get_post_meta( $product->ID, '_woo_compare_category', true ); 
  85. $category_data = WC_Compare_Categories_Data::get_row($compare_category); 
  86. @update_post_meta($product->ID, '_woo_compare_category_name', stripslashes($category_data->category_name)); 
  87.  
  88.  
  89. public function automatic_add_widget_to_sidebar() { 
  90. $add_to_sidebars = array('primary', 'primary-widget-area', 'sidebar-1'); 
  91. $widget_name = 'woo_compare_widget'; 
  92. $sidebar_options = get_option('sidebars_widgets'); 
  93. $compare_widget = get_option('widget_'.$widget_name); 
  94. $have_widget = false; 
  95. foreach ($sidebar_options as $siderbar_name => $sidebar_widgets) { 
  96. if ($siderbar_name == 'wp_inactive_widgets') continue; 
  97. if (is_array($sidebar_widgets) && count($sidebar_widgets) > 0) { 
  98. foreach ($sidebar_widgets as $sidebar_widget) { 
  99. if (stristr($sidebar_widget, $widget_name) !== false) { 
  100. $have_widget = true; 
  101. break; 
  102. if ($have_widget) break; 
  103. if (!$have_widget) { 
  104. if (!is_array($compare_widget)) $compare_widget = array(); 
  105. $count = count($compare_widget)+1; 
  106. $added_widget = false; 
  107. $new_sidebar_options = $sidebar_options; 
  108. foreach ($add_to_sidebars as $sidebar_name) { 
  109. if (isset($sidebar_options[$sidebar_name])) { 
  110. $sidebar_options[$sidebar_name][] = $widget_name.'-'.$count; 
  111. $added_widget = true; 
  112. break; 
  113. if (!$added_widget) { 
  114. foreach ($new_sidebar_options as $siderbar_name => $sidebar_widgets) { 
  115. if ($siderbar_name == 'wp_inactive_widgets') continue; 
  116. $sidebar_options[$siderbar_name][] = $widget_name.'-'.$count; 
  117. break; 
  118.  
  119. // add widget to sidebar: 
  120. $compare_widget[$count] = array( 
  121. 'title' => '' 
  122. ); 
  123. update_option('sidebars_widgets', $sidebar_options); 
  124. update_option('widget_'.$widget_name, $compare_widget); 
  125.