/** * User Meta Shortcode handler * Retrieve the value of a property or meta key from the users and usermeta tables. * usage: [user_meta user_id=1 key="first_name" size="50" wpautop="on" pre="Pre Label " post="Post Label "] * @param array $atts * @param string $content * @return stirng */ function user_meta_shortcode_handler($atts, $content = null) { if (!isset($atts['user_id'])) { $user = wp_get_current_user(); $atts['user_id'] = $user->ID; } if (!isset($atts['size'])) { $atts['size'] = '50'; } $user = new WP_User($atts['user_id']); if (!$user->exists()) { return; } if ($atts['key'] == 'avatar') { return $atts['pre'] . get_avatar($user->ID, $atts['size']) . $atts['post']; } if ($user->has_prop($atts['key'])) { if ($atts['wpautop'] == 'on') { $value = wpautop($user->get($atts['key'])); } else { $value = $user->get($atts['key']); } } if (!empty($value)) { return $atts['pre'] . $value . $atts['post']; } return; }
function test_has_prop() { $user_id = self::factory()->user->create(array('role' => 'author', 'user_login' => 'test_wp_user_has_prop', 'user_pass' => 'password', 'user_email' => '*****@*****.**')); $user = new WP_User($user_id); $this->assertTrue($user->has_prop('user_email')); $this->assertTrue($user->has_prop('use_ssl')); $this->assertFalse($user->has_prop('field_that_does_not_exist')); update_user_meta($user_id, 'dashed-key', 'abcdefg'); $this->assertTrue($user->has_prop('dashed-key')); }
/** * Retrieve user option that can be either per Site or per Network. * * If the user ID is not given, then the current user will be used instead. If * the user ID is given, then the user data will be retrieved. The filter for * the result, will also pass the original option name and finally the user data * object as the third parameter. * * The option will first check for the per site name and then the per Network name. * * @since 2.0.0 * @uses $wpdb WordPress database object for queries. * @uses apply_filters() Calls 'get_user_option_$option' hook with result, * option parameter, and user data object. * * @param string $option User option name. * @param int $user Optional. User ID. * @param bool $deprecated Use get_option() to check for an option in the options table. * @return mixed */ function get_user_option($option, $user = 0, $deprecated = '') { global $wpdb; if (!empty($deprecated)) { _deprecated_argument(__FUNCTION__, '3.0'); } if (empty($user)) { $user = wp_get_current_user(); } else { $user = new WP_User($user); } if (!isset($user->ID)) { return false; } if ($user->has_prop($wpdb->prefix . $option)) { // Blog specific $result = $user->get($wpdb->prefix . $option); } elseif ($user->has_prop($option)) { // User specific and cross-blog $result = $user->get($option); } else { $result = false; } return apply_filters("get_user_option_{$option}", $result, $option, $user); }
/** * Hide the SEO Title, Meta Desc and Focus KW columns if the user hasn't chosen which columns to hide * * @param array|false $result * @param string $option * @param WP_User $user * * @return array|false $result */ public function column_hidden($result, $option, $user) { global $wpdb; $prefix = $wpdb->get_blog_prefix(); if (!$user->has_prop($prefix . $option) && !$user->has_prop($option)) { if (!is_array($result)) { $result = array(); } array_push($result, 'wpseo-title', 'wpseo-metadesc', 'wpseo-focuskw'); } return $result; }
function test_has_prop() { $user = new WP_User(self::$author_id); $this->assertTrue($user->has_prop('user_email')); $this->assertTrue($user->has_prop('use_ssl')); $this->assertFalse($user->has_prop('field_that_does_not_exist')); update_user_meta(self::$author_id, 'dashed-key', 'abcdefg'); $this->assertTrue($user->has_prop('dashed-key')); }