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

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