Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BWS Buddy Cover Photo WP Plugin
<?php
/**
* Cover Photo Attachment class
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
if ( class_exists( 'BP_Attachment') ) :
class Cover_Photo_Attachment extends BP_Attachment {
public function __construct() {
// Set the Custom Attachment parameters
parent::__construct( array(
// The upload action used when uploading a file, $_POST['action'] must be set to this parameter
'action' => 'custom_upload',
// The name attribute used in the file input. (required)
'file_input' => 'custom_file',
// Max upload filesize, defaults to wp_max_upload_size()
'original_max_filesize' => 512000,
// List of Allowed extensions (optionnal), defaults to WordPress allowed mime types
'allowed_mime_types' => array( 'png', 'jpg' ),
// Custom errors to use in the validate function (custom errors start at index 9, previous indexes are used by WordPress)
/* 'upload_error_strings' => array(
9 => __( 'Your file name must contain the term &#8220;custom&#8221;', 'custom-domain' ),
),*/
// base upload dir for your custom component, eg: /wp-content/uploads/custom
'base_dir' => 'cover-photo',
) );
}
/**
* Optionnal, use it if you need to add some custom validation rules
* in our example: the file must contain "custom" in its name
*/
public function validate_upload( $file = array() ) {
/**
* You can use the BP_Attachment->validate() function to check
* for your max upload size
*/
$file = parent::validate_upload( $file );
// Bail if already an error
if ( ! empty( $file['error'] ) ) {
return $file;
}
// Add an error
/* if ( false === strpos( $file['name'], 'custom' ) ) {
$file['error'] = 9;
}*/
return $file;
}
/**
* Optionnal, use it if you need to do some custom actions in the upload directory
* eg: add a subdirectory for each user ids
*/
public function upload_dir_filter() {
/**
* You can use the BP_Attachment->upload_dir_filter() function to get
* your custom upload dir data
*
* if you defined the $base_dir parameter in the construct method
*
* you will get: array(
* 'path' => 'site_path/wp-content/uploads/custom',
* 'url' => 'site_url/wp-content/uploads/custom',
* 'subdir' => false,
* 'basedir' => 'site_path/wp-content/uploads/custom',
* 'baseurl' => 'site_url/wp-content/uploads/custom',
* 'error' => false
* );
*/
$upload_dir_data = parent::upload_dir_filter();
if ( ! is_user_logged_in() ) {
return $upload_dir_data;
}
/**
* Or you can directly dynamically set your custom upload dir data
* eg: /wp-content/uploads/custom/1
*/
return array(
'path' => $this->upload_path . '/' . bp_loggedin_user_id(),
'url' => $this->url . '/' . bp_loggedin_user_id(),
'subdir' => '/' . bp_loggedin_user_id(),
'basedir' => $this->upload_path . '/' . bp_loggedin_user_id(),
'baseurl' => $this->url . '/' . bp_loggedin_user_id(),
'error' => false
);
}
public function create_dir() {
// Create the dir using the BP_Attachment->create_dir() method
$created = parent::create_dir();
// if directory was created, create the .htaccess file
if ( $created && ! file_exists( $this->upload_path .'/.htaccess' ) ) {
// Define the rule to protect uploads dir in Apache servers.
$rules = array( 'Order Allow,Deny','Deny from all' );
// make sure to load the file where the insert_with_markers() function is located
require_once( ABSPATH . '/wp-admin/includes/misc.php' );
// create the .htaccess file
insert_with_markers( $this->upload_path .'/.htaccess', 'Custom Attachments', $rules );
}
}
}
endif;
/**
* Assuming you have defined your attachment class and a function to
* intercept the submitted form.
*/
function custom_attachment_handle_upload() {
// Let's get ready to upload a new custom attachment
$custom_attachment = new Cover_Photo_Attachment();
/**
* Everything is in place to upload the file
* <a href="https://buddypress.org/members/see/" rel="nofollow">@see</a> Custom_Attachment->__construct()
*
* eg:
* - $action 'custom_upload',
* - $file_input 'custom_file'
* - $base_dir '/wp-content/uploads/custom'
*/
$result = $custom_attachment->upload( $_FILES );
// Define a custom redirect inside a BuddyPress page
$redirect = trailingslashit( bp_loggedin_user_domain() );
/**
* If there's an error during the upload process
* $result will be an array containing the error message
*/
if ( ! empty( $result['error'] ) ) {
// Add a feedback message containing the upload error
bp_core_add_message( $result['error'], 'error' );
// Safely redirect the user
bp_core_redirect( $redirect );
/**
* If the file was successfully uploaded
* $result will be an array containing the path to the file,
* its url and its mime type.
*
* array {
* $file Absolute path to the file
* $url Absolute url to the file
* $type the file mime type
* }
*/
} else {
// Add a feedback containing the success message
bp_core_add_message( __( 'Bingo! file successfully uploaded.', 'custom-domain' ) );
/**
* In our example, the result could be:
* array {
* 'file' => ABSPATH . 'wp-content/uploads/custom/custom_image.png',
* 'url' => 'http://site.url/wp-content/uploads/custom/custom_image.png',
* 'type' = 'image/png',
* }
*/
// Safely redirect the user
bp_core_redirect( $redirect );
}
}
add_action( 'bp_setup_components', 'custom_attachment_handle_upload', 20 );
//add_action( 'bp_actions', 'custom_attachment_handle_upload', 20 );
//
//custom_attachment_handle_upload();
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
class BP_Cover_Photo_Component extends BP_Component {
/**
* Initial component setup.
*/
public function __construct() {
parent::start(
// Unique component ID
'bpcoverphoto',
// Used by BP when listing components (eg in the Dashboard)
__( 'Display Cover Photo', 'bp-cover-photo' )
);
}
/**
* Set up component data, as required by BP.
*/
public function setup_globals( $args = array() ) {
/* parent::setup_globals( array(
'slug' => 'members/profile', // used for building URLs
) );*/
}
/**
* Set up component navigation, and register display callbacks.
*/
public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
/* $main_nav = array(
'name' => __( 'Events', 'bp-change-cover-photo' ),
'slug' => $this->slug.'/change-cover-photo/',
'position' => 65,
'default_subnav_slug' => 'events/my-events',
'screen_function' => array( $this, 'screen_function' ),
);*/
$main_nav = array('name' => 'Profile');
// BuddyPress needs to have at least one subnav item, even if
// it's redundant
$sub_nav[] = array(
'name' => __( 'Change Cover Photo', 'bp-change-cover-photo' ),
'slug' => 'change-cover-photo',
'parent_slug' => 'profile',
'parent_url' => bp_displayed_user_domain() . 'profile/',
'screen_function' => array( $this, 'screen_function' ),
);
parent::setup_nav( $main_nav, $sub_nav );
//add_action( 'bp_setup_nav', array( $this, 'buddyboss_child_bp_nav_adder'), 100 );
}
/**
* Set up display screen logic.
*
* We are using BP's plugins.php template as a wrapper, which is
* the easiest technique for compatibility with themes.
*/
public function screen_function() {
add_action( 'bp_template_content', array( $this, 'my_posts_content' ) );
bp_core_load_template( 'members/single/plugins' );
}
// public function buddyboss_child_list() {
// add_action( 'bp_template_content', array( $this, 'my_posts_content' ) );
// bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
// }
/**
* Render the content of the events tab.
*/
public function my_posts_content() {
require( dirname( __FILE__ ) . '/change-cover-photo-screen.php' );
}
}
/**
* Bootstrap the component.
*/
function bpcoverphoto_init() {
buddypress()->bpcoverphoto = new BP_Cover_Photo_Component();
}
add_action( 'bp_loaded', 'bpcoverphoto_init' );
<h4><?php _e( 'Change Cover Photo', 'buddypress' ); ?></h4>
<?php if ( !(int)bp_get_option( 'bp-disable-avatar-uploads' ) ) : ?>
<p><?php _e( 'Your cover photo will be used in your profile header.', 'cover-photo' ); ?></p>
<form action="" method="post" id="avatar-upload-form" class="standard-form" enctype="multipart/form-data">
<?php if ( 'upload-image' == bp_get_avatar_admin_step() ) : ?>
<?php // wp_nonce_field( 'bp_avatar_upload' ); ?>
<p><?php _e( 'Click below to select a JPG, GIF or PNG format photo from your computer and then click \'Upload Image\' to proceed.', 'cover-photo' ); ?></p>
<p id="avatar-upload">
<input type="file" name="custom_file" id="custom-file" />
<input type="submit" name="upload" id="upload" value="<?php esc_attr_e( 'Upload Image', 'cover-photo' ); ?>" />
<input type="hidden" name="action" id="custom-action" value="custom_upload" />
</p>
<?php if ( bp_get_user_has_avatar() ) : ?>
<p><?php _e( "If you'd like to delete your current cover photo but not upload a new one, please use the delete cover photo button.", 'cover-photo' ); ?></p>
<p><a class="button edit" href="<?php bp_avatar_delete_link(); ?>" title="<?php esc_attr_e( 'Delete Cover Photo', 'cover-photo' ); ?>"><?php _e( 'Delete My Cover Photo', 'cover-photo' ); ?></a></p>
<?php endif; ?>
<?php endif; ?>
<?php if ( 'crop-image' == bp_get_avatar_admin_step() ) : ?>
<h5><?php _e( 'Crop Your New Cover Photo', 'cover-photo' ); ?></h5>
<img src="<?php bp_avatar_to_crop(); ?>" id="avatar-to-crop" class="avatar" alt="<?php esc_attr_e( 'Cover Photo to crop', 'cover-photo' ); ?>" />
<div id="avatar-crop-pane">
<img src="<?php bp_avatar_to_crop(); ?>" id="avatar-crop-preview" class="avatar" alt="<?php esc_attr_e( 'Cover Photo preview', 'cover-photo' ); ?>" />
</div>
<input type="submit" name="avatar-crop-submit" id="avatar-crop-submit" value="<?php esc_attr_e( 'Crop Image', 'cover-photo' ); ?>" />
<input type="hidden" name="image_src" id="image_src" value="<?php bp_avatar_to_crop_src(); ?>" />
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
<?php // wp_nonce_field( 'bp_avatar_cropstore' ); ?>
<?php endif; ?>
</form>
<?php
/**
* Load the Avatar UI templates
*
* @since BuddyPress (2.3.0)
*/
bp_avatar_get_templates(); ?>
<?php else : ?>
<p><?php _e( 'Your profile photo will be used on your profile and throughout the site. To change your profile photo, please create an account with <a href="http://gravatar.com">Gravatar</a> using the same email address as you used to register with this site.', 'buddypress' ); ?></p>
<?php endif;
<?php
/*
Plugin Name: BWS Buddy Cover Photo
Version: 1.0
Author: Brian
*/
/**
* Load only when BuddyPress is present.
*/
function bpcoverphoto_include() {
// This plugin requires BuddyPress 2.3
if ( ! version_compare( bp_get_version(), '2.3-alpha', '>=' ) ) {
return;
}
require( dirname( __FILE__ ) . '/bp-cover-photo-component-class.php' );
require( dirname( __FILE__ ) . '/bp-cover-photo-attachment-class.php' );
}
add_action( 'bp_include', 'bpcoverphoto_include' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.