token = WPMCS_TOKEN; // Initialize sync classes foreach ($this->syncClasses as $class) { $class_with_namespace = __NAMESPACE__.'\\'. trim($class); if(class_exists($class_with_namespace)) { $class_with_namespace::instance(); } } } /** * Gets the status of the sync. * * @return array An associative array with the action as key and the status * as value. */ public function get_status() { $status = []; foreach ($this->syncClasses as $class) { $class_with_namespace = __NAMESPACE__.'\\'. trim($class); if(!class_exists($class_with_namespace)) continue; $instance = $class_with_namespace::instance(); $action = $instance->get_action(); $status[$action] = $instance->get_status(); } return $status; } /** * Starts the sync process. * */ public function start( $action ) { $action_class = $this->get_class_by_action($action); if($action_class) { $action_class::instance()->start(); return $action_class::instance()->get_status(); } else { /* translators: %s: action name */ throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action)); } } /** * Pauses the sync process. * */ public function pause( $action ) { $action_class = $this->get_class_by_action($action); if($action_class) { $action_class::instance()->pause(); return $action_class::instance()->get_status(); } else { throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action)); } } /** * Resumes the sync process. * */ public function resume( $action ) { $action_class = $this->get_class_by_action($action); if($action_class) { $action_class::instance()->resume(); return $action_class::instance()->get_status(); } else { throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action)); } } /** * Stops the sync process. * */ public function stop( $action ) { $action_class = $this->get_class_by_action($action); if($action_class) { $action_class::instance()->stop(); return $action_class::instance()->get_status(); } else { throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action)); } } /** * Returns the class associated with the given action. * * @param string $action The action to search for. * * @return string|null The class associated with the action, or null if not found. */ private function get_class_by_action( $action ) { foreach ($this->syncClasses as $class) { $class_with_namespace = __NAMESPACE__.'\\'. trim($class); if(!class_exists($class_with_namespace)) continue; if($class_with_namespace::instance()->get_action() === $action) { return $class_with_namespace; } } return false; } /** * Retrieves the singleton instance of the class. * * @return self */ public static function instance() { if (!self::$instance) self::$instance = new self(); return self::$instance; } }