PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 4.0.7
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v4.0.7
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 trunk 1.0.0 2.0.0 2.0.1 2.0.2 2.0.3 3.0 3.0.1 3.0.2 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1
superb-blocks / src / admin / controllers / wizard / class-wizard-restorationpoint-controller.php
superb-blocks / src / admin / controllers / wizard Last commit date
class-wizard-controller.php 4 days ago class-wizard-restorationpoint-controller.php 4 days ago class-wizard-template-preview-controller.php 4 days ago
class-wizard-restorationpoint-controller.php
128 lines
1 <?php
2
3 namespace SuperbAddons\Admin\Controllers\Wizard;
4
5 use WP_Block_Template;
6
7 defined('ABSPATH') || exit();
8
9 class WizardRestorationPointController
10 {
11 const RESTORATION_TRANSIENT = 'superb_blocks_template_restoration';
12 const RESTORATION_CLEANUP_HOOK = 'superb_blocks_template_restoration_cleanup';
13
14 public static function Initialize()
15 {
16 add_action(self::RESTORATION_CLEANUP_HOOK, array(__CLASS__, 'RestorationPointCleanup'));
17 }
18
19 private static function GetRestorationPointsTransient()
20 {
21 return get_transient(self::RESTORATION_TRANSIENT, []);
22 }
23
24 private static function SetRestorationPointsTransient($value)
25 {
26 return set_transient(self::RESTORATION_TRANSIENT, $value, MONTH_IN_SECONDS * 2);
27 }
28
29 public static function GetThemeRestorationPoints()
30 {
31 $restoration_transient = self::GetRestorationPointsTransient();
32 if (empty($restoration_transient)) {
33 return false;
34 }
35 if (!isset($restoration_transient[get_stylesheet()])) {
36 return false;
37 }
38
39 return $restoration_transient[get_stylesheet()];
40 }
41
42 public static function GetTemplateRestorationPoint($template_id)
43 {
44 $restoration_points = self::GetThemeRestorationPoints();
45 if (!$restoration_points || empty($restoration_points)) {
46 return false;
47 }
48 if (!isset($restoration_points[$template_id])) {
49 return false;
50 }
51 return $restoration_points[$template_id];
52 }
53
54 public static function CreateTemplateRestorationPoint($template_slug, $template_type, $category = false)
55 {
56 $current_template = get_block_template(get_stylesheet() . '//' . $template_slug, $template_type);
57 if (!$current_template || !$current_template instanceof WP_Block_Template) {
58 return false;
59 }
60
61 $restoration_transient = self::GetRestorationPointsTransient();
62 if (!is_array($restoration_transient)) {
63 $restoration_transient = [];
64 }
65 $restoration_transient[get_stylesheet()] = isset($restoration_transient[get_stylesheet()]) ? $restoration_transient[get_stylesheet()] : array();
66 $restoration_transient[get_stylesheet()][$current_template->slug . "//" . $template_type . "//" . time()] = array(
67 "timestamp" => time(),
68 "type" => $template_type,
69 "slug" => $current_template->slug,
70 "content" => $current_template->content,
71 "category" => $category ? $category : $current_template->slug,
72 );
73
74 self::MaybeScheduleRestorationCleanup();
75
76 return self::SetRestorationPointsTransient($restoration_transient);
77 }
78
79 public static function RestorationPointCleanup()
80 {
81 $restoration_transient = self::GetRestorationPointsTransient();
82 if (empty($restoration_transient)) {
83 self::MaybeUnsubscribeCron();
84 return delete_transient(self::RESTORATION_TRANSIENT);
85 }
86
87 foreach ($restoration_transient as $stylesheet => $templates) {
88 foreach ($templates as $template_key => $template) {
89 if ($template['timestamp'] < strtotime('-2 month')) {
90 unset($restoration_transient[$stylesheet][$template_key]);
91 }
92 }
93
94 if (empty($restoration_transient[$stylesheet])) {
95 unset($restoration_transient[$stylesheet]);
96 }
97 }
98
99 if (empty($restoration_transient)) {
100 self::MaybeUnsubscribeCron();
101 return delete_transient(self::RESTORATION_TRANSIENT);
102 }
103
104 return self::SetRestorationPointsTransient($restoration_transient);
105 }
106
107 public static function FullRestorationCleanup()
108 {
109 self::MaybeUnsubscribeCron();
110 return delete_transient(self::RESTORATION_TRANSIENT);
111 }
112
113 private static function MaybeScheduleRestorationCleanup()
114 {
115 if (!wp_next_scheduled(self::RESTORATION_CLEANUP_HOOK)) {
116 wp_schedule_event(time(), 'weekly', self::RESTORATION_CLEANUP_HOOK);
117 }
118 }
119
120 public static function MaybeUnsubscribeCron()
121 {
122 $timestamp = wp_next_scheduled(self::RESTORATION_CLEANUP_HOOK);
123 if ($timestamp) {
124 wp_unschedule_event($timestamp, self::RESTORATION_CLEANUP_HOOK);
125 }
126 }
127 }
128