Permalink
Browse files

wfx_get_image() new parameter

New parameter ‘return’ - defines type of data to return (optional):

- 'path' default behaviour, just returns string of image path (if valid)
- 'detail' returns array with keys of 'path', 'width' and 'height' of image
  • Loading branch information...
Jonnyauk committed Aug 28, 2018
1 parent 5092df9 commit 81edd11e9746296f9d3c9bce853be3ff0454cca2
Showing with 35 additions and 4 deletions.
  1. +6 −1 functions.php
  2. +29 −3 wf-includes/wf-display-functions.php
View
@@ -1305,9 +1305,14 @@
* - 'post_thumbnail' for post WP featured image
* - 'attachment'
* @param [string] $fallback Path to fallback image INSIDE your theme folder
* @param [string] $return Type of data to return:
* - 'path' default behaviour, just returns string of image path (if valid)
* - 'detail' returns array with keys of 'path', 'width' and 'height' of image
* @param [string] $echo Echo or return output. Y/N [N]
*
* @return [mixed] Path to image/false. Path is checked if valid URL, but not escaped - so remember your esc_url()!
* @return [mixed] $return = 'path' returns string with URL of image (checked if valid URL, but not escaped - so remember your esc_url()!)
* $return = 'detail' returns array (see $data param documentation)
* Returns false if invalid image/data
*
*/
if ( !function_exists( 'wfx_get_image' ) ) : function wfx_get_image( $args='' ) {
@@ -2307,8 +2307,13 @@ function wf_get_attachments($args) {
* - 'post_thumbnail' for post WP featured image
* - 'attachment'
* @param [string] $fallback Path to fallback image INSIDE your theme folder
* @param [string] $return Type of data to return:
* - 'path' default behaviour, just returns string of image path (if valid)
* - 'detail' returns array with keys of 'path', 'width' and 'height' of image
*
* @return [mixed] Path to image/false. Path is checked if valid URL, but not escaped - so remember your esc_url()!
* @return [mixed] $return = 'path' returns string with URL of image (checked if valid URL, but not escaped - so remember your esc_url()!)
* $return = 'detail' returns array (see $data param documentation)
* Returns false if invalid image/data
*
*/
function wf_get_image( $args ) {
@@ -2324,7 +2329,8 @@ function wf_get_image( $args ) {
'id' => '',
'size' => 'thumbnail',
'field' => 'post_meta',
'fallback' => ''
'fallback' => '',
'return' => 'path'
);
$args = wp_parse_args( $args, $defaults );
@@ -2336,6 +2342,8 @@ function wf_get_image( $args ) {
$data = false;
$img_url = false;
$img_w = false;
$img_h = false;
switch( $data_type ) {
@@ -2396,7 +2404,7 @@ function wf_get_image( $args ) {
if ( is_array( $size ) ) {
$img_orgin = wp_get_attachment_image_src( $data, 'original', false );
//wfx_debug($img_tn_array_format);
if ( array_key_exists( 1, $img_orgin ) && array_key_exists( 2, $img_orgin ) && is_numeric( $img_orgin[1] ) && is_numeric( $img_orgin[2] ) ) {
$img_format = ( $img_orgin[1] < $img_orgin[2] ) ? 'portrait' : 'landscape';
@@ -2421,10 +2429,28 @@ function wf_get_image( $args ) {
$img_url = ( $img_url != false && wfx_valid_url( $img_url ) ) ? $img_url : false;
$img_url = ( wfx_ends_with( 'media/default.png', $img_url ) ) ? false : $img_url;
// Build extra data for returned array if required
if ( $return == 'detail' && $img_url !== false ) {
$img_w = ( array_key_exists( 1, $img_tn_array ) && is_numeric( $img_tn_array[1] ) ) ? $img_tn_array[1] : '';
$img_h = ( array_key_exists( 2, $img_tn_array ) && is_numeric( $img_tn_array[2] ) ) ? $img_tn_array[2] : '';
}
}
$img_url = ( empty( $img_url ) && !empty( $fallback ) ) ? WF_THEME_URL . '/' . $fallback : $img_url;
if ( $return == 'detail' && $img_url !== false ) {
$img_url = array(
'url' => $img_url,
'width' => $img_w,
'height' => $img_h
);
}
return $img_url;
}

0 comments on commit 81edd11

Please sign in to comment.