| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Abstract_Post_Data. |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Prevent loading this file directly |
| 12 |
*/ |
| 13 |
defined( 'ABSPATH' ) || exit(); |
| 14 |
|
| 15 |
if ( ! class_exists( 'LP_Abstract_Post_Data' ) ) { |
| 16 |
|
| 17 |
/** |
| 18 |
* Class LP_Abstract_Post_Data |
| 19 |
*/ |
| 20 |
class LP_Abstract_Post_Data extends LP_Abstract_Object_Data { |
| 21 |
|
| 22 |
/** |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $_post_type = ''; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $_content = ''; |
| 31 |
|
| 32 |
/** |
| 33 |
* LP_Abstract_Post_Data constructor. |
| 34 |
* |
| 35 |
* @param mixed $post |
| 36 |
* @param array $args |
| 37 |
* |
| 38 |
* @since 3.0.0 |
| 39 |
*/ |
| 40 |
public function __construct( $post, $args = null ) { |
| 41 |
$id = 0; |
| 42 |
if ( is_numeric( $post ) ) { |
| 43 |
$id = absint( $post ); |
| 44 |
} elseif ( $post instanceof LP_Abstract_Post_Data ) { |
| 45 |
$id = absint( $post->get_id() ); |
| 46 |
} elseif ( isset( $post->ID ) ) { |
| 47 |
$id = absint( $post->ID ); |
| 48 |
} |
| 49 |
|
| 50 |
settype( $args, 'array' ); |
| 51 |
$args['id'] = $id; |
| 52 |
parent::__construct( $args ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get status of post. |
| 57 |
* |
| 58 |
* @return array|mixed |
| 59 |
* @since 3.0.0 |
| 60 |
*/ |
| 61 |
public function get_status() { |
| 62 |
return $this->get_data( 'status' ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Check if the post of this instance is exists. |
| 67 |
* |
| 68 |
* @return bool |
| 69 |
* @since 3.0.0 |
| 70 |
*/ |
| 71 |
public function is_exists() { |
| 72 |
return get_post_type( $this->get_id() ) === $this->_post_type; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Check if the post in trash. |
| 77 |
* |
| 78 |
* @return bool |
| 79 |
* @since 3.0.0 |
| 80 |
*/ |
| 81 |
public function is_trashed() { |
| 82 |
return get_post_status( $this->get_id() ) === 'trash'; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check if the post is publish. |
| 87 |
* |
| 88 |
* @return mixed |
| 89 |
* @since 3.0.0 |
| 90 |
*/ |
| 91 |
public function is_publish() { |
| 92 |
return apply_filters( |
| 93 |
'learn-press/' . $this->_post_type . '/is-publish', |
| 94 |
get_post_status( $this->get_id() ) === 'publish' |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get the title. |
| 100 |
* |
| 101 |
* @param string $context |
| 102 |
* |
| 103 |
* @return string |
| 104 |
* @since 3.0.0 |
| 105 |
*/ |
| 106 |
public function get_title( $context = '' ) { |
| 107 |
$title = get_the_title( $this->get_id() ); |
| 108 |
|
| 109 |
if ( 'display' === $context ) { |
| 110 |
$title = do_shortcode( $title ); |
| 111 |
} |
| 112 |
|
| 113 |
return $title; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get the content of course, course's item |
| 118 |
* |
| 119 |
* @param string $context |
| 120 |
* @param int $length |
| 121 |
* @param string $more |
| 122 |
* |
| 123 |
* @return string |
| 124 |
* @since 4.0.0 |
| 125 |
* @editor tungnx |
| 126 |
* todo: should rewrite this |
| 127 |
* @reason Current all post type is item of course ex: lesson, quiz... use same page is course single |
| 128 |
*/ |
| 129 |
public function get_content( $context = 'display', $length = - 1, $more = '' ) { |
| 130 |
if ( 'display' === $context ) { |
| 131 |
if ( ! $this->_content ) { |
| 132 |
global $post, $wp_query; |
| 133 |
|
| 134 |
// Post not preview |
| 135 |
if ( ! isset( $_REQUEST['preview'] ) ) { |
| 136 |
// Fix style, js if content is WP Bakery |
| 137 |
if ( class_exists( 'WPBMap' ) && method_exists( 'WPBMap', 'addAllMappedShortcodes' ) ) { |
| 138 |
WPBMap::addAllMappedShortcodes(); |
| 139 |
} |
| 140 |
|
| 141 |
$post = get_post( $this->get_id() ); |
| 142 |
|
| 143 |
setup_postdata( $post ); |
| 144 |
} |
| 145 |
|
| 146 |
$content_post = get_the_content(); |
| 147 |
$content_post = apply_filters( 'the_content', $content_post ); |
| 148 |
$content_post = str_replace( ']]>', ']]>', $content_post ); |
| 149 |
$this->_content = $content_post; |
| 150 |
wp_reset_postdata(); |
| 151 |
} |
| 152 |
} else { |
| 153 |
$content = get_post_field( 'post_content', $this->get_id() ); |
| 154 |
if ( $length > 1 ) { |
| 155 |
$content = wp_trim_words( $content, $length, $more ); |
| 156 |
} |
| 157 |
|
| 158 |
return $content; |
| 159 |
} |
| 160 |
|
| 161 |
return $this->_content; |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* Get post status. |
| 166 |
* |
| 167 |
* @since 3.0.0 |
| 168 |
*/ |
| 169 |
public function get_post_status() { |
| 170 |
return get_post_status( $this->get_id() ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Get post type. |
| 175 |
* |
| 176 |
* @return false|string |
| 177 |
* @since 3.0.0 |
| 178 |
*/ |
| 179 |
public function get_post_type() { |
| 180 |
return get_post_type( $this->get_id() ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Get default post meta. |
| 185 |
* |
| 186 |
* @return array |
| 187 |
* @since 3.0.0 |
| 188 |
*/ |
| 189 |
public static function get_default_meta() { |
| 190 |
return array(); |
| 191 |
} |
| 192 |
|
| 193 |
public function get_edit_link() { |
| 194 |
return get_edit_post_link( $this->get_id() ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Check user can edit item |
| 199 |
* |
| 200 |
* @return bool |
| 201 |
*/ |
| 202 |
public function current_user_can_edit(): bool { |
| 203 |
return learn_press_get_current_user()->can_edit( $this->get_id() ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get post meta data. |
| 208 |
* Check if the meta is not stored on database then return FALSE |
| 209 |
* |
| 210 |
* @updated 3.1.0 |
| 211 |
* |
| 212 |
* @param string $key |
| 213 |
* @param bool $single |
| 214 |
* |
| 215 |
* @return mixed |
| 216 |
*/ |
| 217 |
public function get_meta( $key, $single = true ) { |
| 218 |
if ( ! metadata_exists( 'post', $this->get_id(), $key ) ) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
return get_post_meta( $this->get_id(), $key, $single ); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|