Skip to content

Commit

Permalink
Merged WPT_Admin and WPT_Options
Browse files Browse the repository at this point in the history
Added support for past events
  • Loading branch information
slimndap committed Dec 27, 2013
1 parent 7e9f6d7 commit 576b0c1
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 168 deletions.
119 changes: 82 additions & 37 deletions functions/wpt_admin.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
<?php
class WPT_Admin {
function __construct() {
add_action( 'admin_init', function() {
wp_enqueue_script( 'wp_theatre_js', plugins_url( '../main.js', __FILE__ ), array('jquery') );
wp_enqueue_style( 'wp_theatre_css', plugins_url( '../style.css', __FILE__ ) );
wp_enqueue_script( 'jquery-ui-timepicker', plugins_url( '../js/jquery-ui-timepicker-addon.js', __FILE__ ), array('jquery-ui-datepicker','jquery-ui-slider') );
wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
});
add_action( 'admin_init', array($this,'admin_init'));
add_action( 'admin_menu', array($this, 'admin_menu' ));
add_action( 'add_meta_boxes', array($this, 'add_meta_boxes'));
add_action( 'edit_post', array( $this, 'edit_post' ));
add_action( 'delete_post',array( $this, 'delete_post' ));
add_action( 'save_post', array( $this, 'save_post' ) );

$this->options = get_option( 'wp_theatre' );
}

function admin_init() {
wp_enqueue_script( 'wp_theatre_js', plugins_url( '../main.js', __FILE__ ), array('jquery') );
wp_enqueue_style( 'wp_theatre_css', plugins_url( '../style.css', __FILE__ ) );
wp_enqueue_script( 'jquery-ui-timepicker', plugins_url( '../js/jquery-ui-timepicker-addon.js', __FILE__ ), array('jquery-ui-datepicker','jquery-ui-slider') );
wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');

register_setting(
'wp_theatre_group', // Option group
'wp_theatre' // Option name
);

add_settings_section(
'display_section_id', // ID
__('Display'), // Title
'', // Callback
'theatre-admin' // Page
);

add_settings_field(
'settings_field_show_events', // ID
__('Show events on production page.'), // Title
array( $this, 'settings_field_show_events' ), // Callback
'theatre-admin', // Page
'display_section_id' // Section
);
}

function admin_menu() {
add_menu_page( __('Theatre'), __('Theatre'), 'edit_posts', 'theatre', array($this, 'hallo'), '', 30);
add_submenu_page( 'theatre', 'Theatre '.__('Settings'), __('Settings'), 'manage_options', 'theatre-admin', array( $this, 'admin_page' ));
}

function hallo() {
Expand Down Expand Up @@ -54,35 +79,12 @@ function add_meta_boxes() {
}

function meta_box_events($production) {
$production = new WPT_Production(get_the_id());


$production = get_the_id();

if (get_post_status($production) == 'auto-draft') {
if (get_post_status($production->ID) == 'auto-draft') {
echo __('You need to save this production before you can add events.');
} else {

$args = array(
'post_type'=>WPT_Event::post_type_name,
'meta_key' => 'event_date',
'order_by' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => WPT_Production::post_type_name,
'value' => get_the_ID(),
'compare' => '=',
),
array(
'key' => 'event_date', // Check the start date field
'value' => date("Y-m-d"), // Set today's date (note the similar format)
'compare' => '>=', // Return the ones greater than today's date
'type' => 'NUMERIC,' // Let WordPress know we're working with numbers
)
),
);

$events = get_posts($args);
$events = $production->upcoming_events();
if (count($events)>0) {
echo '<ul>';
foreach ($events as $event) {
Expand All @@ -97,11 +99,27 @@ function meta_box_events($production) {
echo '</li>';

}
echo '</ul>';

}

echo '<p><a href="'.get_bloginfo('url').'/wp-admin/post-new.php?post_type='.WPT_Event::post_type_name.'&'.WPT_Production::post_type_name.'='.$production.'" class="button button-primary">'.WPT_Event::post_type()->labels->new_item.'</a></p>';
echo '</ul>';
}
$events = $production->past_events();
if (count($events)>0) {
echo '<h4>'.__('Past events').'</h4>';
echo '<ul>';
foreach ($events as $event) {
echo '<li>';
edit_post_link(
strftime('%x %X',strtotime(get_post_meta($event->ID,'event_date',true))),
'','',
$event->ID
);
echo '<br />';
echo get_post_meta($event->ID,'venue',true).', '.get_post_meta($event->ID,'city',true);
echo '</li>';

}
echo '</ul>';
}
echo '<p><a href="'.get_bloginfo('url').'/wp-admin/post-new.php?post_type='.WPT_Event::post_type_name.'&'.WPT_Production::post_type_name.'='.$production->ID.'" class="button button-primary">'.WPT_Event::post_type()->labels->new_item.'</a></p>';
}
}

Expand Down Expand Up @@ -339,6 +357,33 @@ function flush_cache() {

if (function_exists('w3tc_pgcache_flush')) { w3tc_pgcache_flush(); }
}

public function settings_field_show_events() {
printf(
'<input type="checkbox" id="show_events" name="wp_theatre[show_events]" value="yes" %s />',
(isset( $this->options['show_events'] ) && (esc_attr( $this->options['show_events'])=='yes')) ? 'checked="checked"' : ''
);
}

public function admin_page()
{
// Set class property
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Theatre <?php echo __('Settings');?></h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'wp_theatre_group' );
do_settings_sections( 'theatre-admin' );
submit_button();
?>
</form>
</div>
<?php
}

}

if (is_admin()) {
Expand Down
112 changes: 0 additions & 112 deletions functions/wpt_options.php

This file was deleted.

52 changes: 34 additions & 18 deletions functions/wpt_production.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,32 @@ function post_type() {
}

function is_upcoming() {
$events = $this->get_events();
$events = $this->upcoming_events();
return (is_array($events) && (count($events)>0));
}

function dates_short() {
$dates_short = '';
$first_datetimestamp = $last_datetimestamp = '';

$events = $this->get_events();

$events = $this->upcoming_events();
if (is_array($events) && (count($events)>0)) {

foreach ($this->get_events() as $event) {
foreach ($events as $event) {
if ($first_datetimestamp == '') {
$first_datetimestamp = strtotime($event->event_date);
}

}
$last_datetimestamp = strtotime($event->event_date);
$datetimestamp_now = strtotime('now');


if ($datetimestamp_now < $first_datetimestamp) {
if (time() < $first_datetimestamp) {
$dates_short.= strftime('%e %b.', $first_datetimestamp);
if ($last_datetimestamp != $first_datetimestamp) {
$dates_short.= ' t/m '.strftime('%e %b.', $last_datetimestamp);
$dates_short.= ' '.__('until').' '.strftime('%e %b.', $last_datetimestamp);
}
}
else {
if ($last_datetimestamp != $first_datetimestamp) {
$dates_short.= 'nog t/m '.strftime('%e %b.', $last_datetimestamp);
$dates_short.= __('until').' '.strftime('%e %b.', $last_datetimestamp);
}
}
}
Expand All @@ -82,20 +77,15 @@ function get_events() {
$args = array(
'post_type'=>WPT_Event::post_type_name,
'meta_key' => 'event_date',
'order_by' => 'meta_value_num',
'order_by' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => self::post_type_name,
'value' => $this->ID,
'compare' => '=',
),
array(
'key' => 'event_date', // Check the start date field
'value' => date("Y-m-d"), // Set today's date (note the similar format)
'compare' => '>=', // Return the ones greater than today's date
'type' => 'NUMERIC,' // Let WordPress know we're working with numbers
)
),
);
$this->events = get_posts($args);
Expand All @@ -106,6 +96,32 @@ function get_events() {
function events() {
return $this->get_events();
}

function upcoming_events() {
$events = $this->get_events();

$upcoming_events = array();
$now = time();
foreach ($events as $event) {
if (strtotime($event->event_date) >= $now) {
$upcoming_events[] = $event;
}
}
return $upcoming_events;
}

function past_events() {
$events = $this->get_events();

$past_events = array();
$now = time();
foreach ($events as $event) {
if (strtotime($event->event_date) < $now) {
$past_events[] = $event;
}
}
return $past_events;
}

function render_events() {
$html = '';
Expand Down
1 change: 0 additions & 1 deletion theatre.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
require_once(__DIR__ . '/functions/wpt_event.php');
require_once(__DIR__ . '/functions/wpt_setup.php');
require_once(__DIR__ . '/functions/wpt_admin.php');
require_once(__DIR__ . '/functions/wpt_options.php');

$wp_theatre = new WP_Theatre();

Expand Down

0 comments on commit 576b0c1

Please sign in to comment.