__('That supplier no longer exists.', 'ai-eshop-optimizer'))); } $extras = class_exists('AIEO_DMM_Supplier_Extras_Data') ? AIEO_DMM_Supplier_Extras_Data::get('supplier', $id) : array(); wp_send_json_success(array('id' => $id, 'name' => $term->name, 'extras' => $extras)); } public function ajax_save() { AIEO_DMM::verify_ajax_request(); $id = isset($_POST['id']) ? (int) $_POST['id'] : 0; $name = isset($_POST['name']) ? sanitize_text_field(wp_unslash($_POST['name'])) : ''; if ($name === '') { wp_send_json_error(array('message' => __('A supplier needs a name.', 'ai-eshop-optimizer'))); } if (!taxonomy_exists('supplier')) { wp_send_json_error(array('message' => __('The supplier taxonomy is not registered on this site.', 'ai-eshop-optimizer'))); } if ($id > 0) { $term = get_term($id, 'supplier'); if (!$term || is_wp_error($term)) { wp_send_json_error(array('message' => __('That supplier no longer exists.', 'ai-eshop-optimizer'))); } if ($term->name !== $name) { $renamed = wp_update_term($id, 'supplier', array('name' => $name)); if (is_wp_error($renamed)) { wp_send_json_error(array('message' => $renamed->get_error_message())); } } } else { $exists = term_exists($name, 'supplier'); if (is_array($exists) && !empty($exists['term_id'])) { $id = (int) $exists['term_id']; } elseif (is_numeric($exists) && (int) $exists > 0) { $id = (int) $exists; } else { $created = wp_insert_term($name, 'supplier'); if (is_wp_error($created)) { wp_send_json_error(array('message' => $created->get_error_message())); } $id = (int) $created['term_id']; } } $patch = array(); foreach (self::fields() as $field) { if (isset($_POST[$field])) { $patch[$field] = is_string($_POST[$field]) ? wp_unslash($_POST[$field]) : $_POST[$field]; } } if ($patch && class_exists('AIEO_DMM_Supplier_Extras_Data')) { AIEO_DMM_Supplier_Extras_Data::save('supplier', $id, $patch); } wp_send_json_success(array('id' => $id, 'message' => __('Saved.', 'ai-eshop-optimizer'))); } public function render_subtab() { global $wpdb; $terms = get_terms(array( 'taxonomy' => 'supplier', 'hide_empty' => false, 'number' => 500, 'orderby' => 'name', 'order' => 'ASC', )); if (is_wp_error($terms)) { $terms = array(); } $extras_by_term = array(); $brand_counts = array(); $sku_counts = array(); if (function_exists('aieo_dmm_table_exists')) { $se = $wpdb->prefix . 'aieo_dmm_supplier_extras'; if (aieo_dmm_table_exists($se)) { foreach ((array) $wpdb->get_results($wpdb->prepare("SELECT * FROM `{$se}` WHERE taxonomy = %s", 'supplier'), ARRAY_A) as $row) { $extras_by_term[(int) $row['term_id']] = $row; } } $be = $wpdb->prefix . 'aieo_dmm_brand_extras'; if (aieo_dmm_table_exists($be)) { foreach ((array) $wpdb->get_results("SELECT supplier_term_id, COUNT(*) c FROM `{$be}` WHERE supplier_term_id IS NOT NULL GROUP BY supplier_term_id", ARRAY_A) as $row) { $brand_counts[(int) $row['supplier_term_id']] = (int) $row['c']; } } $map = $wpdb->prefix . 'aieo_dmm_supplier_sku_map'; if (aieo_dmm_table_exists($map)) { foreach ((array) $wpdb->get_results("SELECT supplier_term_id, COUNT(*) c FROM `{$map}` WHERE supplier_term_id IS NOT NULL GROUP BY supplier_term_id", ARRAY_A) as $row) { $sku_counts[(int) $row['supplier_term_id']] = (int) $row['c']; } } } $owners = get_users(array('capability' => 'manage_woocommerce', 'fields' => array('ID', 'display_name'), 'number' => 200)); $this->render_template('suppliers.php', array( 'terms' => $terms, 'extras_by_term' => $extras_by_term, 'brand_counts' => $brand_counts, 'sku_counts' => $sku_counts, 'owners' => $owners, 'total' => (int) wp_count_terms(array('taxonomy' => 'supplier', 'hide_empty' => false)), )); } }