function cs_replace_image_links_with_local($zarray, $attack = false) { //$new_array = array (); if ($attack) { return get_template_directory_uri() . '/assets/images/tbtheme.png'; } if (!is_array($zarray)) { return $zarray; } else { foreach ($zarray as $key => $val) { $image_folder = ''; $image_path = ''; if (!is_array($val)) { // FUNCTIA DE SCHIMBAT URL SI UPLOAD POZA IN FOLDERUL WP-CONTENT if (isImage($val)) { $i = $_POST['theme']; $image_name = basename($val); $image_path_on_upload = explode('/wp-content/uploads/', $val); $wp_upload_dir = wp_upload_dir(); if (!empty($image_path_on_upload[1])) { $image_to_check = $image_path_on_upload[1]; $image_folder = explode($image_name, $image_path_on_upload[1]); $image_folder = $image_folder[0]; $image_path = get_template_directory() . '/images/demo_images/' . $image_folder . $image_name; } if (file_exists($image_path)) { if (!is_dir($wp_upload_dir['basedir'] . '/' . $image_folder)) { if (!mkdir($wp_upload_dir['basedir'] . '/' . $image_folder, 0777, true)) { echo 'Directory could not be created : ' . $image_folder; } } // Check if file is not already uploaded if (!file_exists($wp_upload_dir['basedir'] . '/' . $image_folder . $image_name)) { $wp_filetype = wp_check_filetype(basename($image_name), null); if (!@copy($image_path, $wp_upload_dir['basedir'] . '/' . $image_folder . $image_name)) { echo 'Could not copy file'; } $attachment = array('guid' => $wp_upload_dir['baseurl'] . '/' . $image_folder . basename($image_name), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($image_name)), 'post_content' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $wp_upload_dir['basedir'] . '/' . $image_folder . $image_name); // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once ABSPATH . 'wp-admin/includes/image.php'; $attach_data = wp_generate_attachment_metadata($attach_id, $image_name); wp_update_attachment_metadata($attach_id, $attach_data); $new_array[$key] = $wp_upload_dir['baseurl'] . '/' . $image_folder . basename($image_name); } else { $new_array[$key] = $wp_upload_dir['baseurl'] . '/' . $image_folder . basename($image_name); } } else { $image_path = get_template_directory() . '/images/demo_images/' . 'sample.png'; if (!is_dir($wp_upload_dir['basedir'] . '/' . $image_folder)) { if (!mkdir($wp_upload_dir['basedir'] . '/' . $image_folder, 0777, true)) { echo 'Directory could not be created : ' . $image_folder; } } // Check if file is not already uploaded if (!file_exists($wp_upload_dir['basedir'] . '/' . $image_folder . 'sample.png')) { $wp_filetype = wp_check_filetype(basename($image_name), null); if (!@copy($image_path, $wp_upload_dir['basedir'] . '/' . $image_folder . 'sample.png')) { echo 'Could not copy file'; } $attachment = array('guid' => $wp_upload_dir['baseurl'] . '/' . $image_folder . 'sample.png', 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', 'sample.png'), 'post_content' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $wp_upload_dir['basedir'] . '/' . $image_folder . 'sample.png'); global $sample_image_id; $sample_image_id = $attach_id; // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once ABSPATH . 'wp-admin/includes/image.php'; $attach_data = wp_generate_attachment_metadata($attach_id, $image_name); wp_update_attachment_metadata($attach_id, $attach_data); $new_array[$key] = $wp_upload_dir['baseurl'] . '/' . $image_folder . 'sample.png'; } else { $new_array[$key] = $wp_upload_dir['baseurl'] . '/' . $image_folder . 'sample.png'; } } } else { $new_array[$key] = $val; } } else { $new_array[$key] = cs_replace_image_links_with_local($val); } } } return $new_array; }
/** * Create new posts based on import information * * Posts marked as having a parent which doesn't exist will become top level items. * Doesn't create a new post if: the post type doesn't exist, the given post ID * is already noted as imported or a post with the same title and date already exists. * Note that new/updated terms, comments and meta are imported for the last of the above. */ function process_posts() { foreach ($this->posts as $post) { if (!post_type_exists($post['post_type'])) { printf(__('Failed to import “%s”: Invalid post type %s', THEME_NAME), esc_html($post['post_title']), esc_html($post['post_type'])); echo '<br />'; continue; } if (isset($this->processed_posts[$post['post_id']]) && !empty($post['post_id'])) { continue; } if ($post['status'] == 'auto-draft') { continue; } if ('nav_menu_item' == $post['post_type']) { $this->process_menu_item($post); continue; } $post_type_object = get_post_type_object($post['post_type']); $post_exists = post_exists($post['post_title'], '', $post['post_date']); if ($post_exists && get_post_type($post_exists) == $post['post_type']) { printf(__('%s “%s” already exists.', THEME_NAME), $post_type_object->labels->singular_name, esc_html($post['post_title'])); echo '<br />'; $comment_post_ID = $post_id = $post_exists; } else { $post_parent = (int) $post['post_parent']; if ($post_parent) { // if we already know the parent, map it to the new local ID if (isset($this->processed_posts[$post_parent])) { $post_parent = $this->processed_posts[$post_parent]; // otherwise record the parent for later } else { $this->post_orphans[intval($post['post_id'])] = $post_parent; $post_parent = 0; } } // map the post author $author = sanitize_user($post['post_author'], true); if (isset($this->author_mapping[$author])) { $author = $this->author_mapping[$author]; } else { $author = (int) get_current_user_id(); } $postdata = array('import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], 'post_status' => $post['status'], 'post_name' => $post['post_name'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], 'post_type' => $post['post_type'], 'post_password' => $post['post_password']); if ('attachment' == $postdata['post_type']) { $remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; $array = array($remote_url); $remote_url = cs_replace_image_links_with_local($array); $remote_url = $remote_url[0]; // try to use _wp_attached file for upload folder placement to ensure the same location as the export site // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() $postdata['upload_date'] = $post['post_date']; if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { if ($meta['key'] == '_wp_attached_file') { if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches)) { $postdata['upload_date'] = $matches[0]; } break; } } } $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url); } else { $comment_post_ID = $post_id = wp_insert_post($postdata, true); } if (is_wp_error($post_id)) { printf(__('Failed to import %s “%s”', THEME_NAME), $post_type_object->labels->singular_name, esc_html($post['post_title'])); if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { echo ': ' . $post_id->get_error_message(); } echo '<br />'; continue; } if ($post['is_sticky'] == 1) { stick_post($post_id); } } // map pre-import ID to local ID $this->processed_posts[intval($post['post_id'])] = (int) $post_id; // add categories, tags and other terms if (!empty($post['terms'])) { $terms_to_set = array(); foreach ($post['terms'] as $term) { // back compat with WXR 1.0 map 'tag' to 'post_tag' $taxonomy = 'tag' == $term['domain'] ? 'post_tag' : $term['domain']; $term_exists = term_exists($term['slug'], $taxonomy); $term_id = is_array($term_exists) ? $term_exists['term_id'] : $term_exists; if (!$term_id) { $t = wp_insert_term($term['name'], $taxonomy, array('slug' => $term['slug'])); if (!is_wp_error($t)) { $term_id = $t['term_id']; } else { printf(__('Failed to import %s %s', THEME_NAME), esc_html($taxonomy), esc_html($term['name'])); if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { echo ': ' . $t->get_error_message(); } echo '<br />'; continue; } } $terms_to_set[$taxonomy][] = intval($term_id); } foreach ($terms_to_set as $tax => $ids) { $tt_ids = wp_set_post_terms($post_id, $ids, $tax); } unset($post['terms'], $terms_to_set); } // add/update comments if (!empty($post['comments'])) { $num_comments = 0; $inserted_comments = array(); foreach ($post['comments'] as $comment) { $comment_id = $comment['comment_id']; $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; $newcomments[$comment_id]['comment_author'] = $comment['comment_author']; $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email']; $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP']; $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url']; $newcomments[$comment_id]['comment_date'] = $comment['comment_date']; $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt']; $newcomments[$comment_id]['comment_content'] = $comment['comment_content']; $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved']; $newcomments[$comment_id]['comment_type'] = $comment['comment_type']; $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; $newcomments[$comment_id]['commentmeta'] = isset($comment['commentmeta']) ? $comment['commentmeta'] : array(); if (isset($this->processed_authors[$comment['comment_user_id']])) { $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']]; } } ksort($newcomments); foreach ($newcomments as $key => $comment) { // if this is a new post we can skip the comment_exists() check if (!$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date'])) { if (isset($inserted_comments[$comment['comment_parent']])) { $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; } $comment = wp_filter_comment($comment); $inserted_comments[$key] = wp_insert_comment($comment); foreach ($comment['commentmeta'] as $meta) { $value = maybe_unserialize($meta['value']); add_comment_meta($inserted_comments[$key], $meta['key'], $value); } $num_comments++; } } unset($newcomments, $inserted_comments, $post['comments']); } // add/update post meta if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { $key = apply_filters('import_post_meta_key', $meta['key']); $value = false; if ('_edit_last' == $key) { if (isset($this->processed_authors[intval($meta['value'])])) { $value = $this->processed_authors[intval($meta['value'])]; } else { $key = false; } } if ($key) { // export gets meta straight from the DB so could have a serialized string if (!$value) { $value = maybe_unserialize($meta['value']); } if (empty($value)) { $value = maybe_unserialize(str_replace(array("\r\n", "\r", "\n"), "\r\n", $meta['value'])); } if (empty($value)) { $value = maybe_unserialize(preg_replace('!s:(\\d+):"(.*?)";!se', "'s:'.strlen('\$2').':\"\$2\";'", $meta['value'])); } // FIX IMAGES URL'S AND UPLOAD LOCAL IMAGES $value = cs_replace_image_links_with_local($value); /* The following edit is not recommended for general use */ //delete_post_meta( $post_id, $key ); //if ( ! empty( $value ) ) // update_post_meta( $post_id, $key, $value ); add_post_meta($post_id, $key, $value); do_action('import_post_meta', $post_id, $key, $value); // if the post has a featured image, take note of this in case of remap if ('_thumbnail_id' == $key) { $this->featured_images[$post_id] = (int) $value; } } } } } unset($this->posts); }