demo-content
2 weeks ago
author-portfolio-pro-thumb.png
2 weeks ago
demo-setup-admin-notice.php
2 weeks ago
demo-setup.js
2 weeks ago
demo-setup.php
2 weeks ago
handle-ajax-request.php
2 weeks ago
import-books-from-json.php
2 weeks ago
import-elementor-template.php
2 weeks ago
import-posts-from-json.php
2 weeks ago
install-activate-plugins.php
2 weeks ago
import-posts-from-json.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Import Posts From JSON |
| 4 | */ |
| 5 | function rswpthemes_import_posts_from_url($json_file_url) { |
| 6 | $response = wp_remote_get($json_file_url); |
| 7 | if (is_wp_error($response)) return new WP_Error('fetch_error', 'Unable to retrieve JSON file.'); |
| 8 | |
| 9 | $json_input = wp_remote_retrieve_body($response); |
| 10 | $posts = json_decode($json_input, true); |
| 11 | |
| 12 | if (empty($posts) || !is_array($posts)) return new WP_Error('empty_data', 'No post data found.'); |
| 13 | |
| 14 | foreach ($posts as $post_data) { |
| 15 | $post_title = sanitize_text_field($post_data['title']); |
| 16 | $post_content = wp_kses_post($post_data['content']); |
| 17 | $post_excerpt = wp_kses_post($post_data['excerpt']); |
| 18 | |
| 19 | // � |
| 20 | FIXED: Use WP_Query instead of deprecated get_page_by_title() |
| 21 | $existing_post_title = rswpthemes_awt_new_get_page_by_title($post_title, OBJECT, 'post'); |
| 22 | |
| 23 | if ($existing_post_title) { |
| 24 | wp_reset_postdata(); |
| 25 | continue; // Skip if post exists |
| 26 | } |
| 27 | |
| 28 | $post_id = wp_insert_post([ |
| 29 | 'post_title' => $post_title, |
| 30 | 'post_content' => $post_content, |
| 31 | 'post_excerpt' => $post_excerpt, |
| 32 | 'post_status' => 'publish', |
| 33 | 'post_type' => 'post' |
| 34 | ]); |
| 35 | |
| 36 | if (is_wp_error($post_id)) continue; |
| 37 | |
| 38 | // � |
| 39 | FIXED: Handle WP_Term properly |
| 40 | $category_ids = []; |
| 41 | foreach ($post_data['categories'] as $category_name) { |
| 42 | $category = get_term_by('name', $category_name, 'category'); |
| 43 | if (!$category) { |
| 44 | $category = wp_insert_term($category_name, 'category'); |
| 45 | } |
| 46 | if (!is_wp_error($category)) { |
| 47 | $category_ids[] = $category instanceof WP_Term ? $category->term_id : $category['term_id']; |
| 48 | } |
| 49 | } |
| 50 | wp_set_post_categories($post_id, $category_ids); |
| 51 | |
| 52 | // � |
| 53 | FIXED: Assign tags correctly |
| 54 | $tag_ids = []; |
| 55 | foreach ($post_data['tags'] as $tag_name) { |
| 56 | $tag = get_term_by('name', $tag_name, 'post_tag'); |
| 57 | if (!$tag) { |
| 58 | $tag = wp_insert_term($tag_name, 'post_tag'); |
| 59 | } |
| 60 | if (!is_wp_error($tag)) { |
| 61 | $tag_ids[] = $tag instanceof WP_Term ? $tag->term_id : $tag['term_id']; |
| 62 | } |
| 63 | } |
| 64 | wp_set_post_tags($post_id, $tag_ids); |
| 65 | |
| 66 | // � |
| 67 | Handle Featured Image |
| 68 | if (!empty($post_data['featured_image'])) { |
| 69 | rswpthemes_awt_set_featured_image_from_url($post_id, $post_data['featured_image']); |
| 70 | } |
| 71 | |
| 72 | // � |
| 73 | Handle Custom Meta Fields |
| 74 | if (!empty($post_data['custom_meta'])) { |
| 75 | foreach ($post_data['custom_meta'] as $meta_key => $meta_value) { |
| 76 | update_post_meta($post_id, sanitize_key($meta_key), sanitize_text_field($meta_value)); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | return count($posts); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Import Book Reviews From Json |
| 85 | */ |
| 86 | |
| 87 | function rswpthemes_import_reviews_from_url($json_file_url) { |
| 88 | $response = wp_remote_get($json_file_url); |
| 89 | if (is_wp_error($response)) return new WP_Error('fetch_error', 'Unable to retrieve JSON file.'); |
| 90 | |
| 91 | $json_input = wp_remote_retrieve_body($response); |
| 92 | $reviews = json_decode($json_input, true); |
| 93 | |
| 94 | if (empty($reviews) || !is_array($reviews)) return new WP_Error('empty_data', 'No review data found.'); |
| 95 | |
| 96 | $import_count = 0; |
| 97 | |
| 98 | foreach ($reviews as $review_data) { |
| 99 | $review_title = sanitize_text_field($review_data['title']); |
| 100 | $review_content = wp_kses_post($review_data['content']); |
| 101 | |
| 102 | // � |
| 103 | Check if review already exists using WP_Query (instead of deprecated get_page_by_title) |
| 104 | $existing_review_query = new WP_Query([ |
| 105 | 'post_type' => 'book_reviews', |
| 106 | 'posts_per_page' => 1, |
| 107 | 'title' => $review_title |
| 108 | ]); |
| 109 | |
| 110 | if ($existing_review_query->have_posts()) { |
| 111 | wp_reset_postdata(); |
| 112 | continue; // � |
| 113 | Skip if review already exists |
| 114 | } |
| 115 | wp_reset_postdata(); |
| 116 | |
| 117 | // � |
| 118 | Insert new review post |
| 119 | $review_id = wp_insert_post([ |
| 120 | 'post_title' => $review_title, |
| 121 | 'post_content' => $review_content, |
| 122 | 'post_status' => 'publish', |
| 123 | 'post_type' => 'book_reviews' |
| 124 | ]); |
| 125 | |
| 126 | if (is_wp_error($review_id)) continue; |
| 127 | |
| 128 | // � |
| 129 | Set Featured Image if available |
| 130 | if (!empty($review_data['featured_image'])) { |
| 131 | rswpthemes_awt_set_featured_image_from_url($review_id, $review_data['featured_image']); |
| 132 | } |
| 133 | |
| 134 | // � |
| 135 | Import Custom Meta Fields (Reviewer Name, Email, Rating, Reviewed Book) |
| 136 | if (!empty($review_data['meta_data'])) { |
| 137 | foreach ($review_data['meta_data'] as $meta_key => $meta_value) { |
| 138 | update_post_meta($review_id, sanitize_key($meta_key), sanitize_text_field($meta_value)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | $import_count++; |
| 143 | } |
| 144 | |
| 145 | return $import_count; // � |
| 146 | Return the count of imported reviews |
| 147 | } |
| 148 |