| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: BuddyPress |
| 4 |
* Plugin URI: https://wordpress.org/plugins/buddypress/ |
| 5 |
* |
| 6 |
* Compatibility Description: Ensures compatibility with BuddyPress. |
| 7 |
* |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace wpCloud\StatelessMedia { |
| 11 |
|
| 12 |
if( !class_exists( 'wpCloud\StatelessMedia\BuddyPress' ) ) { |
| 13 |
|
| 14 |
class BuddyPress extends ICompatibility { |
| 15 |
protected $id = 'buddypress'; |
| 16 |
protected $title = 'BuddyPress'; |
| 17 |
protected $constant = 'WP_STATELESS_COMPATIBILITY_BUDDYPRESS'; |
| 18 |
protected $description = 'Ensures compatibility with BuddyPress.'; |
| 19 |
protected $plugin_file = [ 'buddypress/bp-loader.php' ]; |
| 20 |
protected $sm_mode_not_supported = [ 'stateless' ]; |
| 21 |
|
| 22 |
/** |
| 23 |
* @param $sm |
| 24 |
*/ |
| 25 |
public function module_init( $sm ) { |
| 26 |
add_action( 'xprofile_avatar_uploaded', array( $this, 'avatar_uploaded' ), 10, 3 ); |
| 27 |
add_action( 'groups_avatar_uploaded', array( $this, 'avatar_uploaded' ), 10, 3 ); |
| 28 |
add_filter( 'bp_core_fetch_avatar', array( $this, 'bp_core_fetch_avatar' ), 10, 3 ); |
| 29 |
add_filter( 'bp_core_pre_delete_existing_avatar', array( $this, 'delete_existing_avatar' ), 10, 2 ); |
| 30 |
add_filter( 'bp_attachments_pre_get_attachment', array( $this, 'bp_attachments_pre_get_attachment' ), 10, 2 ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Sync avatar. |
| 35 |
* @param $item_id |
| 36 |
* @param $type |
| 37 |
* @param $r |
| 38 |
*/ |
| 39 |
public function avatar_uploaded( $item_id, $type, $r ) { |
| 40 |
$full_avatar = bp_core_fetch_avatar( array( |
| 41 |
'object' => $r['object'], |
| 42 |
'item_id' => $r['item_id'], |
| 43 |
'html' => false, |
| 44 |
'type' => 'full', |
| 45 |
) ); |
| 46 |
$thumb_avatar = bp_core_fetch_avatar( array( |
| 47 |
'object' => $r['object'], |
| 48 |
'item_id' => $r['item_id'], |
| 49 |
'html' => false, |
| 50 |
'type' => 'thumb', |
| 51 |
) ); |
| 52 |
|
| 53 |
$wp_uploads_dir = wp_get_upload_dir(); |
| 54 |
|
| 55 |
$full_avatar_path = $wp_uploads_dir[ 'basedir' ] . '/' . apply_filters( 'wp_stateless_file_name', $full_avatar, false ); |
| 56 |
$full_avatar = apply_filters( 'wp_stateless_file_name', $full_avatar, 0 ); |
| 57 |
|
| 58 |
$thumb_avatar_path = $wp_uploads_dir[ 'basedir' ] . '/' . apply_filters( 'wp_stateless_file_name', $thumb_avatar, false ); |
| 59 |
$thumb_avatar = apply_filters( 'wp_stateless_file_name', $thumb_avatar, 0 ); |
| 60 |
|
| 61 |
do_action( 'sm:sync::syncFile', $full_avatar, $full_avatar_path, true, array( 'ephemeral' => false ) ); |
| 62 |
do_action( 'sm:sync::syncFile', $thumb_avatar, $thumb_avatar_path, true, array( 'ephemeral' => false ) ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Convert image url in image html to GCS URL. |
| 67 |
* |
| 68 |
* @param [type] $image_html html code for image. |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function bp_core_fetch_avatar( $image_html ) { |
| 72 |
try { |
| 73 |
preg_match( "/src=(?:'|\")(http.*?)(?:'|\")/", $image_html, $image_url ); |
| 74 |
if( !empty( $image_url[ 1 ] ) ) { |
| 75 |
$gs_image_url = $this->bp_core_fetch_avatar_url( $image_url[ 1 ] ); |
| 76 |
$image_html = str_replace( $image_url[ 1 ], $gs_image_url, $image_html ); |
| 77 |
} |
| 78 |
} catch( \Throwable $th ) { |
| 79 |
//throw $th; |
| 80 |
} |
| 81 |
return $image_html; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Sync then return GCS url. |
| 86 |
* |
| 87 |
* @param [type] $url image url. |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function bp_core_fetch_avatar_url( $url ) { |
| 91 |
$wp_uploads_dir = wp_get_upload_dir(); |
| 92 |
$name = apply_filters( 'wp_stateless_file_name', $url, 0 ); |
| 93 |
$full_avatar_path = $wp_uploads_dir[ 'basedir' ] . '/' . $name; |
| 94 |
|
| 95 |
|
| 96 |
$root_dir = ud_get_stateless_media()->get( 'sm.root_dir' ); |
| 97 |
$root_dir = apply_filters("wp_stateless_handle_root_dir", $root_dir); |
| 98 |
$root_dir = trim( $root_dir, '/ ' ); // Remove any forward slash and empty space. |
| 99 |
// Making sure that we only modify url for uploads dir. |
| 100 |
// @todo support photo in plugins directory. |
| 101 |
|
| 102 |
if( strpos( $name, plugins_url() ) === 0 ) { |
| 103 |
$name = str_replace( plugins_url() . '/', '', $name ); |
| 104 |
$name = apply_filters( 'wp_stateless_file_name', $name, 0 ); |
| 105 |
$full_avatar_path = WP_PLUGIN_DIR . '/' . $name; |
| 106 |
} |
| 107 |
|
| 108 |
if( strpos( $name, "$root_dir/http" ) !== 0 && strpos( $name, "http" ) !== 0 && $root_dir !== $name ) { |
| 109 |
do_action( 'sm:sync::syncFile', $name, $full_avatar_path, false, array( 'ephemeral' => false ) ); |
| 110 |
$url = ud_get_stateless_media()->get_gs_host() . '/' . $name; |
| 111 |
} |
| 112 |
return $url; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Deleting avatar from GCS. |
| 117 |
* @param $return |
| 118 |
* @param $args |
| 119 |
* @return bool |
| 120 |
*/ |
| 121 |
public function delete_existing_avatar( $return, $args ) { |
| 122 |
if( empty( $args[ 'object' ] ) && empty( $args[ 'item_id' ] ) ) { |
| 123 |
return $return; |
| 124 |
} |
| 125 |
|
| 126 |
$full_avatar = bp_core_fetch_avatar( array( 'object' => $args[ 'object' ], 'item_id' => $args[ 'item_id' ], 'html' => false, 'type' => 'full', ) ); |
| 127 |
$thumb_avatar = bp_core_fetch_avatar( array( 'object' => $args[ 'object' ], 'item_id' => $args[ 'item_id' ], 'html' => false, 'type' => 'thumb', ) ); |
| 128 |
|
| 129 |
do_action( 'sm:sync::deleteFile', apply_filters( 'wp_stateless_file_name', $full_avatar, 0 ) ); |
| 130 |
do_action( 'sm:sync::deleteFile', apply_filters( 'wp_stateless_file_name', $thumb_avatar, 0 ) ); |
| 131 |
|
| 132 |
if( ud_get_stateless_media()->get( 'sm.mode' ) === 'ephemeral' ) { |
| 133 |
$return = false; |
| 134 |
} |
| 135 |
|
| 136 |
return $return; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Sync and return GCS url for group images. |
| 141 |
* |
| 142 |
* Used as CSS background-image. |
| 143 |
* |
| 144 |
* @param [type] $return |
| 145 |
* @param [type] $r |
| 146 |
* @return void |
| 147 |
*/ |
| 148 |
public function bp_attachments_pre_get_attachment( $return, $r ) { |
| 149 |
// Return if this is a recursive call. |
| 150 |
if( !empty( $r[ 'recursive' ] ) ) { |
| 151 |
return $return; |
| 152 |
} |
| 153 |
|
| 154 |
try { |
| 155 |
$debug_backtrace = \debug_backtrace( false ); |
| 156 |
|
| 157 |
// Making sure we only return GCS link if the type is url. |
| 158 |
if( !empty( $debug_backtrace[ 3 ][ 'args' ][ 0 ] ) && $debug_backtrace[ 3 ][ 'args' ][ 0 ] == 'url' ) { |
| 159 |
$r[ 'recursive' ] = true; |
| 160 |
|
| 161 |
$url = bp_attachments_get_attachment( 'url', $r ); |
| 162 |
$name = apply_filters( 'wp_stateless_file_name', $url, 0 ); |
| 163 |
|
| 164 |
$root_dir = ud_get_stateless_media()->get( 'sm.root_dir' ); |
| 165 |
$root_dir = apply_filters("wp_stateless_handle_root_dir", $root_dir); |
| 166 |
$root_dir = trim( $root_dir, '/ ' ); // Remove any forward slash and empty space. |
| 167 |
|
| 168 |
if( !empty( $name ) && $root_dir . "/" != $name ) { |
| 169 |
$full_path = bp_attachments_get_attachment( false, $r ); |
| 170 |
do_action( 'sm:sync::syncFile', $name, $full_path, false, array( 'ephemeral' => false ) ); |
| 171 |
$return = ud_get_stateless_media()->get_gs_host() . '/' . $name; |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
} catch( \Throwable $th ) { |
| 176 |
//throw $th; |
| 177 |
} |
| 178 |
return $return; |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
|