class-wizard-controller.php
1 year ago
class-wizard-restorationpoint-controller.php
1 year ago
class-wizard-template-preview-controller.php
1 year ago
class-wizard-restorationpoint-controller.php
124 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) |
| 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 | $restoration_transient[get_stylesheet()] ??= array(); |
| 63 | $restoration_transient[get_stylesheet()][$current_template->slug . "//" . $template_type . "//" . time()] = array( |
| 64 | "timestamp" => time(), |
| 65 | "type" => $template_type, |
| 66 | "slug" => $current_template->slug, |
| 67 | "content" => $current_template->content |
| 68 | ); |
| 69 | |
| 70 | self::MaybeScheduleRestorationCleanup(); |
| 71 | |
| 72 | return self::SetRestorationPointsTransient($restoration_transient); |
| 73 | } |
| 74 | |
| 75 | public static function RestorationPointCleanup() |
| 76 | { |
| 77 | $restoration_transient = self::GetRestorationPointsTransient(); |
| 78 | if (empty($restoration_transient)) { |
| 79 | self::MaybeUnsubscribeCron(); |
| 80 | return delete_transient(self::RESTORATION_TRANSIENT); |
| 81 | } |
| 82 | |
| 83 | foreach ($restoration_transient as $stylesheet => $templates) { |
| 84 | foreach ($templates as $template_key => $template) { |
| 85 | if ($template['timestamp'] < strtotime('-2 month')) { |
| 86 | unset($restoration_transient[$stylesheet][$template_key]); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (empty($restoration_transient[$stylesheet])) { |
| 91 | unset($restoration_transient[$stylesheet]); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (empty($restoration_transient)) { |
| 96 | self::MaybeUnsubscribeCron(); |
| 97 | return delete_transient(self::RESTORATION_TRANSIENT); |
| 98 | } |
| 99 | |
| 100 | return self::SetRestorationPointsTransient($restoration_transient); |
| 101 | } |
| 102 | |
| 103 | public static function FullRestorationCleanup() |
| 104 | { |
| 105 | self::MaybeUnsubscribeCron(); |
| 106 | return delete_transient(self::RESTORATION_TRANSIENT); |
| 107 | } |
| 108 | |
| 109 | private static function MaybeScheduleRestorationCleanup() |
| 110 | { |
| 111 | if (!wp_next_scheduled(self::RESTORATION_CLEANUP_HOOK)) { |
| 112 | wp_schedule_event(time(), 'weekly', self::RESTORATION_CLEANUP_HOOK); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public static function MaybeUnsubscribeCron() |
| 117 | { |
| 118 | $timestamp = wp_next_scheduled(self::RESTORATION_CLEANUP_HOOK); |
| 119 | if ($timestamp) { |
| 120 | wp_unschedule_event($timestamp, self::RESTORATION_CLEANUP_HOOK); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 |