| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
/** |
| 6 |
* Viewing |
| 7 |
* |
| 8 |
* The Property Hive viewing class handles viewing data. |
| 9 |
* |
| 10 |
* @class PH_Viewing |
| 11 |
* @version 1.0.0 |
| 12 |
* @package PropertyHive/Classes |
| 13 |
* @category Class |
| 14 |
* @author PropertyHive |
| 15 |
*/ |
| 16 |
class PH_Viewing { |
| 17 |
|
| 18 |
/** @public int Viewing (post) ID */ |
| 19 |
public $id; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get the viewing if ID is passed, otherwise the viewing is new and empty. |
| 23 |
* |
| 24 |
* @access public |
| 25 |
* @param string $id (default: '') |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function __construct( $id = '' ) { |
| 29 |
if ( $id > 0 ) { |
| 30 |
$this->get_viewing( $id ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Gets a viewing from the database. |
| 36 |
* |
| 37 |
* @access public |
| 38 |
* @param int $id (default: 0) |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public function get_viewing( $id = 0 ) { |
| 42 |
if ( ! $id ) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
if ( $result = get_post( $id ) ) { |
| 46 |
$this->populate( $result ); |
| 47 |
return true; |
| 48 |
} |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* __isset function. |
| 54 |
* |
| 55 |
* @access public |
| 56 |
* @param mixed $key |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public function __isset( $key ) { |
| 60 |
if ( ! $this->id ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
return metadata_exists( 'post', $this->id, '_' . $key ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* __get function. |
| 68 |
* |
| 69 |
* @access public |
| 70 |
* @param mixed $key |
| 71 |
* @return mixed |
| 72 |
*/ |
| 73 |
public function __get( $key ) { |
| 74 |
// Get values or default if not set |
| 75 |
$value = get_post_meta( $this->id, $key, true ); |
| 76 |
if ($value == '') |
| 77 |
{ |
| 78 |
$value = get_post_meta( $this->id, '_' . $key, true ); |
| 79 |
} |
| 80 |
return $value; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Populates a viewing from the loaded post data. |
| 85 |
* |
| 86 |
* @access public |
| 87 |
* @param mixed $result |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function populate( $result ) { |
| 91 |
// Standard post data |
| 92 |
$this->id = $result->ID; |
| 93 |
$this->post_title = $result->post_title; |
| 94 |
$this->post_status = $result->post_status; |
| 95 |
} |
| 96 |
} |
| 97 |
|