Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with WP 4.9, Polylang 2.3, ACF 5.6 PRO #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
*.swp
*.out
.svn/
.svnignore
.svnignore
.idea/
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
Polylang Sync Some Fields
=========================

Makes Polylang and ACF work together well. Attaches a switch to ACFs whether or not to synchronize ACF-content between all Polylang languages.

![Screenshot of Sync Option](img/Screenshot.png)

Compatibility
-------------
Tested With WP 4.5.3, Polylang 1.9.3 - 1.9, ACF Free 4

Features:
---------
- Adds an option to ACF fields "Sync Between Languages"
Tested With WP 4.9, Polylang 2.3, ACF 5.6 PRO

How To Use
----------

* Install Plugin
* In Polylang settings, choose synchronization: Custom Fields
* For your custom fields, choose whether or not to "Sync Between Languages"
* Download and install Plugin
* In Polylang settings: Choose synchronization "Custom Fields"
* For your custom fields, choose whether or not to "Sync between all languages?"

Known Issues
------------

Reordering of Flexible Content Fields leads to:

* Maximum execution time of 30 seconds exceeded (...) wordpress/wp-includes/wp-db.php on line 1924

or:

* Maximum execution time of 30 seconds exceeded in (...) wordpress/wp-content/plugins/polylang-pro/modules/share-slug/share-post-slug.php on line 183
36 changes: 36 additions & 0 deletions css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,39 @@
th.column-polylang_cloner_actions, td.column-polylang_cloner_actions {
width: 10em;
}

.langSyncIcon {
background-image: url("../img/auto-sync.svg");
background-size: contain;
background-repeat: no-repeat;
width: 113px;
height: 19px;
background-position: center;
display: inline-block;
margin: 0 0 15px;
}

.postbox-container {
position: relative;
}

.saveFirstOverlay {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: rgba(0,0,0,0.8);
z-index: 10;
}

.saveFirstOverlay__message {
position: absolute;
top: 60px;
left: 50%;
transform: translateX(-50%);
color: white;
font-size: 30px;
line-height: 45px;
text-align: center;
}
Binary file added img/Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions img/auto-sync.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
190 changes: 143 additions & 47 deletions include/class-PolylangSyncSomeFieldsWatch.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php


if (!class_exists('PolylangSyncSomeFieldsWatch')) :
class PolylangSyncSomeFieldsWatch
{
/**
* Holding the singleton instance
* Holding the singleton instance
*/
private static $_instance = null;

Expand All @@ -21,56 +20,68 @@ public static function instance()
}

/**
* Prevent from creating more instances
* Prevent from creating more instances
*/
private function __clone()
{
}

/**
*/

private function __construct()
{
add_filter('pll_copy_post_metas', array(&$this, 'filter_keys'), 20, 3);
add_action('acf/create_field_options', array(&$this, 'action_acf_create_field_options'), 10, 1);
add_action('acf/create_field', array(&$this, 'action_acf_create_field'), 10, 1);
add_action('acf/render_field_settings', array(&$this, 'action_acf_create_field_options'), 10, 1);
add_action('acf/render_field', array(&$this, 'action_acf_create_field'), 10, 1);
add_action('init', array(&$this, 'register_strings'));

add_action('admin_enqueue_scripts', 'load_assets');
function load_assets() {
wp_register_style('pll_acf_sync_some_fields', plugins_url('/../css/admin.css',__FILE__ ));
wp_enqueue_style('pll_acf_sync_some_fields');
}

}

/**
* Display label if set to sync on editing screen
* @param $field
*/

public function action_acf_create_field($field)
{
if (get_post_type() != 'post') {
return; // FIXME
if (get_post_type() === 'acf-field-group') {
return;
}
$sync = isset($field['lang_sync']) ? $field['lang_sync'] : 0;

$sync = isset($field['lang_sync']) ? $field['lang_sync'] : 1;
if ($sync) {
echo '<small>Synced between languages</small>';
echo '<div class="langSyncIcon">&nbsp;</div>';
}
}
/**
* Register Field Strings so they can be translated
*
* @param $field
* @param $post_id
*/

public function register_strings()
{
if (!PLL_ADMIN) {
return;
}
$return = array();
$return = apply_filters('acf/get_field_groups', $return);
$return = acf_get_field_groups();

foreach($return as $group) {
$lang = pll_get_post_language($group['id']);
if ($lang != 'en') {

$lang = pll_get_post_language($group['ID']);
if ($lang != 'de') {
continue;
}
pll_register_string('group_' . $group['id'] . '_title', $group['title']);

$fields = array();
$fields = apply_filters('acf/field_group/get_fields', $fields, $group['id']);
pll_register_string('group_' . $group['ID'] . '_title', $group['title']);

$fields = acf_get_fields($group['ID']);
foreach($fields as $field) {

pll_register_string($field['key'] . '_label', $field['label']);
if (!isset($field['choices'])) {
continue;
Expand All @@ -89,19 +100,113 @@ public function register_strings()
*/
public function filter_keys($keys)
{
foreach($keys as $index=>$key) {
$field = get_field_object($key);
if (!$field) { // no ACF field
continue;
/**
* Show overlay for saving post first, if new post is created
*/

$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if (strpos($url,'from_post') !== false && strpos($url,'new_lang') !== false) {
include(__DIR__ . '/../js/saveFirstOverlay.php');
return $keys;
}

/**
* Post ID needed for get_post_meta if nested ACF
*/
global $post;


/**
* Used for Debuging - Shows all Keys
*/


// echo '<div class="field" style="margin: 50px 0; width: 80%; margin-left: 20%;">';
// echo "<h2>Passed PLL Keys</h2>";
//
// foreach($keys as $index => $item) {
//
// $fieldToSync = false;
//
// $acfSystemField = get_post_meta( $post->ID, "_".$item, true);
//
// if(get_field_object($acfSystemField)) {
// echo "<span style='color: green;'>".$item."<br></span>";
// } else {
// /**
// * Check if this is a nested ACF Field by getting the field ID as a Substring from the System Field
// */
// $acfSystemField = get_post_meta( $post->ID, "_".$item, true);
// $needle = "_field";
// $startPos = strpos($acfSystemField, $needle);
// $fieldId = substr($acfSystemField , $startPos + 1);
// if(get_field_object($fieldId)) {
// // Is nested ACF field
// echo "<span style='color: green;'>".$item."<br></span>";
// } else {
// /**
// * Check if this is a ACF System Field by getting the field ID
// */
// $acfSystemField = get_post_meta( $post->ID, "_".$item, true);
// if(get_field_object($acfSystemField)) {
// // Is nested ACF field
// echo "<span style='color: green;'>".$item."<br></span>";
// } else {
//
// // Not an ACF Field
// echo "<span style='color: red;'>" . $item . "<br></span>";
// }
// }
// }
//
// if (!$fieldToSync) {
// unset($keys[$index]);
// }
// }
//
// echo '</div>';





foreach($keys as $index => $item) {

$fieldToSync = false;

$acfSystemField = get_post_meta( $post->ID, "_".$item, true);

if(get_field_object($acfSystemField)) {
if(get_field_object($acfSystemField)["lang_sync"]) {
$fieldToSync = true;
}
} else {
/**
* Check if this is a nested ACF Field by getting the field ID as a Substring from the System Field
*/
$acfSystemField = get_post_meta( $post->ID, "_".$item, true);
$needle = "_field";
$startPos = strpos($acfSystemField, $needle);
$fieldId = substr($acfSystemField , $startPos + 1);
if(get_field_object($fieldId)) {
// Is nested ACF field
if(get_field_object($fieldId)["lang_sync"]) {
$fieldToSync = true;
}
} else {
// Not an ACF Field
continue;
}
}
// safeguard: on bulk editing, lang_sync is not set - so assume 0 to not mess up things
// see https://github.com/Mestrona/polylang-acf-sync-some-fields/issues/1
$sync = isset($field['lang_sync']) ? $field['lang_sync'] : 0;
if (!$sync) {

if (!$fieldToSync) {
unset($keys[$index]);
}
}

return $keys;

}

/**
Expand All @@ -111,25 +216,16 @@ public function filter_keys($keys)
*/
public function action_acf_create_field_options($field)
{
?>
<tr class="foo" data-field_name="<?php echo $field['key']; ?>">
<td class="label"><label>Sync Field between Languages</label></td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields[' . $field['key'] . '][lang_sync]',
'value' => isset($field['lang_sync']) ? $field['lang_sync'] : 1,
'choices' => array(
1 => __("Yes", 'acf'),
0 => __("No", 'acf'),
),
'layout' => 'horizontal',
));
?>
</td>
</tr>
<?php
//var_dump($field);
acf_render_field_setting( $field, array(
'label' => __('Sync between all languages?'),
'instructions' => '',
'type' => 'true_false',
'ui' => 1,
'name' => 'lang_sync',
'value' => isset($field['lang_sync']) ? $field['lang_sync'] : 0,
), true);

}
}

Expand Down
Loading