process_attachment() public méthode

If fetching attachments is enabled then attempt to create a new attachment
public process_attachment ( array $post, string $url ) : integer | WP_Error
$post array Attachment post details from WXR
$url string URL to fetch attachment from
Résultat integer | WP_Error Post ID on success, WP_Error otherwise
 public function process_attachment($post, $url)
 {
     $post_id = $post["import_id"];
     $xml = simplexml_load_file($this->file);
     $result = $xml->xpath("channel/wg_custom_attachment_url/attachment[post_id='{$post_id}']/custom_url");
     $custom_url = $result != false ? $result[0] : '';
     if ($custom_url != '') {
         $url = THEME_CUSTOM_URI . "/demo/" . $custom_url;
     }
     $return = parent::process_attachment($post, $url);
     return $return;
 }
Exemple #2
0
 public function process_attachment($postdata, $remote_url)
 {
     $this->updateStats();
     if (count($this->aImported) < 4) {
         if (!$this->origUploadUrl) {
             $tokens = pathinfo($postdata["guid"]);
             $this->origUploadUrl = preg_replace("#/\\d+/\\d+/?#", "", $tokens["dirname"]);
         }
         $current = $this->aCount + 1;
         $remote_url = PE_THEME_URL . "/demo/img{$current}.jpg";
         $postdata["post_name"] = $postdata["post_title"];
         $id = parent::process_attachment($postdata, $remote_url);
         if ($id) {
             $this->aImported[] = $id;
             $this->aCount++;
         }
         return $id;
     }
     return new WP_Error('broke', __("I've fallen and can't get up", 'Pixelentity Theme/Plugin'));
 }
function om_ajax_import_tool()
{
    if (!current_user_can('manage_options')) {
        die;
    }
    if (get_magic_quotes_gpc()) {
        $_POST = stripslashes_deep($_POST);
    }
    if (!isset($_POST['om_action'])) {
        die;
    }
    switch ($_POST['om_action']) {
        case 'start':
            $data = array('error' => 0);
            if (!file_exists($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['wordpress_xml'])) {
                $data['error'] = 1;
                wp_send_json($data);
            }
            if (!class_exists('WXR_Parser')) {
                require $GLOBALS['omImportTool']['path'] . 'includes/parsers.php';
            }
            $parser = new WXR_Parser();
            $import_data = $parser->parse($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['wordpress_xml']);
            unset($parser);
            if (is_wp_error($import_data)) {
                $data['error'] = 1;
                wp_send_json($data);
            }
            $data['common'] = array('base_url' => esc_url($import_data['base_url']));
            $data['attachments'] = array();
            $author = (int) get_current_user_id();
            foreach ($import_data['posts'] as $post) {
                if ('attachment' == $post['post_type']) {
                    $post_parent = (int) $post['post_parent'];
                    $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']);
                    $remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
                    // 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;
                            }
                        }
                    }
                    $postdata['postmeta'] = $post['postmeta'];
                    $data['attachments'][] = array('data' => $postdata, 'remote_url' => $remote_url);
                }
            }
            $data['last_attachment_index'] = -1;
            $variables_dump = get_option(OM_THEME_PREFIX . 'import_process_data');
            if (!empty($variables_dump) && is_array($variables_dump)) {
                if (isset($variables_dump['last_attachment_index'])) {
                    $data['last_attachment_index'] = $variables_dump['last_attachment_index'];
                }
            }
            wp_send_json($data);
            break;
        case 'process_attachments':
            $ret = array('error' => 0);
            if (isset($_POST['data']['attachments'])) {
                if (!defined('WP_LOAD_IMPORTERS')) {
                    define('WP_LOAD_IMPORTERS', true);
                }
                if (!class_exists('WP_Import')) {
                    // if WP importer doesn't exist
                    $wp_import = $GLOBALS['omImportTool']['path'] . 'includes/wordpress-importer.php';
                    include $wp_import;
                }
                if (class_exists('WP_Importer') && class_exists('WP_Import')) {
                    // check for main import class and wp import class
                    $importer = new WP_Import();
                    $importer->base_url = $_POST['data']['common']['base_url'];
                    $importer->fetch_attachments = true;
                    $variables_dump = get_option(OM_THEME_PREFIX . 'import_process_data');
                    if (!empty($variables_dump) && is_array($variables_dump)) {
                        $importer->post_orphans = $variables_dump['post_orphans'];
                        $importer->processed_posts = $variables_dump['processed_posts'];
                        $importer->url_remap = $variables_dump['url_remap'];
                    }
                    $last_attachment_index = $_POST['data']['first_attachment_index'];
                    foreach ($_POST['data']['attachments'] as $attachment) {
                        $post = $attachment['data'];
                        $importer->post_orphans[intval($post['import_id'])] = (int) $post['post_parent'];
                        $post['post_parent'] = 0;
                        $post_id = $importer->process_attachment($post, $attachment['remote_url']);
                        if (is_wp_error($post_id)) {
                            continue;
                        }
                        $importer->processed_posts[intval($post['import_id'])] = (int) $post_id;
                        // add/update post meta
                        if (!empty($post['postmeta'])) {
                            foreach ($post['postmeta'] as $meta) {
                                $key = $meta['key'];
                                $value = false;
                                if ('_edit_last' == $key) {
                                    continue;
                                }
                                if ($key) {
                                    // export gets meta straight from the DB so could have a serialized string
                                    if (!$value) {
                                        $value = maybe_unserialize($meta['value']);
                                    }
                                    add_post_meta($post_id, $key, $value);
                                }
                            }
                        }
                        $variables_dump['last_attachment_index'] = $last_attachment_index;
                        $last_attachment_index++;
                    }
                    $variables_dump['post_orphans'] = $importer->post_orphans;
                    $variables_dump['processed_posts'] = $importer->processed_posts;
                    $variables_dump['url_remap'] = $importer->url_remap;
                    update_option(OM_THEME_PREFIX . 'import_process_data', $variables_dump);
                }
            }
            wp_send_json($ret);
            break;
        case 'process_other':
            $ret = array('error' => 0);
            if (!file_exists($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['wordpress_xml'])) {
                $ret['error'] = 1;
                wp_send_json($ret);
            }
            if (!defined('WP_LOAD_IMPORTERS')) {
                define('WP_LOAD_IMPORTERS', true);
            }
            if (!class_exists('WP_Import')) {
                // if WP importer doesn't exist
                $wp_import = $GLOBALS['omImportTool']['path'] . 'includes/wordpress-importer.php';
                include $wp_import;
            }
            if (class_exists('WP_Importer') && class_exists('WP_Import')) {
                // check for main import class and wp import class
                // Content
                $importer = new WP_Import();
                $importer->fetch_attachments = false;
                $variables_dump = get_option(OM_THEME_PREFIX . 'import_process_data');
                if (!empty($variables_dump) && is_array($variables_dump)) {
                    $importer->post_orphans = $variables_dump['post_orphans'];
                    $importer->processed_posts = $variables_dump['processed_posts'];
                    $importer->url_remap = $variables_dump['url_remap'];
                }
                add_filter('wp_import_post_meta', 'om_import_modify_meta');
                ob_start();
                $importer->import($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['wordpress_xml']);
                ob_end_clean();
                om_import_menu_meta($importer->processed_menu_items);
                update_option(OM_THEME_PREFIX . 'import_process_data', false);
                // Menus to locations
                $locations = get_theme_mod('nav_menu_locations');
                $menus = wp_get_nav_menus();
                if ($menus) {
                    foreach ($menus as $menu) {
                        if (isset($GLOBALS['omImportTool']['menus'][$menu->name])) {
                            $locations[$GLOBALS['omImportTool']['menus'][$menu->name]] = $menu->term_id;
                        }
                    }
                }
                set_theme_mod('nav_menu_locations', $locations);
                // set menus to locations
                // Import Theme Options
                if (file_exists($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['theme_options_dat'])) {
                    $s = trim(file_get_contents($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['theme_options_dat']));
                    $options = @unserialize($s);
                    if (is_array($options)) {
                        $wp_upload_dir = wp_upload_dir();
                        if (isset($options['options'][OM_THEME_PREFIX . "default_title_bg_img"])) {
                            $options['options'][OM_THEME_PREFIX . 'default_title_bg_img'] = str_replace($GLOBALS['omImportTool']['uploads_replace_dir'], $wp_upload_dir['baseurl'], $options['options'][OM_THEME_PREFIX . 'default_title_bg_img']);
                        }
                        om_options_do_import_data($options);
                    }
                }
                // Widgets
                if (file_exists($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['widgets_dat'])) {
                    if (!function_exists('wie_available_widgets')) {
                        require $GLOBALS['omImportTool']['path'] . 'includes/widgets-widgets.php';
                    }
                    if (!function_exists('wie_import_data')) {
                        require $GLOBALS['omImportTool']['path'] . 'includes/widgets-import.php';
                    }
                    $data = json_decode(file_get_contents($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['widgets_dat']));
                    wie_import_data($data);
                }
                // Layer Slider
                if (isset($GLOBALS['lsPluginVersion']) || defined('LS_PLUGIN_VERSION')) {
                    if ($GLOBALS['omImportTool']['layer_slider_dat']) {
                        om_ls_import_sliders($GLOBALS['omImportTool']['path'] . $GLOBALS['omImportTool']['layer_slider_dat']);
                    }
                }
                // RevSlider
                if (class_exists('RevSlider')) {
                    if (!empty($GLOBALS['omImportTool']['rev_slider_dat'])) {
                        foreach ($GLOBALS['omImportTool']['rev_slider_dat'] as $file) {
                            if ($file) {
                                om_rev_import_slider($GLOBALS['omImportTool']['path'] . $file);
                            }
                        }
                    }
                }
                // Set reading options
                $front_page = $GLOBALS['omImportTool']['reading']['front_page_title'] ? get_page_by_title($GLOBALS['omImportTool']['reading']['front_page_title']) : false;
                $posts_page = $GLOBALS['omImportTool']['reading']['posts_page_title'] ? get_page_by_title($GLOBALS['omImportTool']['reading']['posts_page_title']) : false;
                if ($front_page || $posts_page) {
                    update_option('show_on_front', 'page');
                    if ($front_page) {
                        update_option('page_on_front', $front_page->ID);
                    }
                    if ($posts_page) {
                        update_option('page_for_posts', $posts_page->ID);
                    }
                }
            }
            wp_send_json($ret);
            break;
    }
}