elementor
1 year ago
logs
1 month ago
strong-testimonials-beaver-block
1 year ago
submodules
8 months ago
class-strong-gutemberg.php
1 year ago
class-strong-log.php
1 year ago
class-strong-mail.php
1 year ago
class-strong-testimonials-average-shortcode.php
1 year ago
class-strong-testimonials-count-shortcode.php
1 year ago
class-strong-testimonials-defaults.php
1 month ago
class-strong-testimonials-form.php
1 month ago
class-strong-testimonials-order.php
1 year ago
class-strong-testimonials-privacy.php
1 year ago
class-strong-testimonials-render.php
1 month ago
class-strong-testimonials-templates.php
1 year ago
class-strong-testimonials-view-shortcode.php
2 days ago
class-strong-testimonials-view-widget.php
1 year ago
class-strong-view-display.php
1 month ago
class-strong-view-form.php
2 days ago
class-strong-view-slideshow.php
1 month ago
class-strong-view.php
11 months ago
class-walker-strong-category-checklist-front.php
1 year ago
deprecated.php
1 year ago
filters.php
1 month ago
functions-activation.php
1 month ago
functions-content.php
11 months ago
functions-image.php
5 months ago
functions-rating.php
1 year ago
functions-template-form.php
2 days ago
functions-template.php
4 months ago
functions-views.php
1 year ago
functions.php
1 month ago
l10n-polylang.php
1 year ago
l10n-wpml.php
1 year ago
post-types.php
2 days ago
retro.php
1 year ago
scripts.php
1 month ago
functions-image.php
335 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Featured image functions. |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * @param null $size |
| 8 | * |
| 9 | * @return mixed|string |
| 10 | */ |
| 11 | function wpmtst_get_thumbnail( $size = null ) { |
| 12 | if ( ! WPMST()->atts( 'thumbnail' ) && ! is_admin() ) { |
| 13 | return ''; |
| 14 | } |
| 15 | |
| 16 | // let arg override view setting |
| 17 | $size = ( null === $size ) ? WPMST()->atts( 'thumbnail_size' ) : $size; |
| 18 | if ( 'custom' === $size ) { |
| 19 | $size = array( WPMST()->atts( 'thumbnail_width' ), WPMST()->atts( 'thumbnail_height' ) ); |
| 20 | } |
| 21 | |
| 22 | $id = get_the_ID(); |
| 23 | $img = ''; |
| 24 | |
| 25 | // check for a featured image |
| 26 | if ( has_post_thumbnail( $id ) ) { |
| 27 | // show featured image |
| 28 | $img = get_the_post_thumbnail( $id, $size ); |
| 29 | } else { |
| 30 | |
| 31 | // no featured image, now what? |
| 32 | |
| 33 | if ( 'yes' === WPMST()->atts( 'gravatar' ) ) { |
| 34 | // view > gravatar > show gravatar (use default, if not found) |
| 35 | |
| 36 | $img = get_avatar( wpmtst_get_field( 'email' ), apply_filters( 'wpmtst_gravatar_size', $size ) ); |
| 37 | } elseif ( 'if' === WPMST()->atts( 'gravatar' ) ) { |
| 38 | // view > gravatar > show gravatar only if found (and has email) |
| 39 | |
| 40 | if ( wpmtst_get_field( 'email' ) ) { |
| 41 | // get_avatar will return false if not found (via filter) |
| 42 | $img = get_avatar( wpmtst_get_field( 'email' ), apply_filters( 'wpmtst_gravatar_size', $size ) ); |
| 43 | } |
| 44 | } |
| 45 | $img = apply_filters( 'wpmtst_thumbnail_default_img', $img, $id, $size ); |
| 46 | } |
| 47 | |
| 48 | return apply_filters( 'wpmtst_thumbnail_img', $img, $id, $size ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Filter the thumbnail image. |
| 53 | * Used to add link for a lightbox. Will not affect avatars. |
| 54 | * |
| 55 | * @param $img |
| 56 | * @param $post_id |
| 57 | * |
| 58 | * @since 1.23.0 |
| 59 | * @since 2.9.4 classes and filter |
| 60 | * @since 2.30.0 lightbox_class |
| 61 | * |
| 62 | * @return string |
| 63 | */ |
| 64 | function wpmtst_thumbnail_img( $img, $post_id, $size ) { |
| 65 | if ( WPMST()->atts( 'lightbox' ) ) { |
| 66 | $url = wp_get_attachment_url( get_post_thumbnail_id( $post_id ) ); |
| 67 | if ( $url ) { |
| 68 | $class_array = array( WPMST()->atts( 'lightbox_class' ) ); |
| 69 | $classes = implode( ' ', array_unique( apply_filters( 'wpmtst_thumbnail_link_class', $class_array ) ) ); |
| 70 | $img = sprintf( '<a class="%s" href="%s">%s</a>', $classes, esc_url( $url ), $img ); |
| 71 | } |
| 72 | } |
| 73 | return $img; |
| 74 | } |
| 75 | add_filter( 'wpmtst_thumbnail_img', 'wpmtst_thumbnail_img', 10, 3 ); |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * Exclude testimonial thumbnails from Lazy Loading Responsive Images plugin. |
| 80 | * |
| 81 | * @param $attr |
| 82 | * @param $attachment |
| 83 | * @param $size |
| 84 | * |
| 85 | * @since 2.27.0 |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | function wpmtst_exclude_from_lazyload( $attr, $attachment, $size ) { |
| 90 | $options = get_option( 'wpmtst_options' ); |
| 91 | |
| 92 | if ( isset( $options['no_lazyload_plugin'] ) && $options['no_lazyload_plugin'] ) { |
| 93 | if ( 'wpm-testimonial' === get_post_type( $attachment->post_parent ) ) { |
| 94 | $attr['data-no-lazyload'] = 1; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return $attr; |
| 99 | } |
| 100 | /** |
| 101 | * Add filter if Lazy Loading Responsive Images plugin is active. |
| 102 | * |
| 103 | * @since 2.27.0 |
| 104 | */ |
| 105 | function wpmtst_lazyload_check() { |
| 106 | if ( wpmtst_is_plugin_active( 'lazy-loading-responsive-images' ) ) { |
| 107 | add_filter( 'wp_get_attachment_image_attributes', 'wpmtst_exclude_from_lazyload', 10, 3 ); |
| 108 | } |
| 109 | } |
| 110 | add_action( 'init', 'wpmtst_lazyload_check' ); |
| 111 | |
| 112 | /** |
| 113 | * Enable lazy load |
| 114 | * |
| 115 | * @param $attr |
| 116 | * @param $attachment |
| 117 | * @param $size |
| 118 | * |
| 119 | * @since 2.27.0 |
| 120 | * |
| 121 | * @return array |
| 122 | */ |
| 123 | function wpmtst_add_lazyload( $attr, $attachment, $size ) { |
| 124 | if ( ! function_exists( 'wp_lazy_loading_enabled' ) || ! apply_filters( 'wp_lazy_loading_enabled', true, 'img', 'strong_testimonials_has_lazyload' ) ) { |
| 125 | $options = get_option( 'wpmtst_options' ); |
| 126 | |
| 127 | if ( isset( $options['lazyload'] ) && $options['lazyload'] ) { |
| 128 | $parent_type = get_post_type( $attachment->post_parent ); |
| 129 | if ( ( 'testimonial' === $parent_type || 'wpm-testimonial' === $parent_type ) && ! is_admin() ) { |
| 130 | $attr['class'] .= ' lazy-load'; |
| 131 | |
| 132 | $attr['data-src'] = $attr['src']; |
| 133 | unset( $attr['src'] ); |
| 134 | if ( isset( $attr['srcset'] ) ) { |
| 135 | $attr['data-srcset'] = $attr['srcset']; |
| 136 | unset( $attr['srcset'] ); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return $attr; |
| 143 | } |
| 144 | add_filter( 'wp_get_attachment_image_attributes', 'wpmtst_add_lazyload', 10, 3 ); |
| 145 | |
| 146 | /** |
| 147 | * Filter the gravatar size. |
| 148 | * |
| 149 | * @param array|string $size |
| 150 | * @since 1.23.0 |
| 151 | * @return mixed |
| 152 | */ |
| 153 | function wpmtst_gravatar_size_filter( $size = array( 150, 150 ) ) { |
| 154 | // avatars are square so get the width of the requested size |
| 155 | if ( is_array( $size ) ) { |
| 156 | // if dimension array |
| 157 | $gravatar_size = $size[0]; |
| 158 | } else { |
| 159 | // if named size |
| 160 | $image_sizes = wpmtst_get_image_sizes(); |
| 161 | $gravatar_size = $image_sizes[ $size ]['width']; |
| 162 | } |
| 163 | |
| 164 | return $gravatar_size; |
| 165 | } |
| 166 | add_filter( 'wpmtst_gravatar_size', 'wpmtst_gravatar_size_filter' ); |
| 167 | |
| 168 | /** |
| 169 | * Checks to see if the specified email address has a Gravatar image. |
| 170 | * |
| 171 | * Thanks Tom McFarlin https://tommcfarlin.com/check-if-a-user-has-a-gravatar/ |
| 172 | * @param $email_address string The email of the address of the user to check |
| 173 | * @return bool Whether or not the user has a gravatar |
| 174 | * @since 1.23.0 |
| 175 | */ |
| 176 | function wpmtst_has_gravatar( $email_address ) { |
| 177 | // Build the Gravatar URL by hashing the email address |
| 178 | $url = 'http://www.gravatar.com/avatar/' . md5( strtolower( trim( $email_address ) ) ) . '?d=404'; |
| 179 | |
| 180 | // Now check the headers... |
| 181 | $headers = get_headers( $url, 1 ); |
| 182 | |
| 183 | // Check if headers were retrieved successfully |
| 184 | if ( ! is_array( $headers ) || ! isset( $headers[0] ) ) { |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | // If 200 is found, the user has a Gravatar; otherwise, they don't. |
| 189 | return preg_match( '|200|', $headers[0] ) ? true : false; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Before assembling avatar HTML. |
| 194 | * |
| 195 | * @param $url |
| 196 | * @param $id_or_email |
| 197 | * @param $args |
| 198 | * |
| 199 | * @return bool |
| 200 | */ |
| 201 | function wpmtst_get_avatar( $url, $id_or_email, $args ) { |
| 202 | if ( 'if' === WPMST()->atts( 'gravatar' ) && ! wpmtst_has_gravatar( $id_or_email ) ) { |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | return $url; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | function wpmtst_thumbnail_img_platform( $img, $post_id, $size ) { |
| 211 | |
| 212 | $platform = get_post_meta( $post_id, 'platform', true ); |
| 213 | if ( ! $platform ) { |
| 214 | return $img; |
| 215 | } |
| 216 | |
| 217 | $img = apply_filters( 'wpmtst_thumbnail_img_platform_' . $platform, $img, $post_id, $size ); |
| 218 | |
| 219 | return $img; |
| 220 | } |
| 221 | add_filter( 'wpmtst_thumbnail_img', 'wpmtst_thumbnail_img_platform', 10, 3 ); |
| 222 | |
| 223 | |
| 224 | function wpmtst_thumbnail_img_platform_general( $img, $post_id, $size ) { |
| 225 | |
| 226 | $platform_user_photo = get_post_meta( $post_id, 'platform_user_photo', true ); |
| 227 | if ( ! $platform_user_photo || ! wpmtst_is_valid_image_url( $platform_user_photo ) ) { |
| 228 | return $img; |
| 229 | } |
| 230 | |
| 231 | //calculate width & height based on size |
| 232 | if ( is_array( $size ) ) { |
| 233 | $width = $size[0]; |
| 234 | $height = $size[1]; |
| 235 | } else { |
| 236 | $sizes = wpmtst_get_image_sizes( $size ); |
| 237 | $width = $sizes['width']; |
| 238 | $height = $sizes['height']; |
| 239 | } |
| 240 | |
| 241 | return sprintf( '<img src="%s" %s %s/>', $platform_user_photo, $width ? "width='{$width}'" : '', $height ? "height='{$height}'" : '' ); |
| 242 | } |
| 243 | add_filter( 'wpmtst_thumbnail_img_platform_facebook', 'wpmtst_thumbnail_img_platform_general', 10, 3 ); |
| 244 | add_filter( 'wpmtst_thumbnail_img_platform_yelp', 'wpmtst_thumbnail_img_platform_general', 10, 3 ); |
| 245 | add_filter( 'wpmtst_thumbnail_img_platform_zomato', 'wpmtst_thumbnail_img_platform_general', 10, 3 ); |
| 246 | |
| 247 | |
| 248 | function wpmtst_thumbnail_img_platform_woocommerce( $img, $post_id, $size ) { |
| 249 | |
| 250 | $options = get_option( 'wpmtst_importer_options' ); |
| 251 | if ( '' === $options['email_field'] ) { |
| 252 | return $img; |
| 253 | } |
| 254 | |
| 255 | $email = get_post_meta( $post_id, $options['email_field'], true ); |
| 256 | if ( ! $email ) { |
| 257 | return $img; |
| 258 | } |
| 259 | |
| 260 | //calculate width & height based on size |
| 261 | if ( is_array( $size ) ) { |
| 262 | $width = $size[0]; |
| 263 | $height = $size[1]; |
| 264 | } else { |
| 265 | $sizes = wpmtst_get_image_sizes( $size ); |
| 266 | $width = $sizes['width']; |
| 267 | $height = $sizes['height']; |
| 268 | } |
| 269 | |
| 270 | return sprintf( '<img src="%s" %s %s/>', get_avatar_url( $email ), $width ? "width='{$width}'" : '', $height ? "height='{$height}'" : '' ); |
| 271 | } |
| 272 | add_filter( 'wpmtst_thumbnail_img_platform_woocommerce', 'wpmtst_thumbnail_img_platform_woocommerce', 10, 3 ); |
| 273 | |
| 274 | function wpmtst_is_valid_image_url( $url ) { |
| 275 | // Check local |
| 276 | $site_url = site_url(); |
| 277 | if ( strpos( $url, $site_url ) === 0 ) { |
| 278 | // Obține path-ul relativ |
| 279 | $relative_path = str_replace( $site_url, '', $url ); |
| 280 | $file_path = ABSPATH . ltrim( $relative_path, '/' ); |
| 281 | |
| 282 | return file_exists( $file_path ); |
| 283 | } |
| 284 | |
| 285 | // Check external |
| 286 | $headers = @get_headers( $url, 1 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 287 | |
| 288 | if ( false === $headers ) { |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | $status_line = is_array( $headers ) && isset( $headers[0] ) ? $headers[0] : ''; |
| 293 | |
| 294 | return strpos( $status_line, '200 OK' ) !== false; |
| 295 | } |
| 296 | |
| 297 | function wpmtst_thumbnail_img_platform_google( $img, $post_id, $size ) { |
| 298 | |
| 299 | $platform_user_photo = get_post_meta( $post_id, 'platform_user_photo', true ); |
| 300 | if ( ! $platform_user_photo ) { |
| 301 | return $img; |
| 302 | } |
| 303 | |
| 304 | $width = 60; |
| 305 | $height = 60; |
| 306 | |
| 307 | if ( is_array( $size ) ) { |
| 308 | $width = (int) $size[0]; |
| 309 | $height = (int) $size[1]; |
| 310 | } |
| 311 | |
| 312 | $scaled_url = $platform_user_photo; |
| 313 | |
| 314 | if ( false !== strpos( $platform_user_photo, 'googleusercontent.com' ) ) { |
| 315 | if ( preg_match( '/=s\d+(-c)?/', $platform_user_photo ) ) { |
| 316 | $scaled_url = preg_replace( |
| 317 | '/=s\d+(-c)?/', |
| 318 | '=s' . max( $width, $height ) . '-c', |
| 319 | $platform_user_photo |
| 320 | ); |
| 321 | } else { |
| 322 | $scaled_url = $platform_user_photo . '=s' . max( $width, $height ) . '-c'; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return sprintf( |
| 327 | '<img src="%s" width="%d" height="%d" />', |
| 328 | esc_url( $scaled_url ), |
| 329 | $width, |
| 330 | $height |
| 331 | ); |
| 332 | } |
| 333 | |
| 334 | add_filter( 'wpmtst_thumbnail_img_platform_google', 'wpmtst_thumbnail_img_platform_google', 10, 3 ); |
| 335 |