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