Skip to content

Commit

Permalink
Merge pull request #1 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 2525250 + ca46cb7 commit 9a3dd64
Show file tree
Hide file tree
Showing 25 changed files with 3,479 additions and 2 deletions.
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
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
# wp-stateless-polylang-pro-addon
WP-Stateless - Polylang Pro Addon
# WP-Stateless - Polylang Pro Addon

Provides compatibility between the [Polylang Pro](https://polylang.pro/) and the [WP-Stateless](https://wordpress.org/plugins/wp-stateless/) plugins.

### Features

* Sync translation images with Google Cloud Storage.

### Notes

* Tested with Polylang Pro plugin version 3.5.2

### Support, Feedback, & Contribute

We welcome community involvement via the [GitHub repository](https://github.com/udx/wp-stateless-polylang-pro-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-polylang-pro-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.
89 changes: 89 additions & 0 deletions class-polylang-pro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/**
* Compatibility Plugin Name: Polylang Pro
* Compatibility Plugin URI: https://polylang.pro/
*
* Compatibility Description: Ensures post_meta properly updated on media translated with Polylang.
*
*/

namespace WPSL\PolylangPro;

use wpCloud\StatelessMedia\Compatibility;

class PolylangPro extends Compatibility {
protected $id = 'polylang-pro';
protected $title = 'Polylang Pro';
protected $constant = 'WP_STATELESS_COMPATIBILITY_POLYLANG_PRO';
protected $description = 'Ensures compatibility with Polylang Pro.';
protected $plugin_file = ['polylang-pro/polylang.php'];

/**
* @param $sm
*/
public function module_init($sm) {
// Polylang duplicates attachments for all languages used.
// But WP generates image sizes only for one of the attachment copies (currently second attachment).
// Thus image sizes and WP Stateless meta data appears to be broken for other copies.
add_action('pll_translate_media', array($this, 'pll_translate_media'), 10, 3);
}

private function get_stateless_meta($post_id) {
// In case Polylang is not active of codebase not compatible anymore
if (!function_exists('pll_get_post_translations')) {
return null;
}

$metadata = null;

// Get other attachment ids for the same file
$ids = pll_get_post_translations($post_id);

foreach ($ids as $id) {
$meta = get_post_meta($id, 'sm_cloud', true);

if ( !empty($meta) && !empty($meta['name']) ) {
$metadata = $meta;
break;
}
}

return $metadata;
}

/**
* @param $post_id
* @param $tr_id
* @param $lang_slug
*/
public function pll_translate_media($post_id, $tr_id, $lang_slug) {
// We need to delay the metadata update until the metadata is fully generated.
add_filter('wp_stateless_media_synced', function ($metadata, $attachment_id, $force, $args) use ($post_id, $tr_id, $lang_slug) {
if ($attachment_id == $post_id) {
$meta = get_post_meta($tr_id, '_wp_attachment_metadata', true);

if (!empty($meta['sizes'])) {
// with Polylang Pro 2.6 the sizes of original image gets missing.
update_post_meta($attachment_id, '_wp_attachment_metadata', wp_slash($meta));

$metadata = $meta;
} else if (!empty($metadata['sizes'])) {
// But user reported that metadata gets missing on duplicate.
update_post_meta($tr_id, '_wp_attachment_metadata', wp_slash($metadata));
}
}

$cloud_meta = $this->get_stateless_meta($attachment_id);

if ( !empty($cloud_meta) ) {
// Need to update cloud meta for both original attachment
update_post_meta($attachment_id, 'sm_cloud', wp_slash($cloud_meta));
// Update duplicated attachment meta
update_post_meta($tr_id, 'sm_cloud', wp_slash($cloud_meta));
}

return $metadata;
}, 10, 4);
}
}
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-polylang-pro-addon",
"description": "Ensures compatibility with Polylang Pro",
"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-polylang-pro.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 9a3dd64

Please sign in to comment.