1<?php
2/**
3 * Plugin Name: Polylang/WPBakery image filter fix
4 * Description: A plugin that fixes the disappearing images issue when using WPBakery and Polylang together and NOT translating the Media Library
5 * Version: 1.0.0
6 * Author: ellmann creative
7 */
8
9if (!function_exists('is_plugin_active')) { include_once(ABSPATH . 'wp-admin/includes/plugin.php'); }
10add_action('init', function(){
11 $selfDisableFunction = function($s){
12 deactivate_plugins( plugin_basename( __FILE__ ) );
13 if ( isset( $_GET['activate'] ) )
14 unset( $_GET['activate'] );
15
16 define("ec_wpbakery_polylang_fix_notice_content", $s);
17 add_action( 'admin_notices', function(){
18 echo "<div class=\"notice-info is-dismissible\"><p><strong>Polylang/WPBakery image filter fix</strong>: " .
19 ec_wpbakery_polylang_fix_notice_content . " We're disabling the plugin.</p></div>";
20 });
21 };
22
23 if ( !is_plugin_active('polylang/polylang.php') ){
24 $selfDisableFunction("it seems that Polylang is not present or enabled in your installation.");
25 return; // Polylang not present
26 }
27
28 # Polylang is present and active, then
29 if ( get_option('polylang')['media_support'] !== false ){
30 $selfDisableFunction("your Polylang configuration seems to be just fine (or else you're experiencing a different bug).");
31 return; // presumably works correctly?
32 }
33
34 if ( !function_exists('icl_object_id') ){
35 $selfDisableFunction("looks like the problematic code was removed from your version of Polylang.");
36 return; // presumably, the problematic function was removed?
37 }
38
39 # okay, Polylang is present, active, and NOT handling the Media Library
40 # ... so, let's test if it's breaking it instead?
41 $attachments = new WP_Query( ['post_type' => 'attachment', 'posts_per_page'=> 1, 'post_status' => 'any'] );
42 if ( !$attachments->have_posts() )
43 return; // no media present, so we don't have to bother (but don't disable)
44
45 # we have media; time to test the first one
46 $testPostID = $attachments->posts[0]->ID;
47 $filteredPostID = icl_object_id($testPostID, 'post');
48
49 if ( $testPostID != $filteredPostID || $filteredPostID === null ) {
50 # the bug was triggered!
51 if ( !is_admin() ){
52 # fix it (in the front-end)
53 remove_filter('wpml_object_id', 'icl_object_id', 10, 4);
54 add_filter( 'wpml_object_id', 'icl_object_id_patched', 10, 4);
55 }
56 } else if ( is_admin() ) {
57 # the bug was NOT triggered, so we can display a notification and disable ourselves, actually
58 $selfDisableFunction("it looks like your version of Polylang isn't affected by the bug this patch aims to fix.");
59 }
60});
61
62function icl_object_id_patched( $id, $type = 'post', $return_original_if_missing = false, $lang = '' ) {
63 $lang = $lang ? $lang : pll_current_language();
64
65 if ( 'nav_menu' === $type ) {
66 $theme = get_option( 'stylesheet' );
67 if ( isset( PLL()->options['nav_menus'][ $theme ] ) ) {
68 foreach ( PLL()->options['nav_menus'][ $theme ] as $menu ) {
69 if ( array_search( $id, $menu ) && ! empty( $menu[ $lang ] ) ) {
70 $tr_id = $menu[ $lang ];
71 break;
72 }
73 }
74 }
75 } elseif ( $pll_type = ( 'post' === $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' === $type || pll_is_translated_taxonomy( $type ) ? 'term' : false ) ) {
76 $tr_id = PLL()->model->$pll_type->get_translation( $id, $lang );
77 }
78
79 # patch
80 if ( 'attachment' === get_post_type( $id )
81 && get_option('polylang')['media_support'] === false ){
82 $return_original_if_missing = true;
83 }
84 # /patch
85
86 return ! empty( $tr_id ) ? $tr_id : ( $return_original_if_missing ? $id : null );
87}
88
89