/** * Function, process file at location $file, import billboards from it * Function might redirect using wp_redirect() * * @param string $file Path to the import file * * @return void */ function uds_billboard_import($file) { global $uds_billboard_errors, $uds_billboard_attributes; $import = @file_get_contents($file); if (empty($import)) { $uds_billboard_errors[] = __('Import file is empty', uds_billboard_textdomain); return; } try { libxml_use_internal_errors(true); $import = new SimpleXMLElement($import); } catch (Exception $e) { $uds_billboard_errors[] = sprintf(__('An error has occurred during XML Parsing: %s', uds_billboard_textdomain), $e->getMessage()); return; } $billboards = maybe_unserialize(get_option(UDS_BILLBOARD_OPTION, array())); foreach ($import->udsBillboards->udsBillboard as $udsBillboard) { $billboard = new uBillboard(); $billboard->importFromXML($udsBillboard); $billboards[$billboard->name] = $billboard; } if (!$billboard->isValid()) { $uds_billboard_errors[] = __('Import file is corrupted', uds_billboard_textdomain); return; } if (isset($_POST['import-attachments']) && $_POST['import-attachments'] == 'on') { foreach ($billboards as $bbname => $billboard) { foreach ($billboard->slides as $slide) { $urlinfo = parse_url($slide->image); $localurl = parse_url(get_option('siteurl')); //if($urlinfo['hostname'] == $localurl['hostname']) continue; //echo "Downloading attachment"; $image = @file_get_contents($slide->image); if (!empty($image)) { $uploads = wp_upload_dir(); if (false === $uploads['error']) { $filename = pathinfo($urlinfo['path']); $path = trailingslashit($uploads['path']) . wp_unique_filename($uploads['path'], $filename['basename']); if (!(false === @file_put_contents($path, $image))) { $filename = pathinfo($path); $slide->image = $uploads['url'] . '/' . $filename['basename']; $wp_filetype = wp_check_filetype(basename($path), null); $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($path)), 'post_content' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $path); // 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, $path); wp_update_attachment_metadata($attach_id, $attach_data); //echo "Attachment saved in ".$billboards[$bbname]['slides'][$key]->image; } else { $uds_billboard_errors[] = sprintf(__("Failed to save image to %s", uds_billboard_textdomain), $path); break; } } else { $uds_billboard_errors[] = __("Uploads dir is not writable", uds_billboard_textdomain); break; } } else { $uds_billboard_errors[] = __("Failed to download image", uds_billboard_textdomain); break; } } if (!empty($uds_billboards_errors)) { break; } } } update_option(UDS_BILLBOARD_OPTION, maybe_serialize($billboards)); if (empty($uds_billboards_errors)) { wp_redirect('admin.php?page=uds_billboard_admin'); } }
/** * Function, check for POST data and update billboard accordingly * will redirect if successful * * @return void */ function uds_billboard_process_updates() { global $uds_billboard_attributes, $uds_billboard_general_options; $post = isset($_POST['uds_billboard']) ? $_POST['uds_billboard'] : array(); if (empty($post) || !is_admin()) { return; } $billboard = new uBillboard(); $billboard->update($post); if ($billboard->isValid()) { $message = ''; if ((int) $post['regenerate-thumbs'] != 0) { if (!$billboard->createThumbs()) { $message = 'uds-message=' . urlencode(__('Failed to generate thumbnails', uds_billboard_textdomain)) . '&uds-class=' . urlencode('warning'); } } $billboards = maybe_unserialize(get_option(UDS_BILLBOARD_OPTION, array())); $billboards[$billboard->name] = $billboard; update_option(UDS_BILLBOARD_OPTION, maybe_serialize($billboards)); if (empty($message)) { $message = 'uds-message=' . urlencode(__('Billboard updated successfully', uds_billboard_textdomain)) . '&uds-class=' . urlencode('updated'); } if (is_ajax()) { die('OK'); } } else { $message = 'uds-message=' . urlencode(__('Failed to update uBillboard', uds_billboard_textdomain)) . '&uds-class=' . urlencode('error'); } if (is_ajax()) { die('ERROR'); } wp_safe_redirect(admin_url('admin.php?page=uds_billboard_edit&uds-billboard-edit=' . urlencode($billboard->name) . '&' . $message)); exit; }