-
Notifications
You must be signed in to change notification settings - Fork 1
/
kntnt-pll-bb-plugin-compatibility.php
69 lines (53 loc) · 2.29 KB
/
kntnt-pll-bb-plugin-compatibility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* @wordpress-plugin
* Plugin Name: Polylang and Beaver Builder Page Builder compatibility plugin
* Plugin URI: https://www.kntnt.com/
* Description: Makes Polylang compatible with Beaver Builder Page Builder.
* Version: 1.1.1
* Author: Thomas Barregren
* Author URI: https://www.kntnt.com/
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
namespace Kntnt\PLL_BB_Plugin_Compatibility;
defined( 'ABSPATH' ) && new Plugin;
final class Plugin {
public function __construct() {
add_action( 'plugins_loaded', [ $this, 'run' ] );
}
public function run() {
if ( class_exists( 'FLBuilderLoader' ) && class_exists( 'PLL_FLBuilder' ) ) {
add_filter( 'fl_builder_loop_query_args', [ $this, 'fl_builder_loop_query_args' ] );
add_filter( 'rewrite_rules_array', [ $this, 'rewrite_rules_array' ] );
};
}
public function fl_builder_loop_query_args( $args ) {
$args['lang'] = pll_current_language();
return $args;
}
// Beaver Builder add its rewrite rules to $wp_rewrite->extra_rules_top
// which isn't processed by PLL_Links_Directory->rewrite_rules(). This
// function extracts the extra rules added to the top and process them
// the same way as Polylang does.
public function rewrite_rules_array( $rules ) {
global $wp_rewrite;
$newrules = [];
$languages = PLL()->model->get_languages_list( [ 'fields' => 'slug' ] );
if ( PLL()->options['hide_default'] ) {
$languages = array_diff( $languages, [ PLL()->options['default_lang'] ] );
}
if ( ! empty( $languages ) ) {
$slug = $wp_rewrite->root . ( PLL()->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')/';
}
foreach ( $rules as $key => $rule ) {
$newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace(
[ '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ],
[ '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ],
$rule
);
$newrules[ $key ] = $rule;
}
return $newrules;
}
}