hooks(); } private function hooks() { add_action( 'admin_head', array( $this, 'admin_init_later' ) ); add_action( 'wp_ajax_custom-post-avatar-upload-action', array( $this, 'upload_ajax' ) ); add_action( 'show_user_profile', array( $this, 'profile_fields' ), 10, 1 ); add_action( 'edit_user_profile', array( $this, 'profile_fields' ), 10, 1 ); add_action( 'personal_options_update', array( $this, 'save_custom_post_avatar_profile_fields' ), 10, 1 ); add_action( 'edit_user_profile_update', array( $this, 'save_custom_post_avatar_profile_fields' ), 10, 1 ); /* --- */ add_filter( 'posts_where', array( $this, 'exclude_from_media_library' ) ); } public function admin_init_later() { $screen = get_current_screen(); $this->screen = $screen->id; if ( 'user-edit' === $this->screen ) { if ( isset( $_GET['user_id'] ) ) { $this->custom_post_avatar_user_id = (int) $_GET['user_id']; } } else { $this->custom_post_avatar_user_id = get_current_user_id(); } $this->user_path = $this->get_user_path( $this->custom_post_avatar_user_id ); $this->user_path_url = $this->get_user_path_url( $this->custom_post_avatar_user_id ); if ( 'profile' === $this->screen ) { $this->admin_head(); } } public function get_user_dir( $id ) { if ( ! $id || ! get_userdata( $id ) ) { return false; } $plugin_path = '/avatars'; $user_hash = md5( get_userdata( $id )->user_email ); $user_dir = $plugin_path . '/' . $user_hash; return $user_dir; } private function get_user_path( $id ) { $user_dir = $this->get_user_dir( $id ); if ( ! $user_dir ) { return false; } $user_path = wp_get_upload_dir()['basedir'] . $user_dir; if ( ! file_exists( $user_path ) ) { wp_mkdir_p( $user_path ); } return $user_path; } private function get_user_path_url( $id ) { $user_path = $this->get_user_path( $id ); if ( ! $user_path ) { return false; } $user_path_url = str_replace( wp_get_upload_dir()['basedir'], wp_get_upload_dir()['baseurl'], $user_path ); return $user_path_url; } public function upload_dir_filter( $upload_dir = array() ) { $curr_user_id = get_current_user_id(); $user_dir = $this->get_user_dir( $curr_user_id ); if ( ! $user_dir ) { return $upload_dir; } return apply_filters( 'custom_post_avatar_upload_dir', array( 'path' => $upload_dir['basedir'] . $user_dir, 'url' => $upload_dir['baseurl'] . $user_dir, 'subdir' => $user_dir, 'basedir' => $upload_dir['basedir'] . $user_dir, 'baseurl' => $upload_dir['baseurl'] . $user_dir, 'error' => false, ), $upload_dir ); } public function upload_filename_filter( $filename, $filename_raw ) { $info = pathinfo( $filename ); $extension = empty( $info['extension'] ) ? '' : '.' . $info['extension']; $new_filename = time() . $extension; if ( $new_filename !== $filename_raw ) { $new_filename = sanitize_file_name( $new_filename ); } return $new_filename; } public function admin_head() { $uploader_options = array( 'runtimes' => 'html5,silverlight,flash,html4', 'browse_button' => 'custom-post-avatar-uploader-button', 'container' => 'custom-post-avatar-uploader', 'drop_element' => 'custom-post-avatar-uploader-inner', 'file_data_name' => 'async-upload', 'multiple_queues' => true, 'max_file_size' => wp_max_upload_size() . 'b', 'url' => admin_url( 'admin-ajax.php' ), 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ), 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ), 'filters' => array( array( 'title' => __( 'Allowed Files', 'custom-post-avatar' ), 'extensions' => '*', ), ), 'multipart' => true, 'urlstream_upload' => true, 'multi_selection' => true, 'multipart_params' => array( '_ajax_nonce' => wp_create_nonce( __FILE__ ), 'action' => 'custom-post-avatar-upload-action', ), ); ?> true, 'action' => 'custom-post-avatar-upload-action', ) ); if ( is_wp_error( $id ) ) { $response['status'] = 'error'; $response['error'] = $id->get_error_messages(); } else { $src = esc_url( get_post( $id )->guid ); $response['status'] = 'success'; $response['attachment'] = array(); $response['attachment']['id'] = $id; $response['attachment']['src'] = $src; } remove_filter( 'upload_dir', $upload_dir_filter, 10 ); remove_filter( 'sanitize_file_name', $upload_filename_filter, 10, 2 ); } echo wp_json_encode( $response ); exit; } public function profile_fields( $user ) { wp_nonce_field( basename( __FILE__ ), '_custom_post_avatar_nonce' ); ?>
get_all_avatars_radio(),
array(
'input' => array(
'type' => array(),
'name' => array(),
'value' => array(),
'class' => array(),
'style' => array(),
'checked' => array(),
),
'label' => array(),
'img' => array(
'src' => array(),
'class' => array(),
'alt' => array(),
),
)
);
?>
id ) : ?>
|