Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/bdthemes-element-pack/admin/admin-biggopti.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
namespace ElementPack;
4
+
5
+
/**
6
+
* Biggopties class
7
+
*/
8
+
class Biggopties {
9
+
10
+
private static $biggopties = [];
11
+
12
+
private static $instance;
13
+
14
+
public static function get_instance() {
15
+
if (!isset(self::$instance)) {
16
+
self::$instance = new self;
17
+
}
18
+
return self::$instance;
19
+
}
20
+
21
+
public function __construct() {
22
+
23
+
add_action('admin_notices', [$this, 'show_biggopties'], 99);
24
+
add_action('wp_ajax_element_pack_biggopti', [$this, 'dismiss']);
25
+
26
+
// AJAX endpoint to fetch API biggopties on demand (after page load)
27
+
add_action('wp_ajax_ep_fetch_api_biggopti', [$this, 'ajax_fetch_api_biggopti']);
28
+
29
+
}
30
+
31
+
/**
32
+
* Get Remote Biggopties Data from API
33
+
*
34
+
* @return array|mixed
35
+
*/
36
+
private function get_api_biggopties_data() {
37
+
38
+
// 6-hour transient cache for API response
39
+
$transient_key = 'bdt_api_biggopties';
40
+
$cached = get_transient($transient_key);
41
+
if ($cached !== false && is_array($cached)) {
42
+
return $cached;
43
+
}
44
+
45
+
// API endpoint for biggopties - you can change this to your actual endpoint
46
+
$api_url = 'https://store.bdthemes.com/api/notices/api-data-records';
47
+
48
+
$response = wp_remote_get($api_url, [
49
+
'timeout' => 30,
50
+
'headers' => [
51
+
'Accept' => 'application/json',
52
+
'X-ALLOW-KEY' => 'bdthemes',
53
+
],
54
+
]);
55
+
56
+
if (is_wp_error($response)) {
57
+
return [];
58
+
}
59
+
60
+
$response_code = wp_remote_retrieve_response_code($response);
61
+
62
+
$response_body = wp_remote_retrieve_body($response);
63
+
64
+
$biggopties = json_decode($response_body);
65
+
66
+
if( isset($biggopties->api) && isset($biggopties->api->{'element-pack'}) ) {
67
+
$data = $biggopties->api->{'element-pack'};
68
+
if (is_array($data)) {
69
+
$ttl = apply_filters('bdt_api_biggopties_cache_ttl', 6 * HOUR_IN_SECONDS);
70
+
set_transient($transient_key, $data, $ttl);
71
+
return $data;
72
+
}
73
+
}
74
+
75
+
return [];
76
+
}
77
+
78
+
/**
79
+
* Check if a biggopti should be shown based on its enabled status and date range.
80
+
*
81
+
* @param object $biggopti The biggopti data from the API.
82
+
* @return bool True if the biggopti should be shown, false otherwise.
83
+
*/
84
+
private function should_show_biggopti($biggopti) {
85
+
// Development override - set to true to bypass date checks for testing
86
+
$development_mode = false; // Set to true to bypass date checks
87
+
88
+
if ($development_mode) {
89
+
return true;
90
+
}
91
+
92
+
// Check if the biggopti is enabled
93
+
if (!isset($biggopti->is_enabled) || !$biggopti->is_enabled) {
94
+
return false;
95
+
}
96
+
97
+
// Check plugin compatibility
98
+
if (!$this->is_biggopti_compatible_with_plugin($biggopti)) {
99
+
return false;
100
+
}
101
+
102
+
// Check if the biggopti has a start date and end date
103
+
if (!isset($biggopti->start_date) || !isset($biggopti->end_date)) {
104
+
return false;
105
+
}
106
+
107
+
// Get timezone from biggopti or default to UTC
108
+
$timezone = isset($biggopti->timezone) ? $biggopti->timezone : 'UTC';
109
+
110
+
// Create DateTime objects with proper timezone (using global namespace)
111
+
$start_date = new \DateTime($biggopti->start_date, new \DateTimeZone($timezone));
112
+
$end_date = new \DateTime($biggopti->end_date, new \DateTimeZone($timezone));
113
+
$current_date = new \DateTime('now', new \DateTimeZone($timezone));
114
+
115
+
// Convert to timestamps for comparison
116
+
$start_timestamp = $start_date->getTimestamp();
117
+
$end_timestamp = $end_date->getTimestamp();
118
+
$current_timestamp = $current_date->getTimestamp();
119
+
120
+
// Check if the current date is within the start and end dates
121
+
if ($current_timestamp < $start_timestamp || $current_timestamp > $end_timestamp) {
122
+
return false;
123
+
}
124
+
125
+
// Check if no biggopti should be visible after a certain time
126
+
if (isset($biggopti->visible_after) && $biggopti->visible_after > 0) {
127
+
$visible_after_timestamp = $start_timestamp + $biggopti->visible_after;
128
+
if ($current_timestamp < $visible_after_timestamp) {
129
+
return false;
130
+
}
131
+
}
132
+
133
+
return true;
134
+
}
135
+
136
+
/**
137
+
* Check if a biggopti is compatible with the current plugin installation
138
+
*
139
+
* @param object $biggopti The biggopti data from the API.
140
+
* @return bool True if the biggopti should be shown, false otherwise.
141
+
*/
142
+
private function is_biggopti_compatible_with_plugin($biggopti) {
143
+
// Get current plugin info
144
+
$current_plugin_slug = $this->get_current_plugin_slug();
145
+
$is_pro_active = function_exists('element_pack_pro_activated') ? element_pack_pro_activated() : false;
146
+
$is_lite_active = $current_plugin_slug === 'bdthemes-element-pack-lite';
147
+
$is_pro_plugin = $current_plugin_slug === 'bdthemes-element-pack';
148
+
149
+
// Get client targets, default to ['both'] if not set or not an array
150
+
$client_targets = (isset($biggopti->client_targets) && is_array($biggopti->client_targets))
151
+
? $biggopti->client_targets
152
+
: ['both'];
153
+
154
+
// Determine if this is targeted at Pro users
155
+
$pro_targeted = in_array('pro', $client_targets, true);
156
+
157
+
// Ensure client_targets is always an array
158
+
if (!is_array($client_targets)) {
159
+
$client_targets = [$client_targets];
160
+
}
161
+
162
+
// Handle pro_targeted parameter (only for free version)
163
+
if ($pro_targeted && $is_lite_active) {
164
+
// If pro_targeted is true, only show if pro is NOT active
165
+
$should_show = !$is_pro_active;
166
+
return $should_show;
167
+
}
168
+
169
+
// Check if any of the client targets match current plugin status
170
+
foreach ($client_targets as $target) {
171
+
$target = trim($target); // Clean up any whitespace
172
+
173
+
switch ($target) {
174
+
case 'pro':
175
+
// Pro-only biggopties: show only if pro is active
176
+
if ($is_pro_active) {
177
+
return true;
178
+
}
179
+
break;
180
+
181
+
case 'free':
182
+
if ($is_lite_active) {
183
+
return true;
184
+
}
185
+
break;
186
+
}
187
+
}
188
+
189
+
return false;
190
+
}
191
+
192
+
/**
193
+
* Get current plugin slug
194
+
*
195
+
* @return string
196
+
*/
197
+
private function get_current_plugin_slug() {
198
+
// Get plugin basename from current file
199
+
$plugin_file = plugin_basename(BDTEP__FILE__);
200
+
201
+
// Extract plugin slug from basename
202
+
$plugin_slug = dirname($plugin_file);
203
+
204
+
return $plugin_slug;
205
+
}
206
+
207
+
/**
208
+
* Render API biggopti HTML
209
+
*
210
+
* @param object $biggopti
211
+
* @return string
212
+
*/
213
+
private function render_api_biggopti($biggopti) {
214
+
ob_start();
215
+
216
+
// Add custom CSS if provided
217
+
if (isset($biggopti->custom_css) && !empty($biggopti->custom_css)) {
218
+
echo '<style>' . wp_kses_post($biggopti->custom_css) . '</style>';
219
+
}
220
+
221
+
// Prepare background styles
222
+
$background_style = '';
223
+
$wrapper_classes = 'bdt-biggopti-wrapper';
224
+
225
+
if (isset($biggopti->background_color) && !empty($biggopti->background_color)) {
226
+
$background_style .= 'background-color: ' . esc_attr($biggopti->background_color) . ';';
227
+
}
228
+
229
+
if (isset($biggopti->image) && !empty($biggopti->image)) {
230
+
$background_style .= 'background-image: url(' . esc_url($biggopti->image) . ');';
231
+
$wrapper_classes .= ' has-background-image';
232
+
}
233
+
234
+
?>
235
+
<div class="<?php echo esc_attr($wrapper_classes); ?>" <?php echo $background_style ? 'style="' . $background_style . '"' : ''; ?>>
236
+
237
+
238
+
<?php $title = (isset($biggopti->title) && !empty($biggopti->title)) ? $biggopti->title : ''; ?>
239
+
240
+
<div class="bdt-api-biggopti-content">
241
+
<div class="bdt-plugin-logo-wrapper">
242
+
<img height="auto" width="40" src="<?php echo esc_url(BDTEP_ASSETS_URL); ?>images/logo.svg" alt="Element Pack Logo">
243
+
</div>
244
+
245
+
<div class="bdt-biggopti-content">
246
+
<div class="bdt-biggopti-content-inner">
247
+
<?php if (isset($biggopti->logo) && !empty($biggopti->logo)) : ?>
248
+
<div class="bdt-biggopti-logo-wrapper">
249
+
<img width="100" src="<?php echo esc_url($biggopti->logo); ?>" alt="Logo">
250
+
</div>
251
+
<?php endif; ?>
252
+
<div class="bdt-biggopti-title-description">
253
+
<?php if (isset($title) && !empty($title)) : ?>
254
+
<h2 class="bdt-biggopti-title"><?php echo wp_kses_post($title); ?></h2>
255
+
<?php endif; ?>
256
+
257
+
<?php if (isset($biggopti->content) && !empty($biggopti->content)) : ?>
258
+
<div class="bdt-biggopti-html-content">
259
+
<?php echo wp_kses_post($biggopti->content); ?>
260
+
</div>
261
+
<?php endif; ?>
262
+
</div>
263
+
</div>
264
+
265
+
<div class="bdt-biggopti-content-right">
266
+
<?php
267
+
// Only show countdown if it's enabled, has an end date, and the end date is in the future
268
+
$show_countdown = isset($biggopti->show_countdown) && $biggopti->show_countdown && isset($biggopti->end_date);
269
+
if ($show_countdown) {
270
+
$end_timestamp = strtotime($biggopti->end_date);
271
+
$current_timestamp = current_time('timestamp');
272
+
$show_countdown = $end_timestamp > $current_timestamp;
273
+
}
274
+
?>
275
+
<?php if ($show_countdown) : ?>
276
+
<div class="bdt-biggopti-countdown" data-end-date="<?php echo esc_attr($biggopti->end_date); ?>" data-timezone="<?php echo esc_attr($biggopti->timezone ? $biggopti->timezone : 'UTC'); ?>">
277
+
<div class="countdown-timer">Loading...</div>
278
+
</div>
279
+
<?php endif; ?>
280
+
281
+
<?php if (isset($biggopti->link) && !empty($biggopti->link)) : ?>
282
+
<div class="bdt-biggopti-btn">
283
+
<a href="<?php echo esc_url($biggopti->link); ?>" target="_blank">
284
+
<div class="bdt-biggopti-btn-inner">
285
+
<?php echo isset($biggopti->button_text) ? esc_html($biggopti->button_text) : 'Read More'; ?>
286
+
<span class="dashicons dashicons-arrow-right-alt"></span>
287
+
</div>
288
+
</a>
289
+
</div>
290
+
<?php endif; ?>
291
+
</div>
292
+
</div>
293
+
</div>
294
+
</div>
295
+
<?php
296
+
return ob_get_clean();
297
+
}
298
+
299
+
public static function add_biggopti($args = []) {
300
+
if (is_array($args)) {
301
+
self::$biggopties[] = $args;
302
+
}
303
+
}
304
+
305
+
/**
306
+
* AJAX: Build and return API biggopties HTML for dynamic injection
307
+
*/
308
+
public function ajax_fetch_api_biggopti() {
309
+
$nonce = isset($_POST['_wpnonce']) ? sanitize_text_field($_POST['_wpnonce']) : '';
310
+
if (!wp_verify_nonce($nonce, 'element-pack')) {
311
+
wp_send_json_error([ 'message' => 'invalid_nonce' ]);
312
+
}
313
+
314
+
if (!current_user_can('manage_options')) {
315
+
wp_send_json_error([ 'message' => 'forbidden' ]);
316
+
}
317
+
318
+
$biggopties = $this->get_api_biggopties_data();
319
+
$grouped_biggopties = [];
320
+
321
+
if (is_array($biggopties)) {
322
+
foreach ($biggopties as $index => $biggopti) {
323
+
if ($this->should_show_biggopti($biggopti)) {
324
+
$notice_class = isset($biggopti->notice_class) ? $biggopti->notice_class : 'default-' . $index;
325
+
if (!isset($grouped_biggopties[$notice_class])) {
326
+
$grouped_biggopties[$notice_class] = $biggopti;
327
+
}
328
+
}
329
+
}
330
+
}
331
+
332
+
// Build biggopties using the same pipeline as synchronous rendering
333
+
foreach ($grouped_biggopties as $notice_class => $biggopti) {
334
+
$biggopti_id = isset($biggopti->id) ? $notice_class : $biggopti->id;
335
+
336
+
self::add_biggopti([
337
+
'id' => 'api-biggopti-' . $biggopti_id,
338
+
'type' => isset($biggopti->type) ? $biggopti->type : 'info',
339
+
'category' => isset($biggopti->category) ? $biggopti->category : 'regular',
340
+
'dismissible' => true,
341
+
'html_message' => $this->render_api_biggopti($biggopti),
342
+
'dismissible-meta' => 'transient',
343
+
'dismissible-time' => isset($biggopti->end_date) ? max((new \DateTime($biggopti->end_date, new \DateTimeZone('UTC')))->getTimestamp() - time(), 0) : WEEK_IN_SECONDS,
344
+
]);
345
+
}
346
+
347
+
ob_start();
348
+
$this->show_biggopties();
349
+
$markup = ob_get_clean();
350
+
351
+
wp_send_json_success([ 'html' => $markup ]);
352
+
}
353
+
354
+
/**
355
+
* Dismiss Biggopti.
356
+
*/
357
+
public function dismiss() {
358
+
$nonce = (isset($_POST['_wpnonce'])) ? sanitize_text_field($_POST['_wpnonce']) : '';
359
+
$id = (isset($_POST['id'])) ? esc_attr($_POST['id']) : '';
360
+
$time = (isset($_POST['time'])) ? esc_attr($_POST['time']) : '';
361
+
$meta = (isset($_POST['meta'])) ? esc_attr($_POST['meta']) : '';
362
+
363
+
if ( ! wp_verify_nonce($nonce, 'element-pack') ) {
364
+
wp_send_json_error();
365
+
}
366
+
367
+
if ( ! current_user_can('manage_options') ) {
368
+
wp_send_json_error();
369
+
}
370
+
371
+
/**
372
+
* Valid inputs?
373
+
*/
374
+
if (!empty($id)) {
375
+
// Handle regular biggopti
376
+
if ('user' === $meta) {
377
+
update_user_meta(get_current_user_id(), $id, true);
378
+
} else {
379
+
set_transient($id, true, $time);
380
+
}
381
+
382
+
wp_send_json_success();
383
+
}
384
+
385
+
wp_send_json_error();
386
+
}
387
+
388
+
/**
389
+
* Biggopti Types
390
+
*/
391
+
public function show_biggopties() {
392
+
393
+
$defaults = [
394
+
'id' => '',
395
+
'type' => 'info',
396
+
'category' => 'regular',
397
+
'show_if' => true,
398
+
'title' => '',
399
+
'message' => '',
400
+
'class' => 'element-pack-biggopti',
401
+
'dismissible' => false,
402
+
'dismissible-meta' => 'transient',
403
+
'dismissible-time' => WEEK_IN_SECONDS,
404
+
'data' => '',
405
+
'action_link' => '',
406
+
];
407
+
408
+
foreach (self::$biggopties as $key => $biggopti) {
409
+
410
+
$biggopti = wp_parse_args($biggopti, $defaults);
411
+
412
+
// Check if biggopti is for White Label
413
+
if (defined('BDTEP_WL') && $biggopti['category'] === 'regular') {
414
+
continue;
415
+
}
416
+
417
+
$classes = ['biggopti'];
418
+
419
+
$classes[] = $biggopti['class'];
420
+
if (isset($biggopti['type'])) {
421
+
$classes[] = 'biggopti-' . $biggopti['type'];
422
+
}
423
+
424
+
// Is biggopti dismissible?
425
+
if (true === $biggopti['dismissible']) {
426
+
$classes[] = 'is-dismissible';
427
+
428
+
// Dismissable time.
429
+
$biggopti['data'] = ' dismissible-time=' . esc_attr($biggopti['dismissible-time']) . ' ';
430
+
}
431
+
432
+
// Biggopti ID.
433
+
$biggopti_id = 'bdt-admin-biggopti-' . $biggopti['id'];
434
+
$biggopti['id'] = $biggopti_id;
435
+
if (!isset($biggopti['id'])) {
436
+
$biggopti_id = 'bdt-admin-biggopti-' . $biggopti['id'];
437
+
$biggopti['id'] = $biggopti_id;
438
+
} else {
439
+
$biggopti_id = $biggopti['id'];
440
+
}
441
+
442
+
$biggopti['classes'] = implode(' ', $classes);
443
+
444
+
// User meta.
445
+
$biggopti['data'] .= ' dismissible-meta=' . esc_attr($biggopti['dismissible-meta']) . ' ';
446
+
if ('user' === $biggopti['dismissible-meta']) {
447
+
$expired = get_user_meta(get_current_user_id(), $biggopti_id, true);
448
+
} elseif ('transient' === $biggopti['dismissible-meta']) {
449
+
$expired = get_transient($biggopti_id);
450
+
}
451
+
452
+
// Biggopties visible after transient expire.
453
+
if (isset($biggopti['show_if'])) {
454
+
455
+
if (true === $biggopti['show_if']) {
456
+
457
+
// Is transient expired?
458
+
if (false === $expired || empty($expired)) {
459
+
self::biggopti_layout($biggopti);
460
+
}
461
+
}
462
+
} else {
463
+
464
+
// No transient biggopties.
465
+
self::biggopti_layout($biggopti);
466
+
}
467
+
}
468
+
}
469
+
470
+
/**
471
+
* New Biggopti Layout
472
+
* @param array $biggopti Biggopti biggopti_layout.
473
+
* @return void
474
+
* @since 6.11.3
475
+
*/
476
+
477
+
public static function biggopti_layout($biggopti = []) {
478
+
479
+
if( isset($biggopti['html_message']) && ! empty($biggopti['html_message']) ) {
480
+
self::new_biggopti_layout($biggopti);
481
+
return;
482
+
}
483
+
484
+
?>
485
+
<div id="<?php echo esc_attr($biggopti['id']); ?>" class="<?php echo esc_attr($biggopti['classes']); ?>" <?php echo esc_attr($biggopti['data']); ?>>
486
+
<div class="bdt-biggopti-wrapper">
487
+
<div class="bdt-biggopti-icon-wrapper">
488
+
<img height="25" width="25" src="<?php echo esc_url (BDTEP_ASSETS_URL ); ?>images/logo.svg">
489
+
</div>
490
+
491
+
<div class="bdt-biggopti-content">
492
+
<?php if (isset($biggopti['title']) && !empty($biggopti['title'])) : ?>
493
+
<h2 class="bdt-biggopti-title"><?php echo wp_kses_post($biggopti['title']); ?></h2>
494
+
<?php endif; ?>
495
+
496
+
<p class="bdt-biggopti-text"><?php echo wp_kses_post($biggopti['message']); ?></p>
497
+
498
+
<?php if (isset($biggopti['action_link']) && !empty($biggopti['action_link'])) : ?>
499
+
<div class="bdt-biggopti-btn">
500
+
<a href="#">Renew Now</a>
501
+
</div>
502
+
<?php endif; ?>
503
+
</div>
504
+
</div>
505
+
</div>
506
+
<?php
507
+
}
508
+
509
+
public static function new_biggopti_layout( $biggopti = [] ) {
510
+
?>
511
+
<div id="<?php echo esc_attr( $biggopti['id'] ); ?>" class="<?php echo esc_attr( $biggopti['classes'] ); ?>" <?php echo esc_attr( $biggopti['data'] ); ?>>
512
+
<?php
513
+
echo wp_kses_post( $biggopti['html_message'] );
514
+
?>
515
+
</div>
516
+
517
+
<?php
518
+
}
519
+
}
520
+
521
+
Biggopties::get_instance();
522
+