Plugin Directory

Changeset 1265625


Ignore:
Timestamp:
10/14/2015 09:17:27 AM (10 years ago)
Author:
sendmachine
Message:

array_column bugfix

Location:
sendmachine
Files:
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • sendmachine/tags/1.0.3/includes/utils.php

    r1226818 r1265625  
    2323
    2424}
     25
     26if (!function_exists('array_column')) {
     27    /**
     28     * Returns the values from a single column of the input array, identified by
     29     * the $columnKey.
     30     *
     31     * Optionally, you may provide an $indexKey to index the values in the returned
     32     * array by the values from the $indexKey column in the input array.
     33     *
     34     * @param array $input A multi-dimensional array (record set) from which to pull
     35     *                     a column of values.
     36     * @param mixed $columnKey The column of values to return. This value may be the
     37     *                         integer key of the column you wish to retrieve, or it
     38     *                         may be the string key name for an associative array.
     39     * @param mixed $indexKey (Optional.) The column to use as the index/keys for
     40     *                        the returned array. This value may be the integer key
     41     *                        of the column, or it may be the string key name.
     42     * @return array
     43     */
     44    function array_column($input = null, $columnKey = null, $indexKey = null)
     45    {
     46        // Using func_get_args() in order to check for proper number of
     47        // parameters and trigger errors exactly as the built-in array_column()
     48        // does in PHP 5.5.
     49        $argc = func_num_args();
     50        $params = func_get_args();
     51
     52        if ($argc < 2) {
     53            trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
     54            return null;
     55        }
     56
     57        if (!is_array($params[0])) {
     58            trigger_error(
     59                'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given',
     60                E_USER_WARNING
     61            );
     62            return null;
     63        }
     64
     65        if (!is_int($params[1])
     66            && !is_float($params[1])
     67            && !is_string($params[1])
     68            && $params[1] !== null
     69            && !(is_object($params[1]) && method_exists($params[1], '__toString'))
     70        ) {
     71            trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
     72            return false;
     73        }
     74
     75        if (isset($params[2])
     76            && !is_int($params[2])
     77            && !is_float($params[2])
     78            && !is_string($params[2])
     79            && !(is_object($params[2]) && method_exists($params[2], '__toString'))
     80        ) {
     81            trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
     82            return false;
     83        }
     84
     85        $paramsInput = $params[0];
     86        $paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
     87
     88        $paramsIndexKey = null;
     89        if (isset($params[2])) {
     90            if (is_float($params[2]) || is_int($params[2])) {
     91                $paramsIndexKey = (int) $params[2];
     92            } else {
     93                $paramsIndexKey = (string) $params[2];
     94            }
     95        }
     96
     97        $resultArray = array();
     98
     99        foreach ($paramsInput as $row) {
     100            $key = $value = null;
     101            $keySet = $valueSet = false;
     102
     103            if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
     104                $keySet = true;
     105                $key = (string) $row[$paramsIndexKey];
     106            }
     107
     108            if ($paramsColumnKey === null) {
     109                $valueSet = true;
     110                $value = $row;
     111            } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
     112                $valueSet = true;
     113                $value = $row[$paramsColumnKey];
     114            }
     115
     116            if ($valueSet) {
     117                if ($keySet) {
     118                    $resultArray[$key] = $value;
     119                } else {
     120                    $resultArray[] = $value;
     121                }
     122            }
     123
     124        }
     125
     126        return $resultArray;
     127    }
     128}
  • sendmachine/tags/1.0.3/readme.txt

    r1239893 r1265625  
    44Requires at least: 3.2.1
    55Tested up to: 4.3
    6 Stable tag: 1.0.2
     6Stable tag: 1.0.3
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    9696== Changelog ==
    9797
     98= 1.0.3 =
     99* array_column bugfix
     100
    98101= 1.0.2 =
    99102* Blurred credentials
  • sendmachine/tags/1.0.3/sendmachine_wp.php

    r1239889 r1265625  
    55  Plugin URI: https://www.sendmachine.com
    66  Description: The official Sendmachine plugin featuring subscribe forms, users sync, news feed, email sending and transactional campaigns.
    7   Version: 1.0.2
     7  Version: 1.0.3
    88  Author: Sendmachine team
    99  Author URI: http://developers.sendmachine.com/
     
    2323}
    2424
     25require_once SM_PLUGIN_DIR . 'includes/utils.php';
    2526require_once SM_PLUGIN_DIR . 'sendmachine_widget.php';
    2627require_once SM_PLUGIN_DIR . 'sendmachine_wp_admin.php';
     
    2930require_once SM_PLUGIN_DIR . 'includes/sendmachine_email_manager.php';
    3031require_once SM_PLUGIN_DIR . 'includes/sendmachine_feed_manager.php';
    31 require_once SM_PLUGIN_DIR . 'includes/utils.php';
    3232
    3333Sm_wp::init();
  • sendmachine/trunk/includes/utils.php

    r1226818 r1265625  
    2323
    2424}
     25
     26if (!function_exists('array_column')) {
     27    /**
     28     * Returns the values from a single column of the input array, identified by
     29     * the $columnKey.
     30     *
     31     * Optionally, you may provide an $indexKey to index the values in the returned
     32     * array by the values from the $indexKey column in the input array.
     33     *
     34     * @param array $input A multi-dimensional array (record set) from which to pull
     35     *                     a column of values.
     36     * @param mixed $columnKey The column of values to return. This value may be the
     37     *                         integer key of the column you wish to retrieve, or it
     38     *                         may be the string key name for an associative array.
     39     * @param mixed $indexKey (Optional.) The column to use as the index/keys for
     40     *                        the returned array. This value may be the integer key
     41     *                        of the column, or it may be the string key name.
     42     * @return array
     43     */
     44    function array_column($input = null, $columnKey = null, $indexKey = null)
     45    {
     46        // Using func_get_args() in order to check for proper number of
     47        // parameters and trigger errors exactly as the built-in array_column()
     48        // does in PHP 5.5.
     49        $argc = func_num_args();
     50        $params = func_get_args();
     51
     52        if ($argc < 2) {
     53            trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
     54            return null;
     55        }
     56
     57        if (!is_array($params[0])) {
     58            trigger_error(
     59                'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given',
     60                E_USER_WARNING
     61            );
     62            return null;
     63        }
     64
     65        if (!is_int($params[1])
     66            && !is_float($params[1])
     67            && !is_string($params[1])
     68            && $params[1] !== null
     69            && !(is_object($params[1]) && method_exists($params[1], '__toString'))
     70        ) {
     71            trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
     72            return false;
     73        }
     74
     75        if (isset($params[2])
     76            && !is_int($params[2])
     77            && !is_float($params[2])
     78            && !is_string($params[2])
     79            && !(is_object($params[2]) && method_exists($params[2], '__toString'))
     80        ) {
     81            trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
     82            return false;
     83        }
     84
     85        $paramsInput = $params[0];
     86        $paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
     87
     88        $paramsIndexKey = null;
     89        if (isset($params[2])) {
     90            if (is_float($params[2]) || is_int($params[2])) {
     91                $paramsIndexKey = (int) $params[2];
     92            } else {
     93                $paramsIndexKey = (string) $params[2];
     94            }
     95        }
     96
     97        $resultArray = array();
     98
     99        foreach ($paramsInput as $row) {
     100            $key = $value = null;
     101            $keySet = $valueSet = false;
     102
     103            if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
     104                $keySet = true;
     105                $key = (string) $row[$paramsIndexKey];
     106            }
     107
     108            if ($paramsColumnKey === null) {
     109                $valueSet = true;
     110                $value = $row;
     111            } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
     112                $valueSet = true;
     113                $value = $row[$paramsColumnKey];
     114            }
     115
     116            if ($valueSet) {
     117                if ($keySet) {
     118                    $resultArray[$key] = $value;
     119                } else {
     120                    $resultArray[] = $value;
     121                }
     122            }
     123
     124        }
     125
     126        return $resultArray;
     127    }
     128}
  • sendmachine/trunk/readme.txt

    r1239893 r1265625  
    44Requires at least: 3.2.1
    55Tested up to: 4.3
    6 Stable tag: 1.0.2
     6Stable tag: 1.0.3
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    9696== Changelog ==
    9797
     98= 1.0.3 =
     99* array_column bugfix
     100
    98101= 1.0.2 =
    99102* Blurred credentials
  • sendmachine/trunk/sendmachine_wp.php

    r1239889 r1265625  
    55  Plugin URI: https://www.sendmachine.com
    66  Description: The official Sendmachine plugin featuring subscribe forms, users sync, news feed, email sending and transactional campaigns.
    7   Version: 1.0.2
     7  Version: 1.0.3
    88  Author: Sendmachine team
    99  Author URI: http://developers.sendmachine.com/
     
    2323}
    2424
     25require_once SM_PLUGIN_DIR . 'includes/utils.php';
    2526require_once SM_PLUGIN_DIR . 'sendmachine_widget.php';
    2627require_once SM_PLUGIN_DIR . 'sendmachine_wp_admin.php';
     
    2930require_once SM_PLUGIN_DIR . 'includes/sendmachine_email_manager.php';
    3031require_once SM_PLUGIN_DIR . 'includes/sendmachine_feed_manager.php';
    31 require_once SM_PLUGIN_DIR . 'includes/utils.php';
    3232
    3333Sm_wp::init();
Note: See TracChangeset for help on using the changeset viewer.