Avatar_Suggestions

The BP Avatar Suggestions Avatar Suggestions class.

Defined (1)

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

/bp-avatar-suggestions.php  
  1. class Avatar_Suggestions { 
  2. /** 
  3. * Instance of this class. 
  4. * @package BP Avatar Suggestions 
  5. * @since 1.1.0 
  6. * @var object 
  7. */ 
  8. protected static $instance = null; 
  9.  
  10. /** 
  11. * Required BuddyPress version for this plugin. 
  12. * @package BP Avatar Suggestions 
  13. * @since 1.1.0 
  14. * @var string 
  15. */ 
  16. public static $required_bp_version = '2.5.0'; 
  17.  
  18. /** 
  19. * BuddyPress config. 
  20. * @package BP Avatar Suggestions 
  21. * @since 1.1.0 
  22. * @var array 
  23. */ 
  24. public static $bp_config = array(); 
  25.  
  26. /** 
  27. * Plugin name. 
  28. * @package BP Avatar Suggestions 
  29. * @since 1.1.0 
  30. * @var string 
  31. */ 
  32. public static $plugin_name = 'BP Avatar Suggestions'; 
  33.  
  34. /** 
  35. * Initialize the plugin 
  36. * @package BP Avatar Suggestions 
  37. * @since 1.1.0 
  38. */ 
  39. private function __construct() { 
  40. // First you will set your plugin's globals 
  41. $this->setup_globals(); 
  42. // Then include the needed files 
  43. $this->includes(); 
  44. // Then hook to BuddyPress actions & filters 
  45. $this->setup_hooks(); 
  46.  
  47. /** 
  48. * Return an instance of this class. 
  49. * @package BP Avatar Suggestions 
  50. * @since 1.1.0 
  51. * @return object A single instance of this class. 
  52. */ 
  53. public static function start() { 
  54.  
  55. // If the single instance hasn't been set, set it now. 
  56. if ( null == self::$instance ) { 
  57. self::$instance = new self; 
  58.  
  59. return self::$instance; 
  60.  
  61. /** 
  62. * Sets some globals for the plugin 
  63. * @package BP Avatar Suggestions 
  64. * @since 1.1.0 
  65. */ 
  66. private function setup_globals() { 
  67.  
  68. /** Versions & domain ***********************************/ 
  69. $this->version = '1.3.2'; 
  70. $this->domain = 'bp-avatar-suggestions'; 
  71.  
  72. /** Paths ***********************************************/ 
  73. $this->file = __FILE__; 
  74. $this->basename = plugin_basename( $this->file ); 
  75. $this->plugin_dir = plugin_dir_path( $this->file ); 
  76. $this->includes_dir = trailingslashit( $this->plugin_dir . 'includes' ); 
  77. $this->lang_dir = trailingslashit( $this->plugin_dir . 'languages' ); 
  78.  
  79. /** Urls ***********************************************/ 
  80. $this->plugin_url = plugin_dir_url( $this->file ); 
  81. $this->plugin_js = trailingslashit( $this->plugin_url . 'js' ); 
  82. $this->plugin_css = trailingslashit( $this->plugin_url . 'css' ); 
  83.  
  84. $this->avatar_post_id = bp_get_option( 'bp_avatar_suggestions_post_id', 0 ); 
  85. $this->enable_users = bp_get_option( 'bp-avatar-suggestions-enable-users', 1 ); 
  86. $this->enable_groups = bp_get_option( 'bp-avatar-suggestions-enable-groups', 1 ); 
  87.  
  88. /** 
  89. * Include the component's files. 
  90. * @package BP Avatar Suggestions 
  91. * @since 1.1.0 
  92. */ 
  93. private function includes() { 
  94. if ( self::bail() || ( ! bp_is_active( 'xprofile' ) && ! bp_is_active( 'groups' ) ) ) { 
  95. return; 
  96.  
  97. require( $this->includes_dir . 'bp-avatar-suggestions-front.php' ); 
  98.  
  99. if ( bp_is_active( 'groups' ) && ! empty( $this->enable_groups ) ) { 
  100. require( $this->includes_dir . 'bp-avatar-suggestions-groups.php' ); 
  101.  
  102. if ( is_admin() ) { 
  103. require( $this->includes_dir . 'bp-avatar-suggestions-attachment.php' ); 
  104. require( $this->includes_dir . 'bp-avatar-suggestions-admin.php' ); 
  105.  
  106. /** 
  107. * Sets the key hooks to add an action or a filter to 
  108. * @package BP Avatar Suggestions 
  109. * @since 1.1.0 
  110. */ 
  111. private function setup_hooks() { 
  112.  
  113. if ( ! self::bail() && ( bp_is_active( 'xprofile' ) || bp_is_active( 'groups' ) ) ) { 
  114. // Load Front 
  115. add_action( 'bp_loaded', 'bp_avatar_suggestions_front', 20 ); 
  116.  
  117. // Make sure to intercept a deleted avatar 
  118. add_action( 'delete_attachment', array( $this, 'cleanup_avatar_data' ), 10, 1 ); 
  119.  
  120. // Make sure to intercept an edited avatar 
  121. add_action( 'edit_attachment', array( $this, 'is_avatar_suggestion' ), 10, 1 ); 
  122.  
  123. // Make sure to intercept a new avatar 
  124. add_action( 'add_attachment', array( $this, 'is_avatar_suggestion' ), 10, 1 ); 
  125.  
  126. // Load Admin 
  127. if ( is_admin() ) { 
  128. add_action( 'bp_loaded', 'bp_avatar_suggestions_admin', 20 ); 
  129.  
  130. // loads the languages.. 
  131. add_action( 'bp_init', array( $this, 'load_textdomain' ), 5 ); 
  132.  
  133. } else { 
  134. // Display a warning message in network admin or admin 
  135. add_action( self::$bp_config['network_active'] ? 'network_admin_notices' : 'admin_notices', array( $this, 'warning' ) ); 
  136.  
  137. /** 
  138. * Remove all avatar datas related to an attachment 
  139. * @package BP Avatar Suggestions 
  140. * @since 1.2.0 
  141. */ 
  142. public function cleanup_avatar_data( $attachment_id = 0 ) { 
  143. // All avatar suggestions are saved in the root blog 
  144. if ( ! bp_is_root_blog() || empty( $attachment_id ) ) { 
  145. return; 
  146.  
  147. $attachement = get_post( $attachment_id ); 
  148.  
  149. // Make sure it's an avatar suggestion 
  150. if ( empty( $attachement->post_parent ) || $this->avatar_post_id != $attachement->post_parent ) { 
  151. return; 
  152.  
  153. // Get the url of the avatar 
  154. $avatar_url = wp_get_attachment_image_src( $attachment_id, 'bp_avatar_suggestions' ); 
  155.  
  156. // Fallback on thumbnail 
  157. if ( empty( $avatar_url[0] ) || empty( $avatar_url[3] ) ) { 
  158. $avatar_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail' ); 
  159.  
  160. // Delete all user metas having the $avatar_url 
  161. delete_metadata( 'user', false, 'user_avatar_choice', $avatar_url[0], true ); 
  162.  
  163. if ( bp_is_active( 'groups' ) ) { 
  164. // Delete all user metas having the $avatar_url 
  165. groups_delete_groupmeta( false, 'group_avatar_choice', $avatar_url[0], true ); 
  166.  
  167. /** 
  168. * Make sure an avatar suggestion has a type 
  169. * @package BP Avatar Suggestions 
  170. * @since 1.2.0 
  171. */ 
  172. public function is_avatar_suggestion( $attachment_id = 0 ) { 
  173. // All avatar suggestions are saved in the root blog 
  174. if ( ! bp_is_root_blog() || empty( $attachment_id ) ) { 
  175. return; 
  176.  
  177. $attachement = get_post( $attachment_id ); 
  178.  
  179. // Make sure it's an avatar suggestion 
  180. if ( empty( $attachement->post_parent ) || $this->avatar_post_id != $attachement->post_parent ) { 
  181. return; 
  182.  
  183. $has_meta = get_post_meta( $attachment_id, '_bpas_avatar_type', true ); 
  184.  
  185. // Update to the 'All type' 
  186. if ( empty( $has_meta ) ) { 
  187. update_post_meta( $attachment_id, '_bpas_avatar_type', 1 ); 
  188.  
  189. /** 
  190. * Display a warning message to admin 
  191. * @package BP Avatar Suggestions 
  192. * @since 1.1.0 
  193. */ 
  194. public function warning() { 
  195. $warnings = array(); 
  196.  
  197. if( ! self::version_check() ) { 
  198. $warnings[] = sprintf( __( '%1$s requires at least version %2$s of BuddyPress.', 'bp-avatar-suggestions' ), self::$plugin_name, self::$required_bp_version ); 
  199.  
  200. if ( ! empty( self::$bp_config ) ) { 
  201. $config = self::$bp_config; 
  202. } else { 
  203. $config = self::config_check(); 
  204.  
  205. if ( bp_core_do_network_admin() && ! $config['network_status'] ) { 
  206. $warnings[] = sprintf( __( '%s and BuddyPress need to share the same network configuration.', 'bp-avatar-suggestions' ), self::$plugin_name ); 
  207.  
  208. if ( ! empty( $warnings ) ) : 
  209. ?> 
  210. <div id="message" class="error"> 
  211. <?php foreach ( $warnings as $warning ) : ?> 
  212. <p><?php echo esc_html( $warning ) ; ?></p> 
  213. <?php endforeach ; ?> 
  214. </div> 
  215. <?php 
  216. endif; 
  217.  
  218. /** Utilities *****************************************************************************/ 
  219.  
  220. /** 
  221. * Checks BuddyPress version 
  222. * @package BP Avatar Suggestions 
  223. * @since 1.1.0 
  224. */ 
  225. public static function version_check() { 
  226. // taking no risk 
  227. if ( ! defined( 'BP_VERSION' ) ) 
  228. return false; 
  229.  
  230. return version_compare( BP_VERSION, self::$required_bp_version, '>=' ); 
  231.  
  232. /** 
  233. * Checks if your plugin's config is similar to BuddyPress 
  234. * @package BP Avatar Suggestions 
  235. * @since 1.1.0 
  236. */ 
  237. public static function config_check() { 
  238. /** 
  239. * blog_status : true if your plugin is activated on the same blog 
  240. * network_active : true when your plugin is activated on the network 
  241. * network_status : BuddyPress & your plugin share the same network status 
  242. */ 
  243. self::$bp_config = array( 
  244. 'blog_status' => false,  
  245. 'network_active' => false,  
  246. 'network_status' => true,  
  247. ); 
  248.  
  249. if ( get_current_blog_id() == bp_get_root_blog_id() ) { 
  250. self::$bp_config['blog_status'] = true; 
  251.  
  252. $network_plugins = get_site_option( 'active_sitewide_plugins', array() ); 
  253.  
  254. // No Network plugins 
  255. if ( empty( $network_plugins ) ) { 
  256. return self::$bp_config; 
  257.  
  258. $plugin_basename = plugin_basename( __FILE__ ); 
  259.  
  260. // Looking for BuddyPress and this plugin 
  261. $check = array( buddypress()->basename, $plugin_basename ); 
  262.  
  263. // Are they active on the network ? 
  264. $network_active = array_diff( $check, array_keys( $network_plugins ) ); 
  265.  
  266. // If result is 1, your plugin is network activated 
  267. // and not BuddyPress or vice & versa. Config is not ok 
  268. if ( count( $network_active ) == 1 ) { 
  269. self::$bp_config['network_status'] = false; 
  270.  
  271. self::$bp_config['network_active'] = isset( $network_plugins[ $plugin_basename ] ); 
  272.  
  273. return self::$bp_config; 
  274.  
  275. /** 
  276. * Bail if BuddyPress config is different than this plugin 
  277. * @package BP Avatar Suggestions 
  278. * @since 1.1.0 
  279. */ 
  280. public static function bail() { 
  281. $retval = false; 
  282.  
  283. $config = self::config_check(); 
  284.  
  285. if ( ! self::version_check() || ( ! $config['blog_status'] && ! $config['network_status'] ) ) { 
  286. $retval = true; 
  287.  
  288. return $retval; 
  289.  
  290. /** 
  291. * Loads the translation files 
  292. * @package BP Avatar Suggestions 
  293. * @since 1.1.0 
  294. * @since 1.3.2 Use load_plugin_textdomain() 
  295. * @uses get_locale() to get the language of WordPress config 
  296. * @uses load_texdomain() to load the translation if any is available for the language 
  297. */ 
  298. public function load_textdomain() { 
  299. // Traditional WordPress plugin locale filter 
  300. $locale = apply_filters( 'plugin_locale', get_locale(), $this->domain ); 
  301. $mofile = sprintf( '%1$s-%2$s.mo', $this->domain, $locale ); 
  302.  
  303. // Setup paths to current locale file 
  304. $mofile_local = $this->lang_dir . $mofile; 
  305. $mofile_global = WP_LANG_DIR . '/' . $this->domain . '/' . $mofile; 
  306.  
  307. // Look in global /wp-content/languages/bp-avatar-suggestions folder 
  308. load_textdomain( $this->domain, $mofile_global ); 
  309.  
  310. // Look in local /wp-content/plugins/bp-avatar-suggestions/languages/ folder 
  311. load_textdomain( $this->domain, $mofile_local ); 
  312.  
  313. // Look in global /wp-content/languages/plugins/ 
  314. load_plugin_textdomain( $this->domain ); 
  315.