Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
),
),
Expand All @@ -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,
),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand All @@ -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' ),
];

);
}

/**
Expand All @@ -56,10 +55,9 @@ public function get_labels() {
*/
public function get_post_types() {

return [
return array(
Post_Type_Press_Mentions::get_instance()->get_slug(),
];

);
}

/**
Expand All @@ -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()
);
}

}
Loading