Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
5.4 kB
3
Indexable
<?php

// Enqueue child theme style.css
add_action( 'wp_enqueue_scripts', function() {
    wp_enqueue_style( 'child-style', get_stylesheet_uri() );

    if ( is_rtl() ) {
        wp_enqueue_style( 'mylisting-rtl', get_template_directory_uri() . '/rtl.css', [], wp_get_theme()->get('Version') );
    }
}, 500 );

// Enable REST API for job listings
function listings_rest_api() {
    global $wp_post_types;
    if (isset($wp_post_types['job_listing'])) {
        $wp_post_types['job_listing']->show_in_rest = true;
        $wp_post_types['job_listing']->rest_base = 'listings';
    }
}
add_action('init', 'listings_rest_api', 30);

// Register custom fields in REST API
function register_custom_fields_in_rest() {
    $fields_to_register = [
        '_case27_listing_type',
        '_job_tagline',
        '_job_location',
        '_job_category',
        '_job_tags',
        'job_description',
        '_job_email',
        '_job_logo',
        '_job_cover',
        '_job_gallery',
        '_job_website',
        '_job_phone',
        '_job_video_url',
        '_job_date',
        '_work_hours',
        '_comments',
        '_event-images',
        '_contact-information',
        '_event-details'
    ];

    foreach ($fields_to_register as $field_key) {
        register_rest_field(
            'job_listing',
            $field_key,
            [
                'get_callback'    => function ($object, $field_name, $request) {
                    return get_post_meta($object['id'], $field_name, true);
                },
                'update_callback' => function ($value, $object, $field_name) {
                    if (!empty($value)) {
                        update_post_meta($object->ID, $field_name, $value);
                    }
                },
                'schema'          => null,
            ]
        );
    }
}
add_action('rest_api_init', 'register_custom_fields_in_rest');

// Function to update event data via REST API
function update_event_data($post_id, $data) {
    // Update title
    if (isset($data['title'])) {
        wp_update_post([
            'ID' => $post_id,
            'post_title' => $data['title']
        ]);
    }
	
	// Update content
    if (isset($data['content'])) {
        wp_update_post([
            'ID' => $post_id,
            'post_content' => $data['content']
        ]);
    }

    // Update meta fields
    if (isset($data['meta']) && is_array($data['meta'])) {
        foreach ($data['meta'] as $meta_key => $meta_value) {
            update_post_meta($post_id, $meta_key, $meta_value);
        }
    }

    // Other fields update
    $fields_to_update = [
        '_job_tagline',
        '_job_location',
        '_job_category',
        '_job_tags',
		'job_description',
        '_job_email',
        '_job_logo',
        '_job_cover',
        '_job_gallery',
        '_job_website',
        '_job_phone',
        '_job_video_url',
        '_job_date',
        '_work_hours',
        '_comments',
        '_event-images',
        '_contact-information',
        '_event-details'
    ];

    foreach ($fields_to_update as $field) {
        if (isset($data[$field])) {
            update_post_meta($post_id, $field, $data[$field]);
        }
    }

    // Update listing type
    if (isset($data['_case27_listing_type'])) {
        update_post_meta($post_id, '_case27_listing_type', $data['_case27_listing_type']);
    }
}

// Add a filter to ensure 'post_content' is updated via the REST API
function my_rest_api_insert_post($post, $request) {
    if (isset($request['content'])) {
        $post->post_content = $request['content'];
    }
    return $post;
}
add_filter('rest_pre_insert_post', 'my_rest_api_insert_post', 10, 2);

// Register a custom REST route to update event data
add_action('rest_api_init', function() {
    register_rest_route('myplugin/v1', '/update_event/', array(
        'methods' => 'POST',
        'callback' => function($request) {
            $data = $request->get_json_params();
            $post_id = $data['post_id']; // Ensure the post ID is passed

            update_event_data($post_id, $data);

            return new WP_REST_Response('Event updated successfully', 200);
        },
        'permission_callback' => function () {
            return current_user_can('edit_posts');
        }
    ));
});

// Function to get comments with ratings
function get_comments_with_ratings($post_id) {
    $comments = get_comments(['post_id' => $post_id]);
    foreach ($comments as &$comment) {
        $comment->rating = \MyListing\Ext\Reviews\Reviews::get_rating($comment->comment_ID);
        $comment->ratings = get_comment_ratings($comment->comment_ID, $post_id);
    }
    return $comments;
}

// Function to get ratings for a comment
function get_comment_ratings($comment_id, $post_id) {
    $categories = \MyListing\Ext\Reviews\Reviews::get_review_categories($post_id);
    $ratings = get_comment_meta($comment_id, '_case27_ratings', true);
    $ratings = is_array($ratings) ? $ratings : [];

    $result = [];
    foreach ($categories as $category) {
        $category_id = $category['id'];
        $category_label = $category['label'];
        $rating_value = isset($ratings[$category_id]) ? $ratings[$category_id] : 0;
        $result[] = [
            'category' => $category_label,
            'rating' => $rating_value,
        ];
    }

    return $result;
}

?>
Leave a Comment