Skip to content

Instantly share code, notes, and snippets.

@akirk
Created December 9, 2024 10:54
Show Gist options
  • Save akirk/226c85e05a32969ca5099326248329bf to your computer and use it in GitHub Desktop.
Save akirk/226c85e05a32969ca5099326248329bf to your computer and use it in GitHub Desktop.
<?php
// ./src/schemas/schemas.json
// {
// "post": {
// "title": "Blog Post",
// "fields": {
// "date": {
// "type": Date",
// "description": "date of the post",
// "required": true
// },
// "author": "String",
// "title": "String",
// "content": "String",
// },
// },
// "page": {
// "title": "Page",
// "fields": {
// "title": "String",
// "content": "String",
// },
// },
// "product": {
// "title": "Product",
// "fields": {
// "title": "ProductString",
// "description": "String",
// "price": "currency",
// },
// }
// }
//
// Example JS implementation:
// ```js
// subject = {
// {
// type: 'product',
// 'title'
//
// const postData = {
// post_title: "Sample Data Liberation Product",
// post_content: "",
// post_status: "publish",
// meta: {
// // Custom meta data to send with the post
// source_url: source_url,
// title: product_title,
// description: product_description,
// price: product_price,
// }
// };
// // Replace 'YOUR_ACCESS_TOKEN' with your authentication token (if needed)
// fetch('/wp-json/try-wordpress/receive', {
// fetch('/wp-json/wp/v2/data_liberation_product', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// 'Authorization': 'Bearer YOUR_ACCESS_TOKEN' // Use appropriate authentication
// },
// body: JSON.stringify(postData) // Convert the post data to JSON string
// }
// ```
$data_liberation_post_types = array();
foreach (json_decode(file_get_contents(__DIR__ . '/schema.json'), true) as $post_type => $meta) {
$data_liberation_post_types[] = 'data_liberation_' . $post_type;
register_post_type('data_liberation_' . $post_type, array( // 'data_liberation_post' or 'data_liberation_product'
'label' => $meta['title'], // Post or Product
'show_in_rest' => true,
// ...
));
foreach ($meta['fields'] as $name => $metadata) {
register_post_meta('data_liberation_' . $post_type, $name, array(
'type' => $metadata['type'], // string or currency –> string
'sanitize_callback' => $callbacks[$type],
));
}
register_post_meta('data_liberation_' . $post_type, 'source_url', array());
add_action("rest_insert_{$post_type}", function (WP_Post $post, WP_REST_Request $request, bool $creating) use ($post_type) {
$subject = new Subject($post); // Wrapper over WP_Post
do_action('data_liberation_' . $post_type, $subject); // data_liberation_product
}, 10, 3);
}
get_post(array(
'post_type' => 'any',
'meta[source_url]' => $source_url,
));
class Subject
{
private $post;
public function __construct(WP_Post $post)
{
$this->post = $post;
}
public function __get($key)
{
return get_post_meta($this->post->ID, $key, true);
}
}
register_rest_route('try-wordpress', 'receive', array(
'callback' => function (WP_REST_Request $request) {
$post_id = wp_insert_post(array(
'post_type' => 'data_liberation_' . $request->get_param('post_type'),
'post_content' => $request->get_param('post_content'),
));
foreach ($request->get_param('meta') as $key => $value) {
update_post_meta($post_id, $key, $value);
}
do_action("rest_insert_{$post_type}", get_post($post_id));
}
));
add_action('data_liberation_post', function ($subject) {
$post_id = wp_insert_post(array(
'post_type' => 'post',
'post_title' => $subject->title,
'post_content' => $subject->content,
));
$subject->store_reference($post_id);
});
add_action('data_liberation_page', function ($subject) {
$post_id = wp_insert_post(array(
'post_type' => 'page',
'post_title' => $subject->title,
'post_content' => $subject->content,
));
$subject->store_reference($post_id);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment