File: /mnt/data/doccure-wp/wp-content/plugins/doccure/includes/doccurecpt.php
<?php
function doccure_posttype() {
register_post_type( 'services',
// CPT Options
array(
'labels' => array(
'name' => __( 'Services' ),
'singular_name' => __( 'Service' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title','editor','thumbnail'),
'rewrite' => array('slug' => 'services'),
'show_in_rest' => true,
)
);
register_post_type( 'testimonials',
// CPT Options
array(
'labels' => array(
'name' => __( 'Testimonial' ),
'singular_name' => __( 'testimonials' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title','editor','thumbnail','custom-fields','excerpt'),
'rewrite' => array('slug' => 'testimonial'),
'show_in_rest' => true,
)
);
register_post_type( 'portfolio',
// CPT Options
array(
'labels' => array(
'name' => __( 'Portfolio' ),
'singular_name' => __( 'Portfolio' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title','editor','thumbnail'),
'rewrite' => array('slug' => 'portfolio'),
'show_in_rest' => true,
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'doccure_posttype' );
// Add Meta Boxes for Title and Location
function add_testimonial_meta_boxes() {
add_meta_box(
'testimonial_title', // ID
'Testimonial Title', // Title
'render_title_meta_box', // Callback
'testimonials', // Post type
'normal', // Context
'high' // Priority
);
add_meta_box(
'testimonial_location', // ID
'Location', // Title
'render_location_meta_box', // Callback
'testimonials', // Post type
'normal', // Context
'high' // Priority
);
}
add_action( 'add_meta_boxes', 'add_testimonial_meta_boxes' );
// Render the Title Meta Box
function render_title_meta_box( $post ) {
// Use nonce for verification
wp_nonce_field( 'title_meta_box', 'title_meta_box_nonce' );
// Retrieve existing value from the database
$title = get_post_meta( $post->ID, '_testimonial_title', true );
// Display the form field
echo '<label for="testimonial_title">Title: </label>';
echo '<input type="text" id="testimonial_title" name="testimonial_title" value="' . esc_attr( $title ) . '" />';
}
// Render the Location Meta Box
function render_location_meta_box( $post ) {
// Use nonce for verification
wp_nonce_field( 'location_meta_box', 'location_meta_box_nonce' );
// Retrieve existing value from the database
$location = get_post_meta( $post->ID, '_testimonial_location', true );
// Display the form field
echo '<label for="testimonial_location">Location: </label>';
echo '<input type="text" id="testimonial_location" name="testimonial_location" value="' . esc_attr( $location ) . '" />';
}
// Save the Meta Box Data
function save_testimonial_meta_box_data( $post_id ) {
// Check if our nonce is set for title.
if ( ! isset( $_POST['title_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid for title.
if ( ! wp_verify_nonce( $_POST['title_meta_box_nonce'], 'title_meta_box' ) ) {
return;
}
// Check if this is an autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check user permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Sanitize and save the title field
if ( isset( $_POST['testimonial_title'] ) ) {
$title = sanitize_text_field( $_POST['testimonial_title'] );
update_post_meta( $post_id, '_testimonial_title', $title );
}
// Check if our nonce is set for location.
if ( ! isset( $_POST['location_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid for location.
if ( ! wp_verify_nonce( $_POST['location_meta_box_nonce'], 'location_meta_box' ) ) {
return;
}
// Sanitize and save the location field
if ( isset( $_POST['testimonial_location'] ) ) {
$location = sanitize_text_field( $_POST['testimonial_location'] );
update_post_meta( $post_id, '_testimonial_location', $location );
}
}
add_action( 'save_post', 'save_testimonial_meta_box_data' );
function doccure_send_email( $email, $subject, $message, $headers = array() ) {
wp_mail( $email, $subject, $message, $headers );
}
/**
* Get the service image URL (featured image if available, otherwise first attached image, otherwise theme fallback)
*/
function doccure_get_service_image_url( $post_id, $size = 'full' ) {
if ( has_post_thumbnail( $post_id ) ) {
$url = wp_get_attachment_image_url( get_post_thumbnail_id( $post_id ), $size );
if ( $url ) {
return $url;
}
}
$attachments = get_children( array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 1,
'orderby' => 'menu_order ID',
'order' => 'ASC',
) );
if ( ! empty( $attachments ) ) {
$attachment = array_shift( $attachments );
$url = wp_get_attachment_image_url( $attachment->ID, $size );
if ( $url ) {
return $url;
}
}
// Fallback placeholder from theme
return get_template_directory_uri() . '/assets/images/dravatar-100x100.jpg';
}
/**
* When saving a services post, if no featured image is set, use the first attached image as thumbnail.
*/
function doccure_services_autoset_featured_image( $post_id, $post, $update ) {
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
return;
}
if ( $post->post_type !== 'services' ) {
return;
}
if ( has_post_thumbnail( $post_id ) ) {
return;
}
$attachments = get_children( array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 1,
'orderby' => 'menu_order ID',
'order' => 'ASC',
) );
if ( ! empty( $attachments ) ) {
$attachment = array_shift( $attachments );
set_post_thumbnail( $post_id, $attachment->ID );
}
}
add_action( 'save_post', 'doccure_services_autoset_featured_image', 10, 3 );