| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* List Server Base |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @since 141004 |
| 16 |
* @package s2Member\List_Servers |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_list_server_base')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* List Server Base |
| 25 |
* |
| 26 |
* @since 141004 |
| 27 |
* @package s2Member\List_Servers |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_list_server_base |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Validates args. |
| 33 |
* |
| 34 |
* @since 141004 |
| 35 |
* @package s2Member\List_Servers |
| 36 |
* |
| 37 |
* @param array $args Input arguments. |
| 38 |
* |
| 39 |
* @return \stdClass|null An object with only valid properties. |
| 40 |
* If unable to validate, this returns a `NULL` value. |
| 41 |
*/ |
| 42 |
public static function validate_args($args) |
| 43 |
{ |
| 44 |
if(!$args || !is_array($args)) |
| 45 |
return NULL; |
| 46 |
|
| 47 |
$defaults = array( |
| 48 |
'role' => '', |
| 49 |
'level' => '', |
| 50 |
'ccaps' => '', |
| 51 |
'login' => '', |
| 52 |
'pass' => '', |
| 53 |
'email' => '', |
| 54 |
'fname' => '', |
| 55 |
'lname' => '', |
| 56 |
'ip' => '', |
| 57 |
'opt_out' => FALSE, |
| 58 |
'opt_in' => FALSE, |
| 59 |
'double_opt_in' => FALSE, |
| 60 |
'user' => NULL, |
| 61 |
'user_id' => 0 |
| 62 |
); |
| 63 |
$args = array_merge($defaults, $args); |
| 64 |
$args = (object)array_intersect_key($args, $defaults); |
| 65 |
|
| 66 |
foreach($args as $_key => &$_value) |
| 67 |
switch($_key) // Typify. |
| 68 |
{ |
| 69 |
case 'role': |
| 70 |
case 'level': |
| 71 |
$_value = (string)$_value; |
| 72 |
break; |
| 73 |
|
| 74 |
case 'ccaps': // Input can be a string or an array. |
| 75 |
$_value = is_array($_value) ? implode(',', $_value) : (string)$_value; |
| 76 |
break; |
| 77 |
|
| 78 |
case 'login': |
| 79 |
case 'pass': |
| 80 |
case 'email': |
| 81 |
case 'fname': |
| 82 |
case 'lname': |
| 83 |
case 'ip': |
| 84 |
$_value = (string)$_value; |
| 85 |
break; |
| 86 |
|
| 87 |
case 'opt_in': |
| 88 |
case 'double_opt_in': |
| 89 |
$_value = (bool)$_value; |
| 90 |
break; |
| 91 |
|
| 92 |
case 'user': // A `WP_User` object instance. |
| 93 |
$_value = $_value instanceof WP_User ? $_value : NULL; |
| 94 |
break; |
| 95 |
|
| 96 |
case 'user_id': // User ID. |
| 97 |
$_value = (int)$_value; |
| 98 |
break; |
| 99 |
} |
| 100 |
unset($_key, $_value); // Housekeeping. |
| 101 |
|
| 102 |
if(!$args->user_id && $args->user && $args->user->exists()) |
| 103 |
$args->user_id = $args->user->ID; // Use this ID. |
| 104 |
$args->user = new WP_User($args->user_id); // Always based on ID. |
| 105 |
|
| 106 |
$args->ccaps = implode(',', c_ws_plugin__s2member_user_access::user_access_ccaps($args->user)); |
| 107 |
$args->fname = !$args->fname ? ucwords((string)strstr($args->email, '@', TRUE)) : $args->fname; |
| 108 |
$args->lname = !$args->lname ? '-' : $args->lname; // Default last name to `-` because MC requires this. |
| 109 |
$args->name = $args->fname || $args->lname ? trim($args->fname.' '.$args->lname) : ucwords((string)strstr($args->email, '@', TRUE)); |
| 110 |
|
| 111 |
if(!$args->role || !isset($args->level[0]) || !is_numeric($args->level) |
| 112 |
|| !$args->login || !$args->email || !is_email($args->email) |
| 113 |
|| !$args->user_id || !$args->user || !$args->user->exists() |
| 114 |
) return NULL; // Required arguments missing. |
| 115 |
|
| 116 |
return $args; // Now a \stdClass object. |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|