-
Notifications
You must be signed in to change notification settings - Fork 2
Export Wordpress #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Export Wordpress #25
Changes from all commits
41ced26
e356d94
7eb1bf0
8e4a7ed
f304b24
499a268
7106adb
cbabeeb
a2311cc
43e2c96
7ce6e87
da75d8a
9b892c2
ad1abe6
d9c8ec3
10d58e5
7982330
795b8a7
f7a0883
301c851
a7ebc68
f1b2dc8
71db715
82d8c9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,7 +208,7 @@ jobs | |
# plugin for more information. | ||
data | ||
{ | ||
path_matches \/(about|title|description)$ | ||
path_matches \/(about|wptitle|content|description)$ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove this? You inherited this from my branch, but I took it out. |
||
#param1 value1 | ||
#param2 value2 | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"id": 1, "wptitle": {"rendered": "Hello world!"}, "content": {"rendered": "\n<p>Welcome to WordPress. … This is your first post. Edit or delete it, then start writing!</p>\n", "protected": false}, "link": "http://localhost:8888/2020/04/20/hello-world/", "modified_gmt": "2020-09-22T16:01:56"} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"id": 31, "wptitle": {"rendered": "Goodbye"}, "content": {"rendered": "\n<p>Welcome to WordPress. … This is your second post. Edit or delete it, then start writing?</p>\n", "protected": false}, "link": "http://localhost:8888/2020/09/09/test-new-post/", "modified_gmt": "2020-09-22T15:58:53"} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So are Cyber Command / DoITT ok with us installing this custom code on our wpengine instance? What is the approval process? Do we need to go through an approval process every time we change the code? If I wanted to add my own custom php function (say, manually building my own simple "webhook" by hitting a hard coded endpoint every time a wordpress post is edited), would that be ok? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, according to @jisaf the process of having this go through Cyber would be more streamlined compared to if we are to use plugins that are not within WPEngine because it's worked by an unapproved third party |
||
|
||
/*** | ||
* | ||
* CREATE TRANSLATION | ||
* Receiving new content via POST | ||
*/ | ||
|
||
add_action('rest_api_init', 'elsa_translation_endpoints'); | ||
function elsa_translation_endpoints() { | ||
register_rest_route( | ||
'elsa/v1', '/translate/(?P<id>\d+)/(?P<lang>\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 = '<p style="font-family:-apple-system,BlinkMacSystemFont,\'Segoe UI\',Roboto,Oxygen-Sans,Ubuntu,Cantarell,\'Helvetica Neue\',sans-serif;font-size: 13px;line-height: 1.5;color:#444;">' . esc_html__( 'This plugin requires ', 'simplewlv' ) . '<a href="' . esc_url( 'https://wordpress.org/plugins/simplewlv/' ) . '">Polylang</a>' . esc_html__( ' plugin to be active.', 'simplewlv' ) . '</p>'; | ||
die( $error_message ); // WPCS: XSS ok. | ||
} | ||
} | ||
register_activation_hook( __FILE__, 'elsa_requirements_activate' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than receiving the text of the message from a post() parameter, we need to get it from the file system. For instance, here are some examples of spanish (es) output files.
https://github.com/nyc-cto/tms-data/tree/aditya/development/local/source_files/es
I've been using the "wp" prefix to indicate wordpress files. Can you read the json from them?
In our Docker setup, this tms-data git branch gets checked out under /var/tms-data/source_files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re: actually triggering the code to run -- Ideally we need our Serge wrapper to hit your rest api here. Can you ask Aditya about the best place for the call to happen? If there's no good place to put the hook, we can instead use a celery/cron-ish timer to run your code periodically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds good. I'll check in with Aditya.