PluginProbe
Advanced Views – Display Framework for Gutenberg & Elementor / trunk
Advanced Views – Display Framework for Gutenberg & Elementor vtrunk
3.9.4 3.9.5 3.9.6 3.9.3 3.9.2 3.9.1 3.9.0 3.8.11 3.8.9 1.4.10 1.5.0 1.5.10 1.6.0 1.6.10 1.6.16 1.7.0 1.7.10 1.7.11 1.7.12 1.7.13 1.7.14 1.7.15 1.7.16 1.7.19 1.7.20 All 129 releases
acf-views / src / Plugin / Plugin_Environment.php

Plugin_Environment.php in Advanced Views – Display Framework for Gutenberg & Elementor trunk, at src/Plugin/Plugin_Environment.php

182 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Org\Wplake\Advanced_Views\Plugin;
6
7 defined( 'ABSPATH' ) || exit;
8
9 use Org\Wplake\Advanced_Views\Cpt\Base\Cpt_Data_Storage\Cpt_Settings_Storage;
10 use Org\Wplake\Advanced_Views\Cpt\Base\Cpt_Data_Storage\File_System;
11 use Org\Wplake\Advanced_Views\Cpt\Base\Cpt_Data_Storage\File_System_Loader;
12 use Org\Wplake\Advanced_Views\Cpt\Post_Selections\Cpt\Table\Post_Selections_Pre_Built_Tab;
13 use Org\Wplake\Advanced_Views\Cpt\Template\Templates_Environment;
14 use Org\Wplake\Advanced_Views\Plugin\Automated_Reports\State_Report;
15 use Org\Wplake\Advanced_Views\Plugin\Automated_Reports\Usage_Report;
16 use Org\Wplake\Advanced_Views\Plugin\Base\Hooks_Interface;
17 use Org\Wplake\Advanced_Views\Plugin\Settings\Options_Storage;
18 use Org\Wplake\Advanced_Views\Plugin\Settings\Settings_Storage;
19 use Org\Wplake\Advanced_Views\Plugin\Utils\Route_Detector;
20
21 final class Plugin_Environment implements Hooks_Interface {
22 const STARTER_SELECTION_ID = 'card_6a479135a1a81';
23 const STARTER_LAYOUT_ID = 'view_6a47911f78eb0';
24
25 protected const TRUE_TRANSIENT_VALUE = '1';
26
27 private Templates_Environment $template_engines;
28 private State_Report $state_report;
29 private Usage_Report $usage_report;
30 private Settings_Storage $settings;
31 private Plugin $plugin;
32 private Post_Selections_Pre_Built_Tab $selections_pre_built_tab;
33 /**
34 * @var File_System[]
35 */
36 private array $file_systems;
37 /**
38 * @var Cpt_Settings_Storage[]
39 */
40 private array $storages;
41
42 /**
43 * @param File_System[] $file_systems
44 * @param Cpt_Settings_Storage[] $storages
45 */
46 public function __construct(
47 Templates_Environment $template_engines,
48 State_Report $state_report,
49 Usage_Report $usage_report,
50 Settings_Storage $settings,
51 Plugin $plugin,
52 Post_Selections_Pre_Built_Tab $selections_pre_built_tab,
53 array $file_systems,
54 array $storages
55 ) {
56 $this->template_engines = $template_engines;
57 $this->state_report = $state_report;
58 $this->usage_report = $usage_report;
59 $this->settings = $settings;
60 $this->plugin = $plugin;
61 $this->selections_pre_built_tab = $selections_pre_built_tab;
62
63 $this->file_systems = $file_systems;
64 $this->storages = $storages;
65 }
66
67 public function set_hooks( Route_Detector $route_detector ): void {
68 if ( $route_detector->is_admin_route() &&
69 $route_detector->is_complete_cycle_request() ) {
70 $this->process_transient_jobs();
71 }
72 }
73
74 public function prepare_environment(): void {
75 $is_initial_setup = $this->set_initial_plugin_version();
76 $this->template_engines->create_templates_dir();
77
78 $this->state_report->plugin_activated();
79
80 if ( $is_initial_setup ) {
81 // we cannot listen to any hooks right here,
82 // so have to setup transients - https://developer.wordpress.org/reference/functions/register_activation_hook/.
83 Options_Storage::set_transient(
84 Options_Storage::TRANSIENT_FIRST_INSTALLATION,
85 self::TRUE_TRANSIENT_VALUE,
86 WEEK_IN_SECONDS
87 );
88 }
89 }
90
91 public function clean_environment(): void {
92 $this->state_report->plugin_deactivated();
93 // un_schedule in any case, as could be scheduled before disabling the automatic reports.
94 $this->usage_report->unschedule();
95
96 $this->template_engines->remove_templates_dir();
97
98 // do not check for a security token, as the deactivation plugin link contains it,
99 // and WP already has checked it.
100
101 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
102 $is_delete_data = key_exists( 'advanced-views-delete-data', $_GET ) &&
103 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
104 'yes' === $_GET['advanced-views-delete-data'];
105
106 if ( $is_delete_data ) {
107 $this->delete_data();
108 }
109 }
110
111 protected function process_transient_jobs(): void {
112 $transient_jobs = $this->get_transient_jobs();
113
114 foreach ( $transient_jobs as $transient_name => $job ) {
115 if ( self::TRUE_TRANSIENT_VALUE === Options_Storage::get_transient( $transient_name ) ) {
116 $job();
117 }
118 }
119 }
120
121 /**
122 * @return array<string, callable():void>
123 */
124 protected function get_transient_jobs(): array {
125 return array(
126 Options_Storage::TRANSIENT_FIRST_INSTALLATION => function () {
127 File_System_Loader::instance()
128 ->add_loaded_callback(
129 function () {
130 $this->prepare_first_installation();
131
132 Options_Storage::delete_transient( Options_Storage::TRANSIENT_FIRST_INSTALLATION );
133 }
134 );
135 },
136 );
137 }
138
139 protected function prepare_first_installation(): void {
140 // item is in the 'pre_built' folder.
141 $this->selections_pre_built_tab->import_cpt_data_with_all_related_items( self::STARTER_SELECTION_ID );
142 }
143
144 /**
145 * Sets the plugin version in the database if there is no version there.
146 * Otherwise, we keep the db version as is, for the version migration code.
147 */
148 protected function set_initial_plugin_version(): bool {
149 $db_plugin_version = $this->settings->get_version();
150 $is_db_version_unset = '' === $db_plugin_version;
151
152 if ( $is_db_version_unset ) {
153 $code_plugin_version = $this->plugin->get_version();
154
155 $this->settings->set_version( $code_plugin_version );
156 $this->settings->save();
157
158 return true;
159 }
160
161 return false;
162 }
163
164 protected function delete_data(): void {
165 foreach ( $this->storages as $storage ) {
166 $storage->delete_all_items();
167 }
168
169 foreach ( $this->file_systems as $file_system ) {
170 if ( $file_system->is_active() ) {
171 $base_folder = $file_system->get_base_folder();
172
173 $file_system->get_wp_filesystem()
174 ->rmdir( $base_folder, true );
175 }
176 }
177
178 Options_Storage::delete_all_options();
179 Options_Storage::delete_all_transients();
180 }
181 }
182