'avatar_3d', 'fields' => 'ids', ) ); foreach ( $users as $user_id ) : $avatar_3d_user_profile->avatar_delete( $user_id ); endforeach; delete_option( 'avatar_3d_user_profile' ); } class avatar_3d_user_profile { private $user_id_being_edited, $avatar_upload_error, $remove_nonce, $avatar_ratings; public $options; public function __construct() { $this->options = (array) get_option( 'avatar_3d_user_profile' ); $this->avatar_ratings = array( 'G' => __( 'G — Suitable for all audiences', 'avatar-3d-user-profile' ), 'PG' => __( 'PG — Possibly offensive, usually for audiences 13 and above', 'avatar-3d-user-profile' ), 'R' => __( 'R — Intended for adult audiences above 17', 'avatar-3d-user-profile' ), 'X' => __( 'X — Even more mature than above', 'avatar-3d-user-profile' ), ); $this->add_hooks(); } public function add_hooks() { add_filter( 'pre_get_avatar_data', array( $this, 'get_avatar_data' ), 10, 2 ); add_action( 'admin_init', array( $this, 'admin_init' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) ); add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) ); // enrican 271221 todo // add_action( 'admin_notices', array( $this, 'edit_user_profile' ) ); // enrican 271221 add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) ); add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) ); add_action( 'admin_action_remove-avatar-3d', array( $this, 'action_remove_avatar_3d' ) ); add_action( 'wp_ajax_assign_avatar_3d_media', array( $this, 'ajax_assign_avatar_3d_media' ) ); add_action( 'wp_ajax_remove_avatar_3d', array( $this, 'action_remove_avatar_3d' ) ); add_action( 'user_edit_form_tag', array( $this, 'user_edit_form_tag' ) ); add_action( 'rest_api_init', array( $this, 'register_rest_fields' ) ); } public function get_avatar( $avatar = '', $id_or_email = '', $size = 96, $default = '', $alt = '', $args = array() ) { return apply_filters( 'avatar_3d', get_avatar( $id_or_email, $size, $default, $alt, $args ) ); } public function get_avatar_data( $args, $id_or_email ) { if ( ! empty( $args['force_default'] ) ) { return $args; } $avatar_3d_url = $this->get_avatar_3d_url( $id_or_email, $args['size'] ); if ( $avatar_3d_url ) { $args['url'] = $avatar_3d_url; } if ( ! $avatar_3d_url && ! empty( $this->options['only'] ) ) { $args['url'] = $this->get_default_avatar_url( $args['size'] ); } if ( ! empty( $args['url'] ) ) { $args['found_avatar'] = true; } return $args; } public function get_avatar_3d_url( $id_or_email, $size ) { if ( is_numeric( $id_or_email ) ) { $user_id = (int) $id_or_email; } elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) ) { $user_id = $user->ID; } elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) { $user_id = (int) $id_or_email->user_id; } elseif ( $id_or_email instanceof WP_Post && ! empty( $id_or_email->post_author ) ) { $user_id = (int) $id_or_email->post_author; } if ( empty( $user_id ) ) { return ''; } $local_avatars = get_user_meta( $user_id, 'avatar_3d', true ); if ( empty( $local_avatars['full'] ) ) { return ''; } $avatar_rating = get_user_meta( $user_id, 'avatar_3d_rating', true ); if ( ! empty( $avatar_rating ) && 'G' !== $avatar_rating && ( $site_rating = get_option( 'avatar_rating' ) ) ) { $ratings = array_keys( $this->avatar_ratings ); $site_rating_weight = array_search( $site_rating, $ratings ); $avatar_rating_weight = array_search( $avatar_rating, $ratings ); if ( false !== $avatar_rating_weight && $avatar_rating_weight > $site_rating_weight ) { return ''; } } if ( ! empty( $local_avatars['media_id'] ) ) { if ( ! $avatar_full_path = get_attached_file( $local_avatars['media_id'] ) ) { return ''; } } $size = (int) $size; if ( ! array_key_exists( $size, $local_avatars ) ) { $local_avatars[ $size ] = $local_avatars['full']; if ( apply_filters( 'avatar_3d_user_profile_dynamic_resize', true ) ) : $upload_path = wp_upload_dir(); if ( ! isset( $avatar_full_path ) ) { $avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] ); } $editor = wp_get_image_editor( $avatar_full_path ); if ( ! is_wp_error( $editor ) ) { $resized = $editor->resize( $size, $size, true ); if ( ! is_wp_error( $resized ) ) { $dest_file = $editor->generate_filename(); $saved = $editor->save( $dest_file ); if ( ! is_wp_error( $saved ) ) { $local_avatars[ $size ] = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $dest_file ); } } } // save avatar update_user_meta( $user_id, 'avatar_3d', $local_avatars ); endif; } if ( 'http' !== substr( $local_avatars[ $size ], 0, 4 ) ) { $local_avatars[ $size ] = home_url( $local_avatars[ $size ] ); } return esc_url( $local_avatars[ $size ] ); } public function get_default_avatar_url( $size ) { if ( empty( $default ) ) { $avatar_default = get_option( 'avatar_default' ); if ( empty( $avatar_default ) ) { $default = 'mystery'; } else { $default = $avatar_default; } } $host = is_ssl() ? 'https://secure.gravatar.com' : 'http://0.gravatar.com'; if ( 'mystery' === $default ) { $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') } elseif ( 'blank' === $default ) { $default = includes_url( 'images/blank.gif' ); } elseif ( 'gravatar_default' === $default ) { $default = "$host/avatar/?s={$size}"; } else { $default = "$host/avatar/?d=$default&s={$size}"; } return $default; } public function admin_init() { if ( $old_ops = get_option( 'avatar_3d_user_profile_caps' ) ) { if ( ! empty( $old_ops['avatar_3d_user_profile_caps'] ) ) { update_option( 'avatar_3d_user_profile', array( 'caps' => 1 ) ); } delete_option( 'avatar_3d_caps' ); } register_setting( 'discussion', 'avatar_3d_user_profile', array( $this, 'sanitize_options' ) ); add_settings_field( 'avatar-3d-user-profile-only', __( 'Local Avatars Only', 'avatar-3d-user-profile' ), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars', array( 'key' => 'only', 'desc' => __( 'Only allow local avatars (still uses Gravatar for default avatars)', 'avatar-3d-user-profile' ), ) ); add_settings_field( 'avatar-3d-user-profile-caps', __( 'Local Upload Permissions', 'avatar-3d-user-profile' ), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars', array( 'key' => 'caps', 'desc' => __( 'Only allow users with file upload capabilities to upload local avatars (Authors and above)', 'avatar-3d-user-profile' ), ) ); } public function admin_enqueue_scripts( $hook_suffix ) { if ( 'profile.php' !== $hook_suffix && 'user-edit.php' !== $hook_suffix ) { return; } if ( current_user_can( 'upload_files' ) ) { wp_enqueue_media(); } $user_id = ( 'profile.php' === $hook_suffix ) ? get_current_user_id() : (int) $_GET['user_id']; $this->remove_nonce = wp_create_nonce( 'remove_avatar_3d_nonce' ); // enrican 261221 todo $dev = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.dev' : ''; wp_enqueue_script( 'avatar-3d-user-profile', AV3DUP_PLUGIN_DIR . 'js/avatar-3d-user-profile' . $dev . '.js', array( 'jquery' ), false, true ); // enrican 281221 enqueque threejs wp_enqueue_script( 'threejs', AV3DUP_PLUGIN_DIR . 'js/three.min.js', null, $this->version, false ); wp_enqueue_script( 'gltf-loader', AV3DUP_PLUGIN_DIR . 'js/GLTFLoader.js', array( 'threejs' ), $this->version, false ); wp_enqueue_script( 'orbitcontrols', AV3DUP_PLUGIN_DIR . 'js/OrbitControls.js', array( 'threejs' ), $this->version, false ); // enrican 281221 jquery wp_register_script( 'jquery-script', AV3DUP_PLUGIN_DIR . 'js/jquery-script.js', array('jquery')); wp_enqueue_script( 'jquery-script' ); // enrican 291221 css wp_register_style( 'css-script', AV3DUP_PLUGIN_DIR . 'css/style.css' ); wp_enqueue_style('css-script'); wp_enqueue_script( 'default-avatar', AV3DUP_PLUGIN_DIR . 'js/default-avatar.js', null, $this->version, true ); // enrican 291221 pass var php to js wp_localize_script( 'default-avatar', 'PHPTOJS', array( 'PLUGINDIR' => AV3DUP_PLUGIN_DIR, 'GRAVATARIMG' => get_avatar_url(get_current_user_id()) ) ); wp_localize_script( 'avatar-3d-user-profile', 'i10n_Avatar3dUserProfile', array( 'user_id' => $user_id, 'insertMediaTitle' => __( 'Choose an Avatar', 'avatar-3d-user-profile' ), 'insertIntoPost' => __( 'Set as avatar', 'avatar-3d-user-profile' ), 'deleteNonce' => $this->remove_nonce, 'mediaNonce' => wp_create_nonce( 'assign_avatar_3d_nonce' ), ) ); } public function sanitize_options( $input ) { $new_input['caps'] = empty( $input['caps'] ) ? 0 : 1; $new_input['only'] = empty( $input['only'] ) ? 0 : 1; return $new_input; } public function avatar_settings_field( $args ) { $args = wp_parse_args( $args, array( 'key' => '', 'desc' => '', ) ); if ( empty( $this->options[ $args['key'] ] ) ) { $this->options[ $args['key'] ] = 0; } echo ' '; } // output 3d avatar public function edit_user_profile( $profileuser ) { // enrican 261221 // echo do_shortcode('[3d_viewer src="https://3davatar.info/wp-content/uploads/2021/12/047.glb"]'); // echo get_avatar_url(get_current_user_id()); $currenturl = add_query_arg( NULL, NULL ); ?>
| ID ); remove_filter( 'pre_option_avatar_rating', '__return_null' ); ?> |
options['caps'] );
}
if ( $upload_rights ) {
do_action( 'avatar_3d_notices' );
wp_nonce_field( 'avatar_3d_nonce', '_avatar_3d_nonce', false );
$remove_url = add_query_arg(
array(
'action' => 'remove-avatar-3d',
'user_id' => $profileuser->ID,
'_wpnonce' => $this->remove_nonce,
)
);
?>
avatar_3d ) ? ' style="display:none;"' : ''; ?> > avatar_3d ) ) { echo '' . esc_html__( 'No local avatar is set. Set up your avatar at Gravatar.com.', 'avatar-3d-user-profile' ) . ''; } else { echo '' . esc_html__( 'You do not have media management permissions. To change your local avatar, contact the blog administrator.', 'avatar-3d-user-profile' ) . ''; } } ?> |
|