-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-veriteco-timeline.php
417 lines (357 loc) · 14.6 KB
/
wp-veriteco-timeline.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/*
Plugin Name: WP VeriteCo Timeline
Plugin URI: http://www.youngjyoon.com
Description: Internalizes VeriteCo Timeline Management into WordPress
Author: Young J. Yoon
Version: 1.0
Author URI: http://www.youngjyoon.com
*/
?>
<?php
/* TIMELINE ENTRY CLASS */
class wpvtEntry
{
private $post_id;
public $startDate;
public $endDate;
public $headline;
public $text;
public $asset;
public function __construct( $post ) {
$this->post_id = $post->ID;
$meta = get_post_meta( $this->post_id );
$this->startDate = $meta['wpvt_start_date'][0];
$this->endDate = $meta['wpvt_end_date'][0];
$this->headline = get_the_title( $this->post_id );
$text = apply_filters('the_content', $post->post_content);
$text = preg_replace('/\v+|\\\[rn]/','',$text);
$text = $this->undoTexturize($text);
$this->text = $text;
$thumbnail_id = get_post_thumbnail_id( $this->post_id );
if( $thumbnail_id ) {
// if there is featured image
$img = wp_get_attachment_image_src( $thumbnail_id, 'full' );
$thumbnail_image = get_post( $thumbnail_id, 'OBJECT' );
if ($thumbnail_image && isset($thumbnail_image)) {
$this->asset->media = $img[0];
$this->asset->caption = $thumbnail_image->post_excerpt;
}
} else if( $meta['wpvt_video'][0] ) {
// otherwise, look for youtube link
$this->asset->media = $meta['wpvt_video'][0];
$this->asset->caption = $meta['wpvt_video_caption'][0];
}
}
public function toJSON() {
return json_encode($this);
}
public function undoTexturize($content, $deprecated = '') {
if ( !empty( $deprecated ) )
_deprecated_argument( __FUNCTION__, '0.71' );
// Translation of invalid Unicode references range to valid range
$wp_htmltranswinuni = array(
'–' => '-',
'—' => '—',
'’' => '\'',
'‚' => ',',
'“' => '\"',
'”' => '\"'
);
// Fix Word pasting
$content = strtr($content, $wp_htmltranswinuni);
return $content;
}
}
/* Initailaize Back-end */
function wpvt_admin_init() {
wp_register_script( 'veriteco', plugins_url('js/timeline-min.js', __FILE__) );
wp_register_script( 'wpvt_custom', plugins_url('js/wpvt_custom.js', __FILE__) );
wp_register_style( 'wpvt_css', plugins_url('css/wpvt.css', __FILE__) );
$page_title = "WP VeriteCo Timeline Configuration";
$menu_title = "WP Timeline";
$capability = "publish_posts";
$menu_slug = "wpvt_config";
$function = "wpvt_config_page";
$icon_url = "";
$position = "";
add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function );
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_script('veriteco');
wp_enqueue_script('wpvt_custom');
wp_enqueue_style('wpvt_css');
}
add_action('admin_menu', 'wpvt_admin_init');
/* Load Default Settings */
function wpvt_default_settings() {
$tmp = get_option('wpvt_options');
if(!is_array($tmp)) {
$arr = array(
'width' => '900',
'height' => '600',
'maptype' => 'toner',
'font' => 'Bevan-PotanoSans'
);
update_option('wpvt_options', $arr);
}
}
register_activation_hook(__FILE__, 'wpvt_default_settings');
/* Settings */
function wpvt_settings_init() {
$maptypes = array(
'toner' => 'Stamen Maps: Toner',
'toner-lines' => 'Stamen Maps: Toner Lines',
'toner-labels' => 'Stamen Maps: Toner Labels',
'watercolor' => 'Stamen Maps: Watercolor',
'sterrain' => 'Stamen Maps: Terrain',
'ROADMAP' => 'Google Maps: Roadmap',
'TERRAIN' => 'Google Maps: Terrain',
'HYBRID' => 'Google Maps: Hybrid',
'SATELLITE' => 'Google Maps: Satellite'
);
$fonts = array(
'Bevan-PotanoSans' => 'Bevan & Potano Sans',
'Merriweather-NewsCycle' => 'Merriweather & News Cycle',
'PoiretOne-Molengo' => 'Poiret One & Molengo',
'Arvo-PTSans' => 'Arvo & PTSans',
'PTSerif-PTSans' => 'PTSerif & PTSans',
'DroidSerif-DroidSans' => 'Droid Serif & Droid Sans',
'Lekton-Molengo' => 'Lekton & Molengo',
'NixieOne-Ledger' => 'NixieOne & Ledger',
'AbrilFatface-Average' => 'Abril Fatface & Average',
'PlayfairDisplay-Muli' => 'Playfair Display & Muli',
'Rancho-Gudea' => 'Rancho & Gudea',
'BreeSerif-OpenSans' => 'Bree Serif & Open Sans',
'SansitaOne-Kameron' => 'Sansita One & Kameron',
'Pacifico-Arimo' => 'Pacifico & Arimo',
'PT' => 'PT Sans & PT Narrow & PT Serif'
);
$types = array(
'default' => 'Default'
);
add_settings_section('wpvt_id', '', 'wpvt_callback', 'wpvt_page');
register_setting( 'wpvt_optiongroup', 'wpvt_options' ); // General Settings
/* Add fields to cover page settings */
add_settings_field('headline', 'Cover Headline', 'wpvt_setting_string', 'wpvt_page', 'wpvt_id', array('id' => 'headline', 'type' => 'text') );
add_settings_field('text', 'Cover Text', 'wpvt_setting_string', 'wpvt_page', 'wpvt_id', array('id' => 'text', 'type' => 'text') );
add_settings_field('type', 'Timeline Type', 'wpvt_setting_string', 'wpvt_page', 'wpvt_id', array('id' => 'type', 'type' => 'select', 'options' => $types ) );
/* Add fields */
add_settings_field('width', 'Width', 'wpvt_setting_string', 'wpvt_page', 'wpvt_id', array('id' => 'width', 'type' => 'text') );
add_settings_field('height', 'Height', 'wpvt_setting_string', 'wpvt_page', 'wpvt_id', array('id' => 'height', 'type' => 'text') );
add_settings_field('maptype', 'Map Type', 'wpvt_setting_string', 'wpvt_page', 'wpvt_id', array('id' => 'map', 'type' => 'select', 'options' => $maptypes ) );
add_settings_field('font', 'Fonts', 'wpvt_setting_string', 'wpvt_page', 'wpvt_id', array('id' => 'fonts', 'type' =>'select', 'options' => $fonts ) );
add_settings_field('start_at_end', 'Start at the end?', 'wpvt_setting_string', 'wpvt_page', 'wpvt_id', array('id' => 'start_at_end', 'type' => 'checkbox', 'label' => 'Yes') );
add_settings_field('hash_bookmark', 'Hash Bookmarks?', 'wpvt_setting_string', 'wpvt_page', 'wpvt_id', array('id' => 'hash_bookmark', 'type' => 'checkbox', 'label' => 'Yes') );
}
add_action('admin_init', 'wpvt_settings_init');
function wpvt_callback() { echo '<p>Adjust settings for the Timeline here.</p>'; }
function wpvt_setting_string( $args ) {
$options = get_option('wpvt_options');
$id = $args['id'];
$type = $args['type'];
switch($type) {
case 'text':
$class = ($args['class']) ? ' class="'.$args['class'].'"' : '';
echo "<input id='wpvt_".$id."' name='wpvt_options[".$id."]' type='text'". $class ." value='".$options[$id]."' />";
break;
case 'select':
$choices = $args['options'];
echo '<select id="wpvt_'.$id.'" name="wpvt_options['.$id.']">';
foreach($choices as $value => $label) {
$selected = ($options[$id] == $value) ? ' selected' : '';
echo '<option value="' . $value . '" ' . $selected . '>' . $label . '</option>';
}
echo '</select>';
break;
case 'checkbox':
$checked = ($options[$id] == '1') ? ' checked' : '';
echo '<input id="wpvt_'.$id.'" name="wpvt_options['.$id.']" type="checkbox" value="1" class="code" ' . $checked . ' /> '.$args['label'];
break;
default:
break;
}
}
/* Back-end Interface */
function wpvt_config_page() { ?>
<div class="wrap">
<div id="poststuff">
<div id="wpvt-icon"><br /></div>
<?php echo '<h1 class="wpvt-title">' . __( 'WP Veriteco Timeline Configuration', 'wpvt-config' ) . '</h1>'; ?>
<div class="clear"></div>
<div class="postbox timeline-postbox">
<h3>Timeline Settings</h3>
<div class="inside">
<form method="post" action="options.php">
<?php settings_fields( 'wpvt_optiongroup' ); ?>
<?php do_settings_sections( 'wpvt_page' ); ?>
<?php submit_button(); ?>
</form>
</div>
</div><!-- #postbox -->
</div><!-- #poststuff -->
</div>
<?php }
/* Register custom post type */
function wpvt_post_type_init() {
$labels = array(
'name' => _x('Timeline Entries', 'post type general name'),
'singular_name' => _x('Timeline Entry', 'post type singular name'),
'add_new' => _x('Add New', 'timeline'),
'add_new_item' => __('Add New Timeline Entry'),
'edit_item' => __('Edit Timeline Entry'),
'new_item' => __('New Timeline Entry'),
'all_items' => __('All Timeline Entries'),
'view_item' => __('View Timeline Entry'),
'search_items' => __('Search Timeline Entries'),
'not_found' => __('No Timeline Entries found'),
'not_found_in_trash' => __('No Timeline Entries found in Trash'),
'parent_item_colon' => '',
'menu_name' => __('Timeline')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'register_meta_box_cb' => 'wpvt_meta_boxes'
);
register_post_type( 'timeline' , $args );
wp_register_style( 'veriteco_css', plugins_url('css/timeline.css', __FILE__) );
wp_enqueue_style('veriteco_css');
}
add_action( 'init', 'wpvt_post_type_init' );
/* Metaboxes for Timeline Post Type */
function wpvt_meta_boxes() {
add_meta_box( 'timeline-meta', 'Timeline Meta Data', 'wpvt_meta_boxes_inner', 'timeline' );
}
//add_action( 'add_meta_boxes', 'wpvt_meta_boxes' );
/* Prints the box content */
function wpvt_meta_boxes_inner() {
global $post;
wp_nonce_field( plugin_basename( __FILE__ ), 'wpvt_noncename' ); // Use nonce for verification
$meta = get_post_meta($post->ID);
?>
<div class="wpvt-metabox">
<div class="wpvt-metabox-item">
<label for="wpvt_start_date">Start Date:</label>
<input type="text" id="wpvt_start_date" name="wpvtmeta[wpvt_start_date]" class="datepicker" value="<?php echo $meta['wpvt_start_date'][0]; ?>" />
</div>
<div class="wpvt-metabox-item">
<label for="wpvt_end_date">End Date:</label>
<input type="text" id="wpvt_end_date" name="wpvtmeta[wpvt_end_date]" class="datepicker" value="<?php echo $meta['wpvt_end_date'][0]; ?>" />
</div>
<div class="wpvt-metabox-item">
<label for="wpvt_video">Video Embed:</label>
<input type="text" id="wpvt_video" class="longinput" name="wpvtmeta[wpvt_video]" value="<?php echo $meta['wpvt_video'][0]; ?>" />
</div>
<div class="wpvt-metabox-item">
<label for="wpvt_video_caption">Video Caption:</label>
<input type="text" id="wpvt_video_caption" class="longinput" name="wpvtmeta[wpvt_video_caption]" value="<?php echo $meta['wpvt_video_caption'][0]; ?>" />
</div>
<input type="submit" class="button" name="wpvt_meta_submit" value="Save Timeline Data" />
</div>
<?php
}
/* Save Meta Data */
function wpvt_save_wpvt_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['wpvt_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
// Serialize and save.
$wpvt_meta = $_POST['wpvtmeta'];
// Add values of $events_meta as custom fields
foreach ($wpvt_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action('save_post', 'wpvt_save_wpvt_meta', 1, 2); // save the custom fields
/* Save JSON file */
function wpvt_update_json( $post_id ) {
global $post;
$post = get_post( $post_id );
if($post->post_type == 'timeline') {
$options = get_option('wpvt_options');
$string = '
{
"timeline":
{
"headline":"' . $options['headline'] . '",
"type":"' . $options['type'] . '",
"text":"' . $options['text'] . '",
"date": [
';
// TODO: APPEND DATE ENTRIES
$args = array( 'post_type' => 'timeline', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
//$last = ($loop->post_count <= get_option('posts_per_page')) ? $loop->post_count : get_option('posts_per_page');
while ( $loop->have_posts() ) :
$loop->the_post();
$entry = new wpvtEntry( $post );
/*
Acent dont display fix
http://wordpress.org/support/topic/accent-don-t-display-1
$string .= stripslashes($entry->toJSON());
*/
$string .= $entry->toJSON();
if($loop->current_post < $loop->post_count - 1) {
$string .= ',';
}
wp_reset_postdata();
endwhile;
$string .= '
]
}
}
';
$jsonFile = plugin_dir_path( __FILE__ ) . "/timeline.json";
file_put_contents($jsonFile, $string);
}
}
add_action('save_post', 'wpvt_update_json');
/* Shortcodes */
function wpvt_sc_func($atts) {
global $post;
$options = get_option('wpvt_options');
$start_at_end = ($options['start_at_end'] == 1) ? 'true' : 'false';
$hash_bookmark = ($options['hash_bookmark'] == 1) ? 'true' : 'false';
// NOW I JUST NEED TO FETCH ALL THE POSTS, ARRANGE THE INFO INTO JSON THEN PRINT THE JAVASCRIPT CALL.
// MAYBE GO WITH THE OPTION OF WRITING INTO A SEPARATE JSON FILE SO WE DON'T QUERY EVERY TIME.
echo '
<div id="timeline-embed"></div>
<script type="text/javascript">
var timeline_config = {
width: "'.$options['width'].'",
height: "'.$options['height'].'",
source: "'.plugins_url( 'timeline.json', __FILE__ ).'",
start_at_end: '.$start_at_end.',
hash_bookmark: '.$hash_bookmark.',
css: "'.plugins_url( 'css/themes/font/'.$options['fonts'].'.css', __FILE__ ).'" //OPTIONAL
}
</script>
<script type="text/javascript" src="' . plugins_url( 'js/timeline-embed.js', __FILE__ ).'"></script>
';
}
add_shortcode('WPVT', 'wpvt_sc_func');
?>