Last active
August 1, 2023 22:41
-
-
Save BhargavBhandari90/2565aa3a9b8e3dc268dc56f58dc3782c to your computer and use it in GitHub Desktop.
Custom code for getting profile pic, cover pic etc from Amazon s3 bucket
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 | |
/** | |
* Set selected url structure, selected in rtAmazon settings. | |
* | |
* @since 1.4.0 | |
* | |
* @param string $urls urls in content. | |
* | |
* @return array $newurls/$urls return array of all urls in content. | |
*/ | |
function rtamazon_content_url( $urls ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
// get bucket name | |
$bucket = rtamazon_s3_get_bucket_name(); | |
if ( ! empty( $bucket ) ) { | |
// get bucket url structure | |
$url_structure = rtamazon_s3_get_url_structure( $bucket ); | |
$newurls = array(); | |
// If url structure is default than return the content, no need to search and replace. | |
if ( ! empty( $url_structure ) ) { | |
// get custom domain for bucket url | |
$custom_domain = rtamazon_s3_get_custom_url_domain_name( $bucket ); | |
// get default bucket url | |
$amazon_url = rtamazon_s3_get_default_bucket_url( $bucket ); | |
// get bucket url as per bucket url structure and custom domain | |
$domain = rtamazon_s3_get_bucket_url( $bucket, $url_structure, $custom_domain ); | |
// Wp url pattern | |
$wp_url_pattern = preg_replace( "(^https?://)", "", site_url() ); | |
$search = array(); | |
$replace = array(); | |
foreach ( $urls as $url ) { | |
$aws_url = $url; | |
/* Set url according to selected url structure */ | |
if ( strpos( $url, $bucket . '.s3.amazonaws.com' ) !== false ) { // for Bucket name as subdomain | |
/** | |
* Filters elements for search array. | |
* | |
* @since 1.4.0 | |
* | |
* @param array Array of search elements. | |
*/ | |
$search = apply_filters( 'rtamazon_search_pattern', array( $bucket . '.s3.amazonaws.com' ) ); | |
} elseif ( strpos( $url, 's3.amazonaws.com/' . $bucket ) !== false ) { // for Bucket name in path | |
$search = apply_filters( 'rtamazon_search_pattern', array( 's3.amazonaws.com/' . $bucket ) ); | |
} elseif ( ! empty( $custom_domain ) && strpos( $url, $custom_domain ) !== false ) { // for Custom Domain | |
$search = apply_filters( 'rtamazon_search_pattern', array( $custom_domain ) ); | |
} elseif ( strstr( $url , $wp_url_pattern ) !== false ) { | |
$search = apply_filters( 'rtamazon_search_pattern', array( $wp_url_pattern ) ); | |
} | |
/** | |
* Filters elements for replacing array. | |
* | |
* @since 1.4.0 | |
* | |
* @param array Array of replacing elements. | |
*/ | |
$replace = apply_filters( 'rtamazon_replace_pattern', array( $domain ) ); | |
/** | |
* Set original value as a key into array | |
* so we can use that for replace with new | |
* urls in content | |
*/ | |
$newurls[ $url ] = str_replace( $search, $replace, $aws_url ); | |
} // End foreach(). | |
return $newurls; | |
} // End if(). | |
} // End if(). | |
} | |
return $urls; | |
} | |
add_filter( 'rtawss3_match_urls', 'rtamazon_content_url' ); | |
/** | |
* Set default WP url when "WordPress Media URL" is enabled | |
*/ | |
function rtamazon_content_wp_url( $urls ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
$bucket = rtamazon_s3_get_bucket_name(); | |
if ( ! empty( $bucket ) ) { | |
// get bucket url structure | |
$url_structure = rtamazon_s3_get_url_structure( $bucket ); | |
$newurls = array(); | |
// If url structure is default than return the content, no need to search and replace. | |
if( 'rtawss3_wp_url' === $url_structure ) { | |
// get custom domain for bucket url | |
$custom_domain = rtamazon_s3_get_custom_url_domain_name( $bucket ); | |
// get default bucket url | |
$amazon_url = rtamazon_s3_get_default_bucket_url( $bucket ); | |
// get bucket url as per bucket url structure and custom domain | |
$domain = rtamazon_s3_get_bucket_url( $bucket, $url_structure, $custom_domain ); | |
$search = array(); // search the pattern into urls | |
$replace = array(); // replace with this into urls | |
foreach ( $urls as $url ) { | |
/* Set url according to selected url structure */ | |
if ( strpos( $url, $bucket . '.s3.amazonaws.com' ) !== false ) { | |
$search = apply_filters( 'rtamazon_search_pattern', array( $bucket . '.s3.amazonaws.com' ) ); | |
} elseif ( strpos( $url, 's3.amazonaws.com/' . $bucket ) !== false ) { | |
$search = apply_filters( 'rtamazon_search_pattern', array( 's3.amazonaws.com/' . $bucket ) ); | |
} elseif ( strpos( $url, $custom_domain ) !== false ) { | |
$search = apply_filters( 'rtamazon_search_pattern', array( $custom_domain ) ); | |
} | |
$replace = apply_filters( 'rtamazon_replace_pattern', array( $domain ) ); | |
/** | |
* Set original value as a key into array | |
* so we can use that for replace with new | |
* urls in content | |
*/ | |
$newurls[ $url ] = str_replace( $search, $replace, $url ); | |
} | |
return $newurls; | |
} // End if(). | |
} // End if(). | |
} // End if(). | |
return $urls; | |
} // End if(). | |
add_filter( 'rtawss3_match_urls', 'rtamazon_content_wp_url' ); | |
/***************** Cover picture for member and group ********************/ | |
function rtamazon_send_coverimage_to_s3( $emptyarr = array() , $bp_params, $needs_reset, $object_data ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
global $wpdb; | |
/* get bucket name */ | |
$bucket = rtamazon_s3_get_bucket_name(); | |
// Get BuddyPress Attachments Uploads Dir datas. | |
$bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); | |
$cover_subdir = $object_data['dir'] . '/' . $bp_params['item_id'] . '/cover-image'; | |
$cover_dir = trailingslashit( $bp_attachments_uploads_dir['basedir'] ) . $cover_subdir; | |
$match = $cover_subdir; | |
// First, escape the match for use in a LIKE statement. | |
$search = $wpdb->esc_like( $match ); | |
// Add wildcards, since we are searching within match text. | |
$search = '%' . $search . '%'; | |
$sql = "SELECT id, obj_key FROM {$wpdb->prefix}rtamazon_s3_media WHERE ( obj_key LIKE %s )"; | |
// Prepare the SQL statement so the string input gets escaped for security. | |
$sql = $wpdb->prepare( $sql, $search ); | |
$get_results = $wpdb->get_results( $sql ); | |
$object_arr = array(); | |
// Default error message. | |
$error_message = __( 'There was a problem uploading the cover image.', 'buddypress' ); | |
$cover_image_attachment = new BP_Attachment_Cover_Image(); | |
$uploaded = $cover_image_attachment->upload( $_FILES ); | |
// The BP Attachments Uploads Dir is not set, stop. | |
if ( ! $bp_attachments_uploads_dir ) { | |
bp_attachments_json_response( false, $is_html4, array( | |
'type' => 'upload_error', | |
'message' => $error_message, | |
) ); | |
} | |
if ( ! is_dir( $cover_dir ) ) { | |
// Upload error response. | |
bp_attachments_json_response( false, $is_html4, array( | |
'type' => 'upload_error', | |
'message' => $error_message, | |
) ); | |
} | |
$cover = bp_attachments_cover_image_generate_file( array( | |
'file' => $uploaded['file'], | |
'component' => $object_data['component'], | |
'cover_image_dir' => $cover_dir | |
), $cover_image_attachment ); | |
// Build the url to the file. | |
$cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover['cover_basename']; | |
// Init Feedback code, 1 is success. | |
$feedback_code = 1; | |
// Set the name of the file. | |
$name = $_FILES['file']['name']; | |
$name_parts = pathinfo( $name ); | |
$name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) ); | |
$data = array( | |
'url' => $cover_url, | |
'file' => $cover['cover_file'], | |
); | |
$data = apply_filters( 'rtamazon_cover_image_params', $data ); | |
$hash = md5( $cover_url ); | |
rtamazon_sent_to_s3( $data, $hash, true); | |
// delete profile pictures | |
rtamazon_delete_images_from_bucket( $bucket, $get_results ); | |
$cover_url = rtamazon_get_aws_url( $cover_url ); | |
// Finally return the cover image url to the UI. | |
bp_attachments_json_response( true, $is_html4, array( | |
'name' => $name, | |
'url' => $cover_url, | |
'feedback_code' => $feedback_code, | |
) ); | |
} | |
} | |
add_filter( 'bp_attachments_pre_cover_image_ajax_upload', 'rtamazon_send_coverimage_to_s3', 10, 4 ); | |
/** | |
* rtamazon_cover_image_callback Show cover image from s3 bucket | |
*/ | |
function rtamazon_cover_image_callback( $params = array() ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
if ( empty( $params ) && ! ( is_plugin_active( 'buddypress/bp-loader.php' ) ) ) { | |
return; | |
} | |
// New image url from AWS server | |
$cover_image_aws = rtamazon_get_aws_url( $params['cover_image'] ); | |
$cover_image_aws = ( ! empty( $cover_image_aws ) ) ? $cover_image_aws : $params['cover_image']; | |
// Avatar height - padding - 1/2 avatar height. | |
$avatar_offset = $params['height'] - 5 - round( (int) bp_core_avatar_full_height() / 2 ); | |
// Header content offset + spacing. | |
$top_offset = bp_core_avatar_full_height() - 10; | |
$left_offset = bp_core_avatar_full_width() + 20; | |
$cover_image = ( !empty( $params['cover_image'] ) ) ? 'background-image: url(' . $cover_image_aws . ');' : ''; | |
$hide_avatar_style = ''; | |
// Adjust the cover image header, in case avatars are completely disabled. | |
if ( ! buddypress()->avatar->show_avatars ) { | |
$hide_avatar_style = ' | |
#buddypress #item-header-cover-image #item-header-avatar { | |
display: none; | |
} | |
'; | |
if ( bp_is_user() ) { | |
$hide_avatar_style = ' | |
#buddypress #item-header-cover-image #item-header-avatar a { | |
display: block; | |
height: ' . $top_offset . 'px; | |
margin: 0 15px 19px 0; | |
} | |
#buddypress div#item-header #item-header-cover-image #item-header-content { | |
margin-left: auto; | |
} | |
'; | |
} | |
} | |
return ' | |
/* Cover image */ | |
#buddypress #header-cover-image { | |
height: ' . $params["height"] . 'px; | |
' . $cover_image . ' | |
} | |
#buddypress #create-group-form #header-cover-image { | |
margin: 1em 0; | |
position: relative; | |
} | |
.bp-user #buddypress #item-header { | |
padding-top: 0; | |
} | |
#buddypress #item-header-cover-image #item-header-avatar { | |
margin-top: '. $avatar_offset .'px; | |
float: left; | |
overflow: visible; | |
width: auto; | |
} | |
#buddypress div#item-header #item-header-cover-image #item-header-content { | |
clear: both; | |
float: left; | |
margin-left: ' . $left_offset . 'px; | |
margin-top: -' . $top_offset . 'px; | |
width: auto; | |
} | |
body.single-item.groups #buddypress div#item-header #item-header-cover-image #item-header-content, | |
body.single-item.groups #buddypress div#item-header #item-header-cover-image #item-actions { | |
clear: none; | |
margin-top: ' . $params["height"] . 'px; | |
margin-left: 0; | |
max-width: 50%; | |
} | |
body.single-item.groups #buddypress div#item-header #item-header-cover-image #item-actions { | |
max-width: 20%; | |
padding-top: 20px; | |
} | |
' . $hide_avatar_style . ' | |
#buddypress div#item-header-cover-image .user-nicename a, | |
#buddypress div#item-header-cover-image .user-nicename { | |
font-size: 200%; | |
color: #fff; | |
margin: 0 0 0.6em; | |
text-rendering: optimizelegibility; | |
text-shadow: 0 0 3px rgba( 0, 0, 0, 0.8 ); | |
} | |
#buddypress #item-header-cover-image #item-header-avatar img.avatar { | |
background: rgba( 255, 255, 255, 0.8 ); | |
border: solid 2px #fff; | |
} | |
#buddypress #item-header-cover-image #item-header-avatar a { | |
border: 0; | |
text-decoration: none; | |
} | |
#buddypress #item-header-cover-image #item-buttons { | |
margin: 0 0 10px; | |
padding: 0 0 5px; | |
} | |
#buddypress #item-header-cover-image #item-buttons:after { | |
clear: both; | |
content: ""; | |
display: table; | |
} | |
@media screen and (max-width: 782px) { | |
#buddypress #item-header-cover-image #item-header-avatar, | |
.bp-user #buddypress #item-header #item-header-cover-image #item-header-avatar, | |
#buddypress div#item-header #item-header-cover-image #item-header-content { | |
width: 100%; | |
text-align: center; | |
} | |
#buddypress #item-header-cover-image #item-header-avatar a { | |
display: inline-block; | |
} | |
#buddypress #item-header-cover-image #item-header-avatar img { | |
margin: 0; | |
} | |
#buddypress div#item-header #item-header-cover-image #item-header-content, | |
body.single-item.groups #buddypress div#item-header #item-header-cover-image #item-header-content, | |
body.single-item.groups #buddypress div#item-header #item-header-cover-image #item-actions { | |
margin: 0; | |
} | |
body.single-item.groups #buddypress div#item-header #item-header-cover-image #item-header-content, | |
body.single-item.groups #buddypress div#item-header #item-header-cover-image #item-actions { | |
max-width: 100%; | |
} | |
#buddypress div#item-header-cover-image h2 a, | |
#buddypress div#item-header-cover-image h2 { | |
color: inherit; | |
text-shadow: none; | |
margin: 25px 0 0; | |
font-size: 200%; | |
} | |
#buddypress #item-header-cover-image #item-buttons div { | |
float: none; | |
display: inline-block; | |
} | |
#buddypress #item-header-cover-image #item-buttons:before { | |
content: ""; | |
} | |
#buddypress #item-header-cover-image #item-buttons { | |
margin: 5px 0; | |
} | |
} | |
'; | |
} | |
} | |
/** | |
* rtamazon_fetch_cover_image Custom callback function for cover image | |
*/ | |
function rtamazon_fetch_cover_image( $settings = array() ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
$settings['callback'] = 'rtamazon_cover_image_callback'; | |
return $settings; | |
} | |
} | |
add_filter( 'bp_before_xprofile_cover_image_settings_parse_args', 'rtamazon_fetch_cover_image', 10, 1 ); | |
add_filter( 'bp_before_groups_cover_image_settings_parse_args', 'rtamazon_fetch_cover_image', 10, 1 ); | |
/***************** Cover picture for member and group end ********************/ | |
/************************* User profile picture **************************/ | |
/** | |
* Send profile image to amazon s3 | |
*/ | |
function rtamazon_send_avatar_to_s3( $item_id, $item_type ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
$avatar_types = array( 'full','thumb' ); | |
$avatarfolder = apply_filters( 'rtmedia_avatars_foldername','avatars' ); | |
global $wpdb; | |
/* get bucket name */ | |
$bucket = rtamazon_s3_get_bucket_name(); | |
$get_results = rtamazon_get_bucket_images( $avatarfolder, $item_id ); | |
$object_arr = array(); | |
// Upload thumb and full image to amazon s3 | |
foreach ( $avatar_types as $type ) { | |
$avatar = bp_core_fetch_avatar( array( | |
'object' => 'user', | |
'item_id' => $item_id, | |
'html' => false, | |
'type' => $type, | |
) ); | |
$avatar_path = BP_AVATAR_UPLOAD_PATH. '/' . $avatarfolder . '/' . $item_id . '/' . basename($avatar); | |
$data = array( | |
'url' => $avatar, | |
'file' => $avatar_path, | |
); | |
$data = apply_filters( 'rtamazon_avatar_params', $data ); | |
$hash = md5( $avatar ); | |
rtamazon_sent_to_s3( $data, $hash, true); | |
} | |
// delete profile pictures | |
rtamazon_delete_images_from_bucket( $bucket, $get_results ); | |
} | |
} | |
add_action( 'xprofile_avatar_uploaded', 'rtamazon_send_avatar_to_s3', 10, 2 ); | |
/** | |
* Get profile picture from s3 bucket | |
*/ | |
function rtamazon_set_profile( $avatar_url, $params ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
$rtawss3 = RTAWSS3_Class::instance(); | |
$obj = $rtawss3->rtawss3_db( 'get_row', array(), 'wp_url', trim( $avatar_url ) ); | |
if( $obj && isset( $obj->s3_url ) ) { | |
$avatar_url = rtamazon_s3_replace_content_urls( $obj->s3_url ); | |
} | |
//remove_filter( 'bp_core_fetch_avatar_url', 'rtamazon_set_profile', 999 ); | |
return $avatar_url; | |
} | |
} | |
add_filter( 'bp_core_fetch_avatar_url', 'rtamazon_set_profile', 99, 2 ); | |
/** | |
* Get members / groups profile picture from s3 bucket | |
*/ | |
function rtamazon_set_profile_html( $avatar_html, $params ) { | |
if ( class_exists( 'RTAWSS3_Class' ) && ( isset( $params['object'] ) && $params['object'] == 'user' ) ) { | |
$rtawss3 = RTAWSS3_Class::instance(); | |
$matches = rtawss3_custom_match_urls( $avatar_html ); | |
if( ! empty( $matches ) ) { | |
$where = array( | |
'wp_url' =>array( | |
'compare' => 'IN', | |
'value' => array_keys( $matches ), | |
), | |
); | |
$rows = $rtawss3->rtawss3_db( 'get_results', array(), 'status', 'moved', false, array(), $where ); | |
$urls = array(); | |
$urls_data = array(); | |
// check if image is moved to s3 bucket then | |
// show image from bucket | |
if ( count( $rows ) > 0 ) { | |
foreach( $rows as $row ) { | |
if( $rtawss3->rtawss3_check_bucket_url( $row->bucket ) ) { | |
$urls[] = $row->wp_url; | |
$urls_data[] = $row; | |
} | |
} | |
// Change url for images | |
$avatar_html = str_replace( array_keys( $matches ), $matches, $avatar_html ); | |
} | |
foreach( $matches as $match ) { | |
$search = array_search( $match, $urls ); | |
if( $search !== false ) { | |
$row = $urls_data[ $search ]; | |
$avatar_url = str_replace( $match, $row->s3_url, $avatar_url); | |
} | |
} | |
} | |
} | |
return $avatar_html; | |
} | |
add_filter( 'bp_core_fetch_avatar', 'rtamazon_set_profile_html',10, 2 ); | |
/************************* User profile picture end **************************/ | |
/**************************** Delete avatar pic **********************************/ | |
/** | |
* rtamazon_delete_avatar Delete avatar image for member from s3 bucket | |
*/ | |
function rtamazon_delete_avatar( $args ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
if ( empty( $args ) ) { | |
return; | |
} | |
} | |
$item_id = ( isset( $args['item_id'] ) && ! empty( $args['item_id'] ) ) ? $args['item_id'] : 0; | |
if ( 'user' === $args['object'] ) { | |
$avatarfolder = apply_filters( 'rtmedia_grpavatars_foldername','avatars' ); | |
} elseif ( 'group' === $args['object'] ) { | |
$avatarfolder = apply_filters( 'rtmedia_grpavatars_foldername','group-avatars' ); | |
} | |
global $wpdb; | |
/* get bucket name */ | |
$bucket = rtamazon_s3_get_bucket_name(); | |
$get_results = rtamazon_get_bucket_images( $avatarfolder, $item_id ); | |
// delete profile pictures | |
rtamazon_delete_images_from_bucket( $bucket, $get_results ); | |
} | |
add_action( 'bp_core_delete_existing_avatar', 'rtamazon_delete_avatar' ); | |
/**************************** Delete avatar pic end **********************************/ | |
/**************************** Delete cover pic **********************************/ | |
/** | |
* rtamazon_delete_coverphoto Delete cover image for members and groups from s3 bucket | |
*/ | |
function rtamazon_delete_coverphoto( $status, $args ){ | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
if ( empty( $args ) ) { | |
return; | |
} | |
} | |
$item_id = ( isset( $args['item_id'] ) && ! empty( $args['item_id'] ) ) ? $args['item_id'] : 0; | |
if ( 'members' === $args['object_dir'] ) { | |
$coverfolder = apply_filters( 'rtmedia_coveravatars_foldername','members' ); | |
} elseif ( 'groups' === $args['object_dir'] ) { | |
$coverfolder = apply_filters( 'rtmedia_coveravatars_foldername','groups' ); | |
} | |
global $wpdb; | |
/* get bucket name */ | |
$bucket = rtamazon_s3_get_bucket_name(); | |
$get_results = rtamazon_get_bucket_images( $coverfolder, $item_id ); | |
$object_arr = array(); | |
// delete profile pictures | |
rtamazon_delete_images_from_bucket( $bucket, $get_results ); | |
return true; | |
} | |
add_filter( 'bp_attachments_pre_delete_file', 'rtamazon_delete_coverphoto', 10, 2 ); | |
/** | |
* rtamazon_get_bucket_images Get list of perticular images uploaded to s3 amazon | |
*/ | |
function rtamazon_get_bucket_images( $coverfolder, $item_id ){ | |
global $wpdb; | |
$match = '/' . $coverfolder . '/' . $item_id . '/'; | |
// First, escape the match for use in a LIKE statement. | |
$search = $wpdb->esc_like( $match ); | |
// Add wildcards, since we are searching within match text. | |
$search = '%' . $match . '%'; | |
$sql = "SELECT id, obj_key FROM {$wpdb->prefix}rtamazon_s3_media WHERE ( obj_key LIKE %s )"; | |
// Prepare the SQL statement so the string input gets escaped for security. | |
$sql = $wpdb->prepare( $sql, $search ); | |
return $wpdb->get_results( $sql ); | |
} | |
/** | |
* rtamazon_delete_images_from_bucket Delete requested images from s3 bucket | |
*/ | |
function rtamazon_delete_images_from_bucket( $bucket, $get_results = array() ) { | |
global $wpdb; | |
$object_arr = array(); | |
// delete profile pictures | |
if ( count( $get_results ) > 0 ) { | |
foreach( $get_results as $key_arr ) { | |
$object_arr[][ 'Key' ] = $key_arr->obj_key; | |
$rtawss3 = RTAWSS3_Class::instance(); | |
$delete_obj = $rtawss3->rtawss3_db( 'get_row', array(), 'obj_key', trim( $key_arr->obj_key ) ); | |
if( $delete_obj ) { | |
$where[ 'obj_key' ] = trim( $key_arr->obj_key ); | |
$rtawss3->rtawss3_db( 'delete', '', '', '', '', '', $where ); | |
} | |
} | |
$rtawss3_client = new RTAWSS3_Client(); | |
$status = $rtawss3_client->rtawss3_delete_bucket_object( $bucket, $object_arr ); | |
} | |
} | |
/**************************** Delete cover pic end **********************************/ | |
/* | |
* Matching urls | |
*/ | |
function rtawss3_custom_match_urls( $content ) { | |
$regex = '/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i'; | |
preg_match_all( $regex, $content, $matches ); | |
$mediaList = array(); | |
if( isset( $matches[ 0 ] ) && !empty( $matches[ 0 ] ) ) { | |
/** | |
* Filters attachment urls in content. | |
* | |
* @param array Array of all urls into content. | |
*/ | |
$mediaList = apply_filters( 'rtawss3_match_urls', $matches[ 0 ] ); | |
} | |
return $mediaList; | |
} | |
/* | |
* Filtering content | |
*/ | |
function rtawss3_custom_filter_content( $content ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
$matches = rtawss3_custom_match_urls( $content ); | |
if( !empty( $matches ) ) { | |
$content = rtamazon_s3_replace_content_urls( $content ); | |
} | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'rtawss3_custom_filter_content', 99 ); | |
/** | |
* rtamazon_inspirebook_cover_image_url Fetch cover image from s3bucket. | |
* This function is "Inspirebook" theme specific | |
* @param url $url Original url | |
* @return url S3 bucket url | |
*/ | |
function rtamazon_inspirebook_cover_image_url( $url ) { | |
if ( class_exists( 'RTAWSS3_Class' ) ) { | |
// New image url from AWS server | |
$cover_image_aws = rtamazon_get_aws_url( $url ); | |
$cover_image_aws = ( ! empty( $cover_image_aws ) ) ? $cover_image_aws : $url; | |
$url = $cover_image_aws; | |
} | |
return $url; | |
} | |
add_filter( 'inspirebook_cover_image_url', 'rtamazon_inspirebook_cover_image_url' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment