PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.2
WP-Stateless – Google Cloud Storage v3.2.2
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / sync / class-non-library-sync.php

class-non-library-sync.php in WP-Stateless – Google Cloud Storage 3.2.2, at lib/classes/sync/class-non-library-sync.php

183 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace wpCloud\StatelessMedia\Sync;
4
5 use wpCloud\StatelessMedia\FatalException;
6 use wpCloud\StatelessMedia\Singleton;
7 use wpCloud\StatelessMedia\UnprocessableException;
8
9 class NonLibrarySync extends BackgroundSync {
10
11 // Make it singleton
12 use Singleton;
13
14 /**
15 * Unique action
16 */
17 protected $action = 'wps_bg_non_library_sync';
18
19 /**
20 * Items holder
21 */
22 private $items = [];
23
24 /**
25 * Extended construct
26 */
27 public function __construct() {
28 $this->items = array_filter(array_unique(apply_filters('sm:sync::nonMediaFiles', [])));
29 parent::__construct();
30 }
31
32 /**
33 * Get sync name
34 *
35 * @return string
36 */
37 public function get_name() {
38 return __('Compatibility Files <span class="label">Beta</span>', ud_get_stateless_media()->domain);
39 }
40
41 /**
42 * Sync helper window
43 *
44 * @return HelperWindow
45 */
46 public function get_helper_window() {
47 return new HelperWindow(
48 __('What are Compatibility Files?', ud_get_stateless_media()->domain),
49 __('All kind of files that were created by themes and plugins in custom folders out of standard Media Library, and that WP-Stateless has a Compatibility Support for. Limit and Sorting is not supported.', ud_get_stateless_media()->domain)
50 );
51 }
52
53 /**
54 * Start the process
55 *
56 * @return bool
57 */
58 public function start() {
59 try {
60 if ($this->is_process_running()) throw new UnprocessableException(__('Process already running', ud_get_stateless_media()->domain));
61
62 // Make sure there is no orphaned data and state
63 delete_site_option($this->get_stopped_option_key());
64 $this->clear_process_meta();
65
66 $chunks = array_chunk($this->items, $this->get_max_batch_size());
67 if (!empty($chunks)) {
68 foreach ($chunks as $chunk) {
69 foreach ($chunk as $item) {
70 $this->push_to_queue($item);
71 }
72
73 $this->save();
74 }
75 }
76
77 $this->dispatch();
78
79 $this->save_process_meta([
80 'starttime' => current_time('timestamp')
81 ]);
82 $this->log('Started');
83 return true;
84 } catch (UnprocessableException $e) {
85 $this->log(sprintf(__('Could not start the new process: %s'), $e->getMessage()));
86 return true;
87 } catch (\Throwable $e) {
88 $this->log(sprintf(__('Could not start the process due to the error: %s'), $e->getMessage()));
89 $this->stop();
90 return false;
91 }
92 }
93
94 /**
95 * Process one item from the queue
96 */
97 protected function task($item) {
98 try {
99 parent::before_task($item);
100
101 timer_start();
102
103 if (ud_get_stateless_media()->is_connected_to_gs() !== true) {
104 throw new FatalException(__('Not connected to GCS', ud_get_stateless_media()->domain));
105 }
106
107 if (is_multisite() && ($blog_id = get_current_blog_id()) != 1) {
108 switch_to_blog(1);
109 $upload_dir = wp_upload_dir();
110 switch_to_blog($blog_id);
111 } else {
112 $upload_dir = wp_upload_dir();
113 }
114
115 $file_path = trim($item, '/');
116 $fullsizepath = $upload_dir['basedir'] . '/' . $file_path;
117
118 do_action('sm:sync::syncFile', $file_path, $fullsizepath, true, ['remove_from_queue' => true, 'manual_sync' => true]);
119
120 $this->log(sprintf(__('%1$s (ID %2$s) was successfully synchronised in %3$s seconds.', ud_get_stateless_media()->domain), esc_html(get_the_title($file_path)), $file_path, timer_stop()));
121
122 parent::task($item);
123 return false;
124 } catch (FatalException $e) {
125 $this->log("Stopped due to error - {$e->getMessage()}");
126 $this->stop();
127 return false;
128 } catch (UnprocessableException $e) {
129 $this->log($e->getMessage());
130 return false;
131 } catch (\Throwable $e) {
132 $this->log("Stopped due to error - {$e->getMessage()}");
133 $this->stop();
134 return false;
135 }
136 }
137
138 /**
139 * Thing to do in complete
140 */
141 protected function complete() {
142 parent::complete();
143
144 // @todo do something when complete
145 $this->log("Complete");
146 }
147
148 /**
149 * Notice if process seemed to be stuck
150 *
151 * @return string|false
152 */
153 public function get_process_notice() {
154 $notices = parent::get_process_notice();
155 $last = intval($this->get_process_meta('last_at'));
156 if (!$last) {
157 $last = intval($this->get_process_meta('starttime'));
158 if (!$last) return $notices;
159 }
160
161 if (!property_exists($this, 'cron_interval')) return $notices;
162
163 $waiting = current_time('timestamp') - $last;
164 if ($waiting < 5 * MINUTE_IN_SECONDS * $this->cron_interval) return $notices;
165
166 $notices[] = sprintf(__('This process takes longer than it should. Please, make sure loopback connections and WP Cron are enabled and working, or try restarting the process.', ud_get_stateless_media()->domain));
167 return $notices;
168 }
169
170 /**
171 * Get count of items
172 *
173 * @return int
174 */
175 public function get_total_items() {
176 return count($this->items);
177 }
178
179 public function extend_queue() {
180 // Not needed for this kind of sync
181 }
182 }
183