Created
May 18, 2022 03:07
-
-
Save RadGH/de47516a337550fc1e768e9213f92b84 to your computer and use it in GitHub Desktop.
When updating a post, user, or term, ensure specific fields have a blank entry in the database even when null.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* When updating a post, user, or term, ensure specific fields have a blank entry in the database even when null. | |
* | |
* @param $object_id | |
* @param null $type | |
* @param null $subtype | |
*/ | |
function rs_insert_missing_meta_keys_on_save( $object_id, $type = null, $subtype = null ) { | |
if ( $type === null ) return; | |
$required = array( | |
'post' => array( | |
// Post type "evaluation" => post meta | |
'facilitator' => array( | |
'_thumbnail_id', | |
'first_name', | |
'last_name', | |
'business_zip', | |
'business_country', | |
'program', | |
), | |
// Post type "evaluation" => post meta | |
'evaluation' => array( | |
'enable_assessments', | |
'prework_status', | |
'prework_end_date', | |
'prework_end_sent', | |
), | |
), | |
'term' => array( | |
// Taxonomy "account-menu" => term meta | |
'account-menu' => array( | |
'conditions', | |
'conditions_program', | |
'conditions_rs_status', | |
'conditions_training_date', | |
'conditions_training_date_start', | |
'conditions_training_date_end', | |
'conditions_assigned_event', | |
'conditions_role', | |
), | |
), | |
'user' => array( | |
// User metadata (untested) | |
// 'example', | |
// 'example_2', | |
), | |
); | |
// Get the meta keys that we'll be checking | |
if ( $type == 'post' ) { | |
// Post, by post type | |
$keys = isset($required['post'][$subtype]) ? $required['post'][$subtype] : false; | |
}else if ( $type == 'term' ) { | |
// Term, by taxonomy | |
$keys = isset($required['term'][$subtype]) ? $required['term'][$subtype] : false; | |
}else if ( $type == 'user' ) { | |
// Users | |
$keys = $required['user']; | |
}else{ | |
return; | |
} | |
if ( $keys ) { | |
// Get the current meta keys | |
$meta = get_metadata( $type, $object_id ); | |
// Check if each key is present, if not then create it | |
foreach( $keys as $key ) { | |
if ( !isset($meta[$key]) ) { | |
update_metadata( $type, $object_id, $key, '' ); | |
} | |
} | |
} | |
} | |
add_action( 'save_post', function( $post_id ) { rs_insert_missing_meta_keys_on_save($post_id, 'post', get_post_type($post_id)); }, 1, 1 ); | |
add_action( 'profile_update', function( $user_id ) { rs_insert_missing_meta_keys_on_save($user_id, 'user'); }, 1, 2 ); | |
add_action( 'edited_term', function( $term_id, $tt_id, $taxonomy ) { rs_insert_missing_meta_keys_on_save($term_id, 'term', $taxonomy); }, 1, 3 ); | |
// https://rsjan2022.wpengine.com/?rs_bulk_insert_missing_taxonomy_metadata | |
function rs_bulk_insert_missing_taxonomy_metadata() { | |
if ( ! current_user_can('administrator') ) wp_die('admin only in ' . __FILE__); | |
// Loop through all items in a taxonomy and fill meta keys that are missing | |
$taxonomies = array( | |
'account-menu', | |
); | |
$terms = get_terms(array( 'taxonomy' => $taxonomies, 'hide_empty' => false )); | |
echo '<pre>'; | |
echo "Filling keys for these terms:\n\n"; | |
foreach( $terms as $i => $term ) { | |
echo "[$term->taxonomy][$term->term_id] \"$term->name\"" . "\n"; | |
$before = get_term_meta( $term->term_id ); | |
// Uses the same action as from wp-includes/taxonomy.php. | |
// This way we know it works normally - when terms are edited. | |
/** | |
* @param int $term_id Term ID. | |
* @param int $tt_id Term taxonomy ID. | |
* @param string $taxonomy Taxonomy slug. | |
*/ | |
do_action( 'edited_term', $term->term_id, $term->term_taxonomy_id, $term->taxonomy ); | |
// needed for $after to get new values | |
clean_term_cache( $term->term_id ); | |
clean_object_term_cache( $term->term_id, $term->taxonomy ); | |
$after = get_term_meta( $term->term_id ); | |
$diff = count($after) - count($before); | |
echo " "; | |
if ( $diff === 0 ) { | |
echo "No changes"; | |
}else if ( $diff === 1 ) { | |
echo "1 change"; | |
}else { | |
echo $diff . " changes"; | |
} | |
echo "\n"; | |
echo "\n"; | |
} | |
exit; | |
} | |
if ( isset($_GET['rs_bulk_insert_missing_taxonomy_metadata']) ) add_action( 'init', 'rs_bulk_insert_missing_taxonomy_metadata' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment