| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* UsersWP Badge widget. |
| 5 |
*/ |
| 6 |
class UWP_User_Badge_Widget extends WP_Super_Duper { |
| 7 |
|
| 8 |
public $arguments; |
| 9 |
|
| 10 |
/** |
| 11 |
* Sets up the widgets name etc |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
|
| 15 |
$options = array( |
| 16 |
'textdomain' => 'userswp', |
| 17 |
'block-icon' => 'admin-site', |
| 18 |
'block-category' => 'widgets', |
| 19 |
'block-keywords' => "['badge','userswp']", |
| 20 |
'class_name' => __CLASS__, |
| 21 |
'base_id' => 'uwp_user_badge', |
| 22 |
'name' => __( 'UWP > User Badge', 'userswp' ), |
| 23 |
'no_wrap' => true, |
| 24 |
'widget_ops' => array( |
| 25 |
'classname' => 'uwp-user-badge', |
| 26 |
'description' => esc_html__( 'Displays the user badge.', 'userswp' ), |
| 27 |
'userswp' => true, |
| 28 |
) |
| 29 |
); |
| 30 |
|
| 31 |
parent::__construct( $options ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Set widget arguments. |
| 36 |
* |
| 37 |
*/ |
| 38 |
public function set_arguments() { |
| 39 |
$arguments = array( |
| 40 |
'user_id' => array( |
| 41 |
'type' => 'number', |
| 42 |
'title' => __('User ID:', 'userswp'), |
| 43 |
'desc' => __('Leave blank to use current user id.', 'userswp'), |
| 44 |
'placeholder' => __('Leave blank to use current user id.', 'userswp'), |
| 45 |
'default' => '', |
| 46 |
'desc_tip' => true, |
| 47 |
'advanced' => false |
| 48 |
), |
| 49 |
'key' => array( |
| 50 |
'type' => 'select', |
| 51 |
'title' => __('Field Key:', 'userswp'), |
| 52 |
'desc' => __('This is the custom field key.', 'userswp'), |
| 53 |
'placeholder' => '', |
| 54 |
'options' => $this->get_custom_field_keys(), |
| 55 |
'default' => '', |
| 56 |
'desc_tip' => true, |
| 57 |
'advanced' => false |
| 58 |
), |
| 59 |
'condition' => array( |
| 60 |
'type' => 'select', |
| 61 |
'title' => __('Field condition:', 'userswp'), |
| 62 |
'desc' => __('Select the custom field condition.', 'userswp'), |
| 63 |
'placeholder' => '', |
| 64 |
'options' => $this->get_badge_conditions(), |
| 65 |
'default' => 'is_equal', |
| 66 |
'desc_tip' => true, |
| 67 |
'advanced' => false |
| 68 |
), |
| 69 |
'search' => array( |
| 70 |
'type' => 'text', |
| 71 |
'title' => __('Value to match:', 'userswp'), |
| 72 |
'desc' => __('Match this text with field value to display user badge.', 'userswp'), |
| 73 |
'placeholder' => '', |
| 74 |
'default' => '', |
| 75 |
'desc_tip' => true, |
| 76 |
'advanced' => false, |
| 77 |
'element_require' => '[%condition%]!="is_empty" && [%condition%]!="is_not_empty"' |
| 78 |
), |
| 79 |
'badge' => array( |
| 80 |
'type' => 'text', |
| 81 |
'title' => __('Badge:', 'userswp'), |
| 82 |
'desc' => __('Badge text. Leave blank to show field title as a badge, or use %%input%% to use the input value of the field or %%profile_url%% for the user profile url, or the field key for any other info %%email%%.', 'userswp'), |
| 83 |
'placeholder' => '', |
| 84 |
'default' => '', |
| 85 |
'desc_tip' => true, |
| 86 |
'advanced' => false |
| 87 |
), |
| 88 |
'type' => array( |
| 89 |
'title' => __('Type', 'userswp'), |
| 90 |
'desc' => __('Select the badge type.', 'userswp'), |
| 91 |
'type' => 'select', |
| 92 |
'options' => array( |
| 93 |
"" => __('Badge', 'userswp'), |
| 94 |
"pill" => __('Pill', 'userswp'), |
| 95 |
), |
| 96 |
'default' => '', |
| 97 |
'desc_tip' => true, |
| 98 |
'advanced' => false, |
| 99 |
'group' => __("Design","userswp") |
| 100 |
), |
| 101 |
'shadow' => array( |
| 102 |
'title' => __('Shadow', 'userswp'), |
| 103 |
'desc' => __('Select the shadow badge type.', 'userswp'), |
| 104 |
'type' => 'select', |
| 105 |
'options' => array( |
| 106 |
"" => __('None', 'userswp'), |
| 107 |
"small" => __('Small', 'userswp'), |
| 108 |
"medium" => __('Medium', 'userswp'), |
| 109 |
"large" => __('Large', 'userswp'), |
| 110 |
), |
| 111 |
'default' => '', |
| 112 |
'desc_tip' => true, |
| 113 |
'group' => __("Design","userswp") |
| 114 |
), |
| 115 |
'color' => array( |
| 116 |
'title' => __('Badge Color', 'userswp'), |
| 117 |
'desc' => __('Select the the badge color.', 'userswp'), |
| 118 |
'type' => 'select', |
| 119 |
'options' => array( |
| 120 |
"" => __('Custom colors', 'userswp'), |
| 121 |
)+uwp_aui_colors(true), |
| 122 |
'default' => '', |
| 123 |
'desc_tip' => true, |
| 124 |
'advanced' => false, |
| 125 |
'group' => __("Design","userswp") |
| 126 |
), |
| 127 |
'bg_color' => array( |
| 128 |
'type' => 'color', |
| 129 |
'title' => __('Badge background color:', 'userswp'), |
| 130 |
'desc' => __('Color for the badge background.', 'userswp'), |
| 131 |
'placeholder' => '', |
| 132 |
'default' => '#0073aa', |
| 133 |
'desc_tip' => true, |
| 134 |
'group' => __("Design","userswp"), |
| 135 |
'element_require' => '[%color%]==""', |
| 136 |
), |
| 137 |
'txt_color' => array( |
| 138 |
'type' => 'color', |
| 139 |
'title' => __('Badge text color:', 'userswp'), |
| 140 |
'desc' => __('Color for the badge text.', 'userswp'), |
| 141 |
'placeholder' => '', |
| 142 |
'desc_tip' => true, |
| 143 |
'default' => '#ffffff', |
| 144 |
'group' => __("Design","userswp"), |
| 145 |
'element_require' => '[%color%]==""', |
| 146 |
), |
| 147 |
'size' => array( |
| 148 |
'type' => 'select', |
| 149 |
'title' => __('Badge size:', 'userswp'), |
| 150 |
'desc' => __('Size of the badge.', 'userswp'), |
| 151 |
'options' => array( |
| 152 |
"" => __('Medium', 'userswp'), |
| 153 |
"small" => __('Small', 'userswp'), |
| 154 |
"large" => __('Large', 'userswp'), |
| 155 |
"extra-large" => __('Extra Large', 'userswp'), |
| 156 |
), |
| 157 |
'default' => '', |
| 158 |
'desc_tip' => true, |
| 159 |
'group' => __("Design","userswp"), |
| 160 |
), |
| 161 |
'alignment' => array( |
| 162 |
'type' => 'select', |
| 163 |
'title' => __('Alignment:', 'userswp'), |
| 164 |
'desc' => __('How the item should be positioned on the page.', 'userswp'), |
| 165 |
'options' => array( |
| 166 |
"" => __('None', 'userswp'), |
| 167 |
"left" => __('Left', 'userswp'), |
| 168 |
"center" => __('Center', 'userswp'), |
| 169 |
"right" => __('Right', 'userswp'), |
| 170 |
), |
| 171 |
'default' => '', |
| 172 |
'desc_tip' => true, |
| 173 |
'group' => __("Design","userswp"), |
| 174 |
), |
| 175 |
'icon_class' => array( |
| 176 |
'type' => 'text', |
| 177 |
'title' => __('Icon class:', 'userswp'), |
| 178 |
'desc' => __('You can show a font-awesome icon here by entering the icon class.', 'userswp'), |
| 179 |
'placeholder' => 'fas fa-award', |
| 180 |
'default' => '', |
| 181 |
'desc_tip' => true, |
| 182 |
'group' => __("Design","userswp") |
| 183 |
), |
| 184 |
'css_class' => array( |
| 185 |
'type' => 'text', |
| 186 |
'title' => __('Extra class:', 'userswp'), |
| 187 |
'desc' => __('Give the wrapper an extra class so you can style things as you want.', 'userswp'), |
| 188 |
'placeholder' => '', |
| 189 |
'default' => '', |
| 190 |
'desc_tip' => true, |
| 191 |
'group' => __("Design","userswp"), |
| 192 |
), |
| 193 |
'link' => array( |
| 194 |
'type' => 'text', |
| 195 |
'title' => __('Link url:', 'userswp'), |
| 196 |
'desc' => __('Badge link url. You can use this to make the button link to something, %%input%% can be used here if a link or %%profile_url%% for the user profile url.', 'userswp'), |
| 197 |
'placeholder' => '', |
| 198 |
'default' => '', |
| 199 |
'desc_tip' => true, |
| 200 |
'group' => __("Click Action","userswp") |
| 201 |
), |
| 202 |
'new_window' => array( |
| 203 |
'title' => __('Open link in new window:', 'userswp'), |
| 204 |
'desc' => __('This will open the link in a new window.', 'userswp'), |
| 205 |
'type' => 'checkbox', |
| 206 |
'desc_tip' => true, |
| 207 |
'value' => '1', |
| 208 |
'default' => 0, |
| 209 |
'group' => __("Click Action","userswp") |
| 210 |
), |
| 211 |
'popover_title' => array( |
| 212 |
'type' => 'text', |
| 213 |
'title' => __('Popover title:', 'userswp'), |
| 214 |
'desc' => __('Reveals some title text onclick. Enter some text or use %%input%% to use the input value of the field or the field key for any other info %%email%%.', 'userswp'), |
| 215 |
'placeholder' => '', |
| 216 |
'default' => '', |
| 217 |
'desc_tip' => true, |
| 218 |
'group' => __("Click Action","userswp") |
| 219 |
), |
| 220 |
'popover_text' => array( |
| 221 |
'type' => 'text', |
| 222 |
'title' => __('Popover text:', 'userswp'), |
| 223 |
'desc' => __('Reveals some text onclick. Enter some text or use %%input%% to use the input value of the field or the field key for any other info %%email%%.', 'userswp'), |
| 224 |
'placeholder' => '', |
| 225 |
'default' => '', |
| 226 |
'desc_tip' => true, |
| 227 |
'group' => __("Click Action","userswp") |
| 228 |
), |
| 229 |
'tooltip_text' => array( |
| 230 |
'type' => 'text', |
| 231 |
'title' => __('Tooltip text:', 'userswp'), |
| 232 |
'desc' => __('Reveals some text on hover. Enter some text or use %%input%% to use the input value of the field or the field key for any other info %%email%%. (this can NOT be used with popover text)', 'userswp'), |
| 233 |
'placeholder' => '', |
| 234 |
'default' => '', |
| 235 |
'desc_tip' => true, |
| 236 |
'group' => __("Hover Action","userswp") |
| 237 |
), |
| 238 |
'hover_content' => array( |
| 239 |
'type' => 'text', |
| 240 |
'title' => __('Hover content:', 'userswp'), |
| 241 |
'desc' => __('Change the button text on hover. Enter some text or use %%input%% to use the input value of the field or the field key for any other info %%email%%.', 'userswp'), |
| 242 |
'placeholder' => '', |
| 243 |
'default' => '', |
| 244 |
'desc_tip' => true, |
| 245 |
'group' => __("Hover Action","userswp") |
| 246 |
), |
| 247 |
'hover_icon' => array( |
| 248 |
'type' => 'text', |
| 249 |
'title' => __('Hover icon:', 'userswp'), |
| 250 |
'desc' => __('Change the button icon on hover. You can show a font-awesome icon here by entering the icon class.', 'userswp'), |
| 251 |
'placeholder' => 'fas fa-bacon', |
| 252 |
'default' => '', |
| 253 |
'desc_tip' => true, |
| 254 |
'group' => __("Hover Action","userswp") |
| 255 |
), |
| 256 |
); |
| 257 |
|
| 258 |
return $arguments; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Outputs the user badge on the front-end. |
| 264 |
* |
| 265 |
* @param array $args |
| 266 |
* @param array $widget_args |
| 267 |
* @param string $content |
| 268 |
* |
| 269 |
* @return string |
| 270 |
*/ |
| 271 |
public function output( $args = array(), $widget_args = array(), $content = '' ) { |
| 272 |
global $post; |
| 273 |
|
| 274 |
if('post_author' == $args['user_id'] && $post instanceof WP_Post){ |
| 275 |
$user = get_userdata($post->post_author); |
| 276 |
$args['user_id'] = $post->post_author; |
| 277 |
} else if(isset($args['user_id']) && (int)$args['user_id'] > 0){ |
| 278 |
$user = get_userdata($args['user_id']); |
| 279 |
} else { |
| 280 |
$user = uwp_get_displayed_user(); |
| 281 |
} |
| 282 |
|
| 283 |
if(empty($args['user_id']) && !empty($user->ID)){ |
| 284 |
$args['user_id'] = $user->ID; |
| 285 |
} |
| 286 |
|
| 287 |
if(!$user){ |
| 288 |
return ''; |
| 289 |
} |
| 290 |
|
| 291 |
$errors = array(); |
| 292 |
if ( empty( $args['user_id'] ) ) { |
| 293 |
$errors[] = __('User ID is missing','userswp'); |
| 294 |
} |
| 295 |
if ( empty( $args['key'] ) ) { |
| 296 |
$errors[] = __('Field key is missing', 'userswp'); |
| 297 |
} |
| 298 |
|
| 299 |
$output = ''; |
| 300 |
if ( ! empty( $errors ) ){ |
| 301 |
$output .= implode( ", ", $errors ); |
| 302 |
} |
| 303 |
|
| 304 |
$output .= uwp_get_user_badge( $args ); |
| 305 |
|
| 306 |
return $output; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Gets an array of custom field keys for user badge. |
| 311 |
* |
| 312 |
* @return array |
| 313 |
*/ |
| 314 |
public function get_custom_field_keys(){ |
| 315 |
global $wpdb; |
| 316 |
$table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 317 |
|
| 318 |
$fields = $wpdb->get_results("SELECT htmlvar_name, site_title FROM " . $table_name . " WHERE form_type = 'account'"); |
| 319 |
|
| 320 |
$keys = array(); |
| 321 |
if ( !empty( $fields ) ) { |
| 322 |
foreach( $fields as $field ) { |
| 323 |
if ( apply_filters( 'uwp_badge_field_skip_key', false, $field ) ) { |
| 324 |
continue; |
| 325 |
} |
| 326 |
$keys[ $field->htmlvar_name ] = $field->htmlvar_name . ' ( ' . __( wp_unslash( $field->site_title ), 'userswp' ) . ' )'; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return apply_filters( 'uwp_badge_field_keys', $keys ); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Gets an array of badge field conditions. |
| 335 |
* |
| 336 |
* @return array |
| 337 |
*/ |
| 338 |
public function get_badge_conditions(){ |
| 339 |
$conditions = array( |
| 340 |
'is_equal' => __( 'is equal', 'userswp' ), |
| 341 |
'is_not_equal' => __( 'is not equal', 'userswp' ), |
| 342 |
'is_greater_than' => __( 'is greater than', 'userswp' ), |
| 343 |
'is_less_than' => __( 'is less than', 'userswp' ), |
| 344 |
'is_empty' => __( 'is empty', 'userswp' ), |
| 345 |
'is_not_empty' => __( 'is not empty', 'userswp' ), |
| 346 |
); |
| 347 |
|
| 348 |
return apply_filters( 'uwp_badge_conditions', $conditions ); |
| 349 |
} |
| 350 |
|
| 351 |
} |
| 352 |
|