LLMS_Shortcode_User_Info
LifterLMS User Information Shortcode.
Description Description
Shortcode: [llms-user]
Source Source
File: includes/shortcodes/class-llms-shortcode-user-info.php
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | class LLMS_Shortcode_User_Info extends LLMS_Shortcode { /** * Shortcode tag * * @var string */ public $tag = 'llms-user' ; /** * Retrieves a list of keys that cannot be displayed by the shortcode. * * @since 5.0.0 * * @return string[] */ protected function get_blocklist() { /** * Filters the list of keys which cannot be displayed using the [user] shortcode * * @since 5.0.0 * * @param string[] $keys List of user and usermeta keys. */ return apply_filters( 'llms_user_info_shortcode_blocked_keys' , array ( 'user_pass' ) ); } /** * Retrieves an array of default attributes which are automatically merged * with the user submitted attributes and passed to $this->get_output() * * @since 5.0.0 * * @return array */ protected function get_default_attributes() { return array ( 'key' => '' , 'or' => '' , ); } /** * Retrieve the actual content of the shortcode * * $atts & $content are both filtered before being passed to get_output() * output is filtered so the return of get_output() doesn't need its own filter * * @since 5.0.0 * * @return string */ protected function get_output() { /** * Filters the user used to retrieve user information displayed by the [llms-user] shortcode * * @since 5.0.0 * * @param integer $user_id The WP_User ID of the currently logged-in user or `0` if no user logged in. */ $user_id = apply_filters( 'llms_user_info_shortcode_user_id' , get_current_user_id() ); $key = $this ->get_attribute( 'key' ); $default = $this ->get_attribute( 'or' ); if ( in_array( $key , $this ->get_blocklist(), true ) ) { return '' ; } // No user OR no key provided. if ( ! $user_id || ! $key ) { return $default ; } $user = new WP_User( $user_id ); $val = $user ->exists() ? $user ->get( $key ) : null; return ! empty ( $val ) && is_scalar ( $val ) ? $val : $default ; } /** * Merge user attributes with default attributes. * * @since 5.0.0 * * @param array $atts User-submitted shortcode attributes. * * @return array */ protected function set_attributes( $atts = array () ) { // Allow `key` attribute to be submitted without a key, eg: [llms-user first_name]. if ( isset( $atts [0] ) ) { $atts [ 'key' ] = $atts [0]; unset( $atts [0] ); } return parent::set_attributes( $atts ); } } |
Expand full source code Collapse full source code View on GitHub
Methods Methods
- get_blocklist — Retrieves a list of keys that cannot be displayed by the shortcode.
- get_default_attributes — Retrieves an array of default attributes which are automatically merged with the user submitted attributes and passed to $this->get_output()
- get_output — Retrieve the actual content of the shortcode
- set_attributes — Merge user attributes with default attributes.
Changelog Changelog
Version | Description |
---|---|
5.0.0 | Introduced. |