Introducing Comic Easel -> Import (bulk comic upload)

This commit is contained in:
Frumph
2013-04-29 13:27:50 -07:00
parent fb53ad7b36
commit bccbf663b5
4 changed files with 220 additions and 36 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ function ceo_add_menu_pages() {
// the ceo_pluginfo used here actually initiates it.
// $debug_hook = add_submenu_page($menu_location, $plugin_title . ' - ' . $debug_title, $debug_title, 'edit_theme_options', 'comiceasel-debug', 'ceo_debug');
// $import_hook = add_submenu_page($menu_location, $plugin_title . ' - ' . $import_title, $import_title, 'edit_theme_options', 'comiceasel-import', 'ceo_import');
$import_hook = add_submenu_page($menu_location, $plugin_title . ' - ' . $import_title, $import_title, 'edit_theme_options', 'comiceasel-import', 'ceo_import');
$config_hook = add_submenu_page($menu_location, $plugin_title . ' - ' . $config_title, $config_title, 'edit_theme_options', 'comiceasel-config', 'ceo_manager_config');
add_action('admin_head-' . $config_hook, 'ceo_admin_page_head');
add_action('admin_print_scripts-' . $config_hook, 'ceo_admin_print_scripts');
+199 -23
View File
@@ -1,21 +1,137 @@
<?php
function ceo_import_by_namedate($import_date_format = 'Y-m-d', $import_date_mask = '{DATE}*.*', $import_create_post = true) {
echo '<strong>'.__('Import type: by Filename w/date','comiceasel').'</strong><br />';
// Set values used (changed if returned changed)
$import_directory = 'import';
$import_time = isset($_POST['import-time']) ? esc_attr( $_POST['import-time'] ) : '00:01';
$import_type = isset($_POST['import-type']) ? esc_attr( $_POST['import-type'] ) : true;
$import_date_format = isset($_POST['import-date-format']) ? esc_attr($_POST['import-date-format']) : 'Y-m-d';
$import_filename_mask = isset($_POST['import-date-mask']) ? esc_attr($_POST['import-date-mask']) : '{DATE}*.*';
$import_create_post = (isset($_POST['import-create-post']) && !empty($_POST['import-create-post'])) ? true : false;
$import_chapter = isset($_POST['import-chapter']) ? (int)$_POST['import-chapter'] : 0;
function ceo_transform_date_string($string, $replacements) {
if (!is_array($replacements)) { return false; }
if (!is_string($string)) { return false; }
$transformed_string = $string;
foreach (array("Y", "m", "d") as $required_key) {
if (!isset($replacements[$required_key])) { return false; }
$transformed_string = preg_replace('#(?<![\\\])' . $required_key . '#', $replacements[$required_key], $transformed_string);
}
$transformed_string = str_replace('\\', '', $transformed_string);
return $transformed_string;
}
// Set Defaults or overrides if coming back from $_POST
$import_type = isset($_POST['import-type']) ? esc_attr( $_POST['import-type'] ) : '';
$import_date_format = isset($_POST['import-date-format']) ? esc_attr($_POST['import-date-format']) : 'Y-m-d';
$import_date_mask = isset($_POST['import-date-mask']) ? esc_attr($_POST['import-date-mask']) : '{DATE}*.*';
$import_create_post = (isset($_POST['import-create-post']) && $_POST['import-create-post']) ? true : false;
function ceo_breakdown_comic_filename($filename, $import_date_format, $import_filename_mask) {
foreach (array('[0-9]{4}', '[0-9]{2}') as $year_pattern) {
$new_pattern = ceo_transform_date_string($import_date_format, array("Y" => $year_pattern, "m" => '[0-9]{2}', "d" => '[0-9]{2}'));
if (@preg_match("#^(${new_pattern})(.*)\.[^\.]+$#", $filename, $matches) > 0) {
list($all, $date, $title) = $matches;
if (strtotime($date) === false) { return false; }
$converted_title = ucwords(trim(preg_replace('/[\-\_]/', ' ', $title)));
$date = date($import_date_format, strtotime($date));
if (is_numeric($converted_title)) { $converted_title = "Title: ${converted_title}"; }
return compact('filename', 'date', 'title', 'converted_title');
}
}
return false;
}
function ceo_import_add_comic_and_post($comic_to_add, $date_to_add, $title_to_add, $import_create_post, $import_chapter, $import_time, $import_directory) {
global $wpdb;
// (TODO) check to see if the post already exists THEN do wp_insert_post below // $import_create_post
// get the $timestamp set correctly
$post_date = date('Y-m-d H:i:s', strtotime($date_to_add .= " " . $import_time));
$post_args = array(
'post_name' => sanitize_title($title_to_add),
'post_title' => $title_to_add,
'post_date' => $post_date,
'post_type' => 'comic',
'post_status' => 'publish',
'tax_input' => array(
'chapters' => array($import_chapter)
)
);
$post_id = wp_insert_post($post_args);
if (!is_wp_error($post_id)) {
// Attach the Comic Now
$file_url = esc_url(home_url().'/'.$import_directory.'/'.$comic_to_add);
$comic = new WP_Http();
$comic = $comic->request( $file_url );
$comic_date = isset($comic['headers']['last-modified']) ? $comic['headers']['last-modified'] : $comic['headers']['date'];
$attachment = wp_upload_bits( $comic_to_add, null, $comic['body'], date("Y-m", strtotime( $comic_date ) ) );
if (!is_wp_error($attachment)) {
$filetype = wp_check_filetype( basename( $attachment['file'] ), null );
$postinfo_args = array(
'guid' => $attachment['file'],
'post_mime_type' => $filetype['type'],
'post_title' => 'comic-'.$comic_to_add,
'post_content' => '',
'post_status' => 'inherit',
);
$attached_filename = $attachment['file'];
$attach_id = wp_insert_attachment( $postinfo_args, $attached_filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $attached_filename );
wp_update_attachment_metadata($attach_id, $attach_data);
// set it as the post featured image
add_post_meta($post_id, '_thumbnail_id', $attach_id, true);
set_post_thumbnail($post_id, $attach_id);
return __('Comic Post made and Comic Attached','comiceasel');
} else {
return $attachment->get_error_message();
}
} else return $attachment->get_error_message();
return false;
}
function ceo_import_by_namedate($import_directory, $import_date_format, $import_filename_mask, $import_create_post, $import_chapter, $import_time) {
echo '<strong><h2>'.__('Import type: by Filename w/date','comiceasel').'</strong></h2>';
$file_list = array();
if (count($results = glob(ABSPATH.$import_directory.'/*.*')) > 0) {
echo count($results).' '.__('Files found.','comiceasel')."<br />\r\n";
natcasesort($results);
foreach ($results as $filename) {
$breakdown = ceo_breakdown_comic_filename(basename($filename), $import_date_format, $import_filename_mask);
if ($breakdown && is_array($breakdown)) $file_list[] = $breakdown;
}
echo count($file_list).' '.__('files have valid naming and can be imported.','comiceasel');
} else {
echo __('No files found in','comiceasel').' '.ABSPATH.$import_directory.' '.__('or directory does not exist','comiceasel').'<br />';
return;
}
echo "<hr />\r\n";
if (count($file_list)) {
$chapter_object = get_term_by('id', $import_chapter, 'chapters');
echo __('Processing Files...','comiceasel').' '.__('Importing to chapter:','comiceasel').' <strong>'.$chapter_object->name.'</strong><br />'."\r\n";
echo '<table>';
foreach ($file_list as $file_to_process) {
echo '<tr>';
if (empty($file_to_process['converted_title'])) $file_to_process['converted_title'] = $file_to_process['date'];
$result = ceo_import_add_comic_and_post($file_to_process['filename'], $file_to_process['date'], $file_to_process['converted_title'], $import_create_post, $import_chapter, $import_time, $import_directory);
$result = ($result) ? $result : __('System Process Error','comiceasel');
echo '<td>'.__('Date:','comiceasel').' <strong>'.$file_to_process['date'].'</strong></td><td> ---- </td><td>'.__('Title of Post:','comiceasel').' <strong>'.$file_to_process['converted_title'].'</strong></td><td> --- </td><td>'.__('Result:','comiceasel').' <strong>'.$result.'</strong></td>';
echo '</tr>';
}
echo '</table>';
}
}
// Catch the return $_POST and do something with them.
if ( isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'comiceasel-import') ) {
switch ($import_type) {
case 'namedate':
ceo_import_by_namedate($import_date_format, $import_date_mask, $import_create_post);
ceo_import_by_namedate($import_directory, $import_date_format, $import_filename_mask, $import_create_post, $import_chapter, $import_time);
break;
default:
_e('ERROR: No selection made.', 'comiceasel');
break;
}
}
@@ -30,6 +146,8 @@ if ( isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'comicease
<tr>
<th colspan="10"><?php _e('Options','cp2ce'); ?></th>
</tr>
<?php
/*
<tr>
<td valign="top" align="right" style="width:20px">
<input name="import-type" class="import-type" type="radio" value="date" disabled="disabled" />
@@ -38,9 +156,11 @@ if ( isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'comicease
<?php _e('by Date Stamp of file','comiceasel'); ?>
</td>
</tr>
*/
?>
<tr>
<td valign="top" align="right">
<input name="import-type" class="import-type" type="radio" value="namedate" <?php checked($import_type, 'namedate'); ?> />
<input name="import-type" class="import-type" type="radio" value="namedate" <?php checked($import_type, true); ?> />
</td>
<td align="left" style="width:180px">
<?php _e('by Filename w/date','comiceasel'); ?> <cite>(*1)</cite>
@@ -50,12 +170,24 @@ if ( isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'comicease
<a href="http://codex.wordpress.org/Formatting_Date_and_Time" target="_blank"><?php _e('Documentation on date formats','comiceasel'); ?></a><br />
<input name="import-date-format" class="import-date-format" value="<?php echo $import_date_format; ?>" />
</td>
<td align="left" colspan="8">
<?php _e('Date Mask','comiceasel'); ?><br />
{DATE} <?php _e('get\'s replaced with the Date Format','comiceasel'); ?><br />
<input name="import-date-mask" class="import-date-mask" value="<?php echo $import_date_mask; ?>" />
<td align="left" style="width:240px;">
<?php _e('Filename Mask','comiceasel'); ?><br />
{DATE} <?php _e('will be replaced','comiceasel'); ?><br />
<input name="import-date-mask" class="import-date-mask" value="<?php echo $import_filename_mask; ?>" />
</td>
<td align="left" colspan="9">
<?php _e('Set Time of Comic Posts','comiceasel'); ?><br />
<?php _e('24 hour clock'); ?><br />
<input name="import-time" class="import-time" value="<?php echo $import_time; ?>" />
</td>
</tr>
<tr>
<td colspan="13">
<?php echo '<center><strong>'.__('WARNING: When uploading your comics to the import directory, make sure there is only 1 comic per day, multiple comics per day will have issues.','comiceasel').'</strong></center>'; ?>
</td>
</tr>
<?php
/*
<tr>
<td valign="top" align="right">
<input name="import-type" class="import-type" type="radio" value="numbered" disabled="disabled" />
@@ -75,27 +207,71 @@ if ( isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'comicease
<input name="import-backdate-how" class="import-backdate-how-mwf" type="radio" value="mwf" disabled="disabled" /> MWF
</td>
</tr>
*/
?>
<?php
/*
<tr>
<td align="right">
<input name="import-create-post" class="import-create-post" type="checkbox" value="1" <?php checked($import_create_post, true); ?> />
</td>
<td align="left" colspan="12">
<?php _e('Create post where applicable.', 'comiceasel'); ?>
<td align="left">
<?php _e('Import even if comic post already exists for that date.','comiceasel'); ?>
</td>
</tr>
<td align="left" colspan="11">
<?php _e('Having this NOT checked (default) will make it so that if there is already a comic for that date it will not add another one.', 'comiceasel'); ?>
</td>
</tr>
*/
?>
<tr>
<td align="right">
</td>
<td align="left">
<?php
$args = array(
'name' => 'import-chapter',
'orderby' => 'name',
'order' => 'term_group',
'selected' => $import_chapter,
'hide_empty' => 0,
'hierarchical' => 1,
'pad_counts' => 1,
'taxonomy' => 'chapters',
'class' => 'chapter-list' );
wp_dropdown_categories( $args );
?>
</td>
<td align="left" colspan="11">
<?php _e('Select a chapter the comics will go in. If there are multiple chapters, then modify the comics in the import directory and only add the ones you want for the chapter selected. You need to create the chapter first in the comics - chapters area.', 'comiceasel'); ?>
</td>
</tr>
</thead>
</table>
<cite>#1</cite> <?php _e('eg, ComicPress style filenames, if Date Format is: Y-m-d and Date Mask is {DATE}*.*','comiceasel'); ?><br />
<?php _e('the filename would be (as example if image is a .jpg):','comiceasel'); ?> <strong><?php _e('2012-01-01-title-of-comic.jpg','comiceasel'); ?></strong><br />
<p class="submit" style="margin-left: 10px;">
<input type="submit" class="button-primary" value="<?php _e('Import') ?>" />
<input type="hidden" name="action" value="ceo_import" />
<input type="hidden" name="action" value="ceo-import" />
</p>
<p>
<?php
echo '<h3>'.__('DIRECTIONS:','comiceasel').'</h3>';
echo '<ol>';
echo '<li>'.__('FTP into your site and create a directory named','comiceasel').' <strong>"'.$import_directory.'"</strong> '.__('off of the root installation folder of your site.','comiceasel').'</li>';
echo '<li>'.__('Upload into that directory the comics using the ComicPress filename convention for each individual chapter you are importing.','comiceasel').'</li>';
echo '<li>'.__('Make sure the chapter the comics are going into is created in the comic - chapters section of the wp-admin.','comiceasel').'</li>';
echo '<li>'.__('Select chapter in the import section, modify any other values as needed, if you do not know what they are, do not modify them.','comiceasel').'</li>';
echo '<li>'.__('Press the Import button and wish for the best.','comiceasel').'</li>';
echo '</ol>'
?>
</p>
<cite>*1</cite> - <?php _e('eg, ComicPress style filenames, if Date Format is: Y-m-d and Date Mask is {DATE}*.* the filename would be (as example if image is a .jpg):','comiceasel'); ?> <strong><?php _e('2012-01-01-title-of-comic.jpg','comiceasel'); ?></strong><br />
<cite>*2</cite> - <?php _e('ComicPress file convention requires that each comic is its own date., do not have comics uploaded on the same date. While Comic Easel does not require each comic to have its own date, the Import does. There will be other methods of importing made in the future that this will not be an issue.','comiceasel'); ?><br />
</p>
<br />
<?php
$directory_to_check = ABSPATH.'import/';
echo __('Directory to scan: ','comiceasel').'<strong>'.$directory_to_check.'</strong>'."<br />\r\n";
if (count($results = glob($directory_to_check.'*.*')) > 0) {
echo __('Directory to scan: ','comiceasel').'<strong>'.ABSPATH.$import_directory.'</strong>'."<br />\r\n";
if (count($results = glob(ABSPATH.$import_directory.'/*.*')) > 0) {
echo count($results).' '.__('Files found.','comiceasel')."<br />\r\n";
echo "<hr />\r\n";
natcasesort($results);
@@ -103,5 +279,5 @@ if (count($results = glob($directory_to_check.'*.*')) > 0) {
echo '<span style="width:320px;display:inline-block;float:left;">'.basename($filename).'</span>';
}
} else {
echo "No files found in ".$directory_to_check;
echo "No files found in ".ABSPATH.$import_directory;
}
+16 -11
View File
@@ -3,7 +3,7 @@
Plugin Name: Comic Easel
Plugin URI: http://comiceasel.com
Description: Comic Easel allows you to incorporate a WebComic using the WordPress Media Library functionality with Navigation into almost all WordPress themes. With just a few modifications of adding injection do_action locations into a theme, you can have the theme of your choice display and manage a webcomic.
Version: 1.3.5
Version: 1.3.6
Author: Philip M. Hofer (Frumph)
Author URI: http://frumph.net/
@@ -174,6 +174,21 @@ function ceo_register_sidebars() {
add_action('widgets_init', 'ceo_register_sidebars');
/**
* This is function ceo_clean_filename
*
* @param string $filename the BASE filename
* @return string returns the rawurlencoded filename with the %2F put back to /
*
*/
function ceo_clean_filename($filename) {
return str_replace("%2F", "/", rawurlencode($filename));
}
function ceo_revert_filename($filename) {
return rawurldecode($filename);
}
// THIS STUFF ONLY RUNS IN THE WP-ADMIN
if (is_admin()) {
// only load the plugin code of we're in the administration part of WordPress.
@@ -250,16 +265,6 @@ function ceo_activation() {
// Redirects /?latest /?random etc.
@require('functions/redirects.php');
/**
* This is function ceo_clean_filename
*
* @param string $filename the BASE filename
* @return string returns the rawurlencoded filename with the %2F put back to /
*
*/
function ceo_clean_filename($filename) {
return str_replace("%2F", "/", rawurlencode($filename));
}
/**
* Get the path to the plugin folder.
+4 -1
View File
@@ -3,7 +3,7 @@ Contributors: Frumph
Tags: comiceasel, easel, webcomic, comic, webcomic
Requires at least: 3.2
Tested up to: 3.5.1
Stable tag: 1.3.5
Stable tag: 1.3.6
Donate link: http://frumph.net
License: GPLv3 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.html
@@ -126,6 +126,9 @@ The comic navigation widget is only seen if you have the comic sidebar's enabled
== Changelog ==
= 1.3.6 =
Introducing Comic Easel - Import (comic -> import in the wp-admin)
= 1.3.5 =
Added multi thumbnail plugin coding so you can have 2 images per comic, one teaser image used in the thumbnail widget and wherever else you code it in.
CSS Adjustments to some of the navigation images