Avatar_Suggestions_Front

Load Front class.

Defined (1)

The class is defined in the following location(s).

/includes/bp-avatar-suggestions-front.php  
  1. class Avatar_Suggestions_Front { 
  2.  
  3. /** 
  4. * Setup Front. 
  5. * @package BP Avatar Suggestions 
  6. * @subpackage Front 
  7. * @since 1.1.0 
  8. * @uses buddypress() to get BuddyPress main instance. 
  9. */ 
  10. public static function start() { 
  11. // Get BuddyPress instance 
  12. $bp = buddypress(); 
  13.  
  14. if( empty( $bp->extend->avatar_suggestions->front ) ) { 
  15. $bp->extend->avatar_suggestions->front = new self; 
  16.  
  17. return $bp->extend->avatar_suggestions->front; 
  18.  
  19. /** 
  20. * Constructor method. 
  21. * @package BP Avatar Suggestions 
  22. * @subpackage Front 
  23. * @since 1.1.0 
  24. */ 
  25. public function __construct() { 
  26. $this->setup_globals(); 
  27. $this->setup_hooks(); 
  28.  
  29. /** 
  30. * Set the suggestions attachment ids. 
  31. * @package BP Avatar Suggestions 
  32. * @subpackage Front 
  33. * @since 1.1.0 
  34. */ 
  35. private function setup_globals() { 
  36. // Get BuddyPress instance 
  37. $bp = buddypress(); 
  38.  
  39. $this->avatar_post_id = $bp->extend->avatar_suggestions->avatar_post_id; 
  40. $this->enable_users = $bp->extend->avatar_suggestions->enable_users; 
  41. $this->enable_groups = $bp->extend->avatar_suggestions->enable_groups; 
  42. $this->min = ( defined( 'SCRIPT_DEBUG' ) && true == SCRIPT_DEBUG ) ? '' : '.min'; 
  43.  
  44. /** 
  45. * Set the actions & filters 
  46. * @package BP Avatar Suggestions 
  47. * @subpackage Front 
  48. * @since 1.1.0 
  49. */ 
  50. private function setup_hooks() { 
  51. // javascript 
  52. add_action( 'bp_enqueue_scripts', array( $this, 'register_script' ), 2 ); 
  53. add_action( 'bp_admin_enqueue_scripts', array( $this, 'register_script' ), 2 ); 
  54. add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_script' ), 10 ); 
  55.  
  56. // Extend New avatar UI 
  57. add_filter( 'bp_attachment_avatar_script_data', array( $this, 'include_script' ), 10, 2 ); 
  58. add_filter( 'bp_attachments_avatar_nav', array( $this, 'suggestions_nav' ), 10, 2 ); 
  59. add_action( 'bp_attachments_avatar_main_template', array( $this, 'js_template' ) ); 
  60. add_filter( 'bp_attachments_get_plupload_l10n', array( $this, 'suggestions_strings' ), 10, 1 ); 
  61.  
  62. // Load the javascript template for the Suggestions selector 
  63. add_action( 'bp_after_profile_avatar_upload_content', array( $this, 'suggestions_selector' ) ); 
  64. add_action( 'bp_after_group_avatar_creation_step', array( $this, 'suggestions_selector' ) ); 
  65. add_action( 'bp_after_group_admin_content', array( $this, 'suggestions_selector' ) ); 
  66.  
  67. // Get Suggestions 
  68. add_action( 'wp_ajax_get_avatar_suggestions', array( $this, 'get_avatar_suggestions' ) ); 
  69.  
  70. // Set suggestion 
  71. add_action( 'wp_ajax_set_avatar_suggestion', array( $this, 'set_suggestion' ) ); 
  72.  
  73. // remove suggestion 
  74. add_action( 'wp_ajax_remove_avatar_suggestion', array( $this, 'remove_suggestion' ) ); 
  75.  
  76. // filter avatar 
  77. add_filter( 'bp_core_fetch_avatar', array( $this, 'suggestion_avatar' ), 1, 2 ); 
  78. add_filter( 'bp_core_fetch_avatar_url', array( $this, 'suggestion_avatar' ), 1, 2 ); 
  79.  
  80. /** 
  81. * Bail if avatars are not enabled 
  82. * @package BP Avatar Suggestions 
  83. * @subpackage Front 
  84. * @since 1.2.0 
  85. * @return bool whether avatars are enabled or not 
  86. */ 
  87. public function bail() { 
  88. // Get BuddyPress instance 
  89. $bp = buddypress(); 
  90.  
  91. return empty( $bp->avatar->show_avatars ); 
  92.  
  93. /** 
  94. * Prepare a suggestion for javascript 
  95. * @package BP Avatar Suggestions 
  96. * @subpackage Front 
  97. * @since 1.3.0 
  98. * @param object $avatar the attachment object 
  99. * @return array the avatar prepared for javascript 
  100. */ 
  101. public function prepare_for_js( $avatar = null ) { 
  102. if ( empty( $avatar ) ) { 
  103. return false; 
  104.  
  105. // Reset the suggestion size 
  106. $suggestion_size = array(); 
  107. $avatar = wp_prepare_attachment_for_js( $avatar ); 
  108.  
  109. if ( ! empty( $avatar['id'] ) ) { 
  110. // Set the thumbnail size if not available 
  111. if ( empty( $avatar['sizes']['thumbnail'] ) ) { 
  112. $suggestion_size['thumbnail'] = image_downsize( $avatar['id'], 'thumbnail' ); 
  113.  
  114. if ( empty( $suggestion_size['thumbnail'] ) || ( ! empty( $suggestion_size['thumbnail'] ) && ( $suggestion_size['thumbnail'][2] != bp_core_avatar_full_height() || $suggestion_size['thumbnail'][1] != bp_core_avatar_full_width() ) ) ) { 
  115. // Try to use the bp_avatar_suggestions if it's an intermediate size 
  116. $suggestion_size['bp_avatar_suggestions'] = image_downsize( $avatar['id'], 'bp_avatar_suggestions' ); 
  117.  
  118. if ( ! empty( $suggestion_size['bp_avatar_suggestions'] ) ) { 
  119. $suggestion_size['thumbnail'] = $suggestion_size['bp_avatar_suggestions']; 
  120.  
  121. if ( ! empty( $suggestion_size['thumbnail'] ) ) { 
  122. $avatar['sizes']['thumbnail'] = array( 
  123. 'height' => $suggestion_size['thumbnail'][2],  
  124. 'width' => $suggestion_size['thumbnail'][1],  
  125. 'url' => str_replace( array( 'https:', 'http:' ), '', $suggestion_size['thumbnail'][0] ),  
  126. 'orientation' => $suggestion_size['thumbnail'][2] > $suggestion_size['thumbnail'][1] ? 'portrait' : 'landscape',  
  127. ); 
  128. } else { 
  129. return false; 
  130.  
  131. return $avatar; 
  132.  
  133. /** 
  134. * Get and prepare the suggestions for BackBone 
  135. * @package BP Avatar Suggestions 
  136. * @subpackage Front 
  137. * @since 1.2.0 
  138. */ 
  139. public function get_avatar_suggestions() { 
  140. $avatar_types = array( 
  141. 'user' => array( 1, 2 ),  
  142. 'group' => array( 1, 3 ),  
  143. ); 
  144.  
  145. if ( ! isset( $_POST['item_object'] ) || ! isset( $avatar_types[ $_POST['item_object'] ] ) || empty( $_POST['item_id'] ) ) { 
  146. wp_send_json_error(); 
  147.  
  148. check_ajax_referer( 'avatar_suggestions_selector', 'nonce' ); 
  149.  
  150. $object = sanitize_key( $_POST['item_object'] ); 
  151. $item_id = absint( $_POST['item_id'] ); 
  152.  
  153. // Set the suggestions arguments 
  154. $suggestions_args = array( 
  155. 'post_type' => 'attachment',  
  156. 'post_parent' => $this->avatar_post_id,  
  157. 'post_status' => 'inherit',  
  158. 'posts_per_page' => -1,  
  159. 'meta_query' => array( 
  160. array( 
  161. 'key' => '_bpas_avatar_type',  
  162. 'compare' => 'IN',  
  163. 'value' => $avatar_types[ $object ],  
  164. ),  
  165. ); 
  166.  
  167. $query = new WP_Query( $suggestions_args ); 
  168.  
  169. $suggestions = array_map( array( $this, 'prepare_for_js' ), $query->posts ); 
  170.  
  171. if ( ! empty( $suggestions ) ) { 
  172. if ( 'group' == $object ) { 
  173. $current_item_avatar = str_replace( array( 'https:', 'http:' ), '', groups_get_groupmeta( $item_id, 'group_avatar_choice', true ) ); 
  174. } else { 
  175. $current_item_avatar = str_replace( array( 'https:', 'http:' ), '', bp_get_user_meta( $item_id, 'user_avatar_choice', true ) ); 
  176.  
  177. foreach ( $suggestions as $key => $suggestion ) { 
  178. // If no thumbnail url, skip the suggestion 
  179. if ( empty( $suggestion['sizes']['thumbnail']['url'] ) ) { 
  180. unset( $suggestions[ $key ] ); 
  181. continue; 
  182.  
  183. // Set the current user's avatar as selected 
  184. if ( ! empty( $current_item_avatar ) && $current_item_avatar == $suggestion['sizes']['thumbnail']['url'] ) { 
  185. $suggestions[ $key ]['selected'] = 1; 
  186.  
  187. // Set the BuddyPress object (user or group) 
  188. $suggestions[ $key ]['object'] = $object; 
  189. } else { 
  190. wp_send_json_error(); 
  191.  
  192. if ( empty( $suggestions ) ) { 
  193. wp_send_json_error(); 
  194.  
  195. // Send the successful response 
  196. wp_send_json_success( array_filter( $suggestions ) ); 
  197.  
  198. /** 
  199. * Are we editing a user avatar suggestion ? 
  200. * Since 1.3.0, as BuddyPress 2.3.0 includes a new avatar UI, we will use it unless 
  201. * User avatar uploads have been disabled. In this case we will load the scripts 
  202. * introduced in 1.2.0 
  203. * @package BP Avatar Suggestions 
  204. * @subpackage Front 
  205. * @since 1.2.0 
  206. * @return bool true if editing a user avatar, false otherwise 
  207. */ 
  208. public function is_user_set_avatar() { 
  209. $retval = false; 
  210.  
  211. if ( empty( $this->enable_users ) || ! bp_is_user() || ! bp_core_get_root_option( 'bp-disable-avatar-uploads' ) ) { 
  212. return $retval; 
  213.  
  214. if ( bp_is_user_change_avatar() && $this->enable_users ) { 
  215. $retval = true; 
  216.  
  217. return $retval; 
  218.  
  219. /** 
  220. * Are we editing a group suggestion during group creation ? 
  221. * Since 1.3.0, as BuddyPress 2.3.0 includes a new avatar UI, we will use it unless 
  222. * Group avatar uploads have been disabled. In this case we will load the scripts 
  223. * introduced in 1.2.0 
  224. * @package BP Avatar Suggestions 
  225. * @subpackage Front 
  226. * @since 1.2.0 
  227. * @return bool true if editing a group avatar, false otherwise 
  228. */ 
  229. public function is_group_create_avatar() { 
  230. $retval = false; 
  231.  
  232. if ( has_filter( 'bp_disable_group_avatar_uploads', array( 'Avatar_Suggestions_Group', 'display_avatars' ), 10, 1 ) ) { 
  233. $reset_filter = true; 
  234. remove_filter( 'bp_disable_group_avatar_uploads', array( 'Avatar_Suggestions_Group', 'display_avatars' ), 10, 1 ); 
  235.  
  236. if ( empty( $this->enable_groups ) || ! bp_disable_group_avatar_uploads() ) { 
  237. return $retval; 
  238.  
  239. // create 
  240. if ( bp_is_group_create() && bp_is_group_creation_step( 'avatar-suggestions' ) ) { 
  241. $retval = true; 
  242.  
  243. if ( ! empty( $reset_filter ) ) { 
  244. add_filter( 'bp_disable_group_avatar_uploads', array( 'Avatar_Suggestions_Group', 'display_avatars' ), 10, 1 ); 
  245.  
  246. return $retval; 
  247.  
  248. /** 
  249. * Are we editing a group suggestion ? 
  250. * Since 1.3.0, as BuddyPress 2.3.0 includes a new avatar UI, we will use it unless 
  251. * Group avatar uploads have been disabled. In this case we will load the scripts 
  252. * introduced in 1.2.0 
  253. * @package BP Avatar Suggestions 
  254. * @subpackage Front 
  255. * @since 1.2.0 
  256. * @return bool true if editing a group avatar, false otherwise 
  257. */ 
  258. public function is_group_manage_avatar() { 
  259. $retval = false; 
  260.  
  261. if ( has_filter( 'bp_disable_group_avatar_uploads', array( 'Avatar_Suggestions_Group', 'display_avatars' ), 10, 1 ) ) { 
  262. $reset_filter = true; 
  263. remove_filter( 'bp_disable_group_avatar_uploads', array( 'Avatar_Suggestions_Group', 'display_avatars' ), 10, 1 ); 
  264.  
  265. if ( empty( $this->enable_groups ) || ! bp_disable_group_avatar_uploads() ) { 
  266. return $retval; 
  267.  
  268. if ( bp_is_group_admin_page() && bp_is_group_admin_screen( 'avatar-suggestions' ) ) { 
  269. $retval = true; 
  270.  
  271. if ( ! empty( $reset_filter ) ) { 
  272. add_filter( 'bp_disable_group_avatar_uploads', array( 'Avatar_Suggestions_Group', 'display_avatars' ), 10, 1 ); 
  273.  
  274. return $retval; 
  275.  
  276. /** 
  277. * Register the script that will be used in the new avatar UI 
  278. * @package BP Avatar Suggestions 
  279. * @subpackage Front 
  280. * @since 1.3.0 
  281. */ 
  282. public function register_script() { 
  283. $bp = buddypress(); 
  284.  
  285. // Register the style 
  286. wp_register_style( 
  287. 'bp-as-front-style',  
  288. $bp->extend->avatar_suggestions->plugin_css . "bp-as-front{$this->min}.css",  
  289. array(),  
  290. $bp->extend->avatar_suggestions->version 
  291. ); 
  292.  
  293. // Register the script 
  294. wp_register_script( 
  295. 'suggestions-bp-avatar',  
  296. $bp->extend->avatar_suggestions->plugin_js . "avatar-suggestions-front{$this->min}.js",  
  297. array( 'bp-avatar' ),  
  298. $bp->extend->avatar_suggestions->version,  
  299. true 
  300. ); 
  301.  
  302. /** 
  303. * Make sure suggestions script and css will be loaded into the avatar UI 
  304. * @package BP Avatar Suggestions 
  305. * @subpackage Front 
  306. * @since 1.3.0 
  307. * @param array $script_data an array containing the avatar UI needed data 
  308. * @param string $object the object the avatar is related to 
  309. * @return array the script/css to load 
  310. */ 
  311. public function include_script( $script_data = array(), $object = '' ) { 
  312. if ( 'group' === $object || 'user' === $object ) { 
  313.  
  314. $check = 'enable_' . $object . 's'; 
  315. if ( empty( $this->{$check} ) ) { 
  316. return $script_data; 
  317.  
  318. $script_data['extra_js'][] = 'suggestions-bp-avatar'; 
  319. $script_data['extra_css'][] = 'bp-as-front-style'; 
  320.  
  321. return $script_data; 
  322.  
  323. /** 
  324. * Add a new navigation into the avatar UI 
  325. * @package BP Avatar Suggestions 
  326. * @subpackage Front 
  327. * @since 1.3.0 
  328. * @param array $avatar_nav an array containing the avatar UI navs 
  329. * @param string $object the object the avatar is related to 
  330. * @return array the avatar nav including the suggestions one if needed 
  331. */ 
  332. public function suggestions_nav( $avatar_nav = array(), $object = '' ) { 
  333. if ( 'group' === $object || 'user' === $object ) { 
  334.  
  335. $check = 'enable_' . $object . 's'; 
  336. if ( empty( $this->{$check} ) ) { 
  337. return $avatar_nav; 
  338.  
  339. $avatar_nav['avatar_suggestions'] = array( 
  340. 'id' => 'avatar_suggestions',  
  341. 'caption' => __( 'Suggestions', 'bp-avatar-suggestions' ),  
  342. 'order' => 15,  
  343. 'hide' => (int) ! $avatar_nav['delete']['hide'],  
  344. ); 
  345.  
  346. return $avatar_nav; 
  347.  
  348. /** 
  349. * Add custom strings for the avatar UI 
  350. * @package BP Avatar Suggestions 
  351. * @subpackage Front 
  352. * @since 1.3.0 
  353. * @param array $strings an array containing the strings 
  354. * @return array the strings including the suggestions ones if needed 
  355. */ 
  356. public function suggestions_strings( $strings = array() ) { 
  357.  
  358. if ( empty( $this->enable_groups ) && empty( $this->enable_users ) ) { 
  359. return $strings; 
  360.  
  361. $strings['avatar_suggestions'] = array( 
  362. 'nonce' => wp_create_nonce( 'avatar_suggestions_selector' ),  
  363. 'avatarSaved' => esc_html__( 'Success: profile photo saved.', 'bp-avatar-suggestions' ),  
  364. 'avatarNotSaved' => esc_html__( 'Error: profile photo not saved.', 'bp-avatar-suggestions' ),  
  365. 'avatarRemoved' => esc_html__( 'Success: profile photo removed.', 'bp-avatar-suggestions' ),  
  366. 'avatarNotRemoved' => esc_html__( 'Error: profile photo not removed.', 'bp-avatar-suggestions' ),  
  367. 'fetching' => esc_html__( 'Please wait, requesting available suggestions.', 'bp-avatar-suggestions' ),  
  368. 'fetchingFailed' => esc_html__( 'There was a problem while requesting suggestions.', 'bp-avatar-suggestions' ),  
  369. 'fetchingSuccess' => esc_html__( 'Click on one of the suggestions to use it as your profile photo.', 'bp-avatar-suggestions' ),  
  370. 'suggestionSelected' => esc_html__( 'To delete your profile photo, click on the selected suggestion.', 'bp-avatar-suggestions' ),  
  371. ); 
  372.  
  373. return $strings; 
  374.  
  375. /** 
  376. * Enqueue script 
  377. * @package BP Avatar Suggestions 
  378. * @subpackage Front 
  379. * @since 1.1.0 
  380. */ 
  381. public function enqueue_script() { 
  382. // Bail if avatar feature is completely disabled 
  383. if ( $this->bail() ) { 
  384. return; 
  385.  
  386. // Bail if we're not on the change-avatar page 
  387. if ( ! $this->is_user_set_avatar() && ! $this->is_group_create_avatar() && ! $this->is_group_manage_avatar() ) { 
  388. return; 
  389.  
  390. // Bail if the user or the group already has an avatar 
  391. if ( ( bp_is_user() && bp_get_user_has_avatar( bp_displayed_user_id() ) ) || ( bp_is_group() && bp_get_group_has_avatar() ) ) { 
  392. return; 
  393.  
  394. $suggestions_settings = $this->suggestions_strings(); 
  395.  
  396. $suggestions_settings = array_merge( $suggestions_settings['avatar_suggestions'], array( 
  397. 'groupCreateContext' => $this->is_group_create_avatar() 
  398. ) ); 
  399.  
  400. if ( bp_is_user() ) { 
  401. $suggestions_settings['item_object'] = 'user'; 
  402. $suggestions_settings['item_id'] = bp_displayed_user_id(); 
  403. } else if ( bp_is_group() || bp_is_group_create() ) { 
  404. // Get the group id 
  405. $group_id = bp_get_current_group_id(); 
  406.  
  407. // Get the new group id 
  408. if ( empty( $group_id ) ) { 
  409. $group_id = bp_get_new_group_id(); 
  410.  
  411. $suggestions_settings['item_object'] = 'group'; 
  412. $suggestions_settings['item_id'] = $group_id; 
  413.  
  414. // Get BuddyPress instance 
  415. $bp = buddypress(); 
  416.  
  417. wp_enqueue_style ( 'bp-as-front-style' ); 
  418. wp_enqueue_script ( 'bp-as-backbone-front', $bp->extend->avatar_suggestions->plugin_js . "bp-as-backbone-front{$this->min}.js", array( 'wp-backbone' ), $bp->extend->avatar_suggestions->version, true ); 
  419. wp_localize_script( 'bp-as-backbone-front', 'avatar_suggestions_vars', $suggestions_settings ); 
  420.  
  421. /** 
  422. * JS Template for the suggestions 
  423. * @package BP Avatar Suggestions 
  424. * @subpackage Front 
  425. * @since 1.3.0 
  426. */ 
  427. public function js_template() { 
  428. ?> 
  429. <script id="tmpl-suggestion" type="text/html"> 
  430. <img class="avatar avatar-suggestion-item" src="{{ data.sizes.thumbnail.url }}" data-suggestionid="{{data.id}}" /> 
  431. </script> 
  432. <?php 
  433.  
  434. /** 
  435. * Template for the Avatar Suggestions selector 
  436. * @package BP Avatar Suggestions 
  437. * @subpackage Front 
  438. * @since 1.2.0 
  439. */ 
  440. public function suggestions_selector() { 
  441. // Bail if avatar feature is completely disabled 
  442. if ( $this->bail() ) { 
  443. return; 
  444.  
  445. // Bail if we're not on the change-avatar page 
  446. if ( ! $this->is_user_set_avatar() && ! $this->is_group_create_avatar() && ! $this->is_group_manage_avatar() ) { 
  447. return; 
  448.  
  449. // Bail if the user or the group already has an avatar 
  450. if ( ( bp_is_user() && bp_get_user_has_avatar( bp_displayed_user_id() ) ) || ( bp_is_group() && bp_get_group_has_avatar() ) ) { 
  451. return; 
  452.  
  453. ?> 
  454. <div id="avatar-suggestions-selector"> 
  455.  
  456. <h4><?php esc_html_e( 'You can select one of the suggested avatars below', 'bp-avatar-suggestions' ) ;?></h4> 
  457. <p class="description"><?php esc_html_e( 'Click to add or remove the suggested avatar. If a suggestion is selected, you will need to remove it before choosing another one.', 'bp-avatar-suggestions' ) ;?></p> 
  458.  
  459. <div class="avatar-suggestions-list"></div> 
  460. <div class="avatar-suggestions-action"></div> 
  461. </div> 
  462. <?php 
  463. // Load the JS Template 
  464. $this->js_template(); 
  465.  
  466. /** 
  467. * Set suggestion 
  468. * @package BP Avatar Suggestions 
  469. * @subpackage Front 
  470. * @since 1.2.0 
  471. */ 
  472. public function set_suggestion() { 
  473. $response = array( 
  474. 'feedback_code' => 'avatarNotSaved',  
  475. ); 
  476.  
  477. if ( empty( $_POST['item_id'] ) || empty( $_POST['avatar_url'] ) || empty( $_POST['item_object'] ) ) { 
  478. wp_send_json_error( $response ); 
  479.  
  480. check_ajax_referer( 'avatar_suggestions_selector', 'nonce' ); 
  481.  
  482. $item_id = absint( $_POST['item_id'] ); 
  483. $avatar = 'http:' . $_POST['avatar_url']; 
  484. if ( is_ssl() ) { 
  485. $avatar = 'https:' . $_POST['avatar_url']; 
  486.  
  487. $avatar = esc_url_raw( $avatar ); 
  488. $object = sanitize_key( $_POST['item_object'] ); 
  489.  
  490. if ( 'group' == $object && bp_is_active( 'groups' ) ) { 
  491. groups_update_groupmeta( $item_id, 'group_avatar_choice', $avatar ); 
  492.  
  493. if ( bp_is_group_admin_page() ) { 
  494. do_action( 'groups_screen_group_admin_avatar', $item_id ); 
  495. } else if ( 'user' == $object ) { 
  496. bp_update_user_meta( $item_id, 'user_avatar_choice', $avatar ); 
  497.  
  498. do_action( 'xprofile_avatar_uploaded' ); 
  499. } else { 
  500. wp_send_json_error( $response ); 
  501.  
  502. // Send the response 
  503. wp_send_json_success( array( 
  504. 'avatar' => html_entity_decode( bp_core_fetch_avatar( array( 
  505. 'object' => $object,  
  506. 'item_id' => $item_id,  
  507. 'html' => false,  
  508. 'type' => 'full',  
  509. ) ) ),  
  510. 'feedback_code' => 'avatarSaved',  
  511. 'item_id' => $item_id,  
  512. ) ); 
  513.  
  514. /** 
  515. * Remove suggestion 
  516. * @package BP Avatar Suggestions 
  517. * @subpackage Front 
  518. * @since 1.2.0 
  519. */ 
  520. public function remove_suggestion() { 
  521. $response = array( 
  522. 'feedback_code' => 'avatarNotRemoved',  
  523. ); 
  524.  
  525. if ( empty( $_POST['item_id'] ) || empty( $_POST['item_object'] ) ) { 
  526. wp_send_json_error( $response ); 
  527.  
  528. $item_id = absint( $_POST['item_id'] ); 
  529. $object = sanitize_key( $_POST['item_object'] ); 
  530.  
  531. check_ajax_referer( 'avatar_suggestions_selector', 'nonce' ); 
  532.  
  533. if ( 'group' == $object && bp_is_active( 'groups' ) ) { 
  534. groups_delete_groupmeta( $item_id, 'group_avatar_choice' ); 
  535.  
  536. if ( bp_is_group_admin_page() ) { 
  537. do_action( 'groups_screen_group_admin_avatar', $item_id ); 
  538. } elseif ( 'user' == $object ) { 
  539. bp_delete_user_meta( $item_id, 'user_avatar_choice' ); 
  540.  
  541. do_action( 'bp_core_delete_existing_avatar' ); 
  542. } else { 
  543. wp_send_json_error( $response ); 
  544.  
  545. $this->avatar_removed = true; 
  546.  
  547. $avatar = html_entity_decode( bp_core_fetch_avatar( array( 
  548. 'object' => $object,  
  549. 'item_id' => $item_id,  
  550. 'type' => 'full',  
  551. 'html' => false,  
  552. ) ) ); 
  553.  
  554. $this->avatar_removed = false; 
  555.  
  556. // Send the response 
  557. wp_send_json_success( array( 
  558. 'avatar' => $avatar,  
  559. 'feedback_code' => 'avatarRemoved',  
  560. 'item_id' => $item_id,  
  561. ) ); 
  562.  
  563. /** 
  564. * Fitler avatar to eventually replace it by a suggestion 
  565. * @package BP Avatar Suggestions 
  566. * @subpackage Front 
  567. * @since 1.1.0 
  568. */ 
  569. function suggestion_avatar( $image = '', $params = array() ) { 
  570. if ( ! empty( $this->avatar_removed ) ) { 
  571. return $image; 
  572.  
  573. if ( ! empty( $params['no_grav'] ) || empty( $params['item_id'] ) ) { 
  574. return $image; 
  575.  
  576. if ( ! $this->enable_users && 'user' == $params['object'] ) { 
  577. return $image; 
  578.  
  579. if ( ! $this->enable_groups && 'group' == $params['object'] ) { 
  580. return $image; 
  581.  
  582. $component_items = array( 'user' => 1, 'group' => 1 ); 
  583. if ( empty( $component_items[ $params['object'] ] ) ) { 
  584. return $image; 
  585.  
  586. $item_id = absint( $params['item_id'] ); 
  587.  
  588. switch( $params['object'] ) { 
  589. case 'group' : 
  590. if ( ! bp_get_group_has_avatar( $item_id ) ) { 
  591.  
  592. $group_choice = groups_get_groupmeta( $item_id, 'group_avatar_choice', true ); 
  593.  
  594. if ( empty( $group_choice ) ) { 
  595. return $image; 
  596. } else { 
  597. $group_choice = str_replace( array( 'https:', 'http:' ), '', $group_choice ); 
  598.  
  599. if ( ! empty( $params['html'] ) ) { 
  600. $image = preg_replace('/src="([^"]*)"/i', 'src="' . $group_choice . '"', $image ); 
  601. } else { 
  602. $image = $group_choice; 
  603. break; 
  604.  
  605. case 'user' : 
  606. if ( ! bp_get_user_has_avatar( $item_id ) ) { 
  607.  
  608. $user_choice = get_user_meta( $item_id, 'user_avatar_choice', true ); 
  609.  
  610. if ( empty( $user_choice ) ) { 
  611. return $image; 
  612. } else { 
  613. $user_choice = str_replace( array( 'https:', 'http:' ), '', $user_choice ); 
  614.  
  615. if ( ! empty( $params['html'] ) ) { 
  616. $image = preg_replace('/src="([^"]*)"/i', 'src="' . $user_choice . '"', $image ); 
  617. } else { 
  618. $image = $user_choice; 
  619.  
  620. break; 
  621.  
  622. /** in case you need to filter with your own function... */ 
  623. return apply_filters( 'bp_as_fetch_suggested_avatar', $image, $params );