| 1 |
<?php |
| 2 |
/** |
| 3 |
* The WP_Members API Class. |
| 4 |
* |
| 5 |
* @package WP-Members |
| 6 |
* @subpackage WP_Members API Object Class |
| 7 |
* @since 3.1.1 |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit(); |
| 13 |
} |
| 14 |
|
| 15 |
class WP_Members_API { |
| 16 |
|
| 17 |
public $file_user_id; // A container for the uploaded file user ID. |
| 18 |
|
| 19 |
/** |
| 20 |
* Plugin initialization function. |
| 21 |
* |
| 22 |
* @since 3.1.1 |
| 23 |
*/ |
| 24 |
function __construct() { |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Get field keys by meta. |
| 30 |
* |
| 31 |
* @since 3.1.1 |
| 32 |
* |
| 33 |
* @return array $field_keys |
| 34 |
*/ |
| 35 |
function get_field_keys_by_meta() { |
| 36 |
$field_keys = array(); |
| 37 |
foreach ( wpmem_fields() as $key => $field ) { |
| 38 |
$field_keys[ $field[2] ] = $key; |
| 39 |
} |
| 40 |
return $field_keys; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get select field display values. |
| 45 |
* |
| 46 |
* @since 3.1.1 |
| 47 |
* |
| 48 |
* @param string $meta_key The field's meta key. |
| 49 |
* @return array $display_values { |
| 50 |
* The field's display values in an array. |
| 51 |
* Elements are stored_value => display value |
| 52 |
* |
| 53 |
* @type string The display value. |
| 54 |
* } |
| 55 |
*/ |
| 56 |
function get_select_display_values( $meta_key ) { |
| 57 |
$keys = $this->get_field_keys_by_meta(); |
| 58 |
$fields = wpmem_fields(); |
| 59 |
$raw = $fields[ $keys[ $meta_key ] ][7]; |
| 60 |
$delimiter = ( isset( $fields[ $keys[ $meta_key ][8] ] ) ) ? $fields[ $keys[ $meta_key ][8] ] : '|'; |
| 61 |
$display_values = array(); |
| 62 |
foreach ( $raw as $value ) { |
| 63 |
$pieces = explode( $delimiter, trim( $value ) ); |
| 64 |
if ( $pieces[1] != '' ) { |
| 65 |
$display_values[ $pieces[1] ] = $pieces[0]; |
| 66 |
} |
| 67 |
} |
| 68 |
return $display_values; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Gets the display/label value of a field. |
| 73 |
* |
| 74 |
* @since 3.1.8 |
| 75 |
* |
| 76 |
* @param string $meta The field meta key. |
| 77 |
* @param string $user_id The user's ID. |
| 78 |
* @param string $value The field's value, if given. |
| 79 |
* @return string $value The display value. |
| 80 |
*/ |
| 81 |
function get_field_display_value( $meta, $user_id, $value = null ) { |
| 82 |
$fields = wpmem_fields('all'); |
| 83 |
$field = $fields[ $meta ]; |
| 84 |
$value = ( $value ) ? $value : get_user_meta( $user_id, $meta, true ); |
| 85 |
/* |
| 86 |
* Nothing more to do for string values. The following switch |
| 87 |
* handles values that may be something other than a simple string. |
| 88 |
*/ |
| 89 |
switch ( $field['type'] ) { |
| 90 |
case 'multiselect': |
| 91 |
case 'multicheckbox': |
| 92 |
// @todo Right now this is the saved value as a string, not a "display" value. |
| 93 |
// Two possibilities: delimited string (WP-Members) or serialized (WooCommerce) |
| 94 |
if ( is_array( $value ) ) { |
| 95 |
$value = implode( $field['delimiter'], $value ); |
| 96 |
} |
| 97 |
break; |
| 98 |
case 'select': |
| 99 |
case 'radio': |
| 100 |
$value = $field['options'][ $value ]; |
| 101 |
break; |
| 102 |
case 'image': |
| 103 |
case 'file': |
| 104 |
$value = wp_get_attachment_url( $value ); |
| 105 |
break; |
| 106 |
} |
| 107 |
return $value; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Checks that a given user field value is unique. |
| 112 |
* |
| 113 |
* @since 3.1.1 |
| 114 |
* |
| 115 |
* @param string $key The field being checked. |
| 116 |
* @param string $val The value to check. |
| 117 |
* @return bool True if value if is unique. |
| 118 |
*/ |
| 119 |
function is_user_value_unique( $key, $val ) { |
| 120 |
|
| 121 |
if ( $this->is_field_in_wp_users( $key ) ) { |
| 122 |
$args = array( 'search' => $val, 'fields' => 'ID' ); |
| 123 |
} else { |
| 124 |
$args = array( 'meta_query' => array( array( |
| 125 |
'key' => $key, |
| 126 |
'value' => $val, |
| 127 |
'compare' => '=', |
| 128 |
) ) ); |
| 129 |
} |
| 130 |
|
| 131 |
$users = get_users( $args ); |
| 132 |
|
| 133 |
// If there is data in $users, the value is not unique. |
| 134 |
return ( $users ) ? false : true; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Checks counter for next available number and updates the counter. |
| 139 |
* |
| 140 |
* @since 3.1.1 |
| 141 |
* |
| 142 |
* @param string $option The wp_options name for the counter setting (required). |
| 143 |
* @param int $start Number to start with (optional, default 0). |
| 144 |
* @param int $increment Number to increment by (optional, default 1). |
| 145 |
* @return int $number Next number in the series. |
| 146 |
*/ |
| 147 |
function get_incremental_number( $option, $start = 0, $increment = 1 ) { |
| 148 |
|
| 149 |
// Get current number from settings |
| 150 |
$number = get_option( $option ); |
| 151 |
|
| 152 |
// If there is no number, start with $start. |
| 153 |
if ( ! $number ) { |
| 154 |
$number = ( $start <= 0 ) ? $start : $start - $increment; |
| 155 |
} |
| 156 |
|
| 157 |
// Increment the number and save the setting. |
| 158 |
$number = $number + $increment; |
| 159 |
update_option( $option, $number, false ); |
| 160 |
|
| 161 |
// Return the number. |
| 162 |
return $number; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Generates a unique membership number based on settings. |
| 167 |
* |
| 168 |
* @since 3.1.1 |
| 169 |
* @since 3.2.0 Changed "lead" value to "pad". |
| 170 |
* |
| 171 |
* @param array $args { |
| 172 |
* @type string $option The wp_options name for the counter setting (required). |
| 173 |
* @type string $meta_key The field's meta key (required). |
| 174 |
* @type int $start Number to start with (optional, default 0). |
| 175 |
* @type int $increment Number to increment by (optional, default 1). |
| 176 |
* @type int $digits Number of digits for the number (optional). |
| 177 |
* @type boolen $pad Pad leading zeros (optional, default true). |
| 178 |
* } |
| 179 |
* @return string $mem_number |
| 180 |
*/ |
| 181 |
function generate_membership_number( $args ) { |
| 182 |
$defaults = array( |
| 183 |
'start' => 0, |
| 184 |
'increment' => 1, |
| 185 |
'digits' => 8, |
| 186 |
'pad' => true, |
| 187 |
); |
| 188 |
$args = wp_parse_args( $args, $defaults ); |
| 189 |
do { |
| 190 |
// Generate the appropriate next number |
| 191 |
$number = $this->get_incremental_number( $args['option'], $args['start'], $args['increment'] ); |
| 192 |
|
| 193 |
// Cast as string, not integer. |
| 194 |
$mem_number = (string)$number; |
| 195 |
|
| 196 |
// Add leading zeros if less than three digits. |
| 197 |
if ( strlen( $mem_number ) < $args['digits'] ) { |
| 198 |
$mem_number = ( $args['pad'] ) ? str_pad( $mem_number, $args['digits'], '0', STR_PAD_LEFT ) : $mem_number; |
| 199 |
} |
| 200 |
} while ( true !== $this->is_user_value_unique( $args['meta_key'], $mem_number ) ); |
| 201 |
return $mem_number; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Checks if a given setting is set and enabled. |
| 206 |
* |
| 207 |
* @since 3.1.7 |
| 208 |
* |
| 209 |
* @global object $wpmem |
| 210 |
* @param string $setting |
| 211 |
* @return boolean |
| 212 |
*/ |
| 213 |
function is_enabled( $setting ) { |
| 214 |
return ( isset( $wpmem->{$setting} ) && $wpmem->{$setting} ) ? true : false; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Returns a list of wp_users fields. |
| 219 |
* |
| 220 |
* @since 3.5.2 |
| 221 |
*/ |
| 222 |
public function get_wp_users_fields() { |
| 223 |
return array( 'ID','user_login','user_pass','user_nicename','user_email','user_url','user_registered','user_activation_key','user_status','display_name' ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Checks if a field is a wp_users field. |
| 228 |
* A return of false would indicate it is a meta field. |
| 229 |
* |
| 230 |
* @since 3.5.2 |
| 231 |
* |
| 232 |
* @param string $meta |
| 233 |
* @return bool |
| 234 |
*/ |
| 235 |
public function is_field_in_wp_users( $meta ) { |
| 236 |
return ( in_array( $meta, $this->get_wp_users_fields() ) ) ? true : false; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Uploads file from the user. |
| 241 |
* |
| 242 |
* @since 3.1.0 |
| 243 |
* @since 3.5.5 Moved to API class. |
| 244 |
* |
| 245 |
* @param array $file |
| 246 |
* @param int $user_id |
| 247 |
* @return int|bool |
| 248 |
*/ |
| 249 |
function do_file_upload( $file = array(), $user_id = false ) { |
| 250 |
|
| 251 |
// Filter the upload directory. |
| 252 |
add_filter( 'upload_dir', array( &$this, 'file_upload_dir' ) ); |
| 253 |
add_filter( 'sanitize_file_name', 'wpmem_hash_file_name' ); |
| 254 |
|
| 255 |
// Set up user ID for use in upload process. |
| 256 |
$this->file_user_id = ( $user_id ) ? $user_id : 0; |
| 257 |
|
| 258 |
// Get WordPress file upload processing scripts. |
| 259 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 260 |
require_once( ABSPATH . 'wp-admin/includes/media.php' ); |
| 261 |
|
| 262 |
$file_return = wp_handle_upload( $file, array( 'test_form' => false ) ); |
| 263 |
|
| 264 |
remove_filter( 'sanitize_file_name', 'wpmem_hash_file_name' ); |
| 265 |
|
| 266 |
if ( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) { |
| 267 |
return false; |
| 268 |
} else { |
| 269 |
|
| 270 |
$attachment = array( |
| 271 |
'post_mime_type' => $file_return['type'], |
| 272 |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['name'] ) ), |
| 273 |
'post_content' => '', |
| 274 |
'post_status' => 'inherit', |
| 275 |
'guid' => $file_return['url'], |
| 276 |
'post_author' => ( $user_id ) ? $user_id : '', |
| 277 |
); |
| 278 |
|
| 279 |
$attachment_id = wp_insert_attachment( $attachment, $file_return['file'] ); |
| 280 |
|
| 281 |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 282 |
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_return['file'] ); |
| 283 |
wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
| 284 |
|
| 285 |
if ( 0 < intval( $attachment_id ) ) { |
| 286 |
// Returns an array with file information. |
| 287 |
return $attachment_id; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
return false; |
| 292 |
} // End upload_file() |
| 293 |
|
| 294 |
/** |
| 295 |
* Sets the file upload directory. |
| 296 |
* |
| 297 |
* This is a filter function for upload_dir. |
| 298 |
* |
| 299 |
* @link https://codex.wordpress.org/Plugin_API/Filter_Reference/upload_dir |
| 300 |
* |
| 301 |
* @since 3.1.0 |
| 302 |
* @since 3.5.5 Moved to API class. |
| 303 |
* @since 3.5.5 Updated to add a randomized hash to the user directories. |
| 304 |
* |
| 305 |
* @param array $param { |
| 306 |
* The directory information for upload. |
| 307 |
* |
| 308 |
* @type string $path |
| 309 |
* @type string $url |
| 310 |
* @type string $subdir |
| 311 |
* @type string $basedir |
| 312 |
* @type string $baseurl |
| 313 |
* @type string $error |
| 314 |
* } |
| 315 |
* @return array $param |
| 316 |
*/ |
| 317 |
function file_upload_dir( $param ) { |
| 318 |
|
| 319 |
$user_id = ( isset( $this->file_user_id ) ) ? $this->file_user_id : null; |
| 320 |
|
| 321 |
$file_dir = wpmem_get_file_dir_hash(); |
| 322 |
$user_dir = wpmem_get_user_dir_hash( $user_id ); |
| 323 |
|
| 324 |
$args = array( |
| 325 |
'user_id' => $user_id, |
| 326 |
'wpmem_dir' => wpmem_get_upload_base(), |
| 327 |
'user_dir' => trailingslashit( $file_dir ) . $user_dir, |
| 328 |
'basedir' => $param['basedir'], |
| 329 |
'baseurl' => $param['baseurl'], |
| 330 |
'file_hash' => $file_dir, |
| 331 |
'user_hash' => $user_dir, |
| 332 |
); |
| 333 |
|
| 334 |
/** |
| 335 |
* Filter the user directory elements. |
| 336 |
* |
| 337 |
* @since 3.1.0 |
| 338 |
* @since 3.5.5 Added base vals and hashes to args. |
| 339 |
* |
| 340 |
* @param array $args |
| 341 |
*/ |
| 342 |
$args = apply_filters( 'wpmem_user_upload_dir', $args ); |
| 343 |
|
| 344 |
$param['subdir'] = '/' . $args['wpmem_dir'] . '/' . $args['user_dir']; |
| 345 |
$param['path'] = $args['basedir'] . '/' . $args['wpmem_dir'] . '/' . $args['user_dir']; |
| 346 |
$param['url'] = $args['baseurl'] . '/' . $args['wpmem_dir'] . '/' . $args['user_dir']; |
| 347 |
|
| 348 |
return $param; |
| 349 |
} |
| 350 |
|
| 351 |
} // End of WP_Members_Utilties class. |