diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 9e64dbf..841a618 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -276,15 +276,29 @@ class WPMenu: if i["title"]["rendered"] == itemMenu["title"]: idItemMenu = i["id"] self._logger.info("{0} : Length of result for item menus : {1}".format(self._name, len(result))) - if idItemMenu == 0: - try: - data = {"title": itemMenu["title"], "status": "publish", "parent":parent} - page = self._request_wordpress.post("{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data)) - except ConnectionError as err: - self._logger.error("{0} : Connection error for create item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) - exit(1) - except Exception as err: - self._logger.error("{0} : Exception error for get item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + url = "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress) + if idItemMenu != 0: + url = "{1}://{0}/wp-json/wp/v2/menu-items/{2}".format(self._wordpress, self._protocol_wordpress, idItemMenu) + + try: + objectt = itemMenu["type"] + if objectt == "tag": + objectt = "post_tag" + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "menus":idMenu, "object":objectt, "object_id":itemMenu["id"]} + page = self._request_wordpress.post(url, auth=self._basic, headers=self._headers_json, data=json.dumps(data)) + if page.status_code == 201: + self._logger.info("{0} : create item menu : {1}".format(self._name, itemMenu["title"])) + elif page.status_code == 200: + self._logger.info("{0} : update item menu : {1}".format(self._name, itemMenu["title"])) + else: + self._logger.error("{0} : Create menu items didn't get due status code : {1}".format(self._name, page.status_code)) + self._logger.debug("{0} : {1}".format(self._name, page.content)) + + except ConnectionError as err: + self._logger.error("{0} : Connection error for create item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + exit(1) + except Exception as err: + self._logger.error("{0} : Exception error for get item menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) else: self._logger.error("{0} : Get menu items didn't get due status code : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content))