-
-
Save anonymous/e316576aead7c35e3ee3 to your computer and use it in GitHub Desktop.
Pro tutoriál o modelech na Blog.Netcorex.cz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class KT_WP_Post_Base_Model { | |
private $post = null; | |
private $author = null; | |
private $postMetas = null; | |
private $gallery = null; | |
private $data = array( ); | |
/** | |
* Základní model pro práci s daty post_typu | |
* | |
* @param WP_Post $post | |
* @param int $postId | |
* @return \kt_wp_post_model_base | |
*/ | |
function __construct( WP_Post $post = null, $postId = null ) { | |
if ( kt_isset_and_not_empty( $post ) ) { | |
$this->setPost( $post ); | |
return $this; | |
} | |
if ( kt_is_id_format( $postId ) ) { | |
$this->initPostFromId( $postId ); | |
return $this; | |
} | |
} | |
// --- magic functions ----- | |
public function __set( $name, $value ) { | |
$this->data[ $name ] = $value; | |
} | |
public function __get( $name ) { | |
if ( array_key_exists( $name, $this->data ) ) { | |
return $this->data[ $name ]; | |
} | |
return null; | |
} | |
public function __isset( $name ) { | |
return isset( $this->data[ $name ] ); | |
} | |
public function __unset( $name ) { | |
unset( $this->data[ $name ] ); | |
} | |
// --- settery ------------- | |
private function setPost( WP_Post $post ) { | |
$this->post = $post; | |
return $this; | |
} | |
private function setAuthor( WP_User $author ) { | |
$this->author = $author; | |
return $this; | |
} | |
private function setPostMetas( array $postMetas ) { | |
$this->postMetas = $postMetas; | |
return $this; | |
} | |
private function setGallery( KT_WP_Post_Gallery $gallery ) { | |
$this->gallery = $gallery; | |
return $this; | |
} | |
// --- gettery ------------- | |
/** | |
* @return \WP_Post | |
*/ | |
public function getPost() { | |
return $this->post; | |
} | |
/** | |
* @return \WP_User | |
*/ | |
public function getAuthor() { | |
if ( kt_not_isset_or_empty( $this->author ) ) | |
$this->initAuthor(); | |
return $this->author; | |
} | |
/** | |
* @return array | |
*/ | |
public function getPostMetas() { | |
if ( kt_not_isset_or_empty( $this->postMetas ) ) | |
$this->initPostMetas(); | |
return $this->postMetas; | |
} | |
/** | |
* | |
* @return \KT_WP_Post_Gallery | |
*/ | |
public function getGallery() { | |
if ( kt_not_isset_or_empty( $this->gallery ) ) | |
$this->initGallery(); | |
return $this->gallery; | |
} | |
// --- Veřejné funkce ------- | |
/** | |
* Vrátí ID WP_Postu v rámci modelu | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @return int | |
*/ | |
public function getPostId() { | |
return $this->getPost()->ID; | |
} | |
/** | |
* Vrátí ID náhledového obrázku. Pokud není přiřazen, vrátí Null | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @return mixed null || int | |
*/ | |
public function getThumbnailId() { | |
$thumbnailId = $this->getMetaValue( "_thumbnail_id" ); | |
if ( kt_isset_and_not_empty( $thumbnailId ) ) | |
return $thumbnailId; | |
return null; | |
} | |
/** | |
* Vrátí datum publikace příspěvku v základním formátu "d.m.Y" | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @param string $dateFormat | |
* @return string | |
*/ | |
public function getPublishDate( $dateFormat = "d.m.Y" ) { | |
return mysql2date( $dateFormat, $this->getPost()->post_date ); | |
} | |
/** | |
* Vrátí hodnotu z $wpdb->postmeta na základě zadaného meta_key | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @param type $key | |
* @return null | |
*/ | |
public function getMetaValue( $key ) { | |
if ( kt_not_isset_or_empty( $this->postMetas ) ) | |
$this->initPostMetas(); | |
$meta = $this->postMetas[ $key ]; | |
if ( kt_isset_and_not_empty( $meta ) ) | |
return $meta; | |
return null; | |
} | |
/** | |
* Vrátí kolekci všech termů, kam je post zařazen na základě zadané taxonomy | |
* Pokud ještě nebyly načteny, uloží je do proměnné $this->data->{$taxonomy} a znovu se na ně nedotazuje | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @param string $taxonomy | |
* @param array $args // wp_get_object_terms | |
* @return type | |
*/ | |
public function getTermCollection( $taxonomy, array $args = array( ) ) { | |
if ( ! isset( $this->$taxonomy ) ) { | |
$termCollection = self::getTermCollectionByPost( $this->getPost(), $taxonomy, $args ); | |
$this->$taxonomy = $termCollection; | |
} | |
return $this->$taxonomy; | |
} | |
// --- private function ---- | |
/** | |
* Inicializuje WP_Post objekt na základě zadaného id | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @param type $postId | |
* @return \KT_Post_Type_Presenter_Base | |
* @throws KT_Null_Reference_Exception | |
*/ | |
private function initPostFromId( $postId ) { | |
$postId = kt_try_get_int( $postId ); | |
$post = get_post( $postId ); | |
if ( kt_isset_and_not_empty( $post ) ) { | |
$this->setPost( $post ); | |
} else { | |
throw new KT_Null_Reference_Exception( "post" ); | |
} | |
return $this; | |
} | |
// --- privátní funkce ------ | |
/** | |
* Inicializuje WP_User objekt na základě post_author | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @return \KT_Post_Type_Presenter_Base | |
*/ | |
private function initAuthor() { | |
$authorId = $this->getPost()->post_author; | |
if ( kt_is_id_format( $authorId ) ) { | |
$author = new WP_User( $authorId ); | |
$this->setAuthor( $author ); | |
} | |
return $this; | |
} | |
/** | |
* Inicializuje pole postMetas na na základě prefixu nebo všechny | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @param string $metaNamePrefix | |
* @return \KT_Post_Type_Presenter_Base | |
*/ | |
private function initPostMetas( $metaNamePrefix = null ) { | |
$postMetas = kt_get_post_metas( $this->getPost()->ID, $metaNamePrefix ); | |
$this->setPostMetas( $postMetas ); | |
return $this; | |
} | |
/** | |
* Inicializuje objekt WP_Post_Gallery s kolekcí obrázků u postu | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @return \KT_WP_Post_Base_Model | |
*/ | |
private function initGallery() { | |
$postGallery = new KT_WP_Post_Gallery( $this->getPost() ); | |
$this->setGallery( $postGallery ); | |
return $this; | |
} | |
// --- privatní statické funkce -- | |
/** | |
* Vrátí všechny termy, kam daný post patří na základě zvolené taxonomy | |
* | |
* @author Tomáš Kocifaj <kocifaj@ktstudio.cz> | |
* @link www.ktstudio.cz | |
* | |
* @param WP_Post $post | |
* @param string $taxonomy | |
* @param array $args | |
* @return mixed null || array | |
*/ | |
public static function getTermCollectionByPost( WP_Post $post, $taxonomy = KT_WP_CATEGORY_KEY, $args = array( ) ) { | |
$terms = wp_get_object_terms( $post->ID, $taxonomy, $args ); | |
if ( kt_isset_and_not_empty( $terms ) && ! is_wp_error( $terms ) ) { | |
return $terms; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment