Created
January 8, 2017 09:09
-
-
Save poripa/cb703ab4904a433781da6527c37e0bd1 to your computer and use it in GitHub Desktop.
Ultimate Member Gallery: Hide the Gallery Tab on the profile page of specific roles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: User Gallery for Ultimate Member | |
Plugin URI: https://suiteplugins.com/ | |
Description: Allow your user to upload photos from their Ultimate Member profile | |
Version: 1.0.2 | |
Author: SuitePlugins | |
Author URI: https://suiteplugins.com/ | |
*/ | |
define('um_gallery_url',plugin_dir_url(__FILE__ )); | |
define('um_gallery_path',plugin_dir_path(__FILE__ )); | |
define('um_gallery_plugin', plugin_basename( __FILE__ ) ); | |
define( 'UM_GALLERY_STORE_URL', 'https://suiteplugins.com' ); | |
define( 'UM_GALLERY_ITEM_NAME', 'User Gallery for UltimateMembers' ); | |
if(!class_exists('SP_Gallery_UM')): | |
class SP_Gallery_UM{ | |
public $gallery; | |
public $add_photos; | |
public function __construct(){ | |
add_action( 'plugins_loaded', array($this, 'load_plugin_textdomain') ); | |
$this->gallery = $this->get_gallery_slug(); | |
$this->add_photos = $this->get_add_photos_slug(); | |
$this->define_constants(); | |
$this->includes(); | |
add_filter('um_user_profile_tabs', array($this, 'setup_gallery_tabs'), 12, 2); | |
add_action( 'wp_enqueue_scripts', array($this, 'add_scripts') ); | |
add_action( 'um_profile_content_'.$this->gallery, array($this, 'gallery_content_page')); | |
add_action( 'um_profile_content_'.$this->gallery.'_'.$this->gallery, array($this, 'gallery_folder_content_page')); | |
add_action( 'um_profile_content_'.$this->gallery.'_'.$this->add_photos, array($this, 'add_photos_content_page')); | |
//perform upload | |
add_action( 'init', array($this, 'perform_upload')); | |
// | |
add_action('wp_ajax_sp_gallery_um_deleta', array($this, 'delete_item')); | |
//create an action and try to place content below the profile fields | |
add_action('um_profile_content_main', array($this, 'gallery_content_page'), 20); | |
} | |
public function load_plugin_textdomain(){ | |
//taken from sensei-buddypress | |
$locale = apply_filters( 'plugin_locale', get_locale(), 'sp-gallery-um' ); | |
// Allow upgrade safe, site specific language files in /wp-content/languages/ultimate-member-gallery/ | |
load_textdomain( 'sp-gallery-um', WP_LANG_DIR.'/sp-gallery-um-'.$locale.'.mo' ); | |
$plugin_rel_path = apply_filters( 'sp_gallery_um_tranlsation_path', dirname( plugin_basename( __FILE__ ) ) . '/languages' ); | |
load_plugin_textdomain( 'sp-gallery-um', false, $plugin_rel_path ); | |
} | |
public function load_template($tpl){ | |
global $ultimatemember; | |
$file = um_gallery_path . 'templates/' . $tpl . '.php'; | |
$theme_file = get_stylesheet_directory() . '/ultimate-member/templates/' . $tpl . '.php'; | |
if ( file_exists( $theme_file ) ){ | |
$file = $theme_file; | |
} | |
if ( file_exists( $file ) ){ | |
include $file; | |
} | |
} | |
public function define_constants(){ | |
$upload_dir = wp_upload_dir(); | |
define('SP_GALLERY_UM_PATH', ''); | |
define('SP_GALLERY_UM_UPLOAD_PATH', $upload_dir['basedir'].'/um-gallery'); | |
define('SP_GALLERY_UM_UPLOAD_URL', $upload_dir['baseurl'].'/um-gallery'); | |
} | |
public function includes(){ | |
require_once( um_gallery_path. 'includes/gallery-admin.php'); | |
} | |
public function get_gallery_slug(){ | |
return apply_filters('sp_gallery_gallery_slug', 'gallery'); | |
} | |
public function get_add_photos_slug(){ | |
return apply_filters('sp_gallery_add_photos_slug', 'add_photos'); | |
} | |
public function is_owner(){ | |
$user_id = um_profile_id(); | |
$my_ID = get_current_user_id(); | |
if($user_id == $my_ID): | |
return true; | |
else: | |
return false; | |
endif; | |
} | |
public function hasGallery() | |
{ | |
global $ultimatemember; | |
$rolesWithoutGallery = array('parent', 'supporter', 'staff'); | |
return !in_array($ultimatemember->query->get_role_by_userid(um_profile_id()), $rolesWithoutGallery); | |
} | |
public function setup_gallery_tabs($tabs){ | |
if (!$this->hasGallery()) { | |
return $tabs; | |
} | |
$tabs[$this->gallery] = array( | |
'name' => __('Gallery', 'sp-gallery-um'), | |
'icon' => 'um-faicon-camera', | |
'subnav_default'=> 0 | |
); | |
if($this->is_owner()): | |
$tabs[$this->gallery]['subnav'] = array( | |
//$this->gallery => __('Gallery', 'sp-gallery-um'), | |
$this->add_photos => __('Add Photos', 'sp-gallery-um') | |
); | |
endif; | |
return $tabs; | |
} | |
public function add_scripts(){ | |
wp_enqueue_style( | |
'sp-lightbox-style', | |
plugins_url( '/assets/css/lightbox.css' , __FILE__ ) | |
); | |
wp_enqueue_style( | |
'sp-gallery-style', | |
plugins_url( '/assets/css/sp-gallery.css' , __FILE__ ) | |
); | |
wp_enqueue_script( | |
'sp-gallery-dropzone', | |
plugins_url( '/assets/js/dropzone.js' , __FILE__ ), | |
array( 'jquery' ) | |
); | |
wp_enqueue_script( | |
'sp-gallery-lightbox', | |
plugins_url( '/assets/js/lightbox.js' , __FILE__ ), | |
array( 'jquery' ), | |
'1.0.0', true | |
); | |
wp_enqueue_style( | |
'sp-gallery-style-carousel', | |
plugins_url( '/templates/um-gallery/carousel/owl-carousel/owl.carousel.css' , __FILE__ ) | |
); | |
wp_enqueue_style( | |
'sp-gallery-style-theme', | |
plugins_url( '/templates/um-gallery/carousel/owl-carousel/owl.theme.css' , __FILE__ ) | |
); | |
// wp_enqueue_script( | |
// 'sp-gallery-', | |
// plugins_url( '/template/um-gallery/carousel/owl-carousel/owl.carousel.js' , __FILE__ ), | |
// array( 'jquery' ) | |
// ); | |
wp_enqueue_script( | |
'sp-gallery-carousel', | |
plugins_url( '/templates/um-gallery/carousel/owl-carousel/owl.carousel.min.js' , __FILE__ ), | |
array( 'jquery' ) | |
); | |
/*wp_enqueue_script( | |
'sp-gallery-prettyPhoto', | |
plugins_url( '/assets/js/jquery.prettyPhoto.js' , __FILE__ ), | |
array( 'jquery' ) | |
);*/ | |
} | |
public function get_user_image_src($user_id=0, $name='', $size='thumbnail'){ | |
if(empty($user_id) || empty($name)){ | |
return; | |
} | |
$image = SP_GALLERY_UM_UPLOAD_URL.'/'.$user_id.'/'.$name; | |
if($size == 'thumbnail'){ | |
$filetype = wp_check_filetype($image); | |
$basename = basename($image, '.'.$filetype['ext']); | |
return SP_GALLERY_UM_UPLOAD_URL.'/'.$user_id.'/'.$basename.'-thumbnail.'.$filetype['ext']; | |
} | |
return $image; | |
} | |
public function get_user_image_path($user_id=0, $name='', $size='thumbnail'){ | |
if(empty($user_id) || empty($name)){ | |
return; | |
} | |
$image = SP_GALLERY_UM_UPLOAD_PATH.'/'.$user_id.'/'.$name; | |
if($size == 'thumbnail'){ | |
$filetype = wp_check_filetype($image); | |
$basename = basename($image, '.'.$filetype['ext']); | |
return SP_GALLERY_UM_UPLOAD_PATH.'/'.$user_id.'/'.$basename.'-thumbnail.'.$filetype['ext']; | |
} | |
return $image; | |
} | |
public function gallery_folder_content_page(){ | |
} | |
public function gallery_content_page(){ | |
if(!isset($_GET['subnav'])){ | |
global $ultimatemember; | |
$user_id = um_profile_id(); | |
$images = $this->get_images_by_user_id($user_id); | |
//print_r($ultimatemember); | |
if(um_get_option('um_gallery_profile') && (isset($_GET['profiletab']) && 'main'==$_GET['profiletab']) || empty($_GET['profiletab'])){ | |
$layout = um_get_option('um_gallery_type'); | |
switch($layout){ | |
case 'carousel': | |
$this->load_template('um-gallery/content-carousel'); | |
break; | |
case 'grid': | |
$this->load_template('um-gallery/content-grid'); | |
break; | |
case 'slideshow': | |
$this->load_template('um-gallery/content-slideshow'); | |
break; | |
default: | |
$this->load_template('um-gallery/content-grid'); | |
break; | |
} | |
} | |
if(isset($_GET['profiletab']) && 'gallery'==$_GET['profiletab']){ | |
$layout = um_get_option('um_main_gallery_type'); | |
switch($layout){ | |
case 'carousel': | |
$this->load_template('um-gallery/content-carousel'); | |
break; | |
case 'grid': | |
$this->load_template('um-gallery/content-grid'); | |
break; | |
case 'slideshow': | |
$this->load_template('um-gallery/content-slideshow'); | |
break; | |
default: | |
$this->load_template('um-gallery/content-grid'); | |
break; | |
} | |
} | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
lightbox.option({ | |
'resizeDuration': 200, | |
'wrapAround': true | |
}); | |
jQuery(document).on("click",".sp-delete-item", function(event){ | |
event.preventDefault(); | |
var id = jQuery(this).data("id"); | |
var obj = jQuery(this); | |
$.ajax({ | |
method: "POST", | |
url: "<?php echo admin_url('admin-ajax.php'); ?>", | |
data: { action: "sp_gallery_um_deleta", id: id }, | |
success: function(result){ | |
obj.closest('.sp-gallery-item').slideUp().remove(); | |
} | |
}) | |
}); | |
//jQuery("a[rel^='prettyPhoto']").prettyPhoto(); | |
}); | |
</script> | |
<?php | |
} | |
} | |
public function delete_item(){ | |
$id = (int)$_POST['id']; | |
global $wpdb; | |
$file = $wpdb->get_row($wpdb->prepare("SELECT file_name, user_id FROM {$wpdb->prefix}um_gallery WHERE id ='%d'", $id)); | |
$wpdb->delete( $wpdb->prefix.'um_gallery', array( 'id' => $id ) ); | |
$file_url = $this->get_user_image_path($file->user_id, $file->file_name); | |
unlink ( $file_url ); | |
$file_url = $this->get_user_image_path($file->user_id, $file->file_name, 'none'); | |
unlink ( $file_url ); | |
echo 1; | |
exit; | |
} | |
public function add_photos_content_page(){ | |
?> | |
<style type="text/css"> | |
.gallery .um-gallery-message-notice { | |
display: none; | |
color: #fff !important; | |
margin-bottom: 12px !important; | |
font-weight: bold !important; | |
} | |
</style> | |
<p class="um-gallery-message um-gallery-message-notice um-notice success" style=""></p> | |
<form action="<?php echo site_url('?ug_upload'); ?>" class="dropzone" id="uGalleryUpload"> | |
<div id="dropzone"> | |
<div class="dz-message needsclick"> | |
<?php _e('Click to upload a photo.', 'sp-gallery-um'); ?> | |
<br /> | |
</div> | |
<input type="hidden" name="ug_upload" value="1" /> | |
<div class="um-clear"></div> | |
</div> | |
<input type="hidden" name="ug_upload" value="1" /> | |
</form> | |
<div class="um-col-alt" style="margin-top:12px;"> | |
<div class="um-left um-half"> | |
<input type="submit" id="spg_submit_photos" class="um-button" value="<?php _e('Submit','sp-gallery-um'); ?>" /> | |
</div> | |
<div class="um-right um-half"><a href="<?php echo um_get_gallery_link(); ?>" class="um-button um-alt"> | |
<?php _e('Go to Gallery', 'sp-gallery-um'); ?> | |
</a></div> | |
<div class="um-clear"></div> | |
</div> | |
<div class="um-clear"></div> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
Dropzone.autoDiscover = false; | |
var myDropzone = new Dropzone("#dropzone", { | |
url: "<?php echo site_url('?ug_upload=1'); ?>", | |
autoProcessQueue: false, | |
parallelUploads: 56, | |
queuecomplete: function(){ | |
jQuery('.um-gallery-message').html('<?php _e('Upload complete'); ?>').slideDown(); | |
} | |
}); | |
$('#spg_submit_photos').click(function(event){ | |
event.preventDefault(); | |
jQuery('.um-gallery-message').html('').slideUp(); | |
myDropzone.processQueue(); | |
}); | |
<?php /*?>$(".dropzone").dropzone({ | |
url: "<?php echo site_url('?ug_upload'); ?>", | |
acceptedFiles: "image/*" | |
});<?php */?> | |
}); | |
</script> | |
<?php | |
} | |
public function get_images_by_user_id($user_id = 0){ | |
global $wpdb; | |
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}um_gallery WHERE user_id=%d", $user_id)); | |
return $results; | |
} | |
public function perform_upload(){ | |
if(isset($_POST) && isset($_REQUEST['ug_upload'])): | |
if ( ! function_exists( 'wp_handle_upload' ) ) { | |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
} | |
if (!empty($_FILES)) : | |
$user_id = get_current_user_id(); | |
if ( is_user_logged_in() ) { | |
$path = SP_GALLERY_UM_UPLOAD_PATH.'/'.$user_id.'/'; | |
if(!file_exists($path)){ | |
wp_mkdir_p($path); | |
} | |
$tmp_file = $_FILES['file']['tmp_name']; | |
$name = sanitize_text_field($_FILES['file']['name']); | |
$filename = wp_unique_filename( $path, $name, false ); | |
$targetFile = $path. $filename; //5 | |
if(move_uploaded_file($tmp_file,$targetFile)){ | |
$image = wp_get_image_editor( $targetFile ); | |
$filetype = wp_check_filetype($targetFile); | |
$basename = basename($targetFile, '.'.$filetype['ext']); | |
if ( ! is_wp_error( $image ) ) { | |
$image->resize( 150, 150, true ); | |
$image->save( $path.$basename.'-thumbnail.'.$filetype['ext'] ); | |
} | |
global $wpdb; | |
$wpdb->insert( | |
$wpdb->prefix.'um_gallery', | |
array( | |
'file_name' => $filename, | |
'upload_date' => date('Y-m-d H:i:s'), | |
'user_id' => $user_id, | |
'status' => 1 | |
), | |
array( | |
'%s', | |
'%s', | |
'%d', | |
'%d' | |
) | |
); | |
} | |
} | |
endif; | |
exit; | |
endif; | |
} | |
} | |
endif; | |
$sp_gallery = new SP_Gallery_UM(); | |
function sp_load_gallery_um(){ | |
} | |
add_action('plugins_loaded', 'sp_load_gallery_um'); | |
function um_get_gallery_link($id=''){ | |
global $ultimatemember; | |
$slug = 'gallery'; | |
$url = um_user_profile_url(); | |
$url = remove_query_arg('profiletab', $url); | |
$url = remove_query_arg('subnav', $url); | |
$url = add_query_arg( 'profiletab', $slug, $url ); | |
//$url = add_query_arg( 'view', 'edit_doc', $url ); | |
if($id){ | |
$url = add_query_arg( 'view', $id, $url ); | |
} | |
return $url; | |
} | |
function _sp_gallery_activate() { | |
$upload_dir = wp_upload_dir(); | |
$path = $upload_dir['basedir'].'/um-gallery/'; | |
if(!file_exists($path)){ | |
wp_mkdir_p($path); | |
} | |
global $wpdb; | |
$version = get_option( 'sp_gallery_um_db', '1.0.0' ); | |
$charset_collate = !empty( $wpdb->charset ) ? "DEFAULT CHARACTER SET $wpdb->charset" : ''; | |
$table_prefix = $wpdb->prefix; | |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
$sql = "CREATE TABLE IF NOT EXISTS {$table_prefix}um_gallery ( | |
id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
user_id bigint(20) NOT NULL, | |
folder_id bigint(20) NOT NULL, | |
file_name varchar(255) NOT NULL, | |
caption varchar(255) NOT NULL, | |
description varchar(255) NOT NULL, | |
status tinyint(2) NOT NULL, | |
upload_date TIMESTAMP | |
) {$charset_collate};"; | |
dbDelta( $sql ); | |
$sql2 = "CREATE TABLE IF NOT EXISTS {$table_prefix}um_gallery_folder ( | |
id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
user_id bigint(20) NOT NULL, | |
folder_name varchar(255) NOT NULL, | |
caption varchar(255) NOT NULL, | |
description varchar(255) NOT NULL, | |
status tinyint(2) NOT NULL, | |
creation_date TIMESTAMP | |
) {$charset_collate};"; | |
dbDelta( $sql ); | |
update_option( 'sp_gallery_um_db', '1.0.0' ); | |
} | |
register_activation_hook( __FILE__, '_sp_gallery_activate' ); |
Does this still work?
It's been a while since I last did something with it. Also I believe Ultimate Members have had version upgrades so you should test it out yourself first to see if it still works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this still work?