PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.2
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.2
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / public / helpers / class-post-functions.php

class-post-functions.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.2, at public/helpers/class-post-functions.php

266 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file functions.
4 *
5 * @package Smart_Post_Show
6 * @subpackage public
7 *
8 * @since 2.2.0
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 /**
16 * Post views helper method.
17 *
18 * @since 2.2.0
19 */
20 class SP_PC_Functions {
21 /**
22 * WP Version compare
23 *
24 * @param int $version_to_compare The WP version.
25 * @param string $operator Compare operator.
26 * @return mixed
27 */
28 public static function wp_version_compare( $version_to_compare, $operator = '>=' ) {
29 if ( empty( $version_to_compare ) ) {
30 return true;
31 }
32 global $wp_version;
33
34 // Check if using WordPress version 3.7 or higher.
35 return version_compare( $wp_version, $version_to_compare, $operator );
36 }
37
38 /**
39 * Tag name to full tag conversion.
40 *
41 * @param array $meta_tag Tag option.
42 * @return string
43 */
44 public static function short_tag_to_html( $meta_tag ) {
45 $exclude_tag_string = '';
46 foreach ( $meta_tag as $key => $value ) {
47 $exclude_tag_string .= '<' . $value . '>,';
48 }
49 return $exclude_tag_string;
50 }
51
52 /**
53 * Content function.
54 *
55 * @param array $view_options Read more options array.
56 * @param string $type Content type.
57 * @return return
58 */
59 public static function pcp_content( $view_options, $type ) {
60 global $wp_embed;
61 $post_content_ellipsis = isset( $view_options['post_content_ellipsis'] ) ? $view_options['post_content_ellipsis'] : '';
62 $pcp_strip_tags = isset( $view_options['pcp_strip_tags'] ) ? $view_options['pcp_strip_tags'] : '';
63 $pcp_allow_tag_name = isset( $view_options['pcp_allow_tag_name'] ) ? $view_options['pcp_allow_tag_name'] : '';
64 $allowed_tags = explode( ',', $pcp_allow_tag_name );
65 if ( 'full_content' === $type ) {
66 $pcp_post_content = get_the_content();
67 } else {
68 $pcp_post_content = get_the_excerpt();
69 }
70 $pcp_post_content = do_shortcode( $wp_embed->autoembed( wpautop( trim( $pcp_post_content ) ) ) );
71 return $pcp_post_content;
72 }
73
74 /**
75 * Thumbnail alter text
76 *
77 * @param integer $slide_id The slide/post ID.
78 *
79 * @return string
80 */
81 public static function pcp_thumb_alter_text( $slide_id ) {
82 $image_id = get_post_thumbnail_id( $slide_id );
83 $alt_text = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
84 return $alt_text;
85 }
86
87 /**
88 * Thumb Sized function
89 *
90 * @param array $post_thumb_setting Thumbnails options array.
91 * @param integer $slide_id The slide/post ID.
92 *
93 * @return string
94 */
95 public static function pcp_sized_thumb( $post_thumb_setting, $slide_id ) {
96 $thumb_id = '';
97 $image = '';
98 if ( has_post_thumbnail( $slide_id ) ) {
99 $thumb_id = get_post_thumbnail_id();
100 }
101
102 $placeholder_img = SP_PC_URL . 'public/assets/img/placeholder.png';
103 $placeholder_img = apply_filters( 'pcp_no_thumb_placeholder', $placeholder_img );
104 if ( empty( $thumb_id ) && ! empty( $placeholder_img ) ) {
105 $thumb_id = attachment_url_to_postid( $placeholder_img );
106 }
107
108 if ( ! empty( $thumb_id ) ) {
109 $image_sizes = isset( $post_thumb_setting['pcp_thumb_sizes'] ) ? $post_thumb_setting['pcp_thumb_sizes'] : '';
110 $image_src = wp_get_attachment_image_src( $thumb_id, $image_sizes );
111 $image_src = is_array( $image_src ) ? $image_src : array( '', '', '' );
112 $image = $image_src[0];
113 $image_width = $image_src[1];
114 $image_height = $image_src[2];
115 } elseif ( ! empty( $placeholder_img ) ) {
116 $image = $placeholder_img;
117 $image_width = 600;
118 $image_height = 450;
119 }
120 $pcp_image_attr = array(
121 'src' => $image,
122 'width' => $image_width,
123 'height' => $image_height,
124 );
125 return $pcp_image_attr;
126 }
127
128 /**
129 * Process all the post meta.
130 *
131 * @param array $post_meta_fields The selected post meta to show.
132 * @param string $meta_separator The post meta separator.
133 * @return void
134 */
135 public static function pcp_get_post_meta( $post_meta_fields, $meta_separator ) {
136 $i = 0;
137 foreach ( $post_meta_fields as $each_meta ) {
138 $selected_meta = $each_meta['select_post_meta'];
139 $meta_icon = ! empty( $each_meta['select_meta_icon'] ) ? sprintf( '<i class="' . $each_meta['select_meta_icon'] . '"></i>' ) : '';
140 $meta_tag_start = apply_filters( 'pcp_post_meta_html_tag_start', '<li>' );
141 $meta_tag_end = apply_filters( 'pcp_post_meta_html_tag_end', '</li>' );
142
143 switch ( $selected_meta ) {
144 case 'author':
145 if ( 0 < $i ) {
146 echo esc_html( $meta_separator );
147 }
148 echo wp_kses_post( $meta_tag_start );
149 ?>
150 <i class="fa fa-user"></i>
151 <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"
152 rel="author"><?php echo esc_html( get_the_author() ); ?></a>
153 <?php
154 echo wp_kses_post( $meta_tag_end );
155 break;
156 case 'date':
157 if ( 0 < $i ) {
158 echo esc_html( $meta_separator );
159 }
160 echo wp_kses_post( $meta_tag_start );
161 ?>
162 <i class="fa fa-calendar"></i>
163 <time class="entry-date published updated"><?php echo esc_html( get_the_date() ); ?></time>
164 <?php
165 echo wp_kses_post( $meta_tag_end );
166 break;
167 case 'comment_count':
168 if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
169 if ( 0 < $i ) {
170 echo esc_html( $meta_separator );
171 }
172 echo wp_kses_post( $meta_tag_start );
173 ?>
174 <i class="fa fa-comments-o"></i>
175 <a href="<?php the_permalink(); ?>"><?php echo esc_html( get_comments_number() ); ?></a>
176 <?php
177 echo wp_kses_post( $meta_tag_end );
178 }
179 break;
180 }
181 ++$i;
182 } // End Foreach.
183 }
184
185 /**
186 * Maximum pages.
187 *
188 * @param int $total_post Number of total posts.
189 * @param int $post_per_page Posts per page.
190 *
191 * @return void
192 */
193 public static function pcp_max_pages( $total_post, $post_per_page ) {
194 if ( ! $total_post ) {
195 return;
196 }
197 $max_num_pages = ceil( $total_post / $post_per_page );
198 return (int) $max_num_pages;
199 }
200
201 /**
202 * Post per page.
203 *
204 * @param int $limit Post Limit.
205 * @param int $post_per_page post per page.
206 * @param int $page paged number.
207 *
208 * @return int
209 */
210 public static function pcp_post_per_page( $limit, $post_per_page, $page ) {
211 $limit = ( empty( $limit ) || '-1' === $limit ) ? 1000000 : $limit;
212 $offset = (int) $post_per_page * ( $page - 1 );
213 $final_post_per_page = $post_per_page;
214 if ( intval( $post_per_page ) > $limit - $offset ) {
215 $final_post_per_page = $limit - $offset;
216 }
217 return $final_post_per_page;
218 }
219
220 /**
221 * Pagination last page post
222 *
223 * @param int $limit total post limit.
224 * @param int $post_per_page post per page.
225 * @param int $total_page last post page.
226 *
227 * @return int.
228 */
229 public static function pcp_last_page_post( $limit, $post_per_page, $total_page ) {
230 $limit = ( empty( $limit ) || '-1' === $limit ) ? 10000000 : $limit;
231 $offset = $post_per_page * ( $total_page - 1 );
232 $pcp_last_page_post = $limit - $offset;
233 return $pcp_last_page_post;
234 }
235
236 /**
237 * Get view option from view ID
238 *
239 * @param string $shortcode_id ID of custom field.
240 *
241 * @return array
242 */
243 public static function view_options( $shortcode_id ) {
244 if ( ! $shortcode_id ) {
245 return;
246 }
247 $view_options = get_post_meta( $shortcode_id, 'sp_pcp_view_options', true );
248 return $view_options;
249 }
250
251 /**
252 * Get value of a setting from global settings array
253 *
254 * @param string $field The full name of setting to get value.
255 * @param array $array_to_get Array to get values of wanted setting.
256 * @param mixed|null $assign The value to assign if setting is not found.
257 */
258 public static function pcp_metabox_value( $field, $array_to_get = null, $assign = null ) {
259 global $shortcode_id;
260 if ( empty( $array_to_get ) ) {
261 $array_to_get = self::view_options( $shortcode_id );
262 }
263 return isset( $array_to_get[ $field ] ) ? $array_to_get[ $field ] : $assign;
264 }
265 }
266