| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP-Members Utility Functions |
| 4 |
* |
| 5 |
* Handles primary functions that are carried out in most |
| 6 |
* situations. Includes commonly used utility functions. |
| 7 |
* |
| 8 |
* This file is part of the WP-Members plugin by Chad Butler |
| 9 |
* You can find out more about this plugin at https://rocketgeek.com |
| 10 |
* Copyright (c) 2006-2026 Chad Butler |
| 11 |
* WP-Members(tm) is a trademark of butlerblog.com |
| 12 |
* |
| 13 |
* @package WP-Members |
| 14 |
* @subpackage WP-Members Utility Functions |
| 15 |
* @author Chad Butler |
| 16 |
* @copyright 2006-2026 |
| 17 |
*/ |
| 18 |
|
| 19 |
if ( ! function_exists( 'wpmem_securify' ) ): |
| 20 |
/** |
| 21 |
* The Securify Content Filter. |
| 22 |
* |
| 23 |
* This is the primary function that picks up where wpmem() leaves off. |
| 24 |
* Determines whether content is shown or hidden for both post and pages. |
| 25 |
* Since 3.0, this function is an alias for $wpmem->do_securify(). |
| 26 |
* |
| 27 |
* @since 2.0.0 |
| 28 |
* @since 3.0.0 Now an alias for $wpmem->do_securify(). |
| 29 |
* @since 3.2.4 Moved to utility API (could be deprecated). |
| 30 |
* |
| 31 |
* @global object $wpmem The WP-Members object class. |
| 32 |
* |
| 33 |
* @param string $content Content of the current post. |
| 34 |
* @return string $content Content of the current post or replaced content if post is blocked and user is not logged in. |
| 35 |
*/ |
| 36 |
function wpmem_securify( $content = null ) { |
| 37 |
global $wpmem; |
| 38 |
return $wpmem->do_securify( $content ); |
| 39 |
} |
| 40 |
endif; |
| 41 |
|
| 42 |
/** |
| 43 |
* Sets an array of user meta fields to be excluded from update/insert. |
| 44 |
* |
| 45 |
* @since 2.9.3 |
| 46 |
* @since Unknown Now an alias for get_excluded_fields(). |
| 47 |
* @since 3.3.9 excluded_fields() moved to forms object class. |
| 48 |
* |
| 49 |
* @param string $tag A tag so we know where the function is being used. |
| 50 |
* @return array Array of fields to be excluded from the registration form. |
| 51 |
*/ |
| 52 |
function wpmem_get_excluded_meta( $tag ) { |
| 53 |
global $wpmem; |
| 54 |
return $wpmem->forms->excluded_fields( $tag ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Forces a URL to be secure (ssl). |
| 59 |
* |
| 60 |
* @since 3.2.3 |
| 61 |
* @since 3.4.0 Now an alias for rktgk_force_ssl() |
| 62 |
* |
| 63 |
* @param string $url URL to be make secure. |
| 64 |
* @return string The secure URL. |
| 65 |
*/ |
| 66 |
function wpmem_force_ssl( $url ) { |
| 67 |
return rktgk_force_ssl( $url ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Log debugging errors. |
| 72 |
* |
| 73 |
* @since 3.1.2 |
| 74 |
* @since 3.4.0 Now an alias for rktgk_write_log(). |
| 75 |
* |
| 76 |
* @param mixed (string|array|object) $log Information to write in the WP debug file. |
| 77 |
*/ |
| 78 |
function wpmem_write_log( $log ) { |
| 79 |
rktgk_write_log( $log ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* String manipulation utility. |
| 84 |
* |
| 85 |
* Manipulates a given string based on the location of another string to return |
| 86 |
* a requested part or parts of the original string. For extracting a string |
| 87 |
* to get what is before or after, the returned result is a string. If the |
| 88 |
* string is requested to be "split" by the needle string, an array containing |
| 89 |
* the parts before, after, and the "needle" are returned. |
| 90 |
* |
| 91 |
* @since 3.2.0 |
| 92 |
* @since 3.4.0 Now an alias for rktgk_get_sub_str(). |
| 93 |
* |
| 94 |
* @param string $needle |
| 95 |
* @param string $haystack |
| 96 |
* @param string $position (before|after|split default: 'after') |
| 97 |
* @param boolean $keep_needle (default:true) |
| 98 |
* @return string|array $new { |
| 99 |
* An array of the original string, as split by the "needle" string. |
| 100 |
* |
| 101 |
* @type string $before |
| 102 |
* @type string $after |
| 103 |
* @type string $needle |
| 104 |
* } |
| 105 |
*/ |
| 106 |
function wpmem_get_sub_str( $needle, $haystack, $position = 'after', $keep_needle = true ) { |
| 107 |
return rktgk_get_sub_str( $needle, $haystack, $position, $keep_needle ); |
| 108 |
} |
| 109 |
|
| 110 |
if ( ! function_exists( 'wpmem_do_excerpt' ) ): |
| 111 |
/** |
| 112 |
* Creates an excerpt on the fly if there is no 'more' tag. |
| 113 |
* |
| 114 |
* @since 2.6 |
| 115 |
* @since 3.2.3 Now an alias for WP_Members::do_excerpt(). |
| 116 |
* |
| 117 |
* @global object $wpmem The WP_Members object. |
| 118 |
* |
| 119 |
* @param string $content |
| 120 |
* @return string $content |
| 121 |
*/ |
| 122 |
function wpmem_do_excerpt( $content ) { |
| 123 |
global $wpmem; |
| 124 |
$content = $wpmem->do_excerpt( $content ); |
| 125 |
return $content; |
| 126 |
} |
| 127 |
endif; |
| 128 |
|
| 129 |
if ( ! function_exists( 'wpmem_texturize' ) ): |
| 130 |
/** |
| 131 |
* Overrides the wptexturize filter. |
| 132 |
* |
| 133 |
* Currently only used for the login form to remove the <br> tag that WP puts in after the "Remember Me". |
| 134 |
* |
| 135 |
* @since 2.6.4 |
| 136 |
* @since 3.2.3 Now an alias for WP_Members::texturize(). |
| 137 |
* @deprecated 3.4.0. No replacement available. |
| 138 |
* |
| 139 |
* @todo Possibly deprecate or severely alter this process as its need may be obsolete. |
| 140 |
* |
| 141 |
* @global object $wpmem |
| 142 |
* @param string $content |
| 143 |
* @return string $new_content |
| 144 |
*/ |
| 145 |
function wpmem_texturize( $content ) { |
| 146 |
global $wpmem; |
| 147 |
//return $wpmem->texturize( $content ); |
| 148 |
return $content; |
| 149 |
} |
| 150 |
endif; |
| 151 |
|
| 152 |
/** |
| 153 |
* Inserts array items at a specific point in an array. |
| 154 |
* |
| 155 |
* @since 3.1.6 |
| 156 |
* @since 3.2.3 Moved to utilities api. |
| 157 |
* @since 3.4.0 Now an alias for rktgk_array_insert() |
| 158 |
* |
| 159 |
* @param array $array Original array. |
| 160 |
* @param array $new Array of new items to insert into $array. |
| 161 |
* @param string $key Array key to insert new items before or after. |
| 162 |
* @param string $loc Location to insert relative to $key (before|after) default:after. |
| 163 |
* @return array Original array with new items inserted. |
| 164 |
*/ |
| 165 |
function wpmem_array_insert( array $array, array $new, $key, $loc = 'after' ) { |
| 166 |
return rktgk_array_insert( $array, $new, $key, $loc ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Wrapper for load_dropins() |
| 171 |
* |
| 172 |
* @since 3.1.4 |
| 173 |
* @since 3.2.3 Moved to utilities api. |
| 174 |
* |
| 175 |
* @global object $wpmem The WP_Members object. |
| 176 |
*/ |
| 177 |
function wpmem_load_dropins() { |
| 178 |
global $wpmem; |
| 179 |
$wpmem->load_dropins(); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Display a localized date based on the WP date format setting. |
| 184 |
* |
| 185 |
* @since 3.2.4 |
| 186 |
* @since 3.4.0 Now an alias for rktgk_format_date(). |
| 187 |
* |
| 188 |
* @param mixed $args |
| 189 |
* @return date $date |
| 190 |
*/ |
| 191 |
function wpmem_format_date( $args ) { |
| 192 |
/** |
| 193 |
* Filter the date display and format settings. |
| 194 |
* |
| 195 |
* @since 3.2.4 |
| 196 |
* @deprecated 3.4.0 Use rktgk_format_date instead. |
| 197 |
* |
| 198 |
* @param array $args |
| 199 |
*/ |
| 200 |
$args = apply_filters_deprecated( 'wpmem_format_date_args', array( $args ), '3.4.0', 'rktgk_format_date' ); |
| 201 |
return rktgk_format_date( $args ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Call a shortcode function by tag name. |
| 206 |
* |
| 207 |
* Use this function for directly calling a shortcode without using do_shortcode. |
| 208 |
* do_shortcode() runs an extensive regex that goes through every shortcode in |
| 209 |
* the WP global $shortcode_tags. That's a lot of processing wasted if all you |
| 210 |
* want to do is run a specific shortcode/function. Yes, you could run the callback |
| 211 |
* directly, but what if that callback is in a class instance method? This utlitiy |
| 212 |
* allows you to run a shortcode function directly, regardless of whether it is |
| 213 |
* a direct function or in a class. It comes from an article by J.D. Grimes on this |
| 214 |
* subject and I've provided a link to that article. |
| 215 |
* |
| 216 |
* @author J.D. Grimes |
| 217 |
* @link https://codesymphony.co/dont-do_shortcode/ |
| 218 |
* |
| 219 |
* @since 3.2.5 |
| 220 |
* @since 3.4.0 Now an alias for rktgk_do_shortcode(). |
| 221 |
* |
| 222 |
* @param string $tag The shortcode whose function to call. |
| 223 |
* @param array $atts The attributes to pass to the shortcode function. Optional. |
| 224 |
* @param array $content The shortcode's content. Default is null (none). |
| 225 |
* |
| 226 |
* @return string|bool False on failure, the result of the shortcode on success. |
| 227 |
*/ |
| 228 |
function wpmem_do_shortcode( $tag, array $atts = array(), $content = null ) { |
| 229 |
return rktgk_do_shortcode( $tag, $atts, $content ); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Checks if a password is part of registration. |
| 234 |
* |
| 235 |
* Used for moderated registration to determine if a user sets their |
| 236 |
* own password at registration. If so, password is not set during |
| 237 |
* user activation. |
| 238 |
* |
| 239 |
* @since 3.3.0 |
| 240 |
* |
| 241 |
* @return bool $chk_pass true if the user creates a password at registration, otherwise false. |
| 242 |
*/ |
| 243 |
function wpmem_user_sets_password() { |
| 244 |
$chk_pass = false; |
| 245 |
$wpmem_fields = wpmem_fields(); |
| 246 |
foreach ( $wpmem_fields as $field ) { |
| 247 |
if ( $field['type'] == 'password' && $field['register'] ) { |
| 248 |
$chk_pass = true; |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
return $chk_pass; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Better unserialization than WP's maybe_unserialize(). |
| 257 |
* |
| 258 |
* Sanitizes array output before returning. If the unserialized result is an |
| 259 |
* array, then it runs the result through wpmem_sanitize_array(), which |
| 260 |
* sanitizes each individual array element. |
| 261 |
* |
| 262 |
* @since 3.3.0 |
| 263 |
* @since 3.4.0 Now an alias for rktgk_maybe_unserialize(). |
| 264 |
* |
| 265 |
* @param mixed $original |
| 266 |
* @return mixed $original |
| 267 |
*/ |
| 268 |
function wpmem_maybe_unserialize( $original ) { |
| 269 |
return rktgk_maybe_unserialize( $original ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Determines whether to use a .min suffix for a script/style file. |
| 274 |
* |
| 275 |
* @since 3.3.0 |
| 276 |
* @since 3.4.0 Now an alias for rktgk_get_suffix(). |
| 277 |
* |
| 278 |
* @param boolean $echo |
| 279 |
*/ |
| 280 |
function wpmem_get_suffix( $echo = false ) { |
| 281 |
return rktgk_get_suffix( $echo ); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* A utility to determine a redirect_to param. |
| 286 |
* |
| 287 |
* @since 3.4.0 |
| 288 |
* |
| 289 |
* @param array $args |
| 290 |
* @return string $redirect_to |
| 291 |
*/ |
| 292 |
function wpmem_get_redirect_to( $args = array() ) { |
| 293 |
// redirect_to in the form or URL will override a redirect set in the form args. |
| 294 |
if ( isset( $_REQUEST['redirect_to'] ) ) { |
| 295 |
$redirect_to = $_REQUEST['redirect_to']; |
| 296 |
} else { |
| 297 |
if ( isset( $args['redirect_to'] ) ) { |
| 298 |
$raw_redirect_to = $args['redirect_to']; |
| 299 |
// Is it a URL? |
| 300 |
$redirect_to = ( false == filter_var( $raw_redirect_to, FILTER_VALIDATE_URL ) ) ? home_url( $raw_redirect_to ) : $raw_redirect_to; |
| 301 |
} else { |
| 302 |
$redirect_to = ( isset( $_SERVER['REQUEST_URI'] ) ) ? $_SERVER['REQUEST_URI'] : get_permalink(); |
| 303 |
} |
| 304 |
} |
| 305 |
return esc_url_raw( $redirect_to ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Creates an index file in a directory. |
| 310 |
* |
| 311 |
* @since 3.5.0 |
| 312 |
* |
| 313 |
* @param array $args { |
| 314 |
* The name, path, and contents of the file. |
| 315 |
* |
| 316 |
* @type string $path |
| 317 |
* @type string $name |
| 318 |
* @type string $contents |
| 319 |
* } |
| 320 |
*/ |
| 321 |
function wpmem_create_file( $args ) { |
| 322 |
$check_file = trailingslashit( $args['path'] ) . $args['name']; |
| 323 |
if ( ! file_exists( $check_file ) ) { |
| 324 |
$file = fopen( $check_file, "w" ); |
| 325 |
fwrite( $file, $args['contents'] ); |
| 326 |
fclose( $file ); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Gets plugin upload base dir. |
| 332 |
* |
| 333 |
* @since 3.5.0 |
| 334 |
* |
| 335 |
* @return string $wpmem->upload_base |
| 336 |
*/ |
| 337 |
function wpmem_get_upload_base() { |
| 338 |
global $wpmem; |
| 339 |
return ( isset( $wpmem->upload_base ) ) ? $wpmem->upload_base : "wpmembers"; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Gets WP-Members upload dirs as an extension of wp_upload_dir(). |
| 344 |
* |
| 345 |
* @since 3.5.0 |
| 346 |
* |
| 347 |
* @return array { |
| 348 |
* @type string $upload_vars |
| 349 |
* @type string $base_dir |
| 350 |
* @type string $wpmem_base_dir |
| 351 |
* @type string $wpmem_user_files_dir |
| 352 |
* } |
| 353 |
*/ |
| 354 |
function wpmem_upload_dir() { |
| 355 |
$upload_vars = wp_upload_dir( null, false ); |
| 356 |
$base_dir = $upload_vars['basedir']; |
| 357 |
$wpmem_base_dir = trailingslashit( trailingslashit( $base_dir ) . wpmem_get_upload_base() ); |
| 358 |
$wpmem_user_files_dir = $wpmem_base_dir . trailingslashit( wpmem_get_file_dir_hash() ); |
| 359 |
return array( |
| 360 |
'upload_vars' => $upload_vars, |
| 361 |
'base_dir' => $base_dir, |
| 362 |
'wpmem_base_dir' => $wpmem_base_dir, |
| 363 |
'wpmem_user_files_dir' => $wpmem_user_files_dir |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Creates a randomized file name. |
| 369 |
* |
| 370 |
* @since 3.5.5 |
| 371 |
* |
| 372 |
* @param string $filename |
| 373 |
* @return string $key.$ext |
| 374 |
*/ |
| 375 |
function wpmem_hash_file_name( $filename ) { |
| 376 |
// How long a random string do we want? |
| 377 |
$hash_len = 36; |
| 378 |
|
| 379 |
// Get the file extension. |
| 380 |
$ext = pathinfo( $filename, PATHINFO_EXTENSION ); |
| 381 |
|
| 382 |
if ( preg_match( '/^[a-f0-9]{'. $hash_len . '}-.*/', $filename ) ) { |
| 383 |
$filename = substr( $filename, $hash_len + 1 ); |
| 384 |
} |
| 385 |
|
| 386 |
$key = sha1( random_bytes(32) ); |
| 387 |
$key = substr( $key, 0, $hash_len ); |
| 388 |
|
| 389 |
$filename = "$key.$ext"; |
| 390 |
/** |
| 391 |
* Filter the hashed file name. |
| 392 |
* |
| 393 |
* @since 3.5.5 |
| 394 |
* |
| 395 |
* @param string $filename |
| 396 |
* @param int $hash_len |
| 397 |
* @param string $key |
| 398 |
* @param string $ext |
| 399 |
*/ |
| 400 |
return apply_filters( 'wpmem_hashed_file_name', $filename, $hash_len, $key, $ext ); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Gets or creates a user directory hash. |
| 405 |
* |
| 406 |
* @since 3.5.5 |
| 407 |
* |
| 408 |
* @param int $user_id |
| 409 |
* @return string $user_dir_hash |
| 410 |
*/ |
| 411 |
function wpmem_get_user_dir_hash( $user_id ) { |
| 412 |
$hash_len = 36; |
| 413 |
$user_dir_hash = get_user_meta( $user_id, 'wpmem_user_dir_hash', true ); |
| 414 |
if ( ! $user_dir_hash ) { |
| 415 |
$uid_len = strlen( $user_id ); |
| 416 |
$user_dir_hash = $user_id . wp_generate_password( ( $hash_len-$uid_len ), false, false ); |
| 417 |
update_user_meta( $user_id, 'wpmem_user_dir_hash', $user_dir_hash ); |
| 418 |
} |
| 419 |
/** |
| 420 |
* Filter the user directory hash. |
| 421 |
* |
| 422 |
* @since 3.5.5 |
| 423 |
* |
| 424 |
* @param string $user_dir_hash |
| 425 |
* @param int $hash_len |
| 426 |
* @param int $user_id |
| 427 |
*/ |
| 428 |
return apply_filters( 'wpmem_user_dir_hash', $user_dir_hash, $hash_len, $user_id ); |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Gets or creates the file directory hash. |
| 433 |
* |
| 434 |
* @since 3.5.5 |
| 435 |
* |
| 436 |
* @return string $dir_hash |
| 437 |
*/ |
| 438 |
function wpmem_get_file_dir_hash() { |
| 439 |
$hash_len = 36; |
| 440 |
$dir_hash = get_option( 'wpmem_file_dir_hash' ); |
| 441 |
if ( ! $dir_hash ) { |
| 442 |
$dir_hash = wp_generate_password( $hash_len, false, false ); |
| 443 |
update_option( 'wpmem_file_dir_hash', $dir_hash ); |
| 444 |
} |
| 445 |
/** |
| 446 |
* Filter the file directory hash. |
| 447 |
* |
| 448 |
* @since 3.5.5 |
| 449 |
* |
| 450 |
* @param string $dir_hash |
| 451 |
* @param int $hash_len |
| 452 |
*/ |
| 453 |
return apply_filters( 'wpmem_file_dir_hash', 'user_files_' . $dir_hash, $hash_len ); |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* Reads a csv file to a keyed array. |
| 458 |
* |
| 459 |
* @since 3.5.4 |
| 460 |
* |
| 461 |
* @todo If using $cols args, must use non-BOM UTF-8 encoding, otherwise the first row fails. |
| 462 |
* |
| 463 |
* @param string $file The file path (absolute). |
| 464 |
* @param array $cols If the file does not have a header row, an array to key it by (optional). |
| 465 |
*/ |
| 466 |
function wpmem_csv_to_array( $file, $cols = false ) { |
| 467 |
|
| 468 |
// Get rows into array. |
| 469 |
$f = fopen( $file, 'r' ); |
| 470 |
while ( ( $line = fgetcsv( $f ) ) !== FALSE ) { |
| 471 |
//$line is an array of the csv elements |
| 472 |
$rows[] = $line; |
| 473 |
} |
| 474 |
fclose( $f ); |
| 475 |
|
| 476 |
// If first row is header row of column names. |
| 477 |
if ( ! $cols ) { |
| 478 |
$cols = array_shift( $rows ); |
| 479 |
} |
| 480 |
|
| 481 |
// Clean the header if BOM. |
| 482 |
foreach( $cols as $h ) { |
| 483 |
$cleaned_head[] = preg_replace( "/[^\w\d]/", "", esc_attr( $h ) ); |
| 484 |
} |
| 485 |
|
| 486 |
// Build our array for return. |
| 487 |
$csv = array(); |
| 488 |
foreach( $rows as $row ) { |
| 489 |
$csv[] = array_combine( $cleaned_head, $row ); |
| 490 |
} |
| 491 |
|
| 492 |
return $csv; |
| 493 |
} |