| 1 |
<?php |
| 2 |
/** |
| 3 |
* Trait that abstracts the metadata functions |
| 4 |
* |
| 5 |
* @package UpStream\Traits |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace UpStream\Traits; |
| 9 |
|
| 10 |
// Prevent direct access. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Trait that abstracts the metadata functions |
| 17 |
* |
| 18 |
* @package UpStream |
| 19 |
* @subpackage Traits |
| 20 |
* @author UpStream <https://upstreamplugin.com> |
| 21 |
* @copyright Copyright (c) 2018 UpStream Project Management |
| 22 |
* @license GPL-3 |
| 23 |
* @since 1.11.0 |
| 24 |
*/ |
| 25 |
trait PostMetadata { |
| 26 |
|
| 27 |
/** |
| 28 |
* Post ID |
| 29 |
* |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
protected $post_id; |
| 33 |
|
| 34 |
/** |
| 35 |
* Get metadata |
| 36 |
* |
| 37 |
* @param string $meta_key Meta key. |
| 38 |
* @param bool $single Is single. |
| 39 |
* |
| 40 |
* @return mixed |
| 41 |
*/ |
| 42 |
public function get_metadata( $meta_key, $single = false ) { |
| 43 |
return get_post_meta( $this->post_id, $meta_key, $single ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Update metadata. |
| 48 |
* |
| 49 |
* @param array $dataset Dataset. |
| 50 |
*/ |
| 51 |
public function update_metadata( $dataset ) { |
| 52 |
if ( ! empty( $dataset ) ) { |
| 53 |
foreach ( $dataset as $meta_key => $meta_value ) { |
| 54 |
update_post_meta( $this->post_id, $meta_key, $meta_value ); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Delete metadata |
| 61 |
* |
| 62 |
* @param string|array $meta_key Meta key. |
| 63 |
*/ |
| 64 |
public function delete_metadata( $meta_key ) { |
| 65 |
if ( empty( $meta_key ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Only one meta key? |
| 70 |
if ( is_string( $meta_key ) ) { |
| 71 |
delete_post_meta( $this->post_id, $meta_key ); |
| 72 |
|
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// An array of meta keys? |
| 77 |
if ( is_array( $meta_key ) ) { |
| 78 |
foreach ( $meta_key as $key ) { |
| 79 |
if ( ! empty( $key ) ) { |
| 80 |
delete_post_meta( $this->post_id, $key ); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Add Unique Metadata |
| 88 |
* |
| 89 |
* @param array $dataset Dataset. |
| 90 |
* |
| 91 |
* @return array|false |
| 92 |
*/ |
| 93 |
public function add_unique_metadata( $dataset ) { |
| 94 |
if ( empty( $dataset ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
$meta_ids = array(); |
| 99 |
|
| 100 |
foreach ( $dataset as $meta_key => $meta_value ) { |
| 101 |
$meta_ids[ $meta_key ] = add_post_meta( $this->post_id, $meta_key, $meta_key, true ); |
| 102 |
} |
| 103 |
|
| 104 |
return $meta_ids; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Add Non Unique Metadata |
| 109 |
* |
| 110 |
* @param string $meta_key Meta key. |
| 111 |
* @param array $meta_values Meta values. |
| 112 |
* |
| 113 |
* @return array|false |
| 114 |
*/ |
| 115 |
public function add_non_unique_metadata( $meta_key, $meta_values ) { |
| 116 |
if ( empty( $meta_key ) || empty( $meta_values ) ) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
$meta_ids = array(); |
| 121 |
|
| 122 |
foreach ( $meta_values as $meta_value ) { |
| 123 |
$meta_ids[] = add_post_meta( $this->post_id, $meta_key, $meta_value ); |
| 124 |
} |
| 125 |
|
| 126 |
return $meta_ids; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Update Non Unique Metadata |
| 131 |
* |
| 132 |
* @param string $meta_key Meta key. |
| 133 |
* @param array $meta_values Meta values. |
| 134 |
* |
| 135 |
* @return array|false |
| 136 |
*/ |
| 137 |
public function update_non_unique_metadata( $meta_key, $meta_values ) { |
| 138 |
$this->delete_metadata( $meta_key ); |
| 139 |
|
| 140 |
return $this->add_non_unique_metadata( $meta_key, $meta_values ); |
| 141 |
} |
| 142 |
} |
| 143 |
|