From 5399b12133712f6fadad5ae416a1dd747ce8da6b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 27 Jul 2023 00:39:15 +0200 Subject: [PATCH] menu wordpress 75% --- lib/WPMenu.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index d1f349a..e8889de 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -273,7 +273,7 @@ class WPMenu: result = page.json() for i in result: if i["title"]["rendered"] == itemMenu["title"]: - idItemMenu = i["id"] + idItemMenu = int(i["id"]) self._logger.info("{0} : Length of result for item menus : {1}".format(self._name, len(result))) url = "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress) if idItemMenu != 0: @@ -283,28 +283,28 @@ class WPMenu: 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"]} + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "menus":idMenu, "url":"{0}://{1}".format(self._protocol_wordpress, self._wordpress)} + if type(itemMenu["id"]) is str: + if len(itemMenu["id"]) > 0: + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "url": itemMenu["link"], "menus":idMenu, "object":objectt, "object_id":int(itemMenu["id"])} + elif type(itemMenu["id"]) is int: + data = {"title": itemMenu["title"], "status": "publish", "parent":parent, "url": itemMenu["link"], "menus":idMenu, "object":objectt, "object_id":itemMenu["id"]} + self._logger.debug("{0} : data for create/update : {1}".format(self._name, data)) page = self._request_wordpress.post(url, auth=self._basic, headers=self._headers_json, data=json.dumps(data)) - if page.status_code == 201: + if page.status_code in [201, 200]: result = page.json() - idItemMenu = result["id"] - self._logger.info("{0} : create item menu : {1}".format(self._name, itemMenu["title"])) - elif page.status_code == 200: - result = page.json() - for i in result: - if result["title"]["rendered"] == itemMenu["title"]: - idItemMenu = i["id"] - self._logger.info("{0} : update item menu : {1}".format(self._name, itemMenu["title"])) + idItemMenu = int(result["id"]) + self._logger.info("{0} : create/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)) + self._logger.error("{0} : Create menu items for {2} didn't get due status code : {1}".format(self._name, page.status_code, itemMenu["title"])) + self._logger.debug("{0} : {1} {2}".format(self._name, page.content, itemMenu)) 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)) + self._logger.error("{0} : Exception 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)) 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)) @@ -353,6 +353,7 @@ class WPMenu: if i["name"] == title: idMenu = i["id"] self._logger.info("{0} : Get ID menus : {1}".format(self._name, idMenu)) + self._addItemMenu(menu, idMenu) else: self._logger.error("{0} : Get menu didn't get due status code : {1}".format(self._name, page.status_code)) self._logger.debug("{0} : {1}".format(self._name, page.content)) @@ -364,10 +365,11 @@ class WPMenu: self._logger.error("{0} : Exception error for get menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err)) - def addItemMenu(self, menu, idMenu): + def _addItemMenu(self, menu, idMenu): self._logger.info("{0} : add item to menu : {1}".format(self._name, idMenu)) + parent = 0 for i in menu: + self._logger.debug("{0} : debug create item menu : {1}".format(self._name, i)) parent = self._createItemMenu(idMenu, i, parent) - for j in menu["children"]: - self._createItemMenu(idMenu, i, parent) - \ No newline at end of file + for j in i["children"]: + self._createItemMenu(idMenu, j, parent)