su_get_slides

The shortcodes-ultimate su get slides function.

Published Date - February 15, 2021

Description

Get slides data.

su_get_slides( $args );

Parameters

  1. $args


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'su_get_slides' ) ) {
    require_once ABSPATH . PLUGINDIR . 'shortcodes-ultimateshortcodes-ultimate/includes/functions-galleries.php';
}

// The input(s). 
$args = null;

// NOTICE! Understand what this does before running.
$result = su_get_slides( $args );
            

Defined

The function is defined in the following location(s).

shortcodes-ultimate/includes/functions-galleries.php

function su_get_slides( $args ) {

	$args = wp_parse_args(
		$args,
		array(
			'source'  => 'none',
			'limit'   => 20,
			'gallery' => null,
			'type'    => '',
			'link'    => 'none',
		)
	);

	if (
		null !== $args['gallery'] ||
		( 'none' === $args['source'] && get_option( 'su_option_galleries-432' ) )
	) {
		return su_get_slides_432( $args );
	}

	$slides = array();

	foreach ( array( 'media', 'posts', 'category', 'taxonomy' ) as $type ) {

		if ( strpos( trim( $args['source'] ), $type . ':' ) === 0 ) {
			$args['source'] = array(
				'type' => $type,
				'val'  => (string) trim( str_replace( array( $type . ':', ' ' ), '', $args['source'] ), ',' ),
			);
			break;
		}

	}

	if ( ! is_array( $args['source'] ) ) {
		return $slides;
	}

	$query = array( 'posts_per_page' => $args['limit'] );

	if ( 'media' === $args['source']['type'] ) {

		$query['post_type']   = 'attachment';
		$query['post_status'] = 'any';
		$query['post__in']    = (array) explode( ',', $args['source']['val'] );
		$query['orderby']     = 'post__in';

	}

	// Source: posts
	if ( 'posts' === $args['source']['type'] ) {

		if ( 'recent' !== $args['source']['val'] ) {

			$query['post__in']  = (array) explode( ',', $args['source']['val'] );
			$query['orderby']   = 'post__in';
			$query['post_type'] = 'any';

		}

	} elseif ( 'category' === $args['source']['type'] ) {
		$query['category__in'] = (array) explode( ',', $args['source']['val'] );
	} elseif ( 'taxonomy' === $args['source']['type'] ) {

		$args['source']['val'] = explode( '/', $args['source']['val'] );

		if (
			! is_array( $args['source']['val'] ) ||
			count( $args['source']['val'] ) !== 2
		) {
			return $slides;
		}

		$query['tax_query'] = array(
			array(
				'taxonomy' => $args['source']['val'][0],
				'field'    => 'id',
				'terms'    => (array) explode( ',', $args['source']['val'][1] ),
			),
		);
		$query['post_type'] = 'any';

	}

	$query = apply_filters( 'su/slides_query', $query, $args );
	$query = new WP_Query( $query );

	if ( is_array( $query->posts ) ) {

		foreach ( $query->posts as $post ) {

			$thumb = 'media' === $args['source']['type'] || 'attachment' === $post->post_type
				? $post->ID
				: get_post_thumbnail_id( $post->ID );

			if ( ! su_is_positive_number( $thumb ) ) {
				continue;
			}

			$slide = array(
				'image' => wp_get_attachment_url( $thumb ),
				'link'  => '',
				'title' => get_the_title( $post->ID ),
			);

			if ( 'image' === $args['link'] || 'lightbox' === $args['link'] ) {
				$slide['link'] = $slide['image'];
			} elseif ( 'custom' === $args['link'] ) {
				$slide['link'] = get_post_meta( $thumb, 'su_slide_link', true );
			} elseif ( 'post' === $args['link'] ) {
				$slide['link'] = get_permalink( $post->ID );
			} elseif ( 'attachment' === $args['link'] ) {
				$slide['link'] = get_attachment_link( $thumb );
			}

			$slides[] = $slide;

		}

	}

	return $slides;

}