/
jk-comics.php
221 lines (208 loc) · 7.06 KB
/
jk-comics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Plugin Name: JK Comic Manager
* Plugin URI: http://jeffkilroy.com
* Description: Manage and view comic posts
* Version: 1.0.0
* Author: Jeffrey Kilroy
* Author URI: http://jeffkilroy.com
* License: GPL2
*/
//Require JK PostDeveloper
if(!class_exists('WPDeveloper')){
require_once __DIR__.'/classes/WPDeveloper.php';
}
if(!class_exists('Comic')){
require_once __DIR__.'/classes/Comic.php';
}
$wpDeveloper = new WPDeveloper();
$wpDeveloper->verify();
add_action( 'init', 'jkcomics_init' );
function jkcomics_init(){
jkcomics_create_comic_post_type();
jkcomics_create_comic_type_taxonomy();
//Admin Page
add_action( 'admin_menu', 'jkcomics_plugin_menu' );
//Shortcodes
add_shortcode( 'comic', 'jkcomics_display_comic' );
add_shortcode( 'comic-selector', 'jkcomics_display_comic_selector' );
//Scripts
add_action( 'wp_enqueue_scripts', 'jkcomics_scripts' );
}
function jkcomics_scripts(){
wp_enqueue_script( 'comic-selector', plugins_url( 'js/comic-selector.js', __FILE__ ) , array('jquery') );
}
function jkcomics_display_comic_selector($atts){
$a = shortcode_atts( array(
'cat' => false,
), $atts );
//
$output = array();
//
$args = array();
$args['orderby'] = 'date';
$args['order'] = 'ASC';
$term = false;
if($a['cat']){
$term = get_term_by('slug', $a['cat'], 'comic_types');
$args['tax_query'] = array(
array(
'taxonomy' => 'comic_types',
'field' => 'slug',
'terms' => $a['cat'],
),
);
}
$comics = Comic::getAll($args);
$template = new Template();
$templateResponse = $template->get(__DIR__.'/templates/comic-selector.php',array('comics'=>$comics, 'term'=>$term));
$output[] = $templateResponse;
$output = implode("\n",$output);
return $output;
}
function jkcomics_display_comic( $atts ) {
$a = shortcode_atts( array(
'id' => false,
'bar' => 'something else',
), $atts );
$output = array();
//
$comic = new Comic();
if(!$a['id']){
$comic->getMostRecent();
}else{
$comic->getByPostId($a['id']);
}
//
$categories = get_terms('comic_types');
//
$template = new Template();
$templateResponse = $template->get(__DIR__.'/templates/comic.php',array('comic'=>$comic, 'categories'=>$categories));
$output[] = $templateResponse;
$output = implode("\n",$output);
return $output;
}
function jkcomics_create_comic_post_type(){
$postType = new PostType();
$postType->name = 'jkcomic';
$postType->urlSlug = 'comic';
$postType->labelPlural = 'Comics';
$postType->labelSingular = 'Comic';
$postType->excerptTitle = 'Description';
$postType->excerptHelp = 'Comic Alt Text / Description';
$postType->iconCSSContent = '\f161';
$postType->supports = array(
'title',
'editor',
'thumbnail',
'page-attributes'
);
$postType->create();
}
function jkcomics_create_comic_type_taxonomy(){
$taxonomy = new Taxonomy();
$taxonomy->post_type = 'jkcomic';
$taxonomy->name = 'comic_types';
$taxonomy->urlSlug = 'comic-type';
$taxonomy->labelPlural = 'Comic Types';
$taxonomy->labelSingular = 'Comic Type';
$taxonomy->create();
}
//Menu
function jkcomics_plugin_menu() {
add_options_page( 'JK Comic Import', 'JK Comics Import', 'manage_options', 'jk-comics-import', 'jkcomics_import_page' );
}
//Import Page
function jkcomics_import_page() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
$output = array();
//get all comic_types
$terms = get_terms('comic_types', array('hide_empty'=>false));
//post import
if(!empty($_POST['path'])){
$path = $_POST['path'];
$categoryId = $_POST['category'];
//scan directory
if (file_exists($path)) {
$output[] = '<p><code>Directory Found...scanning files</code></p>';
$files = array();
$dh = opendir($path);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
foreach($files as $file){
if(strstr($file,'.gif')){
//import this as a post if the date does not exist
$output[] = '<p><code>'.jkcomics_import_comic_from_file($file, $categoryId, $path).'</code></p>';
}
}
//mkdir("folder/" . $dirname, 0777);
//echo "The directory $dirname was successfully created.";
} else {
$output[] = '<p><strong>Path does not exist</strong></p>';
}
}
//output
//-form
$output[] = '<div class="wrap">';
$output[] = '<form method="post">';
$output[] = '<input type="text" name="path" value="" />';
$output[] = '<select name="category">';
foreach($terms as $term){
$output[] = '<option value="'.$term->term_id.'">'.$term->name.'</option>';
}
$output[] = '</select>';
$output[] = '<input type="submit" value="Import" />';
$output[] = '</form>';
$output[] = '</div>';
//-import response
//
$output = implode("\n", $output);
echo $output;
}
function jkcomics_import_comic_from_file($file, $categoryId, $path){
$output = array();
$fullFilePath = $path.'/'.$file;
$output[] = $fullFilePath;
//$output[] = $file;
$fileWithoutExtension = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
$post_filename_parts = explode('_',$fileWithoutExtension);
if(count($post_filename_parts) != 2){
$output[] = 'file error';
return implode("\n", $output);
}
$comic_date = array('y'=>'','m'=>'','d'=>'');
$comic_date['y'] = $post_filename_parts[0];
$comic_date['m'] = substr($post_filename_parts[1], 0, -2);
$comic_date['m'] = sprintf('%02d', $comic_date['m']);
$comic_date['d'] = substr($post_filename_parts[1], -2);
$comic_date['d'] = sprintf('%02d', $comic_date['d']);
$comic_title = $comic_date['m'].'-'.$comic_date['d'].'-'.$comic_date['y'];
//see if comic exists
$existingPage = get_page_by_title( $comic_title, 'OBJECT', 'jkcomic' );
if($existingPage){
$output[] = $comic_title.' post exists';
}else{
$output[] = $comic_title.' post does not exist';
$post = new Post();
$post->type = 'jkcomic';
$post->title = $comic_title;
$post->setDate($comic_date['y'].'-'.$comic_date['m'].'-'.$comic_date['d']);
$existingPage = $post->insert();
}
if($existingPage) {
//Attach taxonomy
wp_set_post_terms( $existingPage->ID, array($categoryId), 'comic_types' );
//Attach image
$existingAttachmentId = get_post_meta( $existingPage->ID, '_thumbnail_id', true );
$output[] = '<br/>'.$existingAttachmentId.'<br/>';
if(empty($existingAttachmentId)) {
$attachment = new Attachment();
$output[] = $attachment->uploadFeaturedImage($fullFilePath,$existingPage->ID);
}
}
//return
return implode("\n", $output);
}