* * @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