mirror of
https://github.com/Frumph/comic-easel.git
synced 2026-08-24 12:02:53 -04:00
df7a6f78ba
The domain has been 'comiceasel' since 2012 while the wordpress.org slug is 'comic-easel'. Language packs from translate.wordpress.org are keyed to the slug, so they could never load; no working translation ever shipped against the old domain. Renames the domain in every gettext call, in load_plugin_textdomain(), in both Text Domain headers, and on the lang/ template files, which already carried comic-easel domain markers internally. Option keys, admin page slugs, nonce actions and style handles that share the comiceasel prefix are deliberately left alone; only the $domain argument moved. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
299 lines
14 KiB
PHP
299 lines
14 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
// Set values used (changed if returned changed)
|
|
$import_directory = 'import';
|
|
$import_time = '00:01';
|
|
$import_type = true;
|
|
$import_date_format = 'Y-m-d';
|
|
$import_filename_mask = '{DATE}*.*';
|
|
$import_create_post = false;
|
|
$import_chapter = 0;
|
|
$import_request_is_valid = false;
|
|
|
|
if (isset($_POST['_wpnonce']) && is_scalar($_POST['_wpnonce'])) {
|
|
$import_nonce = sanitize_text_field(wp_unslash($_POST['_wpnonce']));
|
|
if (wp_verify_nonce($import_nonce, 'comiceasel-import')) {
|
|
$import_request_is_valid = true;
|
|
$import_time = isset($_POST['import-time']) && is_scalar($_POST['import-time']) ? sanitize_text_field(wp_unslash($_POST['import-time'])) : $import_time;
|
|
$import_type = isset($_POST['import-type']) && is_scalar($_POST['import-type']) ? sanitize_key(wp_unslash($_POST['import-type'])) : $import_type;
|
|
$import_date_format = isset($_POST['import-date-format']) && is_scalar($_POST['import-date-format']) ? sanitize_text_field(wp_unslash($_POST['import-date-format'])) : $import_date_format;
|
|
$import_filename_mask = isset($_POST['import-date-mask']) && is_scalar($_POST['import-date-mask']) ? sanitize_text_field(wp_unslash($_POST['import-date-mask'])) : $import_filename_mask;
|
|
$import_create_post = isset($_POST['import-create-post']) && is_scalar($_POST['import-create-post']) && !empty(sanitize_text_field(wp_unslash($_POST['import-create-post'])));
|
|
$import_chapter = isset($_POST['import-chapter']) && is_scalar($_POST['import-chapter']) ? intval(wp_unslash($_POST['import-chapter'])) : $import_chapter;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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 = (is_multisite()) ? esc_url(network_home_url().'/'.$import_directory.'/'.$comic_to_add) : esc_url(home_url().'/'.$import_directory.'/'.$comic_to_add);
|
|
$comic = new WP_Http();
|
|
$comic = $comic->request( $file_url );
|
|
if (!is_wp_error($comic)) {
|
|
$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','comic-easel');
|
|
|
|
} else return $attachment->get_error_message();
|
|
} else return $comic->get_error_message();
|
|
} else return $post_id->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>'.esc_html__('Import type: by Filename w/date','comic-easel').'</strong></h2>';
|
|
$file_list = array();
|
|
if (count($results = glob(ABSPATH.$import_directory.'/*.*')) > 0) {
|
|
echo count($results).' '.esc_html__('Files found.','comic-easel')."<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).' '.esc_html__('files have valid naming and can be imported.','comic-easel');
|
|
} else {
|
|
echo esc_html__('No files found in','comic-easel').' '.esc_html(ABSPATH.$import_directory).' '.esc_html__('or directory does not exist','comic-easel').'<br />';
|
|
return;
|
|
}
|
|
echo "<hr />\r\n";
|
|
if (count($file_list)) {
|
|
$chapter_object = get_term_by('id', $import_chapter, 'chapters');
|
|
echo esc_html__('Processing Files...','comic-easel').' '.esc_html__('Importing to chapter:','comic-easel').' <strong>'.esc_html($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','comic-easel');
|
|
echo '<td>'.esc_html__('Date:','comic-easel').' <strong>'.esc_html($file_to_process['date']).'</strong></td><td> ---- </td><td>'.esc_html__('Title of Post:','comic-easel').' <strong>'.esc_html($file_to_process['converted_title']).'</strong></td><td> --- </td><td>'.esc_html__('Result:','comic-easel').' <strong>'.$result.'</strong></td>';
|
|
echo '</tr>';
|
|
}
|
|
echo '</table>';
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Catch the return $_POST and do something with them.
|
|
if ($import_request_is_valid) {
|
|
switch ($import_type) {
|
|
case 'namedate':
|
|
ceo_import_by_namedate($import_directory, $import_date_format, $import_filename_mask, $import_create_post, $import_chapter, $import_time);
|
|
break;
|
|
default:
|
|
esc_html_e('ERROR: No selection made.', 'comic-easel');
|
|
break;
|
|
}
|
|
}
|
|
|
|
?>
|
|
<div class="wrap">
|
|
<h2><?php esc_html_e('Comic Easel - Import','comic-easel'); ?></h2>
|
|
<form method="post" id="myForm-import" name="template">
|
|
<?php wp_nonce_field('comiceasel-import') ?>
|
|
<table class="widefat">
|
|
<thead>
|
|
<tr>
|
|
<th colspan="10"><?php esc_html_e('Options','comic-easel'); ?></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" />
|
|
</td>
|
|
<td valign="top" align="left" colspan="12">
|
|
<?php esc_html_e('by Date Stamp of file','comic-easel'); ?>
|
|
</td>
|
|
</tr>
|
|
*/
|
|
?>
|
|
<tr>
|
|
<td valign="top" align="right">
|
|
<input name="import-type" class="import-type" type="radio" value="namedate" <?php checked($import_type, true); ?> />
|
|
</td>
|
|
<td align="left" style="width:180px">
|
|
<?php esc_html_e('by Filename w/date','comic-easel'); ?> <cite>(*1)</cite>
|
|
</td>
|
|
<td align="left" style="width:260px">
|
|
<?php esc_html_e('Date Format','comic-easel'); ?><br />
|
|
<a href="http://codex.wordpress.org/Formatting_Date_and_Time" target="_blank"><?php esc_html_e('Documentation on date formats','comic-easel'); ?></a><br />
|
|
<input name="import-date-format" class="import-date-format" value="<?php echo esc_attr($import_date_format); ?>" />
|
|
</td>
|
|
<td align="left" style="width:240px;">
|
|
<?php esc_html_e('Filename Mask','comic-easel'); ?><br />
|
|
{DATE} <?php esc_html_e('will be replaced','comic-easel'); ?><br />
|
|
<input name="import-date-mask" class="import-date-mask" value="<?php echo esc_attr($import_filename_mask); ?>" />
|
|
</td>
|
|
<td align="left" colspan="9">
|
|
<?php esc_html_e('Set Time of Comic Posts','comic-easel'); ?><br />
|
|
<?php esc_html_e('24 hour clock','comic-easel'); ?><br />
|
|
<input name="import-time" class="import-time" value="<?php echo esc_attr($import_time); ?>" />
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td colspan="13">
|
|
<?php echo '<center><strong>'.esc_html__('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.','comic-easel').'</strong></center>'; ?>
|
|
</td>
|
|
</tr>
|
|
<?php
|
|
/*
|
|
<tr>
|
|
<td valign="top" align="right">
|
|
<input name="import-type" class="import-type" type="radio" value="numbered" disabled="disabled" />
|
|
</td>
|
|
<td align="left">
|
|
<?php esc_html_e('by Numbered filename','comic-easel'); ?>
|
|
</td>
|
|
<td align="left">
|
|
<?php esc_html_e('Filename Mask','comic-easel'); ?><br />
|
|
{NUM} <?php esc_html_e('gets replaced with the comic number, starting at 1 - use Backdate Files option', 'comic-easel'); ?><br />
|
|
<input name="import-filename-mask" class="import-filename-mask" value="comic{NUM}.*" disabled="disabled" />
|
|
</td>
|
|
<td align="left">
|
|
<input name="import-backdate" class="import-backdate" type="checkbox" value="1" checked disabled="disabled" /> <?php esc_html_e('Backdate Files', 'comic-easel'); ?><br />
|
|
<input name="import-backdate-how" class="import-backdate-how-everyday" type="radio" value="everyday" disabled="disabled" /> Everyday<br />
|
|
<input name="import-backdate-how" class="import-backdate-how-weekdays" type="radio" value="weekdays" disabled="disabled" /> Weekdays<br />
|
|
<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">
|
|
<?php esc_html_e('Import even if comic post already exists for that date.','comic-easel'); ?>
|
|
</td>
|
|
<td align="left" colspan="11">
|
|
<?php esc_html_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.', 'comic-easel'); ?>
|
|
</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 esc_html_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.', 'comic-easel'); ?>
|
|
</td>
|
|
</tr>
|
|
</thead>
|
|
</table>
|
|
<p class="submit" style="margin-left: 10px;">
|
|
<input type="submit" class="button-primary" value="<?php esc_attr_e('Import','comic-easel') ?>" />
|
|
<input type="hidden" name="action" value="ceo-import" />
|
|
</p>
|
|
<p>
|
|
<?php
|
|
echo '<h3>'.esc_html__('DIRECTIONS:','comic-easel').'</h3>';
|
|
echo '<ol>';
|
|
echo '<li>'.esc_html__('FTP into your site and create a directory named','comic-easel').' <strong>"'.esc_html($import_directory).'"</strong> '.esc_html__('off of the root installation folder of your site.','comic-easel').'</li>';
|
|
echo '<li>'.esc_html__('Upload into that directory the comics using the ComicPress filename convention for each individual chapter you are importing.','comic-easel').'</li>';
|
|
echo '<li>'.esc_html__('Make sure the chapter the comics are going into is created in the comic - chapters section of the wp-admin.','comic-easel').'</li>';
|
|
echo '<li>'.esc_html__('Select chapter in the import section, modify any other values as needed, if you do not know what they are, do not modify them.','comic-easel').'</li>';
|
|
echo '<li>'.esc_html__('Press the Import button and wish for the best.','comic-easel').'</li>';
|
|
echo '</ol>'
|
|
?>
|
|
</p>
|
|
<cite>*1</cite> - <?php esc_html_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):','comic-easel'); ?> <strong><?php esc_html_e('2012-01-01-title-of-comic.jpg','comic-easel'); ?></strong><br />
|
|
<cite>*2</cite> - <?php esc_html_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.','comic-easel'); ?><br />
|
|
</p>
|
|
<br />
|
|
<?php
|
|
echo esc_html__('Directory to scan:','comic-easel').' <strong>'.esc_html(ABSPATH.$import_directory).'</strong>'."<br />\r\n";
|
|
if (count($results = glob(ABSPATH.$import_directory.'/*.*')) > 0) {
|
|
echo count($results).' '.esc_html__('Files found.','comic-easel')."<br />\r\n";
|
|
echo "<hr />\r\n";
|
|
natcasesort($results);
|
|
foreach ($results as $filename) {
|
|
echo '<span style="width:320px;display:inline-block;float:left;">'.esc_html(basename($filename)).'</span>';
|
|
}
|
|
} else {
|
|
echo esc_html__('No files found in','comic-easel').' '.esc_html(ABSPATH.$import_directory);
|
|
}
|