PluginProbe
Media Cloud Sync / 1.3.0
Media Cloud Sync v1.3.0
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.0, at includes/bulk/sync.php

142 lines 3.8 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 private static $instance = null;
7 private $token;
8
9 private $syncClasses = [
10 'SyncToCloud'
11 ];
12
13 /**
14 * Constructor method.
15 */
16 public function __construct() {
17 $this->token = WPMCS_TOKEN;
18
19 // Initialize sync classes
20 foreach ($this->syncClasses as $class) {
21 $class_with_namespace = __NAMESPACE__.'\\'. trim($class);
22 if(class_exists($class_with_namespace)) {
23 $class_with_namespace::instance();
24 }
25 }
26 }
27
28 /**
29 * Gets the status of the sync.
30 *
31 * @return array An associative array with the action as key and the status
32 * as value.
33 */
34 public function get_status() {
35 $status = [];
36 foreach ($this->syncClasses as $class) {
37 $class_with_namespace = __NAMESPACE__.'\\'. trim($class);
38 if(!class_exists($class_with_namespace)) continue;
39 $instance = $class_with_namespace::instance();
40 $action = $instance->get_action();
41 $status[$action] = $instance->get_status();
42 }
43 return $status;
44 }
45
46
47 /**
48 * Starts the sync process.
49 *
50 */
51 public function start( $action ) {
52 $action_class = $this->get_class_by_action($action);
53
54 if($action_class) {
55 $action_class::instance()->start();
56 return $action_class::instance()->get_status();
57 } else {
58 /* translators: %s: action name */
59 throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action));
60 }
61 }
62
63 /**
64 * Pauses the sync process.
65 *
66 */
67 public function pause( $action ) {
68 $action_class = $this->get_class_by_action($action);
69
70 if($action_class) {
71 $action_class::instance()->pause();
72 return $action_class::instance()->get_status();
73 } else {
74 throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action));
75 }
76 }
77
78
79 /**
80 * Resumes the sync process.
81 *
82 */
83 public function resume( $action ) {
84 $action_class = $this->get_class_by_action($action);
85
86 if($action_class) {
87 $action_class::instance()->resume();
88 return $action_class::instance()->get_status();
89 } else {
90 throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action));
91 }
92 }
93
94
95 /**
96 * Stops the sync process.
97 *
98 */
99 public function stop( $action ) {
100 $action_class = $this->get_class_by_action($action);
101
102 if($action_class) {
103 $action_class::instance()->stop();
104 return $action_class::instance()->get_status();
105 } else {
106 throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action));
107 }
108 }
109
110
111 /**
112 * Returns the class associated with the given action.
113 *
114 * @param string $action The action to search for.
115 *
116 * @return string|null The class associated with the action, or null if not found.
117 */
118 private function get_class_by_action( $action ) {
119 foreach ($this->syncClasses as $class) {
120 $class_with_namespace = __NAMESPACE__.'\\'. trim($class);
121 if(!class_exists($class_with_namespace)) continue;
122
123 if($class_with_namespace::instance()->get_action() === $action) {
124 return $class_with_namespace;
125 }
126 }
127
128 return false;
129 }
130
131
132 /**
133 * Retrieves the singleton instance of the class.
134 *
135 * @return self
136 */
137 public static function instance() {
138 if (!self::$instance) self::$instance = new self();
139 return self::$instance;
140 }
141 }
142