Skip to content

Commit

Permalink
Merge pull request #2 from udx/develop-alexey
Browse files Browse the repository at this point in the history
Develop alexey
  • Loading branch information
balexey88 authored Nov 23, 2023
2 parents 2aea20e + 633e797 commit 38ceaf2
Show file tree
Hide file tree
Showing 25 changed files with 3,643 additions and 1 deletion.
33 changes: 33 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.DS_Store
.vscode
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
# wp-stateless-buddypress-addon
# WP-Stateless - BuddyPress Addon

Provides compatibility between the [BuddyPress](https://wordpress.org/plugins/buddypress/) and the [WP-Stateless](https://wordpress.org/plugins/wp-stateless/) plugins.

### Features

* Apply WP-Stateless functionality for avatars, group avatars, and attachments uploaded using BuddyPress.

### Notes

* Tested with BuddyPress plugin version 11.4.0

### Support, Feedback, & Contribute

We welcome community involvement via the [GitHub repository](https://github.com/udx/wp-stateless-buddypress-addon).

### Frequently Asked Questions

<details>
<summary>Where can I submit feature requests or bug reports?</summary>

We encourage community feedback and discussion through issues on the [GitHub repository](https://github.com/udx/wp-stateless-buddypress-addon/issues).
</details>

<details>
<summary>Can I test new features before they are released?</summary>

To ensure new releases cause as little disruption as possible, we rely on early adopters who assist us by testing out new features before they are released. [Please contact us](https://udx.io/) if you are interested in becoming an early adopter.
</details>

<details>
<summary>Who maintains this plugin?</summary>

[UDX](https://udx.io/) maintains this plugin by continuing development through its own staff, reviewing pull requests, testing, and steering the overall release schedule. UDX is located in Durham, North Carolina, and provides WordPress engineering and hosting services to clients throughout the United States.
</details>
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
== Changelog ==
= 0.0.1 =
* Initial public release.
3 changes: 3 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#### 0.0.1

- Initial public release.
171 changes: 171 additions & 0 deletions class-buddypress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

namespace WPSL\BuddyPress;

use wpCloud\StatelessMedia\Compatibility;

class BuddyPress extends Compatibility {
protected $id = 'buddypress';
protected $title = 'BuddyPress';
protected $constant = 'WP_STATELESS_COMPATIBILITY_BUDDYPRESS';
protected $description = 'Ensures compatibility with BuddyPress.';
protected $plugin_file = [ 'buddypress/bp-loader.php' ];
protected $sm_mode_not_supported = [ 'stateless' ];

/**
* @param $sm
*/
public function module_init( $sm ) {
add_action('xprofile_avatar_uploaded', array($this, 'avatar_uploaded'), 10, 3);
add_action('groups_avatar_uploaded', array($this, 'avatar_uploaded'), 10, 3);
add_filter('bp_core_fetch_avatar', array($this, 'bp_core_fetch_avatar'), 10, 3);
add_filter('bp_core_pre_delete_existing_avatar', array($this, 'delete_existing_avatar'), 10, 2);
add_filter('bp_attachments_pre_get_attachment', array($this, 'bp_attachments_pre_get_attachment'), 10, 2);
}

/**
* Sync avatar.
* @param $item_id
* @param $type
* @param $r
*/
public function avatar_uploaded($item_id, $type, $r) {
$full_avatar = bp_core_fetch_avatar(array(
'object' => $r['object'],
'item_id' => $r['item_id'],
'html' => false,
'type' => 'full',
));
$thumb_avatar = bp_core_fetch_avatar(array(
'object' => $r['object'],
'item_id' => $r['item_id'],
'html' => false,
'type' => 'thumb',
));

$wp_uploads_dir = wp_get_upload_dir();

$full_avatar_path = $wp_uploads_dir['basedir'] . '/' . apply_filters('wp_stateless_file_name', $full_avatar, false);
$full_avatar = apply_filters('wp_stateless_file_name', $full_avatar, 0);

$thumb_avatar_path = $wp_uploads_dir['basedir'] . '/' . apply_filters('wp_stateless_file_name', $thumb_avatar, false);
$thumb_avatar = apply_filters('wp_stateless_file_name', $thumb_avatar, 0);

do_action('sm:sync::syncFile', $full_avatar, $full_avatar_path, true, array('ephemeral' => false));
do_action('sm:sync::syncFile', $thumb_avatar, $thumb_avatar_path, true, array('ephemeral' => false));
}

/**
* Convert image url in image html to GCS URL.
*
* @param [type] $image_html html code for image.
* @return void
*/
public function bp_core_fetch_avatar($image_html) {
try {
preg_match("/src=(?:'|\")(http.*?)(?:'|\")/", $image_html, $image_url);
if (!empty($image_url[1])) {
$gs_image_url = $this->bp_core_fetch_avatar_url($image_url[1]);
$image_html = str_replace($image_url[1], $gs_image_url, $image_html);
}
} catch (\Throwable $th) {
//throw $th;
}
return $image_html;
}

/**
* Sync then return GCS url.
*
* @param [type] $url image url.
* @return void
*/
public function bp_core_fetch_avatar_url($url) {
$wp_uploads_dir = wp_get_upload_dir();
$name = apply_filters('wp_stateless_file_name', $url, 0);
$full_avatar_path = $wp_uploads_dir['basedir'] . '/' . $name;

$root_dir = ud_get_stateless_media()->get('sm.root_dir');
$root_dir = apply_filters("wp_stateless_handle_root_dir", $root_dir);
$root_dir = trim($root_dir, '/ '); // Remove any forward slash and empty space.
// Making sure that we only modify url for uploads dir.
// @todo support photo in plugins directory.

if (strpos($name, plugins_url()) === 0) {
$name = str_replace(plugins_url() . '/', '', $name);
$name = apply_filters('wp_stateless_file_name', $name, 0);
$full_avatar_path = WP_PLUGIN_DIR . '/' . $name;
}

if (strpos($name, "$root_dir/http") !== 0 && strpos($name, "http") !== 0 && $root_dir !== $name) {
do_action('sm:sync::syncFile', $name, $full_avatar_path, false, array('ephemeral' => false));
$url = ud_get_stateless_media()->get_gs_host() . '/' . $name;
}
return $url;
}

/**
* Deleting avatar from GCS.
* @param $return
* @param $args
* @return bool
*/
public function delete_existing_avatar($return, $args) {
if (empty($args['object']) && empty($args['item_id'])) {
return $return;
}

$full_avatar = bp_core_fetch_avatar(array('object' => $args['object'], 'item_id' => $args['item_id'], 'html' => false, 'type' => 'full',));
$thumb_avatar = bp_core_fetch_avatar(array('object' => $args['object'], 'item_id' => $args['item_id'], 'html' => false, 'type' => 'thumb',));

do_action('sm:sync::deleteFile', apply_filters('wp_stateless_file_name', $full_avatar, 0));
do_action('sm:sync::deleteFile', apply_filters('wp_stateless_file_name', $thumb_avatar, 0));

if (ud_get_stateless_media()->get('sm.mode') === 'ephemeral') {
$return = false;
}

return $return;
}

/**
* Sync and return GCS url for group images.
*
* Used as CSS background-image.
*
* @param [type] $return
* @param [type] $r
* @return void
*/
public function bp_attachments_pre_get_attachment($return, $r) {
// Return if this is a recursive call.
if (!empty($r['recursive'])) {
return $return;
}

try {
$debug_backtrace = debug_backtrace(false);

// Making sure we only return GCS link if the type is url.
if (!empty($debug_backtrace[3]['args'][0]) && $debug_backtrace[3]['args'][0] == 'url') {
$r['recursive'] = true;

$url = bp_attachments_get_attachment('url', $r);
$name = apply_filters('wp_stateless_file_name', $url, 0);

$root_dir = ud_get_stateless_media()->get('sm.root_dir');
$root_dir = apply_filters("wp_stateless_handle_root_dir", $root_dir);
$root_dir = trim($root_dir, '/ '); // Remove any forward slash and empty space.

if (!empty($name) && $root_dir . "/" != $name) {
$full_path = bp_attachments_get_attachment(false, $r);
do_action('sm:sync::syncFile', $name, $full_path, false, array('ephemeral' => false));
$return = ud_get_stateless_media()->get_gs_host() . '/' . $name;
}
}
} catch (\Throwable $th) {
//throw $th;
}
return $return;
}
}
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "udx/wp-stateless-buddypress-addon",
"description": "Ensures compatibility with BuddyPress",
"type": "wordpress-plugin",
"license": "MIT",
"authors": [
{
"name": "UDX",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"require-dev": {
"phpunit/phpunit": "^9.5",
"brain/monkey": "2.*"
},
"autoload": {
"files": [
"class-buddypress.php"
]
},
"scripts": {
"test": [
"@composer install",
"./vendor/bin/phpunit --prepend tests/prepend.php tests/ --testdox"
]
},
"scripts-descriptions": {
"test": "Run all tests."
}
}
Loading

0 comments on commit 38ceaf2

Please sign in to comment.