| | 135 | /** |
| | 136 | * Add multiple metadatas for the specified object. Similar to calling add_metadata for each metadata individually, |
| | 137 | * and is only applicable for unique meta data. If a meta key already exists for an object it will not be stored. |
| | 138 | * |
| | 139 | * @since x.x.x |
| | 140 | * |
| | 141 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 142 | * |
| | 143 | * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
| | 144 | * @param int $object_id ID of the object metadata is for |
| | 145 | * @param string $meta_key Metadata key |
| | 146 | * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
| | 147 | * @param bool $unique Optional, default is false. |
| | 148 | * Whether the specified metadata key should be unique for the object. |
| | 149 | * If true, and the object already has a value for the specified metadata key, |
| | 150 | * no change will be made. |
| | 151 | * @return int|false The meta ID on success, false on failure. |
| | 152 | */ |
| | 153 | function add_metadatas( $meta_type, $object_id, $meta_data ) { |
| | 154 | global $wpdb; |
| | 155 | |
| | 156 | if ( ! $meta_type || ! is_numeric( $object_id ) || ! is_array( $meta_data ) ) { |
| | 157 | return false; |
| | 158 | } |
| | 159 | |
| | 160 | $object_id = absint( $object_id ); |
| | 161 | if ( ! $object_id ) { |
| | 162 | return false; |
| | 163 | } |
| | 164 | |
| | 165 | $table = _get_meta_table( $meta_type ); |
| | 166 | if ( ! $table ) { |
| | 167 | return false; |
| | 168 | } |
| | 169 | |
| | 170 | $column = sanitize_key( $meta_type . '_id' ); |
| | 171 | |
| | 172 | /** |
| | 173 | * Filters whether to add metadata of a specific type. |
| | 174 | * |
| | 175 | * The dynamic portion of the hook, `$meta_type`, refers to the meta |
| | 176 | * object type (comment, post, or user). Returning a non-null value |
| | 177 | * will effectively short-circuit the function. |
| | 178 | * |
| | 179 | * @since 3.1.0 |
| | 180 | * |
| | 181 | * @param null|bool $check Whether to allow adding metadata for the given type. |
| | 182 | * @param int $object_id Object ID. |
| | 183 | * @param string $meta_data Meta data, an array of meta keys and meta datas, meta values must be serializable |
| | 184 | * if non-scalar. |
| | 185 | */ |
| | 186 | $check = apply_filters( "add_{$meta_type}_metadatas", null, $object_id, $meta_data ); |
| | 187 | if ( null !== $check ) { |
| | 188 | return $check; |
| | 189 | } |
| | 190 | |
| | 191 | $_meta_data = array(); |
| | 192 | foreach( $meta_data as $key => $value ) { |
| | 193 | if ( 0 == absint( $wpdb->get_var( |
| | 194 | $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d", $key, $object_id ) |
| | 195 | ) ) ) { |
| | 196 | $key = wp_unslash( $key ); |
| | 197 | $value = wp_unslash( sanitize_meta( $key, $value, $meta_type ) ); |
| | 198 | |
| | 199 | $_meta_data[ $key ] = maybe_serialize( $value ); |
| | 200 | |
| | 201 | /** |
| | 202 | * Fires immediately before meta of a specific type is added. |
| | 203 | * |
| | 204 | * The dynamic portion of the hook, `$meta_type`, refers to the meta |
| | 205 | * object type (comment, post, or user). |
| | 206 | * |
| | 207 | * @since 3.1.0 |
| | 208 | * |
| | 209 | * @param int $object_id Object ID. |
| | 210 | * @param string $meta_key Meta key. |
| | 211 | * @param mixed $meta_value Meta value. |
| | 212 | */ |
| | 213 | do_action( "add_{$meta_type}_meta", $object_id, $key, $value ); |
| | 214 | |
| | 215 | } |
| | 216 | } |
| | 217 | |
| | 218 | $rows = array(); |
| | 219 | if( ! empty( $_meta_data ) ) { |
| | 220 | $sql = "INSERT INTO {$table} ({$column}, meta_key, meta_value) VALUES "; |
| | 221 | |
| | 222 | $comma = false; |
| | 223 | foreach( $_meta_data as $key => $value ) { |
| | 224 | if( true == $comma ) { |
| | 225 | $sql .= ','; |
| | 226 | } |
| | 227 | |
| | 228 | $sql = "({$object_id}, '{$key}', '{$value}')"; |
| | 229 | $comma = true; |
| | 230 | } |
| | 231 | } |
| | 232 | |
| | 233 | $result = $wpdb->query( $sql ); |
| | 234 | |
| | 235 | if ( ! $result ) { |
| | 236 | return false; |
| | 237 | } |
| | 238 | |
| | 239 | wp_cache_delete($object_id, $meta_type . '_meta'); |
| | 240 | |
| | 241 | return true; |
| | 242 | } |
| | 243 | |
| | 574 | /** |
| | 575 | * Delete metadatas for the specified object. |
| | 576 | * |
| | 577 | * @since x.x.x |
| | 578 | * |
| | 579 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 580 | * |
| | 581 | * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
| | 582 | * @param int $object_id ID of the object metadata is for |
| | 583 | * @param string $meta_keys Metadata key |
| | 584 | * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete |
| | 585 | * metadata entries with this value. Otherwise, delete all entries with the specified meta_key. |
| | 586 | * Pass `null, `false`, or an empty string to skip this check. (For backward compatibility, |
| | 587 | * it is not possible to pass an empty string to delete those entries with an empty string |
| | 588 | * for a value.) |
| | 589 | * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries for all objects, |
| | 590 | * ignoring the specified object_id. Otherwise, only delete matching metadata entries for |
| | 591 | * the specified object_id. |
| | 592 | * @return bool True on successful delete, false on failure. |
| | 593 | */ |
| | 594 | function delete_metadatas( $meta_type, $object_id, $meta_keys ) { |
| | 595 | global $wpdb; |
| | 596 | |
| | 597 | if ( ! $meta_type || ! is_numeric( $object_id ) && ! is_array( $meta_keys ) ) { |
| | 598 | return false; |
| | 599 | } |
| | 600 | |
| | 601 | $object_id = absint( $object_id ); |
| | 602 | if ( ! $object_id ) { |
| | 603 | return false; |
| | 604 | } |
| | 605 | |
| | 606 | $table = _get_meta_table( $meta_type ); |
| | 607 | if ( ! $table ) { |
| | 608 | return false; |
| | 609 | } |
| | 610 | |
| | 611 | $type_column = sanitize_key( $meta_type . '_id' ); |
| | 612 | $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
| | 613 | // expected_slashed ($meta_key) |
| | 614 | $meta_key = array_map( 'wp_unslash', $meta_keys ); |
| | 615 | |
| | 616 | /** |
| | 617 | * Filters whether to delete metadata of a specific type. |
| | 618 | * |
| | 619 | * The dynamic portion of the hook, `$meta_type`, refers to the meta |
| | 620 | * object type (comment, post, or user). Returning a non-null value |
| | 621 | * will effectively short-circuit the function. |
| | 622 | * |
| | 623 | * @since x.x.x |
| | 624 | * |
| | 625 | * @param null|bool $delete Whether to allow metadata deletion of the given type. |
| | 626 | * @param int $object_id Object ID. |
| | 627 | * @param string $meta_keys Meta keys. |
| | 628 | */ |
| | 629 | $check = apply_filters( "delete_{$meta_type}_metadatas", null, $object_id, $meta_keys ); |
| | 630 | if ( null !== $check ) { |
| | 631 | return (bool) $check; |
| | 632 | } |
| | 633 | |
| | 634 | /** |
| | 635 | * Fires immediately before deleting metadata of a specific type. |
| | 636 | * |
| | 637 | * The dynamic portion of the hook, `$meta_type`, refers to the meta |
| | 638 | * object type (comment, post, or user). |
| | 639 | * |
| | 640 | * @since x.x.x |
| | 641 | * |
| | 642 | * @param int $object_id Object ID. |
| | 643 | * @param string $meta_keys Meta keys. |
| | 644 | */ |
| | 645 | do_action( "delete_{$meta_type}_metas", $object_id, $meta_keys ); |
| | 646 | $sql = "DELETE FROM {$table} WHERE {$type_column} = {$object_id} and meta_key IN( "; |
| | 647 | $comma = false; |
| | 648 | foreach( $meta_keys as $meta_key ) { |
| | 649 | if( true == $comma ) { |
| | 650 | $sql .= ', '; |
| | 651 | } |
| | 652 | $sql .= " `{$meta_key}`"; |
| | 653 | $comma = true; |
| | 654 | } |
| | 655 | $sql .= " )"; |
| | 656 | $count = $wpdb->query( $sql ); |
| | 657 | |
| | 658 | if ( ! $count ) { |
| | 659 | return false; |
| | 660 | } |
| | 661 | wp_cache_delete($object_id, $meta_type . '_meta'); |
| | 662 | |
| | 663 | /** |
| | 664 | * Fires immediately after deleting metadata of a specific type. |
| | 665 | * |
| | 666 | * The dynamic portion of the hook name, `$meta_type`, refers to the meta |
| | 667 | * object type (comment, post, or user). |
| | 668 | * |
| | 669 | * @since x.x.x |
| | 670 | * |
| | 671 | * @param int $object_id Object ID. |
| | 672 | * @param string $meta_keys Meta key. |
| | 673 | */ |
| | 674 | do_action( "deleted_{$meta_type}_metas", $object_id, $meta_keys ); |
| | 675 | |
| | 676 | return true; |
| | 677 | } |
| | 678 | |