diff --git a/composer.json b/composer.json index 0c47ff4740..b8bce7d728 100644 --- a/composer.json +++ b/composer.json @@ -40,6 +40,9 @@ "phpcompatibility/php-compatibility": "^9.0" }, "autoload": { + "files": [ + "lib/Util/ArrayColumn.php" + ], "psr-4": { "MailPoet\\": "lib/", "Sudzy\\": "lib/Util/Sudzy" diff --git a/lib/Cron/Workers/Bounce.php b/lib/Cron/Workers/Bounce.php index e3a48c48ee..c4c175fc88 100644 --- a/lib/Cron/Workers/Bounce.php +++ b/lib/Cron/Workers/Bounce.php @@ -11,7 +11,7 @@ use MailPoet\Tasks\Subscribers\BatchIterator; use MailPoet\Models\Subscriber; use MailPoet\Services\Bridge; use MailPoet\Services\Bridge\API; -use MailPoet\Util\Helpers; +use function MailPoet\Util\array_column; if(!defined('ABSPATH')) exit; diff --git a/lib/Models/Newsletter.php b/lib/Models/Newsletter.php index 1320a46f87..2c3c65117b 100644 --- a/lib/Models/Newsletter.php +++ b/lib/Models/Newsletter.php @@ -6,6 +6,7 @@ use MailPoet\Tasks\Sending as SendingTask; use MailPoet\Util\Helpers; use MailPoet\Util\Security; use MailPoet\WP\Emoji; +use function MailPoet\Util\array_column; if(!defined('ABSPATH')) exit; diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index f6f0cc5c28..0f1c088329 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -1,12 +1,12 @@ subscriber_fields); } -} \ No newline at end of file +} diff --git a/lib/Subscribers/ImportExport/Import/Import.php b/lib/Subscribers/ImportExport/Import/Import.php index afa3d3ae48..c531d894af 100644 --- a/lib/Subscribers/ImportExport/Import/Import.php +++ b/lib/Subscribers/ImportExport/Import/Import.php @@ -11,6 +11,7 @@ use MailPoet\Models\SubscriberSegment; use MailPoet\Subscribers\ImportExport\ImportExportFactory; use MailPoet\Subscribers\Source; use MailPoet\Util\Helpers; +use function MailPoet\Util\array_column; class Import { public $subscribers_data; @@ -420,4 +421,4 @@ class Import { ); } } -} \ No newline at end of file +} diff --git a/lib/Tasks/Sending.php b/lib/Tasks/Sending.php index 995339ad6d..9d225da625 100644 --- a/lib/Tasks/Sending.php +++ b/lib/Tasks/Sending.php @@ -6,7 +6,7 @@ use Carbon\Carbon; use MailPoet\Models\ScheduledTask; use MailPoet\Models\ScheduledTaskSubscriber; use MailPoet\Models\SendingQueue; -use MailPoet\Util\Helpers; +use function MailPoet\Util\array_column; use MailPoet\WP\Functions as WPFunctions; if(!defined('ABSPATH')) exit; diff --git a/lib/Tasks/Subscribers/BatchIterator.php b/lib/Tasks/Subscribers/BatchIterator.php index a27dafcbb2..ced2a15fb5 100644 --- a/lib/Tasks/Subscribers/BatchIterator.php +++ b/lib/Tasks/Subscribers/BatchIterator.php @@ -2,7 +2,7 @@ namespace MailPoet\Tasks\Subscribers; use MailPoet\Models\ScheduledTaskSubscriber; -use MailPoet\Util\Helpers; +use function MailPoet\Util\array_column; if(!defined('ABSPATH')) exit; diff --git a/lib/Util/ArrayColumn.php b/lib/Util/ArrayColumn.php new file mode 100644 index 0000000000..b1164f686c --- /dev/null +++ b/lib/Util/ArrayColumn.php @@ -0,0 +1,79 @@ + '4']], 'id'))) { + return \array_column($input, $column_key, $index_key); + } + $argc = func_num_args(); + $params = func_get_args(); + if($argc < 2) { + trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING); + return null; + } + if(!is_array($params[0])) { + trigger_error( + 'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', + E_USER_WARNING + ); + return null; + } + if(!is_int($params[1]) + && !is_float($params[1]) + && !is_string($params[1]) + && $params[1] !== null + && !(is_object($params[1]) && method_exists($params[1], '__toString')) + ) { + trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING); + return false; + } + if(isset($params[2]) + && !is_int($params[2]) + && !is_float($params[2]) + && !is_string($params[2]) + && !(is_object($params[2]) && method_exists($params[2], '__toString')) + ) { + trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING); + return false; + } + $params_input = $params[0]; + $params_column_key = ($params[1] !== null) ? (string)$params[1] : null; + $params_index_key = null; + if(isset($params[2])) { + if(is_float($params[2]) || is_int($params[2])) { + $params_index_key = (int)$params[2]; + } else { + $params_index_key = (string)$params[2]; + } + } + $result_array = []; + foreach($params_input as $row) { + $key = $value = null; + $key_set = $value_set = false; + if($params_index_key !== null && array_key_exists($params_index_key, $row)) { + $key_set = true; + $key = (string)$row[$params_index_key]; + } + if($params_column_key === null) { + $value_set = true; + $value = $row; + } elseif(is_array($row) && array_key_exists($params_column_key, $row)) { + $value_set = true; + $value = $row[$params_column_key]; + } + if($value_set) { + if($key_set) { + $result_array[$key] = $value; + } else { + $result_array[] = $value; + } + } + } + return $result_array; +} diff --git a/tests/integration/Cron/Workers/BounceTest.php b/tests/integration/Cron/Workers/BounceTest.php index aeb433e359..a88750ab1a 100644 --- a/tests/integration/Cron/Workers/BounceTest.php +++ b/tests/integration/Cron/Workers/BounceTest.php @@ -9,7 +9,6 @@ use MailPoet\Models\ScheduledTaskSubscriber; use MailPoet\Models\Setting; use MailPoet\Models\Subscriber; use MailPoet\Services\Bridge\API; -use MailPoet\Util\Helpers; require_once('BounceTestMockAPI.php'); use MailPoet\Cron\Workers\Bounce\BounceTestMockAPI as MockAPI; @@ -131,4 +130,4 @@ class BounceTest extends \MailPoetTest { \ORM::raw_execute('TRUNCATE ' . ScheduledTaskSubscriber::$_table); \ORM::raw_execute('TRUNCATE ' . Subscriber::$_table); } -} \ No newline at end of file +} diff --git a/tests/integration/Subscribers/ImportExport/Import/ImportTest.php b/tests/integration/Subscribers/ImportExport/Import/ImportTest.php index c1b3e5c95c..eb5f82a243 100644 --- a/tests/integration/Subscribers/ImportExport/Import/ImportTest.php +++ b/tests/integration/Subscribers/ImportExport/Import/ImportTest.php @@ -7,7 +7,6 @@ use MailPoet\Models\Subscriber; use MailPoet\Models\SubscriberCustomField; use MailPoet\Models\SubscriberSegment; use MailPoet\Subscribers\ImportExport\Import\Import; -use MailPoet\Util\Helpers; class ImportTest extends \MailPoetTest { function _before() { @@ -487,4 +486,4 @@ class ImportTest extends \MailPoetTest { \ORM::raw_execute('TRUNCATE ' . CustomField::$_table); \ORM::raw_execute('TRUNCATE ' . SubscriberCustomField::$_table); } -} \ No newline at end of file +}