PageRenderTime 75ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/pods/classes/fields/avatar.php

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