PluginProbe
Cool Timeline (Horizontal & Vertical Timeline) / trunk
Cool Timeline (Horizontal & Vertical Timeline) vtrunk
3.4.0 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 2.2.3 2.3.3 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.6.1 2.7.1 2.8.3 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 All 79 releases
cool-timeline / includes / shortcodes / class-ctl-shortcode-preview.php

class-ctl-shortcode-preview.php in Cool Timeline (Horizontal & Vertical Timeline) trunk, at includes/shortcodes/class-ctl-shortcode-preview.php

242 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if not in the admin area or if accessed directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 if ( ! is_admin() || ! is_user_logged_in() ) {
9 exit;
10 }
11
12 if ( ! check_ajax_referer( 'ctl_preview', 'nonce', false ) ) {
13 wp_die( '0', 400 );
14 exit;
15 }
16
17 if ( ! class_exists( 'CTL_Shortcode_Preview' ) ) {
18 /**
19 * CTL Preview Assets Class.
20 */
21 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound
22 class CTL_Shortcode_Preview {
23 /**
24 * Member Variable
25 *
26 * @var string
27 */
28 private $shortcode = '';
29
30 /**
31 * Member Variable
32 *
33 * @var string
34 */
35 private $designs = '';
36
37 /**
38 * Member Variable
39 *
40 * @var object
41 */
42 private $assets_object = array();
43
44 /**
45 * Constructor function
46 *
47 * @param string $data timeline design.
48 */
49 public function __construct( $data ) {
50 $this->ctl_create_shortcode( $data );
51 $this->assets_loading();
52 }
53
54 /**
55 * Makte Preview Shortcode.
56 *
57 * @param array $data shortcode array.
58 */
59 public function ctl_create_shortcode( $data ) {
60
61 if ( ! is_array( $data ) ) {
62 $data = array();
63 }
64
65 $allowed_shortcode_tag = 'cool-timeline';
66 $incoming_type = isset( $data['shortcodeType'] ) && is_string( $data['shortcodeType'] )
67 ? sanitize_key( $data['shortcodeType'] )
68 : '';
69
70 if ( $allowed_shortcode_tag !== $incoming_type ) {
71 $data['shortcodeType'] = $allowed_shortcode_tag;
72 }
73
74 $shortcode_type = $this->ctl_shortcode_filter( $data['shortcodeType'] );
75 $shortcode = '[' . $shortcode_type;
76 foreach ( $data as $key => $value ) {
77 $shortcode_key = $this->ctl_shortcode_filter( $key );
78 if ( 'date-format' === $key || 'custom-date-format' === $key || 'timeline-title' === $key ) {
79 $shortcode_value = sanitize_text_field( $value );
80 } elseif ( 'pagination' === $key && isset( $data['layout'] ) && 'horizontal' === $data['layout'] ) {
81 $shortcode_value = esc_html( 'none' );
82 } else {
83 $shortcode_value = $this->ctl_shortcode_filter( $value );
84 }
85
86 if ( 'shortcodeType' !== $key ) {
87 $shortcode .= " $shortcode_key=\"$shortcode_value\"";
88 }
89
90 if ( 'layout' === $key ) {
91 $this->designs = $shortcode_value;
92 }
93 }
94
95 $shortcode .= ']';
96
97 $this->shortcode = $shortcode;
98 }
99
100 /**
101 * Assets Loading Classes Call
102 */
103 public function assets_loading() {
104 // Timeline Settings Custom styling.
105 $this->ctl_custom_style();
106 // Timeline Css files path.
107 $this->ctl_preview_css();
108 // Timeline Script files path.
109 $this->ctl_preview_script();
110 }
111
112 /**
113 * Timeline Admin style settings.
114 */
115 public function ctl_custom_style() {
116 require_once CTL_PLUGIN_DIR . 'includes/shortcodes/class-ctl-styles-generator.php';
117 $ctl_options_arr = get_option( 'cool_timeline_settings' );
118 $custom_style = new CTL_Styles_Generator();
119 $color_style = $custom_style::styles_settings_vars( $ctl_options_arr );
120 $style = $custom_style::render_global_style( $color_style );
121 $style .= $custom_style::ctl_global_typography( $ctl_options_arr );
122 $style .= $this->ctl_preview_custom_css();
123 $custom_css = isset( $ctl_options_arr['custom_styles'] ) ? $custom_style::sanitize_custom_css( $ctl_options_arr['custom_styles'] ) : '';
124 $final_css = $custom_style::clt_minify_css( $style );
125 $this->assets_object['custom_style'] = $custom_css . ' ' . $final_css;
126 }
127
128 /**
129 * Shortcode Preview Custom CSs.
130 */
131 public function ctl_preview_custom_css() {
132 $style = '.ctl-wrapper {
133 max-width: calc(100% - 200px) !important;
134 margin: 0 auto 30px !important;
135 }
136 .ctl-wrapper .ctp-media-slider img{
137 width: 100% !important
138 }
139 .ctl-wrapper .ctl-slider-wrapper,.ctl-wrapper .ctl-story,.ctl_load_more_pagination,.ctl-category-container,.ctl-navigation-bar{
140 pointer-events: none
141 }';
142 return $style;
143 }
144
145 /**
146 * Shortcode Preview CSS Files.
147 */
148 public function ctl_preview_css() {
149 $file_names = array( 'swiper.css', 'ctl-common-styles.min.css' );
150 if ( 'horizontal' === $this->designs ) {
151 array_push( $file_names, 'ctl-horizontal-timeline.min.css' );
152 } elseif ( 'compact' === $this->designs ) {
153 array_push( $file_names, 'ctl-compact-style.min.css' );
154 array_push( $file_names, 'ctl-vertical-timeline.min.css' );
155 } else {
156 array_push( $file_names, 'ctl-vertical-timeline.min.css' );
157 }
158
159 $this->assets_object['style'] = $this->files_path( $file_names, 'css' );
160 }
161
162 /**
163 * Shortcode Preview Script Files.
164 */
165 public function ctl_preview_script() {
166 $file_names = array( 'ctl-scripts.min.js' );
167 if ( 'horizontal' === $this->designs ) {
168 array_push( $file_names, 'swiper.js' );
169 array_push( $file_names, 'ctl-horizontal.min.js' );
170 } elseif ( 'compact' === $this->designs ) {
171 array_push( $file_names, 'ctl-compact.min.js' );
172 }
173
174 $scripts = $this->files_path( $file_names, 'js' );
175 // Compact layout: load WordPress core Masonry before compact script.
176 if ( 'compact' === $this->designs ) {
177 array_splice( $scripts, 1, 0, array( includes_url( 'js/masonry.min.js' ) ) );
178 }
179 $this->assets_object['script'] = $scripts;
180 }
181
182 /**
183 * Return both CSS and JS Full file path.
184 *
185 * @param array $files files names.
186 * @param string $type file type css or js.
187 */
188 public function files_path( $files, $type ) {
189 $files_arr = array();
190 $dir = 'css' === $type ? 'includes/shortcodes/assets/css/' : 'includes/shortcodes/assets/js/';
191 foreach ( $files as $file ) {
192 $file_path = CTL_PLUGIN_URL . $dir . $file;
193 array_push( $files_arr, $file_path );
194 }
195 return $files_arr;
196 }
197
198 /**
199 * Filter All Shortocde Attribute
200 *
201 * @param string $data shortcode attribute value.
202 * @return string filtered attribute value.
203 */
204 public function ctl_shortcode_filter( $data ) {
205 // Define symbols to be removed from the attribute value.
206 $symbols = array( '*', '(', ')', '[', ']', '{', '}', '"', "'", '\\', '/', ';', '$', '<', '>', '.', '', '#', '!', '@', '^' );
207
208 // Remove specified symbols from the attribute value.
209 $value = str_replace( $symbols, '', $data );
210
211 // Escape HTML entities in the attribute value.
212 $value = esc_html( $value );
213
214 // Remove white spaces from the attribute value.
215 $value = preg_replace( '/\s+/', '', $value );
216
217 // Removes slashes from a string or recursively removes slashes from strings within an array.
218 $value = wp_unslash( $value );
219
220 // Sanitize the final value to ensure it's safe for output.
221 $filter_value = sanitize_text_field( $value );
222
223 // Return the filtered attribute value.
224 return $filter_value;
225 }
226
227 /**
228 * Preivew Shortcode return
229 */
230 public function ctl_preview_shortcode() {
231 return $this->shortcode;
232 }
233
234 /**
235 * Return files path object.
236 */
237 public function assets_obj() {
238 return $this->assets_object;
239 }
240 }
241 }
242