| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use WPSEO_Meta; |
| 6 |
use WPSEO_Taxonomy_Meta; |
| 7 |
|
| 8 |
/** |
| 9 |
* A helper object for meta. |
| 10 |
*/ |
| 11 |
class Meta_Helper { |
| 12 |
|
| 13 |
/** |
| 14 |
* Get a custom post meta value. |
| 15 |
* |
| 16 |
* Returns the default value if the meta value has not been set. |
| 17 |
* |
| 18 |
* {@internal Unfortunately there isn't a filter available to hook into before returning |
| 19 |
* the results for get_post_meta(), get_post_custom() and the likes. That |
| 20 |
* would have been the preferred solution.}} |
| 21 |
* |
| 22 |
* @codeCoverageIgnore We have to write test when this method contains own code. |
| 23 |
* |
| 24 |
* @param string $key Internal key of the value to get (without prefix). |
| 25 |
* @param int $postid Post ID of the post to get the value for. |
| 26 |
* |
| 27 |
* @return string All 'normal' values returned from get_post_meta() are strings. |
| 28 |
* Objects and arrays are possible, but not used by this plugin |
| 29 |
* and therefore discarted (except when the special 'serialized' field def |
| 30 |
* value is set to true - only used by add-on plugins for now). |
| 31 |
* Will return the default value if no value was found. |
| 32 |
* Will return empty string if no default was found (not one of our keys) or |
| 33 |
* if the post does not exist. |
| 34 |
*/ |
| 35 |
public function get_value( $key, $postid = 0 ) { |
| 36 |
return WPSEO_Meta::get_value( $key, $postid ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Retrieve a taxonomy term's meta value(s). |
| 41 |
* |
| 42 |
* @param mixed $term Term to get the meta value for |
| 43 |
* either (string) term name, (int) term id or (object) term. |
| 44 |
* @param string $taxonomy Name of the taxonomy to which the term is attached. |
| 45 |
* @param string|null $meta Optional. Meta value to get (without prefix). |
| 46 |
* |
| 47 |
* @return mixed|bool Value for the $meta if one is given, might be the default. |
| 48 |
* If no meta is given, an array of all the meta data for the term. |
| 49 |
* False if the term does not exist or the $meta provided is invalid. |
| 50 |
*/ |
| 51 |
public function get_term_value( $term, $taxonomy, $meta = null ) { |
| 52 |
return WPSEO_Taxonomy_Meta::get_term_meta( $term, $taxonomy, $meta ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Set a custom post meta value. |
| 57 |
* |
| 58 |
* @param string $key Internal key of the value to set (without prefix). |
| 59 |
* @param mixed $meta_value The value to set the meta value to. |
| 60 |
* @param int $post_id Post ID of the post to set the value for. |
| 61 |
* |
| 62 |
* @return bool Whether the value was changed. |
| 63 |
*/ |
| 64 |
public function set_value( $key, $meta_value, $post_id ) { |
| 65 |
return WPSEO_Meta::set_value( $key, $meta_value, $post_id ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Deletes a meta value for a post. |
| 70 |
* |
| 71 |
* @param string $key The internal key of the meta value to change (without prefix). |
| 72 |
* @param int $post_id The ID of the post to delete the meta for. |
| 73 |
* |
| 74 |
* @return bool Whether the delete was successful or not. |
| 75 |
*/ |
| 76 |
public function delete( $key, $post_id ) { |
| 77 |
return WPSEO_Meta::delete( $key, $post_id ); |
| 78 |
} |
| 79 |
} |
| 80 |
|