Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.05 KB | None | 0 0
  1. <?php
  2.  
  3. // Namespace
  4. namespace Leximo;
  5.  
  6. // Use
  7. use Exception;
  8.  
  9. /**
  10.  * Meta
  11.  * @param void
  12.  * @return void
  13.  */
  14.  
  15. class Meta {
  16.  
  17.   /* @var string */
  18.   private $object_type = null;
  19.  
  20.   /* @var int */
  21.   private $object_id = null;
  22.  
  23.   /* @var array */
  24.   private $meta = null;
  25.  
  26.   /**
  27.    * Constructor
  28.    * @param string
  29.    * @return object
  30.    */
  31.  
  32.   public function __construct($object_type, $object_id) {
  33.  
  34.     // Set meta
  35.     $this->set($object_type, $object_id);
  36.  
  37.   }
  38.  
  39.   /**
  40.    * Set
  41.    * @param string, int
  42.    * @return array
  43.    */
  44.  
  45.   private function set($object_type, $object_id) {
  46.  
  47.     // Post
  48.     if($object_type == 'post') {
  49.       $meta = get_post_meta($object_id);
  50.     }
  51.  
  52.     // Term
  53.     else if ($object_type == 'term') {
  54.       $meta = get_term_meta($object_id);
  55.     }
  56.  
  57.     // Fuck of
  58.     else {
  59.       throw new Exception('Object type must be term or post!');
  60.     }
  61.  
  62.     // Fill
  63.     foreach($meta as $key=>$value) {
  64.       $this->meta[$key] = isset($value[0]) ? maybe_unserialize($value[0]) : null;
  65.     }
  66.  
  67.   }
  68.  
  69.   /**
  70.    * Get by key
  71.    * @param string
  72.    * @return mixed
  73.    */
  74.  
  75.   private function getByKey($key) {
  76.     return isset($this->meta[$key]) ? $this->meta[$key] : null;
  77.   }
  78.  
  79.   /**
  80.    * Get by prefix
  81.    * @param string
  82.    * @return mixed
  83.    */
  84.  
  85.   private function getByPrefix($prefix, $remove_prefix) {
  86.  
  87.     // Set result
  88.     $meta = [];
  89.  
  90.     // Fill result
  91.     foreach($this->meta as $key=>$val) {
  92.       if(substr($key, 0, strlen($prefix)) == $prefix) {
  93.         $meta[(($remove_prefix) ? str_replace($prefix, '', $key) : $key)] = $val;
  94.       }
  95.     }
  96.  
  97.     // Return result
  98.     return $meta;
  99.  
  100.   }
  101.  
  102.   /**
  103.    * Get
  104.    * @param string, string
  105.    * @return array
  106.    */
  107.  
  108.   public function get($key = null, $prefix = false, $remove_prefix = true) {
  109.  
  110.     // Prefixed
  111.     if($key and $prefix) {
  112.       return $this->getByPrefix($key, $remove_prefix);
  113.     }
  114.  
  115.     // By key
  116.     else if($key) {
  117.       return $this->getByKey($key);
  118.     }
  119.  
  120.     // Get all
  121.     return $this->meta;
  122.  
  123.   }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement