'Attributes', 'hierarchical' => true, 'public' => false, 'query_var' => false, 'rewrite' => false, ); $post_types = apply_filters( 'ic_attributes_register_post_types', product_post_type_array() ); register_taxonomy( 'al_product-attributes', $post_types, $args ); } /** * Adds product attribute label and returns attribute label ID * * @param type $label * @return type */ function ic_add_product_attribute_label( $label ) { if ( is_array( $label ) ) { foreach ( $label as $single_label ) { $term_id = ic_add_product_attribute_label( $single_label ); } return $term_id; } else { $term_id = get_attribute_label_id( $label ); if ( !empty( $term_id ) ) { return $term_id; } $term = wp_insert_term( $label, 'al_product-attributes' ); if ( !is_wp_error( $term ) ) { return $term[ 'term_id' ]; } } return ''; } /** * Adds product attribute value and returns attribute value ID * * @param type $label_id * @param type $value * @return type */ function ic_add_product_attribute_value( $label_id, $value ) { if ( empty( $label_id ) ) { return ''; } if ( is_array( $value ) ) { foreach ( $value as $current_value ) { $term_id = ic_add_product_attribute_value( $label_id, $current_value ); } } else { $term_id = get_attribute_value_id( $label_id, $value ); if ( empty( $term_id ) ) { $term = wp_insert_term( $value, 'al_product-attributes', array( 'parent' => $label_id ) ); if ( !is_wp_error( $term ) ) { $term_id = $term[ 'term_id' ]; } } } return $term_id; } add_filter( 'product_meta_save', 'ic_assign_product_attributes', 2, 2 ); /** * Adds product attributes to the database * * @param type $product_meta * @param type $post * @return type */ function ic_assign_product_attributes( $product_meta, $post, $clear_empty = true ) { $max_attr = product_attributes_number(); if ( $max_attr > 0 ) { $product_id = isset( $post->ID ) ? $post->ID : $post; $attr_ids = array(); for ( $i = 1; $i <= $max_attr; $i++ ) { if ( empty( $product_meta[ '_attribute' . $i ] ) || (isset( $product_meta[ '_attribute' . $i ][ 0 ] ) && empty( $product_meta[ '_attribute' . $i ][ 0 ] )) ) { continue; } if ( !empty( $product_meta[ '_attribute-label' . $i ] ) ) { //$label = is_array( $product_meta[ '_attribute-label' . $i ] ) ? ic_sanitize_product_attribute( $product_meta[ '_attribute-label' . $i ][ 0 ] ) : ic_sanitize_product_attribute( $product_meta[ '_attribute-label' . $i ] ); $label = ic_sanitize_product_attribute( $product_meta[ '_attribute-label' . $i ] ); if ( !empty( $label ) ) { //$value = is_array( $product_meta[ '_attribute' . $i ] ) ? ic_sanitize_product_attribute( $product_meta[ '_attribute' . $i ][ 0 ] ) : ic_sanitize_product_attribute( $product_meta[ '_attribute' . $i ] ); $value = ic_sanitize_product_attribute( $product_meta[ '_attribute' . $i ] ); if ( !empty( $value ) ) { $label_id = ic_add_product_attribute_label( $label ); if ( !empty( $label_id ) ) { $attr_ids[] = $label_id; if ( !is_array( $value ) ) { $value = array( $value ); } foreach ( $value as $val ) { $value_id = ic_add_product_attribute_value( $label_id, $val ); $attr_ids[] = $value_id; } } } } } } if ( !empty( $attr_ids ) ) { $attr_ids = array_unique( array_map( 'intval', $attr_ids ) ); wp_set_object_terms( $product_id, $attr_ids, 'al_product-attributes' ); if ( $clear_empty ) { ic_clear_empty_attributes(); } } } return $product_meta; } add_action( 'ic_scheduled_attributes_clear', 'ic_clear_empty_attributes' ); /** * Clears empty product attributes * */ function ic_clear_empty_attributes() { $max_attr = product_attributes_number(); $attributes = get_terms( 'al_product-attributes', array( 'orderby' => 'count', 'hide_empty' => 0, 'number' => $max_attr ) ); $schedule = false; if ( !empty( $attributes ) && is_array( $attributes ) && !is_wp_error( $attributes ) ) { foreach ( $attributes as $attribute ) { if ( $attribute->count == 0 ) { $schedule = true; wp_delete_term( $attribute->term_id, 'al_product-attributes' ); } else { $schedule = false; break; } } if ( /* !wp_get_schedule( 'ic_scheduled_attributes_clear' ) && */ $schedule ) { //wp_schedule_event( time(), 'hourly', 'ic_scheduled_attributes_clear' ); wp_schedule_single_event( time(), 'ic_scheduled_attributes_clear' ); } else { wp_clear_scheduled_hook( 'ic_scheduled_attributes_clear' ); } } else { wp_clear_scheduled_hook( 'ic_scheduled_attributes_clear' ); } } add_action( 'ic_scheduled_attributes_assignment', 'ic_reassign_all_products_attributes' ); /** * Scheduled even to reassign all products attributes * * @return string */ function ic_reassign_all_products_attributes() { $max_attr = product_attributes_number(); if ( empty( $max_attr ) ) { return; } $done = get_option( 'ic_product_upgrade_done', 0 ); if ( empty( $done ) ) { update_option( 'ic_product_upgrade_done', -1 ); wp_schedule_single_event( time(), 'ic_scheduled_attributes_assignment' ); return ''; } if ( $done < 0 ) { $done = 0; } $products = get_all_catalog_products( 'date', 'ASC', 200, $done ); $max_round = intval( 300 / $max_attr ); if ( $max_round > 100 ) { $max_round = 100; } if ( $done > 100 ) { $max_round = apply_filters( 'ic_database_upgrade_max_round', $max_round * 2 ); } $rounds = 1; foreach ( $products as $post ) { if ( $rounds > $max_round ) { break; } set_time_limit( 30 ); $product_meta = get_post_meta( $post->ID ); ic_assign_product_attributes( $product_meta, $post, false ); $done++; $rounds++; } $products_count = ic_products_count(); if ( $products_count > $done ) { update_option( 'ic_product_upgrade_done', $done ); wp_schedule_single_event( time(), 'ic_scheduled_attributes_assignment' ); } else { delete_option( 'ic_product_upgrade_done' ); wp_clear_scheduled_hook( 'ic_scheduled_attributes_assignment' ); ic_clear_empty_attributes(); } } add_action( 'ic_system_tools', 'ic_system_tools_attributes_upgrade' ); /** * Shows database upgrade button in system tools * */ function ic_system_tools_attributes_upgrade() { $done = get_option( 'ic_product_upgrade_done', 0 ); if ( !empty( $done ) || isset( $_GET[ 'reassign_all_products_attributes' ] ) ) { if ( empty( $done ) && isset( $_GET[ 'reassign_all_products_attributes' ] ) ) { ic_reassign_all_products_attributes(); } echo '
' . $done . ' Items Done! Another round needed.
'; } echo '