PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.20
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.20
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 / class-smart-post-show-public.php

class-smart-post-show-public.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 2.4.20, at public/class-smart-post-show-public.php

280 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * File for the public facing functions.
4 *
5 * @link https://smartpostshow.com/
6 * @since 2.2.0
7 *
8 * @package Smart_Post_Show
9 * @subpackage Smart_Post_Show/public
10 */
11
12 /**
13 * The public-facing functionality of the plugin.
14 */
15 class Smart_Post_Show_Public {
16
17 /**
18 * Script and style suffix
19 *
20 * @since 2.2.0
21 * @access protected
22 * @var string
23 */
24 protected $suffix;
25
26 /**
27 * Initialize the class and set its properties.
28 *
29 * @since 2.2.0
30 */
31 public function __construct() {
32 $this->load_public_dependencies();
33 $this->pcp_public_action();
34 }
35
36 /**
37 * Public dependencies.
38 *
39 * @return void
40 */
41 private function load_public_dependencies() {
42 require_once SP_PC_PATH . 'public/helpers/class-post-functions.php';
43 require_once SP_PC_PATH . 'public/helpers/class-pcp-queryinside.php';
44 require_once SP_PC_PATH . 'public/template/loop/class-loop-html.php';
45 }
46
47 /**
48 * Public Action
49 *
50 * @return void
51 */
52 private function pcp_public_action() {
53 add_shortcode( 'smart_post_show', array( $this, 'pcp_shortcode_render' ) );
54 add_shortcode( 'sp_postcarousel', array( $this, 'pcp_shortcode_render' ) );
55 add_shortcode( 'post-carousel', array( $this, 'pcp_shortcode_render' ) );
56 add_action( 'save_post', array( $this, 'delete_page_sp_pcp_option_on_save' ) );
57
58 $this->suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? '' : '.min';
59 }
60
61 /**
62 * Minify output
63 *
64 * @param statement $html output.
65 * @return statement
66 */
67 public static function minify_output( $html ) {
68 $html = preg_replace( '/<!--(?!s*(?:[if [^]]+]|!|>))(?:(?!-->).)*-->/s', '', $html );
69 $html = str_replace( array( "\r\n", "\r", "\n", "\t" ), '', $html );
70 while ( stristr( $html, ' ' ) ) {
71 $html = str_replace( ' ', ' ', $html );
72 }
73 return $html;
74 }
75
76 /**
77 * Register the stylesheets for the public-facing side of the site.
78 *
79 * @since 2.2.0
80 */
81 public function enqueue_styles() {
82 // Get the existing shortcode ids from the current page.
83 $get_page_data = self::get_page_data();
84 $found_shortcode_id = $get_page_data['generator_id'];
85 // Check shortcode ids are exist in the current page and then enqueue all styles.
86 if ( $found_shortcode_id ) {
87 wp_enqueue_style( 'font-awesome' );
88 wp_enqueue_style( 'pcp_swiper' );
89 wp_enqueue_style( 'pcp-style' );
90 $dynamic_style = self::load_dynamic_style( $found_shortcode_id );
91 wp_add_inline_style( 'pcp-style', $dynamic_style['dynamic_css'] );
92 }
93 }
94
95 /**
96 * Function get layout from attrs and create class depending on it.
97 *
98 * @since 2.0
99 * @param array $attribute attribute of this shortcode.
100 */
101 public function pcp_shortcode_render( $attribute ) {
102 $shortcode_id = esc_attr( intval( $attribute['id'] ) );
103 // Check the shortcode existence and status.
104 if ( empty( $shortcode_id ) || 'sp_post_carousel' !== get_post_type( $shortcode_id ) || 'trash' === get_post_status( $shortcode_id ) ) {
105 return;
106 }
107 // Preset Layouts.
108 $layout = get_post_meta( $shortcode_id, 'sp_pcp_layouts', true );
109 // All the visible options for the Shortcode like – Global, Filter, Display, Popup, Typography etc.
110 $view_options = get_post_meta( $shortcode_id, 'sp_pcp_view_options', true );
111 $section_title = get_the_title( $shortcode_id );
112
113 // Get the existing shortcode id from the current page.
114 $get_page_data = self::get_page_data();
115 $found_shortcode_id = $get_page_data['generator_id'];
116 ob_start();
117 // Check if shortcode and page ids are not exist in the current page then enqueue the stylesheet.
118 if ( ! is_array( $found_shortcode_id ) || ! $found_shortcode_id || ! in_array( $shortcode_id, $found_shortcode_id ) ) {
119 wp_enqueue_style( 'font-awesome' );
120 wp_enqueue_style( 'pcp_swiper' );
121 wp_enqueue_style( 'pcp-style' );
122 $dynamic_style = self::load_dynamic_style( $shortcode_id, $view_options, $layout );
123 // Add dynamic style.
124 echo '<style id="sp_pcp_dynamic_style' . esc_attr( $shortcode_id ) . '">' . $dynamic_style['dynamic_css'] . '</style>'; // phpcs:ignore
125 }
126 // Update options if the existing shortcode id option not found.
127 self::sps_db_options_update( $shortcode_id, $get_page_data );
128 SP_PC_Output::pc_html_show( $view_options, $layout, $shortcode_id, $section_title );
129 return ob_get_clean();
130 }
131
132 /**
133 * Register all Styles and Scripts for the public-facing side of the site.
134 *
135 * @since 2.4.17
136 */
137 public function register_all_scripts() {
138 $pcp_settings = get_option( 'sp_post_carousel_settings' );
139 /**
140 * Register all the styles for the public-facing side of the site.
141 */
142 if ( $pcp_settings['pcp_fontawesome_css'] ) {
143 wp_register_style( 'font-awesome', SP_PC_URL . 'public/assets/css/font-awesome.min.css', array(), SP_PC_VERSION, 'all' );
144 }
145 if ( $pcp_settings['pcp_swiper_css'] ) {
146 wp_register_style( 'pcp_swiper', SP_PC_URL . 'public/assets/css/swiper-bundle' . $this->suffix . '.css', array(), SP_PC_VERSION, 'all' );
147 }
148 wp_register_style( 'pcp-style', SP_PC_URL . 'public/assets/css/style' . $this->suffix . '.css', array(), SP_PC_VERSION, 'all' );
149
150 /**
151 * Register all the Scripts for the public-facing side of the site.
152 */
153 wp_register_script( 'pcp_swiper', SP_PC_URL . 'public/assets/js/swiper-bundle' . $this->suffix . '.js', array( 'jquery' ), SP_PC_VERSION, true );
154 wp_register_script( 'pcp_script', SP_PC_URL . 'public/assets/js/scripts' . $this->suffix . '.js', array( 'jquery' ), SP_PC_VERSION, true );
155
156 wp_localize_script(
157 'pcp_script',
158 'pcp_vars',
159 array(
160 'swiperEnqueue' => $pcp_settings['pcp_swiper_js'],
161 )
162 );
163 }
164
165 /**
166 * Gets the existing shortcode-id, page-id and option-key from the current page.
167 *
168 * @return array
169 */
170 public static function get_page_data() {
171 $current_page_id = get_queried_object_id();
172 $option_key = 'sp_pcp_page_id' . $current_page_id;
173 $found_generator_id = get_option( $option_key );
174 if ( is_multisite() ) {
175 $option_key = 'sp_pcp_page_id' . get_current_blog_id() . $current_page_id;
176 $found_generator_id = get_site_option( $option_key );
177 }
178 $get_page_data = array(
179 'page_id' => $current_page_id,
180 'generator_id' => $found_generator_id,
181 'option_key' => $option_key,
182 );
183 return $get_page_data;
184 }
185
186 /**
187 * If the option does not exist, it will be created.
188 *
189 * It will be serialized before it is inserted into the database.
190 *
191 * @param string $post_id existing shortcode id.
192 * @param array $get_page_data get current page-id, shortcode-id and option-key from the the current page.
193 * @return void
194 */
195 public static function sps_db_options_update( $post_id, $get_page_data ) {
196 $found_generator_id = $get_page_data['generator_id'];
197 $option_key = $get_page_data['option_key'];
198 $current_page_id = $get_page_data['page_id'];
199 if ( $found_generator_id ) {
200 $found_generator_id = is_array( $found_generator_id ) ? $found_generator_id : array( $found_generator_id );
201 if ( ! in_array( $post_id, $found_generator_id ) || empty( $found_generator_id ) ) {
202 // If not found the shortcode id in the page options.
203 array_push( $found_generator_id, $post_id );
204 if ( is_multisite() ) {
205 update_site_option( $option_key, $found_generator_id );
206 } else {
207 update_option( $option_key, $found_generator_id );
208 }
209 }
210 } else {
211 // If option not set in current page add option.
212 if ( $current_page_id ) {
213 if ( is_multisite() ) {
214 add_site_option( $option_key, array( $post_id ) );
215 } else {
216 add_option( $option_key, array( $post_id ) );
217 }
218 }
219 }
220 }
221
222 /**
223 * Load dynamic style of the existing shortcode id.
224 *
225 * @param mixed $found_generator_id to push id option for getting how many shortcode in the page.
226 * @param mixed $view_options shortcode options.
227 * @param mixed $layout all layout based options.
228 * @return array dynamic style use in the existing shortcodes in the current page.
229 */
230 public static function load_dynamic_style( $found_generator_id, $view_options = '', $layout = '' ) {
231 $pcp_settings = get_option( 'sp_post_carousel_settings' );
232 $custom_css = '';
233 // If multiple shortcode found in the current page.
234 if ( is_array( $found_generator_id ) ) {
235 foreach ( $found_generator_id as $pcp_id ) {
236 if ( $pcp_id && is_numeric( $pcp_id ) && get_post_status( $pcp_id ) !== 'trash' ) {
237 $view_options = get_post_meta( $pcp_id, 'sp_pcp_view_options', true );
238 $layout = get_post_meta( $pcp_id, 'sp_pcp_layouts', true );
239 include SP_PC_PATH . 'public/dynamic-css/dynamic-css.php';
240 }
241 }
242 } else {
243 // If single shortcode found in the current page.
244 $pcp_id = $found_generator_id;
245 include SP_PC_PATH . 'public/dynamic-css/dynamic-css.php';
246 }
247 // Custom css merge with dynamic style.
248 $pcp_custom_css = isset( $pcp_settings['pcp_custom_css'] ) ? trim( html_entity_decode( $pcp_settings['pcp_custom_css'] ) ) : '';
249 if ( ! empty( $pcp_custom_css ) ) {
250 $custom_css .= $pcp_custom_css;
251 }
252 $dynamic_style = array(
253 'dynamic_css' => self::minify_output( $custom_css ),
254 );
255 return $dynamic_style;
256 }
257
258 /**
259 * Delete page shortcode ids array option on save
260 *
261 * @param int $post_ID current post id.
262 * @return void
263 *
264 * @since 2.4.17
265 */
266 public function delete_page_sp_pcp_option_on_save( $post_ID ) {
267 if ( is_multisite() ) {
268 $option_key = 'sp_pcp_page_id' . get_current_blog_id() . $post_ID;
269 if ( get_site_option( $option_key ) ) {
270 delete_site_option( $option_key );
271 }
272 } else {
273 if ( get_option( 'sp_pcp_page_id' . $post_ID ) ) {
274 delete_option( 'sp_pcp_page_id' . $post_ID );
275 }
276 }
277 }
278
279 }
280