Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Namespace
- namespace Leximo;
- // Use
- use Exception;
- /**
- * Meta
- * @param void
- * @return void
- */
- class Meta {
- /* @var string */
- private $object_type = null;
- /* @var int */
- private $object_id = null;
- /* @var array */
- private $meta = null;
- /**
- * Constructor
- * @param string
- * @return object
- */
- public function __construct($object_type, $object_id) {
- // Set meta
- $this->set($object_type, $object_id);
- }
- /**
- * Set
- * @param string, int
- * @return array
- */
- private function set($object_type, $object_id) {
- // Post
- if($object_type == 'post') {
- $meta = get_post_meta($object_id);
- }
- // Term
- else if ($object_type == 'term') {
- $meta = get_term_meta($object_id);
- }
- // Fuck of
- else {
- throw new Exception('Object type must be term or post!');
- }
- // Fill
- foreach($meta as $key=>$value) {
- $this->meta[$key] = isset($value[0]) ? maybe_unserialize($value[0]) : null;
- }
- }
- /**
- * Get by key
- * @param string
- * @return mixed
- */
- private function getByKey($key) {
- return isset($this->meta[$key]) ? $this->meta[$key] : null;
- }
- /**
- * Get by prefix
- * @param string
- * @return mixed
- */
- private function getByPrefix($prefix, $remove_prefix) {
- // Set result
- $meta = [];
- // Fill result
- foreach($this->meta as $key=>$val) {
- if(substr($key, 0, strlen($prefix)) == $prefix) {
- $meta[(($remove_prefix) ? str_replace($prefix, '', $key) : $key)] = $val;
- }
- }
- // Return result
- return $meta;
- }
- /**
- * Get
- * @param string, string
- * @return array
- */
- public function get($key = null, $prefix = false, $remove_prefix = true) {
- // Prefixed
- if($key and $prefix) {
- return $this->getByPrefix($key, $remove_prefix);
- }
- // By key
- else if($key) {
- return $this->getByKey($key);
- }
- // Get all
- return $this->meta;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement