diff --git a/plugins/osi-features/inc/classes/post-types/class-post-type-press-mentions.php b/plugins/osi-features/inc/classes/post-types/class-post-type-press-mentions.php index bfc988b..ed830c2 100644 --- a/plugins/osi-features/inc/classes/post-types/class-post-type-press-mentions.php +++ b/plugins/osi-features/inc/classes/post-types/class-post-type-press-mentions.php @@ -97,10 +97,8 @@ public function register_custom_fields() { * @param \WP_Post $post The post object */ public function render_meta_box( $post ) { - // Add nonce for security \wp_nonce_field( 'press_mentions_meta_box', 'press_mentions_meta_box_nonce' ); - // Get existing values $date_of_publication = \get_post_meta( $post->ID, 'date_of_publication', true ); // Convert Ymd format to Y-m-d for the input field if ( ! empty( $date_of_publication ) ) { @@ -143,22 +141,18 @@ public function render_meta_box( $post ) { * @param \WP_Post $post The post object */ public function save_custom_fields( $post_id, $post ) { - // Check if nonce is set if ( ! isset( $_POST['press_mentions_meta_box_nonce'] ) ) { return; } - // Verify nonce if ( ! \wp_verify_nonce( $_POST['press_mentions_meta_box_nonce'], 'press_mentions_meta_box' ) ) { return; } - // If this is an autosave, don't do anything if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } - // Check user permissions if ( ! \current_user_can( 'edit_post', $post_id ) ) { return; } @@ -190,11 +184,18 @@ public function register_rest_fields() { return \get_post_meta( $post['id'], 'date_of_publication', true ); }, 'update_callback' => function ( $value, $post ) { - \update_post_meta( $post->ID, 'date_of_publication', $value ); + // Handle array input from Make.com + $date_value = is_array( $value ) && ! empty( $value ) ? $value[0] : $value; + if ( ! empty( $date_value ) ) { + // Convert the date to Ymd format + $formatted_date = gmdate( 'Ymd', strtotime( $date_value ) ); + \update_post_meta( $post->ID, 'date_of_publication', $formatted_date ); + } }, 'schema' => array( - 'type' => 'string', - 'description' => 'Date of Publication', + 'type' => array( 'string', 'array' ), + 'items' => array( 'type' => 'string' ), + 'description' => 'Date of Publication in Ymd format', 'required' => true, ), ), @@ -208,14 +209,60 @@ public function register_rest_fields() { return \get_post_meta( $post['id'], 'article_url', true ); }, 'update_callback' => function ( $value, $post ) { - \update_post_meta( $post->ID, 'article_url', \esc_url_raw( $value ) ); + // Handle array input from Make.com + $url_value = is_array( $value ) && ! empty( $value ) ? $value[0] : $value; + if ( ! empty( $url_value ) ) { + \update_post_meta( $post->ID, 'article_url', \esc_url_raw( $url_value ) ); + } }, 'schema' => array( - 'type' => 'string', + 'type' => array( 'string', 'array' ), + 'items' => array( 'type' => 'string' ), 'description' => 'Article URL', 'required' => true, ), ), ); + + \register_rest_field( + self::SLUG, + 'publication_name', + array( + 'get_callback' => function ( $post ) { + $terms = \get_the_terms( $post['id'], 'taxonomy-publication' ); + if ( ! empty( $terms ) && ! \is_wp_error( $terms ) ) { + return $terms[0]->name; + } + return ''; + }, + 'update_callback' => function ( $value, $post ) { + // Handle array input from Make.com + $pub_value = is_array( $value ) && ! empty( $value ) ? $value[0] : $value; + if ( ! empty( $pub_value ) ) { + $term = \get_term_by( 'name', $pub_value, 'taxonomy-publication' ); + + if ( ! $term ) { + $new_term = \wp_insert_term( $pub_value, 'taxonomy-publication' ); + if ( ! \is_wp_error( $new_term ) ) { + $term_id = $new_term['term_id']; + } + } else { + $term_id = $term->term_id; + } + + if ( isset( $term_id ) ) { + // Set the taxonomy term for the post + \wp_set_object_terms( $post->ID, array( $term_id ), 'taxonomy-publication' ); + } + } + }, + 'schema' => array( + 'type' => array( 'string', 'array' ), + 'items' => array( 'type' => 'string' ), + 'description' => 'Publication Name', + 'required' => true, + ), + ), + ); } } diff --git a/plugins/osi-features/inc/classes/taxonomies/class-taxonomy-publication.php b/plugins/osi-features/inc/classes/taxonomies/class-taxonomy-publication.php index 16ef9f8..b7052f6 100644 --- a/plugins/osi-features/inc/classes/taxonomies/class-taxonomy-publication.php +++ b/plugins/osi-features/inc/classes/taxonomies/class-taxonomy-publication.php @@ -28,7 +28,7 @@ class Taxonomy_Publication extends Base { */ public function get_labels() { - return [ + return array( 'name' => _x( 'Publication', 'taxonomy general name', 'osi-features' ), 'singular_name' => _x( 'Publication', 'taxonomy singular name', 'osi-features' ), 'search_items' => __( 'Search Publication', 'osi-features' ), @@ -45,8 +45,7 @@ public function get_labels() { 'choose_from_most_used' => __( 'Choose from the most used Publications', 'osi-features' ), 'not_found' => __( 'No Publication found.', 'osi-features' ), 'menu_name' => __( 'Publications', 'osi-features' ), - ]; - + ); } /** @@ -56,10 +55,9 @@ public function get_labels() { */ public function get_post_types() { - return [ + return array( Post_Type_Press_Mentions::get_instance()->get_slug(), - ]; - + ); } /** @@ -68,18 +66,17 @@ public function get_post_types() { * @return array */ public function get_args() { - - return wp_parse_args( - [ + + return wp_parse_args( + array( 'hierarchical' => true, + 'show_in_rest' => true, 'rewrite' => array( - 'slug' => 'publication', + 'slug' => 'publication', 'with_front' => false, ), - - ], + ), parent::get_args() ); } - }