PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.3.3
Gallery : FooGallery v3.3.3
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / compatibility / class-foogallery-divi-compatibility.php
foogallery / includes / compatibility Last commit date
divi 2 days ago elementor 2 days ago class-autoptimize-compatibility.php 2 days ago class-elasticpress-compatibility.php 2 days ago class-elementor-compatibility.php 2 days ago class-foobox-compatibility.php 2 days ago class-foogallery-breeze-compatibility.php 2 days ago class-foogallery-compatibility.php 2 days ago class-foogallery-divi-compatibility.php 2 days ago class-foovideo-compatibility.php 2 days ago class-jetpack-compatibility.php 2 days ago class-polylang-compatibility.php 2 days ago class-responsive-lightbox-dfactory-compatibility.php 2 days ago class-speed-optimizer-compatibility.php 2 days ago class-wpoptimize-compatibility.php 2 days ago class-wprocket-compatibility.php 2 days ago view-foovideo-offer.php 2 days ago
class-foogallery-divi-compatibility.php
205 lines
1 <?php
2 /**
3 * Divi compatibility.
4 *
5 * @package FooGallery
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 if ( ! class_exists( 'FooGallery_Divi_Compatibility' ) ) {
13
14 /**
15 * Integrates FooGallery with the Divi Builder.
16 */
17 class FooGallery_Divi_Compatibility {
18
19 /**
20 * Whether the Divi module has been registered during this request.
21 *
22 * @var bool
23 */
24 private $module_registered = false;
25
26 /**
27 * Number of module shortcodes rendered without Divi's legacy PHP runtime.
28 *
29 * @var int
30 */
31 private $fallback_render_count = 0;
32
33 /**
34 * Register Divi compatibility hooks.
35 */
36 public function __construct() {
37 add_action( 'init', array( $this, 'register_shortcode_fallback' ), 5 );
38 add_action( 'et_builder_ready', array( $this, 'register_module' ) );
39 add_action( 'et_builder_module_lazy_shortcodes_registered', array( $this, 'register_module' ) );
40 add_action( 'divi_visual_builder_assets_after_enqueue_scripts', array( $this, 'enqueue_assets' ) );
41 add_action( 'et_fb_enqueue_assets', array( $this, 'enqueue_assets' ) );
42 add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_classic_builder_assets' ), 20 );
43
44 add_filter( 'foogallery_find_galleries_in_post', array( $this, 'find_galleries_in_divi_modules' ), 10, 2 );
45 }
46
47 /**
48 * Ensure Divi's preview request recognizes the module shortcode.
49 *
50 * Divi's lazy shortcode manager persists third-party module slugs when a
51 * plugin is activated. A module added by a plugin update is not present in
52 * that cache yet, so Divi 5 can otherwise leave the shortcode unprocessed
53 * in its hidden preview document.
54 *
55 * @return void
56 */
57 public function register_shortcode_fallback() {
58 if ( ! shortcode_exists( 'foogallery_divi_gallery' ) ) {
59 add_shortcode( 'foogallery_divi_gallery', array( $this, 'render_module_shortcode_fallback' ) );
60 }
61 }
62
63 /**
64 * Load and delegate to the Divi module if its lazy loader missed the slug.
65 *
66 * @param array|string $attributes Shortcode attributes.
67 * @param string|null $content Enclosed shortcode content.
68 * @param string $tag Shortcode tag.
69 *
70 * @return string
71 */
72 public function render_module_shortcode_fallback( $attributes, $content = null, $tag = 'foogallery_divi_gallery' ) {
73 $content = is_string( $content ) ? $content : '';
74
75 if ( ! class_exists( 'ET_Builder_Module' ) && function_exists( 'et_builder_add_main_elements' ) ) {
76 et_builder_add_main_elements();
77 }
78
79 if ( class_exists( 'ET_Builder_Module' ) ) {
80 require_once FOOGALLERY_PATH . 'includes/compatibility/divi/class-foogallery-divi-module.php';
81
82 if ( class_exists( 'FooGallery_Divi_Module' ) ) {
83 $module = new FooGallery_Divi_Module();
84 $this->module_registered = true;
85
86 return $module->_render( $attributes, $content, $tag );
87 }
88 }
89
90 $attributes = shortcode_atts(
91 array(
92 'gallery_id' => 0,
93 ),
94 is_array( $attributes ) ? $attributes : array(),
95 $tag
96 );
97
98 $gallery_id = absint( $attributes['gallery_id'] );
99
100 if ( $gallery_id > 0 ) {
101 ob_start();
102 foogallery_render_gallery( $gallery_id );
103 $gallery_output = ob_get_clean();
104 } else {
105 $gallery_output = '<p>' . esc_html__( 'Please select a gallery to display.', 'foogallery' ) . '</p>';
106 }
107
108 $order_class = 'foogallery_divi_gallery_' . $this->fallback_render_count;
109 ++$this->fallback_render_count;
110
111 return sprintf(
112 '<div class="et_pb_module et_d4_element foogallery_divi_gallery %1$s">%2$s</div>',
113 esc_attr( $order_class ),
114 $gallery_output // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- FooGallery renders escaped gallery markup.
115 );
116 }
117
118 /**
119 * Register the FooGallery module after Divi has loaded its module API.
120 *
121 * @return void
122 */
123 public function register_module() {
124 if ( $this->module_registered || ! class_exists( 'ET_Builder_Module' ) ) {
125 return;
126 }
127
128 require_once FOOGALLERY_PATH . 'includes/compatibility/divi/class-foogallery-divi-module.php';
129
130 if ( class_exists( 'FooGallery_Divi_Module' ) ) {
131 new FooGallery_Divi_Module();
132 $this->module_registered = true;
133 }
134 }
135
136 /**
137 * Load FooGallery assets before Divi renders shortcode or module previews.
138 *
139 * @return void
140 */
141 public function enqueue_assets() {
142 foogallery_enqueue_core_gallery_template_script();
143 foogallery_enqueue_core_gallery_template_style();
144
145 wp_enqueue_script(
146 'foogallery-divi',
147 FOOGALLERY_URL . 'js/admin-foogallery-divi.js',
148 array( FooGallery_Delayed_Runtime_Loader::READY_HANDLE ),
149 FOOGALLERY_VERSION,
150 true
151 );
152 }
153
154 /**
155 * Load assets through the standard frontend queue in the classic builder.
156 *
157 * @return void
158 */
159 public function maybe_enqueue_classic_builder_assets() {
160 if ( function_exists( 'et_core_is_fb_enabled' ) && et_core_is_fb_enabled() ) {
161 $this->enqueue_assets();
162 }
163 }
164
165 /**
166 * Find gallery IDs selected in FooGallery Divi modules.
167 *
168 * This augments the normal FooGallery shortcode scan so the existing post
169 * usage metadata remains correct when a gallery is inserted as a module.
170 *
171 * @param int[] $gallery_ids Gallery IDs already found in the post.
172 * @param WP_Post $post Post being scanned.
173 *
174 * @return int[]
175 */
176 public function find_galleries_in_divi_modules( $gallery_ids, $post ) {
177 if ( ! is_array( $gallery_ids ) ) {
178 $gallery_ids = array();
179 }
180
181 if ( ! is_object( $post ) || empty( $post->post_content ) || false === strpos( $post->post_content, '[foogallery_divi_gallery' ) ) {
182 return $gallery_ids;
183 }
184
185 $pattern = get_shortcode_regex( array( 'foogallery_divi_gallery' ) );
186
187 if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) ) {
188 foreach ( $matches[3] as $attribute_string ) {
189 $attributes = shortcode_parse_atts( $attribute_string );
190
191 if ( is_array( $attributes ) && ! empty( $attributes['gallery_id'] ) ) {
192 $gallery_id = absint( $attributes['gallery_id'] );
193
194 if ( $gallery_id > 0 ) {
195 $gallery_ids[] = $gallery_id;
196 }
197 }
198 }
199 }
200
201 return array_values( array_unique( array_map( 'absint', $gallery_ids ) ) );
202 }
203 }
204 }
205