diff --git a/ingestion/ingest-wordpress.py b/ingestion/ingest-wordpress.py index c91bc95..ecf044d 100644 --- a/ingestion/ingest-wordpress.py +++ b/ingestion/ingest-wordpress.py @@ -1,6 +1,7 @@ import base64 import json import os.path +import os import requests import sys @@ -42,7 +43,26 @@ def get_posts(): def do_work(): for post in get_posts(): write_post(post) - utils.git_push(utils.PROJECT_ROOT_GIT_PATH, commit_message="Update shared repository: wordpress", enable_push=True) + utils.git_push(utils.PROJECT_ROOT_GIT_PATH, commit_message="Update shared repository: wordpress", enable_push=False) + + +def do_export(id, lang, message): + """ + This sends the tranlated item + """ + export_url = os.environ.get('EXPORT_URL') + + url = f"{export_url}/wp-json/elsa/v1/translate/{id}/{lang}" + + user = os.environ.get('EXPORT_USER') + password = os.environ.get('EXPORT_PASSWORD') + credentials = user + ':' + password + token = base64.b64encode(credentials.encode()) + header = {'Authorization': 'Basic ' + token.decode('utf-8')} + + response = requests.post(url, headers=header, json=message) + return {"status": "success"} + app = Flask(__name__) api = Api(app) @@ -56,7 +76,42 @@ def post(self): do_work() return "Post" +class WordpressExportListener(Resource): + """ + `WordpressExportListener` will be the endpoint to be triggered when we want to return something back to a + wordpress site. It requires the following body attributes: + + + JSON + { + "id": ID of the source content, + "lang": target language code (ex. "fr"), + "content": HTML content (ex. "

Bonjour le monde! Je suis Rapi Castillo

"), + "title": Title of the translated content (ex. "Bonjour le monde!"), + "excerpt": Excerpt of the translated content (ex. "Hello this is a french translation xx"), + "status": "publish" + } + + This will create/update the translated content. + + """ + def post(self): + + post_data = request.json + id=post_data.get("id") + lang=post_data.get("lang") + message = { + "content": post_data.get("content"), + "title": post_data.get("title"), + "excerpt": post_data.get("excerpt"), + "status": post_data.get("status") + } + + return do_export(id, lang, message) + api.add_resource(WordpressUpdateListener, "/wp-updates") +api.add_resource(WordpressExportListener, "/wp-export") + if __name__ == "__main__": # Warning: When debug mode is unsafe. Attackers can use it to run arbitrary python code. diff --git a/serge/configs/base_config.serge b/serge/configs/base_config.serge index c3654f7..4a7f508 100644 --- a/serge/configs/base_config.serge +++ b/serge/configs/base_config.serge @@ -208,7 +208,7 @@ jobs # plugin for more information. data { - path_matches \/(about|title|description)$ + path_matches \/(about|wptitle|content|description)$ #param1 value1 #param2 value2 } diff --git a/shared_directory/en/wp1.json b/shared_directory/en/wp1.json new file mode 100644 index 0000000..2ca57d6 --- /dev/null +++ b/shared_directory/en/wp1.json @@ -0,0 +1 @@ +{"id": 1, "wptitle": {"rendered": "Hello world!"}, "content": {"rendered": "\n

Welcome to WordPress. … This is your first post. Edit or delete it, then start writing!

\n", "protected": false}, "link": "http://localhost:8888/2020/04/20/hello-world/", "modified_gmt": "2020-09-22T16:01:56"} \ No newline at end of file diff --git a/shared_directory/en/wp31.json b/shared_directory/en/wp31.json new file mode 100644 index 0000000..3bbf21d --- /dev/null +++ b/shared_directory/en/wp31.json @@ -0,0 +1 @@ +{"id": 31, "wptitle": {"rendered": "Goodbye"}, "content": {"rendered": "\n

Welcome to WordPress. … This is your second post. Edit or delete it, then start writing?

\n", "protected": false}, "link": "http://localhost:8888/2020/09/09/test-new-post/", "modified_gmt": "2020-09-22T15:58:53"} \ No newline at end of file diff --git a/wordpress/wp-elsa/wp-elsa.php b/wordpress/wp-elsa/wp-elsa.php new file mode 100644 index 0000000..5bd5a40 --- /dev/null +++ b/wordpress/wp-elsa/wp-elsa.php @@ -0,0 +1,118 @@ +\d+)/(?P\w+)', + array( + 'methods' => 'POST', + 'callback' => 'create_translation', + 'args' => array( + 'id' => array( + 'validate_callback' => function($param, $request, $key) { + return is_numeric( $param ); + } + ), + 'lang' + ), + ) + ); +} + + +/** + * create_translation + * + * This processes the request from the /translate endpoint and creates + * a translated content. + */ +function create_translation(WP_REST_Request $request) { + + $language = $request['lang']; + $id = $request['id']; + + $current_language = pll_get_post_language($id); + + $translated_post = pll_get_post($id, $language); + if ($translated_post == null) { + // Create Translation + $json = json_decode($request->get_body()); + $json->id = $id; + $json->lang = $language; + + $new_content = wp_insert_post( + array( + 'post_title' => $json->title, + 'post_excerpt' => $json->excerpt, + 'post_content' => $json->content + ) + ); + + + pll_set_post_language($new_content, $json->lang); + + pll_save_post_translations(array($current_language => $id, $language => $new_content)); + + $content = new WP_Query(array('p' => $new_content, 'post_type' => 'any')); + $response = new WP_REST_Response( + $content + ); + + $response->set_status(201); + return $response; + } else { + // Update the translation + $json = json_decode($request->get_body()); + $json->id = $translated_post; + + $time = strtotime( 'now' ); + $content = array( + 'ID' => $json->id, + 'post_title' => $json->title, + 'post_excerpt' => $json->excerpt, + 'post_content' => $json->content, + 'post_date' => date( 'Y-m-d H:i:s', $time ), + 'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $time ), + ); + + $updated_post = wp_update_post($content); + + $response = new WP_REST_Response( + $content + ); + + $response->set_status(201); + return $response; + } +} + + + + +/** + * elsa_requireents_activate + * + * This function would require wordpress to have Polylang installed when acivating the plugin. + */ +function elsa_requirements_activate() { + + if ( current_user_can( 'activate_plugins' ) + && !( + function_exists( 'pll_get_post_language') + && function_exists('pll_save_post_translations') + ) + ) { + // Deactivate the plugin. + deactivate_plugins( plugin_basename( __FILE__ ) ); + // Throw an error in the WordPress admin console. + $error_message = '

' . esc_html__( 'This plugin requires ', 'simplewlv' ) . 'Polylang' . esc_html__( ' plugin to be active.', 'simplewlv' ) . '

'; + die( $error_message ); // WPCS: XSS ok. + } +} +register_activation_hook( __FILE__, 'elsa_requirements_activate' );