Overview

Namespaces

  • Varunsridharan
    • WordPress

Classes

  • Varunsridharan\WordPress\Post
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 
<?php
/**
 * Simple WP Post Class With Advanced Options.
 *
 * @author    Varun Sridharan <varunsridharan23@gmail.com>
 * @copyright 2018 Varun Sridharan
 * @license   GPLV3 Or Greater
 */

namespace Varunsridharan\WordPress;

if ( ! class_exists( '\Varunsridharan\WordPress\Post' ) ) {
    /**
     * Class Post
     *
     * @package Varunsridharan\WordPress
     * @author Varun Sridharan <varunsridharan23@gmail.com>
     * @since 1.0
     */
    class Post {
        /**
         * Post Version
         *
         * @var string
         */
        public $version = '1.0';

        /**
         * Post id
         *
         * @var null
         */
        protected $id = null;

        /**
         * Post Object.
         *
         * @var null|\WP_Post
         */
        protected $post = null;

        /**
         * Returns All Taxonomies.
         *
         * @var array
         */
        protected $taxes = array();

        /**
         * All Post Data.
         *
         * @var array
         */
        protected $data = array();


        /**
         * VS_WP_Post constructor.
         *
         * @param int|string $post
         */
        public function __construct( $post = '' ) {
            $this->set_post( \get_post( $post ) );
        }

        /**
         * Sets Post Data.
         *
         * @param $post
         */
        protected function set_post( $post ) {
            $this->post = $post;
            $this->id   = ( isset( $post->ID ) ) ? $post->ID : false;

            if ( ! empty( $this->taxonomies() ) ) {
                foreach ( $this->taxonomies() as $taxonomy ) {
                    $this->{$taxonomy} = $this->get_terms( $taxonomy, array( 'fields' => 'all' ) );
                }
            }
        }

        /**
         * @param $name
         *
         * @return bool|mixed
         */
        public function __get( $name ) {
            if ( isset( $this->data[ $name ] ) ) {
                return $this->data[ $name ];
            }
            return false;
        }

        /**
         * Sets A Value to $data array
         *
         * @param $name
         * @param $value
         *
         * @uses \VS_WP_Post::$data
         *
         */
        public function __set( $name, $value ) {
            $this->data[ $name ] = $value;
        }

        /**
         * Returns Post ID.
         *
         * @return null
         */
        public function id() {
            return $this->id;
        }

        /**
         * Returns Post Object.
         *
         * @return null
         */
        public function post() {
            return $this->post;
        }

        /**
         * Checks If Post Has Featured Image.
         *
         * @return bool
         */
        public function has_featured_image() {
            return ( \get_the_post_thumbnail_url( $this->id() ) !== false ) ? true : false;
        }

        /**
         * Returns Featured Image ID.
         *
         * @return bool|int|mixed|string
         */
        public function featured_image_id() {
            if ( isset( $this->featured_image_id ) ) {
                return $this->featured_image_id;
            }
            $this->featured_image_id = \get_post_thumbnail_id( $this->post );
            return $this->featured_image_id;
        }

        /**
         * Returns Featured Image URL.
         *
         * @param string $size
         * @param string $default
         *
         * @return false|string
         */
        public function featured_image_url( $size = 'thumbnail', $default = '' ) {
            $featured_image = \get_the_post_thumbnail_url( $this->id(), $size );
            return ( false !== $featured_image ) ? $featured_image : $default;
        }

        /**
         * Returns Post Permalink.
         *
         * @return false|string
         */
        public function permalink() {
            return \get_permalink( $this->id() );
        }

        /**
         * Returns Post Author.
         *
         * @param bool|boolean $only_id
         *
         * @return bool
         */
        public function author( $only_id = true ) {
            if ( true === $only_id ) {
                return $this->post->post_author;
            }
            return false;
        }

        /**
         * Checks if post has content.
         *
         * @return bool
         */
        public function has_content() {
            return ( empty( $this->post->post_content ) ) ? false : true;
        }

        /**
         * Checks if post has post_excerpt.
         *
         * @return bool
         */
        public function has_excerpt() {
            return ( empty( $this->post->has_excerpt ) ) ? false : true;
        }

        /**
         * Returns Post Title.
         *
         * @return mixed
         */
        public function title() {
            return $this->post->post_title;
        }

        /**
         * Returns Post Slug.
         *
         * @return string
         */
        public function slug() {
            return $this->post->post_name;
        }

        /**
         * Returns Post Parent.
         *
         * @param boolean $only_id
         *
         * @return int
         */
        public function parent( $only_id = true ) {
            if ( $only_id ) {
                return $this->post->post_parent;
            }
            $class = get_called_class();
            return new $class( $this->post->post_parent );
        }

        /**
         * Returns Current Post Type.
         *
         * @return string
         */
        public function type() {
            return $this->post->post_type;
        }

        /**
         * Returns Page Template.
         *
         * @return string
         */
        public function page_template() {
            return $this->post->page_template;
        }

        /**
         * Returns Post post_excerpt.
         *
         * @return mixed
         */
        public function excerpt() {
            return $this->post->post_excerpt;
        }

        /**
         * Returns Post Content.
         *
         * @return string
         */
        public function content() {
            return $this->post->post_content;
        }

        /**
         * Returns Post Status.
         *
         * @return mixed
         */
        public function status() {
            return $this->post->post_status;
        }

        /**
         * Checks if given status is post status.
         *
         * @param string $status
         *
         * @return bool
         */
        public function is_status( $status = 'publish' ) {
            return ( $status === $this->post->post_status ) ? true : false;
        }

        /*====================================================================================
         * Post Meta Handler.                                                                *
         ====================================================================================*/

        /**
         * Gets Post Meta For the given meta key.
         *
         * @param string $meta_key
         * @param string $default
         *
         * @return mixed
         */
        public function get_meta( $meta_key = '', $default = '' ) {
            return \get_post_meta( $this->id(), $meta_key, $default );
        }

        /**
         * Updates Post Meta.
         *
         * @param string $meta_key
         * @param string $values
         * @param string $prev_values
         *
         * @return bool|int
         */
        public function update_meta( $meta_key = '', $values = '', $prev_values = '' ) {
            return \update_post_meta( $this->id(), $meta_key, $values, $prev_values );
        }

        /**
         * Adds Post Meta.
         *
         * @param string       $meta_key
         * @param string       $values
         * @param boolean|bool $unique
         *
         * @return bool|int
         */
        public function add_meta( $meta_key = '', $values = '', $unique = false ) {
            return \add_post_meta( $this->id(), $meta_key, $values, $unique );
        }

        /**
         * Deletes A Post Meta.
         *
         * @param string $meta_key
         * @param mixed  $values
         *
         * @return bool
         */
        public function delete_meta( $meta_key = '', $values = '' ) {
            return \delete_post_meta( $this->id(), $meta_key, $values );
        }

        /*====================================================================================
         * Taxonomy Handler.                                                                 *
         ====================================================================================*/

        /**
         * Returns All Post Taxonomies.
         *
         * @return array
         */
        public function taxonomies() {
            if ( ! empty( $this->taxes ) ) {
                return $this->taxes;
            }
            $taxes       = \get_post_taxonomies( $this->post );
            $this->taxes = $taxes;
            return $this->taxes;
        }

        /**
         * Returns All Terms From Post.
         *
         * @param string $taxonomy
         * @param array  $args
         *
         * @return array|\WP_Error
         */
        public function get_terms( $taxonomy = '', $args = array() ) {
            if ( isset( $this->{$taxonomy} ) && empty( $args ) ) {
                return $this->{$taxonomy};
            } else {
                $name = $taxonomy . '_' . md5( json_encode( $args ) );
                if ( isset( $this->{$name} ) ) {
                    return $this->{$name};
                }
                $data          = \wp_get_post_terms( $this->id(), $taxonomy, $args );
                $this->{$name} = $data;
                return $data;
            }
        }

        /**
         * Sets Given Terms With Tax To The Current Post.
         *
         * @param array  $terms
         * @param string $taxonomy
         * @param bool   $append
         *
         * @return array|false|\WP_Error
         */
        public function set_terms( $terms = array(), $taxonomy = '', $append = false ) {
            return \wp_set_post_terms( $this->id(), $terms, $taxonomy, $append );
        }


        /*====================================================================================
         * Class Helpers.
         ====================================================================================*/

        /**
         * Checks if given key is in array
         *
         * @param string $key
         * @param string $data
         * @param bool   $default
         *
         * @return bool|string
         * @uses \in_array()
         * @uses \VS_WP_Post
         *
         * @example in('somekey','post_category','no') post_category will be taken from post data.
         * @example in('somekey',array('somekey' => 'somekey'), will be used the given array.
         *
         */
        public function in( $key = '', $data = '', $default = false ) {
            if ( is_array( $data ) ) {
                return ( in_array( $key, $data ) ) ? $key : $default;
            }
            if ( isset( $this->{$data} ) ) {
                return ( in_array( $key, $this->{$data} ) ) ? $key : $default;
            }
            return $default;
        }
    }
}
API documentation generated by ApiGen