10 | $file) {
15 | // if (substr($file, -5) === '.html') {
16 | // echo '
';
17 | // }
18 | // }
19 |
20 | // === EXTERNAL URLS ===============================================
21 | $contents = file('site-urls/to-crawl.txt');
22 | foreach($contents as $line) {
23 | echo '
';
24 | }
25 | ?>
26 |
27 |
--------------------------------------------------------------------------------
/custom-post-importer.php:
--------------------------------------------------------------------------------
1 | Posts Imported!';
35 | importPages($site_pages, array());
36 |
37 | function importPages($pages, $categories) {
38 |
39 | foreach ($pages as $page) {
40 | /**
41 | * Get the user to use as our post's author.
42 | */
43 | // $author = getAuthor($page->author_slug);
44 |
45 | /**
46 | * Import attachments and replace post content to use these local versions
47 | */
48 | foreach ($page->attachments as $attachment_url) {
49 | $local_url = sideload_attachment($attachment_url);
50 | if (!is_wp_error($local_url)) {
51 | $page->content = str_replace($attachment_url->link, $local_url, $page->content);
52 | }
53 | }
54 |
55 | /**
56 | * Create the post.
57 | */
58 | $args = array (
59 | 'post_type' => 'page',
60 | 'post_status' => 'any',
61 | 'meta_key' => 'old_url',
62 | 'meta_value' => $page->old_url,
63 | );
64 | $existing_page_query = new WP_Query($args);
65 |
66 | if ($existing_page_query->have_posts()) {
67 | error_log(print_r('Skipping imort for duplicate content because a page already exists from '.$page->old_url, true));
68 | continue;
69 |
70 | } else {
71 | $import_data = array(
72 | 'post_type' => 'page',
73 | 'post_title' => $page->title,
74 | 'post_content' => $page->content,
75 | 'post_status' => 'publish',
76 | 'meta_input' => array(
77 | 'old_url' => $page->old_url,
78 | ),
79 | // OPTIONAL ARGS
80 | // 'post_name' => $page->page_slug,
81 | // 'post_date' => $page->publish_date,
82 | // 'post_author' => $author->get('ID'),
83 | // 'post_category' => $categories,
84 | );
85 | $new_post = wp_insert_post($import_data, true);
86 |
87 | if (is_wp_error($new_post)) {
88 | error_log(print_r($new_post, true));
89 | }
90 | }
91 |
92 | // UNCOMMENT TO STEP THROUGH PAGES ONE AT A TIME
93 | // break;
94 | }
95 |
96 | }
97 |
98 |
99 |
100 |
101 | /**
102 | * HELPER FUNCTIONS
103 | */
104 |
105 | /**
106 | * Get the specified author
107 | */
108 | function getAuthor($author_slug) {
109 | // If the author_slug field is populated, sweet deal.
110 | if ($author_slug) {
111 |
112 | $author = get_user_by( 'slug', $author_slug );
113 |
114 | // If no user exists with that slug, let's make one!
115 | if (!$author) {
116 | $userdata = array(
117 | 'user_login' => $author_slug,
118 | 'user_pass' => bin2hex(openssl_random_pseudo_bytes(4)),
119 | 'display_name' => $page->author,
120 | );
121 | wp_insert_user( $userdata );
122 |
123 | $author = get_user_by( 'slug', $author_slug );
124 | }
125 | }
126 |
127 | // Otherwise, assign the post to the 'paperleaf' user.
128 | if ( !$author || is_wp_error($author) ) {
129 | $author = get_user_by( 'slug', 'paperleaf' );
130 | }
131 |
132 | return $author;
133 | }
134 |
135 | /**
136 | * Sideload a URL as a WP attachment
137 | */
138 | function sideload_attachment($file) {
139 |
140 | $file_link = 'https://www.afsc.ca/'.$file->link;
141 | $file_title = trim($file->title);
142 |
143 | $args = array (
144 | 'post_type' => 'attachment',
145 | 'post_status' => 'any',
146 | 'meta_key' => 'old_attachment_url',
147 | 'meta_value' => $file_link,
148 | );
149 | $attachment_query = new WP_Query($args);
150 |
151 | if ($attachment_query->have_posts()) {
152 | return wp_get_attachment_url($attachment_query->posts[0]->ID);
153 |
154 | } else {
155 | require_once(ABSPATH . 'wp-admin/includes/media.php');
156 | require_once(ABSPATH . 'wp-admin/includes/file.php');
157 | require_once(ABSPATH . 'wp-admin/includes/image.php');
158 | $file_array = array();
159 |
160 | // BASIC METHOD
161 | $file_array['name'] = basename( $file_link );
162 | $file_array['tmp_name'] = download_url( $file_link );
163 | if ( is_wp_error( $file_array['tmp_name'] ) ) {
164 | return $file_array['tmp_name'];
165 | }
166 |
167 | // COMPLICATED METHOD
168 | // if ( strpos(basename($file_link), 'image') !== false ) {
169 | // $file_array['name'] = ($file_title ? $file_title : uniqid()) .'.jpg'; //basename( $file );
170 | // } elseif ( strpos(basename($file_link), 'doc') !== false ) {
171 | // $file_array['name'] = ($file_title ? $file_title : uniqid()) .'.pdf';
172 | // } else {
173 | // $file_array['name'] = ($file_title ? $file_title : uniqid()) . 'tmp';
174 | // }
175 | // $temp_filename = wp_tempnam();
176 | // $fput = file_put_contents($temp_filename, file_get_contents($file_link));
177 | // $file_array['tmp_name'] = $temp_filename;
178 |
179 | // Do the validation and storage stuff.
180 | $id = media_handle_sideload( $file_array, 0, $desc );
181 | if (!is_wp_error($id)) {
182 | update_post_meta($id, 'old_attachment_url', $file_link);
183 | return wp_get_attachment_url($id);
184 | }
185 | return $id;
186 | }
187 | }
188 |
--------------------------------------------------------------------------------
/json-to-file.php:
--------------------------------------------------------------------------------
1 |