From 66aa25c4b0f6fe06a058d2a9237a8d605e6af494 Mon Sep 17 00:00:00 2001 From: lenovo Date: Sat, 5 Nov 2022 10:53:21 +0100 Subject: [PATCH] test add action on post publish - but don't retrieve coordinates yet + test differents functions for custom fields --- srcs/plugins/map_prof/map_prof_hooks.php | 12 +++++ srcs/plugins/map_prof/mp_get_events.php | 68 +++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/srcs/plugins/map_prof/map_prof_hooks.php b/srcs/plugins/map_prof/map_prof_hooks.php index fbd1736..76127f4 100644 --- a/srcs/plugins/map_prof/map_prof_hooks.php +++ b/srcs/plugins/map_prof/map_prof_hooks.php @@ -84,4 +84,16 @@ function mp_add_div() { } add_shortcode('lejourduprof_map', 'mp_add_div'); + + +/** + * when a post is published, check its coordinates + */ +function post_published_coordinates($id, $post) { + if ( ! add_post_meta( $id, 'coordinates', 'banana', true ) ) { + update_post_meta( $id, 'coordinates', 'banana' ); + } +} +add_action( 'publish_post', 'post_published_coordinates', 10, 2 ); + ?> diff --git a/srcs/plugins/map_prof/mp_get_events.php b/srcs/plugins/map_prof/mp_get_events.php index 516c8f0..12089b1 100644 --- a/srcs/plugins/map_prof/mp_get_events.php +++ b/srcs/plugins/map_prof/mp_get_events.php @@ -62,6 +62,9 @@ function mp_get_published_posts() { +// TESTS +// script to publish or unpublish posts +// // $post_args = array( // 'numberposts' => -1, // 'post_status' => 'draft', @@ -79,14 +82,76 @@ function mp_get_published_posts() { $get_posts_args = array( 'numberposts' => -1, - 'post_status' => 'publish', + //'post_status' => 'publish', + 'post_status' => 'draft', 'post_type' => 'post', ); $posts_published = get_posts($get_posts_args); + +// // "get_post" +// // https://developer.wordpress.org/reference/functions/get_posts/ +// $post = $posts_published[0]; +// mp_console_log("get post:"); +// mp_console_log($post); +// // https://developer.wordpress.org/reference/functions/get_post_meta/ +// $post_meta = get_post_meta($post->ID); +// mp_console_log("get post meta:"); +// mp_console_log($post_meta); +// // same as "get_post_meta", but cannot specify a key +// // and is optimized for cache +// // https://developer.wordpress.org/reference/functions/get_post_custom/ +// $post_custom = get_post_custom($post->ID); +// mp_console_log("get post custom:"); +// mp_console_log($post_custom); +// // get fiels 'coordinates' +// $field_coordinates = get_field('coordinates', $post->ID); +// mp_console_log("get field coordinates:"); +// mp_console_log($field_coordinates); +// // get fiels 'ID' +// $field_id = get_field('ID', $post->ID); +// mp_console_log("get field ID:"); +// mp_console_log($field_id); +// // get fiels 'post_title' +// $field_title = get_field('post_title', $post->ID); +// mp_console_log("get field title:"); +// mp_console_log($field_title); + return $posts_published; } +/* + GET POST : + ID: 29693 + comment_count: "0" + comment_status: "closed" + filter: "raw" + guid: "https://local_lejourduprof.com/?p=29693" + menu_order: 0 + ping_status: "closed" + pinged: "" + post_author: "1" + post_content: "" + post_content_filtered: "" + post_date: "2022-11-04 18:05:49" + post_date_gmt: "2022-11-04 17:05:49" + post_excerpt: "Les enseignants vont présenter les projets développés dans leurs classes et partager leurs pratiques et expériences." + post_mime_type: "" + post_modified: "2022-11-05 09:39:46" + post_modified_gmt: "2022-11-05 08:39:46" + post_name: "construisons-ensemble-lavenir" + post_parent: 0 + post_password: "" + post_status: "draft" + post_title: "Construisons ensemble l'avenir" + post_type: "post" + to_ping: "" +*/ + function mp_fill_fields_value($id) { + // get_field is an ACF function + // in "pure" worpdress use : + // get_post_meta or get_post_custom_values + $fields = array( "heure_de_debut", "heure_de_fin", @@ -97,6 +162,7 @@ function mp_fill_fields_value($id) { "adresse", "prenom", "nom", + "coordinates", ); $event = (object)[]; foreach($fields as $field) {