eth-timeline.php 9.74 KB
Newer Older
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
<?php
/*
Plugin Name: ETH Timeline
Plugin URI: http://www.ethitter.com/plugins/
Description: List travel destinations
Author: Erick Hitter
Version: 0.1
Author URI: http://www.ethitter.com/

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

class ETH_Timeline {
	/**
	 * Singleton
	 */
	private static $instance = null;

	/**
	 * Class variables
	 */
	private $post_type = 'eth_timeline';
	// private $taxonomy = 'eth_timeline_event';

37
38
	private $meta_start = '_eth_timeline_start';
	private $meta_end = '_eth_timeline_end';
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	/**
	 * Silence is golden!
	 */
	private function __construct() {}

	/**
	 * Instantiate singleton
	 */
	public static function get_instance() {
		if ( ! is_a( self::$instance, __CLASS__ ) ) {
			self::$instance = new self;

			self::$instance->setup();
		}

		return self::$instance;
	}

	/**
	 *
	 */
	private function setup() {
		add_action( 'init', array( $this, 'action_init' ) );
63

64
65
		add_action( 'admin_enqueue_scripts', array( $this, 'action_admin_enqueue_scripts' ) );
		add_action( 'add_meta_boxes_' . $this->post_type, array( $this, 'action_add_meta_boxes' ) );
66
		add_action( 'save_post', array( $this, 'action_save_post' ) );
67

68
69
70
		add_filter( 'manage_' . $this->post_type . '_posts_columns', array( $this, 'filter_list_table_columns' ) );
		add_action( 'manage_' . $this->post_type . '_posts_custom_column', array( $this, 'do_list_table_columns' ), 10, 2 );

Erick Hitter's avatar
Erick Hitter committed
71
		add_filter( 'enter_title_here', array( $this, 'filter_editor_title_prompt' ), 10, 2 );
72
73
74
75
76
77
78
	}

	/**
	 *
	 */
	public function action_init() {
		register_post_type( $this->post_type, array(
79
			'label'               => __( 'Timeline', 'eth-timeline' ),
80
			'labels'              => array(
81
82
83
84
85
86
87
88
89
90
91
92
93
94
				'name'               => __( 'Timeline', 'eth-timeline' ),
				'singular_name'      => __( 'Timeline', 'eth-timeline' ),
				'menu_name'          => __( 'Timeline', 'eth-timeline' ),
				'all_items'          => __( 'All Entries', 'eth-timeline' ),
				'add_new'            => __( 'Add New', 'eth-timeline' ),
				'add_new_item'       => __( 'Add New', 'eth-timeline' ),
				'edit_item'          => __( 'Edit Entry', 'eth-timeline' ),
				'new_item'           => __( 'New Entry', 'eth-timeline' ),
				'view_item'          => __( 'View Entry', 'eth-timeline' ),
				'items_archive'      => __( 'Entries List', 'eth-timeline' ),
				'search_items'       => __( 'Search Timeline Entries', 'eth-timeline' ),
				'not_found'          => __( 'No entries found', 'eth-timeline' ),
				'not_found_in_trash' => __( 'No trashed entries', 'eth-timeline' ),
				'parent_item_colon'  => __( 'Entries:', 'eth-timeline' ),
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
			),
			'public'              => true,
			'has_archive'         => true,
			'exclude_from_search' => true,
			'show_in_nav_menus'   => false,
			'show_in_admin_bar'   => true,
			'rewrite'             => array(
				'slug'       => 'timeline',
				'with_front' => false
			),
			'supports'            => array(
				'title',
				'editor',
				'author',
			)
		) );

		// register_taxonomy( $this->taxonomy, $this->post_type, array(
113
		//	'label'              => __( 'Events', 'eth-timeline' ),
114
		//	'labels'             => array(
115
116
117
118
119
120
121
122
123
124
125
		//		'name'              => __( 'Events', 'eth-timeline' ),
		//		'singular_name'     => __( 'Event', 'eth-timeline' ),
		//		'search_items'      => __( 'Search Events', 'eth-timeline' ),
		//		'all_items'         => __( 'All Events', 'eth-timeline' ),
		//		'parent_item'       => __( 'Parent Event', 'eth-timeline' ),
		//		'parent_item_colon' => __( 'Parent Event:', 'eth-timeline' ),
		//		'edit_item'         => __( 'Edit Event', 'eth-timeline' ),
		//		'update_item'       => __( 'Update Event', 'eth-timeline' ),
		//		'add_new_item'      => __( 'Add New Event', 'eth-timeline' ),
		//		'new_item_name'     => __( 'New Event Name', 'eth-timeline' ),
		//		'menu_name'         => __( 'Events', 'eth-timeline' ),
126
127
128
129
130
131
132
133
134
135
136
137
138
		//	),
		//	'public'             => true,
		//	'hierarchical'       => true,
		//	'show_in_nav_menus'  => false,
		//	'show_tagcloud'      => false,
		//	'rewrite'            => array(
		//		'slug'              => $this->taxonomy,
		//		'with_front'        => false,
		//		'hierarchical'      => true
		//	)
		// ) );
	}

139
	/**
140
	 * Enqueue admin assets
141
	 *
142
143
144
145
146
147
148
	 * @uses get_current_screen
	 * @uses is_wp_error
	 * @uses wp_enqueue_script
	 * @uses plugins_url
	 * @uses wp_enqueue_style
	 * @action admin_enqueue_scripts
	 * @return null
149
	 */
150
	public function action_admin_enqueue_scripts() {
151
152
		$screen = get_current_screen();

153
		if ( is_object( $screen ) && ! is_wp_error( $screen ) && $this->post_type = $screen->post_type ) {
154
155
156
			wp_enqueue_script( 'eth-timeline-admin', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery', 'jquery-ui-datepicker' ), time(), false );

			wp_enqueue_style( 'eth-timeline-admin', plugins_url( 'css/smoothness.min.css', __FILE__ ), array(), 20130721, 'screen' );
157
158
159
160
		}
	}

	/**
161
	 * Register custom date metabox
162
	 *
163
164
165
	 * @uses add_meta_box
	 * @action add_meta_boxes
	 * @return null
166
167
168
169
170
171
	 */
	public function action_add_meta_boxes() {
		add_meta_box( 'eth-timeline-dates', __( 'Dates', 'eth-timeline' ), array( $this, 'meta_box_dates' ), $this->post_type, 'normal', 'high' );
	}

	/**
172
	 * Render dates metabox
173
	 *
174
175
176
177
178
179
180
181
	 * @param object $post
	 * @uses get_post_meta
	 * @uses _e
	 * @uses wp_nonce_field
	 * @uses ths::get_field_name
	 * @uses this::get_nonce_name
	 * @action add_meta_boxes_{$this->post_type}
	 * @return string
182
183
	 */
	public function meta_box_dates( $post ) {
184
185
186
187
188
		$start = get_post_meta( $post->ID, $this->meta_start, true );
		$start = is_numeric( $start ) ? (int) $start : '';

		$end = get_post_meta( $post->ID, $this->meta_end, true );
		$end = is_numeric( $end ) ? (int) $end : '';
189
190

		?>
191
192
		<p id="eth-timeline-startbox">
			<label for="eth-timeline-start"><?php _e( 'Start:', 'eth-timeline' ); ?></label>
193
			<input type="text" name="eth-timeline[start]" id="eth-timeline-start" class="regular-text" style="width: 11em;" value="<?php echo date( 'F j, Y', $start ); ?>" />
194
195
196
197
		</p>

		<p id="eth-timeline-endbox">
			<label for="eth-timeline-end"><?php _e( 'End:', 'eth-timeline' ); ?></label>
198
			<input type="text" name="eth-timeline[end]" id="eth-timeline-end" class="regular-text" style="width: 11em;" value="<?php echo date( 'F j, Y', $end ); ?>" />
199
		</p>
200
		<?php
201
202

		wp_nonce_field( $this->get_field_name( 'date' ), $this->get_nonce_name( 'date' ), false );
203
204
	}

205
	/**
206
	 * Save custom dates
207
	 *
208
209
210
211
212
213
214
215
	 * @param int $post_id
	 * @uses get_post_type
	 * @uses this::get_nonce_name
	 * @uses this::get_field_name
	 * @uses update_post_meta
	 * @uses delete_post_meta
	 * @action save_post
	 * @return null
216
217
218
219
220
	 */
	public function action_save_post( $post_id ) {
		if ( $this->post_type != get_post_type( $post_id ) )
			return;

221
		if ( isset( $_POST[ $this->get_nonce_name( 'date' ) ] ) && wp_verify_nonce( $_POST[ $this->get_nonce_name( 'date' ) ], $this->get_field_name( 'date' ) ) ) {
222
			$dates = isset( $_POST['eth-timeline'] ) ? $_POST['eth-timeline'] : array();
223

224
225
226
			if ( empty( $dates ) )
				return;

227
			foreach ( $dates as $key => $date ) {
228
229
230
				if ( ! in_array( $key, array( 'start', 'end' ) ) )
					continue;

231
				// Timestamp comes from JS
232
				if ( empty( $date ) )
233
					$timestamp = false;
234
235
				else
					$timestamp = strtotime( $date );
236

237
238
239
240
241
				if ( $timestamp )
					update_post_meta( $post_id, $this->{'meta_' . $key}, $timestamp );
				else
					delete_post_meta( $post_id, $this->{'meta_' . $key} );
			}
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
	/**
	 * Add new date columns to list table
	 *
	 * @param array $columns
	 * @uses __
	 * @filter manage_{$this->post_type}_posts_columns
	 * @return array
	 */
	public function filter_list_table_columns( $columns ) {
		$after = array_splice( $columns, 2 );

		$new_columns = array(
			'eth_timeline_start' => __( 'Start Date', 'eth-timeline' ),
			'eth_timeline_end' => __( 'End Date (Optional)', 'eth-timeline' ),
		);

		$columns = $columns + $new_columns + $after;

		return $columns;
	}

	/**
	 * Display start and end dates in the post list table
	 *
	 * @param string $column
	 * @param int $post_id
	 * @uses get_post_meta
	 * @uses get_option
	 * @action manage_{$this->post_type}_posts_custom_column
	 * @return string or null
	 */
	public function do_list_table_columns( $column, $post_id ) {
		if ( in_array( $column, array( 'eth_timeline_start', 'eth_timeline_end' ) ) ) {
			$key = str_replace( 'eth_timeline_', '', $column );
			$date = get_post_meta( $post_id, $this->{'meta_' . $key}, true );

			if ( is_numeric( $date ) )
				echo date( get_option( 'date_format', 'F j, Y' ), $date );
		}
	}

Erick Hitter's avatar
Erick Hitter committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
	/**
	 * Provide better prompt text for the editor title field
	 *
	 * @param string $text
	 * @param object $post
	 * @uses get_post_type
	 * @uses __
	 * @filter enter_title_here
	 * @return string
	 */
	public function filter_editor_title_prompt( $text, $post ) {
		if ( $this->post_type == get_post_type( $post ) )
			$text = __( 'Enter destination here', 'eth-timeline' );

		return $text;
	}

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
	/**
	 * Return formatted field name
	 *
	 * @param string $field
	 * @return string
	 */
	private function get_field_name( $field ) {
		return $this->post_type . '_' . $field;
	}

	/**
	 * Return formatted nonce name
	 *
	 * @param string $field
	 * @uses this::get_field_name
	 * @return string
	 */
	private function get_nonce_name( $field ) {
		return $this->get_field_name( $field ) . '_nonce';
	}
}

ETH_Timeline::get_instance();