";
}
function DMB_webhook_article_regenerate() {
update_option('_webhook_article_url', bin2hex(function_exists('random_bytes') ? random_bytes(16) : openssl_random_pseudo_bytes(16)));
}
// recuperation et ajout du thumbnail au post
function DMB_add_thumbnail($file, $post_id, $desc) {
$file_array = array();
$file_array['name'] = basename($file) ? basename($file) : 'vignette';
if ( !function_exists( 'download_url' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$file_array['tmp_name'] = download_url( $file );
if (is_wp_error($file_array['tmp_name']) ) {
return $file_array['tmp_name'];
}
if ( !function_exists( 'media_handle_sideload' ) ) {
require_once ABSPATH . 'wp-admin/includes/media.php';
}
if ( !function_exists( 'wp_read_image_metadata' ) ) {
require_once ABSPATH . 'wp-admin/includes/image.php';
}
$id = media_handle_sideload( $file_array, $post_id, $desc );
if (is_wp_error( $id )) {
@unlink( $file_array['tmp_name'] );
return $id;
}
return set_post_thumbnail($post_id, $id);
}
function DMB_process_post($request) {
if (get_option('_webhook_article_url', '') != $request->get_param( 'uid')){
return DMB_REST_response(new WP_Error( 'not_authorized', __( 'Invalid uid' ), array('code'=>401, 'body'=>'Invalid uid') ));
}
try {
$lejson = $request->get_json_params();
$status='draft';
if ($lejson['status']){
if ($lejson['status']==1){
$status = 'publish';
}
}
$le_post = array(
'post_title' => $lejson['titre_post'],
'post_content' => $lejson['contenu'],
'post_status' => $status,
'post_category' => $lejson['categorie'],
'post_author' => $lejson['auteur'],
'post_type' => $lejson['type']
);
$post_id = wp_insert_post($le_post);
if ($lejson['image']){
$image = $lejson['image'];
$tmp2 = explode('.', end(explode('/', $image)))[0];
$ret = DMB_add_thumbnail($image, $post_id, sanitize_text_field($tmp2));
}
if (is_wp_error($ret))
return DMB_REST_response($ret);
elseif (!$ret)
return DMB_REST_response(new WP_Error( 'attach_image_failed', __( 'Internal error' ), array('code'=>500, 'body'=>"Failed to attach ".$image) ));
else // $ret = true
return DMB_REST_response(array('message' => 'OK post_created', 'permalien' => get_permalink($post_id)) );
} catch (Exception $e) {
return DMB_REST_response(new WP_Error( 'create_post_failed', __( 'Internal error' ), array('code'=>500, 'body'=>$e->getMessage()) ));
}
}
function DMB_process_category($request) {
if (get_option('_webhook_article_url', '') != $request->get_param( 'uid')){
return DMB_REST_response(new WP_Error( 'not_authorized', __( 'Invalid uid' ), array('code'=>401, 'body'=>'Invalid uid') ));
}
$lejson = $request->get_json_params();
require_once('wp-load.php' );
require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
wp_create_category( 'test create' );
$cat_defaults = array(
'cat_name' => $lejson['categorie_nom'],
'category_description' => $lejson['categorie_description'],
'category_nicename' => '',
'category_parent' => '',
'taxonomy' => 'category'
);
$cat_id = wp_insert_category( $cat_defaults );
if (is_wp_error($cat_id))
return DMB_REST_response($cat_id);
else // $ret = true
return DMB_REST_response(array('message' => 'OK category_created', 'id' => $cat_id) );
}
function DMB_REST_response($payload) {
if (is_wp_error($payload)) {
return new WP_REST_Response(array( 'message' => $payload ), isset(reset($payload->error_data)['code']) ? reset($payload->error_data)['code'] : 500);
} else {
return new WP_REST_Response($payload, 200);
}
}
function ABC_process_post() {
// the meta_key 'diplay_on_homepage' with the meta_value 'true'
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish'
));
try {
foreach ($posts as $key => $p) {
$Get_Posts[]=array(
'post_id'=>$p->ID,
'post_infos'=>array(
'Titre'=> $p->post_title,
'Permalien'=>get_permalink($p->ID),
'Keywords_RM'=>explode(",",get_post_meta($p->ID, 'rank_math_focus_keyword', true)),
'Keywords_SP'=>explode(",",get_post_meta($p->ID, '_seopress_analysis_target_kw', true))
)
);
}
} catch (Exception $e) {
return DMB_REST_response(new WP_Error( 'create_post_failed', __( 'Internal error' ), array('code'=>500, 'body'=>$e->getMessage()) ));
}
return DMB_REST_response(array('message' => 'OK post_created','post' => $Get_Posts));
}
function ABC_process_category() {
// the meta_key 'diplay_on_homepage' with the meta_value 'true'
$categories = get_categories(array(
'hide_empty' => false,
'orderby' => 'id',
'order' => 'ASC',
));
foreach( $categories as $category ) {
$Get_Cats[]=array(
'cat_id'=>$category->term_id,
'cat_name'=>$category->name
);
}
return($Get_Cats);
}
function ABC_process_author() {
// the meta_key 'diplay_on_homepage' with the meta_value 'true'
$authors = get_users( array( 'role__in' => array( 'author', 'administrator' ) ) );
foreach( $authors as $author ) {
$Get_Authors[]=array(
'author_id'=>$author->ID,
'author_name'=>$author->display_name,
);
}
return($Get_Authors);
}
// creation endpoint custom de la REST API
add_action('rest_api_init', function () {
register_rest_route( 'webhookaddpost/v1', '/article', array(
'methods' => 'POST',
'callback' => 'DMB_process_post',
'permission_callback' => '__return_true' // authentification dans DMB_process_post
));
register_rest_route( 'webhookaddpost/v1', '/linking', array(
'methods' => 'POST',
'callback' => 'ABC_process_post',
'permission_callback' => '__return_true' // authentification dans DMB_process_post
));
register_rest_route( 'webhookaddpost/v1', '/categorie', array(
'methods' => 'POST',
'callback' => 'DMB_process_category',
'permission_callback' => '__return_true' // authentification dans DMB_process_post
));
register_rest_route( 'webhookaddpost/v1', '/categorie', array(
'methods' => 'GET',
'callback' => 'ABC_process_category',
'permission_callback' => '__return_true' // authentification dans DMB_process_post
));
register_rest_route( 'webhookaddpost/v1', '/auteur', array(
'methods' => 'GET',
'callback' => 'ABC_process_author',
'permission_callback' => '__return_true' // authentification dans DMB_process_post
));
});
?>