/**
* Source: https://katharinabrunner.de/2020/11/import-custom-taxonomy-to-woocommerce-by-csv-file/
*
*/
/**
* Register the 'Custom Column' column in the importer.
*
* @source https://github.com/woocommerce/woocommerce/wiki/Product-CSV-Importer-&-Exporter#adding-custom-import-columns-developers
* @param array $options
* @return array $options
*/
function add_artist_column_to_importer($options)
{
// column slug => column name
$options['artist'] = 'Artist';
return $options;
}
add_filter('woocommerce_csv_product_import_mapping_options', 'add_artist_column_to_importer');
/**
* Add automatic mapping support for 'Custom Column'.
* This will automatically select the correct mapping for columns named 'Custom Column' or 'custom column'.
*
* @param array $columns
* @return array $columns
*/
function add_artist_column_to_mapping_screen($columns)
{
// potential column name => column slug
$columns['Artist'] = 'artist';
$columns['artist'] = 'artist';
return $columns;
}
add_filter('woocommerce_csv_product_import_mapping_default_columns', 'add_artist_column_to_mapping_screen');
/**
* Insert new taxonomy in WooCommerce importer
*
* @param array $parsed_data
* @return array
*/
function woocommerce_add_artist_taxonomy($product, $data)
{
// set a variable with your custom taxonomy slug
$custom_taxonomy = 'artist';
if (is_a($product, 'WC_Product')) {
if (!empty($data[$custom_taxonomy])) {
$product->save();
$custom_taxonomy_values = $data[$custom_taxonomy];
$custom_taxonomy_values = explode(",", $custom_taxonomy_values);
$terms = array();
foreach ($custom_taxonomy_values as $custom_taxonomy_value) {
if (!get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)) {
$custom_taxonomy_args = array(
'cat_name' => $custom_taxonomy_value,
'taxonomy' => $custom_taxonomy,
);
$custom_taxonomy_value_cat = wp_insert_category($custom_taxonomy_args);
array_push($terms, $custom_taxonomy_value_cat);
} else {
$custom_taxonomy_value_cat = get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)->term_id;
array_push($terms, $custom_taxonomy_value_cat);
}
}
wp_set_object_terms($product->get_id(), $terms, $custom_taxonomy);
}
}
return $product;
}
add_filter('woocommerce_product_import_pre_insert_product_object', 'woocommerce_add_artist_taxonomy', 10, 2);
/**
* Create new metabox context for Artist Taxonomy
*
* @source https://buddydev.com/adding-custom-metabox-contextlocations-on-wordpress-editadd-new-screen/
*/
function create_new_artist_metabox_context($post)
{
do_meta_boxes(get_current_screen(), 'artist-metabox-context', $post);
}
add_action('edit_form_top', 'create_new_artist_metabox_context');
/**
* Create custom metabox
*/
require('fill_artist_meta_box_content.php');
function add_artist_meta_box()
{
add_meta_box(
'artist_box',
__('Artist'),
'fill_artist_meta_box_content',
'product',
'artist-metabox-context',
'high'
);
}
// add_action('add_meta_boxes', 'add_artist_meta_box');
// Move metabox to the top
function move_artist_metabox()
{
global $post, $wp_meta_boxes;
add_artist_meta_box();
do_meta_boxes(get_current_screen(), 'artist-metabox-context', $post);
// unset($wp_meta_boxes['product']['artist-metabox-context']);
// var_dump($wp_meta_boxes['product']);
// echo 'I\'m at the top?';
}
add_action('edit_form_before_permalink', 'move_artist_metabox');
/**
* Save artist when product is edited
*/
function save_artist()
{
global $post;
if (isset($_POST['artist']))
wp_set_object_terms($post->ID, sanitize_text_field($_POST['artist']), 'artist');
}
add_action('save_post', 'save_artist');
/**
* Metadata logic
*
* @source https://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/
*/
/**
* Add metadata fields to the 'Add new' page
*/
require('artist_add_new_meta_field.php');
add_action('artist_add_form_fields', 'artist_add_new_meta_field', 10, 2);
/**
* Add metadata fields to the 'Edit' page
*/
require('artist_edit_meta_field.php');
add_action('artist_edit_form_fields', 'artist_edit_meta_field', 10, 2);
/**
* Save taxonomy metadata
*/
function save_artist_meta($term_id)
{
if (isset($_POST['term_meta'])) {
$t_id = $term_id;
$term_meta = get_option("taxonomy_$t_id");
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key) {
if (isset($_POST['term_meta'][$key])) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option("taxonomy_$t_id", $term_meta);
}
}
add_action('edited_artist', 'save_artist_meta', 10, 2);
add_action('create_artist', 'save_artist_meta', 10, 2);
/**
* Enqueue media resources (required for media upload modal)
*/
function add_artist_admin_scripts($hook_suffix)
{
if (in_array($hook_suffix, ['edit-tags.php', 'term.php'])) {
wp_enqueue_media();
// wp_register_script('artist-image-upload', WP_PLUGIN_URL . '/artist_taxonomy/image_upload.js', ['jquery']);
// wp_enqueue_script('artist-image-upload');
}
}
add_action('admin_enqueue_scripts', 'add_artist_admin_scripts');
/**
* Load Artist metabox CSS.
*/
function load_artist_metabox_styles()
{
echo '';
}
add_action('admin_head-post.php', 'load_artist_metabox_styles');
/**
* Change columns in Artist list view
*/
function change_artist_columns($columns)
{
unset($columns['description']);
return $columns;
}
add_filter('manage_edit-artist_columns', 'change_artist_columns', 10, 1);
// DISABLING THIS AS WE'RE REMOVING THE DESC COLUMN ENTIRELY.
// add_filter('pre_get_terms', 'filter_if_artist', 10);
function filter_if_artist($query)
{
if (in_array('artist', $query->query_vars['taxonomy'])) {
add_filter('get_terms', 'shorten_artist_descriptions', 10, 4);
}
}
function shorten_artist_descriptions($terms, $taxonomy, $args, $term_query)
{
foreach ($terms as $term) {
if (strlen($term->description) > 30) {
$term->description = substr($term->description, 0, 30) . '...';
}
}
return $terms;
}
add_action('admin_head-edit-tags.php', 'load_artist_columns_css');
function load_artist_columns_css() {
echo '';
}
/**
* Source: https://katharinabrunner.de/2020/11/import-custom-taxonomy-to-woocommerce-by-csv-file/
*
*/
/**
* Source: https://github.com/woocommerce/woocommerce/wiki/Product-CSV-Importer-&-Exporter#adding-custom-import-columns-developers
**/
/**
* Register the 'Custom Column' column in the importer.
*
* @param array $options
* @return array $options
*/
function add_exhibition_column_to_importer($options)
{
// column slug => column name
$options['exhibition'] = 'Exhibition';
return $options;
}
add_filter('woocommerce_csv_product_import_mapping_options', 'add_exhibition_column_to_importer');
/**
* Add automatic mapping support for 'Custom Column'.
* This will automatically select the correct mapping for columns named 'Custom Column' or 'custom column'.
*
* @param array $columns
* @return array $columns
*/
function add_exhibition_column_to_mapping_screen($columns)
{
// potential column name => column slug
$columns['Exhibition'] = 'exhibition';
return $columns;
}
add_filter('woocommerce_csv_product_import_mapping_default_columns', 'add_exhibition_column_to_mapping_screen');
/**
* Insert new taxonomy in WooCommerce importer
*
* @param array $parsed_data
* @return array
*/
function woocommerce_add_exhibition_taxonomy($product, $data)
{
// set a variable with your custom taxonomy slug
$custom_taxonomy = 'exhibition';
if (is_a($product, 'WC_Product')) {
if (!empty($data[$custom_taxonomy])) {
$product->save();
$custom_taxonomy_values = $data[$custom_taxonomy];
$custom_taxonomy_values = explode(",", $custom_taxonomy_values);
$terms = array();
foreach ($custom_taxonomy_values as $custom_taxonomy_value) {
if (!get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)) {
$custom_taxonomy_args = array(
'cat_name' => $custom_taxonomy_value,
'taxonomy' => $custom_taxonomy,
);
$custom_taxonomy_value_cat = wp_insert_category($custom_taxonomy_args);
array_push($terms, $custom_taxonomy_value_cat);
} else {
$custom_taxonomy_value_cat = get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)->term_id;
array_push($terms, $custom_taxonomy_value_cat);
}
}
wp_set_object_terms($product->get_id(), $terms, $custom_taxonomy);
}
}
return $product;
}
add_filter('woocommerce_product_import_pre_insert_product_object', 'woocommerce_add_exhibition_taxonomy', 10, 2 );
/**
* Create new metabox context for Exhibition Taxonomy
*
* @source https://buddydev.com/adding-custom-metabox-contextlocations-on-wordpress-editadd-new-screen/
*/
require('fill_exhibition_meta_box_content.php');
function add_exhibit_meta_box()
{
add_meta_box('exhibition_box', __('Exposições'), 'fill_exhibition_meta_box_content', 'product', 'side');
}
// add_action('add_meta_boxes', 'add_exhibit_meta_box');
// Remove the default metabox for the 'exhibition' taxonomy
// function remove_default_exhibition_metabox()
// {
// remove_meta_box('tagsdiv-exhibition', 'product', 'side');
// }
// add_action('admin_menu', 'remove_default_exhibition_metabox');
/**
* Save exhibition when product is edited
*/
function save_exhibition($post_id)
{
// if (isset($_REQUEST['tax_input']['exhibition']))
// wp_set_object_terms($post_id, sanitize_text_field($_POST['exhibition']), 'exhibition');
}
add_action('save_post', 'save_exhibition');
// Filter the post data before saving
function fix_exhibition_terms($data, $postarr)
{
if (isset($postarr['tax_input']['exhibition'])) {
$exhibition_terms = $postarr['tax_input']['exhibition'];
// Replace the special character with commas within terms
$fixed_terms = array_map(function ($term) {
return str_replace(',', ',', $term);
}, $exhibition_terms);
// Update the exhibition terms in the post data
$data['tax_input']['exhibition'] = $fixed_terms;
}
return $data;
}
// add_filter('wp_insert_post_data', 'fix_exhibition_terms', 10, 2);
/**
* Add metadata fields to the 'Add new' page
*/
require('exhibition_add_new_meta_fields.php');
add_action('exhibition_add_form_fields', 'exhibition_add_new_meta_fields', 10, 2);
/**
* Add metadata fields to the 'Edit' page
*/
require('exhibition_edit_meta_fields.php');
add_action('exhibition_edit_form_fields', 'exhibition_edit_meta_fields', 10, 2);
/**
* Enqueue media resources (required for media upload modal)
*/
function add_exhibition_admin_scripts($hook_suffix)
{
if (in_array($hook_suffix, ['edit-tags.php', 'term.php'])) {
wp_enqueue_media();
}
}
add_action('admin_enqueue_scripts', 'add_exhibition_admin_scripts');
/**
* Save taxonomy metadata
*/
function save_exhibition_meta($term_id)
{
if (isset($_POST['term_meta'])) {
$t_id = $term_id;
$term_meta = get_option("taxonomy_$t_id");
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key) {
if (isset($_POST['term_meta'][$key])) {
if ($key == 'virtual') {
$virtual_value = $_POST['term_meta'][$key];
$_POST['term_meta'][$key] = filter_var($virtual_value, FILTER_VALIDATE_BOOLEAN);
} elseif ($key == 'opening_date') {
// Convert the date to UTC before saving
$date = date_create($_POST['term_meta'][$key]);
$_POST['term_meta'][$key] = date_format($date, 'Y-m-d');
}
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option("taxonomy_$t_id", $term_meta);
}
}
add_action('edited_exhibition', 'save_exhibition_meta', 10, 2);
add_action('create_exhibition', 'save_exhibition_meta', 10, 2);
/**
* Add columns to taxonomy list view
* @source https://wordpress.stackexchange.com/questions/28467/custom-columns-on-edit-tags-php-main-page
*/
function change_exhibition_columns($columns)
{
$columns['posts'] = 'Qtd.';
$columns['opening_date'] = 'Data';
$columns['virtual'] = 'Virtual';
unset($columns['description']);
unset($columns['slug']);
return $columns;
}
add_filter('manage_edit-exhibition_columns', 'change_exhibition_columns');
function add_exhibition_column_content($string, $column_name, $term_id)
{
$term_meta = get_option("taxonomy_$term_id");
if ($column_name == 'virtual') {
$string .= $term_meta['virtual'] ? __('Yes') : __('No');
}
if ($column_name == 'opening_date' && $term_meta['opening_date']) {
$string .= date( 'd/m/Y', strtotime($term_meta['opening_date']));
// $string .= $term_meta['opening_date'];
}
return $string;
}
add_filter('manage_exhibition_custom_column', 'add_exhibition_column_content', 10 , 3);
function exhibition_sortable_columns($columns) {
$columns['virtual'] = 'virtual';
$columns['opening_date'] = 'opening_date';
return $columns;
}
add_filter( 'manage_edit-exhibition_sortable_columns', 'exhibition_sortable_columns', 10, 1);
add_action('admin_head-edit-tags.php', 'load_exhibition_columns_css');
function load_exhibition_columns_css()
{
echo '';
}
// Modify the term query to sort by custom column
function sort_exhibition_terms($query)
{
if (!is_admin() || !isset($query->query_vars['orderby']) || $query->query_vars['orderby'] !== 'opening_date') {
return $query;
}
$query->query_vars['meta_key'] = 'opening_date';
$query->query_vars['orderby'] = 'meta_value';
$query->query_vars['meta_type'] = 'DATE';
return $query;
}
do_action_ref_array( 'pre_get_terms', $query );
add_filter('pre_get_terms', 'sort_exhibition_terms');
// add_filter
/**
* Plugin Name: Inn Gallery Custom Redirects
* Plugin URI: https://poema.dev/
* Description: Redirection for old artist URLs.
* Version: 0.1
* Author: Álvaro Antonio
* Author URI: https://poema.dev/
**/
add_action('template_redirect', 'custom_redirect');
function custom_redirect()
{
require('list.php');
$requested_url = $_SERVER['REQUEST_URI'];
if ($requested_url[-1] == '/') {
$requested_url = substr($requested_url, 0, -1);
}
$redirect_list;
if (array_key_exists($requested_url, $redirect_list)) {
wp_redirect($redirect_list[$requested_url], 301);
exit;
}
}