From cf2c1aa617abc8ce45257c291f9934cc342c1130 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 24 Jul 2023 23:32:20 +0200 Subject: [PATCH] add item menu --- lib/WPMenu.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/WPMenu.py b/lib/WPMenu.py index 0330641..9e64dbf 100644 --- a/lib/WPMenu.py +++ b/lib/WPMenu.py @@ -259,8 +259,42 @@ class WPMenu: menu[i] = {"id":idMenu["id"], "type": idMenu["type"], "title":menu[i]["title"], "link":idMenu["link"], "children": menu[i]["children"]} self._createMenu(menu) + + + exit(0) - + + def _createItemMenu(self, idMenu, itemMenu, parent): + idItemMenu = 0 + self._logger.info("{0} : Create item menu from API Wordpress : {1}".format(self._name, self._wordpress)) + try: + params = {"search": itemMenu["title"], "menus": idMenu} + page = self._request_wordpress.get("{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), auth=self._basic, params=params) + if page.status_code == 200: + result = page.json() + for i in result: + 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)) + 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)) + except ConnectionError as err: + self._logger.error("{0} : Connection error for get item menus {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 menus {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menu-items".format(self._wordpress, self._protocol_wordpress), err)) + return idItemMenu + def _createMenu(self, menu): title = "Menu {0}".format(self._wordpress) self._logger.info("{0} : Create menu from API Wordpress : {1}".format(self._name, title))