Avatar_Suggestions_Admin

Load Admin class.

Defined (1)

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

/includes/bp-avatar-suggestions-admin.php  
  1. class Avatar_Suggestions_Admin { 
  2.  
  3. /** 
  4. * Setup Admin. 
  5. * @package BP Avatar Suggestions 
  6. * @subpackage Admin 
  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->admin ) ) { 
  15. $bp->extend->avatar_suggestions->admin = new self; 
  16.  
  17. return $bp->extend->avatar_suggestions->admin; 
  18.  
  19. /** 
  20. * Constructor method. 
  21. * @package BP Avatar Suggestions 
  22. * @subpackage Admin 
  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 Admin 
  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 Admin 
  48. * @since 1.1.0 
  49. */ 
  50. private function setup_hooks() { 
  51. // Settings 
  52. add_action( 'bp_register_admin_settings', array( $this, 'settings' ) ); 
  53.  
  54. // update plugin's db version 
  55. add_action( 'bp_admin_init', array( $this, 'maybe_update' ) ); 
  56.  
  57. add_action( 'bp_admin_enqueue_scripts', array( $this, 'register_script' ), 2 ); 
  58.  
  59. // javascript 
  60. add_action( 'bp_admin_enqueue_scripts', array( $this, 'enqueue_script' ) ); 
  61.  
  62. // Page 
  63. add_action( bp_core_admin_hook(), array( $this, 'admin_menu' ) ); 
  64.  
  65. add_action( 'admin_head', array( $this, 'admin_head' ), 999 ); 
  66.  
  67. add_action( 'bp_admin_tabs', array( $this, 'admin_tab' ) ); 
  68.  
  69. add_filter( 'set-screen-option', array( $this, 'screen_options' ), 10, 3 ); 
  70.  
  71. // Suggestions Upload 
  72. add_action( 'wp_ajax_avatar_suggestions_upload', array( $this, 'handle_upload' ) ); 
  73.  
  74. /** 
  75. * Bail if avatars are not enabled 
  76. * @package BP Avatar Suggestions 
  77. * @subpackage Admin 
  78. * @since 1.2.0 
  79. * @return bool whether avatars are enabled or not 
  80. */ 
  81. public function bail() { 
  82. // Get BuddyPress instance 
  83. $bp = buddypress(); 
  84.  
  85. return empty( $bp->avatar->show_avatars ); 
  86.  
  87. /** 
  88. * Add a setting to BuddyPress xprofile ones 
  89. * @package BP Avatar Suggestions 
  90. * @subpackage Admin 
  91. * @since 1.1.0 
  92. */ 
  93. public function settings() { 
  94. if ( $this->bail() ) { 
  95. return; 
  96.  
  97. $components = array( 
  98. 'users' => 'bp_xprofile',  
  99. 'groups' => 'bp_groups',  
  100. ); 
  101.  
  102. foreach ( $components as $key => $setting_section ) { 
  103.  
  104. // Allow suggestions 
  105. add_settings_field( 
  106. "bp-avatar-suggestions-enable-{$key}",  
  107. __( 'Avatar Suggestions', 'bp-avatar-suggestions' ),  
  108. array( $this, "settings_callback_{$key}" ),  
  109. 'buddypress',  
  110. $setting_section 
  111. ); 
  112.  
  113. register_setting( 
  114. 'buddypress',  
  115. "bp-avatar-suggestions-enable-{$key}",  
  116. 'intval' 
  117. ); 
  118.  
  119. /** 
  120. * Update plugin version if needed 
  121. * @package BP Avatar Suggestions 
  122. * @subpackage Admin 
  123. * @since 1.1.0 
  124. */ 
  125. public function maybe_update() { 
  126. $db_version = bp_get_option( 'bp-avatar-suggestions-version', 0 ); 
  127. $version = buddypress()->extend->avatar_suggestions->version; 
  128.  
  129. if ( version_compare( $db_version, $version, '<' ) ) { 
  130.  
  131. if ( version_compare( $db_version, '1.2.0', '<' ) ) { 
  132.  
  133. if ( empty( $this->avatar_post_id ) ) { 
  134. $this->avatar_post_id = wp_insert_post( array( 
  135. 'post_title' => __( 'BP Avatar Suggestions required post (do not edit or delete)', 'bp-avatar-suggestions' ),  
  136. 'post_name' => 'bp-avatar-suggestions',  
  137. 'post_status' => 'draft',  
  138. ) ); 
  139.  
  140. // Set the post id to attach the avatar suggestions to. 
  141. bp_update_option( 'bp_avatar_suggestions_post_id', $this->avatar_post_id ); 
  142.  
  143. // Get all attachment ids 
  144. $attachment_ids = bp_get_option( 'suggestion_list_avatar_array', array() ); 
  145.  
  146. // Loop attachments to set their parent 
  147. foreach ( $attachment_ids as $attachment_id ) { 
  148. wp_update_post( array( 'ID' => $attachment_id, 'post_parent' => $this->avatar_post_id ) ); 
  149. update_post_meta( $attachment_id, '_bpas_avatar_type', 1 ); 
  150.  
  151. // Remove an option not used anymore 
  152. bp_delete_option( 'bp-disable-avatar-suggestions' ); 
  153. bp_delete_option( 'suggestion_list_avatar_array' ); 
  154.  
  155. // Init new options 
  156. bp_update_option( 'bp-avatar-suggestions-enable-users', $this->enable_users ); 
  157. bp_update_option( 'bp-avatar-suggestions-enable-groups', $this->enable_groups ); 
  158.  
  159. // Upgrade db version 
  160. bp_update_option( 'bp-avatar-suggestions-version', $version ); 
  161.  
  162. /** 
  163. * Users Setting callback function 
  164. * @package BP Avatar Suggestions 
  165. * @subpackage Admin 
  166. * @since 1.2.0 
  167. */ 
  168. public function settings_callback_users() { 
  169. ?> 
  170. <input id="bp-avatar-suggestions-enable-users" name="bp-avatar-suggestions-enable-users" type="checkbox" value="1" <?php checked( $this->enable_users ); ?> /> 
  171. <label for="bp-avatar-suggestions-enable-users"><?php _e( 'Enable suggestions for users', 'bp-avatar-suggestions' ); ?></label> 
  172. <?php 
  173.  
  174. /** 
  175. * Groups Setting callback function 
  176. * @package BP Avatar Suggestions 
  177. * @subpackage Admin 
  178. * @since 1.2.0 
  179. */ 
  180. public function settings_callback_groups() { 
  181. ?> 
  182. <input id="bp-avatar-suggestions-enable-groups" name="bp-avatar-suggestions-enable-groups" type="checkbox" value="1" <?php checked( $this->enable_groups ); ?> /> 
  183. <label for="bp-avatar-suggestions-enable-groups"><?php _e( 'Enable suggestions for groups', 'bp-avatar-suggestions' ); ?></label> 
  184. <?php 
  185.  
  186. /** 
  187. * Register admin scripts 
  188. * @package BP Avatar Suggestions 
  189. * @subpackage Admin 
  190. * @since 1.3.0 
  191. */ 
  192. public function register_script() { 
  193. $bp = buddypress(); 
  194.  
  195. // Register the style 
  196. wp_register_style( 
  197. 'bp-as-admin-style',  
  198. $bp->extend->avatar_suggestions->plugin_css . "bp-as-admin{$this->min}.css",  
  199. array( 'bp-avatar' ),  
  200. $bp->extend->avatar_suggestions->version 
  201. ); 
  202.  
  203. // Register the script 
  204. wp_register_script( 
  205. 'bp-as-admin-js',  
  206. $bp->extend->avatar_suggestions->plugin_js . "bp-as-admin{$this->min}.js",  
  207. array(),  
  208. $bp->extend->avatar_suggestions->version,  
  209. true 
  210. ); 
  211.  
  212. // Include some data to it 
  213. wp_localize_script( 'bp-as-admin-js', 'bp_as_admin_vars', array( 
  214. 'redirect' => esc_url( add_query_arg( 'page', 'bp-avatar-suggestions', bp_get_admin_url( 'admin.php' ) ) ) 
  215. ) ); 
  216.  
  217. /** 
  218. * Enqueue script 
  219. * @package BP Avatar Suggestions 
  220. * @subpackage Admin 
  221. * @since 1.1.0 
  222. */ 
  223. public function enqueue_script() { 
  224. // Bail if we're not on the change-avatar page 
  225. if ( strpos( get_current_screen()->id, 'bp-avatar-suggestions' ) === false ) { 
  226. return; 
  227.  
  228. // Get BuddyPress instance 
  229. $bp = buddypress(); 
  230.  
  231. wp_enqueue_style ( 'thickbox' ); 
  232. wp_enqueue_script( 'media-upload' ); 
  233.  
  234. // Get Our Uploader 
  235. bp_attachments_enqueue_scripts( 'Avatar_Suggestions_Attachment' ); 
  236.  
  237. /** 
  238. * Set the plugin's page & eventually update db version 
  239. * @package BP Avatar Suggestions 
  240. * @subpackage Admin 
  241. * @since 1.1.0 
  242. */ 
  243. public function admin_menu() { 
  244. if ( $this->bail() ) { 
  245. return; 
  246.  
  247. $page = bp_core_do_network_admin() ? 'settings.php' : 'options-general.php'; 
  248.  
  249. $hook_as = add_submenu_page( 
  250. $page,  
  251. __( 'Avatar Suggestions Settings', 'bp-avatar-suggestions' ),  
  252. __( 'Avatar Suggestions Settings', 'bp-avatar-suggestions' ),  
  253. 'manage_options',  
  254. 'bp-avatar-suggestions',  
  255. array( $this, 'admin_display' ) 
  256. ); 
  257.  
  258. add_action( "admin_head-$hook_as", array( $this, 'modify_highlight' ) ); 
  259. add_action( "load-$hook_as", array( $this, 'admin_load' ) ); 
  260.  
  261. /** 
  262. * Load the Suggestion WP List table. 
  263. * @since 1.2.0 
  264. */ 
  265. public static function get_list_table_class( $class = '' ) { 
  266. if ( empty( $class ) ) { 
  267. return; 
  268.  
  269. if ( ! class_exists( 'WP_List_Table') ) { 
  270. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); 
  271.  
  272. require_once( buddypress()->extend->avatar_suggestions->includes_dir . 'bp-avatar-suggestions-list-table.php' ); 
  273.  
  274. return new $class(); 
  275.  
  276. /** 
  277. * Set pagination. 
  278. * @since 1.2.0 
  279. */ 
  280. public function screen_options( $value = 0, $option = '', $new_value = 0 ) { 
  281. if ( 'settings_page_bp_avatar_suggestions_network_per_page' != $option && 'settings_page_bp_avatar_suggestions_per_page' != $option ) { 
  282. return $value; 
  283.  
  284. // Per page 
  285. $new_value = (int) $new_value; 
  286. if ( $new_value < 1 || $new_value > 999 ) { 
  287. return $value; 
  288.  
  289. return $new_value; 
  290.  
  291. /** 
  292. * Update the suggestion list if needed 
  293. * @package BP Avatar Suggestions 
  294. * @subpackage Admin 
  295. * @since 1.1.0 
  296. */ 
  297. public function admin_load() { 
  298. $this->suggestion_list_table = self::get_list_table_class( 'BP_Avatar_Suggestions_List_Table' ); 
  299.  
  300. // Build redirection URL 
  301. $redirect_to = add_query_arg( 
  302. array( 
  303. 'page' => 'bp-avatar-suggestions',  
  304. ),  
  305. bp_get_admin_url( 'admin.php' ) 
  306. ); 
  307.  
  308. // Get action 
  309. $doaction = bp_admin_list_table_current_bulk_action(); 
  310.  
  311. do_action( 'bp_avatar_suggestions_admin_load', $doaction, $_REQUEST, $redirect_to ); 
  312.  
  313. if ( ( ! empty( $doaction ) && -1 != $doaction ) || ! empty( $_POST['changeit'] ) ) { 
  314.  
  315. check_admin_referer( 'bulk-suggestions' ); 
  316. $suggestions = array(); 
  317.  
  318. if ( ! empty( $_GET['suggestion_id'] ) ) { 
  319. $suggestions = wp_parse_id_list( $_GET['suggestion_id'] ); 
  320. } else if ( ! empty( $_POST['allsuggestions'] ) ) { 
  321. $suggestions = wp_parse_id_list( $_POST['allsuggestions'] ); 
  322.  
  323. if ( 'delete' == $doaction && ! empty( $suggestions ) ) { 
  324.  
  325. // Delete avatar 
  326. foreach ( $suggestions as $suggestion_id ) { 
  327. wp_delete_attachment( $suggestion_id, true ); 
  328.  
  329. wp_safe_redirect( add_query_arg( 'deleted', count( $suggestions ), $redirect_to ) ); 
  330. exit(); 
  331. } else if ( ! empty( $_POST['avatar_suggestions_type'] ) && ! empty( $suggestions ) ) { 
  332.  
  333. // Update avatar types 
  334. foreach ( $suggestions as $suggestion_id ) { 
  335. update_post_meta( $suggestion_id, '_bpas_avatar_type', (int) $_POST['avatar_suggestions_type'] ); 
  336.  
  337. wp_safe_redirect( add_query_arg( 'changed', count( $suggestions ), $redirect_to ) ); 
  338. exit(); 
  339.  
  340. // per_page screen option 
  341. add_screen_option( 'per_page', array( 'label' => _x( 'Suggestions', 'Suggestions per page (screen options)', 'bp-avatar-suggestions' ) ) ); 
  342.  
  343. // Set help tabs 
  344. get_current_screen()->add_help_tab( array( 
  345. 'id' => 'bp-avatar-suggestions-overview',  
  346. 'title' => __( 'Overview', 'bp-avatar-suggestions' ),  
  347. 'content' => 
  348. '<p>' . __( 'This is the administration screen for avatar suggestions.', 'bp-avatar-suggestions' ) . '</p>' . 
  349. '<p>' . __( 'From the screen options, you can customize the displayed columns and the pagination of this screen.', 'bp-avatar-suggestions' ) . '</p>' . 
  350. '<p>' . __( 'You can add a new suggestion by clicking on the "Add new" button to open the uploader window.', 'bp-avatar-suggestions' ) . ' ' . 
  351. __( ' Send your image files from this window and once every image has been uploaded, simply close the uploader window to refresh the list of suggestions.', 'bp-avatar-suggestions' ) . '</p>' 
  352. ) ); 
  353.  
  354. get_current_screen()->add_help_tab( array( 
  355. 'id' => 'bp-avatar-suggestions-actions',  
  356. 'title' => __( 'Actions', 'bp-avatar-suggestions' ),  
  357. 'content' => 
  358. '<p>' . __( 'Hovering over a row in the suggestions list will display the delete link to eventually remove an avatar.', 'bp-avatar-suggestions' ) . '</p>' . 
  359. '<p>' . __( 'If you need to remove more than one avatar, you can use the Delete Bulk action.', 'bp-avatar-suggestions' ) . '</p>' . 
  360. '<p>' . __( 'To change the avatar type, activate the corresponding checkboxes and use the "Change avatar type" dropdown to select the type before clicking on the "Change" button.', 'bp-avatar-suggestions' ) . '</p>' 
  361. ) ); 
  362.  
  363. // Help panel - sidebar links 
  364. get_current_screen()->set_help_sidebar( 
  365. '<p><strong>' . __( 'For more information:', 'bp-avatar-suggestions' ) . '</strong></p>' . 
  366. '<p>' . __( '<a href="https://wordpress.org/support/plugin/bp-avatar-suggestions">Support Forums</a>', 'bp-avatar-suggestions' ) . '</p>' 
  367. ); 
  368.  
  369. /** 
  370. * Modify highlighted menu 
  371. * @package BP Avatar Suggestions 
  372. * @subpackage Admin 
  373. * @since 1.1.0 
  374. */ 
  375. public function modify_highlight() { 
  376. global $plugin_page, $submenu_file; 
  377.  
  378. // This tweaks the Settings subnav menu to show only one BuddyPress menu item 
  379. if ( $plugin_page == 'bp-avatar-suggestions') { 
  380. $submenu_file = 'bp-components'; 
  381.  
  382. /** 
  383. * Hide submenu 
  384. * @package BP Avatar Suggestions 
  385. * @subpackage Admin 
  386. * @since 1.1.0 
  387. */ 
  388. public function admin_head() { 
  389. $page = bp_core_do_network_admin() ? 'settings.php' : 'options-general.php'; 
  390.  
  391. remove_submenu_page( $page, 'bp-avatar-suggestions' ); 
  392.  
  393. /** 
  394. * Suggestions tab 
  395. * @package BP Avatar Suggestions 
  396. * @subpackage Admin 
  397. * @since 1.1.0 
  398. */ 
  399. public function admin_tab() { 
  400. if ( $this->bail() ) { 
  401. return; 
  402.  
  403. $class = false; 
  404.  
  405. if ( strpos( get_current_screen()->id, 'bp-avatar-suggestions' ) !== false ) { 
  406. $class = "nav-tab-active"; 
  407. ?> 
  408. <a href="<?php echo esc_url( bp_get_admin_url( add_query_arg( array( 'page' => 'bp-avatar-suggestions' ), 'admin.php' ) ) );?>" class="nav-tab <?php echo $class;?>"><?php esc_html_e( 'Avatar Suggestions', 'bp-avatar-suggestions' );?></a> 
  409. <?php 
  410.  
  411. /** 
  412. * Display the admin 
  413. * @package BP Avatar Suggestions 
  414. * @subpackage Admin 
  415. * @since 1.1.0 
  416. */ 
  417. public function admin_display() { 
  418. // Prepare the group items for display 
  419. $this->suggestion_list_table->prepare_items(); 
  420.  
  421. $form_url = add_query_arg( 
  422. array( 
  423. 'page' => 'bp-avatar-suggestions',  
  424. ),  
  425. bp_get_admin_url( 'admin.php' ) 
  426. ); 
  427.  
  428. // User feedback 
  429. $message = false; 
  430. if ( ! empty( $_GET['deleted'] ) ) { 
  431. $message = sprintf( 
  432. _nx( '%s suggestion was deleted.', '%s suggestions were deleted.',  
  433. absint( $_GET['deleted'] ),  
  434. 'suggestion deleted',  
  435. 'bp-avatar-suggestions' 
  436. ), number_format_i18n( absint( $_GET['deleted'] ) ) ); 
  437. } else if ( ! empty( $_GET['changed'] ) ) { 
  438. $message = sprintf( 
  439. _nx( '%s suggestion had their type changed.', '%s suggestions had their type changed.',  
  440. absint( $_GET['changed'] ),  
  441. 'suggestion type edited',  
  442. 'bp-avatar-suggestions' 
  443. ), number_format_i18n( absint( $_GET['changed'] ) ) ); 
  444. ?> 
  445.  
  446. <div class="wrap"> 
  447. <h1><?php esc_html_e( 'BuddyPress Settings', 'bp-avatar-suggestions' ); ?></h1> 
  448.  
  449. <h2 class="nav-tab-wrapper"><?php bp_core_admin_tabs( __( 'Avatar Suggestions', 'bp-avatar-suggestions' ) ); ?></h2> 
  450.  
  451. <?php if ( ! empty( $message ) ) : ?> 
  452. <div id="message" class="updated"><p><?php echo esc_html( $message ); ?></p></div> 
  453. <?php 
  454. unset( $message ); 
  455. endif; ?> 
  456.  
  457. <h3><?php _e( 'Suggestions', 'bp-avatar-suggestions' ); ?> 
  458.  
  459. <?php if ( bp_current_user_can( 'upload_files' ) ) : ?> 
  460. <a href="#TB_inline?width=800px&height=400px&inlineId=bp-avatar-suggestions-uploader" title="<?php esc_attr_e( 'Add new suggestion(s)', 'bp-avatar-suggestions' );?>" class="thickbox avatar_upload_image_button add-new-h2"> 
  461. <?php echo esc_html_x( 'Add New', 'Avatar suggestions add button', 'bp-avatar-suggestions' ); ?> 
  462. </a> 
  463. <div id="bp-avatar-suggestions-uploader" style="display:none;"> 
  464. <?php /** Markup for the uploader */ ?> 
  465. <div class="bp-avatar-suggestions"></div> 
  466. <div class="bp-avatar-status"></div> 
  467.  
  468. <?php bp_attachments_get_template_part( 'uploader' ); 
  469. /** Markup for the uploader */ ?> 
  470. </div> 
  471.  
  472. <?php endif; ?> 
  473. </h3> 
  474.  
  475. <?php $this->suggestion_list_table->views(); ?> 
  476.  
  477. <form id="bp-avatar-suggestions-form" action="<?php echo esc_url( $form_url );?>" method="post"> 
  478. <?php $this->suggestion_list_table->display(); ?> 
  479. </form> 
  480.  
  481. </div> 
  482. <?php 
  483.  
  484. /** 
  485. * Upload the suggestions as WordPress attachments 
  486. * @package BP Avatar Suggestions 
  487. * @subpackage Admin 
  488. * @since 1.3.0 
  489. */ 
  490. public function handle_upload() { 
  491. // Bail if not a POST action 
  492. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 
  493. wp_die(); 
  494.  
  495. /** 
  496. * Sending the json response will be different if 
  497. * the current Plupload runtime is html4 
  498. */ 
  499. $is_html4 = false; 
  500. if ( ! empty( $_POST['html4' ] ) ) { 
  501. $is_html4 = true; 
  502.  
  503. // Check the nonce 
  504. check_admin_referer( 'bp-uploader' ); 
  505.  
  506. // Init the BuddyPress parameters 
  507. $bp_params = array(); 
  508.  
  509. // We need it to carry on 
  510. if ( ! empty( $_POST['bp_params' ] ) ) { 
  511. $bp_params = $_POST['bp_params' ]; 
  512. } else { 
  513. bp_attachments_json_response( false, $is_html4 ); 
  514.  
  515. // Check params 
  516. if ( empty( $bp_params['object'] ) || empty( $bp_params['item_id'] ) || 'post' !== $bp_params['object'] || (int) $this->avatar_post_id !== (int) $bp_params['item_id'] ) { 
  517. bp_attachments_json_response( false, $is_html4 ); 
  518.  
  519. // Capability check 
  520. if ( ! bp_current_user_can( 'upload_files' ) ) { 
  521. bp_attachments_json_response( false, $is_html4 ); 
  522.  
  523. $suggestion_attachment = new Avatar_Suggestions_Attachment(); 
  524. $suggestion = $suggestion_attachment->upload( $_FILES ); 
  525.  
  526. // Error while trying to upload the file 
  527. if ( ! empty( $suggestion['error'] ) ) { 
  528. bp_attachments_json_response( false, $is_html4, array( 
  529. 'type' => 'upload_error',  
  530. 'message' => $suggestion['error'],  
  531. ) ); 
  532.  
  533. /** 
  534. * Simulate WordPress's media_handle_upload() function 
  535. */ 
  536. $time = current_time( 'mysql' ); 
  537. $post = get_post( $bp_params['item_id'] ); 
  538.  
  539. if ( is_a( $post, 'WP_Post' ) ) { 
  540. if ( substr( $post->post_date, 0, 4 ) > 0 ) { 
  541. $time = $post->post_date; 
  542.  
  543. $name_parts = pathinfo( $suggestion['file'] ); 
  544. $url = $suggestion['url']; 
  545. $type = $suggestion['type']; 
  546. $file = $suggestion['file']; 
  547. $title = $name_parts['filename']; 
  548. $content = ''; 
  549. $excerpt = ''; 
  550. $image_meta = @wp_read_image_metadata( $file ); 
  551.  
  552. if ( 0 === strpos( $type, 'image/' ) && ! empty( $image_meta ) ) { 
  553. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { 
  554. $title = $image_meta['title']; 
  555.  
  556. if ( trim( $image_meta['caption'] ) ) { 
  557. $excerpt = $image_meta['caption']; 
  558.  
  559. // Construct the attachment array 
  560. $attachment = array( 
  561. 'post_mime_type' => $type,  
  562. 'guid' => $url,  
  563. 'post_parent' => $post->ID,  
  564. 'post_title' => $title,  
  565. 'post_content' => $content,  
  566. 'post_excerpt' => $excerpt,  
  567. ); 
  568.  
  569. // Save the data 
  570. $attachment_id = wp_insert_attachment( $attachment, $file, $post->ID ); 
  571. if ( ! is_wp_error( $attachment_id ) ) { 
  572.  
  573. // Add the avatar image sizes 
  574. add_image_size( 'bp_avatar_suggestions', bp_core_avatar_full_width(), bp_core_avatar_full_height(), true ); 
  575. add_filter( 'intermediate_image_sizes_advanced', array( $this, 'restrict_image_sizes' ), 10, 1 ); 
  576.  
  577. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); 
  578.  
  579. // Remove it so no other attachments will be affected 
  580. remove_image_size( 'bp_avatar_suggestions' ); 
  581. remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'restrict_image_sizes' ), 10, 1 ); 
  582.  
  583. // Try to get the bp_avatar_suggestions size 
  584. $avatar = wp_get_attachment_image_src( $attachment_id, 'bp_avatar_suggestions', true ); 
  585. if ( ! empty( $avatar[0] ) ) { 
  586. $url = $avatar[0]; 
  587.  
  588. // Finally return the avatar to the editor 
  589. bp_attachments_json_response( true, $is_html4, array( 
  590. 'name' => esc_html( $title ),  
  591. 'url' => esc_url_raw( $url ),  
  592. ) ); 
  593. } else { 
  594. bp_attachments_json_response( false, $is_html4, array( 
  595. 'type' => 'upload_error',  
  596. 'message' => esc_html__( 'Something went wrong while saving the image', 'bp-avatar-suggestions' ),  
  597. ) ); 
  598.  
  599. /** 
  600. * Only keep the avatar suggestions size and thumbnail one for backcompat 
  601. * @package BP Avatar Suggestions 
  602. * @subpackage Admin 
  603. * @since 1.3.0 
  604. * @param array $sizes the available WordPress image sizes 
  605. */ 
  606. public function restrict_image_sizes( $sizes = array() ) { 
  607. return array_intersect_key( $sizes, array( 'thumbnail' => true, 'bp_avatar_suggestions' => true ) );