PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.1
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / abstracts / abstract-post-data.php

abstract-post-data.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9.1, at inc/abstracts/abstract-post-data.php

240 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $posts = apply_filters_ref_array(
137 'the_posts',
138 array(
139 array( get_post( $this->get_id() ) ),
140 &$wp_query,
141 )
142 );
143
144 // Fix style, js if content is WP Bakery
145 if ( class_exists( 'WPBMap' ) && method_exists( 'WPBMap', 'addAllMappedShortcodes' ) ) {
146 WPBMap::addAllMappedShortcodes();
147 }
148
149 if ( $posts ) {
150 $post = $posts[0];
151 }
152
153 setup_postdata( $post );
154 $content_post = get_the_content();
155 $content_post = apply_filters( 'the_content', $content_post );
156 $content_post = str_replace( ']]>', ']]&gt;', $content_post );
157 $this->_content = $content_post;
158 wp_reset_postdata();
159 } else { // Post is previewd
160 $content_post = get_the_content();
161 $content_post = apply_filters( 'the_content', $content_post );
162 $content_post = str_replace( ']]>', ']]&gt;', $content_post );
163 $this->_content = $content_post;
164 }
165 }
166 } else {
167 $content = get_post_field( 'post_content', $this->get_id() );
168 if ( $length > 1 ) {
169 $content = wp_trim_words( $content, $length, $more );
170 }
171
172 return $content;
173 }
174
175 return $this->_content;
176 }
177
178 /*
179 * Get post status.
180 *
181 * @since 3.0.0
182 */
183 public function get_post_status() {
184 return get_post_status( $this->get_id() );
185 }
186
187 /**
188 * Get post type.
189 *
190 * @return false|string
191 * @since 3.0.0
192 */
193 public function get_post_type() {
194 return get_post_type( $this->get_id() );
195 }
196
197 /**
198 * Get default post meta.
199 *
200 * @return array
201 * @since 3.0.0
202 */
203 public static function get_default_meta() {
204 return array();
205 }
206
207 public function get_edit_link() {
208 return get_edit_post_link( $this->get_id() );
209 }
210
211 /**
212 * Check user can edit item
213 *
214 * @return bool
215 */
216 public function current_user_can_edit(): bool {
217 return learn_press_get_current_user()->can_edit( $this->get_id() );
218 }
219
220 /**
221 * Get post meta data.
222 * Check if the meta is not stored on database then return FALSE
223 *
224 * @updated 3.1.0
225 *
226 * @param string $key
227 * @param bool $single
228 *
229 * @return mixed
230 */
231 public function get_meta( $key, $single = true ) {
232 if ( ! metadata_exists( 'post', $this->get_id(), $key ) ) {
233 return false;
234 }
235
236 return get_post_meta( $this->get_id(), $key, $single );
237 }
238 }
239 }
240