* * @param int $id The image attachment ID to use. * @param array $item Slider item data. * @param array $data The slider data to use for retrieval. * @param string $type The type of cropped image to retrieve. * @return string The proper image src attribute for the image. */ public function get_image_src( $id, $item, $data, $type = 'slider' ) { // Get the full image src. If it does not return the data we need, return the image link instead. $src = wp_get_attachment_image_src( $id, 'full' ); $image = ! empty( $src[0] ) ? $src[0] : false; // Allow image to be filtered to use a different thumbnail than the main image. if ( 'thumbnails' === $type ) { $image = apply_filters( 'soliloquy_cropped_image', $image, $id, $item, $data, $data['id'] ); } // If no image, return with the base link. if ( ! $image ) { $image = ! empty( $item['src'] ) ? $item['src'] : false; if ( ! $image ) { return apply_filters( 'soliloquy_no_image_src', $item['link'], $id, $item, $data ); } } // Generate the cropped image if necessary. $type = 'thumbnails' !== $type ? apply_filters( 'soliloquy_crop_type', $type, $id, $item, $data ) : $type; if ( empty( $type ) ) { return apply_filters( 'soliloquy_no_image_type', $item['link'], $id, $item, $data ); } // If the setting exists, go onward with cropping. if ( isset( $data['config']['slider'] ) && $data['config']['slider'] ) { if ( isset( $data['config'][ $type ] ) && $data['config'][ $type ] ) { $common = Soliloquy_Common_Lite::get_instance(); $args = apply_filters( 'soliloquy_crop_image_args', [ 'position' => 'c', 'width' => $this->get_config( $type . '_width', $data ), 'height' => $this->get_config( $type . '_height', $data ), 'quality' => 82, 'retina' => false, ] ); $cropped_image = $common->resize_image( $image, $args['width'], $args['height'], true, $args['position'], $args['quality'], $args['retina'] ); // If there is an error, possibly output error message and return the default image src. if ( is_wp_error( $cropped_image ) ) { // If WP_DEBUG is enabled, and we're logged in, output an error to the user. // Return the non-cropped image as a fallback. return apply_filters( 'soliloquy_image_src', $image, $id, $item, $data ); } else { return apply_filters( 'soliloquy_image_src', $cropped_image, $id, $item, $data ); } } else { return apply_filters( 'soliloquy_image_src', $image, $id, $item, $data ); } } else { return apply_filters( 'soliloquy_image_src', $image, $id, $item, $data ); } } /** * Helper method for positioning the slider. * * @since 1.0.0 * * @param string $style String of slider container styles. * @param array $data Array of slider data. * @return string $style Amended string of slider container styles. */ public function position_slider( $style, $data ) { $gutter = $this->get_config( 'gutter', $data ); $position = ''; switch ( $this->get_config( 'position', $data ) ) { case 'center': $position .= 'margin:0 auto ' . $gutter . 'px;'; break; case 'left': $position .= 'float:left;margin:0 ' . $gutter . 'px ' . $gutter . 'px 0;'; break; case 'right': $position .= 'float:right;margin:0 0 ' . $gutter . 'px ' . $gutter . 'px;'; break; } $position = apply_filters( 'soliloquy_position_style', $position, $data ); return $style . $position; } /** * Helper method for retrieving the mobile image src attribute. * * @since 1.0.0 * * @param string $attr String of image attributes. * @param int $id The ID of the image attachment. * @param array $item The array of date for the image. * @param array $data Array of slider data. * @return string $attr Amended string of image attributes. */ public function mobile_image( $attr, $id, $item, $data ) { $mobile_image = $this->get_image_src( $id, $item, $data, 'mobile' ); return $attr . ' data-soliloquy-src-mobile="' . esc_url( $mobile_image ) . '"'; } /** * Shuffles and randomizes images in a slider. * * @since 1.0.0 * * @param array $data The slider data to use for retrieval. * @return array $data Shuffled slider data. */ public function shuffle( $data ) { // Return early there are no items to shuffle. if ( ! is_array( $data['slider'] ) ) { return $data; } // Prepare variables. $random = []; $keys = array_keys( $data['slider'] ); // Shuffle the keys and loop through them to create a new, randomized array of images. shuffle( $keys ); foreach ( $keys as $key ) { $random[ $key ] = $data['slider'][ $key ]; } // Return the randomized image array. $data['slider'] = $random; return $data; } /** * Helper method for retrieving config values. * * @since 1.0.0 * * @param string $key The config key to retrieve. * @param array $data The slider data to use for retrieval. * @return string Key value on success, default if not set. */ public function get_config( $key, $data ) { $instance = Soliloquy_Common_Lite::get_instance(); return isset( $data['config'][ $key ] ) ? $data['config'][ $key ] : $instance->get_config_default( $key ); } /** * Helper method to minify a string of data. * * @since 1.0.4 * * @param string $string String of data to minify. * @return string $string Minified string of data. */ public function minify( $string ) { $clean = preg_replace( '/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/', '', $string ); $clean = str_replace( [ "\r\n", "\r", "\t", "\n", ' ', ' ', ' ' ], '', $clean ); return apply_filters( 'soliloquy_minified_string', $clean, $string ); } /** * Outputs only the first image of the slider inside a regular
tag * to avoid styling issues with feeds. * * @since 1.0.0 * * @param array $data Array of slider data. * @return string $slider Custom slider output for feeds. */ public function do_feed_output( $data ) { $slider = '
'; foreach ( $data['slider'] as $id => $item ) { // Skip over images that are pending (ignore if in Preview mode). if ( isset( $item['status'] ) && 'pending' === $item['status'] && ! is_preview() ) { continue; } $imagesrc = $this->get_image_src( $id, $item, $data ); $slider .= '' . esc_attr( $item['alt'] ) . ''; break; } $slider .= '
'; return apply_filters( 'soliloquy_feed_output', $slider, $data ); } /** * Returns the query args to be passed to YouTube videos. * * @since 1.0.0 * * @param array $data Array of slider data. */ public function get_youtube_args( $data ) { return apply_filters( 'soliloquy_youtube_args', [ 'enablejsapi' => 1, 'version' => 3, 'wmode' => 'transparent', 'rel' => 0, 'showinfo' => 0, 'modestbranding' => 1, 'autoplay' => 1, 'origin' => get_home_url(), ], $data ); } /** * Returns the query args to be passed to Vimeo videos. * * @since 1.0.0 * * @param array $data Array of slider data. */ public function get_vimeo_args( $data ) { return apply_filters( 'soliloquy_vimeo_args', [ 'api' => 1, 'wmode' => 'transparent', 'byline' => 0, 'title' => 0, 'portrait' => 0, 'autoplay' => 1, 'badge' => 0, 'fullscreen' => 1, ], $data ); } /** * Returns the query args to be passed to Wistia videos. * * @since 1.0.0 * * @param array $data Array of slider data. */ public function get_wistia_args( $data ) { return apply_filters( 'soliloquy_wistia_args', [ 'version' => 'v1', 'wmode' => 'opaque', 'volumeControl' => 1, 'controlsVisibleOnLoad' => 1, 'videoFoam' => 1, ], $data ); } /** * Flag for detecting a mobile device server-side. * * @since 1.0.0 * * @return bool True if on a mobile device, false otherwise. */ public function is_mobile() { // Return wp_is_mobile for the final check. return wp_is_mobile(); } /** * Returns the singleton instance of the class. * * @since 1.0.0 * * @return object The Soliloquy_Shortcode_Lite object. */ public static function get_instance() { if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Soliloquy_Shortcode_Lite ) ) { self::$instance = new Soliloquy_Shortcode_Lite(); } return self::$instance; } } // Load the shortcode class. $soliloquy_shortcode_lite = Soliloquy_Shortcode_Lite::get_instance();