Plugin Directory

Changeset 1801448


Ignore:
Timestamp:
01/12/2018 01:42:37 AM (8 years ago)
Author:
naomicbush
Message:

Update Gravity Forms helper functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gravity-forms-stripe/trunk/includes/gf-utility-functions.php

    r1368395 r1801448  
    33if ( ! function_exists( 'rgget' ) ) {
    44    /**
    5      * @param      $name
    6      * @param null $array
     5     * Helper function for getting values from query strings or arrays
    76     *
    8      * @return string
     7     * @param string $name  The key
     8     * @param array  $array The array to search through.  If null, checks query strings.  Defaults to null.
     9     *
     10     * @return string The value.  If none found, empty string.
    911     */
    10     function rgget ( $name, $array = null ) {
     12    function rgget( $name, $array = null ) {
    1113        if ( ! isset( $array ) ) {
    1214            $array = $_GET;
    1315        }
    1416
    15         if ( isset( $array[$name] ) ) {
    16             return $array[$name];
     17        if ( ! is_array( $array ) ) {
     18            return '';
    1719        }
    1820
    19         return "";
     21        if ( isset( $array[ $name ] ) ) {
     22            return $array[ $name ];
     23        }
     24
     25        return '';
    2026    }
    2127}
    2228
    23 
    2429if ( ! function_exists( 'rgpost' ) ) {
    2530    /**
    26      * @param      $name
    27      * @param bool $do_stripslashes
     31     * Helper function to obtain POST values.
    2832     *
    29      * @return mixed|string
     33     * @param string $name            The key
     34     * @param bool   $do_stripslashes Optional. Performs stripslashes_deep.  Defaults to true.
     35     *
     36     * @return string The value.  If none found, empty string.
    3037     */
    31     function rgpost ( $name, $do_stripslashes = true ) {
    32         if ( isset( $_POST[$name] ) ) {
    33             return $do_stripslashes ? stripslashes_deep( $_POST[$name] ) : $_POST[$name];
     38    function rgpost( $name, $do_stripslashes = true ) {
     39        if ( isset( $_POST[ $name ] ) ) {
     40            return $do_stripslashes ? stripslashes_deep( $_POST[ $name ] ) : $_POST[ $name ];
    3441        }
    3542
     
    4047if ( ! function_exists( 'rgar' ) ) {
    4148    /**
    42      * @param $array
    43      * @param $name
     49     * Get a specific property of an array without needing to check if that property exists.
    4450     *
    45      * @return string
     51     * Provide a default value if you want to return a specific value if the property is not set.
     52     *
     53     * @since  Unknown
     54     * @access public
     55     *
     56     * @param array  $array   Array from which the property's value should be retrieved.
     57     * @param string $prop    Name of the property to be retrieved.
     58     * @param string $default Optional. Value that should be returned if the property is not set or empty. Defaults to null.
     59     *
     60     * @return null|string|mixed The value
    4661     */
    47     function rgar ( $array, $name ) {
    48         if ( isset( $array[$name] ) ) {
    49             return $array[$name];
     62    function rgar( $array, $prop, $default = null ) {
     63
     64        if ( ! is_array( $array ) && ! ( is_object( $array ) && $array instanceof ArrayAccess ) ) {
     65            return $default;
    5066        }
    5167
    52         return '';
     68        if ( isset( $array[ $prop ] ) ) {
     69            $value = $array[ $prop ];
     70        } else {
     71            $value = '';
     72        }
     73
     74        return empty( $value ) && $default !== null ? $default : $value;
    5375    }
    5476}
     
    5678if ( ! function_exists( 'rgars' ) ) {
    5779    /**
    58      * @param $array
    59      * @param $name
     80     * Gets a specific property within a multidimensional array.
    6081     *
    61      * @return string
     82     * @since  Unknown
     83     * @access public
     84     *
     85     * @param array  $array   The array to search in.
     86     * @param string $name    The name of the property to find.
     87     * @param string $default Optional. Value that should be returned if the property is not set or empty. Defaults to null.
     88     *
     89     * @return null|string|mixed The value
    6290     */
    63     function rgars ( $array, $name ) {
     91    function rgars( $array, $name, $default = null ) {
     92
     93        if ( ! is_array( $array ) && ! ( is_object( $array ) && $array instanceof ArrayAccess ) ) {
     94            return $default;
     95        }
     96
    6497        $names = explode( '/', $name );
    6598        $val   = $array;
    6699        foreach ( $names as $current_name ) {
    67             $val = rgar( $val, $current_name );
     100            $val = rgar( $val, $current_name, $default );
    68101        }
    69102
     
    74107if ( ! function_exists( 'rgempty' ) ) {
    75108    /**
    76      * @param      $name
    77      * @param null $array
     109     * Determines if a value is empty.
    78110     *
    79      * @return bool
     111     * @since  Unknown
     112     * @access public
     113     *
     114     * @param string $name  The property name to check.
     115     * @param array  $array Optional. An array to check through.  Otherwise, checks for POST variables.
     116     *
     117     * @return bool True if empty.  False otherwise.
    80118     */
    81     function rgempty ( $name, $array = null ) {
     119    function rgempty( $name, $array = null ) {
     120
     121        if ( is_array( $name ) ) {
     122            return empty( $name );
     123        }
     124
    82125        if ( ! $array ) {
    83126            $array = $_POST;
    84127        }
    85128
    86         $val = rgget( $name, $array );
     129        $val = rgar( $array, $name );
    87130
    88131        return empty( $val );
     
    90133}
    91134
    92 
    93135if ( ! function_exists( 'rgblank' ) ) {
    94136    /**
    95      * @param $text
     137     * Checks if the string is empty
    96138     *
    97      * @return bool
     139     * @since  Unknown
     140     * @access public
     141     *
     142     * @param string $text The string to check.
     143     *
     144     * @return bool True if empty.  False otherwise.
    98145     */
    99     function rgblank ( $text ) {
    100         return empty( $text ) && strval( $text ) != '0';
     146    function rgblank( $text ) {
     147        return empty( $text ) && ! is_array( $text ) && strval( $text ) != '0';
    101148    }
    102149}
     150
     151if ( ! function_exists( 'rgobj' ) ) {
     152    /**
     153     * Gets a property value from an object
     154     *
     155     * @since  Unknown
     156     * @access public
     157     *
     158     * @param object $obj  The object to check
     159     * @param string $name The property name to check for
     160     *
     161     * @return string The property value
     162     */
     163    function rgobj( $obj, $name ) {
     164        if ( isset( $obj->$name ) ) {
     165            return $obj->$name;
     166        }
     167
     168        return '';
     169    }
     170}
     171if ( ! function_exists( 'rgexplode' ) ) {
     172    /**
     173     * Converts a delimiter separated string to an array.
     174     *
     175     * @since  Unknown
     176     * @access public
     177     *
     178     * @param string $sep    The delimiter between values
     179     * @param string $string The string to convert
     180     * @param int    $count  The expected number of items in the resulting array
     181     *
     182     * @return array $ary The exploded array
     183     */
     184    function rgexplode( $sep, $string, $count ) {
     185        $ary = explode( $sep, $string );
     186        while ( count( $ary ) < $count ) {
     187            $ary[] = '';
     188        }
     189
     190        return $ary;
     191    }
     192}
Note: See TracChangeset for help on using the changeset viewer.