PodsField_Avatar

The Pods - Custom Content Types and Fields PodsField Avatar class.

Defined (1)

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

/classes/fields/avatar.php  
  1. class PodsField_Avatar extends PodsField { 
  2.  
  3. /** 
  4. * Field Type Group 
  5. * @var string 
  6. * @since 2.0 
  7. */ 
  8. public static $group = 'Relationships / Media'; 
  9.  
  10. /** 
  11. * Field Type Identifier 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. public static $type = 'avatar'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'Avatar'; 
  23.  
  24. /** 
  25. * Pod Types supported on (true for all, false for none, or give array of specific types supported) 
  26. * @var array|bool 
  27. * @since 2.1 
  28. */ 
  29. public static $pod_types = array( 'user' ); 
  30.  
  31. /** 
  32. * Do things like register/enqueue scripts and stylesheets 
  33. * @since 2.0 
  34. */ 
  35. public function __construct () { 
  36.  
  37.  
  38. /** 
  39. * Add options and set defaults to 
  40. * @param array $options 
  41. * @since 2.0 
  42. */ 
  43. public function options () { 
  44. $sizes = get_intermediate_image_sizes(); 
  45.  
  46. $image_sizes = array(); 
  47.  
  48. foreach ( $sizes as $size ) { 
  49. $image_sizes[ $size ] = ucwords( str_replace( '-', ' ', $size ) ); 
  50.  
  51. $options = array( 
  52. self::$type . '_uploader' => array( 
  53. 'label' => __( 'Avatar Uploader', 'pods' ),  
  54. 'default' => 'attachment',  
  55. 'type' => 'pick',  
  56. 'data' => apply_filters( 
  57. 'pods_form_ui_field_avatar_uploader_options',  
  58. array( 
  59. 'attachment' => __( 'Attachments (WP Media Library)', 'pods' ),  
  60. 'plupload' => __( 'Plupload', 'pods' ) 
  61. ),  
  62. 'dependency' => true 
  63. ),  
  64. self::$type . '_attachment_tab' => array( 
  65. 'label' => __( 'Attachments Default Tab', 'pods' ),  
  66. 'depends-on' => array( self::$type . '_uploader' => 'attachment' ),  
  67. 'default' => 'upload',  
  68. 'type' => 'pick',  
  69. 'data' => array( 
  70. // keys MUST match WP's router names 
  71. 'upload' => __( 'Upload File', 'pods' ),  
  72. 'browse' => __( 'Media Library', 'pods' ) 
  73. ),  
  74. self::$type . '_restrict_filesize' => array( 
  75. 'label' => __( 'Restrict File Size', 'pods' ),  
  76. 'depends-on' => array( self::$type . '_uploader' => 'plupload' ),  
  77. 'default' => '10MB',  
  78. 'type' => 'text' 
  79. ),  
  80. self::$type . '_add_button' => array( 
  81. 'label' => __( 'Add Button Text', 'pods' ),  
  82. 'default' => __( 'Add File', 'pods' ),  
  83. 'type' => 'text' 
  84. ),  
  85. self::$type . '_modal_title' => array( 
  86. 'label' => __( 'Modal Title', 'pods' ),  
  87. 'depends-on' => array( self::$type . '_uploader' => 'attachment' ),  
  88. 'default' => __( 'Attach a file', 'pods' ),  
  89. 'type' => 'text' 
  90. ),  
  91. self::$type . '_modal_add_button' => array( 
  92. 'label' => __( 'Modal Add Button Text', 'pods' ),  
  93. 'depends-on' => array( self::$type . '_uploader' => 'attachment' ),  
  94. 'default' => __( 'Add File', 'pods' ),  
  95. 'type' => 'text' 
  96. ); 
  97.  
  98. if ( !pods_version_check( 'wp', '3.5' ) ) { 
  99. unset( $options[ self::$type . '_modal_title' ] ); 
  100. unset( $options[ self::$type . '_modal_add_button' ] ); 
  101.  
  102. $options[ self::$type . '_attachment_tab' ][ 'default' ] = 'type'; 
  103. $options[ self::$type . '_attachment_tab' ][ 'data' ] = array( 
  104. 'type' => __( 'Upload File', 'pods' ),  
  105. 'library' => __( 'Media Library', 'pods' ) 
  106. ); 
  107.  
  108. return $options; 
  109.  
  110. /** 
  111. * Define the current field's schema for DB table storage 
  112. * @param array $options 
  113. * @return array 
  114. * @since 2.0 
  115. */ 
  116. public function schema ( $options = null ) { 
  117. $schema = false; 
  118.  
  119. return $schema; 
  120.  
  121. /** 
  122. * Change the way the value of the field is displayed with Pods::get 
  123. * @param mixed $value 
  124. * @param string $name 
  125. * @param array $options 
  126. * @param array $pod 
  127. * @param int $id 
  128. * @return mixed|null 
  129. * @since 2.0 
  130. */ 
  131. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  132. return $value; 
  133.  
  134. /** 
  135. * Customize output of the form field 
  136. * @param string $name 
  137. * @param mixed $value 
  138. * @param array $options 
  139. * @param array $pod 
  140. * @param int $id 
  141. * @since 2.0 
  142. */ 
  143. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  144. $options = (array) $options; 
  145. $form_field_type = PodsForm::$field_type; 
  146.  
  147. if ( !is_admin() ) { 
  148. include_once( ABSPATH . '/wp-admin/includes/template.php' ); 
  149.  
  150. if ( is_multisite() ) 
  151. include_once( ABSPATH . '/wp-admin/includes/ms.php' ); 
  152.  
  153. if ( ( ( defined( 'PODS_DISABLE_FILE_UPLOAD' ) && true === PODS_DISABLE_FILE_UPLOAD ) 
  154. || ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && true === PODS_UPLOAD_REQUIRE_LOGIN && !is_user_logged_in() ) 
  155. || ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && !is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && ( !is_user_logged_in() || !current_user_can( PODS_UPLOAD_REQUIRE_LOGIN ) ) ) ) 
  156. && ( ( defined( 'PODS_DISABLE_FILE_BROWSER' ) && true === PODS_DISABLE_FILE_BROWSER ) 
  157. || ( defined( 'PODS_FILES_REQUIRE_LOGIN' ) && is_bool( PODS_FILES_REQUIRE_LOGIN ) && true === PODS_FILES_REQUIRE_LOGIN && !is_user_logged_in() ) 
  158. || ( defined( 'PODS_FILES_REQUIRE_LOGIN' ) && !is_bool( PODS_FILES_REQUIRE_LOGIN ) && ( !is_user_logged_in() || !current_user_can( PODS_FILES_REQUIRE_LOGIN ) ) ) ) 
  159. ) { 
  160. ?> 
  161. <p>You do not have access to upload / browse files. Contact your website admin to resolve.</p> 
  162. <?php 
  163. return; 
  164.  
  165. if ( 'plupload' == pods_v( self::$type . '_uploader', $options ) ) 
  166. $field_type = 'plupload'; 
  167. elseif ( 'attachment' == pods_v( self::$type . '_uploader', $options ) ) { 
  168. if ( !pods_version_check( 'wp', '3.5' ) || !is_admin() ) // @todo test frontend media modal 
  169. $field_type = 'attachment'; 
  170. else 
  171. $field_type = 'media'; 
  172. else { 
  173. // Support custom File Uploader integration 
  174. do_action( 'pods_form_ui_field_avatar_uploader_' . pods_v( self::$type . '_uploader', $options ), $name, $value, $options, $pod, $id ); 
  175. do_action( 'pods_form_ui_field_avatar_uploader', pods_v( self::$type . '_uploader', $options ), $name, $value, $options, $pod, $id ); 
  176. return; 
  177.  
  178. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  179.  
  180. /** 
  181. * Build regex necessary for JS validation 
  182. * @param mixed $value 
  183. * @param string $name 
  184. * @param array $options 
  185. * @param string $pod 
  186. * @param int $id 
  187. * @return bool 
  188. * @since 2.0 
  189. */ 
  190. public function regex ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  191. return false; 
  192.  
  193. /** 
  194. * Validate a value before it's saved 
  195. * @param mixed $value 
  196. * @param string $name 
  197. * @param array $options 
  198. * @param array $fields 
  199. * @param array $pod 
  200. * @param int $id 
  201. * @param null $params 
  202. * @return bool 
  203. * @since 2.0 
  204. */ 
  205. public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { 
  206. // check file size 
  207. // check file extensions 
  208. return true; 
  209.  
  210. /** 
  211. * Change the value or perform actions after validation but before saving to the DB 
  212. * @param mixed $value 
  213. * @param int $id 
  214. * @param string $name 
  215. * @param array $options 
  216. * @param array $fields 
  217. * @param array $pod 
  218. * @param object $params 
  219. * @return mixed 
  220. * @since 2.0 
  221. */ 
  222. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  223. return $value; 
  224.  
  225. /** 
  226. * Customize the Pods UI manage table column output 
  227. * @param int $id 
  228. * @param mixed $value 
  229. * @param string $name 
  230. * @param array $options 
  231. * @param array $fields 
  232. * @param array $pod 
  233. * @return mixed|void 
  234. * @since 2.0 
  235. */ 
  236. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  237. if ( empty( $value ) ) 
  238. return; 
  239.  
  240. if ( !empty( $value ) && isset( $value[ 'ID' ] ) ) 
  241. $value = array( $value ); 
  242.  
  243. foreach ( $value as $v ) { 
  244. echo pods_image( $v, 'thumbnail' ); 
  245.  
  246. /** 
  247. * Take over the avatar served from WordPress 
  248. * @param string $avatar Default Avatar Image output from WordPress 
  249. * @param int|string|object $id_or_email A user ID, email address, or comment object 
  250. * @param int $size Size of the avatar image 
  251. * @param string $default URL to a default image to use if no avatar is available 
  252. * @param string $alt Alternate text to use in image tag. Defaults to blank 
  253. * @return string <img> tag for the user's avatar 
  254. */ 
  255. public function get_avatar ( $avatar, $id_or_email, $size, $default = '', $alt ='' ) { 
  256. // Don't replace for the Avatars section of the Discussion settings page 
  257. if ( is_admin() ) { 
  258. $current_screen = get_current_screen(); 
  259. if ( ! is_null( $current_screen ) && 'options-discussion' == $current_screen->id && 32 == $size ) { 
  260. return $avatar; 
  261.  
  262. $_user_ID = 0; 
  263.  
  264. if ( is_numeric( $id_or_email ) && 0 < $id_or_email ) 
  265. $_user_ID = (int) $id_or_email; 
  266. elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && 0 < $id_or_email->user_id ) 
  267. $_user_ID = (int) $id_or_email->user_id; 
  268. elseif ( is_object( $id_or_email ) && isset( $id_or_email->ID ) && isset( $id_or_email->user_login ) && 0 < $id_or_email->ID ) 
  269. $_user_ID = (int) $id_or_email->ID; 
  270. elseif ( !is_object( $id_or_email ) && false !== strpos( $id_or_email, '@' ) ) { 
  271. $_user = get_user_by( 'email', $id_or_email ); 
  272.  
  273. if ( !empty( $_user ) ) 
  274. $_user_ID = (int) $_user->ID; 
  275.  
  276. // Include PodsMeta if not already included 
  277. pods_meta(); 
  278.  
  279. if ( 0 < $_user_ID && !empty( PodsMeta::$user ) ) { 
  280. $avatar_cached = pods_cache_get( $_user_ID . '-' . $size, 'pods_avatars' ); 
  281.  
  282. if ( !empty( $avatar_cached ) ) 
  283. $avatar = $avatar_cached; 
  284. else { 
  285. $avatar_field = pods_transient_get( 'pods_avatar_field' ); 
  286.  
  287. $user = current( PodsMeta::$user ); 
  288.  
  289. if ( empty( $avatar_field ) ) { 
  290. foreach ( $user[ 'fields' ] as $field ) { 
  291. if ( 'avatar' == $field[ 'type' ] ) { 
  292. $avatar_field = $field[ 'name' ]; 
  293.  
  294. pods_transient_set( 'pods_avatar_field', $avatar_field ); 
  295.  
  296. break; 
  297. elseif ( !isset( $user[ 'fields' ][ $avatar_field ] ) ) 
  298. $avatar_field = false; 
  299.  
  300. if ( !empty( $avatar_field ) ) { 
  301. $user_avatar = get_user_meta( $_user_ID, $avatar_field . '.ID', true ); 
  302.  
  303. if ( !empty( $user_avatar ) ) { 
  304. $attributes = array( 
  305. 'alt' => '',  
  306. 'class' => 'avatar avatar-' . $size . ' photo' 
  307. ); 
  308.  
  309. if ( !empty( $alt ) ) 
  310. $attributes[ 'alt' ] = $alt; 
  311.  
  312. $user_avatar = pods_image( $user_avatar, array( $size, $size ), 0, $attributes ); 
  313.  
  314. if ( !empty( $user_avatar ) ) { 
  315. $avatar = $user_avatar; 
  316.  
  317. pods_cache_set( $_user_ID . '-' . $size, $avatar, 'pods_avatars' ); 
  318.  
  319. return $avatar;