PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.1.36
Gallery : FooGallery v3.1.36
3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / public / class-shortcodes.php
foogallery / includes / public Last commit date
class-admin-bar.php 2 months ago class-aioseo-sitemaps.php 2 months ago class-css-load-optimizer.php 2 months ago class-foogallery-template-loader.php 2 months ago class-public.php 2 months ago class-rank-math-seo-sitemaps.php 2 months ago class-shortcodes.php 2 months ago class-yoast-seo-sitemaps.php 2 months ago index.php 2 months ago
class-shortcodes.php
159 lines
1 <?php
2 /*
3 * FooGallery Shortcodes
4 */
5
6 if ( ! class_exists( 'FooGallery_Shortcodes' ) ) {
7
8 class FooGallery_Shortcodes {
9
10 function __construct() {
11 add_action( 'foogallery_load_template', array( $this, 'handle_lightbox_field' ) );
12 add_action( 'foogallery_loaded_template', array( $this, 'render_custom_css' ) );
13 add_action( 'plugins_loaded', array( $this, 'init_shortcodes' ) );
14 }
15
16 function init_shortcodes() {
17 add_shortcode( foogallery_gallery_shortcode_tag(), array( $this, 'render_foogallery_shortcode' ) );
18 add_shortcode( 'foogallery-enqueue', array( $this, 'render_foogallery_enqueue' ) );
19 }
20
21 function render_foogallery_shortcode( $atts ) {
22
23 $args = wp_parse_args( $atts, array(
24 'id' => 0,
25 'gallery' => '',
26 ) );
27 $args = $this->normalize_shortcode_args( $args );
28
29 $args = apply_filters( 'foogallery_shortcode_atts', $args );
30
31 //create new instance of template engine
32 $engine = new FooGallery_Template_Loader();
33
34 ob_start();
35
36 $engine->render_template( $args );
37
38 $output_string = ob_get_contents();
39 ob_end_clean();
40 return $output_string;
41 }
42
43 /**
44 * Normalize supported gallery shortcode aliases to their canonical arguments.
45 *
46 * @param array $args Parsed shortcode arguments.
47 *
48 * @return array
49 */
50 function normalize_shortcode_args( $args ) {
51 if ( ! is_array( $args ) ) {
52 return $args;
53 }
54
55 if ( ! array_key_exists( 'template', $args ) && array_key_exists( 'layout', $args ) ) {
56 $args['template'] = $args['layout'];
57 }
58
59 $args = $this->normalize_shortcode_thumbnail_size_arg( $args, 'thumbnail_size' );
60 $args = $this->normalize_shortcode_thumbnail_size_arg( $args, 'thumbnail_dimensions' );
61
62 if ( array_key_exists( 'thumbnail_size', $args ) && is_array( $args['thumbnail_size'] ) && ! array_key_exists( 'thumbnail_dimensions', $args ) ) {
63 $args['thumbnail_dimensions'] = $args['thumbnail_size'];
64 } else if ( array_key_exists( 'thumbnail_dimensions', $args ) && is_array( $args['thumbnail_dimensions'] ) && ! array_key_exists( 'thumbnail_size', $args ) ) {
65 $args['thumbnail_size'] = $args['thumbnail_dimensions'];
66 }
67
68 if ( array_key_exists( 'thumbnail_size', $args ) && is_array( $args['thumbnail_size'] ) && ! array_key_exists( 'crop', $args['thumbnail_size'] ) ) {
69 $args['thumbnail_size']['crop'] = '0';
70 }
71
72 if ( ! array_key_exists( 'thumbnail_width', $args ) && array_key_exists( 'thumbnail_size', $args ) && is_array( $args['thumbnail_size'] ) && array_key_exists( 'width', $args['thumbnail_size'] ) ) {
73 $args['thumbnail_width'] = $args['thumbnail_size']['width'];
74 }
75
76 return $args;
77 }
78
79 /**
80 * Normalize a compound thumbnail size shortcode argument.
81 *
82 * @param array $args Parsed shortcode arguments.
83 * @param string $key Argument key.
84 *
85 * @return array
86 */
87 function normalize_shortcode_thumbnail_size_arg( $args, $key ) {
88 if ( ! array_key_exists( $key, $args ) || is_array( $args[ $key ] ) ) {
89 return $args;
90 }
91
92 $thumbnail_size = $this->parse_shortcode_thumbnail_size( $args[ $key ] );
93
94 if ( false !== $thumbnail_size ) {
95 $args[ $key ] = $thumbnail_size;
96 }
97
98 return $args;
99 }
100
101 /**
102 * Parse a compound thumbnail size shortcode attribute.
103 *
104 * @param string $thumbnail_size Thumbnail size in the form WIDTHxHEIGHT or WIDTHxHEIGHTxcrop.
105 *
106 * @return array|false
107 */
108 function parse_shortcode_thumbnail_size( $thumbnail_size ) {
109 if ( ! is_string( $thumbnail_size ) ) {
110 return false;
111 }
112
113 if ( 1 !== preg_match( '/^(\d+)x(\d+)(?:x(crop))?$/i', trim( $thumbnail_size ), $matches ) ) {
114 return false;
115 }
116
117 $args = array(
118 'width' => absint( $matches[1] ),
119 'height' => absint( $matches[2] ),
120 );
121
122 if ( ! empty( $matches[3] ) ) {
123 $args['crop'] = '1';
124 }
125
126 return $args;
127 }
128
129 function render_foogallery_enqueue() {
130 foogallery_enqueue_core_gallery_template_script();
131 foogallery_enqueue_core_gallery_template_style();
132 wp_enqueue_script( 'masonry' );
133 }
134
135 /**
136 * Handle a gallery that has a lightbox. This allows us to include any scripts or CSS that is needed for the lightbox
137 *
138 * @param $gallery FooGallery
139 */
140 function handle_lightbox_field( $gallery ) {
141 if ( $gallery->gallery_template_has_field_of_type( 'lightbox' ) ) {
142 $lightbox = foogallery_gallery_template_setting_lightbox();
143
144 if ( !empty( $lightbox ) ) {
145 do_action( "foogallery_template_lightbox-{$lightbox}", $gallery );
146 }
147 }
148 }
149
150 function render_custom_css( $foogallery ) {
151 if ( !empty( $foogallery->custom_css ) ) {
152 echo '<style type="text/css">';
153 echo $foogallery->custom_css; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Custom CSS from gallery settings. The custom CSS is already sanitized
154 echo '</style>';
155 }
156 }
157 }
158 }
159