su_parse_images_source

The shortcodes-ultimate su parse images source function.

Published Date - February 15, 2021

Description

Helper function to parse image source strings.

su_parse_images_source( $source );

Parameters

  1. $source


Usage

The following example is for adding a hook callback.

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

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

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

Defined

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

shortcodes-ultimate/includes/functions-galleries.php

function su_parse_images_source( $source ) {

	$source = str_replace( ' ', '', $source );
	$source = strtolower( $source );

	/**
	 * $match - result of preg_match
	 *  - 1: pattern match the subject
	 *  - 0: pattern does not match the subject
	 *  - False: error occurred
	 *
	 * $source[1] - image source (e.g. media, posts)
	 * $source[2] - taxonomy name (e.g. book)
	 * $source[3] - post/term IDs (e.g. 1,2,3, recent)
	 */
	$match = preg_match(
		'/^(media|posts|taxonomy):(?:([a-zA-Z0-9-_]*)\/)*((?:\d+,)*\d+|recent)/',
		$source,
		$source
	);

	if ( 1 !== $match ) {
		return false;
	}

	if ( 'recent' !== $source[3] ) {
		$source[3] = explode( ',', $source[3] );
	}

	return array(
		'type' => $source[1],
		'tax'  => $source[2],
		'ids'  => $source[3],
	);

}