/** * Creates or updates the channel and returns it. * * @param WiseChatChannel $channel * * @return WiseChatChannel * @throws Exception On validation error */ public function save($channel) { global $wpdb; // low-level validation: if ($channel->getName() === null) { throw new Exception('Name of the channel cannot equal null'); } // prepare channel data: $table = WiseChatInstaller::getChannelsTable(); $columns = array('name' => $channel->getName(), 'password' => $channel->getPassword()); // update or insert: if ($channel->getId() !== null) { $wpdb->update($table, $columns, array('id' => $channel->getId()), '%s', '%d'); } else { $wpdb->insert($table, $columns); $channel->setId($wpdb->insert_id); } return $channel; }
/** * Returns rendered channel statistics. * * @param WiseChatChannel $channel * * @return string HTML source */ public function getRenderedChannelStats($channel) { if ($channel === null) { return 'ERROR: channel does not exist'; } $variables = array('channel' => $channel->getName(), 'messages' => $this->messagesService->getNumberByChannelName($channel->getName()), 'users' => $this->channelUsersDAO->getAmountOfUsersInChannel($channel->getId())); return $this->getTemplatedString($variables, $this->options->getOption('template', 'ERROR: TEMPLATE NOT SPECIFIED')); }
/** * Maintenance actions performed periodically. The method authenticates user if there was * no authentication performed. * * @param WiseChatChannel $channel * * @return null */ public function periodicMaintenance($channel) { // check and authenticate user: if (!$this->authentication->isAuthenticated()) { $user = $this->authentication->authenticateAnonymously(); $this->actions->publishAction('refreshPlainUserName', array('name' => $user->getName()), $user); } // signal presence in the channel: if ($this->userEvents->shouldTriggerEvent('ping', $channel->getName())) { $this->markPresenceInChannel($channel); } $this->refreshChannelUsersData(); }
/** * Deletes old messages according to the plugin's settings. * Images connected to the messages (WordPress Media Library attachments) are also deleted. * * @param WiseChatChannel $channel * * @throws Exception */ private function deleteOldMessages($channel) { $minutesThreshold = $this->options->getIntegerOption('auto_clean_after', 0); if ($minutesThreshold > 0) { $criteria = new WiseChatMessagesCriteria(); $criteria->setChannelName($channel->getName()); $criteria->setIncludeAdminMessages(true); $criteria->setMaximumTime(time() - $minutesThreshold * 60); $messages = $this->messagesDAO->getAllByCriteria($criteria); $messagesIds = array(); foreach ($messages as $message) { $messagesIds[] = $message->getId(); } if (count($messagesIds) > 0) { $this->attachmentsService->deleteAttachmentsByMessageIds($messagesIds); $this->actions->publishAction('deleteMessages', array('ids' => $messagesIds)); $this->messagesDAO->deleteAllByCriteria($criteria); } } }