PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / bulk / sync.php

sync.php in Media Cloud Sync 1.3.11, at includes/bulk/sync.php

164 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3 defined('ABSPATH') || exit;
4
5 class Sync {
6 protected static $instance = null;
7
8 private $syncClasses = [
9 'SyncToCloud'
10 ];
11
12 /**
13 * Constructor method.
14 */
15 public function __construct() {
16 // Initialize sync classes
17 foreach ($this->syncClasses as $class) {
18 $class_with_namespace = __NAMESPACE__.'\\'. trim($class);
19 if(class_exists($class_with_namespace)) {
20 $class_with_namespace::instance();
21 }
22 }
23 }
24
25
26 /**
27 * Add new sync classes to the list of sync classes.
28 *
29 * @param array $classes List of new sync classes to add.
30 *
31 * @return void
32 */
33 public function add_sync_classes($classes = []) {
34 // Only add classes that are not already in syncClasses
35 $new_classes = array_diff($classes, $this->syncClasses);
36
37 // Merge new classes
38 $this->syncClasses = array_merge($this->syncClasses, $new_classes);
39
40 // Initialize new classes
41 foreach ($new_classes as $class) {
42 $class_with_namespace = __NAMESPACE__ . '\\' . trim($class);
43 if (class_exists($class_with_namespace)) {
44 $class_with_namespace::instance();
45 }
46 }
47 }
48
49
50 /**
51 * Gets the status of the sync.
52 *
53 * @return array An associative array with the action as key and the status
54 * as value.
55 */
56 public function get_status() {
57 $status = [];
58 foreach ($this->syncClasses as $class) {
59 $class_with_namespace = __NAMESPACE__.'\\'. trim($class);
60 if(!class_exists($class_with_namespace)) continue;
61 $instance = $class_with_namespace::instance();
62 $action = $instance->get_action();
63 $status[$action] = $instance->get_status();
64 }
65 return $status;
66 }
67
68
69 /**
70 * Starts the sync process.
71 *
72 */
73 public function start( $action ) {
74 $action_class = $this->get_class_by_action($action);
75
76 if($action_class) {
77 $action_class->start();
78 return $action_class->get_status();
79 } else {
80 /* translators: %s: action name */
81 throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action));
82 }
83 }
84
85 /**
86 * Pauses the sync process.
87 *
88 */
89 public function pause( $action ) {
90 $action_class = $this->get_class_by_action($action);
91
92 if($action_class) {
93 $action_class->pause();
94 return $action_class->get_status();
95 } else {
96 throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action));
97 }
98 }
99
100
101 /**
102 * Resumes the sync process.
103 *
104 */
105 public function resume( $action ) {
106 $action_class = $this->get_class_by_action($action);
107
108 if($action_class) {
109 $action_class->resume();
110 return $action_class->get_status();
111 } else {
112 throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action));
113 }
114 }
115
116
117 /**
118 * Stops the sync process.
119 *
120 */
121 public function stop( $action ) {
122 $action_class = $this->get_class_by_action($action);
123
124 if($action_class) {
125 $action_class->stop();
126 return $action_class->get_status();
127 } else {
128 throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action));
129 }
130 }
131
132
133 /**
134 * Returns the class associated with the given action.
135 *
136 * @param string $action The action to search for.
137 *
138 * @return string|null The class associated with the action, or null if not found.
139 */
140 public function get_class_by_action( $action ) {
141 foreach ($this->syncClasses as $class) {
142 $class_with_namespace = __NAMESPACE__.'\\'. trim($class);
143 if(!class_exists($class_with_namespace)) continue;
144
145 if($class_with_namespace::instance()->get_action() === $action) {
146 return $class_with_namespace::instance();
147 }
148 }
149
150 return false;
151 }
152
153
154 /**
155 * Retrieves the singleton instance of the class.
156 *
157 * @return self
158 */
159 public static function instance() {
160 if (!self::$instance) self::$instance = new self();
161 return self::$instance;
162 }
163 }
164