service = new UniteCreatorAddonChangelog(); parent::__construct($args); } /** * Process the action. * * @return void */ public function processAction(){ $generalQueryArgs = array("_wp_http_referer", "_wpnonce", "ucwindow"); $actionQueryArgs = array("action", "action2", self::FILTER_ID, self::EDIT_FIELD_TYPE, self::EDIT_FIELD_TEXT); $action = $this->current_action(); if($action === self::ACTION_EXPORT){ $this->processExportAction(); exit; } $ids = $this->getFilter(self::FILTER_ID); if(empty($ids) === false){ $ids = is_array($ids) ? $ids : array($ids); $result = false; switch($action){ case self::ACTION_EDIT: $result = $this->processEditAction($ids); break; case self::ACTION_DELETE: $result = $this->processDeleteAction($ids); break; } if(empty($result) === false){ $url = wp_get_referer(); $url = remove_query_arg($actionQueryArgs, $url); wp_redirect($url); exit; } } $containedQueryArgs = array_intersect($generalQueryArgs, array_keys($_REQUEST)); if(empty($containedQueryArgs) === false){ $url = wp_unslash($_SERVER["REQUEST_URI"]); $url = remove_query_arg(array_merge($generalQueryArgs, $actionQueryArgs), $url); wp_redirect($url); exit; } } /** * Gets the current action. * * @return string|false */ public function current_action(){ if(isset($_REQUEST[self::ACTION_EXPORT])) return self::ACTION_EXPORT; return parent::current_action(); } /** * Gets a list of columns. * * @return array */ public function get_columns(){ $columns = array( "cb" => '', "addon" => __("Widget", "unlimited-elements-for-elementor"), "type" => __("Type", "unlimited-elements-for-elementor"), "text" => __("Text", "unlimited-elements-for-elementor"), "version" => __("Version", "unlimited-elements-for-elementor"), "user" => __("Author", "unlimited-elements-for-elementor"), "date" => __("Date", "unlimited-elements-for-elementor"), ); return $columns; } /** * Prepares the list of items for displaying. * * @return void */ public function prepare_items(){ $this->processAction(); // prepare columns $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array($columns, $hidden, $sortable); // prepare items and pagination $data = $this->prepareData(); $limit = $this->getLimit(); $this->items = $data["items"]; $this->set_pagination_args([ "total_items" => $data["total"], "per_page" => $limit, ]); } /** * Message to be displayed when there are no items. * * @return void */ public function no_items(){ echo __("No changelogs found.", "unlimited-elements-for-elementor"); } /** * Displays the table. * * @return void */ public function display(){ $this->prepare_items(); $this->displayHeader(); ?>
displayHiddenFields(); ?> views(); ?> displayFilterScript(); ?> displayEditTemplate(); ?> displayEditStyle(); ?> displayEditScript(); ?>
__("Delete Permanently", "unlimited-elements-for-elementor"), ); return $actions; } /** * Generates content for a single row of the table. * * @param array $item * * @return void */ public function single_row($item){ echo ''; $this->single_row_columns($item); echo ''; } /** * Renders the checkbox column. * * @param array $item * * @return string */ protected function column_cb($item){ return ''; } /** * Renders the id column. * * @param array $item * * @return string */ protected function column_id($item){ return $item["id"]; } /** * Renders the main column. * * @param array $item * * @return string */ protected function column_addon($item){ $id = $item["id"]; $content = $item["addon_title"]; $actions = array( self::ACTION_EDIT => $this->getActionLink(self::ACTION_EDIT, $id, __("Edit", "unlimited-elements-for-elementor")), self::ACTION_DELETE => $this->getActionLink(self::ACTION_DELETE, $id, __("Delete Permanently", "unlimited-elements-for-elementor")), ); return $content . '
' . $this->row_actions($actions) . '
'; } /** * Renders the type column. * * @param array $item * * @return string */ protected function column_type($item){ return $item["type_title"]; } /** * Renders the text column. * * @param array $item * * @return string */ protected function column_text($item){ return $item["text_html"]; } /** * Renders the version column. * * @param array $item * * @return string */ protected function column_version($item){ return $item["plugin_version"]; } /** * Renders the user column. * * @param array $item * * @return string */ protected function column_user($item){ return $item["user_username"]; } /** * Renders the date column. * * @param array $item * * @return string */ protected function column_date($item){ return $item["created_date"]; } /** * Extra controls to be displayed between bulk actions and pagination. * * @param string $which * * return void */ protected function extra_tablenav($which){ if($which !== "top") return; $addons = $this->getAddonsFilter(); $versions = $this->getVersionsFilter(); $selectedAddon = $this->getFilter(self::FILTER_ADDON); $selectedVersion = $this->getFilter(self::FILTER_VERSION); ?>
displayFilterSelect(self::FILTER_ADDON, __("Filter by Widget", "unlimited-elements-for-elementor"), __("All Widgets", "unlimited-elements-for-elementor"), $addons, $selectedAddon); ?> displayFilterSelect(self::FILTER_VERSION, __("Filter by Version", "unlimited-elements-for-elementor"), __("All Versions", "unlimited-elements-for-elementor"), $versions, $selectedVersion); ?> "filter-submit")); ?>
"export-submit")); ?>
service->updateChangelog($ids, array( "type" => $type, "text" => $text, )); return $result; } /** * Process the delete action. * * @param array $ids * * @return int */ private function processDeleteAction($ids){ $result = $this->service->deleteChangelog($ids); return $result; } /** * sort export items */ public function sortExportItems($item1, $item2){ $arrNumbers = array( "feature"=>1, "change"=>2, "fix"=>3, "other"=>4 ); $type1 = UniteFunctionsUC::getVal($item1, "type"); $type2 = UniteFunctionsUC::getVal($item2, "type"); $num1 = UniteFunctionsUC::getVal($arrNumbers, $type1, 4); $num2 = UniteFunctionsUC::getVal($arrNumbers, $type2, 4); if($num1 == $num2) return(0); if($num1 > $num2) return(1); return(-1); } /** * Process the export action. * * @return void */ private function processExportAction(){ global $wpdb; $filters = $this->getFilters(); $sql = " SELECT id FROM {$this->service->getTable()} WHERE {$this->getWhere($filters)} ORDER BY {$this->getOrderBy()} "; $ids = $wpdb->get_col($sql); $items = $this->service->findChangelog($ids); $lines = array(); usort($items, array($this,"sortExportItems")); foreach($items as $item){ $title = $item["addon_title"]; if(empty($item["addon_version"]) === false) $title .= " ({$item["addon_version"]})"; $type = $item["type_title"]; $text = $item["text"]; $line = "* {$type}: {$title} - {$text}"; $lines[] = $line; } $filename = "changelog-" . current_time("mysql") . ".txt"; $content = implode("\n", $lines); UniteFunctionsUC::downloadTxt($filename, $content); } /** * Prepares the list of items. * * @return array */ private function prepareData(){ global $wpdb; $filters = $this->getFilters(); $table = $this->service->getTable(); $where = $this->getWhere($filters); $sql = " SELECT COUNT(*) FROM $table WHERE $where "; $total = $wpdb->get_var($sql); $sql = " SELECT id FROM $table WHERE $where ORDER BY {$this->getOrderBy()} LIMIT {$this->getLimit()} OFFSET {$this->getOffset()} "; $ids = $wpdb->get_col($sql); $items = $this->service->findChangelog($ids); $data = array( "items" => $items, "total" => $total, ); return $data; } /** * Get the list of addons for the filter. * * @return array */ private function getAddonsFilter(){ global $wpdb; $sql = " SELECT addon_id, addon_title FROM {$this->service->getTable()} GROUP BY addon_id ORDER BY addon_id "; $results = $wpdb->get_results($sql); $items = array(); foreach($results as $result){ $items[$result->addon_id] = $this->service->getAddonTitle($result->addon_id, $result->addon_title); } return $items; } /** * Get the list of versions for the filter. * * @return array */ private function getVersionsFilter(){ global $wpdb; $sql = " SELECT plugin_version FROM {$this->service->getTable()} GROUP BY plugin_version ORDER BY plugin_version DESC "; $results = $wpdb->get_results($sql); $items = array(); foreach($results as $result){ $items[$result->plugin_version] = $result->plugin_version; } return $items; } /** * Get the action link. * * @param string $action * @param int $id * @param string $label * * @return string */ private function getActionLink($action, $id, $label){ $url = array(); $url["page"] = $_REQUEST["page"]; $url["action"] = $action; $url["ucwindow"] = "blank"; $url[self::FILTER_ID] = $id; if(empty($_REQUEST["view"]) === false) $url["view"] = $_REQUEST["view"]; return '' . esc_html($label) . ''; } /** * Get the filter values. * * @return array */ private function getFilters(){ $keys = array( self::FILTER_ID, self::FILTER_ADDON, self::FILTER_VERSION, ); $filters = array(); foreach($keys as $key){ $filters[$key] = $this->getFilter($key); } return $filters; } /** * Get the filter value. * * @param string $key * * @return mixed */ private function getFilter($key){ $value = null; switch($key){ case self::FILTER_ID: $value = UniteFunctionsUC::getGetVar(self::FILTER_ID, null, UniteFunctionsUC::SANITIZE_NOTHING); break; case self::FILTER_ADDON: $value = UniteFunctionsUC::getGetVar(self::FILTER_ADDON, null, UniteFunctionsUC::SANITIZE_ID); break; case self::FILTER_VERSION: $value = UniteFunctionsUC::getGetVar(self::FILTER_VERSION, null, UniteFunctionsUC::SANITIZE_NOTHING); break; } return $value; } /** * Get the where clause. * * @param array $filters * * @return string */ private function getWhere(array $filters){ global $wpdb; $where = "1 = 1"; $id = UniteFunctionsUC::getVal($filters, self::FILTER_ID, null); if(empty($id) === false){ $ids = is_array($id) ? $id : array($id); $placeholders = UniteFunctionsWPUC::getDBPlaceholders($ids, "%d"); $where .= $wpdb->prepare(" AND id IN($placeholders)", $ids); } $addon = UniteFunctionsUC::getVal($filters, self::FILTER_ADDON, null); if(empty($addon) === false) $where .= $wpdb->prepare(" AND addon_id = %d", array($addon)); $version = UniteFunctionsUC::getVal($filters, self::FILTER_VERSION, null); if(empty($version) === false) $where .= $wpdb->prepare(" AND plugin_version = %s", array($version)); return $where; } /** * Get the sorting clause. * * @return string */ private function getOrderBy(){ $orderBy = "created_at DESC"; return $orderBy; } /** * Get the limit. * * @return int */ private function getLimit(){ return 20; } /** * Get the offset. * * @return int */ private function getOffset(){ $page = $this->get_pagenum(); $limit = $this->getLimit(); $offset = ($page - 1) * $limit; return $offset; } /** * Display the header. * * @return void */ private function displayHeader(){ $headerTitle = __("Changelog", "unlimited-elements-for-elementor"); require HelperUC::getPathTemplate("header"); } /** * Display the hidden fields. * * @return void */ private function displayHiddenFields(){ echo ''; if(empty($_REQUEST["view"]) === false) echo ''; echo ''; } /** * Display the filter select. * * @param string $name * @param string $label * @param string $allLabel * @param array $options * @param mixed $selectedValue * * @return void */ private function displayFilterSelect($name, $label, $allLabel, $options, $selectedValue = null){ $id = "filter-$name"; ?> service->getTypes(); ?> display();