protected function init_trigger() { if ($this->woo_ready == false) { return; } global $woocommerce, $_wd_msg; $_color = 'color'; $attribute_name = isset($_color) ? woocommerce_sanitize_taxonomy_name(stripslashes((string) $_color)) : ''; $attribute_name = wc_attribute_taxonomy_name($attribute_name); $attribute_name_array = wc_get_attribute_taxonomy_names(); //$taxonomy_exists = taxonomy_exists( wc_attribute_taxonomy_name( $attribute_name ) ); $taxonomy_exists = in_array($attribute_name, $attribute_name_array); /**************** Check if attribute available ****************/ $this->tax_slug = ''; if (!$taxonomy_exists) { $this->color_ready = false; $_wd_msg = "<strong>Color attribute is not exist.</strong>.Go to Products => Attributes,create new attibute with slug <strong>color</strong>"; add_action('admin_notices', array($this, 'show_msg')); } else { $this->color_ready = true; add_image_size('wd_pc_thumb', 30, 30, true); $_tax_slug = wc_attribute_taxonomy_name($attribute_name); $this->tax_slug = $_tax_slug; $this->init_script(); $this->init_handle(); } }
function translated_attribute_label($label, $name) { global $sitepress; if (is_admin() && !wpml_is_ajax()) { global $wpdb, $sitepress_settings; if ($sitepress_settings['admin_default_language'] == $sitepress_settings['st']['strings_language']) { return $label; } else { $string_id = icl_get_string_id('taxonomy singular name: ' . $label, 'WordPress'); if ($string_id) { $string = $wpdb->get_var($wpdb->prepare("SELECT value FROM {$wpdb->prefix}icl_string_translations WHERE string_id = %s and language = %s", $string_id, $sitepress_settings['admin_default_language'])); if ($string) { return $string; } } } } $name = woocommerce_sanitize_taxonomy_name($name); $lang = $sitepress->get_current_language(); $trnsl_labels = get_option('wcml_custom_attr_translations'); if (isset($trnsl_labels[$lang][$name])) { return $trnsl_labels[$lang][$name]; } return icl_t('WordPress', 'taxonomy singular name: ' . $label, $label); }
/** * Get a product attributes label. * * @access public * @param mixed $name * @return string */ public function attribute_label($name) { global $wpdb; if (taxonomy_is_product_attribute($name)) { $name = woocommerce_sanitize_taxonomy_name(str_replace('pa_', '', $name)); $label = $wpdb->get_var($wpdb->prepare("SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name)); if (!$label) { $label = ucfirst($name); } } else { $label = $name; } return apply_filters('woocommerce_attribute_label', $label, $name); }
function attribute_label($name, $key = NULL) { global $wpdb; $label = false; $name = str_replace('attribute_', '', $name); if (taxonomy_is_product_attribute($name)) { $name = woocommerce_sanitize_taxonomy_name(str_replace('pa_', '', $name)); $label = $wpdb->get_var($wpdb->prepare("SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name)); if (!$label) { $label = ucfirst($name); } } else { if ($key) { $label = $wpdb->get_var("SELECT meta_key FROM " . $wpdb->prefix . "woocommerce_order_itemmeta WHERE meta_value = '{$key}' limit 1"); } if (!$label) { $label = ucfirst($name); } } return apply_filters('woocommerce_attribute_label', $label, $name); }
/** * Attributes admin panel * * Shows the created attributes and lets you add new ones or edit existing ones. * The added attributes are stored in the database and can be used for layered navigation. * * @access public * @return void */ function woocommerce_attributes() { global $wpdb, $woocommerce; // Action to perform: add, edit, delete or none $action = ''; if (!empty($_POST['add_new_attribute'])) { $action = 'add'; } elseif (!empty($_POST['save_attribute']) && !empty($_GET['edit'])) { $action = 'edit'; } elseif (!empty($_GET['delete'])) { $action = 'delete'; } // Add or edit an attribute if ('add' === $action || 'edit' === $action) { // Security check if ('add' === $action) { check_admin_referer('woocommerce-add-new_attribute'); } if ('edit' === $action) { $attribute_id = absint($_GET['edit']); check_admin_referer('woocommerce-save-attribute_' . $attribute_id); } // Grab the submitted data $attribute_label = isset($_POST['attribute_label']) ? (string) stripslashes($_POST['attribute_label']) : ''; $attribute_name = isset($_POST['attribute_name']) ? woocommerce_sanitize_taxonomy_name(stripslashes((string) $_POST['attribute_name'])) : ''; $attribute_type = isset($_POST['attribute_type']) ? (string) stripslashes($_POST['attribute_type']) : ''; $attribute_orderby = isset($_POST['attribute_orderby']) ? (string) stripslashes($_POST['attribute_orderby']) : ''; // Auto-generate the label or slug if only one of both was provided if (!$attribute_label) { $attribute_label = ucwords($attribute_name); } elseif (!$attribute_name) { $attribute_name = woocommerce_sanitize_taxonomy_name(stripslashes($attribute_label)); } // Forbidden attribute names // http://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms $reserved_terms = array('attachment', 'attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'cpage', 'day', 'debug', 'error', 'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name', 'nav_menu', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'type', 'w', 'withcomments', 'withoutcomments', 'year'); // Error checking if (!$attribute_name || !$attribute_name || !$attribute_type) { $error = __('Please, provide an attribute name, slug and type.', 'woocommerce'); } elseif (strlen($attribute_name) >= 28) { $error = sprintf(__('Slug “%s” is too long (28 characters max). Shorten it, please.', 'woocommerce'), sanitize_title($attribute_name)); } elseif (in_array($attribute_name, $reserved_terms)) { $error = sprintf(__('Slug “%s” is not allowed because it is a reserved term. Change it, please.', 'woocommerce'), sanitize_title($attribute_name)); } else { $taxonomy_exists = taxonomy_exists($woocommerce->attribute_taxonomy_name($attribute_name)); if ('add' === $action && $taxonomy_exists) { $error = sprintf(__('Slug “%s” is already in use. Change it, please.', 'woocommerce'), sanitize_title($attribute_name)); } if ('edit' === $action) { $old_attribute_name = $wpdb->get_var("SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = {$attribute_id}"); if ($old_attribute_name != $attribute_name && woocommerce_sanitize_taxonomy_name($old_attribute_name) != $attribute_name && $taxonomy_exists) { $error = sprintf(__('Slug “%s” is already in use. Change it, please.', 'woocommerce'), sanitize_title($attribute_name)); } } } // Show the error message if any if (!empty($error)) { echo '<div id="woocommerce_errors" class="error fade"><p>' . $error . '</p></div>'; } else { // Add new attribute if ('add' === $action) { $attribute = array('attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_orderby); $wpdb->insert($wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute); do_action('woocommerce_attribute_added', $wpdb->insert_id, $attribute); $action_completed = true; } // Edit existing attribute if ('edit' === $action) { $attribute = array('attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => $attribute_orderby); $wpdb->update($wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute, array('attribute_id' => $attribute_id)); do_action('woocommerce_attribute_updated', $attribute_id, $attribute, $old_attribute_name); if ($old_attribute_name != $attribute_name && !empty($old_attribute_name)) { // Update taxonomies in the wp term taxonomy table $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => $woocommerce->attribute_taxonomy_name($attribute_name)), array('taxonomy' => 'pa_' . $old_attribute_name)); // Update taxonomy ordering term meta $wpdb->update($wpdb->prefix . 'woocommerce_termmeta', array('meta_key' => 'order_pa_' . sanitize_title($attribute_name)), array('meta_key' => 'order_pa_' . sanitize_title($old_attribute_name))); // Update product attributes which use this taxonomy $old_attribute_name_length = strlen($old_attribute_name) + 3; $attribute_name_length = strlen($attribute_name) + 3; $wpdb->query("\r\n\t\t\t\t\t\tUPDATE {$wpdb->postmeta}\r\n\t\t\t\t\t\tSET meta_value = REPLACE( meta_value, 's:{$old_attribute_name_length}:\"pa_{$old_attribute_name}\"', 's:{$attribute_name_length}:\"pa_{$attribute_name}\"' )\r\n\t\t\t\t\t\tWHERE meta_key = '_product_attributes'"); // Update variations which use this taxonomy $wpdb->update($wpdb->postmeta, array('meta_key' => 'attribute_pa_' . sanitize_title($attribute_name)), array('meta_key' => 'attribute_pa_' . sanitize_title($old_attribute_name))); } $action_completed = true; } flush_rewrite_rules(); } } // Delete an attribute if ('delete' === $action) { // Security check $attribute_id = absint($_GET['delete']); check_admin_referer('woocommerce-delete-attribute_' . $attribute_id); $attribute_name = $wpdb->get_var("SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = {$attribute_id}"); if ($attribute_name && $wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = {$attribute_id}")) { $taxonomy = $woocommerce->attribute_taxonomy_name($attribute_name); if (taxonomy_exists($taxonomy)) { $terms = get_terms($taxonomy, 'orderby=name&hide_empty=0'); foreach ($terms as $term) { wp_delete_term($term->term_id, $taxonomy); } } do_action('woocommerce_attribute_deleted', $attribute_id, $attribute_name, $taxonomy); $action_completed = true; } } // If an attribute was added, edited or deleted: clear cache and redirect if (!empty($action_completed)) { delete_transient('wc_attribute_taxonomies'); wp_safe_redirect(get_admin_url() . 'edit.php?post_type=product&page=woocommerce_attributes'); exit; } // Show admin interface if (!empty($_GET['edit'])) { woocommerce_edit_attribute(); } else { woocommerce_add_attribute(); } }
function process_attributes() { global $wpdb, $woocommerce; $count = $wpdb->get_var("SELECT count(*) FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'wpsc-variation' AND parent = '0'"); if ($count) { $q = "\n\t\t\t\tSELECT t.term_id,t.name\n\t\t\t\tFROM {$wpdb->term_taxonomy} AS tt, {$wpdb->terms} AS t\n\t\t\t\tWHERE\n\t\t\t\t\ttt.taxonomy = 'wpsc-variation'\n\t\t\t\t\tAND tt.parent = '0'\n\t\t\t\t\tAND tt.term_id = t.term_id\n\t\t\t\t"; $attributes = $wpdb->get_results($q); if (!empty($attributes)) { $ids = array(); foreach ($attributes as $attribute) { $old_id = $attribute->term_id; $attribute_name = woocommerce_sanitize_taxonomy_name($attribute->name); $attribute_type = 'select'; $attribute_label = $attribute->name; if (function_exists('wc_attribute_taxonomy_name')) { $attribute_taxonomy_name = wc_attribute_taxonomy_name($attribute_name); } else { $attribute_taxonomy_name = $woocommerce->attribute_taxonomy_name($attribute_name); } if ($attribute_name && $attribute_type && !taxonomy_exists($attribute_taxonomy_name)) { $wpdb->insert($wpdb->prefix . "woocommerce_attribute_taxonomies", array('attribute_name' => $attribute_name, 'attribute_label' => $attribute_label, 'attribute_type' => $attribute_type), array('%s', '%s')); printf('<p>' . __('<b>%s</b> product attributes was converted', 'woo_wpec') . '</p>', $attribute_name); } else { printf('<p>' . __('<b>%s</b> product attributes does exist', 'woo_wpec') . '</p>', $attribute_name); } $attribute_terms = $wpdb->get_col("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'wpsc-variation' AND parent = '{$old_id}'"); if (!empty($attribute_terms)) { $attribute_terms = implode(', ', (array) $attribute_terms); $wpdb->query("UPDATE {$wpdb->term_taxonomy} SET taxonomy = '{$attribute_taxonomy_name}', parent = '0' WHERE term_id IN ({$attribute_terms})"); clean_term_cache($attribute_terms, $attribute_taxonomy_name, true); } $wpdb->query("DELETE FROM {$wpdb->term_taxonomy} WHERE term_id = '{$old_id}'"); $wpdb->query("DELETE FROM {$wpdb->terms} WHERE term_id = '{$old_id}'"); } } } }
public function add_attribute($post_id, $key, $value) { global $wpdb; global $woocommerce; $amazon_settings = $this->the_plugin->getAllSettings('array', 'amazon'); // get attribute name, label if (isset($amazon_settings['attr_title_normalize']) && $amazon_settings['attr_title_normalize'] == 'yes') { $attribute_label = $key; } else { $attribute_label = $key; } $attribute_name = woocommerce_sanitize_taxonomy_name($key); // set attribute type $attribute_type = 'select'; // check for duplicates $attribute_taxonomies = $wpdb->get_var("SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = '" . esc_sql($attribute_name) . "'"); if ($attribute_taxonomies) { // update existing attribute $wpdb->update($wpdb->prefix . 'woocommerce_attribute_taxonomies', array('attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => 'name'), array('attribute_name' => $attribute_name)); } else { // add new attribute $wpdb->insert($wpdb->prefix . 'woocommerce_attribute_taxonomies', array('attribute_label' => $attribute_label, 'attribute_name' => $attribute_name, 'attribute_type' => $attribute_type, 'attribute_orderby' => 'name')); } // avoid object to be inserted in terms if (is_object($value)) { return; } // add attribute values if not exist $taxonomy = wc_attribute_taxonomy_name($attribute_name); if (is_array($value)) { $values = $value; } else { $values = array($value); } // check taxonomy if (!taxonomy_exists($taxonomy)) { // add attribute value foreach ($values as $attribute_value) { $attribute_value = (string) $attribute_value; if (is_string($attribute_value)) { // add term $name = stripslashes($attribute_value); $slug = sanitize_title($name); if (!term_exists($name)) { if (trim($slug) != '' && trim($name) != '') { $this->the_plugin->db_custom_insert($wpdb->terms, array('values' => array('name' => $name, 'slug' => $slug), 'format' => array('%s', '%s')), true); /*$wpdb->insert( $wpdb->terms, array( 'name' => $name, 'slug' => $slug ) );*/ // add term taxonomy $term_id = $wpdb->insert_id; $this->the_plugin->db_custom_insert($wpdb->term_taxonomy, array('values' => array('term_id' => $term_id, 'taxonomy' => $taxonomy), 'format' => array('%d', '%s')), true); /*$wpdb->insert( $wpdb->term_taxonomy, array( 'term_id' => $term_id, 'taxonomy' => $taxonomy ) );*/ $term_taxonomy_id = $wpdb->insert_id; $__dbg = compact('taxonomy', 'attribute_value', 'term_id', 'term_taxonomy_id'); //var_dump('<pre>1: ',$__dbg,'</pre>'); } } else { // add term taxonomy $term_id = $wpdb->get_var("SELECT term_id FROM {$wpdb->terms} WHERE name = '" . esc_sql($name) . "'"); $this->the_plugin->db_custom_insert($wpdb->term_taxonomy, array('values' => array('term_id' => $term_id, 'taxonomy' => $taxonomy), 'format' => array('%d', '%s')), true); /*$wpdb->insert( $wpdb->term_taxonomy, array( 'term_id' => $term_id, 'taxonomy' => $taxonomy ) );*/ $term_taxonomy_id = $wpdb->insert_id; $__dbg = compact('taxonomy', 'attribute_value', 'term_id', 'term_taxonomy_id'); //var_dump('<pre>1c: ',$__dbg,'</pre>'); } } } } else { // get already existing attribute values $attribute_values = array(); /*$terms = get_terms($taxonomy, array('hide_empty' => true)); if( !is_wp_error( $terms ) ) { foreach ($terms as $term) { $attribute_values[] = $term->name; } } else { $error_string = $terms->get_error_message(); var_dump('<pre>',$error_string,'</pre>'); }*/ $terms = $this->the_plugin->load_terms($taxonomy); foreach ($terms as $term) { $attribute_values[] = $term->name; } // Check if $attribute_value is not empty if (!empty($attribute_values)) { foreach ($values as $attribute_value) { $attribute_value = (string) $attribute_value; if (!in_array($attribute_value, $attribute_values)) { // add new attribute value $__term_and_tax = wp_insert_term($attribute_value, $taxonomy); $__dbg = compact('taxonomy', 'attribute_value', '__term_and_tax'); //var_dump('<pre>1b: ',$__dbg,'</pre>'); } } } } // Add terms if (is_array($value)) { foreach ($value as $dm_v) { $dm_v = (string) $dm_v; if (!is_array($dm_v) && is_string($dm_v)) { $__term_and_tax = wp_insert_term($dm_v, $taxonomy); $__dbg = compact('taxonomy', 'dm_v', '__term_and_tax'); //var_dump('<pre>2: ',$__dbg,'</pre>'); } } } else { $value = (string) $value; if (!is_array($value) && is_string($value)) { $__term_and_tax = wp_insert_term($value, $taxonomy); $__dbg = compact('taxonomy', 'value', '__term_and_tax'); //var_dump('<pre>2b: ',$__dbg,'</pre>'); } } // link to woocommerce attribute values if (!empty($values)) { foreach ($values as $term) { if (!is_array($term) && !is_object($term)) { $term = sanitize_title($term); $term_taxonomy_id = $wpdb->get_var("SELECT tt.term_taxonomy_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE t.slug = '" . esc_sql($term) . "' AND tt.taxonomy = '" . esc_sql($taxonomy) . "'"); if ($term_taxonomy_id) { $checkSql = "SELECT * FROM {$wpdb->term_relationships} WHERE object_id = {$post_id} AND term_taxonomy_id = {$term_taxonomy_id}"; if (!$wpdb->get_var($checkSql)) { $wpdb->insert($wpdb->term_relationships, array('object_id' => $post_id, 'term_taxonomy_id' => $term_taxonomy_id)); } } } } } }