/set-address-function.php Secret
Last active
April 7, 2022 22:35
Star
You must be signed in to star a gist
all the post metas
This file contains 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 | |
/** | |
* Set the customer address | |
* | |
* @param array $address Address data | |
* @param string $type billing or shipping | |
*/ | |
public function set_address( $address, $type = 'billing' ) { | |
$meta_datas = array(); | |
foreach ( $address as $key => $value ) { | |
$meta_datas[ "_{$type}_" . $key ] = $value; | |
} | |
update_post_metas( $this->id, $meta_datas ); | |
} |
This file contains 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 | |
/** | |
* Add metadatas to a post. | |
* | |
* @since x.x.x | |
* | |
* @param int $post_id Post ID. | |
* @param string $meta_data Metadata as an key/value pair array | |
* | |
* @return bool Was the data inserted | |
*/ | |
function add_post_metas( $post_id, $meta_data ) { | |
// Make sure meta is added to the post, not a revision. | |
if ( $the_post = wp_is_post_revision($post_id) ) | |
$post_id = $the_post; | |
return add_metadatas('post', $post_id, $meta_data); | |
} | |
/** | |
* Update metadatas on a post. This is done by deleting all the post meta and then re-inserting it. | |
* | |
* To update post meta another way it would require two calls of update_post_meta | |
* Each update_post_meta call creates 1 select and 1 update mysql query | |
* 2 update_post_meta calls creates 2 select and 2 update queries | |
* update_post_metas creates 1 delete query, 1 insert query, and 1 select for every post meta being added | |
* | |
* Updating X metas: | |
* update_post_meta = X selects, X updates | |
* update_post_metas = X selects, 1 delete, 1 insert | |
* | |
* The benefits of update_post_metas grows the more post metas being updated. | |
* | |
* Example: Updating 22 address metas on a WooCommerce order | |
* update_post_meta = 22 selects, 22 updates | |
* update_post_metas = 22 selects, 1 delete, 1 insert | |
* | |
* @since x.x.x | |
* | |
* @param int $post_id Post ID. | |
* @param string $meta_data Metadata as an key/value pair array | |
* | |
* @return bool Was the data inserted | |
*/ | |
function update_post_metas( $post_id, $meta_data ) { | |
// Make sure meta is added to the post, not a revision. | |
if ( $the_post = wp_is_post_revision($post_id) ) | |
$post_id = $the_post; | |
delete_post_metas( $post_id, array_keys( $meta_data ) ); | |
return add_post_metas( $post_id, $meta_data ); | |
} | |
/** | |
* Delete metadatas on a post. | |
* | |
* @since x.x.x | |
* | |
* @param int $post_id Post ID. | |
* @param string $meta_data Metadata as an key/value pair array | |
* | |
* @return bool Was the data inserted | |
*/ | |
function delete_post_metas( $post_id, $meta_keys ) { | |
// Make sure meta is added to the post, not a revision. | |
if ( $the_post = wp_is_post_revision($post_id) ) | |
$post_id = $the_post; | |
return delete_metadatas('post', $post_id, $meta_keys); | |
} | |
/** | |
* Add multiple metadatas for the specified object. Similar to calling add_metadata for each metadata individually, | |
* and is only applicable for unique meta data. If a meta key already exists for an object it will not be stored. | |
* | |
* @since x.x.x | |
* | |
* @global wpdb $wpdb WordPress database abstraction object. | |
* | |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) | |
* @param int $object_id ID of the object metadata is for | |
* @param array $meta_data Metadata as an key/value pair array | |
* | |
* @return bool If the metadata was stored successfully. | |
*/ | |
function add_metadatas($meta_type, $object_id, $meta_data) { | |
global $wpdb; | |
if ( ! $meta_type || ! is_array( $meta_data ) || ! is_numeric( $object_id ) ) { | |
return false; | |
} | |
$object_id = absint( $object_id ); | |
if ( ! $object_id ) { | |
return false; | |
} | |
$table = _get_meta_table( $meta_type ); | |
if ( ! $table ) { | |
return false; | |
} | |
$column = sanitize_key($meta_type . '_id'); | |
/** | |
* Filter whether to add metadatas of a specific type. | |
* | |
* The dynamic portion of the hook, `$meta_type`, refers to the meta | |
* object type (comment, post, or user). Returning a non-null value | |
* will effectively short-circuit the function. | |
* | |
* @since x.x.x | |
* | |
* @param null|bool $check Whether to allow adding metadata for the given type. | |
* @param int $object_id Object ID. | |
* @param string $meta_key Meta key. | |
* @param mixed $meta_value Meta value. Must be serializable if non-scalar. | |
* @param bool $unique Whether the specified meta key should be unique | |
* for the object. Optional. Default false. | |
*/ | |
$check = apply_filters( "add_{$meta_type}_metadatas", null, $object_id, $meta_data ); | |
if ( null !== $check ) | |
return $check; | |
$_meta_data = array(); | |
foreach( $meta_data as $key => $value ) { | |
if ( 0 == absint( $wpdb->get_var( | |
$wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d", $key, $object_id ) | |
) ) ) { | |
$key = wp_unslash( $key ); | |
$value = wp_unslash( sanitize_meta( $key, $value, $meta_type ) ); | |
$_meta_data[ $key ] = maybe_serialize( $value ); | |
/** | |
* Fires immediately before meta of a specific type is added. | |
* | |
* The dynamic portion of the hook, `$meta_type`, refers to the meta | |
* object type (comment, post, or user). | |
* | |
* @since 3.1.0 | |
* | |
* @param int $object_id Object ID. | |
* @param string $meta_key Meta key. | |
* @param mixed $meta_value Meta value. | |
*/ | |
do_action( "add_{$meta_type}_meta", $object_id, $key, $value ); | |
} | |
} | |
$sql = false; | |
$rows = array(); | |
if( ! empty( $_meta_data ) ) { | |
$sql = "INSERT INTO {$table} ({$column}, meta_key, meta_value) VALUES "; | |
$comma = false; | |
$value_string = '( %d, %s, %s)'; | |
foreach( $_meta_data as $key => $value ) { | |
if( true == $comma ) { | |
$sql .= ', '; | |
} | |
$sql .= $value_string; | |
$sql = $wpdb->prepare( $sql, $object_id, $key, $value ); | |
$comma = true; | |
} | |
} | |
if( $sql ) { | |
$result = $wpdb->query( $sql ); | |
} else { | |
return false; | |
} | |
if ( ! $result ) { | |
return false; | |
} | |
wp_cache_delete($object_id, $meta_type . '_meta'); | |
return true; | |
} | |
/** | |
* Delete metadata for the specified object. | |
* | |
* @since x.x.x | |
* | |
* @global wpdb $wpdb WordPress database abstraction object. | |
* | |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) | |
* @param int $object_id ID of the object metadata is for | |
* @param string $meta_keys Metadata keys | |
* @return bool True on successful delete, false on failure. | |
*/ | |
function delete_metadatas($meta_type, $object_id, $meta_keys) { | |
global $wpdb; | |
if ( ! $meta_type || ! is_array( $meta_keys ) || ! is_numeric( $object_id ) ) { | |
return false; | |
} | |
$object_id = absint( $object_id ); | |
if ( ! $object_id ) { | |
return false; | |
} | |
$table = _get_meta_table( $meta_type ); | |
if ( ! $table ) { | |
return false; | |
} | |
$type_column = sanitize_key($meta_type . '_id'); | |
// expected_slashed ($meta_key) | |
$meta_key = array_map( 'wp_unslash', $meta_keys ); | |
/** | |
* Filter whether to delete metadata of a specific type. | |
* | |
* The dynamic portion of the hook, `$meta_type`, refers to the meta | |
* object type (comment, post, or user). Returning a non-null value | |
* will effectively short-circuit the function. | |
* | |
* @since x.x.x | |
* | |
* @param null|bool $delete Whether to allow metadata deletion of the given type. | |
* @param int $object_id Object ID. | |
* @param string $meta_keys Meta keys. | |
*/ | |
$check = apply_filters( "delete_{$meta_type}_metadatas", null, $object_id, $meta_keys ); | |
if ( null !== $check ) | |
return (bool) $check; | |
/** | |
* Fires immediately before deleting metadata of a specific type. | |
* | |
* The dynamic portion of the hook, `$meta_type`, refers to the meta | |
* object type (comment, post, or user). | |
* | |
* @since x.x.x | |
* | |
* @param int $object_id Object ID. | |
* @param string $meta_keys Meta keys. | |
*/ | |
do_action( "delete_{$meta_type}_metas", $object_id, $meta_keys ); | |
$sql = "DELETE FROM {$table} WHERE {$type_column} = {$object_id} and meta_key IN( "; | |
$comma = false; | |
foreach( $meta_keys as $meta_key ) { | |
if( true == $comma ) { | |
$sql .= ', '; | |
} | |
$sql .= " `{$meta_key}`"; | |
$comma = true; | |
} | |
$sql .= " )"; | |
$count = $wpdb->query( $sql ); | |
if ( ! $count ) | |
return false; | |
wp_cache_delete($object_id, $meta_type . '_meta'); | |
/** | |
* Fires immediately after deleting metadata of a specific type. | |
* | |
* The dynamic portion of the hook name, `$meta_type`, refers to the meta | |
* object type (comment, post, or user). | |
* | |
* @since x.x.x | |
* | |
* @param int $object_id Object ID. | |
* @param string $meta_keys Meta key. | |
*/ | |
do_action( "deleted_{$meta_type}_metas", $object_id, $meta_keys ); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment