PluginProbe ʕ •ᴥ•ʔ
DearFlip – PDF Flipbook, 3D Flipbook, PDF embed, PDF viewer / 2.2.56
DearFlip – PDF Flipbook, 3D Flipbook, PDF embed, PDF viewer v2.2.56
2.4.30 trunk 1.7.16 1.7.35 2.0.86 2.2.32 2.2.39 2.2.41 2.2.47 2.2.54 2.2.55 2.2.56 2.3.24 2.3.32 2.3.52 2.3.53 2.3.54 2.3.55 2.3.57 2.3.74 2.3.75 2.4.20 2.4.27
3d-flipbook-dflip-lite / inc / shortcode.php
3d-flipbook-dflip-lite / inc Last commit date
index.php 1 year ago metaboxes.php 1 year ago post-type.php 1 year ago settings.php 1 year ago shortcode.php 1 year ago
shortcode.php
417 lines
1 <?php
2
3 /**
4 * Created by PhpStorm.
5 * User: Deepak
6 * Date: 5/3/2016
7 * Time: 2:27 PM
8 */
9 class DFlip_ShortCode {
10
11 /**
12 * Holds the singleton class object.
13 *
14 * @since 1.0.0
15 *
16 * @var object
17 */
18 public static $instance;
19
20 /**
21 * Holds the base DFlip class object.
22 *
23 * @since 1.0.0
24 *
25 * @var object
26 */
27 public $base;
28
29 /**
30 * Primary class constructor.
31 *
32 * @since 1.0.0
33 */
34 public function __construct() {
35
36 // Load the base class object.
37 $this->base = DFlip::get_instance();
38
39 // Load shortcode hooks and filters.
40 add_shortcode( 'dflip', array( $this, 'shortcode' ) );
41 // add_shortcode( 'dflip-multi', array( $this, 'dflip_multi_shortcode' ) );
42 }
43
44 /**
45 * Builds the dFlip Shortcode for the plugin
46 *
47 * @param array $attr Attributes of the shortcode.
48 * @param string $content Content of the button or thumb
49 *
50 * @return string HTML content to display image-text.
51 * @since 1.0.0
52 *
53 */
54 public function shortcode( $attr, $content = '' ) {
55
56 if ( $this->base->selective_script_loading == true ) {
57 //enqueue script
58 wp_enqueue_script( $this->base->plugin_slug . '-script' );
59
60 //enqueue styles
61 wp_enqueue_style( $this->base->plugin_slug . '-style' );
62 }
63
64 $ismulti = isset( $attr['books'] ) && trim( $attr['books'] ) !== '';
65 $atts_default = array(
66 'class' => '',
67 'id' => '',
68 'books' => ''
69 );
70 //atts or post defaults
71 $atts = shortcode_atts( $atts_default, $attr, 'dflip' );
72
73 if ( $ismulti ) {
74 $limit = isset( $attr['limit'] ) ? (int) $attr['limit'] : 5;
75 $ids = array();
76 $books = explode( ',', $atts['books'] );
77 foreach ( (array) $books as $query ) {
78 $query = trim( $query );
79 if ( is_numeric( $query ) ) {
80 array_push( $ids, $query );
81 } else {
82 if ( $query == 'all' || $query == '*' ) {
83 $postslist = get_posts( array(
84 'post_type' => 'dflip',
85 'posts_per_page' => - 1,
86 'numberposts' => $limit,
87 'nopaging' => true,
88 'exclude' => $ids
89 ) );
90 foreach ( $postslist as $post ) {
91 array_push( $ids, $post->ID );
92 }
93 } else {
94 $postslist = get_posts( array(
95 'tax_query' => array(
96 array(
97 'taxonomy' => 'dflip_category',
98 'field' => 'slug',
99 'terms' => $query,
100 )
101 ),
102 'post_type' => 'dflip',
103 'posts_per_page' => - 1,
104 'numberposts' => $limit,
105 'nopaging' => true,
106 'exclude' => $ids
107 ) );
108 foreach ( $postslist as $post ) {
109 array_push( $ids, $post->ID );
110 }
111 }
112 }
113 }
114 $html = '<div class="dflip-books">';
115 $limitMax = $limit == '-1' ? 999 : (int) $limit;
116 $limit = 0;
117 foreach ( $ids as $id ) {
118 if ( $limit >= $limitMax ) {
119 break;
120 }
121 $attr['id'] = esc_attr( $id );
122 $html .= $this->book( $attr, $content, true );
123 $limit ++;
124
125 }
126
127 return $html . '</div>';
128
129 } else {
130 return $this->book( $attr, $content );
131 }
132 }
133
134 /**
135 * Helper function for dFlip Shortcode
136 *
137 * @param $attr
138 * @param string $content Content of the button or thumb
139 *
140 * @param bool $multi checks if this is a part of multiple books request
141 *
142 * @return string HTML content to display image-text.
143 * @since 1.0.0
144 *
145 * @internal param array $attr Attributes of the shortcode.
146 */
147 public function book( $attr, $content = '', $multi = false ) {
148 $base = $this->base;
149
150 $atts_default = array(
151 'class' => '',
152 'id' => '',
153 'type' => $multi ? 'thumb' : 'book'
154 );
155
156 //atts or post defaults
157 $atts = shortcode_atts( $atts_default, $attr, 'dflip' );
158 if($atts["type"] === "hidden"){
159 return "";
160 }
161 //in PHP7 if $attr is not an array it causes issue
162 if ( is_array( $attr ) == false ) {
163 $attr = array();
164 }
165 $html_attr = array();
166
167 //default data
168 $id = $atts['id'] === '' ? 'df_rand' . rand() : $atts['id'];
169 $id = sanitize_title($id);
170 $type = $atts['type'];
171 $class = $atts['class'];
172 $title = do_shortcode( $content );
173
174 //get Id
175 $post_id = $id;
176 $hasId = false;
177 $thumb_url = '';
178 $thumb_tag_type = $base->get_config( 'thumb_tag_type' );
179 $share_slug = $base->get_config( 'share_slug' );
180
181 $post_data = array();
182
183 $help_info = '';
184 //pull post data if available for the script part only
185 if ( !empty( $post_id ) && is_numeric( $post_id ) ) {
186
187 /* $post = get_post( $post_id );
188 if ( $post == null ) {
189 return '';
190 }*/
191
192 $id = 'df_' . $post_id;
193
194 $post_meta = get_post_meta( $post_id, '_dflip_data' );
195
196 if ( $title === '' ) {
197 $title = get_the_title( $post_id );
198 }
199
200
201 if ( is_array( $post_meta ) && count( $post_meta ) > 0 ) {
202 $post_data = $post_meta[0];
203 }
204
205 //conversion
206 $post_data['enableDownload'] = isset( $post_data['enable_download'] ) ? $post_data['enable_download'] : null;
207 $post_data['backgroundColor'] = isset( $post_data['bg_color'] ) ? $post_data['bg_color'] : null;
208 $post_data['backgroundImage'] = isset( $post_data['bg_image'] ) ? $post_data['bg_image'] : null;
209 $post_data['autoEnableOutline'] = isset( $post_data['auto_outline'] ) ? $post_data['auto_outline'] : null;
210 $post_data['autoEnableThumbnail'] = isset( $post_data['auto_thumbnail'] ) ? $post_data['auto_thumbnail'] : null;
211 $post_data['overwritePDFOutline'] = isset( $post_data['overwrite_outline'] ) ? $post_data['overwrite_outline'] : null;
212 $post_data['soundEnable'] = isset( $post_data['auto_sound'] ) ? $post_data['auto_sound'] : null;
213 $post_data['maxTextureSize'] = isset( $post_data['texture_size'] ) ? $post_data['texture_size'] : null;
214 $post_data['pageMode'] = isset( $post_data['page_mode'] ) ? $post_data['page_mode'] : null;
215 $post_data['singlePageMode'] = isset( $post_data['single_page_mode'] ) ? $post_data['single_page_mode'] : null;
216 $post_data['pageSize'] = isset( $post_data['page_size'] ) ? $post_data['page_size'] : null;
217 $post_data['controlsPosition'] = isset( $post_data['controls_position'] ) ? $post_data['controls_position'] : null;
218 // $post_data['forceFit'] = isset( $post_data['force_fit'] ) ? $post_data['force_fit'] : null;
219 $post_data['autoPlay'] = isset( $post_data['autoplay'] ) ? $post_data['autoplay'] : null;
220 $post_data['autoPlayDuration'] = isset( $post_data['autoplay_duration'] ) ? $post_data['autoplay_duration'] : null;
221 $post_data['autoPlayStart'] = isset( $post_data['autoplay_start'] ) ? $post_data['autoplay_start'] : null;
222
223
224 $post_defaults = array(
225 'webgl' => $base->get_default( 'webgl' ),
226 'class' => '',
227 'id' => '',
228 //internal
229 'source_type' => $base->get_default( 'source_type' ),
230 'pdf_source' => '',
231 'pdf_thumb' => '',
232 'pages' => array(),
233 'outline' => '',
234 'backgroundColor' => $base->get_default( 'bg_color' ),
235 'height' => $base->get_default( 'height' ),
236 'duration' => $base->get_default( 'duration' ),
237 'hard' => $base->get_default( 'hard' ),
238 // 'forceFit' => $base->get_default( 'force_fit' ),
239 'autoEnableOutline' => $base->get_default( 'auto_outline' ),
240 'autoEnableThumbnail' => $base->get_default( 'auto_thumbnail' ),
241 'overwritePDFOutline' => $base->get_default( 'overwrite_outline' ),
242 'enableDownload' => $base->get_default( 'enable_download' ),
243 'backgroundImage' => $base->get_default( 'bg_image' ),
244 'direction' => $base->get_default( 'direction' ),
245 'pageSize' => $base->get_default( 'page_size' ),
246 'pageMode' => $base->get_default( 'page_mode' ),
247 'singlePageMode' => $base->get_default( 'single_page_mode' ),
248 'controlsPosition' => $base->get_default( 'controls_position' ),
249 'soundEnable' => $base->get_default( 'auto_sound' ),
250 'maxTextureSize' => $base->get_default( 'texture_size' ),
251 'autoPlay' => $base->get_default( 'autoplay' ),
252 'autoPlayDuration' => $base->get_default( 'autoplay_duration' ),
253 'autoPlayStart' => $base->get_default( 'autoplay_start' ),
254 'thumb' => '',
255 'source' => '',
256 'wpOptions' => 'true'
257 );
258
259 $post_data = shortcode_atts( $post_defaults, $post_data, 'dflip' );
260 // $data = shortcode_atts( $post_data, $attr, 'dflip' );
261
262 $source_type = $post_data['source_type'];
263 $pdf_source = $post_data['pdf_source'];
264
265 $post_data['source'] = '';
266
267 if ( $source_type == 'pdf' ) {
268 $post_data['source'] = $pdf_source;
269 $thumb_url = empty( $post_data['pdf_thumb'] ) ? '' : $post_data['pdf_thumb'];
270 }
271
272 if ( $source_type == 'image' ) {
273 $pages = array_map( 'maybe_unserialize', $post_data['pages'] );
274 $source_list = array();
275 $links = array();
276 $index = 0;
277 foreach ( $pages as $image ) {
278 if ( $thumb_url === '' ) {
279 $thumb_url = $image['url'];
280 }
281 if ( $image['url'] !== '' ) {
282 array_push( $source_list, $image['url'] );
283 }
284 if ( isset( $image['hotspots'] ) && $image['hotspots'] !== '' ) {
285 $links[ $index ] = $image['hotspots'];
286 }
287 $index ++;
288 }
289 $post_data['links'] = $links;
290 $post_data['source'] = $source_list;
291 }
292
293 unset( $post_data['pages'] );
294 unset( $post_data['pdf_source'] );
295 unset( $post_data['pdf_thumb'] );
296 unset( $post_data['thumb'] );
297 unset( $post_data['source_type'] );
298 unset( $post_data['class'] );
299 unset( $post_data['id'] );
300
301 foreach ( $post_data as $key => $value ) {
302 if ( $value === "" || $value === null || $value == "global" ) {//newly added will be null in old post
303 unset( $post_data[ $key ] );
304 }
305 }
306 // $attr['slug'] = $post->post_name;
307
308 $help_tip = ""; //todo remove
309 $help_info = ''; //todo remove
310 $html_attr['_slug'] = get_post( $post_id )->post_name;
311 } else {
312 /*handled by new attribute support*/
313 }
314
315 //deep-link
316 $html_attr['data-title'] = sanitize_title( $title );
317 if ( !$multi && isset( $attr['slug'] ) && !empty( $attr['slug'] ) ) {
318 $html_attr['slug'] = sanitize_title( $attr['slug'] );
319 } else if ( $share_slug == 'true' ) {
320 $html_attr['slug'] = get_post( $post_id )->post_name;
321 }
322
323
324 if ( empty( $title ) ) {
325 $title = "Open Book";
326 }
327
328 // if (0 === strpos($data['source'], '/wp-content/')) {
329 // $data['source'] = get_site_url() . $data['source'];
330 // }
331
332 /*Attribute overrides*/
333 $attrHTML = ' ';
334
335 $html_attr['wpoptions'] = 'true';
336
337 if ( !isset( $attr['thumb'] ) && $thumb_url !== '' ) {
338 $html_attr['thumb'] = esc_attr( $thumb_url );
339 }
340 if ( isset( $attr['thumb'] ) ) {
341 $html_attr['thumb'] = $attr['thumb'];
342 }
343
344 if ( !isset( $attr['thumbtype'] ) ) {
345 $html_attr['thumbtype'] = esc_attr( $thumb_tag_type );
346 } else {
347 $html_attr['thumbtype'] = $attr['thumbtype'];
348 }
349
350 //$attr is removed since it can contain insecure and malicious data, atts hold only required keys and sanitized values
351 if ( isset( $attr['data-page'] ) ) {
352 $html_attr['data-page'] = esc_attr( $attr['data-page'] );
353 }
354 if ( isset( $attr['source'] ) ) {
355 $html_attr['source'] = esc_attr( $attr['source'] );
356 }
357 if ( isset( $attr["height"] ) ) {
358 $html_attr["height"] = $attr["height"];
359 }
360 if ( isset( $attr["webgl"] ) ) {
361 $html_attr["webgl"] = $attr["webgl"];
362 }
363 foreach ( $html_attr as $key => $value ) {
364 $attrHTML .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
365 }
366
367 $html = "";
368
369
370 if ( $type == 'thumb' ) {
371 $html = '<div class="_df_' . $type . ' ' . esc_attr( $class ) . '" id="' . esc_attr( $id ) . '" ' . $attrHTML . '>' . esc_attr( $title ) . '</div>';
372 }
373 //
374 //
375 else {
376
377 $html = '<div class="_df_book df-lite' . esc_attr( $class ) . '" id="' . esc_attr( $id ) . '" ' . $attrHTML . '></div>';
378 }
379
380 if ( count( $post_data ) > 0 ) {
381
382 /*Normally this occurs only when a valid post id is added*/
383
384 $code = 'window.option_' . $id . ' = ' . json_encode( $post_data ) . '; if(window.DFLIP && window.DFLIP.parseBooks){window.DFLIP.parseBooks();}';
385
386 $html .= '<script class="df-shortcode-script" type="application/javascript">' . $code . '</script>';
387
388 }
389
390 return $html;
391 }
392
393 /**
394 * Returns the singleton instance of the class.
395 *
396 * @return object dFlip_PostType object.
397 * @since 1.0.0
398 *
399 */
400 public static function get_instance() {
401
402 if ( !isset( self::$instance )
403 && !( self::$instance instanceof DFlip_ShortCode ) ) {
404 self::$instance = new DFlip_ShortCode();
405 }
406
407 return self::$instance;
408
409 }
410
411 }
412
413 //Load the dFlip Plugin Class
414 $dflip_shortcode = DFlip_ShortCode::get_instance();
415
416
417