| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
/** |
| 6 |
* WPSight_Agents Class |
| 7 |
*/ |
| 8 |
class WPSight_Agents { |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor |
| 12 |
*/ |
| 13 |
function __construct() { |
| 14 |
add_filter( 'pre_get_posts', array( $this, 'author_listings' ) ); |
| 15 |
add_filter( 'get_avatar' , array( $this, 'agent_avatar' ), 1, 5 ); |
| 16 |
add_filter( 'user_has_cap', array( $this, 'allow_agent_own_attachment_management' ), 10, 4 ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* allow_agent_own_attachment_management() |
| 21 |
* |
| 22 |
* Allow listing agents to delete their own media files and edit their own images. |
| 23 |
* |
| 24 |
* @param array $allcaps User capabilities. |
| 25 |
* @param array $caps Primitive capabilities required for the requested action. |
| 26 |
* @param array $args Capability check arguments. |
| 27 |
* @param WP_User $user User object. |
| 28 |
* @return array Filtered user capabilities. |
| 29 |
* |
| 30 |
* @since 1.5.5 |
| 31 |
*/ |
| 32 |
public function allow_agent_own_attachment_management( $allcaps, $caps, $args, $user ) { |
| 33 |
if ( |
| 34 |
empty( $args[0] ) || |
| 35 |
! in_array( $args[0], array( 'delete_post', 'edit_post' ), true ) || |
| 36 |
empty( $args[2] ) |
| 37 |
) { |
| 38 |
return $allcaps; |
| 39 |
} |
| 40 |
|
| 41 |
if ( empty( $allcaps['edit_listings'] ) || empty( $allcaps['upload_files'] ) ) { |
| 42 |
return $allcaps; |
| 43 |
} |
| 44 |
|
| 45 |
$attachment = get_post( absint( $args[2] ) ); |
| 46 |
|
| 47 |
if ( ! $attachment instanceof WP_Post || 'attachment' !== $attachment->post_type ) { |
| 48 |
return $allcaps; |
| 49 |
} |
| 50 |
|
| 51 |
if ( (int) $attachment->post_author !== (int) $user->ID ) { |
| 52 |
return $allcaps; |
| 53 |
} |
| 54 |
|
| 55 |
if ( 'edit_post' === $args[0] && ! wp_attachment_is_image( $attachment ) ) { |
| 56 |
return $allcaps; |
| 57 |
} |
| 58 |
|
| 59 |
// Grant only the primitive caps required for managing the agent's own attachment. |
| 60 |
foreach ( $caps as $cap ) { |
| 61 |
$allcaps[ $cap ] = true; |
| 62 |
} |
| 63 |
|
| 64 |
return $allcaps; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Preserve the former attachment deletion callback for backward compatibility. |
| 69 |
* |
| 70 |
* @param array $allcaps User capabilities. |
| 71 |
* @param array $caps Primitive capabilities required for the requested action. |
| 72 |
* @param array $args Capability check arguments. |
| 73 |
* @param WP_User $user User object. |
| 74 |
* @return array Filtered user capabilities. |
| 75 |
* |
| 76 |
* @since 1.5.4 |
| 77 |
*/ |
| 78 |
public function allow_agent_own_attachment_deletion( $allcaps, $caps, $args, $user ) { |
| 79 |
if ( empty( $args[0] ) || 'delete_post' !== $args[0] ) { |
| 80 |
return $allcaps; |
| 81 |
} |
| 82 |
|
| 83 |
return $this->allow_agent_own_attachment_management( $allcaps, $caps, $args, $user ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* init_user_roles() |
| 88 |
* |
| 89 |
* Create or reinitialize custom agent roles. |
| 90 |
* |
| 91 |
* @uses self::remove_user_roles() |
| 92 |
* @uses wpsight_agent_roles() |
| 93 |
* @uses get_role() |
| 94 |
* @uses add_role() |
| 95 |
* @uses WP_Roles::add_cap() |
| 96 |
* @return void |
| 97 |
* |
| 98 |
* @since 1.5.0 |
| 99 |
*/ |
| 100 |
public static function init_user_roles() { |
| 101 |
global $wp_roles; |
| 102 |
|
| 103 |
if ( class_exists( 'WP_Roles' ) && ! isset( $wp_roles ) ) { |
| 104 |
$wp_roles = new WP_Roles(); |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! is_object( $wp_roles ) ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$agent_roles = wpsight_agent_roles(); |
| 112 |
|
| 113 |
self::remove_user_roles(); |
| 114 |
|
| 115 |
// Add listing_admin caps to administrator. |
| 116 |
foreach ( $agent_roles['listing_admin']['caps'] as $cap => $granted ) { |
| 117 |
if ( $granted ) { |
| 118 |
$wp_roles->add_cap( 'administrator', $cap ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Add agent roles with caps. |
| 123 |
foreach ( $agent_roles as $role ) { |
| 124 |
add_role( $role['id'], $role['name'], $role['caps'] ); |
| 125 |
|
| 126 |
/** |
| 127 |
* Add level_1 to caps to show custom roles in author dropdown. |
| 128 |
* |
| 129 |
* @see https://core.trac.wordpress.org/ticket/16841 |
| 130 |
*/ |
| 131 |
$user_role = get_role( $role['id'] ); |
| 132 |
|
| 133 |
if ( $user_role ) { |
| 134 |
$user_role->add_cap( 'level_1' ); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* remove_user_roles() |
| 141 |
* |
| 142 |
* Remove custom agent roles and related administrator capabilities. |
| 143 |
* |
| 144 |
* @uses wpsight_agent_roles() |
| 145 |
* @uses remove_role() |
| 146 |
* @uses WP_Roles::remove_cap() |
| 147 |
* @return void |
| 148 |
* |
| 149 |
* @since 1.5.0 |
| 150 |
*/ |
| 151 |
public static function remove_user_roles() { |
| 152 |
global $wp_roles; |
| 153 |
|
| 154 |
if ( class_exists( 'WP_Roles' ) && ! isset( $wp_roles ) ) { |
| 155 |
$wp_roles = new WP_Roles(); |
| 156 |
} |
| 157 |
|
| 158 |
if ( ! is_object( $wp_roles ) ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
$agent_roles = wpsight_agent_roles(); |
| 163 |
|
| 164 |
// Remove WPCasa related listing_admin caps from administrator |
| 165 |
foreach ( $agent_roles['listing_admin']['caps'] as $cap => $granted ) { |
| 166 |
if ( $granted && strpos( $cap, '_listing' ) ) { |
| 167 |
$wp_roles->remove_cap( 'administrator', $cap ); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
// Remove all custom agent roles. |
| 172 |
foreach ( $agent_roles as $role ) { |
| 173 |
remove_role( $role['id'] ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* agent_roles() |
| 179 |
* |
| 180 |
* Define custom user roles for agents: |
| 181 |
* |
| 182 |
* - Listing Admin |
| 183 |
* - Listing Agent |
| 184 |
* - Listing Subscriber |
| 185 |
* |
| 186 |
* Capabilities for listings are created in |
| 187 |
* /includes/class-wpsight-post-types.php with |
| 188 |
* capability_type 'listing' and map_meta_cap true. |
| 189 |
* |
| 190 |
* [cap] => stdClass Object |
| 191 |
* ( |
| 192 |
* [edit_post] => edit_listing |
| 193 |
* [read_post] => read_listing |
| 194 |
* [delete_post] => delete_listing |
| 195 |
* [edit_posts] => edit_listings |
| 196 |
* [edit_others_posts] => edit_others_listings |
| 197 |
* [publish_posts] => publish_listings |
| 198 |
* [read_private_posts] => read_private_listings |
| 199 |
* [delete_posts] => delete_listings |
| 200 |
* [delete_private_posts] => delete_private_listings |
| 201 |
* [delete_published_posts] => delete_published_listings |
| 202 |
* [delete_others_posts] => delete_others_listings |
| 203 |
* [edit_private_posts] => edit_private_listings |
| 204 |
* [edit_published_posts] => edit_published_listings |
| 205 |
* [create_posts] => edit_listings |
| 206 |
* ) |
| 207 |
* |
| 208 |
* @return array $roles Array of roles to be used by add_role() in /includes/class-wpsight-install.php |
| 209 |
* |
| 210 |
* @since 1.0.0 |
| 211 |
*/ |
| 212 |
public static function agent_roles() { |
| 213 |
|
| 214 |
$roles = array( |
| 215 |
|
| 216 |
'listing_admin' => array( |
| 217 |
'id' => 'listing_admin', |
| 218 |
'name' => _x( 'Listing Admin', 'agent role', 'wpcasa' ), |
| 219 |
'caps' => array( |
| 220 |
'read' => true, |
| 221 |
'upload_files' => true, |
| 222 |
'unfiltered_html' => true, |
| 223 |
'edit_listing' => true, |
| 224 |
'edit_listing_id' => true, |
| 225 |
'read_listing' => true, |
| 226 |
'delete_listing' => true, |
| 227 |
'edit_listings' => true, |
| 228 |
'edit_others_listings' => true, |
| 229 |
'publish_listings' => true, |
| 230 |
'read_private_listings' => true, |
| 231 |
'delete_listings' => true, |
| 232 |
'delete_private_listings' => true, |
| 233 |
'delete_published_listings' => true, |
| 234 |
'delete_others_listings' => true, |
| 235 |
'edit_private_listings' => true, |
| 236 |
'edit_published_listings' => true, |
| 237 |
'manage_listing_terms' => true, |
| 238 |
'edit_listing_terms' => true, |
| 239 |
'delete_listing_terms' => true, |
| 240 |
'assign_listing_terms' => true |
| 241 |
) |
| 242 |
), |
| 243 |
|
| 244 |
'listing_agent' => array( |
| 245 |
'id' => 'listing_agent', |
| 246 |
'name' => _x( 'Listing Agent', 'agent role', 'wpcasa' ), |
| 247 |
'caps' => array( |
| 248 |
'read' => true, |
| 249 |
'upload_files' => true, |
| 250 |
'edit_listing' => true, |
| 251 |
'read_listing' => true, |
| 252 |
'delete_listing' => true, |
| 253 |
'edit_listings' => true, |
| 254 |
'delete_listings' => true, |
| 255 |
'assign_listing_terms' => true |
| 256 |
) |
| 257 |
), |
| 258 |
|
| 259 |
'listing_subscriber' => array( |
| 260 |
'id' => 'listing_subscriber', |
| 261 |
'name' => _x( 'Listing Subscriber', 'agent role', 'wpcasa' ), |
| 262 |
'caps' => array( |
| 263 |
'read' => true, |
| 264 |
'read_listing' => true |
| 265 |
) |
| 266 |
) |
| 267 |
|
| 268 |
); |
| 269 |
|
| 270 |
return apply_filters( 'wpsight_agent_roles', $roles ); |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* get_listing_agent_image() |
| 276 |
* |
| 277 |
* Return HTML image tag of the agent image of |
| 278 |
* the current or a specific listing. |
| 279 |
* |
| 280 |
* @param integer|object $post Post ID or object of required listing (defaults to null = current listing) |
| 281 |
* @param string|array $size Size of the image (WordPress sizes or custom width and height in array) |
| 282 |
* @uses wpsight_get_attachment_id_by_url() |
| 283 |
* @uses wp_get_attachment_image_src() |
| 284 |
* @uses wpsight_get_listing_agent_name() |
| 285 |
* @uses wpsight_get_agent_image() |
| 286 |
* @return string|bool $agent_image HTML image tag of the listing agent image or false |
| 287 |
* |
| 288 |
* @since 1.0.0 |
| 289 |
*/ |
| 290 |
public static function get_listing_agent_image( $post = null, $size = array( 75, 75 ) ) { |
| 291 |
|
| 292 |
$agent_image = ''; |
| 293 |
$agent_image_url = ''; |
| 294 |
|
| 295 |
// Get post object |
| 296 |
$post = get_post( $post ); |
| 297 |
|
| 298 |
// Get image URL from post meta |
| 299 |
$agent_image_url = $post->_agent_logo; |
| 300 |
|
| 301 |
// If we have a URL, built the image |
| 302 |
|
| 303 |
if ( ! empty( $agent_image_url ) ) { |
| 304 |
|
| 305 |
// Get attachment ID |
| 306 |
$attachment_id = wpsight_get_attachment_id_by_url( $agent_image_url ); |
| 307 |
|
| 308 |
if ( $attachment_id ) { |
| 309 |
|
| 310 |
$attachment = wp_get_attachment_image_src( $attachment_id, $size ); |
| 311 |
|
| 312 |
// Build HTML image tag |
| 313 |
$agent_image = '<img src="' . $attachment[0] . '" width="' . $attachment[1] . '" height="' . $attachment[2] . '" alt="' . wpsight_get_listing_agent_name( $post ) . '" />'; |
| 314 |
} else { |
| 315 |
// Build HTML image tag |
| 316 |
$agent_image = '<img src="' . $agent_image_url . '" alt="' . wpsight_get_listing_agent_name( $post ) . '" />'; |
| 317 |
} |
| 318 |
|
| 319 |
// Get attachment URL for specific size |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
// If no image, return agent image |
| 324 |
|
| 325 |
if ( empty( $agent_image ) ) |
| 326 |
$agent_image = wpsight_get_agent_image( $post->post_author ); |
| 327 |
|
| 328 |
// Return agent image or false |
| 329 |
return apply_filters( 'wpsight_listing_agent_image', $agent_image, $post, $size ); |
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* get_agent_image() |
| 335 |
* |
| 336 |
* Return HTML image tag of image of a specific agent. |
| 337 |
* |
| 338 |
* @param integer $user User ID of corresponding agent (required) |
| 339 |
* @param string|array $size Size of the image (WordPress sizes or custom width and height in array) |
| 340 |
* @uses get_user_meta() |
| 341 |
* @uses wp_get_attachment_image_src() |
| 342 |
* @uses get_userdata() |
| 343 |
* @return string|bool $agent_image HTML image tag of the listing agent image or false |
| 344 |
* |
| 345 |
* @since 1.0.0 |
| 346 |
*/ |
| 347 |
public static function get_agent_image( $user_id, $size = array( 75, 75 ) ) { |
| 348 |
|
| 349 |
// Get image ID from user meta |
| 350 |
$agent_image_id = get_user_meta( $user_id, 'agent_logo_id', true ); |
| 351 |
|
| 352 |
// If we have a URL, built the image |
| 353 |
|
| 354 |
if ( ! empty( $agent_image_id ) ) { |
| 355 |
|
| 356 |
// Get attachment URL for specific size |
| 357 |
$attachment = wp_get_attachment_image_src( $agent_image_id, $size ); |
| 358 |
|
| 359 |
if( is_array( $attachment ) ) { |
| 360 |
|
| 361 |
// Build HTML image tag |
| 362 |
$agent_image = '<img src="' . $attachment[0] . '" width="' . $attachment[1] . '" height="' . $attachment[2] . '" alt="' . esc_attr( get_userdata( $user_id )->display_name ) . '" />'; |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
// If no image, return false |
| 369 |
|
| 370 |
if ( empty( $agent_image ) ) |
| 371 |
$agent_image = false; |
| 372 |
|
| 373 |
// Return agent image or false |
| 374 |
return apply_filters( 'wpsight_agent_image', $agent_image, $user_id, $size ); |
| 375 |
|
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* get_listing_agent_name() |
| 380 |
* |
| 381 |
* Return agent name of the |
| 382 |
* current or a specific listing. |
| 383 |
* |
| 384 |
* @param integer|object $post Post ID or object of required listing (defaults to null = current listing) |
| 385 |
* @return string|bool $agent_name Agent name of the listing agent or false |
| 386 |
* |
| 387 |
* @since 1.0.0 |
| 388 |
*/ |
| 389 |
public static function get_listing_agent_name( $post = null ) { |
| 390 |
|
| 391 |
$agent_name = ''; |
| 392 |
|
| 393 |
// Get post object |
| 394 |
$post = get_post( $post ); |
| 395 |
|
| 396 |
// Get agent name from post meta |
| 397 |
$agent_name = $post->_agent_name; |
| 398 |
|
| 399 |
// If no name, return false |
| 400 |
|
| 401 |
if ( empty( $agent_name ) ) |
| 402 |
$agent_name = false; |
| 403 |
|
| 404 |
// Return agent name or false |
| 405 |
return apply_filters( 'wpsight_listing_agent_name', $agent_name, $post ); |
| 406 |
|
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* get_agent_name() |
| 411 |
* |
| 412 |
* Return name of a specific agent. |
| 413 |
* |
| 414 |
* @param integer $user_id User ID of corresponding agent (required) |
| 415 |
* @param string $name The name type to be returned (defaults to display_name) |
| 416 |
* @uses get_userdata() |
| 417 |
* @uses get_user_meta() |
| 418 |
* @return string|bool $agent_name Agent name or false |
| 419 |
* |
| 420 |
* @since 1.0.0 |
| 421 |
*/ |
| 422 |
public static function get_agent_name( $user_id, $name = 'display_name' ) { |
| 423 |
|
| 424 |
$agent_name = ''; |
| 425 |
|
| 426 |
// Get user object |
| 427 |
$user = get_userdata( $user_id ); |
| 428 |
|
| 429 |
if( in_array( $name, array( 'display_name', 'user_login', 'user_nicename' ) ) ) { |
| 430 |
$agent_name = $user->$name; |
| 431 |
} elseif( in_array( $name, array( 'first_name', 'last_name', 'nickname' ) ) ) { |
| 432 |
$agent_name = get_user_meta( $user_id, $name, true ); |
| 433 |
} |
| 434 |
|
| 435 |
// If no name, return false |
| 436 |
|
| 437 |
if ( empty( $agent_name ) ) |
| 438 |
$agent_name = false; |
| 439 |
|
| 440 |
// Return agent name or false |
| 441 |
return apply_filters( 'wpsight_agent_name', $agent_name, $user_id, $name ); |
| 442 |
|
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* get_listing_agent_company() |
| 447 |
* |
| 448 |
* Return agent company of the |
| 449 |
* current or a specific listing. |
| 450 |
* |
| 451 |
* @param integer|object $post Post ID or object of required listing (defaults to null = current listing) |
| 452 |
* @return string|bool $agent_company Agent company of the listing agent or false |
| 453 |
* |
| 454 |
* @since 1.0.0 |
| 455 |
*/ |
| 456 |
public static function get_listing_agent_company( $post = null ) { |
| 457 |
|
| 458 |
$agent_company = ''; |
| 459 |
|
| 460 |
// Get post object |
| 461 |
$post = get_post( $post ); |
| 462 |
|
| 463 |
// Get agent company from post meta |
| 464 |
$agent_company = $post->_agent_company; |
| 465 |
|
| 466 |
// If no name, return false |
| 467 |
|
| 468 |
if ( empty( $agent_company ) ) |
| 469 |
$agent_company = false; |
| 470 |
|
| 471 |
// Return agent name or false |
| 472 |
return apply_filters( 'wpsight_listing_agent_company', $agent_company, $post ); |
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* get_agent_company() |
| 478 |
* |
| 479 |
* Return company of a specific agent. |
| 480 |
* |
| 481 |
* @param integer $user_id User ID of corresponding agent (required) |
| 482 |
* @uses get_user_meta() |
| 483 |
* @return string|bool $agent_company Agent company or false |
| 484 |
* |
| 485 |
* @since 1.0.0 |
| 486 |
*/ |
| 487 |
public static function get_agent_company( $user_id ) { |
| 488 |
|
| 489 |
$agent_company = ''; |
| 490 |
|
| 491 |
// Get agent company from user meta |
| 492 |
$agent_company = get_user_meta( $user_id, 'company', true ); |
| 493 |
|
| 494 |
// If no name, return false |
| 495 |
|
| 496 |
if ( empty( $agent_company ) ) |
| 497 |
$agent_company = false; |
| 498 |
|
| 499 |
// Return agent name or false |
| 500 |
return apply_filters( 'wpsight_agent_company', $agent_company, $user_id ); |
| 501 |
|
| 502 |
} |
| 503 |
|
| 504 |
/** |
| 505 |
* get_listing_agent_description() |
| 506 |
* |
| 507 |
* Return agent description of the |
| 508 |
* current or a specific listing. |
| 509 |
* |
| 510 |
* @param integer|object $post Post ID or object of required listing (defaults to null = current listing) |
| 511 |
* @uses wpsight_format_content() |
| 512 |
* @return string|bool $agent_description Agent description of the listing agent or false |
| 513 |
* |
| 514 |
* @since 1.0.0 |
| 515 |
*/ |
| 516 |
public static function get_listing_agent_description( $post = null ) { |
| 517 |
|
| 518 |
$agent_description = ''; |
| 519 |
|
| 520 |
// Get post object |
| 521 |
$post = get_post( $post ); |
| 522 |
|
| 523 |
// Get agent description from post meta |
| 524 |
$agent_description = ! empty( $post->_agent_description ) ? wpsight_format_content( $post->_agent_description ) : false; |
| 525 |
|
| 526 |
// Return agent description or false |
| 527 |
return apply_filters( 'wpsight_listing_agent_description', $agent_description, $post ); |
| 528 |
|
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* get_agent_description() |
| 533 |
* |
| 534 |
* Return description of a specific agent. |
| 535 |
* |
| 536 |
* @param integer $user_id User ID of corresponding agent (required) |
| 537 |
* @uses get_user_meta() |
| 538 |
* @uses wpsight_format_content() |
| 539 |
* @return string|bool $agent_description Agent description or false |
| 540 |
* |
| 541 |
* @since 1.0.0 |
| 542 |
*/ |
| 543 |
public static function get_agent_description( $user_id ) { |
| 544 |
|
| 545 |
// Get agent description from user meta |
| 546 |
$description = get_user_meta( $user_id, 'description', true ); |
| 547 |
|
| 548 |
// If not empty save formatted description, else false |
| 549 |
$agent_description = ! empty( $description ) ? wpsight_format_content( $description ) : false; |
| 550 |
|
| 551 |
// Return agent description or false |
| 552 |
return apply_filters( 'wpsight_agent_description', $agent_description, $user_id ); |
| 553 |
|
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* get_listing_agent_website() |
| 558 |
* |
| 559 |
* Return agent website of the |
| 560 |
* current or a specific listing. |
| 561 |
* |
| 562 |
* @param integer|object $post Post ID or object of required listing (defaults to null = current listing) |
| 563 |
* @return string|bool $agent_website Agent website of the listing agent or false |
| 564 |
* |
| 565 |
* @since 1.0.0 |
| 566 |
*/ |
| 567 |
public static function get_listing_agent_website( $post = null ) { |
| 568 |
|
| 569 |
$agent_website = ''; |
| 570 |
|
| 571 |
// Get post object |
| 572 |
$post = get_post( $post ); |
| 573 |
|
| 574 |
// Get agent website from post meta |
| 575 |
$agent_website = esc_url( $post->_agent_website ); |
| 576 |
|
| 577 |
// If no website, return false |
| 578 |
|
| 579 |
if ( empty( $agent_website ) ) |
| 580 |
$agent_website = false; |
| 581 |
|
| 582 |
// Return agent website or false |
| 583 |
return apply_filters( 'wpsight_listing_agent_website', $agent_website, $post ); |
| 584 |
|
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* get_agent_website() |
| 589 |
* |
| 590 |
* Return website of a specific agent. |
| 591 |
* |
| 592 |
* @param integer $user_id User ID of corresponding agent (required) |
| 593 |
* @uses get_userdata() |
| 594 |
* @return string|bool $agent_website Agent website or false |
| 595 |
* |
| 596 |
* @since 1.0.0 |
| 597 |
*/ |
| 598 |
public static function get_agent_website( $user_id ) { |
| 599 |
|
| 600 |
// Get agent website from user data |
| 601 |
$agent_website = esc_url( get_userdata( $user_id )->user_url ); |
| 602 |
|
| 603 |
// If no website, return false |
| 604 |
|
| 605 |
if ( empty( $agent_website ) ) |
| 606 |
$agent_website = false; |
| 607 |
|
| 608 |
// Return agent website or false |
| 609 |
return apply_filters( 'wpsight_agent_website', $agent_website, $user_id ); |
| 610 |
|
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* get_listing_agent_phone() |
| 615 |
* |
| 616 |
* Return agent phone of the |
| 617 |
* current or a specific listing. |
| 618 |
* |
| 619 |
* @param integer|object $post Post ID or object of required listing (defaults to null = current listing) |
| 620 |
* @return string|bool $agent_phone Agent phone of the listing agent or false |
| 621 |
* |
| 622 |
* @since 1.0.0 |
| 623 |
*/ |
| 624 |
public static function get_listing_agent_phone( $post = null ) { |
| 625 |
|
| 626 |
$agent_phone = ''; |
| 627 |
|
| 628 |
// Get post object |
| 629 |
$post = get_post( $post ); |
| 630 |
|
| 631 |
// Get agent phone from post meta |
| 632 |
$agent_phone = $post->_agent_phone ? sanitize_text_field( $post->_agent_phone ) : false; |
| 633 |
|
| 634 |
// Return agent website or false |
| 635 |
return apply_filters( 'wpsight_listing_agent_phone', $agent_phone, $post ); |
| 636 |
|
| 637 |
} |
| 638 |
|
| 639 |
/** |
| 640 |
* get_agent_phone() |
| 641 |
* |
| 642 |
* Return phone a specific agent. |
| 643 |
* |
| 644 |
* @param integer $user_id User ID of corresponding agent (required) |
| 645 |
* @uses get_user_meta() |
| 646 |
* @return string|bool $agent_phone Agent phone or false |
| 647 |
* |
| 648 |
* @since 1.0.0 |
| 649 |
*/ |
| 650 |
public static function get_agent_phone( $user_id ) { |
| 651 |
|
| 652 |
// Get agent description from user meta |
| 653 |
$phone = get_user_meta( $user_id, 'phone', true ); |
| 654 |
|
| 655 |
// Set sanitized phone or false |
| 656 |
$agent_phone = $phone ? sanitize_text_field( $phone ) : false; |
| 657 |
|
| 658 |
// Return agent website or false |
| 659 |
return apply_filters( 'wpsight_agent_phone', $agent_phone, $user_id ); |
| 660 |
|
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* get_listing_agent_twitter() |
| 665 |
* |
| 666 |
* Return agent twitter of the |
| 667 |
* current or a specific listing. |
| 668 |
* |
| 669 |
* @param integer|object $post Post ID or object of required listing (defaults to null = current listing) |
| 670 |
* @param string $return Return Twitter user or URL (defaults to 'user' - can be 'url') |
| 671 |
* @return string|bool $agent_twitter Agent twitter of the listing agent or false |
| 672 |
* |
| 673 |
* @since 1.0.0 |
| 674 |
*/ |
| 675 |
public static function get_listing_agent_twitter( $post = null, $return = 'user' ) { |
| 676 |
|
| 677 |
$agent_twitter = ''; |
| 678 |
|
| 679 |
// Get post object |
| 680 |
$post = get_post( $post ); |
| 681 |
|
| 682 |
// Get agent twitter from post meta |
| 683 |
$agent_twitter = $post->_agent_twitter ? sanitize_text_field( $post->_agent_twitter ) : false; |
| 684 |
|
| 685 |
// Remove @ to get username only |
| 686 |
|
| 687 |
if ( strpos( $agent_twitter, '@' ) === 0 ) |
| 688 |
$agent_twitter = substr( $agent_twitter, 1 ); |
| 689 |
|
| 690 |
if ( $return == 'url' ) { |
| 691 |
// If no twitter, return false. Else prepend twitter.com if URL requested |
| 692 |
$agent_twitter = empty( $agent_twitter ) ? false : esc_url( 'https://twitter.com/' . $agent_twitter ); |
| 693 |
} |
| 694 |
|
| 695 |
// Return agent twitter or false |
| 696 |
return apply_filters( 'wpsight_listing_agent_twitter', $agent_twitter, $post, $return ); |
| 697 |
|
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
* get_agent_twitter() |
| 702 |
* |
| 703 |
* Return twitter of a specific agent. |
| 704 |
* |
| 705 |
* @param integer $user_id User ID of corresponding agent (required) |
| 706 |
* @param string $return Return Twitter user or URL (defaults to 'user' - can be 'url') |
| 707 |
* @uses get_user_meta() |
| 708 |
* @return string|bool $agent_twitter Agent twitter or false |
| 709 |
* |
| 710 |
* @since 1.0.0 |
| 711 |
*/ |
| 712 |
public static function get_agent_twitter( $user_id, $return = 'user' ) { |
| 713 |
|
| 714 |
// Get agent twitter from user meta |
| 715 |
$twitter = get_user_meta( $user_id, 'twitter', true ); |
| 716 |
|
| 717 |
// Set sanitized twitter or false |
| 718 |
$agent_twitter = $twitter ? sanitize_text_field( $twitter ) : false; |
| 719 |
|
| 720 |
// Remove @ to get username only |
| 721 |
|
| 722 |
if ( strpos( $agent_twitter, '@' ) === 0 ) |
| 723 |
$agent_twitter = substr( $agent_twitter, 1 ); |
| 724 |
|
| 725 |
if ( $return == 'url' ) { |
| 726 |
// If no twitter, return false. Else prepend twitter.com if URL requested |
| 727 |
$agent_twitter = empty( $agent_twitter ) ? false : esc_url( 'https://twitter.com/' . $agent_twitter ); |
| 728 |
} |
| 729 |
|
| 730 |
// Return agent twitter or false |
| 731 |
return apply_filters( 'wpsight_agent_twitter', $agent_twitter, $user_id, $return ); |
| 732 |
|
| 733 |
} |
| 734 |
|
| 735 |
/** |
| 736 |
* get_listing_agent_facebook() |
| 737 |
* |
| 738 |
* Return agent facebook of the |
| 739 |
* current or a specific listing. |
| 740 |
* |
| 741 |
* @param integer|object $post Post ID or object of required listing (defaults to null = current listing) |
| 742 |
* @param string $return Return Facebook user or URL (defaults to 'user' - can be 'url') |
| 743 |
* @return string|bool $agent_facebook Agent facebook of the listing agent or false |
| 744 |
* |
| 745 |
* @since 1.0.0 |
| 746 |
*/ |
| 747 |
public static function get_listing_agent_facebook( $post = null, $return = 'user' ) { |
| 748 |
|
| 749 |
$agent_facebook = ''; |
| 750 |
|
| 751 |
// Get post object |
| 752 |
$post = get_post( $post ); |
| 753 |
|
| 754 |
// Get agent twitter from post meta |
| 755 |
$agent_facebook = $post->_agent_facebook; |
| 756 |
|
| 757 |
if ( $return == 'url' ) { |
| 758 |
// If no facebook, return false. Else prepend facebook.com if URL requested |
| 759 |
$agent_facebook = empty( $agent_facebook ) ? false : esc_url( 'https://www.facebook.com/' . $agent_facebook ); |
| 760 |
} |
| 761 |
|
| 762 |
// Return agent twitter or false |
| 763 |
return apply_filters( 'wpsight_listing_agent_facebook', $agent_facebook, $post, $return ); |
| 764 |
|
| 765 |
} |
| 766 |
|
| 767 |
/** |
| 768 |
* get_agent_facebook() |
| 769 |
* |
| 770 |
* Return facebook of a specific agent. |
| 771 |
* |
| 772 |
* @param integer $user_id User ID of corresponding agent (required) |
| 773 |
* @param string $return Return Facebook user or URL (defaults to 'user' - can be 'url') |
| 774 |
* @uses get_user_meta() |
| 775 |
* @return string|bool $agent_facebook Agent facebook or false |
| 776 |
* |
| 777 |
* @since 1.0.0 |
| 778 |
*/ |
| 779 |
public static function get_agent_facebook( $user_id, $return = 'user' ) { |
| 780 |
|
| 781 |
// Get agent twitter from user meta |
| 782 |
$facebook = get_user_meta( $user_id, 'facebook', true ); |
| 783 |
|
| 784 |
// Set sanitized facebook or false |
| 785 |
$agent_facebook = $facebook ? sanitize_text_field( $facebook ) : false; |
| 786 |
|
| 787 |
if ( $return == 'url' ) { |
| 788 |
// If no facebook, return false. Else prepend facebook.com if URL requested |
| 789 |
$agent_facebook = empty( $agent_facebook ) ? false : esc_url( 'https://www.facebook.com/' . $agent_facebook ); |
| 790 |
} |
| 791 |
|
| 792 |
// Return agent twitter or false |
| 793 |
return apply_filters( 'wpsight_listing_agent_facebook', $agent_facebook, $user_id, $return ); |
| 794 |
|
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* get_listing_agent_archive() |
| 799 |
* |
| 800 |
* Get listing agent archive by adding |
| 801 |
* "listings=1" to get_author_posts_url(). |
| 802 |
* |
| 803 |
* @param integer|object $post Post ID or object of required listing (defaults to null = current listing) |
| 804 |
* @param integer $user_id User ID of the corresponding agent (defaults to post_author) |
| 805 |
* @uses get_author_posts_url() |
| 806 |
* @uses add_query_arg() |
| 807 |
* @return string $agent_archive Author posts URL with additional query arg "listings=1" |
| 808 |
* |
| 809 |
* @since 1.0.0 |
| 810 |
*/ |
| 811 |
public static function get_listing_agent_archive( $post = null, $user_id = false ) { |
| 812 |
|
| 813 |
$agent_archive = ''; |
| 814 |
|
| 815 |
// Get post object |
| 816 |
$post = get_post( $post ); |
| 817 |
|
| 818 |
// Set user ID |
| 819 |
$user_id = $user_id == false ? absint( $post->post_author ) : absint( $user_id ); |
| 820 |
|
| 821 |
// If user ID, set author posts URL with query arg, else false |
| 822 |
$agent_archive = ! empty( $user_id ) ? esc_url( add_query_arg( 'listings', '1', get_author_posts_url( $user_id ) ) ) : false; |
| 823 |
|
| 824 |
// Return agent archive link or false |
| 825 |
return apply_filters( 'wpsight_get_listing_agent_archive', $agent_archive, $post, $user_id ); |
| 826 |
|
| 827 |
} |
| 828 |
|
| 829 |
/** |
| 830 |
* get_agent_archive() |
| 831 |
* |
| 832 |
* Get archive link of a specific agent. |
| 833 |
* |
| 834 |
* @param integer $user_id User ID of corresponding agent (required) |
| 835 |
* @uses get_author_posts_url() |
| 836 |
* @uses add_query_arg() |
| 837 |
* @return string $agent_archive Author posts URL with additional query arg "listings=1" |
| 838 |
* |
| 839 |
* @since 1.0.0 |
| 840 |
*/ |
| 841 |
public static function get_agent_archive( $user_id ) { |
| 842 |
|
| 843 |
// Add listings=1 to get_author_posts_url() |
| 844 |
$agent_archive = esc_url( add_query_arg( 'listings', '1', get_author_posts_url( absint( $user_id ) ) ) ); |
| 845 |
|
| 846 |
// Return agent archive link or false |
| 847 |
return apply_filters( 'wpsight_get_listing_agent_archive', $agent_archive, $user_id ); |
| 848 |
|
| 849 |
} |
| 850 |
|
| 851 |
/** |
| 852 |
* author_listings() |
| 853 |
* |
| 854 |
* Limit author archive entries to listings |
| 855 |
* when corresponding query arg is set. |
| 856 |
* |
| 857 |
* @param object $query WP_Query object |
| 858 |
* @uses wpsight_is_listing_agent_archive() |
| 859 |
* @uses wpsight_post_type() |
| 860 |
* @uses $query->set() |
| 861 |
* |
| 862 |
* @since 1.0.0 |
| 863 |
*/ |
| 864 |
public static function author_listings( $query ) { |
| 865 |
|
| 866 |
if ( wpsight_is_listing_agent_archive( $query ) ) |
| 867 |
$query->set( 'post_type', array( wpsight_post_type() ) ); |
| 868 |
|
| 869 |
} |
| 870 |
|
| 871 |
/** |
| 872 |
* author_avatar() |
| 873 |
* |
| 874 |
* Filter get_avatar to optionally |
| 875 |
* replace avatar with custom agent image. |
| 876 |
* |
| 877 |
* @param mixed $avatar Image tag for the user's avatar |
| 878 |
* @param integer|string $id_or_email A user ID, email address, or comment object |
| 879 |
* @param integer $size Square avatar width and height in pixels to retrieve |
| 880 |
* @param string $alt Alternative text to use in the avatar image tag |
| 881 |
* @uses get_user_by() |
| 882 |
* @uses self::get_agent_image() |
| 883 |
* |
| 884 |
* @since 1.0.0 |
| 885 |
*/ |
| 886 |
function agent_avatar( $avatar, $id_or_email, $size, $default, $alt ) { |
| 887 |
|
| 888 |
$user = false; |
| 889 |
|
| 890 |
if ( is_numeric( $id_or_email ) ) { |
| 891 |
$id = (int) $id_or_email; |
| 892 |
$user = get_user_by( 'id' , $id ); |
| 893 |
} elseif ( is_object( $id_or_email ) ) { |
| 894 |
if ( ! empty( $id_or_email->user_id ) ) { |
| 895 |
$id = (int) $id_or_email->user_id; |
| 896 |
$user = get_user_by( 'id' , $id ); |
| 897 |
} |
| 898 |
} else { |
| 899 |
$user = get_user_by( 'email', $id_or_email ); |
| 900 |
} |
| 901 |
|
| 902 |
if ( $user && is_object( $user ) && self::get_agent_image( $user->ID ) ) |
| 903 |
$avatar = self::get_agent_image( $user->ID, array( $size, $size ) ); |
| 904 |
|
| 905 |
return apply_filters( 'wpsight_agent_avatar', $avatar, $user, $size ); |
| 906 |
|
| 907 |
} |
| 908 |
|
| 909 |
/** |
| 910 |
* get_user_posts_by_type() |
| 911 |
* |
| 912 |
* Return posts created by a specific author. |
| 913 |
* |
| 914 |
* @param integer $user_id ID of the corresponding user |
| 915 |
* @param string $post_type Post type |
| 916 |
* @uses get_post_types() |
| 917 |
* @uses get_current_user_id() |
| 918 |
* @uses get_posts_by_author_sql() |
| 919 |
* @uses $wpdb->get_col() |
| 920 |
* @return array $post_ids Array of post IDs |
| 921 |
* |
| 922 |
* @since 1.0.0 |
| 923 |
*/ |
| 924 |
public static function get_user_posts_by_type( $user_id = false, $post_type = 'post' ) { |
| 925 |
global $wpdb; |
| 926 |
|
| 927 |
// Stop if post type not valid |
| 928 |
|
| 929 |
if( ! in_array( $post_type, get_post_types() ) ) |
| 930 |
return false; |
| 931 |
|
| 932 |
// Set user ID |
| 933 |
$user_id = $user_id === false ? get_current_user_id() : absint( $user_id ); |
| 934 |
|
| 935 |
// Author SQL |
| 936 |
$where = get_posts_by_author_sql( $post_type, true, $user_id, false ); |
| 937 |
|
| 938 |
// Set SQL query |
| 939 |
$query = "SELECT ID FROM $wpdb->posts $where"; |
| 940 |
|
| 941 |
// Get post IDs |
| 942 |
$post_ids = $wpdb->get_col( $wpdb->prepare( "%s", $query ) ); |
| 943 |
|
| 944 |
return apply_filters( 'wpsight_get_user_posts_by_type', $post_ids, $user_id ); |
| 945 |
|
| 946 |
} |
| 947 |
|
| 948 |
/** |
| 949 |
* profile_contact_fields() |
| 950 |
* |
| 951 |
* Return user contact fields |
| 952 |
* |
| 953 |
* @return array $fields Array of contact fields |
| 954 |
* |
| 955 |
* @since 1.0.0 |
| 956 |
*/ |
| 957 |
public static function profile_contact_fields() { |
| 958 |
|
| 959 |
$fields = array( |
| 960 |
'company' => array( |
| 961 |
'label' => __( 'Company', 'wpcasa' ) |
| 962 |
), |
| 963 |
'phone' => array( |
| 964 |
'label' => __( 'Phone', 'wpcasa' ) |
| 965 |
), |
| 966 |
'twitter' => array( |
| 967 |
'label' => 'X' |
| 968 |
), |
| 969 |
'facebook' => array( |
| 970 |
'label' => 'Facebook' |
| 971 |
) |
| 972 |
); |
| 973 |
|
| 974 |
return apply_filters( 'wpsight_profile_contact_fields', $fields ); |
| 975 |
|
| 976 |
} |
| 977 |
|
| 978 |
} |
| 979 |
|